--- trunk/src/memory.c 2007/10/08 16:20:26 28 +++ trunk/src/memory.c 2007/10/08 16:22:11 40 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2006 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.192 2006/07/14 16:33:27 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. */ @@ -43,6 +43,7 @@ extern int verbose; +extern int quiet_mode; /* @@ -120,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; } @@ -278,18 +289,18 @@ 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; @@ -298,10 +309,10 @@ 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); } @@ -327,12 +338,12 @@ int i; for (i=0; in_mmapped_devices; i++) { - if (mem->dev_extra[i] != extra) + if (mem->devices[i].extra != extra) continue; - mem->dev_dyntrans_data[i] = data; - mem->dev_dyntrans_write_low[i] = (uint64_t)-1; - mem->dev_dyntrans_write_high[i] = 0; + mem->devices[i].dyntrans_data = data; + mem->devices[i].dyntrans_write_low = (uint64_t)-1; + mem->devices[i].dyntrans_write_high = 0; } } @@ -340,8 +351,7 @@ /* * 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, @@ -351,35 +361,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) @@ -408,56 +412,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); } @@ -476,29 +465,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 --; @@ -506,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_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; } @@ -626,7 +601,7 @@ { uint64_t internal_state = 0x80624185376feff2ULL; uint64_t checksum = 0xcb9a87d5c010072cULL; - const int n_entries = (1 << BITS_PER_PAGETABLE) - 1; + 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; @@ -646,3 +621,62 @@ return checksum; } + +/* + * memory_warn_about_unimplemented_addr(): + * + * Called from memory_rw whenever memory outside of the physical address space + * is accessed (and quiet_mode isn't set). + */ +void memory_warn_about_unimplemented_addr(struct cpu *cpu, struct memory *mem, + int writeflag, uint64_t paddr, uint8_t *data, size_t len) +{ + uint64_t offset, old_pc = cpu->pc; + 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); + } +} +