File: head.S

package info (click to toggle)
proll 18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,012 kB
  • ctags: 3,459
  • sloc: ansic: 12,161; asm: 2,313; makefile: 546
file content (591 lines) | stat: -rw-r--r-- 17,548 bytes parent folder | download | duplicates (3)
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
/**
 ** Standalone startup code for Linux PROM emulator.
 ** Copyright 1999 Pete A. Zaitcev
 ** This code is licensed under GNU General Public License.
 **/
/*
 * $Id: head.S,v 1.14 2002/07/23 05:45:26 zaitcev Exp $
 *
 * This is a version of src/head.S for Roxy.
 * - Framebuffer debugging I/O replaced with serial.
 * - SILO support discarded.
 * - Remapping is done regardless of our initial virtual address,
 *   and 16MB at a time because L2 page table is not allocated for us.
 */

#include <psr.h>
#include <asi.h>
// #include <crs.h>
/* #include <asm/head.h> */	/* Trap entries. Do not use. */

#define PHYS_RO_SU_A	0x300002f8
#define C_LABEL(name)   name
#define REGWIN_SZ       0x40

#define WRITE_PAUSE    nop; nop; nop; /* Have to do this after %wim/%psr chg */

  /* 22 is 24-2, (va)>>(SRMMU_PGDIR_SHIFT-PTESIZESHFT) */
#define VATOPGDOFF(va)		(((va)>>22)&0x3FC)
#define VATOPMDOFF(va)		(((va)>>16)&0xFC)
#define VATOPGDOFF_REG(vr)	srl vr,22,vr; and vr,0x3FC,vr
#define VATOPMDOFF_REG(vr)	srl vr,16,vr; and vr,0xFC,vr

#define NOP_INSN       0x01000000     /* Used to patch sparc_save_state */

/* Here are some trap goodies */

/* Data/text faults. */
#define SRMMU_TFAULT rd %psr, %l0; rd %wim, %l3; b C_LABEL(srmmu_fault); mov 1, %l7;
#define SRMMU_DFAULT rd %psr, %l0; rd %wim, %l3; b C_LABEL(srmmu_fault); mov 0, %l7;

/* Window overflows/underflows are special and we need to try to be as
 * efficient as possible here....
 */
#define WINDOW_SPILL \
        rd %psr, %l0; rd %wim, %l3; b spill_window_entry; nop;

#define WINDOW_FILL \
        rd %psr, %l0; rd %wim, %l3; b fill_window_entry; nop;

#define STUB_TRAP	ba stub_trap; nop; nop; nop;

#define TRAP_ENTRY(a,b)		STUB_TRAP
#define SKIP_TRAP(a,b)		STUB_TRAP
#define SUNOS_SYSCALL_TRAP	STUB_TRAP
#define SOLARIS_SYSCALL_TRAP	STUB_TRAP
#define NETBSD_SYSCALL_TRAP	STUB_TRAP
#define LINUX_SYSCALL_TRAP	STUB_TRAP
#define BREAKPOINT_TRAP		STUB_TRAP
#define NMI_TRAP		STUB_TRAP
#define GETCC_TRAP		STUB_TRAP
#define SETCC_TRAP		STUB_TRAP
#define BAD_TRAP(n)		STUB_TRAP
#define	TRAP_ENTRY_INTERRUPT(i)		STUB_TRAP
#define	INDIRECT_SOLARIS_SYSCALL(i)	STUB_TRAP

	.section ".text"
	.globl start, _start
_start:
start:
	b goprol; nop
	/* We waste 0x3000 in order to keep LOADBASE at 0x4000 */
	.skip 0x2ff8

	.globl spill_window_entry, fill_window_entry
