/[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 4 by dpavlin, Mon Oct 8 16:18:00 2007 UTC revision 12 by dpavlin, Mon Oct 8 16:18:38 2007 UTC
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: memory.c,v 1.164 2005/04/09 21:10:54 debug Exp $   *  $Id: memory.c,v 1.175 2005/08/14 15:47:36 debug Exp $
29   *   *
30   *  Functions for handling the memory of an emulated machine.   *  Functions for handling the memory of an emulated machine.
31   */   */
# Line 108  void memory_writemax64(struct cpu *cpu, Line 108  void memory_writemax64(struct cpu *cpu,
108   *  zeroed_alloc():   *  zeroed_alloc():
109   *   *
110   *  Allocates a block of memory using mmap(), and if that fails, try   *  Allocates a block of memory using mmap(), and if that fails, try
111   *  malloc() + memset().   *  malloc() + memset(). The returned memory block contains only zeroes.
112   */   */
113  void *zeroed_alloc(size_t s)  void *zeroed_alloc(size_t s)
114  {  {
# Line 132  void *zeroed_alloc(size_t s) Line 132  void *zeroed_alloc(size_t s)
132   *  This function creates a new memory object. An emulated machine needs one   *  This function creates a new memory object. An emulated machine needs one
133   *  of these.   *  of these.
134   */   */
135  struct memory *memory_new(uint64_t physical_max)  struct memory *memory_new(uint64_t physical_max, int arch)
136  {  {
137          struct memory *mem;          struct memory *mem;
138          int bits_per_pagetable = BITS_PER_PAGETABLE;          int bits_per_pagetable = BITS_PER_PAGETABLE;
# Line 157  struct memory *memory_new(uint64_t physi Line 157  struct memory *memory_new(uint64_t physi
157          }          }
158    
159          mem->physical_max = physical_max;          mem->physical_max = physical_max;
160            mem->dev_dyntrans_alignment = 4095;
161            if (arch == ARCH_ALPHA)
162                    mem->dev_dyntrans_alignment = 8191;
163    
164          s = entries_per_pagetable * sizeof(void *);          s = entries_per_pagetable * sizeof(void *);
165    
# Line 253  char *memory_conv_to_string(struct cpu * Line 256  char *memory_conv_to_string(struct cpu *
256    
257    
258  /*  /*
259   *  memory_device_bintrans_access():   *  memory_device_dyntrans_access():
260   *   *
261   *  Get the lowest and highest bintrans access since last time.   *  Get the lowest and highest dyntrans (or bintrans) access since last time.
262   */   */
263  void memory_device_bintrans_access(struct cpu *cpu, struct memory *mem,  void memory_device_dyntrans_access(struct cpu *cpu, struct memory *mem,
264          void *extra, uint64_t *low, uint64_t *high)          void *extra, uint64_t *low, uint64_t *high)
265  {  {
 #ifdef BINTRANS  
266          int i, j;          int i, j;
267          size_t s;          size_t s;
268          int need_inval = 0;          int need_inval = 0;
# Line 271  void memory_device_bintrans_access(struc Line 273  void memory_device_bintrans_access(struc
273    
274          for (i=0; i<mem->n_mmapped_devices; i++) {          for (i=0; i<mem->n_mmapped_devices; i++) {
275                  if (mem->dev_extra[i] == extra &&                  if (mem->dev_extra[i] == extra &&
276                      mem->dev_bintrans_data[i] != NULL) {                      mem->dev_dyntrans_data[i] != NULL) {
277                          if (mem->dev_bintrans_write_low[i] != (uint64_t) -1)                          if (mem->dev_dyntrans_write_low[i] != (uint64_t) -1)
278                                  need_inval = 1;                                  need_inval = 1;
279                          if (low != NULL)                          if (low != NULL)
280                                  *low = mem->dev_bintrans_write_low[i];                                  *low = mem->dev_dyntrans_write_low[i];
281                          mem->dev_bintrans_write_low[i] = (uint64_t) -1;                          mem->dev_dyntrans_write_low[i] = (uint64_t) -1;
282    
283                          if (high != NULL)                          if (high != NULL)
284                                  *high = mem->dev_bintrans_write_high[i];                                  *high = mem->dev_dyntrans_write_high[i];
285                          mem->dev_bintrans_write_high[i] = 0;                          mem->dev_dyntrans_write_high[i] = 0;
286    
287                          if (!need_inval)                          if (!need_inval)
288                                  return;                                  return;
289    
                         if (cpu->machine->arch != ARCH_MIPS) {  
                                 /*  TODO!  */  
   
                                 return;  
                         }  
   
290                          /*  Invalidate any pages of this device that might                          /*  Invalidate any pages of this device that might
291                              be in the bintrans load/store cache, by marking                              be in the dyntrans load/store cache, by marking
292                              the pages read-only.  */                              the pages read-only.  */
293                            if (cpu->invalidate_translation_caches_paddr != NULL) {
294                          for (s=0; s<mem->dev_length[i]; s+=4096) {                                  for (s=0; s<mem->dev_length[i];
295                                  mips_invalidate_translation_caches_paddr(                                      s+=cpu->machine->arch_pagesize)
296                                      cpu, mem->dev_baseaddr[i] + s);                                          cpu->invalidate_translation_caches_paddr
297                                                (cpu, mem->dev_baseaddr[i] + s);
298                          }                          }
299    
300                          /*  ... and invalidate the "fast_vaddr_to_hostaddr"                          if (cpu->machine->arch == ARCH_MIPS) {
301                              cache entries that contain pointers to this                                  /*
302                              device:  (NOTE: Device i, cache entry j)  */                                   *  ... and invalidate the "fast_vaddr_to_
303                          for (j=0; j<N_BINTRANS_VADDR_TO_HOST; j++) {                                   *  hostaddr" cache entries that contain
304                                  if (cpu->cd.mips.bintrans_data_hostpage[j] >=                                   *  pointers to this device:  (NOTE: Device i,
305                                      mem->dev_bintrans_data[i] &&                                   *  cache entry j)
306                                      cpu->cd.mips.bintrans_data_hostpage[j] <                                   */
307                                      mem->dev_bintrans_data[i] +                                  for (j=0; j<N_BINTRANS_VADDR_TO_HOST; j++) {
308                                      mem->dev_length[i])                                          if (cpu->cd.
309                                          cpu->cd.mips.                                              mips.bintrans_data_hostpage[j] >=
310                                              bintrans_data_hostpage[j] = NULL;                                              mem->dev_dyntrans_data[i] &&
311                                                cpu->cd.mips.
312                                                bintrans_data_hostpage[j] <
313                                                mem->dev_dyntrans_data[i] +
314                                                mem->dev_length[i])
315                                                    cpu->cd.mips.
316                                                        bintrans_data_hostpage[j]
317                                                        = NULL;
318                                    }
319                          }                          }
   
320                          return;                          return;
321                  }                  }
322          }          }
 #endif  
323  }  }
324    
325    
# Line 356  void memory_device_register(struct memor Line 359  void memory_device_register(struct memor
359          uint64_t baseaddr, uint64_t len,          uint64_t baseaddr, uint64_t len,
360          int (*f)(struct cpu *,struct memory *,uint64_t,unsigned char *,          int (*f)(struct cpu *,struct memory *,uint64_t,unsigned char *,
361                  size_t,int,void *),                  size_t,int,void *),
362          void *extra, int flags, unsigned char *bintrans_data)          void *extra, int flags, unsigned char *dyntrans_data)
363  {  {
364          int i;          int i;
365    
# Line 383  void memory_device_register(struct memor Line 386  void memory_device_register(struct memor
386          debug("device %2i at 0x%010llx: %s",          debug("device %2i at 0x%010llx: %s",
387              mem->n_mmapped_devices, (long long)baseaddr, device_name);              mem->n_mmapped_devices, (long long)baseaddr, device_name);
388    
389  #ifdef BINTRANS          if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK)
390          if (flags & (MEM_BINTRANS_OK | MEM_BINTRANS_WRITE_OK)              && (baseaddr & mem->dev_dyntrans_alignment) != 0) {
391              && (baseaddr & 0xfff) != 0) {                  fatal("\nWARNING: Device dyntrans access, but unaligned"
                 fatal("\nWARNING: Device bintrans access, but unaligned"  
392                      " baseaddr 0x%llx.\n", (long long)baseaddr);                      " baseaddr 0x%llx.\n", (long long)baseaddr);
393          }          }
394    
395          if (flags & (MEM_BINTRANS_OK | MEM_BINTRANS_WRITE_OK)) {          if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK)) {
396                  debug(" (bintrans %s)",                  debug(" (dyntrans %s)",
397                      (flags & MEM_BINTRANS_WRITE_OK)? "R/W" : "R");                      (flags & MEM_DYNTRANS_WRITE_OK)? "R/W" : "R");
398          }          }
 #endif  
