/[gxemul]/trunk/src/memory.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/src/memory.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 18 by dpavlin, Mon Oct 8 16:19:11 2007 UTC revision 22 by dpavlin, Mon Oct 8 16:19:37 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2003-2005  Anders Gavare.  All rights reserved.   *  Copyright (C) 2003-2006  Anders Gavare.  All rights reserved.
3   *   *
4   *  Redistribution and use in source and binary forms, with or without   *  Redistribution and use in source and binary forms, with or without
5   *  modification, are permitted provided that the following conditions are met:   *  modification, are permitted provided that the following conditions are met:
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: memory.c,v 1.180 2005/10/25 15:51:02 debug Exp $   *  $Id: memory.c,v 1.187 2006/01/14 12:51:59 debug Exp $
29   *   *
30   *  Functions for handling the memory of an emulated machine.   *  Functions for handling the memory of an emulated machine.
31   */   */
# Line 36  Line 36 
36  #include <sys/types.h>  #include <sys/types.h>
37  #include <sys/mman.h>  #include <sys/mman.h>
38    
 #include "bintrans.h"  
 #include "cop0.h"  
39  #include "cpu.h"  #include "cpu.h"
40  #include "machine.h"  #include "machine.h"
41  #include "memory.h"  #include "memory.h"
 #include "mips_cpu_types.h"  
42  #include "misc.h"  #include "misc.h"
43    
44    
45  extern int quiet_mode;  extern int verbose;
 extern volatile int single_step;  
