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.176 2005/08/28 20:16:23 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_paddr != 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_paddr |
297 |
|
(cpu, mem->dev_baseaddr[i] + s, |
298 |
|
JUST_MARK_AS_NON_WRITABLE); |
299 |
} |
} |
300 |
|
|
301 |
/* ... and invalidate the "fast_vaddr_to_hostaddr" |
if (cpu->machine->arch == ARCH_MIPS) { |
302 |
cache entries that contain pointers to this |
/* |
303 |
device: (NOTE: Device i, cache entry j) */ |
* ... and invalidate the "fast_vaddr_to_ |
304 |
for (j=0; j<N_BINTRANS_VADDR_TO_HOST; j++) { |
* hostaddr" cache entries that contain |
305 |
if (cpu->cd.mips.bintrans_data_hostpage[j] >= |
* pointers to this device: (NOTE: Device i, |
306 |
mem->dev_bintrans_data[i] && |
* cache entry j) |
307 |
cpu->cd.mips.bintrans_data_hostpage[j] < |
*/ |
308 |
mem->dev_bintrans_data[i] + |
for (j=0; j<N_BINTRANS_VADDR_TO_HOST; j++) { |
309 |
mem->dev_length[i]) |
if (cpu->cd. |
310 |
cpu->cd.mips. |
mips.bintrans_data_hostpage[j] >= |
311 |
bintrans_data_hostpage[j] = NULL; |
mem->dev_dyntrans_data[i] && |
312 |
|
cpu->cd.mips. |
313 |
|
bintrans_data_hostpage[j] < |
314 |
|
mem->dev_dyntrans_data[i] + |
315 |
|
mem->dev_length[i]) |
316 |
|
cpu->cd.mips. |
317 |
|
bintrans_data_hostpage[j] |
318 |
|
= NULL; |
319 |
|
} |
320 |
} |
} |
|
|
|
321 |
return; |
return; |
322 |
} |
} |
323 |
} |
} |
|
#endif |
|
324 |
} |
} |
325 |
|
|
326 |
|
|
360 |
uint64_t baseaddr, uint64_t len, |
uint64_t baseaddr, uint64_t len, |
361 |
int (*f)(struct cpu *,struct memory *,uint64_t,unsigned char *, |
int (*f)(struct cpu *,struct memory *,uint64_t,unsigned char *, |
362 |
size_t,int,void *), |
size_t,int,void *), |
363 |
void *extra, int flags, unsigned char *bintrans_data) |
void *extra, int flags, unsigned char *dyntrans_data) |
364 |
{ |
{ |
365 |
int i; |
int i; |
366 |
|
|
387 |
debug("device %2i at 0x%010llx: %s", |
debug("device %2i at 0x%010llx: %s", |
388 |
mem->n_mmapped_devices, (long long)baseaddr, device_name); |
mem->n_mmapped_devices, (long long)baseaddr, device_name); |
389 |
|
|
390 |
#ifdef BINTRANS |
if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK) |
391 |
if (flags & (MEM_BINTRANS_OK | MEM_BINTRANS_WRITE_OK) |
&& (baseaddr & mem->dev_dyntrans_alignment) != 0) { |
392 |
&& (baseaddr & 0xfff) != 0) { |
fatal("\nWARNING: Device dyntrans access, but unaligned" |
|
fatal("\nWARNING: Device bintrans access, but unaligned" |
|
393 |
" baseaddr 0x%llx.\n", (long long)baseaddr); |
" baseaddr 0x%llx.\n", (long long)baseaddr); |
394 |
} |
} |
395 |
|
|
396 |
if (flags & (MEM_BINTRANS_OK | MEM_BINTRANS_WRITE_OK)) { |
if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK)) { |
397 |
debug(" (bintrans %s)", |
debug(" (dyntrans %s)", |
398 |
(flags & MEM_BINTRANS_WRITE_OK)? "R/W" : "R"); |
(flags & MEM_DYNTRANS_WRITE_OK)? "R/W" : "R"); |
399 |
} |
} |
|
#endif |
|
400 |
debug("\n"); |
debug("\n"); |
401 |
|
|
402 |
mem->dev_name[mem->n_mmapped_devices] = strdup(device_name); |
mem->dev_name[mem->n_mmapped_devices] = strdup(device_name); |
403 |
mem->dev_baseaddr[mem->n_mmapped_devices] = baseaddr; |
mem->dev_baseaddr[mem->n_mmapped_devices] = baseaddr; |
404 |
mem->dev_length[mem->n_mmapped_devices] = len; |
mem->dev_length[mem->n_mmapped_devices] = len; |
405 |
mem->dev_flags[mem->n_mmapped_devices] = flags; |
mem->dev_flags[mem->n_mmapped_devices] = flags; |
406 |
mem->dev_bintrans_data[mem->n_mmapped_devices] = bintrans_data; |
mem->dev_dyntrans_data[mem->n_mmapped_devices] = dyntrans_data; |
407 |
|
|
408 |
if (mem->dev_name[mem->n_mmapped_devices] == NULL) { |
if (mem->dev_name[mem->n_mmapped_devices] == NULL) { |
409 |
fprintf(stderr, "out of memory\n"); |
fprintf(stderr, "out of memory\n"); |
410 |
exit(1); |
exit(1); |
411 |
} |
} |
412 |
|
|
413 |
if ((size_t)bintrans_data & 1) { |
if (flags & (MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK) |
414 |
|
&& dyntrans_data == NULL) { |
415 |
|
fatal("\nERROR: Device dyntrans access, but dyntrans_data" |
416 |
|
" = NULL!\n"); |
417 |
|
exit(1); |
418 |
|
} |
419 |
|
|
420 |
|
if ((size_t)dyntrans_data & 7) { |
421 |
fprintf(stderr, "memory_device_register():" |
fprintf(stderr, "memory_device_register():" |
422 |
" bintrans_data not aligned correctly\n"); |
" dyntrans_data not aligned correctly (%p)\n", |
423 |
|
dyntrans_data); |
424 |
exit(1); |
exit(1); |
425 |
} |
} |
426 |
|
|
427 |
#ifdef BINTRANS |
mem->dev_dyntrans_write_low[mem->n_mmapped_devices] = (uint64_t)-1; |
428 |
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 |
|
429 |
mem->dev_f[mem->n_mmapped_devices] = f; |
mem->dev_f[mem->n_mmapped_devices] = f; |
430 |
mem->dev_extra[mem->n_mmapped_devices] = extra; |
mem->dev_extra[mem->n_mmapped_devices] = extra; |
431 |
mem->n_mmapped_devices++; |
mem->n_mmapped_devices++; |
432 |
|
|
433 |
if (baseaddr < mem->mmap_dev_minaddr) |
if (baseaddr < mem->mmap_dev_minaddr) |
434 |
mem->mmap_dev_minaddr = baseaddr; |
mem->mmap_dev_minaddr = baseaddr & ~mem->dev_dyntrans_alignment; |
435 |
if (baseaddr + len > mem->mmap_dev_maxaddr) |
if (baseaddr + len > mem->mmap_dev_maxaddr) |
436 |
mem->mmap_dev_maxaddr = baseaddr + len; |
mem->mmap_dev_maxaddr = (((baseaddr + len) - 1) | |
437 |
|
mem->dev_dyntrans_alignment) + 1; |
438 |
} |
} |
439 |
|
|
440 |
|
|
473 |
(MAX_DEVICES - i - 1)); |
(MAX_DEVICES - i - 1)); |
474 |
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 *) * |
475 |
(MAX_DEVICES - i - 1)); |
(MAX_DEVICES - i - 1)); |
476 |
memmove(&mem->dev_bintrans_data[i], &mem->dev_bintrans_data[i+1], |
memmove(&mem->dev_dyntrans_data[i], &mem->dev_dyntrans_data[i+1], |
477 |
sizeof(void *) * (MAX_DEVICES - i - 1)); |
sizeof(void *) * (MAX_DEVICES - i - 1)); |
478 |
#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 |
|
479 |
[i+1], sizeof(void *) * (MAX_DEVICES - i - 1)); |
[i+1], sizeof(void *) * (MAX_DEVICES - i - 1)); |
480 |
memmove(&mem->dev_bintrans_write_high[i], &mem->dev_bintrans_write_high |
memmove(&mem->dev_dyntrans_write_high[i], &mem->dev_dyntrans_write_high |
481 |
[i+1], sizeof(void *) * (MAX_DEVICES - i - 1)); |
[i+1], sizeof(void *) * (MAX_DEVICES - i - 1)); |
|
#endif |
|
482 |
} |
} |
483 |
|
|
484 |
|
|
508 |
table = mem->pagetable; |
table = mem->pagetable; |
509 |
entry = (paddr >> shrcount) & mask; |
entry = (paddr >> shrcount) & mask; |
510 |
|
|
511 |
/* printf(" entry = %x\n", entry); */ |
/* printf("memory_paddr_to_hostaddr(): p=%16llx w=%i => entry=0x%x\n", |
512 |
|
(long long)paddr, writeflag, entry); */ |
513 |
|
|
514 |
if (table[entry] == NULL) { |
if (table[entry] == NULL) { |
515 |
size_t alloclen; |
size_t alloclen; |