25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: memory.c,v 1.163 2005/03/14 12:49:17 debug Exp $ |
* $Id: memory.c,v 1.180 2005/10/25 15:51:02 debug Exp $ |
29 |
* |
* |
30 |
* Functions for handling the memory of an emulated machine. |
* Functions for handling the memory of an emulated machine. |
31 |
*/ |
*/ |
108 |
* zeroed_alloc(): |
* zeroed_alloc(): |
109 |
* |
* |
110 |
* Allocates a block of memory using mmap(), and if that fails, try |
* Allocates a block of memory using mmap(), and if that fails, try |
111 |
* malloc() + memset(). |
* malloc() + memset(). The returned memory block contains only zeroes. |
112 |
*/ |
*/ |
113 |
void *zeroed_alloc(size_t s) |
void *zeroed_alloc(size_t s) |
114 |
{ |
{ |
132 |
* This function creates a new memory object. An emulated machine needs one |
* This function creates a new memory object. An emulated machine needs one |
133 |
* of these. |
* of these. |
134 |
*/ |
*/ |
135 |
struct memory *memory_new(uint64_t physical_max) |
struct memory *memory_new(uint64_t physical_max, int arch) |
136 |
{ |
{ |
137 |
struct memory *mem; |
struct memory *mem; |
138 |
int bits_per_pagetable = BITS_PER_PAGETABLE; |
int bits_per_pagetable = BITS_PER_PAGETABLE; |
157 |
} |
} |
158 |
|
|
159 |
mem->physical_max = physical_max; |
mem->physical_max = physical_max; |
160 |
|
mem->dev_dyntrans_alignment = 4095; |
161 |
|
if (arch == ARCH_ALPHA) |
162 |
|
mem->dev_dyntrans_alignment = 8191; |
163 |
|
|
164 |
s = entries_per_pagetable * sizeof(void *); |
s = entries_per_pagetable * sizeof(void *); |
165 |
|
|
256 |
|
|
257 |
|
|
258 |
/* |
/* |
259 |
* memory_device_bintrans_access(): |
* memory_device_dyntrans_access(): |
260 |
* |
* |
261 |
* Get the lowest and highest bintrans access since last time. |
* Get the lowest and highest dyntrans (or bintrans) access since last time. |
262 |
*/ |
*/ |
263 |
void memory_device_bintrans_access(struct cpu *cpu, struct memory *mem, |
void memory_device_dyntrans_access(struct cpu *cpu, struct memory *mem, |
264 |
void *extra, uint64_t *low, uint64_t *high) |
void *extra, uint64_t *low, uint64_t *high) |
265 |
{ |
{ |
|
#ifdef BINTRANS |
|
266 |
int i, j; |
int i, j; |
267 |
size_t s; |
size_t s; |
268 |
int need_inval = 0; |
int need_inval = 0; |
273 |
|
|
274 |
for (i=0; i<mem->n_mmapped_devices; i++) { |
for (i=0; i<mem->n_mmapped_devices; i++) { |
275 |
if (mem->dev_extra[i] == extra && |
if (mem->dev_extra[i] == extra && |
276 |
mem->dev_bintrans_data[i] != NULL) { |
mem->dev_dyntrans_data[i] != NULL) { |
277 |
if (mem->dev_bintrans_write_low[i] != (uint64_t) -1) |
if (mem->dev_dyntrans_write_low[i] != (uint64_t) -1) |
278 |
need_inval = 1; |
need_inval = 1; |
279 |
if (low != NULL) |
if (low != NULL) |
280 |
*low = mem->dev_bintrans_write_low[i]; |
*low = mem->dev_dyntrans_write_low[i]; |
281 |
mem->dev_bintrans_write_low[i] = (uint64_t) -1; |
mem->dev_dyntrans_write_low[i] = (uint64_t) -1; |
282 |
|
|
283 |
if (high != NULL) |
if (high != NULL) |
284 |
*high = mem->dev_bintrans_write_high[i]; |
*high = mem->dev_dyntrans_write_high[i]; |
285 |
mem->dev_bintrans_write_high[i] = 0; |
mem->dev_dyntrans_write_high[i] = 0; |
286 |
|
|
287 |
if (!need_inval) |
if (!need_inval) |
288 |
return; |
return; |
289 |
|
|
|
if (cpu->machine->arch != ARCH_MIPS) { |
|
|
/* TODO! */ |
|
|
|
|
|
return; |
|
|
} |
|
|
|
|
290 |
/* Invalidate any pages of this device that might |
/* Invalidate any pages of this device that might |
291 |
be in the bintrans load/store cache, by marking |
be in the dyntrans load/store cache, by marking |
292 |
the pages read-only. */ |
the pages read-only. */ |
293 |
|
if (cpu->invalidate_translation_caches != NULL) { |
294 |
for (s=0; s<mem->dev_length[i]; s+=4096) { |
for (s=0; s<mem->dev_length[i]; |
295 |
mips_invalidate_translation_caches_paddr( |
s+=cpu->machine->arch_pagesize) |
296 |
cpu, mem->dev_baseaddr[i] + s); |
cpu->invalidate_translation_caches |
297 |
|
(cpu, mem->dev_baseaddr[i] + s, |
298 |
|
JUST_MARK_AS_NON_WRITABLE |
299 |
|
| INVALIDATE_PADDR); |
300 |
} |
} |
301 |
|
|
302 |
/* ... and invalidate the "fast_vaddr_to_hostaddr" |
if (cpu->machine->arch == ARCH_MIPS) { |
303 |
cache entries that contain pointers to this |
/* |
304 |
device: (NOTE: Device i, cache entry j) */ |
* ... and invalidate the "fast_vaddr_to_ |
305 |
for (j=0; j<N_BINTRANS_VADDR_TO_HOST; j++) { |
* hostaddr" cache entries that contain |
306 |
if (cpu->cd.mips.bintrans_data_hostpage[j] >= |
* pointers to this device: (NOTE: Device i, |
307 |
mem->dev_bintrans_data[i] && |
* cache entry j) |
308 |
cpu->cd.mips.bintrans_data_hostpage[j] < |
*/ |
309 |
mem->dev_bintrans_data[i] + |
for (j=0; j<N_BINTRANS_VADDR_TO_HOST; j++) { |
310 |
mem->dev_length[i]) |
if (cpu->cd. |
311 |
cpu->cd.mips. |
mips.bintrans_data_hostpage[j] >= |
312 |
bintrans_data_hostpage[j] = NULL; |
mem->dev_dyntrans_data[i] && |
313 |
|
cpu->cd.mips. |
314 |
|
bintrans_data_hostpage[j] < |
315 |
|
mem->dev_dyntrans_data[i] + |
316 |
|
mem->dev_length[i]) |
317 |
|
cpu->cd.mips. |
318 |
|
bintrans_data_hostpage[j] |
319 |
|
= NULL; |
320 |
|
} |
321 |
} |
} |
|
|
|
322 |
return; |
return; |
323 |
} |
} |
324 |
} |
} |
|
#endif |
|
325 |
} |
} |
326 |
|
|
327 |
|
|
361 |
uint64_t baseaddr, uint64_t len, |
uint64_t baseaddr, uint64_t len, |
362 |
int (*f)(struct cpu *,struct memory *,uint64_t,unsigned char *, |
int (*f)(struct cpu *,struct memory *,uint64_t,unsigned char *, |
363 |
size_t,int,void *), |
size_t,int,void *), |
364 |
void *extra, int flags, unsigned char *bintrans_data) |
void *extra, int flags, unsigned char *dyntrans_data) |
365 |
{ |
{ |
366 |
int i; |
int i; |
367 |
|
|
388 |
debug("device %2i at 0x%010llx: %s", |
debug("device %2i at 0x%010llx: %s", |
389 |
mem->n_mmapped_devices, (long long)baseaddr, device_name); |
mem->n_mmapped_devices, (long long)baseaddr, device_name); |
390 |
|
|
391 |
#ifdef BINTRANS |
if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK) |
392 |
if (flags & (MEM_BINTRANS_OK | MEM_BINTRANS_WRITE_OK) |
&& (baseaddr & mem->dev_dyntrans_alignment) != 0) { |
393 |
&& (baseaddr & 0xfff) != 0) { |
fatal("\nWARNING: Device dyntrans access, but unaligned" |
|
fatal("\nWARNING: Device bintrans access, but unaligned" |
|
394 |
" baseaddr 0x%llx.\n", (long long)baseaddr); |
" baseaddr 0x%llx.\n", (long long)baseaddr); |
395 |
} |
} |
396 |
|
|
397 |
if (flags & (MEM_BINTRANS_OK | MEM_BINTRANS_WRITE_OK)) { |
if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK)) { |
398 |
debug(" (bintrans %s)", |
debug(" (dyntrans %s)", |
399 |
(flags & MEM_BINTRANS_WRITE_OK)? "R/W" : "R"); |
(flags & MEM_DYNTRANS_WRITE_OK)? "R/W" : "R"); |
400 |
} |
} |
|
#endif |
|
401 |
debug("\n"); |
debug("\n"); |
402 |
|
|
403 |
mem->dev_name[mem->n_mmapped_devices] = strdup(device_name); |
mem->dev_name[mem->n_mmapped_devices] = strdup(device_name); |
404 |
mem->dev_baseaddr[mem->n_mmapped_devices] = baseaddr; |
mem->dev_baseaddr[mem->n_mmapped_devices] = baseaddr; |
405 |
|
mem->dev_endaddr[mem->n_mmapped_devices] = baseaddr + len; |
406 |
mem->dev_length[mem->n_mmapped_devices] = len; |
mem->dev_length[mem->n_mmapped_devices] = len; |
407 |
mem->dev_flags[mem->n_mmapped_devices] = flags; |
mem->dev_flags[mem->n_mmapped_devices] = flags; |
408 |
mem->dev_bintrans_data[mem->n_mmapped_devices] = bintrans_data; |
mem->dev_dyntrans_data[mem->n_mmapped_devices] = dyntrans_data; |
409 |
|
|
410 |
if (mem->dev_name[mem->n_mmapped_devices] == NULL) { |
if (mem->dev_name[mem->n_mmapped_devices] == NULL) { |
411 |
fprintf(stderr, "out of memory\n"); |
fprintf(stderr, "out of memory\n"); |
412 |
exit(1); |
exit(1); |
413 |
} |
} |
414 |
|
|
415 |
if ((size_t)bintrans_data & 1) { |
if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK) |
416 |
|
&& !(flags & MEM_EMULATED_RAM) && dyntrans_data == NULL) { |
417 |
|
fatal("\nERROR: Device dyntrans access, but dyntrans_data" |
418 |
|
" = NULL!\n"); |
419 |
|
exit(1); |
420 |
|
} |
421 |
|
|
422 |
|
if ((size_t)dyntrans_data & (sizeof(void *) - 1)) { |
423 |
fprintf(stderr, "memory_device_register():" |
fprintf(stderr, "memory_device_register():" |
424 |
" bintrans_data not aligned correctly\n"); |
" dyntrans_data not aligned correctly (%p)\n", |
425 |
|
dyntrans_data); |
426 |
exit(1); |
exit(1); |
427 |
} |
} |
428 |
|
|
429 |
#ifdef BINTRANS |
mem->dev_dyntrans_write_low[mem->n_mmapped_devices] = (uint64_t)-1; |
430 |
mem->dev_bintrans_write_low[mem->n_mmapped_devices] = (uint64_t)-1; |
mem->dev_dyntrans_write_high[mem->n_mmapped_devices] = 0; |
|
mem->dev_bintrans_write_high[mem->n_mmapped_devices] = 0; |
|
|
#endif |
|
431 |
mem->dev_f[mem->n_mmapped_devices] = f; |
mem->dev_f[mem->n_mmapped_devices] = f; |
432 |
mem->dev_extra[mem->n_mmapped_devices] = extra; |
mem->dev_extra[mem->n_mmapped_devices] = extra; |
433 |
mem->n_mmapped_devices++; |
mem->n_mmapped_devices++; |
434 |
|
|
435 |
if (baseaddr < mem->mmap_dev_minaddr) |
if (baseaddr < mem->mmap_dev_minaddr) |
436 |
mem->mmap_dev_minaddr = baseaddr; |
mem->mmap_dev_minaddr = baseaddr & ~mem->dev_dyntrans_alignment; |
437 |
if (baseaddr + len > mem->mmap_dev_maxaddr) |
if (baseaddr + len > mem->mmap_dev_maxaddr) |
438 |
mem->mmap_dev_maxaddr = baseaddr + len; |
mem->mmap_dev_maxaddr = (((baseaddr + len) - 1) | |
439 |
|
mem->dev_dyntrans_alignment) + 1; |
440 |
} |
} |
441 |
|
|
442 |
|
|
475 |
(MAX_DEVICES - i - 1)); |
(MAX_DEVICES - i - 1)); |
476 |
memmove(&mem->dev_f_state[i], &mem->dev_f_state[i+1], sizeof(void *) * |
memmove(&mem->dev_f_state[i], &mem->dev_f_state[i+1], sizeof(void *) * |
477 |
(MAX_DEVICES - i - 1)); |
(MAX_DEVICES - i - 1)); |
478 |
memmove(&mem->dev_bintrans_data[i], &mem->dev_bintrans_data[i+1], |
memmove(&mem->dev_dyntrans_data[i], &mem->dev_dyntrans_data[i+1], |
479 |
sizeof(void *) * (MAX_DEVICES - i - 1)); |
sizeof(void *) * (MAX_DEVICES - i - 1)); |
480 |
#ifdef BINTRANS |
memmove(&mem->dev_dyntrans_write_low[i], &mem->dev_dyntrans_write_low |
|
memmove(&mem->dev_bintrans_write_low[i], &mem->dev_bintrans_write_low |
|
481 |
[i+1], sizeof(void *) * (MAX_DEVICES - i - 1)); |
[i+1], sizeof(void *) * (MAX_DEVICES - i - 1)); |
482 |
memmove(&mem->dev_bintrans_write_high[i], &mem->dev_bintrans_write_high |
memmove(&mem->dev_dyntrans_write_high[i], &mem->dev_dyntrans_write_high |
483 |
[i+1], sizeof(void *) * (MAX_DEVICES - i - 1)); |
[i+1], sizeof(void *) * (MAX_DEVICES - i - 1)); |
|
#endif |
|
484 |
} |
} |
485 |
|
|
486 |
|
|
510 |
table = mem->pagetable; |
table = mem->pagetable; |
511 |
entry = (paddr >> shrcount) & mask; |
entry = (paddr >> shrcount) & mask; |
512 |
|
|
513 |
/* printf(" entry = %x\n", entry); */ |
/* printf("memory_paddr_to_hostaddr(): p=%16llx w=%i => entry=0x%x\n", |
514 |
|
(long long)paddr, writeflag, entry); */ |
515 |
|
|
516 |
if (table[entry] == NULL) { |
if (table[entry] == NULL) { |
517 |
size_t alloclen; |
size_t alloclen; |