C_LABEL(trapbase):
t_zero:	b goprol; nop; nop; nop
t_tflt:	SRMMU_TFAULT                        /* Inst. Access Exception        */
t_bins:	TRAP_ENTRY(0x2, bad_instruction)    /* Illegal Instruction           */
t_pins:	TRAP_ENTRY(0x3, priv_instruction)   /* Privileged Instruction        */
t_fpd:	TRAP_ENTRY(0x4, fpd_trap_handler)   /* Floating Point Disabled       */
t_wovf:	WINDOW_SPILL                        /* Window Overflow               */
t_wunf:	WINDOW_FILL                         /* Window Underflow              */
t_mna:	TRAP_ENTRY(0x7, mna_handler)        /* Memory Address Not Aligned    */
t_fpe:	TRAP_ENTRY(0x8, fpe_trap_handler)   /* Floating Point Exception      */
t_dflt:	SRMMU_DFAULT                        /* Data Miss Exception           */
t_tio:	TRAP_ENTRY(0xa, do_tag_overflow)    /* Tagged Instruction Ovrflw     */
t_wpt:	TRAP_ENTRY(0xb, do_watchpoint)      /* Watchpoint Detected           */
t_badc:	BAD_TRAP(0xc) BAD_TRAP(0xd) BAD_TRAP(0xe) BAD_TRAP(0xf) BAD_TRAP(0x10)
t_irq1:	TRAP_ENTRY_INTERRUPT(1)             /* IRQ Software/SBUS Level 1     */
t_irq2:	TRAP_ENTRY_INTERRUPT(2)             /* IRQ SBUS Level 2              */
t_irq3:	TRAP_ENTRY_INTERRUPT(3)             /* IRQ SCSI/DMA/SBUS Level 3     */
t_irq4:	TRAP_ENTRY_INTERRUPT(4)             /* IRQ Software Level 4          */
t_irq5:	TRAP_ENTRY_INTERRUPT(5)             /* IRQ SBUS/Ethernet Level 5     */
t_irq6:	TRAP_ENTRY_INTERRUPT(6)             /* IRQ Software Level 6          */
t_irq7:	TRAP_ENTRY_INTERRUPT(7)             /* IRQ Video/SBUS Level 5        */
t_irq8:	TRAP_ENTRY_INTERRUPT(8)             /* IRQ SBUS Level 6              */
t_irq9:	TRAP_ENTRY_INTERRUPT(9)             /* IRQ SBUS Level 7              */
t_irq10:TRAP_ENTRY_INTERRUPT(10)            /* IRQ Timer #1 (one we use)     */
t_irq11:TRAP_ENTRY_INTERRUPT(11)            /* IRQ Floppy Intr.              */
t_irq12:TRAP_ENTRY_INTERRUPT(12)            /* IRQ Zilog serial chip         */
t_irq13:TRAP_ENTRY_INTERRUPT(13)            /* IRQ Audio Intr.               */
t_irq14:TRAP_ENTRY_INTERRUPT(14)            /* IRQ Timer #2                  */
t_nmi:	NMI_TRAP                            /* Level 15 (NMI)                */
t_racc:	TRAP_ENTRY(0x20, do_reg_access)     /* General Register Access Error */
t_iacce:BAD_TRAP(0x21)                      /* Instr Access Error            */
t_bad22:BAD_TRAP(0x22) BAD_TRAP(0x23)
t_cpdis:TRAP_ENTRY(0x24, do_cp_disabled)    /* Co-Processor Disabled         */
t_uflsh:SKIP_TRAP(0x25, unimp_flush)        /* Unimplemented FLUSH inst.     */
t_bad26:BAD_TRAP(0x26) BAD_TRAP(0x27)
t_cpexc:TRAP_ENTRY(0x28, do_cp_exception)   /* Co-Processor Exception        */
t_dacce:SRMMU_DFAULT                        /* Data Access Error             */
t_hwdz:	TRAP_ENTRY(0x2a, do_hw_divzero)     /* Division by zero, you lose... */
t_dserr:BAD_TRAP(0x2b)                      /* Data Store Error              */
t_daccm:BAD_TRAP(0x2c)                      /* Data Access MMU-Miss          */
t_bad2d:               BAD_TRAP(0x2d) BAD_TRAP(0x2e) BAD_TRAP(0x2f)
        BAD_TRAP(0x30) BAD_TRAP(0x31) BAD_TRAP(0x32) BAD_TRAP(0x33)
        BAD_TRAP(0x34) BAD_TRAP(0x35) BAD_TRAP(0x36) BAD_TRAP(0x37)
        BAD_TRAP(0x38) BAD_TRAP(0x39) BAD_TRAP(0x3a) BAD_TRAP(0x3b)
