sim: ppc: do not exit when parsing args w/gdb
[external/binutils.git] / sim / ppc / emul_generic.c
1 /*  This file is part of the program psim.
2
3     Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, see <http://www.gnu.org/licenses/>.
17
18     */
19
20
21 #ifndef _EMUL_GENERIC_C_
22 #define _EMUL_GENERIC_C_
23
24 #include "emul_generic.h"
25
26 #ifndef STATIC_INLINE_EMUL_GENERIC
27 #define STATIC_INLINE_EMUL_GENERIC STATIC_INLINE
28 #endif
29
30
31 STATIC_INLINE_EMUL_GENERIC void
32 emul_syscall_enter(emul_syscall *emul,
33                    int call,
34                    int arg0,
35                    cpu *processor,
36                    unsigned_word cia)
37 {
38   printf_filtered("%d:0x%lx:%s(",
39                   cpu_nr(processor) + 1,
40                   (long)cia,
41                   emul->syscall_descriptor[call].name);
42 }
43
44
45 STATIC_INLINE_EMUL_GENERIC void
46 emul_syscall_exit(emul_syscall *emul,
47                   int call,
48                   int arg0,
49                   cpu *processor,
50                   unsigned_word cia)
51 {
52   int status = cpu_registers(processor)->gpr[3];
53   int error = cpu_registers(processor)->gpr[0];
54   printf_filtered(")=%d", status);
55   if (error > 0 && error < emul->nr_error_names)
56     printf_filtered("[%s]", emul->error_names[error]);
57   printf_filtered("\n");
58 }
59
60
61 INLINE_EMUL_GENERIC unsigned64
62 emul_read_gpr64(cpu *processor,
63                 int g)
64 {
65   unsigned32 hi;
66   unsigned32 lo;
67   if (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN) {
68     hi = cpu_registers(processor)->gpr[g];
69     lo = cpu_registers(processor)->gpr[g+1];
70   }
71   else {
72     lo = cpu_registers(processor)->gpr[g];
73     hi = cpu_registers(processor)->gpr[g+1];
74   }
75   return (INSERTED64(hi, 0, 31) | INSERTED64(lo, 32, 63));
76 }
77
78
79 INLINE_EMUL_GENERIC void
80 emul_write_gpr64(cpu *processor,
81                  int g,
82                  unsigned64 val)
83 {
84   unsigned32 hi = EXTRACTED64(val, 0, 31);
85   unsigned32 lo = EXTRACTED64(val, 32, 63);
86   if (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN) {
87     cpu_registers(processor)->gpr[g] = hi;
88     cpu_registers(processor)->gpr[g+1] = lo;
89   }
90   else {
91     cpu_registers(processor)->gpr[g] = lo;
92     cpu_registers(processor)->gpr[g+1] = hi;
93   }
94 }
95
96
97 INLINE_EMUL_GENERIC char *
98 emul_read_string(char *dest,
99                  unsigned_word addr,
100                  unsigned nr_bytes,
101                  cpu *processor,
102                  unsigned_word cia)
103 {
104   unsigned nr_moved = 0;
105   if (addr == 0)
106     return NULL;
107   while (1) {
108     dest[nr_moved] = vm_data_map_read_1(cpu_data_map(processor),
109                                         addr + nr_moved,
110                                         processor, cia);
111     if (dest[nr_moved] == '\0' || nr_moved >= nr_bytes)
112       break;
113     nr_moved++;
114   }
115   dest[nr_moved] = '\0';
116   return dest;
117 }
118
119
120 INLINE_EMUL_GENERIC void
121 emul_write_status(cpu *processor,
122                   int status,
123                   int errno)
124 {
125   if (status == -1 && errno != 0) {
126     cpu_registers(processor)->gpr[3] = errno;
127     CR_SET(0, cr_i_summary_overflow);
128   }
129   else {
130     cpu_registers(processor)->gpr[3] = status;
131     CR_SET(0, 0);
132   }
133 }
134
135
136 INLINE_EMUL_GENERIC void
137 emul_write2_status(cpu *processor,
138                    int status1,
139                    int status2,
140                    int errno)
141 {
142   if (status1 == -1 && errno != 0) {
143     cpu_registers(processor)->gpr[3] = errno;
144     CR_SET(0, cr_i_summary_overflow);
145   }
146   else {
147     cpu_registers(processor)->gpr[3] = status1;
148     cpu_registers(processor)->gpr[4] = status2;
149     CR_SET(0, 0);
150   }
151 }
152
153
154 INLINE_EMUL_GENERIC unsigned_word
155 emul_read_word(unsigned_word addr,
156                cpu *processor,
157                unsigned_word cia)
158 {
159   return vm_data_map_read_word(cpu_data_map(processor),
160                                addr,
161                                processor, cia);
162 }
163
164
165 INLINE_EMUL_GENERIC void
166 emul_write_word(unsigned_word addr,
167                 unsigned_word buf,
168                 cpu *processor,
169                 unsigned_word cia)
170 {
171   vm_data_map_write_word(cpu_data_map(processor),
172                          addr,
173                          buf,
174                          processor, cia);
175 }
176
177
178 INLINE_EMUL_GENERIC void
179 emul_write_buffer(const void *source,
180                   unsigned_word addr,
181                   unsigned nr_bytes,
182                   cpu *processor,
183                   unsigned_word cia)
184 {
185   int nr_moved;
186   for (nr_moved = 0; nr_moved < nr_bytes; nr_moved++) {
187     vm_data_map_write_1(cpu_data_map(processor),
188                         addr + nr_moved,
189                         ((const char*)source)[nr_moved],
190                         processor, cia);
191   }
192 }
193
194
195 INLINE_EMUL_GENERIC void
196 emul_read_buffer(void *dest,
197                  unsigned_word addr,
198                  unsigned nr_bytes,
199                  cpu *processor,
200                  unsigned_word cia)
201 {
202   int nr_moved;
203   for (nr_moved = 0; nr_moved < nr_bytes; nr_moved++) {
204     ((char*)dest)[nr_moved] = vm_data_map_read_1(cpu_data_map(processor),
205                                                  addr + nr_moved,
206                                                  processor, cia);
207   }
208 }
209
210
211 INLINE_EMUL_GENERIC void
212 emul_do_system_call(os_emul_data *emul_data,
213                     emul_syscall *emul,
214                     unsigned call,
215                     const int arg0,
216                     cpu *processor,
217                     unsigned_word cia)
218 {
219   emul_syscall_handler *handler = NULL;
220   if (call >= emul->nr_system_calls)
221     error("do_call() os_emul call %d out-of-range\n", call);
222
223   handler = emul->syscall_descriptor[call].handler;
224   if (handler == NULL) {
225     if (emul->syscall_descriptor[call].name) {
226       error("do_call() unimplemented call %s\n", emul->syscall_descriptor[call].name);
227     } else {
228       error("do_call() unimplemented call %d\n", call);
229     }
230   }
231
232   if (WITH_TRACE && ppc_trace[trace_os_emul])
233     emul_syscall_enter(emul, call, arg0, processor, cia);
234
235   cpu_registers(processor)->gpr[0] = 0; /* default success */
236   handler(emul_data, call, arg0, processor, cia);
237
238   if (WITH_TRACE && ppc_trace[trace_os_emul])
239     emul_syscall_exit(emul, call, arg0, processor, cia);
240 }
241
242
243 /* default size for the first bank of memory */
244
245 #ifndef OEA_MEMORY_SIZE
246 #define OEA_MEMORY_SIZE 0x100000
247 #endif
248
249
250 /* Add options to the device tree */
251
252 INLINE_EMUL_GENERIC void
253 emul_add_tree_options(device *tree,
254                       bfd *image,
255                       const char *emul,
256                       const char *env,
257                       int oea_interrupt_prefix)
258 {
259   int little_endian = 0;
260
261   /* sort out little endian */
262   if (tree_find_property(tree, "/options/little-endian?"))
263     little_endian = tree_find_boolean_property(tree, "/options/little-endian?");
264   else {
265 #ifdef bfd_little_endian        /* new bfd */
266     little_endian = (image != NULL && bfd_little_endian(image));
267 #else
268     little_endian = (image != NULL &&
269                      !image->xvec->byteorder_big_p);
270 #endif
271     tree_parse(tree, "/options/little-endian? %s",
272                little_endian ? "true" : "false");
273   }
274
275   /* misc other stuff */
276   tree_parse(tree, "/openprom/options/oea-memory-size 0x%x",
277              OEA_MEMORY_SIZE);
278   tree_parse(tree, "/openprom/options/oea-interrupt-prefix %d",
279              oea_interrupt_prefix);
280   tree_parse(tree, "/openprom/options/smp 1");
281   tree_parse(tree, "/openprom/options/env %s", env);
282   tree_parse(tree, "/openprom/options/os-emul %s", emul);
283   tree_parse(tree, "/openprom/options/strict-alignment? %s",
284              (WITH_ALIGNMENT == STRICT_ALIGNMENT)
285              ? "true" : "false");
286   tree_parse(tree, "/openprom/options/floating-point? %s",
287              WITH_FLOATING_POINT ? "true" : "false");
288   tree_parse(tree, "/openprom/options/use-stdio? %s",
289              ((WITH_STDIO == DO_USE_STDIO
290                || WITH_STDIO == 0)
291               ? "true" : "false"));
292   tree_parse(tree, "/openprom/options/model \"%s",
293              model_name[WITH_DEFAULT_MODEL]);
294   tree_parse(tree, "/openprom/options/model-issue %d",
295              MODEL_ISSUE_IGNORE);
296
297   /* useful options */
298 }
299
300 INLINE_EMUL_GENERIC void
301 emul_add_tree_hardware(device *root)
302 {
303   int i;
304   int nr_cpus = tree_find_integer_property(root, "/openprom/options/smp");
305
306   /* sanity check the number of processors */
307   if (nr_cpus > MAX_NR_PROCESSORS)
308     error("Specified number of processors (%d) exceeds the number configured (%d).\n",
309           nr_cpus, MAX_NR_PROCESSORS);
310
311   /* set the number of address cells (1 or 2) */
312   tree_parse(root, "#address-cells %d", WITH_TARGET_WORD_BITSIZE / 32);
313
314   /* add some memory */
315   if (tree_find_device(root, "/memory") == NULL) {
316     unsigned_word memory_size =
317       tree_find_integer_property(root, "/openprom/options/oea-memory-size");
318     const unsigned_word avail_start = 0x3000;
319     tree_parse(root, "/memory@0/reg 0x0 0x%lx",
320                (unsigned long)memory_size);
321     /* reserve the first 0x3000 for the PowerPC interrupt table */
322     tree_parse(root, "/memory@0/available 0x%lx  0x%lx",
323                (unsigned long)avail_start,
324                (unsigned long)memory_size - avail_start);
325   }
326
327   /* our processors */
328   for (i = 0; i < nr_cpus; i++) {
329     tree_parse(root, "/cpus/cpu@%d/cpu-nr %d", i, i);
330   }
331
332   /* the debugging pal - hide it in the openprom and don't attach it
333      to any bus */
334   tree_parse(root, "/openprom/pal");
335
336   /* chosen etc */
337   tree_parse(root, "/chosen/stdin */openprom/pal");
338   tree_parse(root, "/chosen/stdout !/chosen/stdin");
339   tree_parse(root, "/chosen/memory */memory");
340 }
341
342 #endif /* _EMUL_GENERIC_C_ */