399          debug("\n");          debug("\n");
400    
401          mem->dev_name[mem->n_mmapped_devices] = strdup(device_name);          mem->dev_name[mem->n_mmapped_devices] = strdup(device_name);
402          mem->dev_baseaddr[mem->n_mmapped_devices] = baseaddr;          mem->dev_baseaddr[mem->n_mmapped_devices] = baseaddr;
403          mem->dev_length[mem->n_mmapped_devices] = len;          mem->dev_length[mem->n_mmapped_devices] = len;
404          mem->dev_flags[mem->n_mmapped_devices] = flags;          mem->dev_flags[mem->n_mmapped_devices] = flags;
405          mem->dev_bintrans_data[mem->n_mmapped_devices] = bintrans_data;          mem->dev_dyntrans_data[mem->n_mmapped_devices] = dyntrans_data;
406    
407          if (mem->dev_name[mem->n_mmapped_devices] == NULL) {          if (mem->dev_name[mem->n_mmapped_devices] == NULL) {
408                  fprintf(stderr, "out of memory\n");                  fprintf(stderr, "out of memory\n");
409                  exit(1);                  exit(1);
410          }          }
411    
412          if ((size_t)bintrans_data & 1) {          if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK)
413                && dyntrans_data == NULL) {
414                    fatal("\nERROR: Device dyntrans access, but dyntrans_data"
415                        " = NULL!\n");
416                    exit(1);
417            }
418    
419            if ((size_t)dyntrans_data & 7) {
420                  fprintf(stderr, "memory_device_register():"                  fprintf(stderr, "memory_device_register():"
421                      " bintrans_data not aligned correctly\n");                      " dyntrans_data not aligned correctly (%p)\n",
422                        dyntrans_data);
423                  exit(1);                  exit(1);
424          }          }
425    
426  #ifdef BINTRANS          mem->dev_dyntrans_write_low[mem->n_mmapped_devices] = (uint64_t)-1;
427          mem->dev_bintrans_write_low[mem->n_mmapped_devices] = (uint64_t)-1;          mem->dev_dyntrans_write_high[mem->n_mmapped_devices] = 0;
         mem->dev_bintrans_write_high[mem->n_mmapped_devices] = 0;  
 #endif  
