summaryrefslogtreecommitdiffstats
path: root/src/core/arm/interpreter/armvirt.cpp
blob: a072b73be56731294dad8630cef5e5d9361bb004 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*  armvirt.c -- ARMulator virtual memory interace:  ARM6 Instruction Emulator.
    Copyright (C) 1994 Advanced RISC Machines Ltd.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/* This file contains a complete ARMulator memory model, modelling a
"virtual memory" system. A much simpler model can be found in armfast.c,
and that model goes faster too, but has a fixed amount of memory. This
model's memory has 64K pages, allocated on demand from a 64K entry page
table. The routines PutWord and GetWord implement this. Pages are never
freed as they might be needed again. A single area of memory may be
defined to generate aborts. */

#include "armdefs.h"
#include "skyeye_defs.h"
//#include "code_cov.h"

#ifdef VALIDATE			/* for running the validate suite */
#define TUBE 48 * 1024 * 1024	/* write a char on the screen */
#define ABORTS 1
#endif

/* #define ABORTS */

#ifdef ABORTS			/* the memory system will abort */
/* For the old test suite Abort between 32 Kbytes and 32 Mbytes
   For the new test suite Abort between 8 Mbytes and 26 Mbytes */
/* #define LOWABORT 32 * 1024
#define HIGHABORT 32 * 1024 * 1024 */
#define LOWABORT 8 * 1024 * 1024
#define HIGHABORT 26 * 1024 * 1024

#endif

#define NUMPAGES 64 * 1024
#define PAGESIZE 64 * 1024
#define PAGEBITS 16
#define OFFSETBITS 0xffff
//chy 2003-08-19: seems no use ????
int SWI_vector_installed = FALSE;
extern ARMword skyeye_cachetype;

/***************************************************************************\
*        Get a byte into Virtual Memory, maybe allocating the page          *
\***************************************************************************/
static fault_t
GetByte (ARMul_State * state, ARMword address, ARMword * data)
{
	fault_t fault;

	fault = mmu_read_byte (state, address, data);
	if (fault) {
//chy 2003-07-11: sometime has fault, but linux can continue running  !!!!????
//              printf("SKYEYE: GetByte fault %d \n", fault);
	}
	return fault;
}

/***************************************************************************\
*        Get a halfword into Virtual Memory, maybe allocating the page          *
\***************************************************************************/
static fault_t
GetHalfWord (ARMul_State * state, ARMword address, ARMword * data)
{
	fault_t fault;

	fault = mmu_read_halfword (state, address, data);
	if (fault) {
//chy 2003-07-11: sometime has fault, but linux can continue running  !!!!????
//              printf("SKYEYE: GetHalfWord fault %d \n", fault);
	}
	return fault;
}

/***************************************************************************\
*        Get a Word from Virtual Memory, maybe allocating the page          *
\***************************************************************************/

static fault_t
GetWord (ARMul_State * state, ARMword address, ARMword * data)
{
	fault_t fault;

	fault = mmu_read_word (state, address, data);
	if (fault) {
//chy 2003-07-11: sometime has fault, but linux can continue running  !!!!????
#if 0
/* XXX */ extern int hack;
		hack = 1;
#endif
#if 0
		printf ("mmu_read_word at 0x%08x: ", address);
		switch (fault) {
		case ALIGNMENT_FAULT:
			printf ("ALIGNMENT_FAULT");
			break;
		case SECTION_TRANSLATION_FAULT:
			printf ("SECTION_TRANSLATION_FAULT");
			break;
		case PAGE_TRANSLATION_FAULT:
			printf ("PAGE_TRANSLATION_FAULT");
			break;
		case SECTION_DOMAIN_FAULT:
			printf ("SECTION_DOMAIN_FAULT");
			break;
		case SECTION_PERMISSION_FAULT:
			printf ("SECTION_PERMISSION_FAULT");
			break;
		case SUBPAGE_PERMISSION_FAULT:
			printf ("SUBPAGE_PERMISSION_FAULT");
			break;
		default:
			printf ("Unrecognized fault number!");
		}
		printf ("\tpc = 0x%08x\n", state->Reg[15]);
#endif
	}
	return fault;
}

//2003-07-10 chy: lyh change
/****************************************************************************\
 * 			Load a Instrion Word into Virtual Memory						*
\****************************************************************************/
static fault_t
LoadInstr (ARMul_State * state, ARMword address, ARMword * instr)
{
	fault_t fault;
	fault = mmu_load_instr (state, address, instr);
	return fault;
	//if (fault)
	//      log_msg("load_instr fault = %d, address = %x\n", fault, address);
}

