/[gxemul]/trunk/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

Contents of /trunk/src/memory.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 22 - (show annotations)
Mon Oct 8 16:19:37 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 16495 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1121 2006/02/18 21:03:08 debug Exp $
20051126	Cobalt and PReP now work with the 21143 NIC.
		Continuing on Alpha dyntrans things.
		Fixing some more left-shift-by-24 to unsigned.
20051127	Working on OpenFirmware emulation; major cleanup/redesign.
		Progress on MacPPC emulation: NetBSD detects two CPUs (when
		running with -n 2), framebuffer output (for text) works.
		Adding quick-hack Bandit PCI controller and "gc" interrupt
		controller for MacPPC.
20051128	Changing from a Bandit to a Uni-North controller for macppc.
		Continuing on OpenFirmware and MacPPC emulation in general
		(obio controller, and wdc attached to the obio seems to work).
20051129	More work on MacPPC emulation (adding a dummy ADB controller).
		Continuing the PCI bus cleanup (endianness and tag composition)
		and rewriting all PCI controllers' access functions.
20051130	Various minor PPC dyntrans optimizations.
		Manually inlining some parts of the framebuffer redraw routine.
		Slowly beginning the conversion of the old MIPS emulation into
		dyntrans (but this will take quite some time to get right).
		Generalizing quick_pc_to_pointers.
20051201	Documentation update (David Muse has made available a kernel
		which simplifies Debian/DECstation installation).
		Continuing on the ADB bus controller.
20051202	Beginning a rewrite of the Zilog serial controller (dev_zs).
20051203	Continuing on the zs rewrite (now called dev_z8530); conversion
		to devinit style.
		Reworking some of the input-only vs output-only vs input-output
		details of src/console.c, better warning messages, and adding
		a debug dump.
		Removing the concept of "device state"; it wasn't really used.
		Changing some debug output (-vv should now be used to show all
		details about devices and busses; not shown during normal
		startup anymore).
		Beginning on some SPARC instruction disassembly support.
20051204	Minor PPC updates (WALNUT skeleton stuff).
		Continuing on the MIPS dyntrans rewrite.
		More progress on the ADB controller (a keyboard is "detected"
		by NetBSD and OpenBSD).
		Downgrading OpenBSD/arc as a guest OS from "working" to
		"almost working" in the documentation.
		Progress on Algor emulation ("v3" PCI controller).
20051205	Minor updates.
20051207	Sorting devices according to address; this reduces complexity
		of device lookups from O(n) to O(log n) in memory_rw (but no
		real performance increase (yet) in experiments).
20051210	Beginning the work on native dyntrans backends (by making a
		simple skeleton; so far only for Alpha hosts).
20051211	Some very minor SPARC updates.
20051215	Fixing a bug in the MIPS mul (note: not mult) instruction,
		so it also works with non-64-bit emulation. (Thanks to Alec
		Voropay for noticing the problem.)
20051216	More work on the fake/empty/simple/skeleton/whatever backend;
		performance doesn't increase, so this isn't really worth it,
		but it was probably worth it to prepare for a real backend
		later.
20051219	More instr call statistics gathering and analysis stuff.
20051220	Another fix for MIPS 'mul'. Also converting mul and {d,}cl{o,z}
		to dyntrans.
		memory_ppc.c syntax error fix (noticed by Peter Valchev).
		Beginning to move out machines from src/machine.c into
		individual files in src/machines (in a way similar to the
		autodev system for devices).
20051222	Updating the documentation regarding NetBSD/pmax 3.0.
20051223	- " - NetBSD/cats 3.0.
20051225	- " - NetBSD/hpcmips 3.0.
20051226	Continuing on the machine registry redesign.
		Adding support for ARM rrx (33-bit rotate).
		Fixing some signed/unsigned issues (exposed by gcc -W).
20051227	Fixing the bug which prevented a NetBSD/prep 3.0 install kernel
		from starting (triggered when an mtmsr was the last instruction
		on a page). Unfortunately not enough to get the kernel to run
		as well as the 2.1 kernels did.