t_iaccm:BAD_TRAP(0x3c)                      /* Instr Access MMU-Miss         */
 BAD_TRAP(0x3d) BAD_TRAP(0x3e) BAD_TRAP(0x3f)
 BAD_TRAP(0x40) BAD_TRAP(0x41) BAD_TRAP(0x42) BAD_TRAP(0x43)
 BAD_TRAP(0x44) BAD_TRAP(0x45) BAD_TRAP(0x46) BAD_TRAP(0x47)
 BAD_TRAP(0x48) BAD_TRAP(0x49) BAD_TRAP(0x4a) BAD_TRAP(0x4b)
 BAD_TRAP(0x4c) BAD_TRAP(0x4d) BAD_TRAP(0x4e) BAD_TRAP(0x4f)
 BAD_TRAP(0x50) BAD_TRAP(0x51) BAD_TRAP(0x52) BAD_TRAP(0x53)
 BAD_TRAP(0x54) BAD_TRAP(0x55) BAD_TRAP(0x56) BAD_TRAP(0x57)
 BAD_TRAP(0x58) BAD_TRAP(0x59) BAD_TRAP(0x5a) BAD_TRAP(0x5b)
 BAD_TRAP(0x5c) BAD_TRAP(0x5d) BAD_TRAP(0x5e) BAD_TRAP(0x5f)
 BAD_TRAP(0x60) BAD_TRAP(0x61) BAD_TRAP(0x62) BAD_TRAP(0x63)
 BAD_TRAP(0x64) BAD_TRAP(0x65) BAD_TRAP(0x66) BAD_TRAP(0x67)
 BAD_TRAP(0x68) BAD_TRAP(0x69) BAD_TRAP(0x6a) BAD_TRAP(0x6b)
 BAD_TRAP(0x6c) BAD_TRAP(0x6d) BAD_TRAP(0x6e) BAD_TRAP(0x6f)
 BAD_TRAP(0x70) BAD_TRAP(0x71) BAD_TRAP(0x72) BAD_TRAP(0x73)
 BAD_TRAP(0x74) BAD_TRAP(0x75) BAD_TRAP(0x76) BAD_TRAP(0x77)
 BAD_TRAP(0x78) BAD_TRAP(0x79) BAD_TRAP(0x7a) BAD_TRAP(0x7b)
 BAD_TRAP(0x7c) BAD_TRAP(0x7d) BAD_TRAP(0x7e) BAD_TRAP(0x7f)
t_sunos:SUNOS_SYSCALL_TRAP                  /* SunOS System Call             */
t_sbkpt:BREAKPOINT_TRAP                     /* Software Breakpoint/KGDB      */
t_divz:	BAD_TRAP(0x82)                      /* Divide by zero trap           */
t_flwin:TRAP_ENTRY(0x83, do_flush_windows)  /* Flush Windows Trap            */
t_clwin:BAD_TRAP(0x84)                      /* Clean Windows Trap            */
t_rchk:	BAD_TRAP(0x85)                      /* Range Check                   */
t_funal:BAD_TRAP(0x86)                      /* Fix Unaligned Access Trap     */
t_iovf:	BAD_TRAP(0x87)                      /* Integer Overflow Trap         */
t_slowl:SOLARIS_SYSCALL_TRAP                /* Slowaris System Call          */
t_netbs:NETBSD_SYSCALL_TRAP                 /* Net-B.S. System Call          */
t_bad8a:BAD_TRAP(0x8a) BAD_TRAP(0x8b) BAD_TRAP(0x8c) BAD_TRAP(0x8d)
 BAD_TRAP(0x8e) BAD_TRAP(0x8f)
t_linux:LINUX_SYSCALL_TRAP                  /* Linux System Call             */
t_bad91:BAD_TRAP(0x91) BAD_TRAP(0x92) BAD_TRAP(0x93)
 BAD_TRAP(0x94) BAD_TRAP(0x95) BAD_TRAP(0x96) BAD_TRAP(0x97)
 BAD_TRAP(0x98) BAD_TRAP(0x99) BAD_TRAP(0x9a) BAD_TRAP(0x9b) BAD_TRAP(0x9c) BAD_TRAP(0x9d) BAD_TRAP(0x9e) BAD_TRAP(0x9f)
