--- trunk/src/memory.c 2007/10/08 16:18:51 14 +++ trunk/src/memory.c 2007/10/08 16:19:56 24 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2005 Anders Gavare. All rights reserved. + * Copyright (C) 2003-2006 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.176 2005/08/28 20:16:23 debug Exp $ + * $Id: memory.c,v 1.190 2006/06/16 18:31:25 debug Exp $ * * Functions for handling the memory of an emulated machine. */ @@ -36,17 +36,13 @@ #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 quiet_mode; -extern volatile int single_step; +extern int verbose; /* @@ -59,11 +55,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; @@ -184,7 +190,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 +220,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,14 +265,13 @@ /* * 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 @@ -273,6 +279,7 @@ 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) need_inval = 1; @@ -290,34 +297,15 @@ /* Invalidate any pages of this device that might be in the dyntrans load/store cache, by marking the pages read-only. */ - if (cpu->invalidate_translation_caches_paddr != NULL) { + if (cpu->invalidate_translation_caches != NULL) { for (s=0; sdev_length[i]; s+=cpu->machine->arch_pagesize) - cpu->invalidate_translation_caches_paddr + cpu->invalidate_translation_caches (cpu, mem->dev_baseaddr[i] + s, - JUST_MARK_AS_NON_WRITABLE); + 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; } } @@ -325,32 +313,6 @@ /* - * 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; in_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); -} - - -/* * memory_device_register(): * * Register a (memory mapped) device by adding it to the dev_* fields of a @@ -362,7 +324,7 @@ size_t,int,void *), void *extra, int flags, unsigned char *dyntrans_data) { - int i; + int i, newi = 0; if (mem->n_mmapped_devices >= MAX_DEVICES) { fprintf(stderr, "memory_device_register(): too many " @@ -370,65 +332,129 @@ exit(1); } - /* 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 (i == 0 && baseaddr + len <= mem->dev_baseaddr[i]) + newi = i; + if (i > 0 && baseaddr + len <= mem->dev_baseaddr[i] && + baseaddr >= mem->dev_endaddr[i-1]) + newi = i; + if (i == mem->n_mmapped_devices - 1 && + baseaddr >= mem->dev_endaddr[i]) + newi = i + 1; + /* If we are not colliding with device i, then continue: */ if (baseaddr + len <= mem->dev_baseaddr[i]) continue; - if (baseaddr >= mem->dev_baseaddr[i] + mem->dev_length[i]) + if (baseaddr >= mem->dev_endaddr[i]) continue; - fatal("\nWARNING! \"%s\" collides with device %i (\"%s\")!\n" - " Run-time behaviour will be undefined!\n\n", + fatal("\nERROR! \"%s\" collides with device %i (\"%s\")!\n", device_name, i, mem->dev_name[i]); + 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 (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); + 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)) { - debug(" (dyntrans %s)", - (flags & MEM_DYNTRANS_WRITE_OK)? "R/W" : "R"); + 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) + && 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]); + exit(1); + } } - debug("\n"); - mem->dev_name[mem->n_mmapped_devices] = strdup(device_name); - mem->dev_baseaddr[mem->n_mmapped_devices] = baseaddr; - 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; + mem->n_mmapped_devices++; - if (mem->dev_name[mem->n_mmapped_devices] == NULL) { + /* + * YUCK! This is ugly. TODO: fix + */ + /* 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 (mem->dev_name[newi] == NULL) { fprintf(stderr, "out of memory\n"); exit(1); } - if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK) - && 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); } - if ((size_t)dyntrans_data & 7) { + if ((size_t)dyntrans_data & (sizeof(void *) - 1)) { fprintf(stderr, "memory_device_register():" " dyntrans_data not aligned correctly (%p)\n", dyntrans_data); 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->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; if (baseaddr < mem->mmap_dev_minaddr) mem->mmap_dev_minaddr = baseaddr & ~mem->dev_dyntrans_alignment; @@ -463,6 +489,8 @@ (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) * @@ -471,14 +499,12 @@ (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)); + [i+1], sizeof(uint64_t) * (MAX_DEVICES - i - 1)); memmove(&mem->dev_dyntrans_write_high[i], &mem->dev_dyntrans_write_high - [i+1], sizeof(void *) * (MAX_DEVICES - i - 1)); + [i+1], sizeof(uint64_t) * (MAX_DEVICES - i - 1)); } @@ -508,8 +534,8 @@ 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; @@ -532,8 +558,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) { @@ -547,3 +572,44 @@ return (unsigned char *) table[entry]; } + +#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