20051230	Some dyntrans refactoring.
20051231	Continuing on the machine registry redesign.
20060101-10	Continuing... moving more machines. Moving MD interrupt stuff
		from machine.c into a new src/machines/interrupts.c.
20060114	Adding various mvmeppc machine skeletons.
20060115	Continuing on mvme* stuff. NetBSD/mvmeppc prints boot messages
		(for MVME1600) and reaches the root device prompt, but no
		specific hardware devices are emulated yet.
20060116	Minor updates to the mvme1600 emulation mode; the Eagle PCI bus
		seems to work without much modification, and a 21143 can be
		detected, interrupts might work (but untested so far).
		Adding a fake MK48Txx (mkclock) device, for NetBSD/mvmeppc.
20060121	Adding an aux control register for ARM. (A BIG thank you to
		Olivier Houchard for tracking down this bug.)
20060122	Adding more ARM instructions (smulXY), and dev_iq80321_7seg.
20060124	Adding disassembly of more ARM instructions (mia*, mra/mar),
		and some semi-bogus XScale and i80321 registers.
20060201-02	Various minor updates. Moving the last machines out of
		machine.c.
20060204	Adding a -c command line option, for running debugger commands
		before the simulation starts, but after all files have been
		loaded.
		Minor iq80321-related updates.
20060209	Minor hacks (DEVINIT macro, etc).
		Preparing for the generalization of the 64-bit dyntrans address
		translation subsystem.
20060216	Adding ARM ldrd (double-register load).
20060217	Continuing on various ARM-related stuff.
20060218	More progress on the ATA/wdc emulation for NetBSD/iq80321.
		NetBSD/evbarm can now be installed :-)  Updating the docs, etc.
		Continuing on Algor emulation.

==============  RELEASE 0.3.8  ==============


