--- trunk/src/memory.c 2007/10/08 16:19:11 18 +++ trunk/src/memory.c 2007/10/08 16:22:11 40 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2005 Anders Gavare. All rights reserved. + * Copyright (C) 2003-2007 Anders Gavare. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: memory.c,v 1.180 2005/10/25 15:51:02 debug Exp $ + * $Id: memory.c,v 1.202 2007/04/28 09:19:51 debug Exp $ * * Functions for handling the memory of an emulated machine. */ @@ -36,17 +36,14 @@ #include #include -#include "bintrans.h" -#include "cop0.h" #include "cpu.h" #include "machine.h" #include "memory.h" -#include "mips_cpu_types.h" #include "misc.h" +extern int verbose; extern int quiet_mode; -extern volatile int single_step; /* @@ -59,11 +56,16 @@ */ uint64_t memory_readmax64(struct cpu *cpu, unsigned char *buf, int len) { - int i; + int i, byte_order = cpu->byte_order; uint64_t x = 0; + if (len & MEM_PCI_LITTLE_ENDIAN) { + len &= ~MEM_PCI_LITTLE_ENDIAN; + byte_order = EMUL_LITTLE_ENDIAN; + } + /* Switch byte order for incoming data, if necessary: */ - if (cpu->byte_order == EMUL_BIG_ENDIAN) + if (byte_order == EMUL_BIG_ENDIAN) for (i=0; ibyte_order; + + if (len & MEM_PCI_LITTLE_ENDIAN) { + len &= ~MEM_PCI_LITTLE_ENDIAN; + byte_order = EMUL_LITTLE_ENDIAN; + } - if (cpu->byte_order == EMUL_LITTLE_ENDIAN) + if (byte_order == EMUL_LITTLE_ENDIAN) for (i=0; i>= 8; @@ -114,14 +121,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; } @@ -184,7 +201,8 @@ /* * memory_points_to_string(): * - * 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 + * addr, otherwise 0. */ int memory_points_to_string(struct cpu *cpu, struct memory *mem, uint64_t addr, int min_string_length) @@ -213,8 +231,8 @@ /* * memory_conv_to_string(): * - * Convert virtual memory contents to a string, placing it in a - * buffer provided by the caller. + * Convert emulated memory contents to a string, placing it in a buffer + * provided by the caller. */ char *memory_conv_to_string(struct cpu *cpu, struct memory *mem, uint64_t addr, char *buf, int bufsize) @@ -258,31 +276,31 @@ /* * memory_device_dyntrans_access(): * - * Get the lowest and highest dyntrans (or bintrans) access since last time. + * Get the lowest and highest dyntrans access since last time. */ 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_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; @@ -291,34 +309,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; } } @@ -326,36 +324,34 @@ /* - * memory_device_register_statefunction(): + * memory_device_update_data(): * - * TODO: Hm. This is semi-ugly. Should probably be rewritten/redesigned - * some day. + * 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_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)) +void memory_device_update_data(struct memory *mem, void *extra, + unsigned char *data) { int i; - for (i=0; in_mmapped_devices; i++) - if (mem->dev_extra[i] == extra) { - mem->dev_f_state[i] = dev_f_state; - return; - } + for (i=0; in_mmapped_devices; i++) { + if (mem->devices[i].extra != extra) + continue; - printf("memory_device_register_statefunction(): " - "couldn't find the device\n"); - exit(1); + 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, @@ -363,57 +359,100 @@ size_t,int,void *), void *extra, int flags, unsigned char *dyntrans_data) { - int i; - - if (mem->n_mmapped_devices >= MAX_DEVICES) { - fprintf(stderr, "memory_device_register(): too many " - "devices registered, cannot register '%s'\n", device_name); - exit(1); - } + int i, newi = 0; - /* Check for collisions: */ + /* + * Figure out at which index to insert this device, and simultaneously + * check for collisions: + */ + newi = -1; for (i=0; in_mmapped_devices; i++) { - /* If we are not colliding with device i, then continue: */ - if (baseaddr + len <= mem->dev_baseaddr[i]) + if (i == 0 && baseaddr + len <= mem->devices[i].baseaddr) + newi = i; + 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->devices[i].endaddr) + newi = i + 1; + + /* If this is not colliding with device i, then continue: */ + if (baseaddr + len <= mem->devices[i].baseaddr) continue; - if (baseaddr >= mem->dev_baseaddr[i] + mem->dev_length[i]) + if (baseaddr >= mem->devices[i].endaddr) continue; - fatal("\nWARNING! \"%s\" collides with device %i (\"%s\")!\n" - " Run-time behaviour will be undefined!\n\n", - device_name, i, mem->dev_name[i]); + fatal("\nERROR! \"%s\" collides with device %i (\"%s\")!\n", + device_name, i, mem->devices[i].name); + exit(1); + } + if (mem->n_mmapped_devices == 0) + newi = 0; + if (newi == -1) { + fatal("INTERNAL ERROR\n"); + exit(1); } - /* (40 bits of physical address is displayed) */ - debug("device %2i at 0x%010llx: %s", - mem->n_mmapped_devices, (long long)baseaddr, device_name); + if (verbose >= 2) { + /* (40 bits of physical address is displayed) */ + 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%"PRIx64".\n", (uint64_t) baseaddr); + } + + if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) { + debug(" (dyntrans %s)", + (flags & DM_DYNTRANS_WRITE_OK)? "R/W" : "R"); + } + debug("\n"); + } - if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK) - && (baseaddr & mem->dev_dyntrans_alignment) != 0) { - fatal("\nWARNING: Device dyntrans access, but unaligned" - " baseaddr 0x%llx.\n", (long long)baseaddr); + for (i=0; in_mmapped_devices; i++) { + 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->devices[i].name); + exit(1); + } } - if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK)) { - debug(" (dyntrans %s)", - (flags & MEM_DYNTRANS_WRITE_OK)? "R/W" : "R"); + mem->n_mmapped_devices++; + + 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); } - 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; + /* Make space for the new entry: */ + 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[mem->n_mmapped_devices] == NULL) { + if (mem->devices[newi].name == NULL) { fprintf(stderr, "out of memory\n"); exit(1); } - if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK) - && !(flags & MEM_EMULATED_RAM) && dyntrans_data == NULL) { + if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK) + && !(flags & DM_EMULATED_RAM) && dyntrans_data == NULL) { fatal("\nERROR: Device dyntrans access, but dyntrans_data" " = NULL!\n"); exit(1); @@ -426,30 +465,32 @@ exit(1); } - mem->dev_dyntrans_write_low[mem->n_mmapped_devices] = (uint64_t)-1; - mem->dev_dyntrans_write_high[mem->n_mmapped_devices] = 0; - mem->dev_f[mem->n_mmapped_devices] = f; - mem->dev_extra[mem->n_mmapped_devices] = extra; - mem->n_mmapped_devices++; + 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 --; @@ -457,30 +498,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_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_f_state[i], &mem->dev_f_state[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(void *) * (MAX_DEVICES - i - 1)); - memmove(&mem->dev_dyntrans_write_high[i], &mem->dev_dyntrans_write_high - [i+1], sizeof(void *) * (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; } @@ -494,9 +518,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, @@ -506,12 +532,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; @@ -534,8 +561,7 @@ /* Anonymous mmap() should return zero-filled memory, try malloc + memset if mmap failed. */ table[entry] = (void *) mmap(NULL, alloclen, - PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, - -1, 0); + PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (table[entry] == NULL) { table[entry] = malloc(alloclen); if (table[entry] == NULL) { @@ -546,6 +572,111 @@ } } - 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 size_t 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; ipc; + char *symbol; + + /* + * This allows guest OS kernels to probe memory a few KBs past the + * end of memory, without giving too many warnings. + */ + if (paddr < mem->physical_max + 0x40000) + return; + + if (!cpu->machine->halt_on_nonexistant_memaccess && quiet_mode) + return; + + fatal("[ memory_rw(): %s ", writeflag? "write":"read"); + + if (writeflag) { + unsigned int i; + debug("data={", writeflag); + if (len > 16) { + int start2 = len-16; + for (i=0; i<16; i++) + debug("%s%02x", i?",":"", data[i]); + debug(" .. "); + if (start2 < 16) + start2 = 16; + for (i=start2; i= physical_max; pc=", (long long)paddr); + if (cpu->is_32bit) + fatal("0x%08"PRIx32, (uint32_t) old_pc); + else + fatal("0x%016"PRIx64, (uint64_t) old_pc); + symbol = get_symbol_name(&cpu->machine->symbol_context, + old_pc, &offset); + fatal(" <%s> ]\n", symbol? symbol : " no symbol "); + + if (cpu->machine->halt_on_nonexistant_memaccess) { + /* TODO: Halt in a nicer way. Not possible with the + current dyntrans system... */ + exit(1); + } }