428          mem->dev_f[mem->n_mmapped_devices] = f;          mem->dev_f[mem->n_mmapped_devices] = f;
429          mem->dev_extra[mem->n_mmapped_devices] = extra;          mem->dev_extra[mem->n_mmapped_devices] = extra;
430          mem->n_mmapped_devices++;          mem->n_mmapped_devices++;
431    
432          if (baseaddr < mem->mmap_dev_minaddr)          if (baseaddr < mem->mmap_dev_minaddr)
433                  mem->mmap_dev_minaddr = baseaddr & ~0xfff;                  mem->mmap_dev_minaddr = baseaddr & ~mem->dev_dyntrans_alignment;
434          if (baseaddr + len > mem->mmap_dev_maxaddr)          if (baseaddr + len > mem->mmap_dev_maxaddr)
435                  mem->mmap_dev_maxaddr = (((baseaddr + len) - 1) | 0xfff) + 1;                  mem->mmap_dev_maxaddr = (((baseaddr + len) - 1) |
436                        mem->dev_dyntrans_alignment) + 1;
437  }  }
438    
439    
# Line 464  void memory_device_remove(struct memory Line 472  void memory_device_remove(struct memory
472              (MAX_DEVICES - i - 1));              (MAX_DEVICES - i - 1));
473          memmove(&mem->dev_f_state[i], &mem->dev_f_state[i+1], sizeof(void *) *          memmove(&mem->dev_f_state[i], &mem->dev_f_state[i+1], sizeof(void *) *
474              (MAX_DEVICES - i - 1));              (MAX_DEVICES - i - 1));
475          memmove(&mem->dev_bintrans_data[i], &mem->dev_bintrans_data[i+1],          memmove(&mem->dev_dyntrans_data[i], &mem->dev_dyntrans_data[i+1],
476              sizeof(void *) * (MAX_DEVICES - i - 1));              sizeof(void *) * (MAX_DEVICES - i - 1));
477  #ifdef BINTRANS          memmove(&mem->dev_dyntrans_write_low[i], &mem->dev_dyntrans_write_low
         memmove(&mem->dev_bintrans_write_low[i], &mem->dev_bintrans_write_low  
478              [i+1], sizeof(void *) * (MAX_DEVICES - i - 1));              [i+1], sizeof(void *) * (MAX_DEVICES - i - 1));
479          memmove(&mem->dev_bintrans_write_high[i], &mem->dev_bintrans_write_high          memmove(&mem->dev_dyntrans_write_high[i], &mem->dev_dyntrans_write_high
480              [i+1], sizeof(void *) * (MAX_DEVICES - i - 1));              [i+1], sizeof(void *) * (MAX_DEVICES - i - 1));
 #endif  
481  }  }
482    
483    
# Line 501  unsigned char *memory_paddr_to_hostaddr( Line 507  unsigned char *memory_paddr_to_hostaddr(
507          table = mem->pagetable;          table = mem->pagetable;
508          entry = (paddr >> shrcount) & mask;          entry = (paddr >> shrcount) & mask;
509    
510          /*  printf("   entry = %x\n", entry);  */          /*  printf("memory_paddr_to_hostaddr(): p=%16llx w=%i => entry=0x%x\n",
511                (long long)paddr, writeflag, entry);  */
512    
513          if (table[entry] == NULL) {          if (table[entry] == NULL) {
514                  size_t alloclen;                  size_t alloclen;

Legend:
Removed from v.4  
changed lines
  Added in v.12

  ViewVC Help
Powered by ViewVC 1.1.26