46    
47    
48  /*  /*
# Line 59  extern volatile int single_step; Line 55  extern volatile int single_step;
55   */   */
56  uint64_t memory_readmax64(struct cpu *cpu, unsigned char *buf, int len)  uint64_t memory_readmax64(struct cpu *cpu, unsigned char *buf, int len)
57  {  {
58          int i;          int i, byte_order = cpu->byte_order;
59          uint64_t x = 0;          uint64_t x = 0;
60    
61            if (len & MEM_PCI_LITTLE_ENDIAN) {
62                    len &= ~MEM_PCI_LITTLE_ENDIAN;
63                    byte_order = EMUL_LITTLE_ENDIAN;
64            }
65    
66          /*  Switch byte order for incoming data, if necessary:  */          /*  Switch byte order for incoming data, if necessary:  */
67          if (cpu->byte_order == EMUL_BIG_ENDIAN)          if (byte_order == EMUL_BIG_ENDIAN)
68                  for (i=0; i<len; i++) {                  for (i=0; i<len; i++) {
69                          x <<= 8;                          x <<= 8;
70                          x |= buf[i];                          x |= buf[i];
# Line 89  uint64_t memory_readmax64(struct cpu *cp Line 90  uint64_t memory_readmax64(struct cpu *cp
90  void memory_writemax64(struct cpu *cpu, unsigned char *buf, int len,  void memory_writemax64(struct cpu *cpu, unsigned char *buf, int len,
91          uint64_t data)          uint64_t data)
92  {  {
93          int i;          int i, byte_order = cpu->byte_order;
94    
95          if (cpu->byte_order == EMUL_LITTLE_ENDIAN)          if (len & MEM_PCI_LITTLE_ENDIAN) {
96                    len &= ~MEM_PCI_LITTLE_ENDIAN;
97                    byte_order = EMUL_LITTLE_ENDIAN;
98            }
99    
100            if (byte_order == EMUL_LITTLE_ENDIAN)
101                  for (i=0; i<len; i++) {                  for (i=0; i<len; i++) {
102                          buf[i] = data & 255;                          buf[i] = data & 255;
103                          data >>= 8;                          data >>= 8;
# Line 184  struct memory *memory_new(uint64_t physi Line 190  struct memory *memory_new(uint64_t physi
190  /*  /*
191   *  memory_points_to_string():   *  memory_points_to_string():
192   *   *
193   *  Returns 1 if there's something string-like at addr, otherwise 0.   *  Returns 1 if there's something string-like in emulated memory at address
194     *  addr, otherwise 0.
195   */   */
196  int memory_points_to_string(struct cpu *cpu, struct memory *mem, uint64_t addr,  int memory_points_to_string(struct cpu *cpu, struct memory *mem, uint64_t addr,
197          int min_string_length)          int min_string_length)
# Line 213  int memory_points_to_string(struct cpu * Line 220  int memory_points_to_string(struct cpu *
220  /*  /*
221   *  memory_conv_to_string():   *  memory_conv_to_string():
222   *   *
223   *  Convert virtual memory contents to a string, placing it in a   *  Convert emulated memory contents to a string, placing it in a buffer
224   *  buffer provided by the caller.   *  provided by the caller.
225   */   */
226  char *memory_conv_to_string(struct cpu *cpu, struct memory *mem, uint64_t addr,  char *memory_conv_to_string(struct cpu *cpu, struct memory *mem, uint64_t addr,
227          char *buf, int bufsize)          char *buf, int bufsize)
# Line 258  char *memory_conv_to_string(struct cpu * Line 265  char *memory_conv_to_string(struct cpu *
265  /*  /*
266   *  memory_device_dyntrans_access():   *  memory_device_dyntrans_access():
267   *   *
268   *  Get the lowest and highest dyntrans (or bintrans) access since last time.   *  Get the lowest and highest dyntrans access since last time.
269   */   */
270  void memory_device_dyntrans_access(struct cpu *cpu, struct memory *mem,  void memory_device_dyntrans_access(struct cpu *cpu, struct memory *mem,
271          void *extra, uint64_t *low, uint64_t *high)          void *extra, uint64_t *low, uint64_t *high)
# Line 273  void memory_device_dyntrans_access(struc Line 280  void memory_device_dyntrans_access(struc
280    
281          for (i=0; i<mem->n_mmapped_devices; i++) {          for (i=0; i<mem->n_mmapped_devices; i++) {
282                  if (mem->dev_extra[i] == extra &&                  if (mem->dev_extra[i] == extra &&
283                        mem->dev_flags[i] & DM_DYNTRANS_WRITE_OK &&
284                      mem->dev_dyntrans_data[i] != NULL) {                      mem->dev_dyntrans_data[i] != NULL) {
285                          if (mem->dev_dyntrans_write_low[i] != (uint64_t) -1)                          if (mem->dev_dyntrans_write_low[i] != (uint64_t) -1)
286                                  need_inval = 1;                                  need_inval = 1;
# Line 326  void memory_device_dyntrans_access(struc Line 334  void memory_device_dyntrans_access(struc
334    
335    
336  /*  /*
  *  memory_device_register_statefunction():  
  *  
  *  TODO: Hm. This is semi-ugly. Should probably be rewritten/redesigned  
  *  some day.  
  */  
 void memory_device_register_statefunction(  
         struct memory *mem, void *extra,  
         int (*dev_f_state)(struct cpu *,  
             struct memory *, void *extra, int wf, int nr,  
             int *type, char **namep, void **data, size_t *len))  
 {  
         int i;  
   
         for (i=0; i<mem->n_mmapped_devices; i++)  
                 if (mem->dev_extra[i] == extra) {  
                         mem->dev_f_state[i] = dev_f_state;  
                         return;  
                 }  
   
         printf("memory_device_register_statefunction(): "  
             "couldn't find the device\n");  
         exit(1);  
 }  
   
   
 /*  
337   *  memory_device_register():   *  memory_device_register():
338   *   *
339   *  Register a (memory mapped) device by adding it to the dev_* fields of a   *  Register a (memory mapped) device by adding it to the dev_* fields of a
# Line 363  void memory_device_register(struct memor Line 345  void memory_device_register(struct memor
345                  size_t,int,void *),                  size_t,int,void *),
346          void *extra, int flags, unsigned char *dyntrans_data)          void *extra, int flags, unsigned char *dyntrans_data)
347  {  {
348          int i;          int i, newi = 0;
349    
350          if (mem->n_mmapped_devices >= MAX_DEVICES) {          if (mem->n_mmapped_devices >= MAX_DEVICES) {
351                  fprintf(stderr, "memory_device_register(): too many "                  fprintf(stderr, "memory_device_register(): too many "
# Line 371  void memory_device_register(struct memor Line 353  void memory_device_register(struct memor
353                  exit(1);                  exit(1);
354          }          }
355    
356          /*  Check for collisions:  */          /*
357             *  Figure out at which index to insert this device, and simultaneously
358             *  check for collisions:
359             */
360            newi = -1;
361          for (i=0; i<mem->n_mmapped_devices; i++) {          for (i=0; i<mem->n_mmapped_devices; i++) {
362                    if (i == 0 && baseaddr + len <= mem->dev_baseaddr[i])
363                            newi = i;
364                    if (i > 0 && baseaddr + len <= mem->dev_baseaddr[i] &&
365                        baseaddr >= mem->dev_endaddr[i-1])
366                            newi = i;
367                    if (i == mem->n_mmapped_devices - 1 &&
368                        baseaddr >= mem->dev_endaddr[i])
369                            newi = i + 1;
370    
371                  /*  If we are not colliding with device i, then continue:  */                  /*  If we are not colliding with device i, then continue:  */
372                  if (baseaddr + len <= mem->dev_baseaddr[i])                  if (baseaddr + len <= mem->dev_baseaddr[i])
373                          continue;                          continue;
374                  if (baseaddr >= mem->dev_baseaddr[i] + mem->dev_length[i])                  if (baseaddr >= mem->dev_endaddr[i])
375                          continue;                          continue;
376    
377                  fatal("\nWARNING! \"%s\" collides with device %i (\"%s\")!\n"                  fatal("\nERROR! \"%s\" collides with device %i (\"%s\")!\n",
                     "         Run-time behaviour will be undefined!\n\n",  
378                      device_name, i, mem->dev_name[i]);                      device_name, i, mem->dev_name[i]);
379                    exit(1);
380            }
381            if (mem->n_mmapped_devices == 0)
382                    newi = 0;
383            if (newi == -1) {
384                    fatal("INTERNAL ERROR\n");
385                    exit(1);
386            }
387    
388            if (verbose >= 2) {
389                    /*  (40 bits of physical address is displayed)  */
390                    debug("device at 0x%010llx: %s", (long long)baseaddr,
391                        device_name);
392    
393                    if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)
394                        && (baseaddr & mem->dev_dyntrans_alignment) != 0) {
395                            fatal("\nWARNING: Device dyntrans access, but unaligned"
396                                " baseaddr 0x%llx.\n", (long long)baseaddr);
397                    }
398    
399                    if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) {
400                            debug(" (dyntrans %s)",
401                                (flags & DM_DYNTRANS_WRITE_OK)? "R/W" : "R");
402                    }
403                    debug("\n");
404          }          }
405    
406          /*  (40 bits of physical address is displayed)  */          for (i=0; i<mem->n_mmapped_devices; i++) {
407          debug("device %2i at 0x%010llx: %s",                  if (dyntrans_data == mem->dev_dyntrans_data[i] &&
408              mem->n_mmapped_devices, (long long)baseaddr, device_name);                      mem->dev_flags[i] & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)
409                        && flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) {
410          if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK)                          fatal("ERROR: the data pointer used for dyntrans "
411              && (baseaddr & mem->dev_dyntrans_alignment) != 0) {                              "accesses must only be used once!\n");
412                  fatal("\nWARNING: Device dyntrans access, but unaligned"                          fatal("(%p cannot be used by '%s'; already in use by '"
413                      " baseaddr 0x%llx.\n", (long long)baseaddr);                              "%s')\n", dyntrans_data, device_name,
414          }                              mem->dev_name[i]);
415                            exit(1);
416          if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK)) {                  }
417                  debug(" (dyntrans %s)",          }
                     (flags & MEM_DYNTRANS_WRITE_OK)? "R/W" : "R");  
         }  
         debug("\n");  
   
         mem->dev_name[mem->n_mmapped_devices] = strdup(device_name);  
         mem->dev_baseaddr[mem->n_mmapped_devices] = baseaddr;  
         mem->dev_endaddr[mem->n_mmapped_devices] = baseaddr + len;  
         mem->dev_length[mem->n_mmapped_devices] = len;  
         mem->dev_flags[mem->n_mmapped_devices] = flags;  
         mem->dev_dyntrans_data[mem->n_mmapped_devices] = dyntrans_data;  
418    
419          if (mem->dev_name[mem->n_mmapped_devices] == NULL) {          mem->n_mmapped_devices++;
420    
421            /*
422             *  YUCK! This is ugly. TODO: fix
423             */
424            /*  Make space for the new entry:  */
425            memmove(&mem->dev_name[newi+1], &mem->dev_name[newi], sizeof(char *) *
426                (MAX_DEVICES - newi - 1));
427            memmove(&mem->dev_baseaddr[newi+1], &mem->dev_baseaddr[newi],
428                sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
429            memmove(&mem->dev_endaddr[newi+1], &mem->dev_endaddr[newi],
430                sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
431            memmove(&mem->dev_length[newi+1], &mem->dev_length[newi],
432                sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
433            memmove(&mem->dev_flags[newi+1], &mem->dev_flags[newi], sizeof(int) *
434                (MAX_DEVICES - newi - 1));
435            memmove(&mem->dev_extra[newi+1], &mem->dev_extra[newi], sizeof(void *) *
436                (MAX_DEVICES - newi - 1));
437            memmove(&mem->dev_f[newi+1], &mem->dev_f[newi], sizeof(void *) *
438                (MAX_DEVICES - newi - 1));
439            memmove(&mem->dev_dyntrans_data[newi+1], &mem->dev_dyntrans_data[newi],
440                sizeof(void *) * (MAX_DEVICES - newi - 1));
441            memmove(&mem->dev_dyntrans_write_low[newi+1],
442                &mem->dev_dyntrans_write_low[newi],
443                sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
444            memmove(&mem->dev_dyntrans_write_high[newi+1],
445                &mem->dev_dyntrans_write_high[newi],
446                sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
447    
448    
449            mem->dev_name[newi] = strdup(device_name);
450            mem->dev_baseaddr[newi] = baseaddr;
451            mem->dev_endaddr[newi] = baseaddr + len;
452            mem->dev_length[newi] = len;
453            mem->dev_flags[newi] = flags;
454            mem->dev_dyntrans_data[newi] = dyntrans_data;
455    
456            if (mem->dev_name[newi] == NULL) {
457                  fprintf(stderr, "out of memory\n");                  fprintf(stderr, "out of memory\n");
458                  exit(1);                  exit(1);
459          }          }
460    
461          if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK)          if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)
462              && !(flags & MEM_EMULATED_RAM) && dyntrans_data == NULL) {              && !(flags & DM_EMULATED_RAM) && dyntrans_data == NULL) {
463                  fatal("\nERROR: Device dyntrans access, but dyntrans_data"                  fatal("\nERROR: Device dyntrans access, but dyntrans_data"
464                      " = NULL!\n");                      " = NULL!\n");
465                  exit(1);                  exit(1);
# Line 426  void memory_device_register(struct memor Line 472  void memory_device_register(struct memor
472                  exit(1);                  exit(1);
473          }          }
474    
475          mem->dev_dyntrans_write_low[mem->n_mmapped_devices] = (uint64_t)-1;          mem->dev_dyntrans_write_low[newi] = (uint64_t)-1;
476          mem->dev_dyntrans_write_high[mem->n_mmapped_devices] = 0;          mem->dev_dyntrans_write_high[newi] = 0;
477          mem->dev_f[mem->n_mmapped_devices] = f;          mem->dev_f[newi] = f;
478          mem->dev_extra[mem->n_mmapped_devices] = extra;          mem->dev_extra[newi] = extra;
         mem->n_mmapped_devices++;  
479    
480          if (baseaddr < mem->mmap_dev_minaddr)          if (baseaddr < mem->mmap_dev_minaddr)
481                  mem->mmap_dev_minaddr = baseaddr & ~mem->dev_dyntrans_alignment;                  mem->mmap_dev_minaddr = baseaddr & ~mem->dev_dyntrans_alignment;
# Line 465  void memory_device_remove(struct memory Line 510  void memory_device_remove(struct memory
510              (MAX_DEVICES - i - 1));              (MAX_DEVICES - i - 1));
511          memmove(&mem->dev_baseaddr[i], &mem->dev_baseaddr[i+1],          memmove(&mem->dev_baseaddr[i], &mem->dev_baseaddr[i+1],
512              sizeof(uint64_t) * (MAX_DEVICES - i - 1));              sizeof(uint64_t) * (MAX_DEVICES - i - 1));
513            memmove(&mem->dev_endaddr[i], &mem->dev_endaddr[i+1],
514                sizeof(uint64_t) * (MAX_DEVICES - i - 1));
515          memmove(&mem->dev_length[i], &mem->dev_length[i+1], sizeof(uint64_t) *          memmove(&mem->dev_length[i], &mem->dev_length[i+1], sizeof(uint64_t) *
516              (MAX_DEVICES - i - 1));              (MAX_DEVICES - i - 1));
517          memmove(&mem->dev_flags[i], &mem->dev_flags[i+1], sizeof(int) *          memmove(&mem->dev_flags[i], &mem->dev_flags[i+1], sizeof(int) *
# Line 473  void memory_device_remove(struct memory Line 520  void memory_device_remove(struct memory
520              (MAX_DEVICES - i - 1));              (MAX_DEVICES - i - 1));
521          memmove(&mem->dev_f[i], &mem->dev_f[i+1], sizeof(void *) *          memmove(&mem->dev_f[i], &mem->dev_f[i+1], sizeof(void *) *
522              (MAX_DEVICES - i - 1));              (MAX_DEVICES - i - 1));
         memmove(&mem->dev_f_state[i], &mem->dev_f_state[i+1], sizeof(void *) *  
             (MAX_DEVICES - i - 1));  
523          memmove(&mem->dev_dyntrans_data[i], &mem->dev_dyntrans_data[i+1],          memmove(&mem->dev_dyntrans_data[i], &mem->dev_dyntrans_data[i+1],
524              sizeof(void *) * (MAX_DEVICES - i - 1));              sizeof(void *) * (MAX_DEVICES - i - 1));
525          memmove(&mem->dev_dyntrans_write_low[i], &mem->dev_dyntrans_write_low          memmove(&mem->dev_dyntrans_write_low[i], &mem->dev_dyntrans_write_low
526              [i+1], sizeof(void *) * (MAX_DEVICES - i - 1));              [i+1], sizeof(uint64_t) * (MAX_DEVICES - i - 1));
527          memmove(&mem->dev_dyntrans_write_high[i], &mem->dev_dyntrans_write_high          memmove(&mem->dev_dyntrans_write_high[i], &mem->dev_dyntrans_write_high
528              [i+1], sizeof(void *) * (MAX_DEVICES - i - 1));              [i+1], sizeof(uint64_t) * (MAX_DEVICES - i - 1));
529  }  }
530    
531    
# Line 534  unsigned char *memory_paddr_to_hostaddr( Line 579  unsigned char *memory_paddr_to_hostaddr(
579                  /*  Anonymous mmap() should return zero-filled memory,                  /*  Anonymous mmap() should return zero-filled memory,
580                      try malloc + memset if mmap failed.  */                      try malloc + memset if mmap failed.  */
581                  table[entry] = (void *) mmap(NULL, alloclen,                  table[entry] = (void *) mmap(NULL, alloclen,
582                      PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,                      PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
                     -1, 0);  
583                  if (table[entry] == NULL) {                  if (table[entry] == NULL) {
584                          table[entry] = malloc(alloclen);                          table[entry] = malloc(alloclen);
585                          if (table[entry] == NULL) {                          if (table[entry] == NULL) {

Legend:
Removed from v.18  
changed lines
  Added in v.22

  ViewVC Help
Powered by ViewVC 1.1.26