/***************************************************************************\
*        Put a byte into Virtual Memory, maybe allocating the page          *
\***************************************************************************/
static fault_t
PutByte (ARMul_State * state, ARMword address, ARMword data)
{
	fault_t fault;

	fault = mmu_write_byte (state, address, data);
	if (fault) {
//chy 2003-07-11: sometime has fault, but linux can continue running  !!!!????
//              printf("SKYEYE: PutByte fault %d \n", fault);
	}
	return fault;
}

/***************************************************************************\
*        Put a halfword into Virtual Memory, maybe allocating the page          *
\***************************************************************************/
static fault_t
PutHalfWord (ARMul_State * state, ARMword address, ARMword data)
{
	fault_t fault;

	fault = mmu_write_halfword (state, address, data);
	if (fault) {
//chy 2003-07-11: sometime has fault, but linux can continue running  !!!!????
//              printf("SKYEYE: PutHalfWord fault %d \n", fault);
	}
	return fault;
}

/***************************************************************************\
*        Put a Word into Virtual Memory, maybe allocating the page          *
\***************************************************************************/

static fault_t
PutWord (ARMul_State * state, ARMword address, ARMword data)
{
	fault_t fault;

	fault = mmu_write_word (state, address, data);
	if (fault) {
//chy 2003-07-11: sometime has fault, but linux can continue running  !!!!????
#if 0
/* XXX */ extern int hack;
		hack = 1;
#endif
#if 0
		printf ("mmu_write_word at 0x%08x: ", address);
		switch (fault) {
		case ALIGNMENT_FAULT:
			printf ("ALIGNMENT_FAULT");
			break;
		case SECTION_TRANSLATION_FAULT:
			printf ("SECTION_TRANSLATION_FAULT");
			break;
		case PAGE_TRANSLATION_FAULT:
			printf ("PAGE_TRANSLATION_FAULT");
			break;
		case SECTION_DOMAIN_FAULT:
			printf ("SECTION_DOMAIN_FAULT");
			break;
		case SECTION_PERMISSION_FAULT:
			printf ("SECTION_PERMISSION_FAULT");
			break;
		case SUBPAGE_PERMISSION_FAULT:
			printf ("SUBPAGE_PERMISSION_FAULT");
			break;
		default:
			printf ("Unrecognized fault number!");
		}
		printf ("\tpc = 0x%08x\n", state->Reg[15]);
#endif
	}
	return fault;
}

/***************************************************************************\
*                      Initialise the memory interface                      *
\***************************************************************************/

unsigned
ARMul_MemoryInit (ARMul_State * state, unsigned int initmemsize)
{
	return TRUE;
}

/***************************************************************************\
*                         Remove the memory interface                       *
\***************************************************************************/

void
ARMul_MemoryExit (ARMul_State * state)
{
}

/***************************************************************************\
*                   ReLoad Instruction                                     *
\***************************************************************************/

ARMword
ARMul_ReLoadInstr (ARMul_State * state, ARMword address, ARMword isize)
{
	ARMword data;
	fault_t fault;

#ifdef ABORTS
	if (address >= LOWABORT && address < HIGHABORT) {
		ARMul_PREFETCHABORT (address);
		return ARMul_ABORTWORD;
	}
	else {
		ARMul_CLEARABORT;
	}
#endif
#if 0
	/* do profiling for code coverage */
	if (skyeye_config.code_cov.prof_on)
			cov_prof(EXEC_FLAG, address);
#endif
#if 1
	if ((isize == 2) && (address & 0x2)) {
		ARMword lo, hi;
		if (!(skyeye_cachetype == INSTCACHE))
			fault = GetHalfWord (state, address, &lo);
		else
			fault = LoadInstr (state, address, &lo);
#if 0
		if (!fault) {
			if (!(skyeye_cachetype == INSTCACHE))
				fault = GetHalfWord (state, address + isize, &hi);
			else
				fault = LoadInstr (state, address + isize, &hi);

		}
#endif
		if (fault) {
			ARMul_PREFETCHABORT (address);
			return ARMul_ABORTWORD;
		}
		else {
			ARMul_CLEARABORT;
		}
		return lo;
#if 0
		if (state->bigendSig == HIGH)
			return (lo << 16) | (hi >> 16);
		else
			return ((hi & 0xFFFF) << 16) | (lo >> 16);
#endif
	}
#endif
	if (!(skyeye_cachetype == INSTCACHE))
		fault = GetWord (state, address, &data);
	else
		fault = LoadInstr (state, address, &data);

	if (fault) {

		/* dyf add for s3c6410 no instcache temporary 2010.9.17 */
		if (!(skyeye_cachetype == INSTCACHE)) {
			/* set translation fault  on prefetch abort */
			state->mmu.fault_statusi = fault  & 0xFF;
			state->mmu.fault_address = address;
		}
		/* add end */

		ARMul_PREFETCHABORT (address);
		return ARMul_ABORTWORD;
	}
	else {
		ARMul_CLEARABORT;
	}

	return data;
}