t_getcc:GETCC_TRAP                          /* Get Condition Codes           */
t_setcc:SETCC_TRAP                          /* Set Condition Codes           */
t_bada2:BAD_TRAP(0xa2) BAD_TRAP(0xa3)
 BAD_TRAP(0xa4) BAD_TRAP(0xa5) BAD_TRAP(0xa6)
t_slowi:INDIRECT_SOLARIS_SYSCALL(156)
 BAD_TRAP(0xa8) BAD_TRAP(0xa9) BAD_TRAP(0xaa) BAD_TRAP(0xab)
 BAD_TRAP(0xac) BAD_TRAP(0xad) BAD_TRAP(0xae) BAD_TRAP(0xaf)
 BAD_TRAP(0xb0) BAD_TRAP(0xb1) BAD_TRAP(0xb2) BAD_TRAP(0xb3)
 BAD_TRAP(0xb4) BAD_TRAP(0xb5) BAD_TRAP(0xb6) BAD_TRAP(0xb7)
 BAD_TRAP(0xb8) BAD_TRAP(0xb9) BAD_TRAP(0xba) BAD_TRAP(0xbb)
 BAD_TRAP(0xbc) BAD_TRAP(0xbd) BAD_TRAP(0xbe) BAD_TRAP(0xbf)
t_badc0:BAD_TRAP(0xc0) BAD_TRAP(0xc1) BAD_TRAP(0xc2) BAD_TRAP(0xc3)
 BAD_TRAP(0xc4) BAD_TRAP(0xc5) BAD_TRAP(0xc6) BAD_TRAP(0xc7)
 BAD_TRAP(0xc8) BAD_TRAP(0xc9) BAD_TRAP(0xca) BAD_TRAP(0xcb)
 BAD_TRAP(0xcc) BAD_TRAP(0xcd) BAD_TRAP(0xce) BAD_TRAP(0xcf)
 BAD_TRAP(0xd0) BAD_TRAP(0xd1) BAD_TRAP(0xd2) BAD_TRAP(0xd3)
t_badd4:BAD_TRAP(0xd4) BAD_TRAP(0xd5) BAD_TRAP(0xd6) BAD_TRAP(0xd7)
 BAD_TRAP(0xd8) BAD_TRAP(0xd9) BAD_TRAP(0xda) BAD_TRAP(0xdb)
 BAD_TRAP(0xdc) BAD_TRAP(0xdd) BAD_TRAP(0xde) BAD_TRAP(0xdf)
 BAD_TRAP(0xe0) BAD_TRAP(0xe1) BAD_TRAP(0xe2) BAD_TRAP(0xe3)
 BAD_TRAP(0xe4) BAD_TRAP(0xe5) BAD_TRAP(0xe6) BAD_TRAP(0xe7)
t_bade8:BAD_TRAP(0xe8) BAD_TRAP(0xe9) BAD_TRAP(0xea) BAD_TRAP(0xeb)
 BAD_TRAP(0xec) BAD_TRAP(0xed) BAD_TRAP(0xee) BAD_TRAP(0xef)
 BAD_TRAP(0xf0) BAD_TRAP(0xf1) BAD_TRAP(0xf2) BAD_TRAP(0xf3)
 BAD_TRAP(0xf4) BAD_TRAP(0xf5) BAD_TRAP(0xf6) BAD_TRAP(0xf7)
 BAD_TRAP(0xf8) BAD_TRAP(0xf9) BAD_TRAP(0xfa) BAD_TRAP(0xfb)
t_badfc:BAD_TRAP(0xfc) BAD_TRAP(0xfd)
dbtrap:	BAD_TRAP(0xfe)                      /* Debugger/PROM breakpoint #1   */
dbtrap2:BAD_TRAP(0xff)                      /* Debugger/PROM breakpoint #2   */	

stub_trap:
 	set PHYS_RO_SU_A, %g1
	or %g0, 0x40, %g2
	stba %g2, [%g1] ASI_M_BYPASS
1:	ba 1b; nop

	.section ".bss"
	.align 8
