/[gxemul]/upstream/0.4.0/src/memory.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /upstream/0.4.0/src/memory.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (hide annotations)
Mon Oct 8 16:20:03 2007 UTC (16 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 17036 byte(s)
0.4.0
1 dpavlin 2 /*
2 dpavlin 22 * Copyright (C) 2003-2006 Anders Gavare. All rights reserved.
3 dpavlin 2 *
4     * Redistribution and use in source and binary forms, with or without
5     * modification, are permitted provided that the following conditions are met:
6     *
7     * 1. Redistributions of source code must retain the above copyright
8     * notice, this list of conditions and the following disclaimer.
9     * 2. Redistributions in binary form must reproduce the above copyright
10     * notice, this list of conditions and the following disclaimer in the
11     * documentation and/or other materials provided with the distribution.
12     * 3. The name of the author may not be used to endorse or promote products
13     * derived from this software without specific prior written permission.
14     *
15     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18     * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20     * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21     * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24     * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25     * SUCH DAMAGE.
26     *
27     *
28 dpavlin 24 * $Id: memory.c,v 1.190 2006/06/16 18:31:25 debug Exp $
29 dpavlin 2 *
30     * Functions for handling the memory of an emulated machine.
31     */
32    
33     #include <stdio.h>
34     #include <stdlib.h>
35     #include <string.h>
36     #include <sys/types.h>
37     #include <sys/mman.h>
38    
39     #include "cpu.h"
40     #include "machine.h"
41     #include "memory.h"
42     #include "misc.h"
43    
44    
45 dpavlin 22 extern int verbose;
46 dpavlin 2
47    
48     /*
49     * memory_readmax64():
50     *
51     * Read at most 64 bits of data from a buffer. Length is given by
52     * len, and the byte order by cpu->byte_order.
53     *
54     * This function should not be called with cpu == NULL.
55     */
56     uint64_t memory_readmax64(struct cpu *cpu, unsigned char *buf, int len)
57     {
58 dpavlin 20 int i, byte_order = cpu->byte_order;
59 dpavlin 2 uint64_t x = 0;
60    
61 dpavlin 20 if (len & MEM_PCI_LITTLE_ENDIAN) {
62     len &= ~MEM_PCI_LITTLE_ENDIAN;
63     byte_order = EMUL_LITTLE_ENDIAN;
64     }
65    
66 dpavlin 2 /* Switch byte order for incoming data, if necessary: */
67 dpavlin 20 if (byte_order == EMUL_BIG_ENDIAN)
68 dpavlin 2 for (i=0; i<len; i++) {
69     x <<= 8;
70     x |= buf[i];
71     }
72     else
73     for (i=len-1; i>=0; i--) {
74     x <<= 8;
75     x |= buf[i];
76     }
77    
78     return x;
79     }
80    
81    
82     /*
83     * memory_writemax64():
84     *
85     * Write at most 64 bits of data to a buffer. Length is given by
86     * len, and the byte order by cpu->byte_order.
87     *
88     * This function should not be called with cpu == NULL.
89     */
90     void memory_writemax64(struct cpu *cpu, unsigned char *buf, int len,
91     uint64_t data)
92     {
93 dpavlin 20 int i, byte_order = cpu->byte_order;
94 dpavlin 2
95 dpavlin 20 if (len & MEM_PCI_LITTLE_ENDIAN) {
96     len &= ~MEM_PCI_LITTLE_ENDIAN;
97     byte_order = EMUL_LITTLE_ENDIAN;
98     }
99    
100     if (byte_order == EMUL_LITTLE_ENDIAN)
101 dpavlin 2 for (i=0; i<len; i++) {
102     buf[i] = data & 255;
103     data >>= 8;
104     }
105     else
106     for (i=0; i<len; i++) {
107     buf[len - 1 - i] = data & 255;
108     data >>= 8;
109     }
110     }
111    
112    
113     /*
114     * zeroed_alloc():
115     *
116     * Allocates a block of memory using mmap(), and if that fails, try
117 dpavlin 12 * malloc() + memset(). The returned memory block contains only zeroes.
118 dpavlin 2 */
119     void *zeroed_alloc(size_t s)
120     {
121     void *p = mmap(NULL, s, PROT_READ | PROT_WRITE,
122     MAP_ANON | MAP_PRIVATE, -1, 0);
123     if (p == NULL) {
124     p = malloc(s);
125     if (p == NULL) {
126     fprintf(stderr, "out of memory\n");
127     exit(1);
128     }
129     memset(p, 0, s);
130     }
131     return p;
132     }
133    
134    
135     /*
136     * memory_new():
137     *
138     * This function creates a new memory object. An emulated machine needs one
139     * of these.
140     */
141 dpavlin 12 struct memory *memory_new(uint64_t physical_max, int arch)
142 dpavlin 2 {
143     struct memory *mem;
144     int bits_per_pagetable = BITS_PER_PAGETABLE;
145     int bits_per_memblock = BITS_PER_MEMBLOCK;
146     int entries_per_pagetable = 1 << BITS_PER_PAGETABLE;
147     int max_bits = MAX_BITS;
148     size_t s;
149    
150     mem = malloc(sizeof(struct memory));
151     if (mem == NULL) {
152     fprintf(stderr, "out of memory\n");
153     exit(1);
154     }
155    
156     memset(mem, 0, sizeof(struct memory));
157    
158     /* Check bits_per_pagetable and bits_per_memblock for sanity: */
159     if (bits_per_pagetable + bits_per_memblock != max_bits) {
160     fprintf(stderr, "memory_new(): bits_per_pagetable and "
161     "bits_per_memblock mismatch\n");
162     exit(1);
163     }
164    
165     mem->physical_max = physical_max;
166 dpavlin 12 mem->dev_dyntrans_alignment = 4095;
167     if (arch == ARCH_ALPHA)
168     mem->dev_dyntrans_alignment = 8191;
169 dpavlin 2
170     s = entries_per_pagetable * sizeof(void *);
171    
172     mem->pagetable = (unsigned char *) mmap(NULL, s,
173     PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
174     if (mem->pagetable == NULL) {
175     mem->pagetable = malloc(s);
176     if (mem->pagetable == NULL) {
177     fprintf(stderr, "out of memory\n");
178     exit(1);
179     }
180     memset(mem->pagetable, 0, s);
181     }
182    
183     mem->mmap_dev_minaddr = 0xffffffffffffffffULL;
184     mem->mmap_dev_maxaddr = 0;
185    
186     return mem;
187     }
188    
189    
190     /*
191     * memory_points_to_string():
192     *
193 dpavlin 22 * Returns 1 if there's something string-like in emulated memory at address
194     * addr, otherwise 0.
195 dpavlin 2 */
196     int memory_points_to_string(struct cpu *cpu, struct memory *mem, uint64_t addr,
197     int min_string_length)
198     {
199     int cur_length = 0;
200     unsigned char c;
201    
202     for (;;) {
203     c = '\0';
204     cpu->memory_rw(cpu, mem, addr+cur_length,
205     &c, sizeof(c), MEM_READ, CACHE_NONE | NO_EXCEPTIONS);
206     if (c=='\n' || c=='\t' || c=='\r' || (c>=' ' && c<127)) {
207     cur_length ++;
208     if (cur_length >= min_string_length)
209     return 1;
210     } else {
211     if (cur_length >= min_string_length)
212     return 1;
213     else
214     return 0;
215     }
216     }
217     }
218    
219    
220     /*
221     * memory_conv_to_string():
222     *
223 dpavlin 22 * Convert emulated memory contents to a string, placing it in a buffer
224     * provided by the caller.
225 dpavlin 2 */
226     char *memory_conv_to_string(struct cpu *cpu, struct memory *mem, uint64_t addr,
227     char *buf, int bufsize)
228     {
229     int len = 0;
230     int output_index = 0;
231     unsigned char c, p='\0';
232    
233     while (output_index < bufsize-1) {
234     c = '\0';
235     cpu->memory_rw(cpu, mem, addr+len, &c, sizeof(c), MEM_READ,
236     CACHE_NONE | NO_EXCEPTIONS);
237     buf[output_index] = c;
238     if (c>=' ' && c<127) {
239     len ++;
240     output_index ++;
241     } else if (c=='\n' || c=='\r' || c=='\t') {
242     len ++;
243     buf[output_index] = '\\';
244     output_index ++;
245     switch (c) {
246     case '\n': p = 'n'; break;
247     case '\r': p = 'r'; break;
248     case '\t': p = 't'; break;
249     }
250     if (output_index < bufsize-1) {
251     buf[output_index] = p;
252     output_index ++;
253     }
254     } else {
255     buf[output_index] = '\0';
256     return buf;
257     }
258     }
259    
260     buf[bufsize-1] = '\0';
261     return buf;
262     }
263    
264    
265     /*
266 dpavlin 12 * memory_device_dyntrans_access():
267 dpavlin 2 *
268 dpavlin 22 * Get the lowest and highest dyntrans access since last time.
269 dpavlin 2 */
270 dpavlin 12 void memory_device_dyntrans_access(struct cpu *cpu, struct memory *mem,
271 dpavlin 2 void *extra, uint64_t *low, uint64_t *high)
272     {
273     size_t s;
274 dpavlin 24 int i, need_inval = 0;
275 dpavlin 2
276     /* TODO: This is O(n), so it might be good to rewrite it some day.
277     For now, it will be enough, as long as this function is not
278     called too often. */
279    
280     for (i=0; i<mem->n_mmapped_devices; i++) {
281     if (mem->dev_extra[i] == extra &&
282 dpavlin 22 mem->dev_flags[i] & DM_DYNTRANS_WRITE_OK &&
283 dpavlin 12 mem->dev_dyntrans_data[i] != NULL) {
284     if (mem->dev_dyntrans_write_low[i] != (uint64_t) -1)
285 dpavlin 2 need_inval = 1;
286     if (low != NULL)
287 dpavlin 12 *low = mem->dev_dyntrans_write_low[i];
288     mem->dev_dyntrans_write_low[i] = (uint64_t) -1;
289 dpavlin 2
290     if (high != NULL)
291 dpavlin 12 *high = mem->dev_dyntrans_write_high[i];
292     mem->dev_dyntrans_write_high[i] = 0;
293 dpavlin 2
294     if (!need_inval)
295     return;
296    
297     /* Invalidate any pages of this device that might
298 dpavlin 12 be in the dyntrans load/store cache, by marking
299 dpavlin 2 the pages read-only. */
300 dpavlin 18 if (cpu->invalidate_translation_caches != NULL) {
301 dpavlin 12 for (s=0; s<mem->dev_length[i];
302     s+=cpu->machine->arch_pagesize)
303 dpavlin 18 cpu->invalidate_translation_caches
304 dpavlin 14 (cpu, mem->dev_baseaddr[i] + s,
305 dpavlin 18 JUST_MARK_AS_NON_WRITABLE
306     | INVALIDATE_PADDR);
307 dpavlin 2 }
308    
309     return;
310     }
311     }
312     }
313    
314    
315     /*
316     * memory_device_register():
317     *
318     * Register a (memory mapped) device by adding it to the dev_* fields of a
319     * memory struct.
320     */
321     void memory_device_register(struct memory *mem, const char *device_name,
322     uint64_t baseaddr, uint64_t len,
323     int (*f)(struct cpu *,struct memory *,uint64_t,unsigned char *,
324     size_t,int,void *),
325 dpavlin 12 void *extra, int flags, unsigned char *dyntrans_data)
326 dpavlin 2 {
327 dpavlin 22 int i, newi = 0;
328 dpavlin 2
329     if (mem->n_mmapped_devices >= MAX_DEVICES) {
330     fprintf(stderr, "memory_device_register(): too many "
331     "devices registered, cannot register '%s'\n", device_name);
332     exit(1);
333     }
334    
335 dpavlin 22 /*
336     * Figure out at which index to insert this device, and simultaneously
337     * check for collisions:
338     */
339     newi = -1;
340 dpavlin 2 for (i=0; i<mem->n_mmapped_devices; i++) {
341 dpavlin 22 if (i == 0 && baseaddr + len <= mem->dev_baseaddr[i])
342     newi = i;
343     if (i > 0 && baseaddr + len <= mem->dev_baseaddr[i] &&
344     baseaddr >= mem->dev_endaddr[i-1])
345     newi = i;
346     if (i == mem->n_mmapped_devices - 1 &&
347     baseaddr >= mem->dev_endaddr[i])
348     newi = i + 1;
349    
350 dpavlin 2 /* If we are not colliding with device i, then continue: */
351     if (baseaddr + len <= mem->dev_baseaddr[i])
352     continue;
353 dpavlin 22 if (baseaddr >= mem->dev_endaddr[i])
354 dpavlin 2 continue;
355    
356 dpavlin 22 fatal("\nERROR! \"%s\" collides with device %i (\"%s\")!\n",
357 dpavlin 2 device_name, i, mem->dev_name[i]);
358 dpavlin 22 exit(1);
359 dpavlin 2 }
360 dpavlin 22 if (mem->n_mmapped_devices == 0)
361     newi = 0;
362     if (newi == -1) {
363     fatal("INTERNAL ERROR\n");
364     exit(1);
365     }
366 dpavlin 2
367 dpavlin 22 if (verbose >= 2) {
368     /* (40 bits of physical address is displayed) */
369 dpavlin 24 debug("device at 0x%010"PRIx64": %s", (uint64_t) baseaddr,
370 dpavlin 22 device_name);
371 dpavlin 2
372 dpavlin 22 if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)
373     && (baseaddr & mem->dev_dyntrans_alignment) != 0) {
374     fatal("\nWARNING: Device dyntrans access, but unaligned"
375 dpavlin 24 " baseaddr 0x%"PRIx64".\n", (uint64_t) baseaddr);
376 dpavlin 22 }
377    
378     if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) {
379     debug(" (dyntrans %s)",
380     (flags & DM_DYNTRANS_WRITE_OK)? "R/W" : "R");
381     }
382     debug("\n");
383 dpavlin 2 }
384    
385 dpavlin 22 for (i=0; i<mem->n_mmapped_devices; i++) {
386     if (dyntrans_data == mem->dev_dyntrans_data[i] &&
387     mem->dev_flags[i] & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)
388     && flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) {
389     fatal("ERROR: the data pointer used for dyntrans "
390     "accesses must only be used once!\n");
391     fatal("(%p cannot be used by '%s'; already in use by '"
392     "%s')\n", dyntrans_data, device_name,
393     mem->dev_name[i]);
394     exit(1);
395     }
396 dpavlin 2 }
397    
398 dpavlin 22 mem->n_mmapped_devices++;
399 dpavlin 2
400 dpavlin 22 /*
401     * YUCK! This is ugly. TODO: fix
402     */
403     /* Make space for the new entry: */
404     memmove(&mem->dev_name[newi+1], &mem->dev_name[newi], sizeof(char *) *
405     (MAX_DEVICES - newi - 1));
406     memmove(&mem->dev_baseaddr[newi+1], &mem->dev_baseaddr[newi],
407     sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
408     memmove(&mem->dev_endaddr[newi+1], &mem->dev_endaddr[newi],
409     sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
410     memmove(&mem->dev_length[newi+1], &mem->dev_length[newi],
411     sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
412     memmove(&mem->dev_flags[newi+1], &mem->dev_flags[newi], sizeof(int) *
413     (MAX_DEVICES - newi - 1));
414     memmove(&mem->dev_extra[newi+1], &mem->dev_extra[newi], sizeof(void *) *
415     (MAX_DEVICES - newi - 1));
416     memmove(&mem->dev_f[newi+1], &mem->dev_f[newi], sizeof(void *) *
417     (MAX_DEVICES - newi - 1));
418     memmove(&mem->dev_dyntrans_data[newi+1], &mem->dev_dyntrans_data[newi],
419     sizeof(void *) * (MAX_DEVICES - newi - 1));
420     memmove(&mem->dev_dyntrans_write_low[newi+1],
421     &mem->dev_dyntrans_write_low[newi],
422     sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
423     memmove(&mem->dev_dyntrans_write_high[newi+1],
424     &mem->dev_dyntrans_write_high[newi],
425     sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
426    
427    
428     mem->dev_name[newi] = strdup(device_name);
429     mem->dev_baseaddr[newi] = baseaddr;
430     mem->dev_endaddr[newi] = baseaddr + len;
431     mem->dev_length[newi] = len;
432     mem->dev_flags[newi] = flags;
433     mem->dev_dyntrans_data[newi] = dyntrans_data;
434    
435     if (mem->dev_name[newi] == NULL) {
436 dpavlin 2 fprintf(stderr, "out of memory\n");
437     exit(1);
438     }
439    
440 dpavlin 20 if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)
441     && !(flags & DM_EMULATED_RAM) && dyntrans_data == NULL) {
442 dpavlin 12 fatal("\nERROR: Device dyntrans access, but dyntrans_data"
443     " = NULL!\n");
444     exit(1);
445     }
446    
447 dpavlin 18 if ((size_t)dyntrans_data & (sizeof(void *) - 1)) {
448 dpavlin 2 fprintf(stderr, "memory_device_register():"
449 dpavlin 12 " dyntrans_data not aligned correctly (%p)\n",
450     dyntrans_data);
451 dpavlin 2 exit(1);
452     }
453    
454 dpavlin 22 mem->dev_dyntrans_write_low[newi] = (uint64_t)-1;
455     mem->dev_dyntrans_write_high[newi] = 0;
456     mem->dev_f[newi] = f;
457     mem->dev_extra[newi] = extra;
458 dpavlin 2
459     if (baseaddr < mem->mmap_dev_minaddr)
460 dpavlin 12 mem->mmap_dev_minaddr = baseaddr & ~mem->dev_dyntrans_alignment;
461 dpavlin 2 if (baseaddr + len > mem->mmap_dev_maxaddr)
462 dpavlin 12 mem->mmap_dev_maxaddr = (((baseaddr + len) - 1) |
463     mem->dev_dyntrans_alignment) + 1;
464 dpavlin 2 }
465    
466    
467     /*
468     * memory_device_remove():
469     *
470     * Unregister a (memory mapped) device from a memory struct.
471     */
472     void memory_device_remove(struct memory *mem, int i)
473     {
474     if (i < 0 || i >= mem->n_mmapped_devices) {
475     fatal("memory_device_remove(): invalid device number %i\n", i);
476     return;
477     }
478    
479     mem->n_mmapped_devices --;
480    
481     if (i == mem->n_mmapped_devices)
482     return;
483    
484     /*
485     * YUCK! This is ugly. TODO: fix
486     */
487    
488     memmove(&mem->dev_name[i], &mem->dev_name[i+1], sizeof(char *) *
489     (MAX_DEVICES - i - 1));
490     memmove(&mem->dev_baseaddr[i], &mem->dev_baseaddr[i+1],
491     sizeof(uint64_t) * (MAX_DEVICES - i - 1));
492 dpavlin 22 memmove(&mem->dev_endaddr[i], &mem->dev_endaddr[i+1],
493     sizeof(uint64_t) * (MAX_DEVICES - i - 1));
494 dpavlin 2 memmove(&mem->dev_length[i], &mem->dev_length[i+1], sizeof(uint64_t) *
495     (MAX_DEVICES - i - 1));
496     memmove(&mem->dev_flags[i], &mem->dev_flags[i+1], sizeof(int) *
497     (MAX_DEVICES - i - 1));
498     memmove(&mem->dev_extra[i], &mem->dev_extra[i+1], sizeof(void *) *
499     (MAX_DEVICES - i - 1));
500     memmove(&mem->dev_f[i], &mem->dev_f[i+1], sizeof(void *) *
501     (MAX_DEVICES - i - 1));
502 dpavlin 12 memmove(&mem->dev_dyntrans_data[i], &mem->dev_dyntrans_data[i+1],
503 dpavlin 2 sizeof(void *) * (MAX_DEVICES - i - 1));
504 dpavlin 12 memmove(&mem->dev_dyntrans_write_low[i], &mem->dev_dyntrans_write_low
505 dpavlin 22 [i+1], sizeof(uint64_t) * (MAX_DEVICES - i - 1));
506 dpavlin 12 memmove(&mem->dev_dyntrans_write_high[i], &mem->dev_dyntrans_write_high
507 dpavlin 22 [i+1], sizeof(uint64_t) * (MAX_DEVICES - i - 1));
508 dpavlin 2 }
509    
510    
511     #define MEMORY_RW userland_memory_rw
512     #define MEM_USERLAND
513     #include "memory_rw.c"
514     #undef MEM_USERLAND
515     #undef MEMORY_RW
516    
517    
518     /*
519     * memory_paddr_to_hostaddr():
520     *
521     * Translate a physical address into a host address.
522     *
523     * Return value is a pointer to a host memblock, or NULL on failure.
524     * On reads, a NULL return value should be interpreted as reading all zeroes.
525     */
526     unsigned char *memory_paddr_to_hostaddr(struct memory *mem,
527     uint64_t paddr, int writeflag)
528     {
529     void **table;
530     int entry;
531     const int mask = (1 << BITS_PER_PAGETABLE) - 1;
532     const int shrcount = MAX_BITS - BITS_PER_PAGETABLE;
533    
534     table = mem->pagetable;
535     entry = (paddr >> shrcount) & mask;
536    
537 dpavlin 24 /* printf("memory_paddr_to_hostaddr(): p=%16"PRIx64
538     " w=%i => entry=0x%x\n", (uint64_t) paddr, writeflag, entry); */
539 dpavlin 2
540     if (table[entry] == NULL) {
541     size_t alloclen;
542    
543     /*
544     * Special case: reading from a nonexistant memblock
545     * returns all zeroes, and doesn't allocate anything.
546     * (If any intermediate pagetable is nonexistant, then
547     * the same thing happens):
548     */
549     if (writeflag == MEM_READ)
550     return NULL;
551    
552     /* Allocate a memblock: */
553     alloclen = 1 << BITS_PER_MEMBLOCK;
554    
555     /* printf(" allocating for entry %i, len=%i\n",
556     entry, alloclen); */
557    
558     /* Anonymous mmap() should return zero-filled memory,
559     try malloc + memset if mmap failed. */
560     table[entry] = (void *) mmap(NULL, alloclen,
561 dpavlin 22 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
562 dpavlin 2 if (table[entry] == NULL) {
563     table[entry] = malloc(alloclen);
564     if (table[entry] == NULL) {
565     fatal("out of memory\n");
566     exit(1);
567     }
568     memset(table[entry], 0, alloclen);
569     }
570     }
571    
572     return (unsigned char *) table[entry];
573     }
574    
575 dpavlin 24
576     #define UPDATE_CHECKSUM(value) { \
577     internal_state -= 0x118c7771c0c0a77fULL; \
578     internal_state = ((internal_state + (value)) << 7) ^ \
579     (checksum >> 11) ^ ((checksum - (value)) << 3) ^ \
580     (internal_state - checksum) ^ ((value) - internal_state); \
581     checksum ^= internal_state; \
582     }
583    
584    
585     /*
586     * memory_checksum():
587     *
588     * Calculate a 64-bit checksum of everything in a struct memory. This is
589     * useful for tracking down bugs; an old (presumably working) version of
590     * the emulator can be compared to a newer (buggy) version.
591     */
592     uint64_t memory_checksum(struct memory *mem)
593     {
594     uint64_t internal_state = 0x80624185376feff2ULL;
595     uint64_t checksum = 0xcb9a87d5c010072cULL;
596     const int n_entries = (1 << BITS_PER_PAGETABLE) - 1;
597     const size_t len = (1 << BITS_PER_MEMBLOCK) / sizeof(uint64_t);
598     size_t entry, i;
599    
600     for (entry=0; entry<=n_entries; entry++) {
601     uint64_t **table = mem->pagetable;
602     uint64_t *memblock = table[entry];
603    
604     if (memblock == NULL) {
605     UPDATE_CHECKSUM(0x1198ab7c8174a76fULL);
606     continue;
607     }
608    
609     for (i=0; i<len; i++)
610     UPDATE_CHECKSUM(memblock[i]);
611     }
612    
613     return checksum;
614     }
615    

  ViewVC Help
Powered by ViewVC 1.1.26