1 /*
2 * Copyright (C) 2003-2006 Anders Gavare. All rights reserved.
3 *
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 * $Id: memory.c,v 1.187 2006/01/14 12:51:59 debug Exp $
29 *
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 extern int verbose;
46
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 int i, byte_order = cpu->byte_order;
59 uint64_t x = 0;
60
61 if (len & MEM_PCI_LITTLE_ENDIAN) {
62 len &= ~MEM_PCI_LITTLE_ENDIAN;
63 byte_order = EMUL_LITTLE_ENDIAN;
64 }
65
66 /* Switch byte order for incoming data, if necessary: */
67 if (byte_order == EMUL_BIG_ENDIAN)
68 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 int i, byte_order = cpu->byte_order;
94
95 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 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 * malloc() + memset(). The returned memory block contains only zeroes.
118 */
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 struct memory *memory_new(uint64_t physical_max, int arch)
142 {
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 mem->dev_dyntrans_alignment = 4095;
167 if (arch == ARCH_ALPHA)
168 mem->dev_dyntrans_alignment = 8191;
169
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 * Returns 1 if there's something string-like in emulated memory at address
194 * addr, otherwise 0.
195 */
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 * Convert emulated memory contents to a string, placing it in a buffer
224 * provided by the caller.
225 */
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 * memory_device_dyntrans_access():
267 *
268 * Get the lowest and highest dyntrans access since last time.
269 */
270 void memory_device_dyntrans_access(struct cpu *cpu, struct memory *mem,
271 void *extra, uint64_t *low, uint64_t *high)
272 {
273 int i, j;
274 size_t s;
275 int need_inval = 0;
276
277 /* TODO: This is O(n), so it might be good to rewrite it some day.
278 For now, it will be enough, as long as this function is not
279 called too often. */
280
281 for (i=0; i<mem->n_mmapped_devices; i++) {
282 if (mem->dev_extra[i] == extra &&
283 mem->dev_flags[i] & DM_DYNTRANS_WRITE_OK &&
284 mem->dev_dyntrans_data[i] != NULL) {
285 if (mem->dev_dyntrans_write_low[i] != (uint64_t) -1)
286 need_inval = 1;
287 if (low != NULL)
288 *low = mem->dev_dyntrans_write_low[i];
289 mem->dev_dyntrans_write_low[i] = (uint64_t) -1;
290
291 if (high != NULL)
292 *high = mem->dev_dyntrans_write_high[i];
293 mem->dev_dyntrans_write_high[i] = 0;
294
295 if (!need_inval)
296 return;
297
298 /* Invalidate any pages of this device that might
299 be in the dyntrans load/store cache, by marking
300 the pages read-only. */
301 if (cpu->invalidate_translation_caches != NULL) {
302 for (s=0; s<mem->dev_length[i];
303 s+=cpu->machine->arch_pagesize)
304 cpu->invalidate_translation_caches
305 (cpu, mem->dev_baseaddr[i] + s,
306 JUST_MARK_AS_NON_WRITABLE
307 | INVALIDATE_PADDR);
308 }
309
310 if (cpu->machine->arch == ARCH_MIPS) {
311 /*
312 * ... and invalidate the "fast_vaddr_to_
313 * hostaddr" cache entries that contain
314 * pointers to this device: (NOTE: Device i,
315 * cache entry j)
316 */
317 for (j=0; j<N_BINTRANS_VADDR_TO_HOST; j++) {
318 if (cpu->cd.
319 mips.bintrans_data_hostpage[j] >=
320 mem->dev_dyntrans_data[i] &&
321 cpu->cd.mips.
322 bintrans_data_hostpage[j] <
323 mem->dev_dyntrans_data[i] +
324 mem->dev_length[i])
325 cpu->cd.mips.
326 bintrans_data_hostpage[j]
327 = NULL;
328 }
329 }
330 return;
331 }
332 }
333 }
334
335
336 /*
337 * memory_device_register():
338 *
339 * Register a (memory mapped) device by adding it to the dev_* fields of a
340 * memory struct.
341 */
342 void memory_device_register(struct memory *mem, const char *device_name,
343 uint64_t baseaddr, uint64_t len,
344 int (*f)(struct cpu *,struct memory *,uint64_t,unsigned char *,
345 size_t,int,void *),
346 void *extra, int flags, unsigned char *dyntrans_data)
347 {
348 int i, newi = 0;
349
350 if (mem->n_mmapped_devices >= MAX_DEVICES) {
351 fprintf(stderr, "memory_device_register(): too many "
352 "devices registered, cannot register '%s'\n", device_name);
353 exit(1);
354 }
355
356 /*
357 * Figure out at which index to insert this device, and simultaneously
358 * check for collisions:
359 */
360 newi = -1;
361 for (i=0; i<mem->n_mmapped_devices; i++) {
362 if (i == 0 && baseaddr + len <= mem->dev_baseaddr[i])
363 newi = i;
364 if (i > 0 && baseaddr + len <= mem->dev_baseaddr[i] &&
365 baseaddr >= mem->dev_endaddr[i-1])
366 newi = i;
367 if (i == mem->n_mmapped_devices - 1 &&
368 baseaddr >= mem->dev_endaddr[i])
369 newi = i + 1;
370
371 /* If we are not colliding with device i, then continue: */
372 if (baseaddr + len <= mem->dev_baseaddr[i])
373 continue;
374 if (baseaddr >= mem->dev_endaddr[i])
375 continue;
376
377 fatal("\nERROR! \"%s\" collides with device %i (\"%s\")!\n",
378 device_name, i, mem->dev_name[i]);
379 exit(1);
380 }
381 if (mem->n_mmapped_devices == 0)
382 newi = 0;
383 if (newi == -1) {
384 fatal("INTERNAL ERROR\n");
385 exit(1);
386 }
387
388 if (verbose >= 2) {
389 /* (40 bits of physical address is displayed) */
390 debug("device at 0x%010llx: %s", (long long)baseaddr,
391 device_name);
392
393 if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)
394 && (baseaddr & mem->dev_dyntrans_alignment) != 0) {
395 fatal("\nWARNING: Device dyntrans access, but unaligned"
396 " baseaddr 0x%llx.\n", (long long)baseaddr);
397 }
398
399 if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) {
400 debug(" (dyntrans %s)",
401 (flags & DM_DYNTRANS_WRITE_OK)? "R/W" : "R");
402 }
403 debug("\n");
404 }
405
406 for (i=0; i<mem->n_mmapped_devices; i++) {
407 if (dyntrans_data == mem->dev_dyntrans_data[i] &&
408 mem->dev_flags[i] & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)
409 && flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)) {
410 fatal("ERROR: the data pointer used for dyntrans "
411 "accesses must only be used once!\n");
412 fatal("(%p cannot be used by '%s'; already in use by '"
413 "%s')\n", dyntrans_data, device_name,
414 mem->dev_name[i]);
415 exit(1);
416 }
417 }
418
419 mem->n_mmapped_devices++;
420
421 /*
422 * YUCK! This is ugly. TODO: fix
423 */
424 /* Make space for the new entry: */
425 memmove(&mem->dev_name[newi+1], &mem->dev_name[newi], sizeof(char *) *
426 (MAX_DEVICES - newi - 1));
427 memmove(&mem->dev_baseaddr[newi+1], &mem->dev_baseaddr[newi],
428 sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
429 memmove(&mem->dev_endaddr[newi+1], &mem->dev_endaddr[newi],
430 sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
431 memmove(&mem->dev_length[newi+1], &mem->dev_length[newi],
432 sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
433 memmove(&mem->dev_flags[newi+1], &mem->dev_flags[newi], sizeof(int) *
434 (MAX_DEVICES - newi - 1));
435 memmove(&mem->dev_extra[newi+1], &mem->dev_extra[newi], sizeof(void *) *
436 (MAX_DEVICES - newi - 1));
437 memmove(&mem->dev_f[newi+1], &mem->dev_f[newi], sizeof(void *) *
438 (MAX_DEVICES - newi - 1));
439 memmove(&mem->dev_dyntrans_data[newi+1], &mem->dev_dyntrans_data[newi],
440 sizeof(void *) * (MAX_DEVICES - newi - 1));
441 memmove(&mem->dev_dyntrans_write_low[newi+1],
442 &mem->dev_dyntrans_write_low[newi],
443 sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
444 memmove(&mem->dev_dyntrans_write_high[newi+1],
445 &mem->dev_dyntrans_write_high[newi],
446 sizeof(uint64_t) * (MAX_DEVICES - newi - 1));
447
448
449 mem->dev_name[newi] = strdup(device_name);
450 mem->dev_baseaddr[newi] = baseaddr;
451 mem->dev_endaddr[newi] = baseaddr + len;
452 mem->dev_length[newi] = len;
453 mem->dev_flags[newi] = flags;
454 mem->dev_dyntrans_data[newi] = dyntrans_data;
455
456 if (mem->dev_name[newi] == NULL) {
457 fprintf(stderr, "out of memory\n");
458 exit(1);
459 }
460
461 if (flags & (DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK)
462 && !(flags & DM_EMULATED_RAM) && dyntrans_data == NULL) {
463 fatal("\nERROR: Device dyntrans access, but dyntrans_data"
464 " = NULL!\n");
465 exit(1);
466 }
467
468 if ((size_t)dyntrans_data & (sizeof(void *) - 1)) {
469 fprintf(stderr, "memory_device_register():"
470 " dyntrans_data not aligned correctly (%p)\n",
471 dyntrans_data);
472 exit(1);
473 }
474
475 mem->dev_dyntrans_write_low[newi] = (uint64_t)-1;
476 mem->dev_dyntrans_write_high[newi] = 0;
477 mem->dev_f[newi] = f;
478 mem->dev_extra[newi] = extra;
479
480 if (baseaddr < mem->mmap_dev_minaddr)
481 mem->mmap_dev_minaddr = baseaddr & ~mem->dev_dyntrans_alignment;
482 if (baseaddr + len > mem->mmap_dev_maxaddr)
483 mem->mmap_dev_maxaddr = (((baseaddr + len) - 1) |
484 mem->dev_dyntrans_alignment) + 1;
485 }
486
487
488 /*
489 * memory_device_remove():
490 *
491 * Unregister a (memory mapped) device from a memory struct.
492 */
493 void memory_device_remove(struct memory *mem, int i)
494 {
495 if (i < 0 || i >= mem->n_mmapped_devices) {
496 fatal("memory_device_remove(): invalid device number %i\n", i);
497 return;
498 }
499
500 mem->n_mmapped_devices --;
501
502 if (i == mem->n_mmapped_devices)
503 return;
504
505 /*
506 * YUCK! This is ugly. TODO: fix
507 */
508
509 memmove(&mem->dev_name[i], &mem->dev_name[i+1], sizeof(char *) *
510 (MAX_DEVICES - i - 1));
511 memmove(&mem->dev_baseaddr[i], &mem->dev_baseaddr[i+1],
512 sizeof(uint64_t) * (MAX_DEVICES - i - 1));
513 memmove(&mem->dev_endaddr[i], &mem->dev_endaddr[i+1],
514 sizeof(uint64_t) * (MAX_DEVICES - i - 1));
515 memmove(&mem->dev_length[i], &mem->dev_length[i+1], sizeof(uint64_t) *
516 (MAX_DEVICES - i - 1));
517 memmove(&mem->dev_flags[i], &mem->dev_flags[i+1], sizeof(int) *
518 (MAX_DEVICES - i - 1));
519 memmove(&mem->dev_extra[i], &mem->dev_extra[i+1], sizeof(void *) *
520 (MAX_DEVICES - i - 1));
521 memmove(&mem->dev_f[i], &mem->dev_f[i+1], sizeof(void *) *
522 (MAX_DEVICES - i - 1));
523 memmove(&mem->dev_dyntrans_data[i], &mem->dev_dyntrans_data[i+1],
524 sizeof(void *) * (MAX_DEVICES - i - 1));
525 memmove(&mem->dev_dyntrans_write_low[i], &mem->dev_dyntrans_write_low
526 [i+1], sizeof(uint64_t) * (MAX_DEVICES - i - 1));
527 memmove(&mem->dev_dyntrans_write_high[i], &mem->dev_dyntrans_write_high
528 [i+1], sizeof(uint64_t) * (MAX_DEVICES - i - 1));
529 }
530
531
532 #define MEMORY_RW userland_memory_rw
533 #define MEM_USERLAND
534 #include "memory_rw.c"
535 #undef MEM_USERLAND
536 #undef MEMORY_RW
537
538
539 /*
540 * memory_paddr_to_hostaddr():
541 *
542 * Translate a physical address into a host address.
543 *
544 * Return value is a pointer to a host memblock, or NULL on failure.
545 * On reads, a NULL return value should be interpreted as reading all zeroes.
546 */
547 unsigned char *memory_paddr_to_hostaddr(struct memory *mem,
548 uint64_t paddr, int writeflag)
549 {
550 void **table;
551 int entry;
552 const int mask = (1 << BITS_PER_PAGETABLE) - 1;
553 const int shrcount = MAX_BITS - BITS_PER_PAGETABLE;
554
555 table = mem->pagetable;
556 entry = (paddr >> shrcount) & mask;
557
558 /* printf("memory_paddr_to_hostaddr(): p=%16llx w=%i => entry=0x%x\n",
559 (long long)paddr, writeflag, entry); */
560
561 if (table[entry] == NULL) {
562 size_t alloclen;
563
564 /*
565 * Special case: reading from a nonexistant memblock
566 * returns all zeroes, and doesn't allocate anything.
567 * (If any intermediate pagetable is nonexistant, then
568 * the same thing happens):
569 */
570 if (writeflag == MEM_READ)
571 return NULL;
572
573 /* Allocate a memblock: */
574 alloclen = 1 << BITS_PER_MEMBLOCK;
575
576 /* printf(" allocating for entry %i, len=%i\n",
577 entry, alloclen); */
578
579 /* Anonymous mmap() should return zero-filled memory,
580 try malloc + memset if mmap failed. */
581 table[entry] = (void *) mmap(NULL, alloclen,
582 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
583 if (table[entry] == NULL) {
584 table[entry] = malloc(alloclen);
585 if (table[entry] == NULL) {
586 fatal("out of memory\n");
587 exit(1);
588 }
589 memset(table[entry], 0, alloclen);
590 }
591 }
592
593 return (unsigned char *) table[entry];
594 }
595

  ViewVC Help
Powered by ViewVC 1.1.26