bss_start:

	.section ".text"
	.align 4
goprol:
	call reloc
	 nop
reloc:
	mov %o7, %g6

	! Verify that we are on a known hardware.
	rd %psr, %g1
	srl %g1, 24, %g7
	subcc %g7, 0x42, %g0	! MicroSPARC-II by TI
	be 2f
	subcc %g7, 0x04, %g0	! MicroSPARC-II by Fujitsu
	be 2f
	 nop
3:
	ba 3b
	 nop
2:
	! Map us high
	! From this point on PROM is not available (moot point on Roxy though).
	rd %psr, %g1
	or %g1, PSR_PIL, %g1
	wr %g1, 0, %psr
	WRITE_PAUSE

	set	0x4001, %g5			! PTP, a make believe L2 table

	set     AC_M_CTPR, %g1
	lda     [%g1] ASI_M_MMUREGS, %g1	! get ctx table ptr
	sll     %g1, 0x4, %g1			! make physical addr
	lda     [%g1] ASI_M_BYPASS, %g1		! ptr to level 1 pg_table
	srl     %g1, 0x4, %g1
	sll     %g1, 0x8, %g1			! make phys addr for l1 tbl

	or	%g6, 0, %g2
	VATOPGDOFF_REG(%g2)
	add	%g1, %g2, %g2
	lda	[%g2] ASI_M_BYPASS, %g2		! get l1 entry for va=%g6

	srl     %g2, 0x4, %g2
	sll     %g2, 0x8, %g2			! make phys addr for l2 tbl

	or	%g6, 0, %g4
	VATOPMDOFF_REG(%g4)
	add	%g2, %g4, %g4
	lda	[%g4] ASI_M_BYPASS, %g4		! get l2 entry for va=%g6

	add	%g1, VATOPGDOFF(PROLBASE), %g3
	sta     %g5, [%g3] ASI_M_BYPASS		! "allocate" a table, bleh...

	srl     %g5, 0x4, %g3
	sll     %g3, 0x8, %g3			! make phys addr for l2 tbl
	/* 100 hacker points to one who bzeros the table here. */
	add     %g3, VATOPMDOFF(PROLBASE), %g5
	sta     %g4, [%g5] ASI_M_BYPASS		! place at PROLBASE entry

	/*
	 * Flush cache
	 */
	mov	%g0, %g1		! addr
	set	0x2000, %o0		! const(0x2000)
2:
	subcc	%g1, %o0, %g0
	bcc	1f
	 nop

	sta	%g0, [%g1] ASI_M_DATAC_TAG
	sll	%g1, 1, %g2
	sta	%g0, [%g2] ASI_M_TXTC_TAG

	add	%g1, 0x10, %g1
	ba	2b
	 nop
1:

	/*
	 * Flush old page table references from TLB
	 */
	set	0x400, %g1
	sta %g0, [%g1] ASI_M_FLUSH_PROBE

	/*
	 * Now we can jump.
	 */
	set himem1, %g1
	jmpl %g1 + 0, %g0
	 nop

	.section ".data"

	.align 4
	.globl  C_LABEL(cputyp)
        .globl  C_LABEL(nwindows)
        .globl  C_LABEL(nwindowsm1)
C_LABEL(cputyp):			.word   1
C_LABEL(nwindows):			.word   8
C_LABEL(nwindowsm1):			.word   7

	.align	0x1000		! PAGE_SIZE
	.globl C_LABEL(bootup_user_stack)
C_LABEL(bootup_user_stack):		.skip 0x2000

	.section ".text"
