--- trunk/src/memory.c 2007/10/08 16:19:23 20 +++ trunk/src/memory.c 2007/10/08 16:19:37 22 @@ -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.182 2005/11/22 16:26:36 debug Exp $ + * $Id: memory.c,v 1.187 2006/01/14 12:51:59 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; /* @@ -194,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) @@ -223,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) @@ -268,7 +265,7 @@ /* * 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) @@ -283,6 +280,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; @@ -336,32 +334,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 @@ -373,7 +345,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 " @@ -381,43 +353,107 @@ 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 (verbose >= 2) { + /* (40 bits of physical address is displayed) */ + debug("device at 0x%010llx: %s", (long long)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); + } - 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); + if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) { + debug(" (dyntrans %s)", + (flags & DM_DYNTRANS_WRITE_OK)? "R/W" : "R"); + } + debug("\n"); } - if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) { - debug(" (dyntrans %s)", - (flags & DM_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_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; + 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); } @@ -436,11 +472,10 @@ 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; @@ -475,6 +510,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) * @@ -483,14 +520,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)); } @@ -544,8 +579,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) {