/***************************************************************************\
*                   Load Instruction, Sequential Cycle                      *
\***************************************************************************/

ARMword
ARMul_LoadInstrS (ARMul_State * state, ARMword address, ARMword isize)
{
	state->NumScycles++;

#ifdef HOURGLASS
	if ((state->NumScycles & HOURGLASS_RATE) == 0) {
		HOURGLASS;
	}
#endif

	return ARMul_ReLoadInstr (state, address, isize);
}

/***************************************************************************\
*                 Load Instruction, Non Sequential Cycle                    *
\***************************************************************************/

ARMword
ARMul_LoadInstrN (ARMul_State * state, ARMword address, ARMword isize)
{
	state->NumNcycles++;

	return ARMul_ReLoadInstr (state, address, isize);
}

/***************************************************************************\
*                      Read Word (but don't tell anyone!)                   *
\***************************************************************************/

ARMword
ARMul_ReadWord (ARMul_State * state, ARMword address)
{
	ARMword data;
	fault_t fault;

#ifdef ABORTS
	if (address >= LOWABORT && address < HIGHABORT) {
		ARMul_DATAABORT (address);
		return ARMul_ABORTWORD;
	}
	else {
		ARMul_CLEARABORT;
	}
#endif

	fault = GetWord (state, address, &data);
	if (fault) {
		state->mmu.fault_status =
			(fault | (state->mmu.last_domain << 4)) & 0xFF;
		state->mmu.fault_address = address;
		ARMul_DATAABORT (address);
		return ARMul_ABORTWORD;
	}
	else {
		ARMul_CLEARABORT;
	}
	return data;
}

/***************************************************************************\
*                        Load Word, Sequential Cycle                        *
\***************************************************************************/

ARMword
ARMul_LoadWordS (ARMul_State * state, ARMword address)
{
	state->NumScycles++;

	return ARMul_ReadWord (state, address);
}

/***************************************************************************\
*                      Load Word, Non Sequential Cycle                      *
\***************************************************************************/

ARMword
ARMul_LoadWordN (ARMul_State * state, ARMword address)
{
	state->NumNcycles++;

	return ARMul_ReadWord (state, address);
}

/***************************************************************************\
*                     Load Halfword, (Non Sequential Cycle)                 *
\***************************************************************************/

ARMword
ARMul_LoadHalfWord (ARMul_State * state, ARMword address)
{
	ARMword data;
	fault_t fault;

	state->NumNcycles++;
	fault = GetHalfWord (state, address, &data);

	if (fault) {
		state->mmu.fault_status =
			(fault | (state->mmu.last_domain << 4)) & 0xFF;
		state->mmu.fault_address = address;
		ARMul_DATAABORT (address);
		return ARMul_ABORTWORD;
	}
	else {
		ARMul_CLEARABORT;
	}

	return data;

}

/***************************************************************************\
*                      Read Byte (but don't tell anyone!)                   *
\***************************************************************************/
int ARMul_ICE_ReadByte(ARMul_State * state, ARMword address, ARMword *presult)
{
 ARMword data;
 fault_t fault;
 fault = GetByte (state, address, &data);
 if (fault) {
	 *presult=-1; fault=ALIGNMENT_FAULT; return fault;
 }else{
	 *(char *)presult=(unsigned char)(data & 0xff); fault=NO_FAULT; return fault;
 }
}
	 
 
ARMword
ARMul_ReadByte (ARMul_State * state, ARMword address)
{
	ARMword data;
	fault_t fault;

	fault = GetByte (state, address, &data);

	if (fault) {
		state->mmu.fault_status =
			(fault | (state->mmu.last_domain << 4)) & 0xFF;
		state->mmu.fault_address = address;
		ARMul_DATAABORT (address);
		return ARMul_ABORTWORD;
	}
	else {
		ARMul_CLEARABORT;
	}

	return data;

}

/***************************************************************************\
*                     Load Byte, (Non Sequential Cycle)                     *
\***************************************************************************/