himem1:				! We can use relocated labels now.
	sethi	%hi( C_LABEL(cputyp) ), %o0
	st	%g7, [%o0 + %lo( C_LABEL(cputyp) )]

	/*
	 * Clear trap reporting for good luck.
	 */
	set AC_M_SFAR, %g2
	lda [%g2] ASI_M_MMUREGS, %g0
	set AC_M_SFSR, %g1
	lda [%g1] ASI_M_MMUREGS, %g0

	/*
	 * The code which enables traps is a simplified version of
	 * kernel head.S.
	 *
	 * We know number of windows as 8 so we do not calculate them.
	 * The deadwood is here for any case.
	 */

	/* Turn on Supervisor, EnableFloating, and all the PIL bits.
	 * Also puts us in register window zero with traps off.
	 */
	set	(PSR_PS | PSR_S | PSR_PIL | PSR_EF), %g2
	wr	%g2, 0x0, %psr
	WRITE_PAUSE

	/* I want a kernel stack NOW! */
	set	C_LABEL(bootup_user_stack), %g1
	set	(0x2000 - REGWIN_SZ), %g2
	add	%g1, %g2, %sp
	mov	0, %fp			/* And for good luck */

	/* Zero out our BSS section. */
	set	C_LABEL(bss_start) , %o0	! First address of BSS
	set	C_LABEL(end) , %o1		! Last address of BSS
	ba	2f
	 nop
1:
	st	%g0, [%o0]
2:
	subcc	%o0, %o1, %g0
	bl	1b
	 add	%o0, 0x4, %o0

	mov	2, %g1
	wr	%g1, 0x0, %wim			! make window 1 invalid
	WRITE_PAUSE

#if 0
	wr  %g0, 0x0, %wim
	WRITE_PAUSE
	save
	rd  %psr, %g3
	restore
	and  %g3, PSR_CWP, %g3
	add  %g3, 0x1, %g3
#else
	or	%g0, 8, %g3
#endif

	sethi	%hi( C_LABEL(nwindows) ), %g4
	st	%g3, [%g4 + %lo( C_LABEL(nwindows) )]

	sub	%g3, 0x1, %g3
	sethi	%hi( C_LABEL(nwindowsm1) ), %g4
	st	%g3, [%g4 + %lo( C_LABEL(nwindowsm1) )]

	/* Here we go, start using Linux's trap table... */
	set	C_LABEL(trapbase), %g3
	wr	%g3, 0x0, %tbr
	WRITE_PAUSE

	/* Finally, turn on traps so that we can call c-code. */
	rd	%psr, %g3
	wr	%g3, 0x0, %psr
	WRITE_PAUSE

	wr	%g3, PSR_ET, %psr
	WRITE_PAUSE

	.globl prolmain
	call C_LABEL(prolmain)
	 nop

3:
	b       3b
	 nop

/*
 * Memory access trap handler
 *   %l0  program %psr from trap table entry
 *   %l1  program %pc from hardware
 *   %l2  program %npc from hardware
 *   %l3  program %wim from trap table entry
 *   %l4
 *   %l5
 *   %l6
 *   %l7  text flag from trap table entry
 */
	.section ".data"
	.align 4
	.globl ignore_fault, fault_ignored
C_LABEL(ignore_fault):		.word 0
C_LABEL(fault_ignored):		.word 0

	.section ".text"
	.globl srmmu_fault
C_LABEL(srmmu_fault):

	set AC_M_SFAR, %l6
	set AC_M_SFSR, %l5
	lda [%l6] ASI_M_MMUREGS, %l6
	lda [%l5] ASI_M_MMUREGS, %l5

	set ignore_fault, %l5
	ld [%l5], %l5
	subcc %l5, %g0, %g0		/* NULL pointer trap faults always */
	be 3f
	 nop
	subcc %l5, %l6, %g0
	be 2f
	 nop
3:

1:	ba 1b; nop

2:
	set C_LABEL(fault_ignored), %l5
	mov 1, %l6
	st %l6, [%l5]

	/*
	 * Skip the faulting instruction.
	 * I think it works when next instruction is a branch even.
	 */
	or %l2, 0, %l1
	add %l2, 4, %l2

	wr %l0, 0, %psr
	WRITE_PAUSE
	jmp %l1
	rett %l2

/*
 * Slow external versions of st_bypass and ld_bypass.
 * rconsole.c uses inlines. We call these in places which are not speed
 * critical, to avoid compiler bugs.
 */
	.globl C_LABEL(st_bypass)
C_LABEL(st_bypass):
	retl
	 sta %o1, [%o0] ASI_M_BYPASS
	.globl C_LABEL(ld_bypass)
C_LABEL(ld_bypass):
	retl
	 lda [%o0] ASI_M_BYPASS, %o0
	.globl C_LABEL(sth_bypass)
