armvirt.c revision 1.1.1.1 1 1.1 christos /* armvirt.c -- ARMulator virtual memory interace: ARM6 Instruction Emulator.
2 1.1 christos Copyright (C) 1994 Advanced RISC Machines Ltd.
3 1.1 christos
4 1.1 christos This program is free software; you can redistribute it and/or modify
5 1.1 christos it under the terms of the GNU General Public License as published by
6 1.1 christos the Free Software Foundation; either version 3 of the License, or
7 1.1 christos (at your option) any later version.
8 1.1 christos
9 1.1 christos This program is distributed in the hope that it will be useful,
10 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
11 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 1.1 christos GNU General Public License for more details.
13 1.1 christos
14 1.1 christos You should have received a copy of the GNU General Public License
15 1.1 christos along with this program; if not, see <http://www.gnu.org/licenses/>. */
16 1.1 christos
17 1.1 christos /* This file contains a complete ARMulator memory model, modelling a
18 1.1 christos "virtual memory" system. A much simpler model can be found in armfast.c,
19 1.1 christos and that model goes faster too, but has a fixed amount of memory. This
20 1.1 christos model's memory has 64K pages, allocated on demand from a 64K entry page
21 1.1 christos table. The routines PutWord and GetWord implement this. Pages are never
22 1.1 christos freed as they might be needed again. A single area of memory may be
23 1.1 christos defined to generate aborts. */
24 1.1 christos
25 1.1 christos #include "armopts.h"
26 1.1 christos #include "armos.h"
27 1.1 christos #include "armdefs.h"
28 1.1 christos #include "ansidecl.h"
29 1.1 christos
30 1.1 christos #ifdef VALIDATE /* for running the validate suite */
31 1.1 christos #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
32 1.1 christos #define ABORTS 1
33 1.1 christos #endif
34 1.1 christos
35 1.1 christos /* #define ABORTS */
36 1.1 christos
37 1.1 christos #ifdef ABORTS /* the memory system will abort */
38 1.1 christos /* For the old test suite Abort between 32 Kbytes and 32 Mbytes
39 1.1 christos For the new test suite Abort between 8 Mbytes and 26 Mbytes */
40 1.1 christos /* #define LOWABORT 32 * 1024
41 1.1 christos #define HIGHABORT 32 * 1024 * 1024 */
42 1.1 christos #define LOWABORT 8 * 1024 * 1024
43 1.1 christos #define HIGHABORT 26 * 1024 * 1024
44 1.1 christos
45 1.1 christos #endif
46 1.1 christos
47 1.1 christos #define NUMPAGES 64 * 1024
48 1.1 christos #define PAGESIZE 64 * 1024
49 1.1 christos #define PAGEBITS 16
50 1.1 christos #define OFFSETBITS 0xffff
51 1.1 christos
52 1.1 christos int SWI_vector_installed = FALSE;
53 1.1 christos
54 1.1 christos /***************************************************************************\
55 1.1 christos * Get a Word from Virtual Memory, maybe allocating the page *
56 1.1 christos \***************************************************************************/
57 1.1 christos
58 1.1 christos static ARMword
59 1.1 christos GetWord (ARMul_State * state, ARMword address, int check)
60 1.1 christos {
61 1.1 christos ARMword page;
62 1.1 christos ARMword offset;
63 1.1 christos ARMword **pagetable;
64 1.1 christos ARMword *pageptr;
65 1.1 christos
66 1.1 christos if (check && state->is_XScale)
67 1.1 christos XScale_check_memacc (state, &address, 0);
68 1.1 christos
69 1.1 christos page = address >> PAGEBITS;
70 1.1 christos offset = (address & OFFSETBITS) >> 2;
71 1.1 christos pagetable = (ARMword **) state->MemDataPtr;
72 1.1 christos pageptr = *(pagetable + page);
73 1.1 christos
74 1.1 christos if (pageptr == NULL)
75 1.1 christos {
76 1.1 christos pageptr = (ARMword *) malloc (PAGESIZE);
77 1.1 christos
78 1.1 christos if (pageptr == NULL)
79 1.1 christos {
80 1.1 christos perror ("ARMulator can't allocate VM page");
81 1.1 christos exit (12);
82 1.1 christos }
83 1.1 christos
84 1.1 christos *(pagetable + page) = pageptr;
85 1.1 christos }
86 1.1 christos
87 1.1 christos return *(pageptr + offset);
88 1.1 christos }
89 1.1 christos
90 1.1 christos /***************************************************************************\
91 1.1 christos * Put a Word into Virtual Memory, maybe allocating the page *
92 1.1 christos \***************************************************************************/
93 1.1 christos
94 1.1 christos static void
95 1.1 christos PutWord (ARMul_State * state, ARMword address, ARMword data, int check)
96 1.1 christos {
97 1.1 christos ARMword page;
98 1.1 christos ARMword offset;
99 1.1 christos ARMword **pagetable;
100 1.1 christos ARMword *pageptr;
101 1.1 christos
102 1.1 christos if (check && state->is_XScale)
103 1.1 christos XScale_check_memacc (state, &address, 1);
104 1.1 christos
105 1.1 christos page = address >> PAGEBITS;
106 1.1 christos offset = (address & OFFSETBITS) >> 2;
107 1.1 christos pagetable = (ARMword **) state->MemDataPtr;
108 1.1 christos pageptr = *(pagetable + page);
109 1.1 christos
110 1.1 christos if (pageptr == NULL)
111 1.1 christos {
112 1.1 christos pageptr = (ARMword *) malloc (PAGESIZE);
113 1.1 christos if (pageptr == NULL)
114 1.1 christos {
115 1.1 christos perror ("ARMulator can't allocate VM page");
116 1.1 christos exit (13);
117 1.1 christos }
118 1.1 christos
119 1.1 christos *(pagetable + page) = pageptr;
120 1.1 christos }
121 1.1 christos
122 1.1 christos if (address == 0x8)
123 1.1 christos SWI_vector_installed = TRUE;
124 1.1 christos
125 1.1 christos *(pageptr + offset) = data;
126 1.1 christos }
127 1.1 christos
128 1.1 christos /***************************************************************************\
129 1.1 christos * Initialise the memory interface *
130 1.1 christos \***************************************************************************/
131 1.1 christos
132 1.1 christos unsigned
133 1.1 christos ARMul_MemoryInit (ARMul_State * state, unsigned long initmemsize)
134 1.1 christos {
135 1.1 christos ARMword **pagetable;
136 1.1 christos unsigned page;
137 1.1 christos
138 1.1 christos if (initmemsize)
139 1.1 christos state->MemSize = initmemsize;
140 1.1 christos
141 1.1 christos pagetable = (ARMword **) malloc (sizeof (ARMword *) * NUMPAGES);
142 1.1 christos
143 1.1 christos if (pagetable == NULL)
144 1.1 christos return FALSE;
145 1.1 christos
146 1.1 christos for (page = 0; page < NUMPAGES; page++)
147 1.1 christos *(pagetable + page) = NULL;
148 1.1 christos
149 1.1 christos state->MemDataPtr = (unsigned char *) pagetable;
150 1.1 christos
151 1.1 christos ARMul_ConsolePrint (state, ", 4 Gb memory");
152 1.1 christos
153 1.1 christos return TRUE;
154 1.1 christos }
155 1.1 christos
156 1.1 christos /***************************************************************************\
157 1.1 christos * Remove the memory interface *
158 1.1 christos \***************************************************************************/
159 1.1 christos
160 1.1 christos void
161 1.1 christos ARMul_MemoryExit (ARMul_State * state)
162 1.1 christos {
163 1.1 christos ARMword page;
164 1.1 christos ARMword **pagetable;
165 1.1 christos ARMword *pageptr;
166 1.1 christos
167 1.1 christos pagetable = (ARMword **) state->MemDataPtr;
168 1.1 christos for (page = 0; page < NUMPAGES; page++)
169 1.1 christos {
170 1.1 christos pageptr = *(pagetable + page);
171 1.1 christos if (pageptr != NULL)
172 1.1 christos free ((char *) pageptr);
173 1.1 christos }
174 1.1 christos free ((char *) pagetable);
175 1.1 christos return;
176 1.1 christos }
177 1.1 christos
178 1.1 christos /***************************************************************************\
179 1.1 christos * ReLoad Instruction *
180 1.1 christos \***************************************************************************/
181 1.1 christos
182 1.1 christos ARMword
183 1.1 christos ARMul_ReLoadInstr (ARMul_State * state, ARMword address, ARMword isize)
184 1.1 christos {
185 1.1 christos #ifdef ABORTS
186 1.1 christos if (address >= LOWABORT && address < HIGHABORT)
187 1.1 christos {
188 1.1 christos ARMul_PREFETCHABORT (address);
189 1.1 christos return ARMul_ABORTWORD;
190 1.1 christos }
191 1.1 christos else
192 1.1 christos {
193 1.1 christos ARMul_CLEARABORT;
194 1.1 christos }
195 1.1 christos #endif
196 1.1 christos
197 1.1 christos if ((isize == 2) && (address & 0x2))
198 1.1 christos {
199 1.1 christos /* We return the next two halfwords: */
200 1.1 christos ARMword lo = GetWord (state, address, FALSE);
201 1.1 christos ARMword hi = GetWord (state, address + 4, FALSE);
202 1.1 christos
203 1.1 christos if (state->bigendSig == HIGH)
204 1.1 christos return (lo << 16) | (hi >> 16);
205 1.1 christos else
206 1.1 christos return ((hi & 0xFFFF) << 16) | (lo >> 16);
207 1.1 christos }
208 1.1 christos
209 1.1 christos return GetWord (state, address, TRUE);
210 1.1 christos }
211 1.1 christos
212 1.1 christos /***************************************************************************\
213 1.1 christos * Load Instruction, Sequential Cycle *
214 1.1 christos \***************************************************************************/
215 1.1 christos
216 1.1 christos ARMword ARMul_LoadInstrS (ARMul_State * state, ARMword address, ARMword isize)
217 1.1 christos {
218 1.1 christos state->NumScycles++;
219 1.1 christos
220 1.1 christos #ifdef HOURGLASS
221 1.1 christos if ((state->NumScycles & HOURGLASS_RATE) == 0)
222 1.1 christos {
223 1.1 christos HOURGLASS;
224 1.1 christos }
225 1.1 christos #endif
226 1.1 christos
227 1.1 christos return ARMul_ReLoadInstr (state, address, isize);
228 1.1 christos }
229 1.1 christos
230 1.1 christos /***************************************************************************\
231 1.1 christos * Load Instruction, Non Sequential Cycle *
232 1.1 christos \***************************************************************************/
233 1.1 christos
234 1.1 christos ARMword ARMul_LoadInstrN (ARMul_State * state, ARMword address, ARMword isize)
235 1.1 christos {
236 1.1 christos state->NumNcycles++;
237 1.1 christos
238 1.1 christos return ARMul_ReLoadInstr (state, address, isize);
239 1.1 christos }
240 1.1 christos
241 1.1 christos /***************************************************************************\
242 1.1 christos * Read Word (but don't tell anyone!) *
243 1.1 christos \***************************************************************************/
244 1.1 christos
245 1.1 christos ARMword ARMul_ReadWord (ARMul_State * state, ARMword address)
246 1.1 christos {
247 1.1 christos #ifdef ABORTS
248 1.1 christos if (address >= LOWABORT && address < HIGHABORT)
249 1.1 christos {
250 1.1 christos ARMul_DATAABORT (address);
251 1.1 christos return ARMul_ABORTWORD;
252 1.1 christos }
253 1.1 christos else
254 1.1 christos {
255 1.1 christos ARMul_CLEARABORT;
256 1.1 christos }
257 1.1 christos #endif
258 1.1 christos
259 1.1 christos return GetWord (state, address, TRUE);
260 1.1 christos }
261 1.1 christos
262 1.1 christos /***************************************************************************\
263 1.1 christos * Load Word, Sequential Cycle *
264 1.1 christos \***************************************************************************/
265 1.1 christos
266 1.1 christos ARMword ARMul_LoadWordS (ARMul_State * state, ARMword address)
267 1.1 christos {
268 1.1 christos state->NumScycles++;
269 1.1 christos
270 1.1 christos return ARMul_ReadWord (state, address);
271 1.1 christos }
272 1.1 christos
273 1.1 christos /***************************************************************************\
274 1.1 christos * Load Word, Non Sequential Cycle *
275 1.1 christos \***************************************************************************/
276 1.1 christos
277 1.1 christos ARMword ARMul_LoadWordN (ARMul_State * state, ARMword address)
278 1.1 christos {
279 1.1 christos state->NumNcycles++;
280 1.1 christos
281 1.1 christos return ARMul_ReadWord (state, address);
282 1.1 christos }
283 1.1 christos
284 1.1 christos /***************************************************************************\
285 1.1 christos * Load Halfword, (Non Sequential Cycle) *
286 1.1 christos \***************************************************************************/
287 1.1 christos
288 1.1 christos ARMword ARMul_LoadHalfWord (ARMul_State * state, ARMword address)
289 1.1 christos {
290 1.1 christos ARMword temp, offset;
291 1.1 christos
292 1.1 christos state->NumNcycles++;
293 1.1 christos
294 1.1 christos temp = ARMul_ReadWord (state, address);
295 1.1 christos offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
296 1.1 christos
297 1.1 christos return (temp >> offset) & 0xffff;
298 1.1 christos }
299 1.1 christos
300 1.1 christos /***************************************************************************\
301 1.1 christos * Read Byte (but don't tell anyone!) *
302 1.1 christos \***************************************************************************/
303 1.1 christos
304 1.1 christos ARMword ARMul_ReadByte (ARMul_State * state, ARMword address)
305 1.1 christos {
306 1.1 christos ARMword temp, offset;
307 1.1 christos
308 1.1 christos temp = ARMul_ReadWord (state, address);
309 1.1 christos offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
310 1.1 christos
311 1.1 christos return (temp >> offset & 0xffL);
312 1.1 christos }
313 1.1 christos
314 1.1 christos /***************************************************************************\
315 1.1 christos * Load Byte, (Non Sequential Cycle) *
316 1.1 christos \***************************************************************************/
317 1.1 christos
318 1.1 christos ARMword ARMul_LoadByte (ARMul_State * state, ARMword address)
319 1.1 christos {
320 1.1 christos state->NumNcycles++;
321 1.1 christos
322 1.1 christos return ARMul_ReadByte (state, address);
323 1.1 christos }
324 1.1 christos
325 1.1 christos /***************************************************************************\
326 1.1 christos * Write Word (but don't tell anyone!) *
327 1.1 christos \***************************************************************************/
328 1.1 christos
329 1.1 christos void
330 1.1 christos ARMul_WriteWord (ARMul_State * state, ARMword address, ARMword data)
331 1.1 christos {
332 1.1 christos #ifdef ABORTS
333 1.1 christos if (address >= LOWABORT && address < HIGHABORT)
334 1.1 christos {
335 1.1 christos ARMul_DATAABORT (address);
336 1.1 christos return;
337 1.1 christos }
338 1.1 christos else
339 1.1 christos {
340 1.1 christos ARMul_CLEARABORT;
341 1.1 christos }
342 1.1 christos #endif
343 1.1 christos
344 1.1 christos PutWord (state, address, data, TRUE);
345 1.1 christos }
346 1.1 christos
347 1.1 christos /***************************************************************************\
348 1.1 christos * Store Word, Sequential Cycle *
349 1.1 christos \***************************************************************************/
350 1.1 christos
351 1.1 christos void
352 1.1 christos ARMul_StoreWordS (ARMul_State * state, ARMword address, ARMword data)
353 1.1 christos {
354 1.1 christos state->NumScycles++;
355 1.1 christos
356 1.1 christos ARMul_WriteWord (state, address, data);
357 1.1 christos }
358 1.1 christos
359 1.1 christos /***************************************************************************\
360 1.1 christos * Store Word, Non Sequential Cycle *
361 1.1 christos \***************************************************************************/
362 1.1 christos
363 1.1 christos void
364 1.1 christos ARMul_StoreWordN (ARMul_State * state, ARMword address, ARMword data)
365 1.1 christos {
366 1.1 christos state->NumNcycles++;
367 1.1 christos
368 1.1 christos ARMul_WriteWord (state, address, data);
369 1.1 christos }
370 1.1 christos
371 1.1 christos /***************************************************************************\
372 1.1 christos * Store HalfWord, (Non Sequential Cycle) *
373 1.1 christos \***************************************************************************/
374 1.1 christos
375 1.1 christos void
376 1.1 christos ARMul_StoreHalfWord (ARMul_State * state, ARMword address, ARMword data)
377 1.1 christos {
378 1.1 christos ARMword temp, offset;
379 1.1 christos
380 1.1 christos state->NumNcycles++;
381 1.1 christos
382 1.1 christos #ifdef VALIDATE
383 1.1 christos if (address == TUBE)
384 1.1 christos {
385 1.1 christos if (data == 4)
386 1.1 christos state->Emulate = FALSE;
387 1.1 christos else
388 1.1 christos (void) putc ((char) data, stderr); /* Write Char */
389 1.1 christos return;
390 1.1 christos }
391 1.1 christos #endif
392 1.1 christos
393 1.1 christos temp = ARMul_ReadWord (state, address);
394 1.1 christos offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
395 1.1 christos
396 1.1 christos PutWord (state, address,
397 1.1 christos (temp & ~(0xffffL << offset)) | ((data & 0xffffL) << offset),
398 1.1 christos TRUE);
399 1.1 christos }
400 1.1 christos
401 1.1 christos /***************************************************************************\
402 1.1 christos * Write Byte (but don't tell anyone!) *
403 1.1 christos \***************************************************************************/
404 1.1 christos
405 1.1 christos void
406 1.1 christos ARMul_WriteByte (ARMul_State * state, ARMword address, ARMword data)
407 1.1 christos {
408 1.1 christos ARMword temp, offset;
409 1.1 christos
410 1.1 christos temp = ARMul_ReadWord (state, address);
411 1.1 christos offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
412 1.1 christos
413 1.1 christos PutWord (state, address,
414 1.1 christos (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),
415 1.1 christos TRUE);
416 1.1 christos }
417 1.1 christos
418 1.1 christos /***************************************************************************\
419 1.1 christos * Store Byte, (Non Sequential Cycle) *
420 1.1 christos \***************************************************************************/
421 1.1 christos
422 1.1 christos void
423 1.1 christos ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
424 1.1 christos {
425 1.1 christos state->NumNcycles++;
426 1.1 christos
427 1.1 christos #ifdef VALIDATE
428 1.1 christos if (address == TUBE)
429 1.1 christos {
430 1.1 christos if (data == 4)
431 1.1 christos state->Emulate = FALSE;
432 1.1 christos else
433 1.1 christos (void) putc ((char) data, stderr); /* Write Char */
434 1.1 christos return;
435 1.1 christos }
436 1.1 christos #endif
437 1.1 christos
438 1.1 christos ARMul_WriteByte (state, address, data);
439 1.1 christos }
440 1.1 christos
441 1.1 christos /***************************************************************************\
442 1.1 christos * Swap Word, (Two Non Sequential Cycles) *
443 1.1 christos \***************************************************************************/
444 1.1 christos
445 1.1 christos ARMword ARMul_SwapWord (ARMul_State * state, ARMword address, ARMword data)
446 1.1 christos {
447 1.1 christos ARMword temp;
448 1.1 christos
449 1.1 christos state->NumNcycles++;
450 1.1 christos
451 1.1 christos temp = ARMul_ReadWord (state, address);
452 1.1 christos
453 1.1 christos state->NumNcycles++;
454 1.1 christos
455 1.1 christos PutWord (state, address, data, TRUE);
456 1.1 christos
457 1.1 christos return temp;
458 1.1 christos }
459 1.1 christos
460 1.1 christos /***************************************************************************\
461 1.1 christos * Swap Byte, (Two Non Sequential Cycles) *
462 1.1 christos \***************************************************************************/
463 1.1 christos
464 1.1 christos ARMword ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
465 1.1 christos {
466 1.1 christos ARMword temp;
467 1.1 christos
468 1.1 christos temp = ARMul_LoadByte (state, address);
469 1.1 christos ARMul_StoreByte (state, address, data);
470 1.1 christos
471 1.1 christos return temp;
472 1.1 christos }
473 1.1 christos
474 1.1 christos /***************************************************************************\
475 1.1 christos * Count I Cycles *
476 1.1 christos \***************************************************************************/
477 1.1 christos
478 1.1 christos void
479 1.1 christos ARMul_Icycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
480 1.1 christos {
481 1.1 christos state->NumIcycles += number;
482 1.1 christos ARMul_CLEARABORT;
483 1.1 christos }
484 1.1 christos
485 1.1 christos /***************************************************************************\
486 1.1 christos * Count C Cycles *
487 1.1 christos \***************************************************************************/
488 1.1 christos
489 1.1 christos void
490 1.1 christos ARMul_Ccycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
491 1.1 christos {
492 1.1 christos state->NumCcycles += number;
493 1.1 christos ARMul_CLEARABORT;
494 1.1 christos }
495 1.1 christos
496 1.1 christos
497 1.1 christos /* Read a byte. Do not check for alignment or access errors. */
498 1.1 christos
499 1.1 christos ARMword
500 1.1 christos ARMul_SafeReadByte (ARMul_State * state, ARMword address)
501 1.1 christos {
502 1.1 christos ARMword temp, offset;
503 1.1 christos
504 1.1 christos temp = GetWord (state, address, FALSE);
505 1.1 christos offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
506 1.1 christos
507 1.1 christos return (temp >> offset & 0xffL);
508 1.1 christos }
509 1.1 christos
510 1.1 christos void
511 1.1 christos ARMul_SafeWriteByte (ARMul_State * state, ARMword address, ARMword data)
512 1.1 christos {
513 1.1 christos ARMword temp, offset;
514 1.1 christos
515 1.1 christos temp = GetWord (state, address, FALSE);
516 1.1 christos offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
517 1.1 christos
518 1.1 christos PutWord (state, address,
519 1.1 christos (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),
520 1.1 christos FALSE);
521 1.1 christos }
522