ARMword
ARMul_LoadByte (ARMul_State * state, ARMword address)
{
	state->NumNcycles++;

	return ARMul_ReadByte (state, address);
}

/***************************************************************************\
*                     Write Word (but don't tell anyone!)                   *
\***************************************************************************/

void
ARMul_WriteWord (ARMul_State * state, ARMword address, ARMword data)
{
	fault_t fault;

#ifdef ABORTS
	if (address >= LOWABORT && address < HIGHABORT) {
		ARMul_DATAABORT (address);
		return;
	}
	else {
		ARMul_CLEARABORT;
	}
#endif

	fault = PutWord (state, address, data);
	if (fault) {
		state->mmu.fault_status =
			(fault | (state->mmu.last_domain << 4)) & 0xFF;
		state->mmu.fault_address = address;
		ARMul_DATAABORT (address);
		return;
	}
	else {
		ARMul_CLEARABORT;
	}
}

/***************************************************************************\
*                       Store Word, Sequential Cycle                        *
\***************************************************************************/

void
ARMul_StoreWordS (ARMul_State * state, ARMword address, ARMword data)
{
	state->NumScycles++;

	ARMul_WriteWord (state, address, data);
}

/***************************************************************************\
*                       Store Word, Non Sequential Cycle                        *
\***************************************************************************/

void
ARMul_StoreWordN (ARMul_State * state, ARMword address, ARMword data)
{
	state->NumNcycles++;

	ARMul_WriteWord (state, address, data);
}

/***************************************************************************\
*                    Store HalfWord, (Non Sequential Cycle)                 *
\***************************************************************************/

void
ARMul_StoreHalfWord (ARMul_State * state, ARMword address, ARMword data)
{
	fault_t fault;
	state->NumNcycles++;
	fault = PutHalfWord (state, address, data);
	if (fault) {
		state->mmu.fault_status =
			(fault | (state->mmu.last_domain << 4)) & 0xFF;
		state->mmu.fault_address = address;
		ARMul_DATAABORT (address);
		return;
	}
	else {
		ARMul_CLEARABORT;
	}
}

//chy 2006-04-15 
int ARMul_ICE_WriteByte (ARMul_State * state, ARMword address, ARMword data)
{
	fault_t fault;
	fault = PutByte (state, address, data);
	if (fault) 
		return 1; 
	else 
		return 0;
}
/***************************************************************************\
*                     Write Byte (but don't tell anyone!)                   *
\***************************************************************************/
//chy 2003-07-10, add real write byte fun
void
ARMul_WriteByte (ARMul_State * state, ARMword address, ARMword data)
{
	fault_t fault;
	fault = PutByte (state, address, data);
	if (fault) {
		state->mmu.fault_status =
			(fault | (state->mmu.last_domain << 4)) & 0xFF;
		state->mmu.fault_address = address;
		ARMul_DATAABORT (address);
		return;
	}
	else {
		ARMul_CLEARABORT;
	}
}

/***************************************************************************\
*                    Store Byte, (Non Sequential Cycle)                     *
\***************************************************************************/

void
ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
{
	state->NumNcycles++;

#ifdef VALIDATE
	if (address == TUBE) {
		if (data == 4)
			state->Emulate = FALSE;
		else
			(void) putc ((char) data, stderr);	/* Write Char */
		return;
	}
#endif

	ARMul_WriteByte (state, address, data);
}

/***************************************************************************\
*                   Swap Word, (Two Non Sequential Cycles)                  *
\***************************************************************************/

ARMword
ARMul_SwapWord (ARMul_State * state, ARMword address, ARMword data)
{
	ARMword temp;

	state->NumNcycles++;

	temp = ARMul_ReadWord (state, address);

	state->NumNcycles++;

	PutWord (state, address, data);

	return temp;
}

/***************************************************************************\
*                   Swap Byte, (Two Non Sequential Cycles)                  *
\***************************************************************************/

ARMword
ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
{
	ARMword temp;

	temp = ARMul_LoadByte (state, address);
	ARMul_StoreByte (state, address, data);

	return temp;
}

/***************************************************************************\
*                             Count I Cycles                                *
\***************************************************************************/

void
ARMul_Icycles (ARMul_State * state, unsigned number,
	       ARMword address)
{
	state->NumIcycles += number;
	ARMul_CLEARABORT;
}

/***************************************************************************\
*                             Count C Cycles                                *
\***************************************************************************/

void
ARMul_Ccycles (ARMul_State * state, unsigned number,
	       ARMword address)
{
	state->NumCcycles += number;
	ARMul_CLEARABORT;
}