C_LABEL(sth_bypass):
	retl
	 stha %o1, [%o0] ASI_M_BYPASS
	.globl C_LABEL(ldh_bypass)
C_LABEL(ldh_bypass):
	retl
	 lduha [%o0] ASI_M_BYPASS, %o0
	.globl C_LABEL(stb_bypass)
C_LABEL(stb_bypass):
	retl
	 stba %o1, [%o0] ASI_M_BYPASS
	.globl C_LABEL(ldb_bypass)
C_LABEL(ldb_bypass):
	retl
	 lduba [%o0] ASI_M_BYPASS, %o0

/*
 * move_phys_high(unsigned newpa, int size)
 * Here we move ourselves from low physical to high physical.
 * We are in a position of a man who stands on a rag which
 * he wants to move somewhere else.
 */
	.globl C_LABEL(move_phys_high)
C_LABEL(move_phys_high):
	save	%sp, -64, %sp

	/*
	 * We jump low so high mappings are free for abuse.
	 */
	set (PROLBASE-0x4000), %l1		! PROLBASE-LOADBASE
	set move_phys_high_atlow, %l0
	sub %l0, %l1, %l0
	jmp %l0
	 nop
move_phys_high_atlow:

	set     AC_M_CTPR, %l1
	lda     [%l1] ASI_M_MMUREGS, %l1	! get ctx table ptr
	sll     %l1, 0x4, %l1			! make physical addr
	lda     [%l1] ASI_M_BYPASS, %l1		! ptr to level 1 pg_table
	srl     %l1, 0x4, %l1
	sll     %l1, 0x8, %i5		! rpa:%i5

	set PROLBASE, %i2		! va:%i2
	set 0x1000, %i3			! PAGE_SIZE:%i3
	mov %i1, %i4			! size:%i4
w_3:
	subcc %i4, %g0, %g0		! while (size > 0)
	ble w_2
	 nop
	sub %i4, %i3, %i4

	/*
	 * Walk tables down to a page, move it.
	 */

	srl %i2, 24, %l0
	and %l0, 0xFF, %l0
	sll %l0, 2, %l0
	add %i5, %l0, %l0
	lda [%l0] ASI_M_BYPASS, %l1
	andcc %l1, 0x03, %g0
	bne i_1
	 nop

1: ba 1b; nop

i_1:
	srl %l1, 4, %l0
	sll %l0, 8, %l0

	srl %i2, 18, %l1
	and %l1, 0x3F, %l1
	sll %l1, 2, %l1
	add %l0, %l1, %l0
	lda [%l0] ASI_M_BYPASS, %l0
	andcc %l0, 0x03, %g0
	bne i_2
	 nop

1: ba 1b; nop

i_2:
	srl %l0, 4, %l0
	sll %l0, 8, %l0

	srl %i2, 12, %l1
	and %l1, 0x3F, %l1
	sll %l1, 2, %l1
	add %l0, %l1, %l0

	srl %i0, 4, %l1
	or %l1, 0xBE, %l1
	sta %l1, [%l0] ASI_M_BYPASS

	add %i3, %i2, %i2	! va += PAGE_SIZE
	add %i3, %i0, %i0	! newpa += PAGE_SIZE
	ba w_3
	 nop
w_2:

	/*
	 * Flush TLB
	 * We lazy to flush cache because we are about to copy anyways.
	 */
	set	0x400, %l1
	sta %g0, [%l1] ASI_M_FLUSH_PROBE

	/*
	 * Copy the stuff over
	 */
	set PROLBASE, %i2		! va:%i2
	set 0x4000, %i3			! ova:%i3 = LOADBASE
	mov %i1, %i4			! size:%i4
	ba w_5
	 nop
w_4:
	ld [%i3], %i5
	add %i3, 4, %i3
	st %i5, [%i2]
	add %i2, 4, %i2
w_5:
	cmp %i4, 0			! while (size > 0)
	bg w_4
	 sub %i4, 4, %i4

	/*
	 * All done, return to high virt and high phys.
	 */
	set move_phys_high_athigh, %l0
	jmp %l0
	 nop
move_phys_high_athigh:

	ret
	 restore