Home | History | Annotate | Line # | Download | only in ppc
emul_generic.c revision 1.1.1.4
      1 /*  This file is part of the program psim.
      2 
      3     Copyright (C) 1994-1997, Andrew Cagney <cagney (at) 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 uint64_t
     62 emul_read_gpr64(cpu *processor,
     63 		int g)
     64 {
     65   uint32_t hi;
     66   uint32_t lo;
     67   if (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) {
     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 		 uint64_t val)
     83 {
     84   uint32_t hi = EXTRACTED64(val, 0, 31);
     85   uint32_t lo = EXTRACTED64(val, 32, 63);
     86   if (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) {
     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 err)
    124 {
    125   if (status == -1 && err != 0) {
    126     cpu_registers(processor)->gpr[3] = err;
    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 err)
    141 {
    142   if (status1 == -1 && err != 0) {
    143     cpu_registers(processor)->gpr[3] = err;
    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     little_endian = (image != NULL && bfd_little_endian(image));
    266     tree_parse(tree, "/options/little-endian? %s",
    267 	       little_endian ? "true" : "false");
    268   }
    269 
    270   /* misc other stuff */
    271   tree_parse(tree, "/openprom/options/oea-memory-size 0x%x",
    272 	     OEA_MEMORY_SIZE);
    273   tree_parse(tree, "/openprom/options/oea-interrupt-prefix %d",
    274 	     oea_interrupt_prefix);
    275   tree_parse(tree, "/openprom/options/smp 1");
    276   tree_parse(tree, "/openprom/options/env %s", env);
    277   tree_parse(tree, "/openprom/options/os-emul %s", emul);
    278   tree_parse(tree, "/openprom/options/strict-alignment? %s",
    279 	     (WITH_ALIGNMENT == STRICT_ALIGNMENT)
    280 	     ? "true" : "false");
    281   tree_parse(tree, "/openprom/options/floating-point? %s",
    282 	     WITH_FLOATING_POINT ? "true" : "false");
    283   tree_parse(tree, "/openprom/options/use-stdio? %s",
    284 	     ((WITH_STDIO == DO_USE_STDIO
    285 	       || WITH_STDIO == 0)
    286 	      ? "true" : "false"));
    287   tree_parse(tree, "/openprom/options/model \"%s",
    288 	     model_name[WITH_DEFAULT_MODEL]);
    289   tree_parse(tree, "/openprom/options/model-issue %d",
    290 	     MODEL_ISSUE_IGNORE);
    291 
    292   /* useful options */
    293 }
    294 
    295 INLINE_EMUL_GENERIC void
    296 emul_add_tree_hardware(device *root)
    297 {
    298   int i;
    299   int nr_cpus = tree_find_integer_property(root, "/openprom/options/smp");
    300 
    301   /* sanity check the number of processors */
    302   if (nr_cpus > MAX_NR_PROCESSORS)
    303     error("Specified number of processors (%d) exceeds the number configured (%d).\n",
    304 	  nr_cpus, MAX_NR_PROCESSORS);
    305 
    306   /* set the number of address cells (1 or 2) */
    307   tree_parse(root, "#address-cells %d", WITH_TARGET_WORD_BITSIZE / 32);
    308 
    309   /* add some memory */
    310   if (tree_find_device(root, "/memory") == NULL) {
    311     unsigned_word memory_size =
    312       tree_find_integer_property(root, "/openprom/options/oea-memory-size");
    313     const unsigned_word avail_start = 0x3000;
    314     tree_parse(root, "/memory@0/reg 0x0 0x%lx",
    315 	       (unsigned long)memory_size);
    316     /* reserve the first 0x3000 for the PowerPC interrupt table */
    317     tree_parse(root, "/memory@0/available 0x%lx  0x%lx",
    318 	       (unsigned long)avail_start,
    319 	       (unsigned long)memory_size - avail_start);
    320   }
    321 
    322   /* our processors */
    323   for (i = 0; i < nr_cpus; i++) {
    324     tree_parse(root, "/cpus/cpu@%d/cpu-nr %d", i, i);
    325   }
    326 
    327   /* the debugging pal - hide it in the openprom and don't attach it
    328      to any bus */
    329   tree_parse(root, "/openprom/pal");
    330 
    331   /* chosen etc */
    332   tree_parse(root, "/chosen/stdin */openprom/pal");
    333   tree_parse(root, "/chosen/stdout !/chosen/stdin");
    334   tree_parse(root, "/chosen/memory */memory");
    335 }
    336 
    337 #endif /* _EMUL_GENERIC_C_ */
    338