--- trunk/src/memory.c 2007/10/08 16:19:37 22 +++ trunk/src/memory.c 2007/10/08 16:20:58 32 @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: memory.c,v 1.187 2006/01/14 12:51:59 debug Exp $ + * $Id: memory.c,v 1.199 2006/10/24 09:32:48 debug Exp $ * * Functions for handling the memory of an emulated machine. */ @@ -120,14 +120,24 @@ { void *p = mmap(NULL, s, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); + if (p == NULL) { +#if 1 + fprintf(stderr, "zeroed_alloc(): mmap() failed. This should" + " not usually happen. If you can reproduce this, then" + " please contact me with details about your run-time" + " environment.\n"); + exit(1); +#else p = malloc(s); if (p == NULL) { fprintf(stderr, "out of memory\n"); exit(1); } memset(p, 0, s); +#endif } + return p; } @@ -270,27 +280,26 @@ void memory_device_dyntrans_access(struct cpu *cpu, struct memory *mem, void *extra, uint64_t *low, uint64_t *high) { - int i, j; size_t s; - int need_inval = 0; + int i, need_inval = 0; /* TODO: This is O(n), so it might be good to rewrite it some day. For now, it will be enough, as long as this function is not called too often. */ for (i=0; in_mmapped_devices; i++) { - if (mem->dev_extra[i] == extra && - mem->dev_flags[i] & DM_DYNTRANS_WRITE_OK && - mem->dev_dyntrans_data[i] != NULL) { - if (mem->dev_dyntrans_write_low[i] != (uint64_t) -1) + if (mem->devices[i].extra == extra && + mem->devices[i].flags & DM_DYNTRANS_WRITE_OK && + mem->devices[i].dyntrans_data != NULL) { + if (mem->devices[i].dyntrans_write_low != (uint64_t) -1) need_inval = 1; if (low != NULL) - *low = mem->dev_dyntrans_write_low[i]; - mem->dev_dyntrans_write_low[i] = (uint64_t) -1; + *low = mem->devices[i].dyntrans_write_low; + mem->devices[i].dyntrans_write_low = (uint64_t) -1; if (high != NULL) - *high = mem->dev_dyntrans_write_high[i]; - mem->dev_dyntrans_write_high[i] = 0; + *high = mem->devices[i].dyntrans_write_high; + mem->devices[i].dyntrans_write_high = 0; if (!need_inval) return; @@ -299,34 +308,14 @@ be in the dyntrans load/store cache, by marking the pages read-only. */ if (cpu->invalidate_translation_caches != NULL) { - for (s=0; sdev_length[i]; - s+=cpu->machine->arch_pagesize) + for (s = *low; s <= *high; + s += cpu->machine->arch_pagesize) cpu->invalidate_translation_caches - (cpu, mem->dev_baseaddr[i] + s, + (cpu, mem->devices[i].baseaddr + s, JUST_MARK_AS_NON_WRITABLE | INVALIDATE_PADDR); } - if (cpu->machine->arch == ARCH_MIPS) { - /* - * ... and invalidate the "fast_vaddr_to_ - * hostaddr" cache entries that contain - * pointers to this device: (NOTE: Device i, - * cache entry j) - */ - for (j=0; jcd. - mips.bintrans_data_hostpage[j] >= - mem->dev_dyntrans_data[i] && - cpu->cd.mips. - bintrans_data_hostpage[j] < - mem->dev_dyntrans_data[i] + - mem->dev_length[i]) - cpu->cd.mips. - bintrans_data_hostpage[j] - = NULL; - } - } return; } } @@ -334,10 +323,34 @@ /* + * memory_device_update_data(): + * + * Update a device' dyntrans data pointer. + * + * SUPER-IMPORTANT NOTE: Anyone who changes a dyntrans data pointer while + * things are running also needs to invalidate all CPUs' address translation + * caches! Otherwise, these may contain old pointers to the old data. + */ +void memory_device_update_data(struct memory *mem, void *extra, + unsigned char *data) +{ + int i; + + for (i=0; in_mmapped_devices; i++) { + if (mem->devices[i].extra != extra) + continue; + + mem->devices[i].dyntrans_data = data; + mem->devices[i].dyntrans_write_low = (uint64_t)-1; + mem->devices[i].dyntrans_write_high = 0; + } +} + + +/* * memory_device_register(): * - * Register a (memory mapped) device by adding it to the dev_* fields of a - * memory struct. + * Register a memory mapped device. */ void memory_device_register(struct memory *mem, const char *device_name, uint64_t baseaddr, uint64_t len, @@ -347,35 +360,29 @@ { int i, newi = 0; - if (mem->n_mmapped_devices >= MAX_DEVICES) { - fprintf(stderr, "memory_device_register(): too many " - "devices registered, cannot register '%s'\n", device_name); - exit(1); - } - /* * Figure out at which index to insert this device, and simultaneously * check for collisions: */ newi = -1; for (i=0; in_mmapped_devices; i++) { - if (i == 0 && baseaddr + len <= mem->dev_baseaddr[i]) + if (i == 0 && baseaddr + len <= mem->devices[i].baseaddr) newi = i; - if (i > 0 && baseaddr + len <= mem->dev_baseaddr[i] && - baseaddr >= mem->dev_endaddr[i-1]) + if (i > 0 && baseaddr + len <= mem->devices[i].baseaddr && + baseaddr >= mem->devices[i-1].endaddr) newi = i; if (i == mem->n_mmapped_devices - 1 && - baseaddr >= mem->dev_endaddr[i]) + baseaddr >= mem->devices[i].endaddr) newi = i + 1; - /* If we are not colliding with device i, then continue: */ - if (baseaddr + len <= mem->dev_baseaddr[i]) + /* If this is not colliding with device i, then continue: */ + if (baseaddr + len <= mem->devices[i].baseaddr) continue; - if (baseaddr >= mem->dev_endaddr[i]) + if (baseaddr >= mem->devices[i].endaddr) continue; fatal("\nERROR! \"%s\" collides with device %i (\"%s\")!\n", - device_name, i, mem->dev_name[i]); + device_name, i, mem->devices[i].name); exit(1); } if (mem->n_mmapped_devices == 0) @@ -387,13 +394,13 @@ if (verbose >= 2) { /* (40 bits of physical address is displayed) */ - debug("device at 0x%010llx: %s", (long long)baseaddr, + debug("device at 0x%010"PRIx64": %s", (uint64_t) baseaddr, device_name); if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK) && (baseaddr & mem->dev_dyntrans_alignment) != 0) { fatal("\nWARNING: Device dyntrans access, but unaligned" - " baseaddr 0x%llx.\n", (long long)baseaddr); + " baseaddr 0x%"PRIx64".\n", (uint64_t) baseaddr); } if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) { @@ -404,56 +411,41 @@ } for (i=0; in_mmapped_devices; i++) { - if (dyntrans_data == mem->dev_dyntrans_data[i] && - mem->dev_flags[i] & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK) + if (dyntrans_data == mem->devices[i].dyntrans_data && + mem->devices[i].flags&(DM_DYNTRANS_OK|DM_DYNTRANS_WRITE_OK) && flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) { fatal("ERROR: the data pointer used for dyntrans " "accesses must only be used once!\n"); fatal("(%p cannot be used by '%s'; already in use by '" "%s')\n", dyntrans_data, device_name, - mem->dev_name[i]); + mem->devices[i].name); exit(1); } } mem->n_mmapped_devices++; - /* - * YUCK! This is ugly. TODO: fix - */ + mem->devices = realloc(mem->devices, sizeof(struct memory_device) + * mem->n_mmapped_devices); + if (mem->devices == NULL) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + /* Make space for the new entry: */ - memmove(&mem->dev_name[newi+1], &mem->dev_name[newi], sizeof(char *) * - (MAX_DEVICES - newi - 1)); - memmove(&mem->dev_baseaddr[newi+1], &mem->dev_baseaddr[newi], - sizeof(uint64_t) * (MAX_DEVICES - newi - 1)); - memmove(&mem->dev_endaddr[newi+1], &mem->dev_endaddr[newi], - sizeof(uint64_t) * (MAX_DEVICES - newi - 1)); - memmove(&mem->dev_length[newi+1], &mem->dev_length[newi], - sizeof(uint64_t) * (MAX_DEVICES - newi - 1)); - memmove(&mem->dev_flags[newi+1], &mem->dev_flags[newi], sizeof(int) * - (MAX_DEVICES - newi - 1)); - memmove(&mem->dev_extra[newi+1], &mem->dev_extra[newi], sizeof(void *) * - (MAX_DEVICES - newi - 1)); - memmove(&mem->dev_f[newi+1], &mem->dev_f[newi], sizeof(void *) * - (MAX_DEVICES - newi - 1)); - memmove(&mem->dev_dyntrans_data[newi+1], &mem->dev_dyntrans_data[newi], - sizeof(void *) * (MAX_DEVICES - newi - 1)); - memmove(&mem->dev_dyntrans_write_low[newi+1], - &mem->dev_dyntrans_write_low[newi], - sizeof(uint64_t) * (MAX_DEVICES - newi - 1)); - memmove(&mem->dev_dyntrans_write_high[newi+1], - &mem->dev_dyntrans_write_high[newi], - sizeof(uint64_t) * (MAX_DEVICES - newi - 1)); - - - mem->dev_name[newi] = strdup(device_name); - mem->dev_baseaddr[newi] = baseaddr; - mem->dev_endaddr[newi] = baseaddr + len; - mem->dev_length[newi] = len; - mem->dev_flags[newi] = flags; - mem->dev_dyntrans_data[newi] = dyntrans_data; + if (newi + 1 != mem->n_mmapped_devices) + memmove(&mem->devices[newi+1], &mem->devices[newi], + sizeof(struct memory_device) + * (mem->n_mmapped_devices - newi - 1)); + + mem->devices[newi].name = strdup(device_name); + mem->devices[newi].baseaddr = baseaddr; + mem->devices[newi].endaddr = baseaddr + len; + mem->devices[newi].length = len; + mem->devices[newi].flags = flags; + mem->devices[newi].dyntrans_data = dyntrans_data; - if (mem->dev_name[newi] == NULL) { + if (mem->devices[newi].name == NULL) { fprintf(stderr, "out of memory\n"); exit(1); } @@ -472,29 +464,32 @@ exit(1); } - mem->dev_dyntrans_write_low[newi] = (uint64_t)-1; - mem->dev_dyntrans_write_high[newi] = 0; - mem->dev_f[newi] = f; - mem->dev_extra[newi] = extra; + mem->devices[newi].dyntrans_write_low = (uint64_t)-1; + mem->devices[newi].dyntrans_write_high = 0; + mem->devices[newi].f = f; + mem->devices[newi].extra = extra; if (baseaddr < mem->mmap_dev_minaddr) mem->mmap_dev_minaddr = baseaddr & ~mem->dev_dyntrans_alignment; if (baseaddr + len > mem->mmap_dev_maxaddr) mem->mmap_dev_maxaddr = (((baseaddr + len) - 1) | mem->dev_dyntrans_alignment) + 1; + + if (newi < mem->last_accessed_device) + mem->last_accessed_device ++; } /* * memory_device_remove(): * - * Unregister a (memory mapped) device from a memory struct. + * Unregister a memory mapped device from a memory object. */ void memory_device_remove(struct memory *mem, int i) { if (i < 0 || i >= mem->n_mmapped_devices) { fatal("memory_device_remove(): invalid device number %i\n", i); - return; + exit(1); } mem->n_mmapped_devices --; @@ -502,30 +497,13 @@ if (i == mem->n_mmapped_devices) return; - /* - * YUCK! This is ugly. TODO: fix - */ + memmove(&mem->devices[i], &mem->devices[i+1], + sizeof(struct memory_device) * (mem->n_mmapped_devices - i)); - memmove(&mem->dev_name[i], &mem->dev_name[i+1], sizeof(char *) * - (MAX_DEVICES - i - 1)); - memmove(&mem->dev_baseaddr[i], &mem->dev_baseaddr[i+1], - sizeof(uint64_t) * (MAX_DEVICES - i - 1)); - memmove(&mem->dev_endaddr[i], &mem->dev_endaddr[i+1], - sizeof(uint64_t) * (MAX_DEVICES - i - 1)); - memmove(&mem->dev_length[i], &mem->dev_length[i+1], sizeof(uint64_t) * - (MAX_DEVICES - i - 1)); - memmove(&mem->dev_flags[i], &mem->dev_flags[i+1], sizeof(int) * - (MAX_DEVICES - i - 1)); - memmove(&mem->dev_extra[i], &mem->dev_extra[i+1], sizeof(void *) * - (MAX_DEVICES - i - 1)); - memmove(&mem->dev_f[i], &mem->dev_f[i+1], sizeof(void *) * - (MAX_DEVICES - i - 1)); - memmove(&mem->dev_dyntrans_data[i], &mem->dev_dyntrans_data[i+1], - sizeof(void *) * (MAX_DEVICES - i - 1)); - memmove(&mem->dev_dyntrans_write_low[i], &mem->dev_dyntrans_write_low - [i+1], sizeof(uint64_t) * (MAX_DEVICES - i - 1)); - memmove(&mem->dev_dyntrans_write_high[i], &mem->dev_dyntrans_write_high - [i+1], sizeof(uint64_t) * (MAX_DEVICES - i - 1)); + if (i <= mem->last_accessed_device) + mem->last_accessed_device --; + if (mem->last_accessed_device < 0) + mem->last_accessed_device = 0; } @@ -539,9 +517,11 @@ /* * memory_paddr_to_hostaddr(): * - * Translate a physical address into a host address. + * Translate a physical address into a host address. The usual way to call + * this function is to make sure that paddr is page aligned, which will result + * in the host _page_ corresponding to that address. * - * Return value is a pointer to a host memblock, or NULL on failure. + * Return value is a pointer to the address in the host, or NULL on failure. * On reads, a NULL return value should be interpreted as reading all zeroes. */ unsigned char *memory_paddr_to_hostaddr(struct memory *mem, @@ -551,12 +531,13 @@ int entry; const int mask = (1 << BITS_PER_PAGETABLE) - 1; const int shrcount = MAX_BITS - BITS_PER_PAGETABLE; + unsigned char *hostptr; table = mem->pagetable; entry = (paddr >> shrcount) & mask; - /* printf("memory_paddr_to_hostaddr(): p=%16llx w=%i => entry=0x%x\n", - (long long)paddr, writeflag, entry); */ + /* printf("memory_paddr_to_hostaddr(): p=%16"PRIx64 + " w=%i => entry=0x%x\n", (uint64_t) paddr, writeflag, entry); */ if (table[entry] == NULL) { size_t alloclen; @@ -590,6 +571,52 @@ } } - return (unsigned char *) table[entry]; + hostptr = (unsigned char *) table[entry]; + + if (hostptr != NULL) + hostptr += (paddr & ((1 << BITS_PER_MEMBLOCK) - 1)); + + return hostptr; +} + + +#define UPDATE_CHECKSUM(value) { \ + internal_state -= 0x118c7771c0c0a77fULL; \ + internal_state = ((internal_state + (value)) << 7) ^ \ + (checksum >> 11) ^ ((checksum - (value)) << 3) ^ \ + (internal_state - checksum) ^ ((value) - internal_state); \ + checksum ^= internal_state; \ + } + + +/* + * memory_checksum(): + * + * Calculate a 64-bit checksum of everything in a struct memory. This is + * useful for tracking down bugs; an old (presumably working) version of + * the emulator can be compared to a newer (buggy) version. + */ +uint64_t memory_checksum(struct memory *mem) +{ + uint64_t internal_state = 0x80624185376feff2ULL; + uint64_t checksum = 0xcb9a87d5c010072cULL; + const int n_entries = (1 << BITS_PER_PAGETABLE) - 1; + const size_t len = (1 << BITS_PER_MEMBLOCK) / sizeof(uint64_t); + size_t entry, i; + + for (entry=0; entry<=n_entries; entry++) { + uint64_t **table = mem->pagetable; + uint64_t *memblock = table[entry]; + + if (memblock == NULL) { + UPDATE_CHECKSUM(0x1198ab7c8174a76fULL); + continue; + } + + for (i=0; i