xref: /titanic_52/usr/src/uts/sun4v/ml/trap_table.s (revision 505d05c73a6e56769f263d4803b22eddd168ee24)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29#if !defined(lint)
30#include "assym.h"
31#endif /* !lint */
32#include <sys/asm_linkage.h>
33#include <sys/privregs.h>
34#include <sys/sun4asi.h>
35#include <sys/machasi.h>
36#include <sys/hypervisor_api.h>
37#include <sys/machtrap.h>
38#include <sys/machthread.h>
39#include <sys/pcb.h>
40#include <sys/pte.h>
41#include <sys/mmu.h>
42#include <sys/machpcb.h>
43#include <sys/async.h>
44#include <sys/intreg.h>
45#include <sys/scb.h>
46#include <sys/psr_compat.h>
47#include <sys/syscall.h>
48#include <sys/machparam.h>
49#include <sys/traptrace.h>
50#include <vm/hat_sfmmu.h>
51#include <sys/archsystm.h>
52#include <sys/utrap.h>
53#include <sys/clock.h>
54#include <sys/intr.h>
55#include <sys/fpu/fpu_simulator.h>
56#include <vm/seg_spt.h>
57
58/*
59 * WARNING: If you add a fast trap handler which can be invoked by a
60 * non-privileged user, you may have to use the FAST_TRAP_DONE macro
61 * instead of "done" instruction to return back to the user mode. See
62 * comments for the "fast_trap_done" entry point for more information.
63 *
64 * An alternate FAST_TRAP_DONE_CHK_INTR macro should be used for the
65 * cases where you always want to process any pending interrupts before
66 * returning back to the user mode.
67 */
68#define	FAST_TRAP_DONE		\
69	ba,a	fast_trap_done
70
71#define	FAST_TRAP_DONE_CHK_INTR	\
72	ba,a	fast_trap_done_chk_intr
73
74/*
75 * SPARC V9 Trap Table
76 *
77 * Most of the trap handlers are made from common building
78 * blocks, and some are instantiated multiple times within
79 * the trap table. So, I build a bunch of macros, then
80 * populate the table using only the macros.
81 *
82 * Many macros branch to sys_trap.  Its calling convention is:
83 *	%g1		kernel trap handler
84 *	%g2, %g3	args for above
85 *	%g4		desire %pil
86 */
87
88#ifdef	TRAPTRACE
89
90/*
91 * Tracing macro. Adds two instructions if TRAPTRACE is defined.
92 */
93#define	TT_TRACE(label)		\
94	ba	label		;\
95	rd	%pc, %g7
96#define	TT_TRACE_INS	2
97
98#define	TT_TRACE_L(label)	\
99	ba	label		;\
100	rd	%pc, %l4	;\
101	clr	%l4
102#define	TT_TRACE_L_INS	3
103
104#else
105
106#define	TT_TRACE(label)
107#define	TT_TRACE_INS	0
108
109#define	TT_TRACE_L(label)
110#define	TT_TRACE_L_INS	0
111
112#endif
113
114/*
115 * This macro is used to update per cpu mmu stats in perf critical
116 * paths. It is only enabled in debug kernels or if SFMMU_STAT_GATHER
117 * is defined.
118 */
119#if defined(DEBUG) || defined(SFMMU_STAT_GATHER)
120#define	HAT_PERCPU_DBSTAT(stat)			\
121	mov	stat, %g1			;\
122	ba	stat_mmu			;\
123	rd	%pc, %g7
124#else
125#define	HAT_PERCPU_DBSTAT(stat)
126#endif /* DEBUG || SFMMU_STAT_GATHER */
127
128/*
129 * This first set are funneled to trap() with %tt as the type.
130 * Trap will then either panic or send the user a signal.
131 */
132/*
133 * NOT is used for traps that just shouldn't happen.
134 * It comes in both single and quadruple flavors.
135 */
136#if !defined(lint)
137	.global	trap
138#endif /* !lint */
139#define	NOT			\
140	TT_TRACE(trace_gen)	;\
141	set	trap, %g1	;\
142	rdpr	%tt, %g3	;\
143	ba,pt	%xcc, sys_trap	;\
144	sub	%g0, 1, %g4	;\
145	.align	32
146#define	NOT4	NOT; NOT; NOT; NOT
147
148#define	NOTP				\
149	TT_TRACE(trace_gen)		;\
150	ba,pt	%xcc, ptl1_panic	;\
151	  mov	PTL1_BAD_TRAP, %g1	;\
152	.align	32
153#define	NOTP4	NOTP; NOTP; NOTP; NOTP
154
155/*
156 * RED is for traps that use the red mode handler.
157 * We should never see these either.
158 */
159#define	RED	NOT
160/*
161 * BAD is used for trap vectors we don't have a kernel
162 * handler for.
163 * It also comes in single and quadruple versions.
164 */
165#define	BAD	NOT
166#define	BAD4	NOT4
167
168#define	DONE			\
169	done;			\
170	.align	32
171
172/*
173 * TRAP vectors to the trap() function.
174 * It's main use is for user errors.
175 */
176#if !defined(lint)
177	.global	trap
178#endif /* !lint */
179#define	TRAP(arg)		\
180	TT_TRACE(trace_gen)	;\
181	set	trap, %g1	;\
182	mov	arg, %g3	;\
183	ba,pt	%xcc, sys_trap	;\
184	sub	%g0, 1, %g4	;\
185	.align	32
186
187/*
188 * SYSCALL is used for system calls on both ILP32 and LP64 kernels
189 * depending on the "which" parameter (should be either syscall_trap
190 * or syscall_trap32).
191 */
192#define	SYSCALL(which)			\
193	TT_TRACE(trace_gen)		;\
194	set	(which), %g1		;\
195	ba,pt	%xcc, sys_trap		;\
196	sub	%g0, 1, %g4		;\
197	.align	32
198
199/*
200 * GOTO just jumps to a label.
201 * It's used for things that can be fixed without going thru sys_trap.
202 */
203#define	GOTO(label)		\
204	.global	label		;\
205	ba,a	label		;\
206	.empty			;\
207	.align	32
208
209/*
210 * GOTO_TT just jumps to a label.
211 * correctable ECC error traps at  level 0 and 1 will use this macro.
212 * It's used for things that can be fixed without going thru sys_trap.
213 */
214#define	GOTO_TT(label, ttlabel)		\
215	.global	label		;\
216	TT_TRACE(ttlabel)	;\
217	ba,a	label		;\
218	.empty			;\
219	.align	32
220
221/*
222 * Privileged traps
223 * Takes breakpoint if privileged, calls trap() if not.
224 */
225#define	PRIV(label)			\
226	rdpr	%tstate, %g1		;\
227	btst	TSTATE_PRIV, %g1	;\
228	bnz	label			;\
229	rdpr	%tt, %g3		;\
230	set	trap, %g1		;\
231	ba,pt	%xcc, sys_trap		;\
232	sub	%g0, 1, %g4		;\
233	.align	32
234
235
236/*
237 * DTrace traps.
238 */
239#define	DTRACE_FASTTRAP			\
240	.global dtrace_fasttrap_probe				;\
241	.global dtrace_fasttrap_probe_ptr			;\
242	sethi	%hi(dtrace_fasttrap_probe_ptr), %g4		;\
243	ldn	[%g4 + %lo(dtrace_fasttrap_probe_ptr)], %g4	;\
244	set	dtrace_fasttrap_probe, %g1			;\
245	brnz,pn	%g4, user_trap					;\
246	sub	%g0, 1, %g4					;\
247	FAST_TRAP_DONE						;\
248	.align	32
249
250#define	DTRACE_PID			\
251	.global dtrace_pid_probe				;\
252	set	dtrace_pid_probe, %g1				;\
253	ba,pt	%xcc, user_trap					;\
254	sub	%g0, 1, %g4					;\
255	.align	32
256
257#define	DTRACE_RETURN			\
258	.global dtrace_return_probe				;\
259	set	dtrace_return_probe, %g1			;\
260	ba,pt	%xcc, user_trap					;\
261	sub	%g0, 1, %g4					;\
262	.align	32
263
264/*
265 * REGISTER WINDOW MANAGEMENT MACROS
266 */
267
268/*
269 * various convenient units of padding
270 */
271#define	SKIP(n)	.skip 4*(n)
272
273/*
274 * CLEAN_WINDOW is the simple handler for cleaning a register window.
275 */
276#define	CLEAN_WINDOW						\
277	TT_TRACE_L(trace_win)					;\
278	rdpr %cleanwin, %l0; inc %l0; wrpr %l0, %cleanwin	;\
279	clr %l0; clr %l1; clr %l2; clr %l3			;\
280	clr %l4; clr %l5; clr %l6; clr %l7			;\
281	clr %o0; clr %o1; clr %o2; clr %o3			;\
282	clr %o4; clr %o5; clr %o6; clr %o7			;\
283	retry; .align 128
284
285#if !defined(lint)
286
287/*
288 * If we get an unresolved tlb miss while in a window handler, the fault
289 * handler will resume execution at the last instruction of the window
290 * hander, instead of delivering the fault to the kernel.  Spill handlers
291 * use this to spill windows into the wbuf.
292 *
293 * The mixed handler works by checking %sp, and branching to the correct
294 * handler.  This is done by branching back to label 1: for 32b frames,
295 * or label 2: for 64b frames; which implies the handler order is: 32b,
296 * 64b, mixed.  The 1: and 2: labels are offset into the routines to
297 * allow the branchs' delay slots to contain useful instructions.
298 */
299
300/*
301 * SPILL_32bit spills a 32-bit-wide kernel register window.  It
302 * assumes that the kernel context and the nucleus context are the
303 * same.  The stack pointer is required to be eight-byte aligned even
304 * though this code only needs it to be four-byte aligned.
305 */
306#define	SPILL_32bit(tail)					\
307	srl	%sp, 0, %sp					;\
3081:	st	%l0, [%sp + 0]					;\
309	st	%l1, [%sp + 4]					;\
310	st	%l2, [%sp + 8]					;\
311	st	%l3, [%sp + 12]					;\
312	st	%l4, [%sp + 16]					;\
313	st	%l5, [%sp + 20]					;\
314	st	%l6, [%sp + 24]					;\
315	st	%l7, [%sp + 28]					;\
316	st	%i0, [%sp + 32]					;\
317	st	%i1, [%sp + 36]					;\
318	st	%i2, [%sp + 40]					;\
319	st	%i3, [%sp + 44]					;\
320	st	%i4, [%sp + 48]					;\
321	st	%i5, [%sp + 52]					;\
322	st	%i6, [%sp + 56]					;\
323	st	%i7, [%sp + 60]					;\
324	TT_TRACE_L(trace_win)					;\
325	saved							;\
326	retry							;\
327	SKIP(31-19-TT_TRACE_L_INS)				;\
328	ba,a,pt	%xcc, fault_32bit_/**/tail			;\
329	.empty
330
331/*
332 * SPILL_32bit_asi spills a 32-bit-wide register window into a 32-bit
333 * wide address space via the designated asi.  It is used to spill
334 * non-kernel windows.  The stack pointer is required to be eight-byte
335 * aligned even though this code only needs it to be four-byte
336 * aligned.
337 */
338#define	SPILL_32bit_asi(asi_num, tail)				\
339	srl	%sp, 0, %sp					;\
3401:	sta	%l0, [%sp + %g0]asi_num				;\
341	mov	4, %g1						;\
342	sta	%l1, [%sp + %g1]asi_num				;\
343	mov	8, %g2						;\
344	sta	%l2, [%sp + %g2]asi_num				;\
345	mov	12, %g3						;\
346	sta	%l3, [%sp + %g3]asi_num				;\
347	add	%sp, 16, %g4					;\
348	sta	%l4, [%g4 + %g0]asi_num				;\
349	sta	%l5, [%g4 + %g1]asi_num				;\
350	sta	%l6, [%g4 + %g2]asi_num				;\
351	sta	%l7, [%g4 + %g3]asi_num				;\
352	add	%g4, 16, %g4					;\
353	sta	%i0, [%g4 + %g0]asi_num				;\
354	sta	%i1, [%g4 + %g1]asi_num				;\
355	sta	%i2, [%g4 + %g2]asi_num				;\
356	sta	%i3, [%g4 + %g3]asi_num				;\
357	add	%g4, 16, %g4					;\
358	sta	%i4, [%g4 + %g0]asi_num				;\
359	sta	%i5, [%g4 + %g1]asi_num				;\
360	sta	%i6, [%g4 + %g2]asi_num				;\
361	sta	%i7, [%g4 + %g3]asi_num				;\
362	TT_TRACE_L(trace_win)					;\
363	saved							;\
364	retry							;\
365	SKIP(31-25-TT_TRACE_L_INS)				;\
366	ba,a,pt %xcc, fault_32bit_/**/tail			;\
367	.empty
368
369#define	SPILL_32bit_tt1(asi_num, tail)				\
370	ba,a,pt	%xcc, fault_32bit_/**/tail			;\
371	.empty							;\
372	.align 128
373
374
375/*
376 * FILL_32bit fills a 32-bit-wide kernel register window.  It assumes
377 * that the kernel context and the nucleus context are the same.  The
378 * stack pointer is required to be eight-byte aligned even though this
379 * code only needs it to be four-byte aligned.
380 */
381#define	FILL_32bit(tail)					\
382	srl	%sp, 0, %sp					;\
3831:	TT_TRACE_L(trace_win)					;\
384	ld	[%sp + 0], %l0					;\
385	ld	[%sp + 4], %l1					;\
386	ld	[%sp + 8], %l2					;\
387	ld	[%sp + 12], %l3					;\
388	ld	[%sp + 16], %l4					;\
389	ld	[%sp + 20], %l5					;\
390	ld	[%sp + 24], %l6					;\
391	ld	[%sp + 28], %l7					;\
392	ld	[%sp + 32], %i0					;\
393	ld	[%sp + 36], %i1					;\
394	ld	[%sp + 40], %i2					;\
395	ld	[%sp + 44], %i3					;\
396	ld	[%sp + 48], %i4					;\
397	ld	[%sp + 52], %i5					;\
398	ld	[%sp + 56], %i6					;\
399	ld	[%sp + 60], %i7					;\
400	restored						;\
401	retry							;\
402	SKIP(31-19-TT_TRACE_L_INS)				;\
403	ba,a,pt	%xcc, fault_32bit_/**/tail			;\
404	.empty
405
406/*
407 * FILL_32bit_asi fills a 32-bit-wide register window from a 32-bit
408 * wide address space via the designated asi.  It is used to fill
409 * non-kernel windows.  The stack pointer is required to be eight-byte
410 * aligned even though this code only needs it to be four-byte
411 * aligned.
412 */
413#define	FILL_32bit_asi(asi_num, tail)				\
414	srl	%sp, 0, %sp					;\
4151:	TT_TRACE_L(trace_win)					;\
416	mov	4, %g1						;\
417	lda	[%sp + %g0]asi_num, %l0				;\
418	mov	8, %g2						;\
419	lda	[%sp + %g1]asi_num, %l1				;\
420	mov	12, %g3						;\
421	lda	[%sp + %g2]asi_num, %l2				;\
422	lda	[%sp + %g3]asi_num, %l3				;\
423	add	%sp, 16, %g4					;\
424	lda	[%g4 + %g0]asi_num, %l4				;\
425	lda	[%g4 + %g1]asi_num, %l5				;\
426	lda	[%g4 + %g2]asi_num, %l6				;\
427	lda	[%g4 + %g3]asi_num, %l7				;\
428	add	%g4, 16, %g4					;\
429	lda	[%g4 + %g0]asi_num, %i0				;\
430	lda	[%g4 + %g1]asi_num, %i1				;\
431	lda	[%g4 + %g2]asi_num, %i2				;\
432	lda	[%g4 + %g3]asi_num, %i3				;\
433	add	%g4, 16, %g4					;\
434	lda	[%g4 + %g0]asi_num, %i4				;\
435	lda	[%g4 + %g1]asi_num, %i5				;\
436	lda	[%g4 + %g2]asi_num, %i6				;\
437	lda	[%g4 + %g3]asi_num, %i7				;\
438	restored						;\
439	retry							;\
440	SKIP(31-25-TT_TRACE_L_INS)				;\
441	ba,a,pt %xcc, fault_32bit_/**/tail			;\
442	.empty
443
444
445/*
446 * SPILL_64bit spills a 64-bit-wide kernel register window.  It
447 * assumes that the kernel context and the nucleus context are the
448 * same.  The stack pointer is required to be eight-byte aligned.
449 */
450#define	SPILL_64bit(tail)					\
4512:	stx	%l0, [%sp + V9BIAS64 + 0]			;\
452	stx	%l1, [%sp + V9BIAS64 + 8]			;\
453	stx	%l2, [%sp + V9BIAS64 + 16]			;\
454	stx	%l3, [%sp + V9BIAS64 + 24]			;\
455	stx	%l4, [%sp + V9BIAS64 + 32]			;\
456	stx	%l5, [%sp + V9BIAS64 + 40]			;\
457	stx	%l6, [%sp + V9BIAS64 + 48]			;\
458	stx	%l7, [%sp + V9BIAS64 + 56]			;\
459	stx	%i0, [%sp + V9BIAS64 + 64]			;\
460	stx	%i1, [%sp + V9BIAS64 + 72]			;\
461	stx	%i2, [%sp + V9BIAS64 + 80]			;\
462	stx	%i3, [%sp + V9BIAS64 + 88]			;\
463	stx	%i4, [%sp + V9BIAS64 + 96]			;\
464	stx	%i5, [%sp + V9BIAS64 + 104]			;\
465	stx	%i6, [%sp + V9BIAS64 + 112]			;\
466	stx	%i7, [%sp + V9BIAS64 + 120]			;\
467	TT_TRACE_L(trace_win)					;\
468	saved							;\
469	retry							;\
470	SKIP(31-18-TT_TRACE_L_INS)				;\
471	ba,a,pt	%xcc, fault_64bit_/**/tail			;\
472	.empty
473
474#define	SPILL_64bit_ktt1(tail)				\
475	ba,a,pt	%xcc, fault_64bit_/**/tail			;\
476	.empty							;\
477	.align 128
478
479#define	SPILL_mixed_ktt1(tail)				\
480	btst	1, %sp						;\
481	bz,a,pt	%xcc, fault_32bit_/**/tail			;\
482	srl	%sp, 0, %sp					;\
483	ba,a,pt	%xcc, fault_64bit_/**/tail			;\
484	.empty							;\
485	.align 128
486
487/*
488 * SPILL_64bit_asi spills a 64-bit-wide register window into a 64-bit
489 * wide address space via the designated asi.  It is used to spill
490 * non-kernel windows.  The stack pointer is required to be eight-byte
491 * aligned.
492 */
493#define	SPILL_64bit_asi(asi_num, tail)				\
494	mov	0 + V9BIAS64, %g1				;\
4952:	stxa	%l0, [%sp + %g1]asi_num				;\
496	mov	8 + V9BIAS64, %g2				;\
497	stxa	%l1, [%sp + %g2]asi_num				;\
498	mov	16 + V9BIAS64, %g3				;\
499	stxa	%l2, [%sp + %g3]asi_num				;\
500	mov	24 + V9BIAS64, %g4				;\
501	stxa	%l3, [%sp + %g4]asi_num				;\
502	add	%sp, 32, %g5					;\
503	stxa	%l4, [%g5 + %g1]asi_num				;\
504	stxa	%l5, [%g5 + %g2]asi_num				;\
505	stxa	%l6, [%g5 + %g3]asi_num				;\
506	stxa	%l7, [%g5 + %g4]asi_num				;\
507	add	%g5, 32, %g5					;\
508	stxa	%i0, [%g5 + %g1]asi_num				;\
509	stxa	%i1, [%g5 + %g2]asi_num				;\
510	stxa	%i2, [%g5 + %g3]asi_num				;\
511	stxa	%i3, [%g5 + %g4]asi_num				;\
512	add	%g5, 32, %g5					;\
513	stxa	%i4, [%g5 + %g1]asi_num				;\
514	stxa	%i5, [%g5 + %g2]asi_num				;\
515	stxa	%i6, [%g5 + %g3]asi_num				;\
516	stxa	%i7, [%g5 + %g4]asi_num				;\
517	TT_TRACE_L(trace_win)					;\
518	saved							;\
519	retry							;\
520	SKIP(31-25-TT_TRACE_L_INS)				;\
521	ba,a,pt %xcc, fault_64bit_/**/tail			;\
522	.empty
523
524#define	SPILL_64bit_tt1(asi_num, tail)				\
525	ba,a,pt	%xcc, fault_64bit_/**/tail			;\
526	.empty							;\
527	.align 128
528
529/*
530 * FILL_64bit fills a 64-bit-wide kernel register window.  It assumes
531 * that the kernel context and the nucleus context are the same.  The
532 * stack pointer is required to be eight-byte aligned.
533 */
534#define	FILL_64bit(tail)					\
5352:	TT_TRACE_L(trace_win)					;\
536	ldx	[%sp + V9BIAS64 + 0], %l0			;\
537	ldx	[%sp + V9BIAS64 + 8], %l1			;\
538	ldx	[%sp + V9BIAS64 + 16], %l2			;\
539	ldx	[%sp + V9BIAS64 + 24], %l3			;\
540	ldx	[%sp + V9BIAS64 + 32], %l4			;\
541	ldx	[%sp + V9BIAS64 + 40], %l5			;\
542	ldx	[%sp + V9BIAS64 + 48], %l6			;\
543	ldx	[%sp + V9BIAS64 + 56], %l7			;\
544	ldx	[%sp + V9BIAS64 + 64], %i0			;\
545	ldx	[%sp + V9BIAS64 + 72], %i1			;\
546	ldx	[%sp + V9BIAS64 + 80], %i2			;\
547	ldx	[%sp + V9BIAS64 + 88], %i3			;\
548	ldx	[%sp + V9BIAS64 + 96], %i4			;\
549	ldx	[%sp + V9BIAS64 + 104], %i5			;\
550	ldx	[%sp + V9BIAS64 + 112], %i6			;\
551	ldx	[%sp + V9BIAS64 + 120], %i7			;\
552	restored						;\
553	retry							;\
554	SKIP(31-18-TT_TRACE_L_INS)				;\
555	ba,a,pt	%xcc, fault_64bit_/**/tail			;\
556	.empty
557
558/*
559 * FILL_64bit_asi fills a 64-bit-wide register window from a 64-bit
560 * wide address space via the designated asi.  It is used to fill
561 * non-kernel windows.  The stack pointer is required to be eight-byte
562 * aligned.
563 */
564#define	FILL_64bit_asi(asi_num, tail)				\
565	mov	V9BIAS64 + 0, %g1				;\
5662:	TT_TRACE_L(trace_win)					;\
567	ldxa	[%sp + %g1]asi_num, %l0				;\
568	mov	V9BIAS64 + 8, %g2				;\
569	ldxa	[%sp + %g2]asi_num, %l1				;\
570	mov	V9BIAS64 + 16, %g3				;\
571	ldxa	[%sp + %g3]asi_num, %l2				;\
572	mov	V9BIAS64 + 24, %g4				;\
573	ldxa	[%sp + %g4]asi_num, %l3				;\
574	add	%sp, 32, %g5					;\
575	ldxa	[%g5 + %g1]asi_num, %l4				;\
576	ldxa	[%g5 + %g2]asi_num, %l5				;\
577	ldxa	[%g5 + %g3]asi_num, %l6				;\
578	ldxa	[%g5 + %g4]asi_num, %l7				;\
579	add	%g5, 32, %g5					;\
580	ldxa	[%g5 + %g1]asi_num, %i0				;\
581	ldxa	[%g5 + %g2]asi_num, %i1				;\
582	ldxa	[%g5 + %g3]asi_num, %i2				;\
583	ldxa	[%g5 + %g4]asi_num, %i3				;\
584	add	%g5, 32, %g5					;\
585	ldxa	[%g5 + %g1]asi_num, %i4				;\
586	ldxa	[%g5 + %g2]asi_num, %i5				;\
587	ldxa	[%g5 + %g3]asi_num, %i6				;\
588	ldxa	[%g5 + %g4]asi_num, %i7				;\
589	restored						;\
590	retry							;\
591	SKIP(31-25-TT_TRACE_L_INS)				;\
592	ba,a,pt	%xcc, fault_64bit_/**/tail			;\
593	.empty
594
595
596#endif /* !lint */
597
598/*
599 * SPILL_mixed spills either size window, depending on
600 * whether %sp is even or odd, to a 32-bit address space.
601 * This may only be used in conjunction with SPILL_32bit/
602 * FILL_64bit.
603 * Clear upper 32 bits of %sp if it is odd.
604 * We won't need to clear them in 64 bit kernel.
605 */
606#define	SPILL_mixed						\
607	btst	1, %sp						;\
608	bz,a,pt	%xcc, 1b					;\
609	srl	%sp, 0, %sp					;\
610	ba,pt	%xcc, 2b					;\
611	nop							;\
612	.align	128
613
614/*
615 * FILL_mixed(ASI) fills either size window, depending on
616 * whether %sp is even or odd, from a 32-bit address space.
617 * This may only be used in conjunction with FILL_32bit/
618 * FILL_64bit. New versions of FILL_mixed_{tt1,asi} would be
619 * needed for use with FILL_{32,64}bit_{tt1,asi}. Particular
620 * attention should be paid to the instructions that belong
621 * in the delay slots of the branches depending on the type
622 * of fill handler being branched to.
623 * Clear upper 32 bits of %sp if it is odd.
624 * We won't need to clear them in 64 bit kernel.
625 */
626#define	FILL_mixed						\
627	btst	1, %sp						;\
628	bz,a,pt	%xcc, 1b					;\
629	srl	%sp, 0, %sp					;\
630	ba,pt	%xcc, 2b					;\
631	nop							;\
632	.align	128
633
634
635/*
636 * SPILL_32clean/SPILL_64clean spill 32-bit and 64-bit register windows,
637 * respectively, into the address space via the designated asi.  The
638 * unbiased stack pointer is required to be eight-byte aligned (even for
639 * the 32-bit case even though this code does not require such strict
640 * alignment).
641 *
642 * With SPARC v9 the spill trap takes precedence over the cleanwin trap
643 * so when cansave == 0, canrestore == 6, and cleanwin == 6 the next save
644 * will cause cwp + 2 to be spilled but will not clean cwp + 1.  That
645 * window may contain kernel data so in user_rtt we set wstate to call
646 * these spill handlers on the first user spill trap.  These handler then
647 * spill the appropriate window but also back up a window and clean the
648 * window that didn't get a cleanwin trap.
649 */
650#define	SPILL_32clean(asi_num, tail)				\
651	srl	%sp, 0, %sp					;\
652	sta	%l0, [%sp + %g0]asi_num				;\
653	mov	4, %g1						;\
654	sta	%l1, [%sp + %g1]asi_num				;\
655	mov	8, %g2						;\
656	sta	%l2, [%sp + %g2]asi_num				;\
657	mov	12, %g3						;\
658	sta	%l3, [%sp + %g3]asi_num				;\
659	add	%sp, 16, %g4					;\
660	sta	%l4, [%g4 + %g0]asi_num				;\
661	sta	%l5, [%g4 + %g1]asi_num				;\
662	sta	%l6, [%g4 + %g2]asi_num				;\
663	sta	%l7, [%g4 + %g3]asi_num				;\
664	add	%g4, 16, %g4					;\
665	sta	%i0, [%g4 + %g0]asi_num				;\
666	sta	%i1, [%g4 + %g1]asi_num				;\
667	sta	%i2, [%g4 + %g2]asi_num				;\
668	sta	%i3, [%g4 + %g3]asi_num				;\
669	add	%g4, 16, %g4					;\
670	sta	%i4, [%g4 + %g0]asi_num				;\
671	sta	%i5, [%g4 + %g1]asi_num				;\
672	sta	%i6, [%g4 + %g2]asi_num				;\
673	sta	%i7, [%g4 + %g3]asi_num				;\
674	TT_TRACE_L(trace_win)					;\
675	b	.spill_clean					;\
676	  mov	WSTATE_USER32, %g7				;\
677	SKIP(31-25-TT_TRACE_L_INS)				;\
678	ba,a,pt	%xcc, fault_32bit_/**/tail			;\
679	.empty
680
681#define	SPILL_64clean(asi_num, tail)				\
682	mov	0 + V9BIAS64, %g1				;\
683	stxa	%l0, [%sp + %g1]asi_num				;\
684	mov	8 + V9BIAS64, %g2				;\
685	stxa	%l1, [%sp + %g2]asi_num				;\
686	mov	16 + V9BIAS64, %g3				;\
687	stxa	%l2, [%sp + %g3]asi_num				;\
688	mov	24 + V9BIAS64, %g4				;\
689	stxa	%l3, [%sp + %g4]asi_num				;\
690	add	%sp, 32, %g5					;\
691	stxa	%l4, [%g5 + %g1]asi_num				;\
692	stxa	%l5, [%g5 + %g2]asi_num				;\
693	stxa	%l6, [%g5 + %g3]asi_num				;\
694	stxa	%l7, [%g5 + %g4]asi_num				;\
695	add	%g5, 32, %g5					;\
696	stxa	%i0, [%g5 + %g1]asi_num				;\
697	stxa	%i1, [%g5 + %g2]asi_num				;\
698	stxa	%i2, [%g5 + %g3]asi_num				;\
699	stxa	%i3, [%g5 + %g4]asi_num				;\
700	add	%g5, 32, %g5					;\
701	stxa	%i4, [%g5 + %g1]asi_num				;\
702	stxa	%i5, [%g5 + %g2]asi_num				;\
703	stxa	%i6, [%g5 + %g3]asi_num				;\
704	stxa	%i7, [%g5 + %g4]asi_num				;\
705	TT_TRACE_L(trace_win)					;\
706	b	.spill_clean					;\
707	  mov	WSTATE_USER64, %g7				;\
708	SKIP(31-25-TT_TRACE_L_INS)				;\
709	ba,a,pt	%xcc, fault_64bit_/**/tail			;\
710	.empty
711
712
713/*
714 * Floating point disabled.
715 */
716#define	FP_DISABLED_TRAP		\
717	TT_TRACE(trace_gen)		;\
718	ba,pt	%xcc,.fp_disabled	;\
719	nop				;\
720	.align	32
721
722/*
723 * Floating point exceptions.
724 */
725#define	FP_IEEE_TRAP			\
726	TT_TRACE(trace_gen)		;\
727	ba,pt	%xcc,.fp_ieee_exception	;\
728	nop				;\
729	.align	32
730
731#define	FP_TRAP				\
732	TT_TRACE(trace_gen)		;\
733	ba,pt	%xcc,.fp_exception	;\
734	nop				;\
735	.align	32
736
737#if !defined(lint)
738
739/*
740 * ECACHE_ECC error traps at level 0 and level 1
741 */
742#define	ECACHE_ECC(table_name)		\
743	.global	table_name		;\
744table_name:				;\
745	membar	#Sync			;\
746	set	trap, %g1		;\
747	rdpr	%tt, %g3		;\
748	ba,pt	%xcc, sys_trap		;\
749	sub	%g0, 1, %g4		;\
750	.align	32
751
752#endif /* !lint */
753
754/*
755 * illegal instruction trap
756 */
757#define	ILLTRAP_INSTR			  \
758	membar	#Sync			  ;\
759	TT_TRACE(trace_gen)		  ;\
760	or	%g0, P_UTRAP4, %g2	  ;\
761	or	%g0, T_UNIMP_INSTR, %g3   ;\
762	sethi	%hi(.check_v9utrap), %g4  ;\
763	jmp	%g4 + %lo(.check_v9utrap) ;\
764	nop				  ;\
765	.align	32
766
767/*
768 * tag overflow trap
769 */
770#define	TAG_OVERFLOW			  \
771	TT_TRACE(trace_gen)		  ;\
772	or	%g0, P_UTRAP10, %g2	  ;\
773	or	%g0, T_TAG_OVERFLOW, %g3  ;\
774	sethi	%hi(.check_v9utrap), %g4  ;\
775	jmp	%g4 + %lo(.check_v9utrap) ;\
776	nop				  ;\
777	.align	32
778
779/*
780 * divide by zero trap
781 */
782#define	DIV_BY_ZERO			  \
783	TT_TRACE(trace_gen)		  ;\
784	or	%g0, P_UTRAP11, %g2	  ;\
785	or	%g0, T_IDIV0, %g3	  ;\
786	sethi	%hi(.check_v9utrap), %g4  ;\
787	jmp	%g4 + %lo(.check_v9utrap) ;\
788	nop				  ;\
789	.align	32
790
791/*
792 * trap instruction for V9 user trap handlers
793 */
794#define	TRAP_INSTR			  \
795	TT_TRACE(trace_gen)		  ;\
796	or	%g0, T_SOFTWARE_TRAP, %g3 ;\
797	sethi	%hi(.check_v9utrap), %g4  ;\
798	jmp	%g4 + %lo(.check_v9utrap) ;\
799	nop				  ;\
800	.align	32
801#define	TRP4	TRAP_INSTR; TRAP_INSTR; TRAP_INSTR; TRAP_INSTR
802
803/*
804 * LEVEL_INTERRUPT is for level N interrupts.
805 * VECTOR_INTERRUPT is for the vector trap.
806 */
807#define	LEVEL_INTERRUPT(level)		\
808	.global	tt_pil/**/level		;\
809tt_pil/**/level:			;\
810	ba,pt	%xcc, pil_interrupt	;\
811	mov	level, %g4		;\
812	.align	32
813
814#define	LEVEL14_INTERRUPT			\
815	ba	pil14_interrupt			;\
816	mov	PIL_14, %g4			;\
817	.align	32
818
819#define CPU_MONDO			\
820	ba,a,pt	%xcc, cpu_mondo		;\
821	.align	32
822
823#define DEV_MONDO			\
824	ba,a,pt	%xcc, dev_mondo		;\
825	.align	32
826
827/*
828 * MMU Trap Handlers.
829 */
830
831/*
832 * synthesize for trap(): SFSR in %g3
833 */
834#define	IMMU_EXCEPTION							\
835	MMU_FAULT_STATUS_AREA(%g3)					;\
836	rdpr	%tpc, %g2						;\
837	ldx	[%g3 + MMFSA_I_TYPE], %g1				;\
838	ldx	[%g3 + MMFSA_I_CTX], %g3				;\
839	sllx	%g3, SFSR_CTX_SHIFT, %g3				;\
840	or	%g3, %g1, %g3						;\
841	ba,pt	%xcc, .mmu_exception_end				;\
842	mov	T_INSTR_EXCEPTION, %g1					;\
843	.align	32
844
845/*
846 * synthesize for trap(): TAG_ACCESS in %g2, SFSR in %g3
847 */
848#define	DMMU_EXCEPTION							\
849	ba,a,pt	%xcc, .dmmu_exception					;\
850	.align	32
851
852/*
853 * synthesize for trap(): SFAR in %g2, SFSR in %g3
854 */
855#define	DMMU_EXC_AG_PRIV						\
856	MMU_FAULT_STATUS_AREA(%g3)					;\
857	ldx	[%g3 + MMFSA_D_ADDR], %g2				;\
858	/* Fault type not available in MMU fault status area */		;\
859	mov	MMFSA_F_PRVACT, %g1					;\
860	ldx	[%g3 + MMFSA_D_CTX], %g3				;\
861	sllx	%g3, SFSR_CTX_SHIFT, %g3				;\
862	ba,pt	%xcc, .mmu_priv_exception				;\
863	or	%g3, %g1, %g3						;\
864	.align	32
865
866/*
867 * synthesize for trap(): SFAR in %g2, SFSR in %g3
868 */
869#define	DMMU_EXC_AG_NOT_ALIGNED						\
870	MMU_FAULT_STATUS_AREA(%g3)					;\
871	ldx	[%g3 + MMFSA_D_ADDR], %g2				;\
872	/* Fault type not available in MMU fault status area */		;\
873	mov	MMFSA_F_UNALIGN, %g1					;\
874	ldx	[%g3 + MMFSA_D_CTX], %g3				;\
875	sllx	%g3, SFSR_CTX_SHIFT, %g3				;\
876	ba,pt	%xcc, .mmu_exception_not_aligned			;\
877	or	%g3, %g1, %g3			/* SFSR */		;\
878	.align	32
879/*
880 * SPARC V9 IMPL. DEP. #109(1) and (2) and #110(1) and (2)
881 */
882
883/*
884 * synthesize for trap(): SFAR in %g2, SFSR in %g3
885 */
886#define	DMMU_EXC_LDDF_NOT_ALIGNED					\
887	ba,a,pt	%xcc, .dmmu_exc_lddf_not_aligned			;\
888	.align	32
889/*
890 * synthesize for trap(): SFAR in %g2, SFSR in %g3
891 */
892#define	DMMU_EXC_STDF_NOT_ALIGNED					\
893	ba,a,pt	%xcc, .dmmu_exc_stdf_not_aligned			;\
894	.align	32
895
896#if TAGACC_CTX_MASK != CTXREG_CTX_MASK
897#error "TAGACC_CTX_MASK != CTXREG_CTX_MASK"
898#endif
899
900#if defined(cscope)
901/*
902 * Define labels to direct cscope quickly to labels that
903 * are generated by macro expansion of DTLB_MISS().
904 */
905	.global	tt0_dtlbmiss
906tt0_dtlbmiss:
907	.global	tt1_dtlbmiss
908tt1_dtlbmiss:
909	nop
910#endif
911
912/*
913 * Data miss handler (must be exactly 32 instructions)
914 *
915 * This handler is invoked only if the hypervisor has been instructed
916 * not to do any TSB walk.
917 *
918 * Kernel and invalid context cases are handled by the sfmmu_kdtlb_miss
919 * handler.
920 *
921 * User TLB miss handling depends upon whether a user process has one or
922 * two TSBs. User TSB information (physical base and size code) is kept
923 * in two dedicated scratchpad registers. Absence of a user TSB (primarily
924 * second TSB) is indicated by a negative value (-1) in that register.
925 */
926
927/*
928 * synthesize for miss handler: TAG_ACCESS in %g2
929 */
930#define	DTLB_MISS(table_name)						;\
931	.global	table_name/**/_dtlbmiss					;\
932table_name/**/_dtlbmiss:						;\
933	HAT_PERCPU_DBSTAT(TSBMISS_DTLBMISS) /* 3 instr ifdef DEBUG */	;\
934	MMU_FAULT_STATUS_AREA(%g7)					;\
935	ldx	[%g7 + MMFSA_D_ADDR], %g2	/* address */		;\
936	ldx	[%g7 + MMFSA_D_CTX], %g3	/* g3 = ctx */		;\
937	or	%g2, %g3, %g2			/* TAG_ACCESS */	;\
938	cmp	%g3, INVALID_CONTEXT					;\
939	ble,pn	%xcc, sfmmu_kdtlb_miss					;\
940	  srlx	%g2, TAG_VALO_SHIFT, %g7	/* g7 = tsb tag */	;\
941	mov	SCRATCHPAD_UTSBREG2, %g1				;\
942	ldxa	[%g1]ASI_SCRATCHPAD, %g1	/* get 2nd tsbreg */	;\
943	brgez,pn %g1, sfmmu_udtlb_slowpath	/* brnach if 2 TSBs */	;\
944	  nop								;\
945	GET_1ST_TSBE_PTR(%g2, %g1, %g4, %g5)	/* 11 instr */		;\
946	ba,pt	%xcc, sfmmu_udtlb_fastpath	/* no 4M TSB, miss */	;\
947	  srlx	%g2, TAG_VALO_SHIFT, %g7	/* g7 = tsb tag */	;\
948	.align 128
949
950
951#if defined(cscope)
952/*
953 * Define labels to direct cscope quickly to labels that
954 * are generated by macro expansion of ITLB_MISS().
955 */
956	.global	tt0_itlbmiss
957tt0_itlbmiss:
958	.global	tt1_itlbmiss
959tt1_itlbmiss:
960	nop
961#endif
962
963/*
964 * Instruction miss handler.
965 *
966 * This handler is invoked only if the hypervisor has been instructed
967 * not to do any TSB walk.
968 *
969 * ldda instructions will have their ASI patched
970 * by sfmmu_patch_ktsb at runtime.
971 * MUST be EXACTLY 32 instructions or we'll break.
972 */
973
974/*
975 * synthesize for miss handler: TAG_ACCESS in %g2
976 */
977#define	ITLB_MISS(table_name)						 \
978	.global	table_name/**/_itlbmiss					;\
979table_name/**/_itlbmiss:						;\
980	HAT_PERCPU_DBSTAT(TSBMISS_ITLBMISS) /* 3 instr ifdef DEBUG */	;\
981	MMU_FAULT_STATUS_AREA(%g7)					;\
982	ldx	[%g7 + MMFSA_I_ADDR], %g2	/* g2 = address */	;\
983	ldx	[%g7 + MMFSA_I_CTX], %g3	/* g3 = ctx */		;\
984	or	%g2, %g3, %g2			/* TAG_ACCESS */	;\
985	cmp	%g3, INVALID_CONTEXT					;\
986	ble,pn	%xcc, sfmmu_kitlb_miss					;\
987	  srlx	%g2, TAG_VALO_SHIFT, %g7	/* g7 = tsb tag */	;\
988	mov	SCRATCHPAD_UTSBREG2, %g1				;\
989	ldxa	[%g1]ASI_SCRATCHPAD, %g1	/* get 2nd tsbreg */	;\
990	brgez,pn %g1, sfmmu_uitlb_slowpath	/* branch if 2 TSBS */	;\
991	  nop								;\
992	GET_1ST_TSBE_PTR(%g2, %g1, %g4, %g5)	/* 11 instr */		;\
993	ba,pt	%xcc, sfmmu_uitlb_fastpath	/* no 4M TSB, miss */	;\
994	  srlx	%g2, TAG_VALO_SHIFT, %g7	/* g7 = tsb tag */	;\
995	.align 128
996
997#define	DTSB_MISS \
998	GOTO_TT(sfmmu_slow_dmmu_miss,trace_dmmu)
999
1000#define	ITSB_MISS \
1001	GOTO_TT(sfmmu_slow_immu_miss,trace_immu)
1002
1003/*
1004 * This macro is the first level handler for fast protection faults.
1005 * It first demaps the tlb entry which generated the fault and then
1006 * attempts to set the modify bit on the hash.  It needs to be
1007 * exactly 32 instructions.
1008 */
1009/*
1010 * synthesize for miss handler: TAG_ACCESS in %g2
1011 */
1012#define	DTLB_PROT							 \
1013	MMU_FAULT_STATUS_AREA(%g7)					;\
1014	ldx	[%g7 + MMFSA_D_ADDR], %g2	/* address */		;\
1015	ldx	[%g7 + MMFSA_D_CTX], %g3	/* %g3 = ctx */		;\
1016	or	%g2, %g3, %g2			/* TAG_ACCESS */	;\
1017	/*								;\
1018	 *   g2 = tag access register					;\
1019	 *   g3 = ctx number						;\
1020	 */								;\
1021	TT_TRACE(trace_dataprot)	/* 2 instr ifdef TRAPTRACE */	;\
1022					/* clobbers g1 and g6 XXXQ? */	;\
1023	brnz,pt %g3, sfmmu_uprot_trap		/* user trap */		;\
1024	  nop								;\
1025	ba,a,pt	%xcc, sfmmu_kprot_trap		/* kernel trap */	;\
1026	.align 128
1027
1028#define	DMMU_EXCEPTION_TL1						;\
1029	ba,a,pt	%xcc, mmu_trap_tl1					;\
1030	.align 32
1031
1032#define	MISALIGN_ADDR_TL1						;\
1033	ba,a,pt	%xcc, mmu_trap_tl1					;\
1034	.align 32
1035
1036/*
1037 * Trace a tsb hit
1038 * g1 = tsbe pointer (in/clobbered)
1039 * g2 = tag access register (in)
1040 * g3 - g4 = scratch (clobbered)
1041 * g5 = tsbe data (in)
1042 * g6 = scratch (clobbered)
1043 * g7 = pc we jumped here from (in)
1044 * ttextra = value to OR in to trap type (%tt) (in)
1045 */
1046#ifdef TRAPTRACE
1047#define TRACE_TSBHIT(ttextra)						 \
1048	membar	#Sync							;\
1049	sethi	%hi(FLUSH_ADDR), %g6					;\
1050	flush	%g6							;\
1051	TRACE_PTR(%g3, %g6)						;\
1052	GET_TRACE_TICK(%g6)						;\
1053	stxa	%g6, [%g3 + TRAP_ENT_TICK]%asi				;\
1054	stna	%g2, [%g3 + TRAP_ENT_SP]%asi	/* tag access */	;\
1055	stna	%g5, [%g3 + TRAP_ENT_F1]%asi	/* tsb data */		;\
1056	rdpr	%tnpc, %g6						;\
1057	stna	%g6, [%g3 + TRAP_ENT_F2]%asi				;\
1058	stna	%g1, [%g3 + TRAP_ENT_F3]%asi	/* tsb pointer */	;\
1059	stna	%g0, [%g3 + TRAP_ENT_F4]%asi				;\
1060	rdpr	%tpc, %g6						;\
1061	stna	%g6, [%g3 + TRAP_ENT_TPC]%asi				;\
1062	TRACE_SAVE_TL_GL_REGS(%g3, %g6)					;\
1063	rdpr	%tt, %g6						;\
1064	or	%g6, (ttextra), %g1					;\
1065	stha	%g1, [%g3 + TRAP_ENT_TT]%asi				;\
1066	MMU_FAULT_STATUS_AREA(%g4)					;\
1067	mov	MMFSA_D_ADDR, %g1					;\
1068	cmp	%g6, FAST_IMMU_MISS_TT					;\
1069	move	%xcc, MMFSA_I_ADDR, %g1					;\
1070	cmp	%g6, T_INSTR_MMU_MISS					;\
1071	move	%xcc, MMFSA_I_ADDR, %g1					;\
1072	ldx	[%g4 + %g1], %g1					;\
1073	stxa	%g1, [%g3 + TRAP_ENT_TSTATE]%asi /* fault addr */	;\
1074	mov	MMFSA_D_CTX, %g1					;\
1075	cmp	%g6, FAST_IMMU_MISS_TT					;\
1076	move	%xcc, MMFSA_I_CTX, %g1					;\
1077	cmp	%g6, T_INSTR_MMU_MISS					;\
1078	move	%xcc, MMFSA_I_CTX, %g1					;\
1079	ldx	[%g4 + %g1], %g1					;\
1080	stna	%g1, [%g3 + TRAP_ENT_TR]%asi				;\
1081	TRACE_NEXT(%g3, %g4, %g6)
1082#else
1083#define TRACE_TSBHIT(ttextra)
1084#endif
1085
1086
1087#if defined(lint)
1088
1089struct scb	trap_table;
1090struct scb	scb;		/* trap_table/scb are the same object */
1091
1092#else /* lint */
1093
1094/*
1095 * =======================================================================
1096 *		SPARC V9 TRAP TABLE
1097 *
1098 * The trap table is divided into two halves: the first half is used when
1099 * taking traps when TL=0; the second half is used when taking traps from
1100 * TL>0. Note that handlers in the second half of the table might not be able
1101 * to make the same assumptions as handlers in the first half of the table.
1102 *
1103 * Worst case trap nesting so far:
1104 *
1105 *	at TL=0 client issues software trap requesting service
1106 *	at TL=1 nucleus wants a register window
1107 *	at TL=2 register window clean/spill/fill takes a TLB miss
1108 *	at TL=3 processing TLB miss
1109 *	at TL=4 handle asynchronous error
1110 *
1111 * Note that a trap from TL=4 to TL=5 places Spitfire in "RED mode".
1112 *
1113 * =======================================================================
1114 */
1115	.section ".text"
1116	.align	4
1117	.global trap_table, scb, trap_table0, trap_table1, etrap_table
1118	.type	trap_table, #function
1119	.type	trap_table0, #function
1120	.type	trap_table1, #function
1121	.type	scb, #function
1122trap_table:
1123scb:
1124trap_table0:
1125	/* hardware traps */
1126	NOT;				/* 000	reserved */
1127	RED;				/* 001	power on reset */
1128	RED;				/* 002	watchdog reset */
1129	RED;				/* 003	externally initiated reset */
1130	RED;				/* 004	software initiated reset */
1131	RED;				/* 005	red mode exception */
1132	NOT; NOT;			/* 006 - 007 reserved */
1133	IMMU_EXCEPTION;			/* 008	instruction access exception */
1134	ITSB_MISS;			/* 009	instruction access MMU miss */
1135 	NOT;				/* 00A  reserved */
1136	NOT; NOT4;			/* 00B - 00F reserved */
1137	ILLTRAP_INSTR;			/* 010	illegal instruction */
1138	TRAP(T_PRIV_INSTR);		/* 011	privileged opcode */
1139	TRAP(T_UNIMP_LDD);		/* 012	unimplemented LDD */
1140	TRAP(T_UNIMP_STD);		/* 013	unimplemented STD */
1141	NOT4; NOT4; NOT4;		/* 014 - 01F reserved */
1142	FP_DISABLED_TRAP;		/* 020	fp disabled */
1143	FP_IEEE_TRAP;			/* 021	fp exception ieee 754 */
1144	FP_TRAP;			/* 022	fp exception other */
1145	TAG_OVERFLOW;			/* 023	tag overflow */
1146	CLEAN_WINDOW;			/* 024 - 027 clean window */
1147	DIV_BY_ZERO;			/* 028	division by zero */
1148	NOT;				/* 029	internal processor error */
1149	NOT; NOT; NOT4;			/* 02A - 02F reserved */
1150	DMMU_EXCEPTION;			/* 030	data access exception */
1151	DTSB_MISS;			/* 031	data access MMU miss */
1152	NOT;				/* 032  reserved */
1153	NOT;				/* 033	data access protection */
1154	DMMU_EXC_AG_NOT_ALIGNED;	/* 034	mem address not aligned */
1155	DMMU_EXC_LDDF_NOT_ALIGNED;	/* 035	LDDF mem address not aligned */
1156	DMMU_EXC_STDF_NOT_ALIGNED;	/* 036	STDF mem address not aligned */
1157	DMMU_EXC_AG_PRIV;		/* 037	privileged action */
1158	NOT;				/* 038	LDQF mem address not aligned */
1159	NOT;				/* 039	STQF mem address not aligned */
1160	NOT; NOT; NOT4;			/* 03A - 03F reserved */
1161	NOT;				/* 040	async data error */
1162	LEVEL_INTERRUPT(1);		/* 041	interrupt level 1 */
1163	LEVEL_INTERRUPT(2);		/* 042	interrupt level 2 */
1164	LEVEL_INTERRUPT(3);		/* 043	interrupt level 3 */
1165	LEVEL_INTERRUPT(4);		/* 044	interrupt level 4 */
1166	LEVEL_INTERRUPT(5);		/* 045	interrupt level 5 */
1167	LEVEL_INTERRUPT(6);		/* 046	interrupt level 6 */
1168	LEVEL_INTERRUPT(7);		/* 047	interrupt level 7 */
1169	LEVEL_INTERRUPT(8);		/* 048	interrupt level 8 */
1170	LEVEL_INTERRUPT(9);		/* 049	interrupt level 9 */
1171	LEVEL_INTERRUPT(10);		/* 04A	interrupt level 10 */
1172	LEVEL_INTERRUPT(11);		/* 04B	interrupt level 11 */
1173	LEVEL_INTERRUPT(12);		/* 04C	interrupt level 12 */
1174	LEVEL_INTERRUPT(13);		/* 04D	interrupt level 13 */
1175	LEVEL14_INTERRUPT;		/* 04E	interrupt level 14 */
1176	LEVEL_INTERRUPT(15);		/* 04F	interrupt level 15 */
1177	NOT4; NOT4; NOT4; NOT4;		/* 050 - 05F reserved */
1178	NOT;				/* 060	interrupt vector */
1179	GOTO(kmdb_trap);		/* 061	PA watchpoint */
1180	GOTO(kmdb_trap);		/* 062	VA watchpoint */
1181	NOT;				/* 063	reserved */
1182	ITLB_MISS(tt0);			/* 064	instruction access MMU miss */
1183	DTLB_MISS(tt0);			/* 068	data access MMU miss */
1184	DTLB_PROT;			/* 06C	data access protection */
1185	NOT;				/* 070  reserved */
1186	NOT;				/* 071  reserved */
1187	NOT;				/* 072  reserved */
1188	NOT;				/* 073  reserved */
1189	NOT4; NOT4			/* 074 - 07B reserved */
1190	CPU_MONDO;			/* 07C	cpu_mondo */
1191	DEV_MONDO;			/* 07D	dev_mondo */
1192	GOTO_TT(resumable_error, trace_gen);	/* 07E  resumable error */
1193	GOTO_TT(nonresumable_error, trace_gen);	/* 07F  non-reasumable error */
1194	NOT4;				/* 080	spill 0 normal */
1195	SPILL_32bit_asi(ASI_AIUP,sn0);	/* 084	spill 1 normal */
1196	SPILL_64bit_asi(ASI_AIUP,sn0);	/* 088	spill 2 normal */
1197	SPILL_32clean(ASI_AIUP,sn0);	/* 08C	spill 3 normal */
1198	SPILL_64clean(ASI_AIUP,sn0);	/* 090	spill 4 normal */
1199	SPILL_32bit(not);		/* 094	spill 5 normal */
1200	SPILL_64bit(not);		/* 098	spill 6 normal */
1201	SPILL_mixed;			/* 09C	spill 7 normal */
1202	NOT4;				/* 0A0	spill 0 other */
1203	SPILL_32bit_asi(ASI_AIUS,so0);	/* 0A4	spill 1 other */
1204	SPILL_64bit_asi(ASI_AIUS,so0);	/* 0A8	spill 2 other */
1205	SPILL_32bit_asi(ASI_AIUS,so0);	/* 0AC	spill 3 other */
1206	SPILL_64bit_asi(ASI_AIUS,so0);	/* 0B0	spill 4 other */
1207	NOT4;				/* 0B4	spill 5 other */
1208	NOT4;				/* 0B8	spill 6 other */
1209	NOT4;				/* 0BC	spill 7 other */
1210	NOT4;				/* 0C0	fill 0 normal */
1211	FILL_32bit_asi(ASI_AIUP,fn0);	/* 0C4	fill 1 normal */
1212	FILL_64bit_asi(ASI_AIUP,fn0);	/* 0C8	fill 2 normal */
1213	FILL_32bit_asi(ASI_AIUP,fn0);	/* 0CC	fill 3 normal */
1214	FILL_64bit_asi(ASI_AIUP,fn0);	/* 0D0	fill 4 normal */
1215	FILL_32bit(not);		/* 0D4	fill 5 normal */
1216	FILL_64bit(not);		/* 0D8	fill 6 normal */
1217	FILL_mixed;			/* 0DC	fill 7 normal */
1218	NOT4;				/* 0E0	fill 0 other */
1219	NOT4;				/* 0E4	fill 1 other */
1220	NOT4;				/* 0E8	fill 2 other */
1221	NOT4;				/* 0EC	fill 3 other */
1222	NOT4;				/* 0F0	fill 4 other */
1223	NOT4;				/* 0F4	fill 5 other */
1224	NOT4;				/* 0F8	fill 6 other */
1225	NOT4;				/* 0FC	fill 7 other */
1226	/* user traps */
1227	GOTO(syscall_trap_4x);		/* 100	old system call */
1228	TRAP(T_BREAKPOINT);		/* 101	user breakpoint */
1229	TRAP(T_DIV0);			/* 102	user divide by zero */
1230	GOTO(.flushw);			/* 103	flush windows */
1231	GOTO(.clean_windows);		/* 104	clean windows */
1232	BAD;				/* 105	range check ?? */
1233	GOTO(.fix_alignment);		/* 106	do unaligned references */
1234	BAD;				/* 107	unused */
1235	SYSCALL(syscall_trap32);	/* 108	ILP32 system call on LP64 */
1236	GOTO(set_trap0_addr);		/* 109	set trap0 address */
1237	BAD; BAD; BAD4;			/* 10A - 10F unused */
1238	TRP4; TRP4; TRP4; TRP4;		/* 110 - 11F V9 user trap handlers */
1239	GOTO(.getcc);			/* 120	get condition codes */
1240	GOTO(.setcc);			/* 121	set condition codes */
1241	GOTO(.getpsr);			/* 122	get psr */
1242	GOTO(.setpsr);			/* 123	set psr (some fields) */
1243	GOTO(get_timestamp);		/* 124	get timestamp */
1244	GOTO(get_virtime);		/* 125	get lwp virtual time */
1245	PRIV(self_xcall);		/* 126	self xcall */
1246	GOTO(get_hrestime);		/* 127	get hrestime */
1247	BAD;				/* 128	ST_SETV9STACK */
1248	GOTO(.getlgrp);			/* 129  get lgrpid */
1249	BAD; BAD; BAD4;			/* 12A - 12F unused */
1250	BAD4; BAD4; 			/* 130 - 137 unused */
1251	DTRACE_PID;			/* 138  dtrace pid tracing provider */
1252	DTRACE_FASTTRAP;		/* 139  dtrace fasttrap provider */
1253	DTRACE_RETURN;			/* 13A	dtrace pid return probe */
1254	BAD; BAD4;			/* 13B - 13F unused */
1255	SYSCALL(syscall_trap)		/* 140  LP64 system call */
1256	BAD;				/* 141  unused */
1257#ifdef DEBUG_USER_TRAPTRACECTL
1258	GOTO(.traptrace_freeze);	/* 142  freeze traptrace */
1259	GOTO(.traptrace_unfreeze);	/* 143  unfreeze traptrace */
1260#else
1261	BAD; BAD;			/* 142 - 143 unused */
1262#endif
1263	BAD4; BAD4; BAD4;		/* 144 - 14F unused */
1264	BAD4; BAD4; BAD4; BAD4;		/* 150 - 15F unused */
1265	BAD4; BAD4; BAD4; BAD4;		/* 160 - 16F unused */
1266	BAD;				/* 170 - unused */
1267	BAD;				/* 171 - unused */
1268	BAD; BAD;			/* 172 - 173 unused */
1269	BAD4; BAD4;			/* 174 - 17B unused */
1270#ifdef	PTL1_PANIC_DEBUG
1271	mov PTL1_BAD_DEBUG, %g1; GOTO(ptl1_panic);
1272					/* 17C	test ptl1_panic */
1273#else
1274	BAD;				/* 17C  unused */
1275#endif	/* PTL1_PANIC_DEBUG */
1276	PRIV(kmdb_trap);		/* 17D	kmdb enter (L1-A) */
1277	PRIV(kmdb_trap);		/* 17E	kmdb breakpoint */
1278	PRIV(obp_bpt);			/* 17F	obp breakpoint */
1279	/* reserved */
1280	NOT4; NOT4; NOT4; NOT4;		/* 180 - 18F reserved */
1281	NOT4; NOT4; NOT4; NOT4;		/* 190 - 19F reserved */
1282	NOT4; NOT4; NOT4; NOT4;		/* 1A0 - 1AF reserved */
1283	NOT4; NOT4; NOT4; NOT4;		/* 1B0 - 1BF reserved */
1284	NOT4; NOT4; NOT4; NOT4;		/* 1C0 - 1CF reserved */
1285	NOT4; NOT4; NOT4; NOT4;		/* 1D0 - 1DF reserved */
1286	NOT4; NOT4; NOT4; NOT4;		/* 1E0 - 1EF reserved */
1287	NOT4; NOT4; NOT4; NOT4;		/* 1F0 - 1FF reserved */
1288	.size	trap_table0, (.-trap_table0)
1289trap_table1:
1290	NOT4; NOT4;			/* 000 - 007 unused */
1291	NOT;				/* 008	instruction access exception */
1292	ITSB_MISS;			/* 009	instruction access MMU miss */
1293 	NOT;				/* 00A  reserved */
1294	NOT; NOT4;			/* 00B - 00F unused */
1295	NOT4; NOT4; NOT4; NOT4;		/* 010 - 01F unused */
1296	NOT4;				/* 020 - 023 unused */
1297	CLEAN_WINDOW;			/* 024 - 027 clean window */
1298	NOT4; NOT4;			/* 028 - 02F unused */
1299	DMMU_EXCEPTION_TL1;		/* 030 	data access exception */
1300	DTSB_MISS;			/* 031  data access MMU miss */
1301	NOT;				/* 032  reserved */
1302	NOT;				/* 033	unused */
1303	MISALIGN_ADDR_TL1;		/* 034	mem address not aligned */
1304	NOT; NOT; NOT; NOT4; NOT4	/* 035 - 03F unused */
1305	NOT4; NOT4; NOT4; NOT4;		/* 040 - 04F unused */
1306	NOT4; NOT4; NOT4; NOT4;		/* 050 - 05F unused */
1307	NOT;				/* 060	unused */
1308	GOTO(kmdb_trap_tl1);		/* 061	PA watchpoint */
1309	GOTO(kmdb_trap_tl1);		/* 062	VA watchpoint */
1310	NOT;				/* 063	reserved */
1311	ITLB_MISS(tt1);			/* 064	instruction access MMU miss */
1312	DTLB_MISS(tt1);			/* 068	data access MMU miss */
1313	DTLB_PROT;			/* 06C	data access protection */
1314	NOT;				/* 070  reserved */
1315	NOT;				/* 071  reserved */
1316	NOT;				/* 072  reserved */
1317	NOT;				/* 073  reserved */
1318	NOT4; NOT4; NOT4;		/* 074 - 07F reserved */
1319	NOTP4;				/* 080	spill 0 normal */
1320	SPILL_32bit_tt1(ASI_AIUP,sn1);	/* 084	spill 1 normal */
1321	SPILL_64bit_tt1(ASI_AIUP,sn1);	/* 088	spill 2 normal */
1322	SPILL_32bit_tt1(ASI_AIUP,sn1);	/* 08C	spill 3 normal */
1323	SPILL_64bit_tt1(ASI_AIUP,sn1);	/* 090	spill 4 normal */
1324	NOTP4;				/* 094	spill 5 normal */
1325	SPILL_64bit_ktt1(sk);		/* 098	spill 6 normal */
1326	SPILL_mixed_ktt1(sk);		/* 09C	spill 7 normal */
1327	NOTP4;				/* 0A0	spill 0 other */
1328	SPILL_32bit_tt1(ASI_AIUS,so1);	/* 0A4  spill 1 other */
1329	SPILL_64bit_tt1(ASI_AIUS,so1);	/* 0A8	spill 2 other */
1330	SPILL_32bit_tt1(ASI_AIUS,so1);	/* 0AC	spill 3 other */
1331	SPILL_64bit_tt1(ASI_AIUS,so1);	/* 0B0  spill 4 other */
1332	NOTP4;				/* 0B4  spill 5 other */
1333	NOTP4;				/* 0B8  spill 6 other */
1334	NOTP4;				/* 0BC  spill 7 other */
1335	NOT4;				/* 0C0	fill 0 normal */
1336	NOT4;				/* 0C4	fill 1 normal */
1337	NOT4;				/* 0C8	fill 2 normal */
1338	NOT4;				/* 0CC	fill 3 normal */
1339	NOT4;				/* 0D0	fill 4 normal */
1340	NOT4;				/* 0D4	fill 5 normal */
1341	NOT4;				/* 0D8	fill 6 normal */
1342	NOT4;				/* 0DC	fill 7 normal */
1343	NOT4; NOT4; NOT4; NOT4;		/* 0E0 - 0EF unused */
1344	NOT4; NOT4; NOT4; NOT4;		/* 0F0 - 0FF unused */
1345/*
1346 * Code running at TL>0 does not use soft traps, so
1347 * we can truncate the table here.
1348 * However:
1349 * sun4v uses (hypervisor) ta instructions at TL > 0, so
1350 * provide a safety net for now.
1351 */
1352	/* soft traps */
1353	BAD4; BAD4; BAD4; BAD4;		/* 100 - 10F unused */
1354	BAD4; BAD4; BAD4; BAD4;		/* 110 - 11F unused */
1355	BAD4; BAD4; BAD4; BAD4;		/* 120 - 12F unused */
1356	BAD4; BAD4; BAD4; BAD4;		/* 130 - 13F unused */
1357	BAD4; BAD4; BAD4; BAD4;		/* 140 - 14F unused */
1358	BAD4; BAD4; BAD4; BAD4;		/* 150 - 15F unused */
1359	BAD4; BAD4; BAD4; BAD4;		/* 160 - 16F unused */
1360	BAD4; BAD4; BAD4; BAD4;		/* 170 - 17F unused */
1361	/* reserved */
1362	NOT4; NOT4; NOT4; NOT4;		/* 180 - 18F reserved */
1363	NOT4; NOT4; NOT4; NOT4;		/* 190 - 19F reserved */
1364	NOT4; NOT4; NOT4; NOT4;		/* 1A0 - 1AF reserved */
1365	NOT4; NOT4; NOT4; NOT4;		/* 1B0 - 1BF reserved */
1366	NOT4; NOT4; NOT4; NOT4;		/* 1C0 - 1CF reserved */
1367	NOT4; NOT4; NOT4; NOT4;		/* 1D0 - 1DF reserved */
1368	NOT4; NOT4; NOT4; NOT4;		/* 1E0 - 1EF reserved */
1369	NOT4; NOT4; NOT4; NOT4;		/* 1F0 - 1FF reserved */
1370etrap_table:
1371	.size	trap_table1, (.-trap_table1)
1372	.size	trap_table, (.-trap_table)
1373	.size	scb, (.-scb)
1374
1375/*
1376 * We get to exec_fault in the case of an instruction miss and tte
1377 * has no execute bit set.  We go to tl0 to handle it.
1378 *
1379 * g1 = tsbe pointer (in/clobbered)
1380 * g2 = tag access register (in)
1381 * g3 - g4 = scratch (clobbered)
1382 * g5 = tsbe data (in)
1383 * g6 = scratch (clobbered)
1384 * g7 = pc we jumped here from (in)
1385 */
1386/*
1387 * synthesize for trap(): TAG_ACCESS in %g2
1388 */
1389	ALTENTRY(exec_fault)
1390	TRACE_TSBHIT(TT_MMU_EXEC)
1391	MMU_FAULT_STATUS_AREA(%g4)
1392	ldx	[%g4 + MMFSA_I_ADDR], %g2	/* g2 = address */
1393	ldx	[%g4 + MMFSA_I_CTX], %g3	/* g3 = ctx */
1394	srlx	%g2, MMU_PAGESHIFT, %g2		! align address to page boundry
1395	sllx	%g2, MMU_PAGESHIFT, %g2
1396	or	%g2, %g3, %g2			/* TAG_ACCESS */
1397	mov	T_INSTR_MMU_MISS, %g3		! arg2 = traptype
1398	set	trap, %g1
1399	ba,pt	%xcc, sys_trap
1400	  mov	-1, %g4
1401
1402.mmu_exception_not_aligned:
1403	/* %g2 = sfar, %g3 = sfsr */
1404	rdpr	%tstate, %g1
1405	btst	TSTATE_PRIV, %g1
1406	bnz,pn	%icc, 2f
1407	nop
1408	CPU_ADDR(%g1, %g4)				! load CPU struct addr
1409	ldn	[%g1 + CPU_THREAD], %g1			! load thread pointer
1410	ldn	[%g1 + T_PROCP], %g1			! load proc pointer
1411	ldn	[%g1 + P_UTRAPS], %g5			! are there utraps?
1412	brz,pt	%g5, 2f
1413	nop
1414	ldn	[%g5 + P_UTRAP15], %g5			! unaligned utrap?
1415	brz,pn	%g5, 2f
1416	nop
1417	btst	1, %sp
1418	bz,pt	%xcc, 1f				! 32 bit user program
1419	nop
1420	ba,pt	%xcc, .setup_v9utrap			! 64 bit user program
1421	nop
14221:
1423	ba,pt	%xcc, .setup_utrap
1424	or	%g2, %g0, %g7
14252:
1426	ba,pt	%xcc, .mmu_exception_end
1427	mov	T_ALIGNMENT, %g1
1428
1429.mmu_priv_exception:
1430	rdpr	%tstate, %g1
1431	btst	TSTATE_PRIV, %g1
1432	bnz,pn	%icc, 1f
1433	nop
1434	CPU_ADDR(%g1, %g4)				! load CPU struct addr
1435	ldn	[%g1 + CPU_THREAD], %g1			! load thread pointer
1436	ldn	[%g1 + T_PROCP], %g1			! load proc pointer
1437	ldn	[%g1 + P_UTRAPS], %g5			! are there utraps?
1438	brz,pt	%g5, 1f
1439	nop
1440	ldn	[%g5 + P_UTRAP16], %g5
1441	brnz,pt	%g5, .setup_v9utrap
1442	nop
14431:
1444	mov	T_PRIV_INSTR, %g1
1445
1446.mmu_exception_end:
1447	CPU_INDEX(%g4, %g5)
1448	set	cpu_core, %g5
1449	sllx	%g4, CPU_CORE_SHIFT, %g4
1450	add	%g4, %g5, %g4
1451	lduh	[%g4 + CPUC_DTRACE_FLAGS], %g5
1452	andcc	%g5, CPU_DTRACE_NOFAULT, %g0
1453	bz	1f
1454	or	%g5, CPU_DTRACE_BADADDR, %g5
1455	stuh	%g5, [%g4 + CPUC_DTRACE_FLAGS]
1456	done
1457
14581:
1459	sllx	%g3, 32, %g3
1460	or	%g3, %g1, %g3
1461	set	trap, %g1
1462	ba,pt	%xcc, sys_trap
1463	sub	%g0, 1, %g4
1464
1465.fp_disabled:
1466	CPU_ADDR(%g1, %g4)				! load CPU struct addr
1467	ldn	[%g1 + CPU_THREAD], %g1			! load thread pointer
1468	rdpr	%tstate, %g4
1469	btst	TSTATE_PRIV, %g4
1470	bnz,a,pn %icc, ptl1_panic
1471	  mov	PTL1_BAD_FPTRAP, %g1
1472
1473	ldn	[%g1 + T_PROCP], %g1			! load proc pointer
1474	ldn	[%g1 + P_UTRAPS], %g5			! are there utraps?
1475	brz,a,pt %g5, 2f
1476	  nop
1477	ldn	[%g5 + P_UTRAP7], %g5			! fp_disabled utrap?
1478	brz,a,pn %g5, 2f
1479	  nop
1480	btst	1, %sp
1481	bz,a,pt	%xcc, 1f				! 32 bit user program
1482	  nop
1483	ba,a,pt	%xcc, .setup_v9utrap			! 64 bit user program
1484	  nop
14851:
1486	ba,pt	%xcc, .setup_utrap
1487	  or	%g0, %g0, %g7
14882:
1489	set	fp_disabled, %g1
1490	ba,pt	%xcc, sys_trap
1491	  sub	%g0, 1, %g4
1492
1493.fp_ieee_exception:
1494	rdpr	%tstate, %g1
1495	btst	TSTATE_PRIV, %g1
1496	bnz,a,pn %icc, ptl1_panic
1497	  mov	PTL1_BAD_FPTRAP, %g1
1498	CPU_ADDR(%g1, %g4)				! load CPU struct addr
1499	stx	%fsr, [%g1 + CPU_TMP1]
1500	ldx	[%g1 + CPU_TMP1], %g2
1501	ldn	[%g1 + CPU_THREAD], %g1			! load thread pointer
1502	ldn	[%g1 + T_PROCP], %g1			! load proc pointer
1503	ldn	[%g1 + P_UTRAPS], %g5			! are there utraps?
1504	brz,a,pt %g5, 1f
1505	  nop
1506	ldn	[%g5 + P_UTRAP8], %g5
1507	brnz,a,pt %g5, .setup_v9utrap
1508	  nop
15091:
1510	set	_fp_ieee_exception, %g1
1511	ba,pt	%xcc, sys_trap
1512	  sub	%g0, 1, %g4
1513
1514/*
1515 * Register Inputs:
1516 *	%g5		user trap handler
1517 *	%g7		misaligned addr - for alignment traps only
1518 */
1519.setup_utrap:
1520	set	trap, %g1			! setup in case we go
1521	mov	T_FLUSH_PCB, %g3		! through sys_trap on
1522	sub	%g0, 1, %g4			! the save instruction below
1523
1524	/*
1525	 * If the DTrace pid provider is single stepping a copied-out
1526	 * instruction, t->t_dtrace_step will be set. In that case we need
1527	 * to abort the single-stepping (since execution of the instruction
1528	 * was interrupted) and use the value of t->t_dtrace_npc as the %npc.
1529	 */
1530	save	%sp, -SA(MINFRAME32), %sp	! window for trap handler
1531	CPU_ADDR(%g1, %g4)			! load CPU struct addr
1532	ldn	[%g1 + CPU_THREAD], %g1		! load thread pointer
1533	ldub	[%g1 + T_DTRACE_STEP], %g2	! load t->t_dtrace_step
1534	rdpr	%tnpc, %l2			! arg1 == tnpc
1535	brz,pt	%g2, 1f
1536	rdpr	%tpc, %l1			! arg0 == tpc
1537
1538	ldub	[%g1 + T_DTRACE_AST], %g2	! load t->t_dtrace_ast
1539	ldn	[%g1 + T_DTRACE_NPC], %l2	! arg1 = t->t_dtrace_npc (step)
1540	brz,pt	%g2, 1f
1541	st	%g0, [%g1 + T_DTRACE_FT]	! zero all pid provider flags
1542	stub	%g2, [%g1 + T_ASTFLAG]		! aston(t) if t->t_dtrace_ast
15431:
1544	mov	%g7, %l3			! arg2 == misaligned address
1545
1546	rdpr	%tstate, %g1			! cwp for trap handler
1547	rdpr	%cwp, %g4
1548	bclr	TSTATE_CWP_MASK, %g1
1549	wrpr	%g1, %g4, %tstate
1550	wrpr	%g0, %g5, %tnpc			! trap handler address
1551	FAST_TRAP_DONE
1552	/* NOTREACHED */
1553
1554.check_v9utrap:
1555	rdpr	%tstate, %g1
1556	btst	TSTATE_PRIV, %g1
1557	bnz,a,pn %icc, 3f
1558	  nop
1559	CPU_ADDR(%g4, %g1)				! load CPU struct addr
1560	ldn	[%g4 + CPU_THREAD], %g5			! load thread pointer
1561	ldn	[%g5 + T_PROCP], %g5			! load proc pointer
1562	ldn	[%g5 + P_UTRAPS], %g5			! are there utraps?
1563
1564	cmp	%g3, T_SOFTWARE_TRAP
1565	bne,a,pt %icc, 1f
1566	  nop
1567
1568	brz,pt %g5, 3f			! if p_utraps == NULL goto trap()
1569	  rdpr	%tt, %g3		! delay - get actual hw trap type
1570
1571	sub	%g3, 254, %g1		! UT_TRAP_INSTRUCTION_16 = p_utraps[18]
1572	ba,pt	%icc, 2f
1573	  smul	%g1, CPTRSIZE, %g2
15741:
1575	brz,a,pt %g5, 3f		! if p_utraps == NULL goto trap()
1576	  nop
1577
1578	cmp	%g3, T_UNIMP_INSTR
1579	bne,a,pt %icc, 2f
1580	  nop
1581
1582	mov	1, %g1
1583	st	%g1, [%g4 + CPU_TL1_HDLR] ! set CPU_TL1_HDLR
1584	rdpr	%tpc, %g1		! ld trapping instruction using
1585	lduwa	[%g1]ASI_AIUP, %g1	! "AS IF USER" ASI which could fault
1586	st	%g0, [%g4 + CPU_TL1_HDLR] ! clr CPU_TL1_HDLR
1587
1588	sethi	%hi(0xc1c00000), %g4	! setup mask for illtrap instruction
1589	andcc	%g1, %g4, %g4		! and instruction with mask
1590	bnz,a,pt %icc, 3f		! if %g4 == zero, %g1 is an ILLTRAP
1591	  nop				! fall thru to setup
15922:
1593	ldn	[%g5 + %g2], %g5
1594	brnz,a,pt %g5, .setup_v9utrap
1595	  nop
15963:
1597	set	trap, %g1
1598	ba,pt	%xcc, sys_trap
1599	  sub	%g0, 1, %g4
1600	/* NOTREACHED */
1601
1602/*
1603 * Register Inputs:
1604 *	%g5		user trap handler
1605 */
1606.setup_v9utrap:
1607	set	trap, %g1			! setup in case we go
1608	mov	T_FLUSH_PCB, %g3		! through sys_trap on
1609	sub	%g0, 1, %g4			! the save instruction below
1610
1611	/*
1612	 * If the DTrace pid provider is single stepping a copied-out
1613	 * instruction, t->t_dtrace_step will be set. In that case we need
1614	 * to abort the single-stepping (since execution of the instruction
1615	 * was interrupted) and use the value of t->t_dtrace_npc as the %npc.
1616	 */
1617	save	%sp, -SA(MINFRAME64), %sp	! window for trap handler
1618	CPU_ADDR(%g1, %g4)			! load CPU struct addr
1619	ldn	[%g1 + CPU_THREAD], %g1		! load thread pointer
1620	ldub	[%g1 + T_DTRACE_STEP], %g2	! load t->t_dtrace_step
1621	rdpr	%tnpc, %l7			! arg1 == tnpc
1622	brz,pt	%g2, 1f
1623	rdpr	%tpc, %l6			! arg0 == tpc
1624
1625	ldub	[%g1 + T_DTRACE_AST], %g2	! load t->t_dtrace_ast
1626	ldn	[%g1 + T_DTRACE_NPC], %l7	! arg1 == t->t_dtrace_npc (step)
1627	brz,pt	%g2, 1f
1628	st	%g0, [%g1 + T_DTRACE_FT]	! zero all pid provider flags
1629	stub	%g2, [%g1 + T_ASTFLAG]		! aston(t) if t->t_dtrace_ast
16301:
1631	rdpr	%tstate, %g2			! cwp for trap handler
1632	rdpr	%cwp, %g4
1633	bclr	TSTATE_CWP_MASK, %g2
1634	wrpr	%g2, %g4, %tstate
1635
1636	ldn	[%g1 + T_PROCP], %g4		! load proc pointer
1637	ldn	[%g4 + P_AS], %g4		! load as pointer
1638	ldn	[%g4 + A_USERLIMIT], %g4	! load as userlimit
1639	cmp	%l7, %g4			! check for single-step set
1640	bne,pt	%xcc, 4f
1641	  nop
1642	ldn	[%g1 + T_LWP], %g1		! load klwp pointer
1643	ld	[%g1 + PCB_STEP], %g4		! load single-step flag
1644	cmp	%g4, STEP_ACTIVE		! step flags set in pcb?
1645	bne,pt	%icc, 4f
1646	  nop
1647	stn	%g5, [%g1 + PCB_TRACEPC]	! save trap handler addr in pcb
1648	mov	%l7, %g4			! on entry to precise user trap
1649	add	%l6, 4, %l7			! handler, %l6 == pc, %l7 == npc
1650						! at time of trap
1651	wrpr	%g0, %g4, %tnpc			! generate FLTBOUNDS,
1652						! %g4 == userlimit
1653	FAST_TRAP_DONE
1654	/* NOTREACHED */
16554:
1656	wrpr	%g0, %g5, %tnpc			! trap handler address
1657	FAST_TRAP_DONE_CHK_INTR
1658	/* NOTREACHED */
1659
1660.fp_exception:
1661	CPU_ADDR(%g1, %g4)
1662	stx	%fsr, [%g1 + CPU_TMP1]
1663	ldx	[%g1 + CPU_TMP1], %g2
1664
1665	/*
1666	 * Cheetah takes unfinished_FPop trap for certain range of operands
1667	 * to the "fitos" instruction. Instead of going through the slow
1668	 * software emulation path, we try to simulate the "fitos" instruction
1669	 * via "fitod" and "fdtos" provided the following conditions are met:
1670	 *
1671	 *	fpu_exists is set (if DEBUG)
1672	 *	not in privileged mode
1673	 *	ftt is unfinished_FPop
1674	 *	NXM IEEE trap is not enabled
1675	 *	instruction at %tpc is "fitos"
1676	 *
1677	 *  Usage:
1678	 *	%g1	per cpu address
1679	 *	%g2	%fsr
1680	 *	%g6	user instruction
1681	 *
1682	 * Note that we can take a memory access related trap while trying
1683	 * to fetch the user instruction. Therefore, we set CPU_TL1_HDLR
1684	 * flag to catch those traps and let the SFMMU code deal with page
1685	 * fault and data access exception.
1686	 */
1687#if defined(DEBUG) || defined(NEED_FPU_EXISTS)
1688	sethi	%hi(fpu_exists), %g7
1689	ld	[%g7 + %lo(fpu_exists)], %g7
1690	brz,pn %g7, .fp_exception_cont
1691	  nop
1692#endif
1693	rdpr	%tstate, %g7			! branch if in privileged mode
1694	btst	TSTATE_PRIV, %g7
1695	bnz,pn	%xcc, .fp_exception_cont
1696	srl	%g2, FSR_FTT_SHIFT, %g7		! extract ftt from %fsr
1697	and	%g7, (FSR_FTT>>FSR_FTT_SHIFT), %g7
1698	cmp	%g7, FTT_UNFIN
1699	set	FSR_TEM_NX, %g5
1700	bne,pn	%xcc, .fp_exception_cont	! branch if NOT unfinished_FPop
1701	  andcc	%g2, %g5, %g0
1702	bne,pn	%xcc, .fp_exception_cont	! branch if FSR_TEM_NX enabled
1703	  rdpr	%tpc, %g5			! get faulting PC
1704
1705	or	%g0, 1, %g7
1706	st	%g7, [%g1 + CPU_TL1_HDLR]	! set tl1_hdlr flag
1707	lda	[%g5]ASI_USER, %g6		! get user's instruction
1708	st	%g0, [%g1 + CPU_TL1_HDLR]	! clear tl1_hdlr flag
1709
1710	set	FITOS_INSTR_MASK, %g7
1711	and	%g6, %g7, %g7
1712	set	FITOS_INSTR, %g5
1713	cmp	%g7, %g5
1714	bne,pn	%xcc, .fp_exception_cont	! branch if not FITOS_INSTR
1715	 nop
1716
1717	/*
1718	 * This is unfinished FPops trap for "fitos" instruction. We
1719	 * need to simulate "fitos" via "fitod" and "fdtos" instruction
1720	 * sequence.
1721	 *
1722	 * We need a temporary FP register to do the conversion. Since
1723	 * both source and destination operands for the "fitos" instruction
1724	 * have to be within %f0-%f31, we use an FP register from the upper
1725	 * half to guarantee that it won't collide with the source or the
1726	 * dest operand. However, we do have to save and restore its value.
1727	 *
1728	 * We use %d62 as a temporary FP register for the conversion and
1729	 * branch to appropriate instruction within the conversion tables
1730	 * based upon the rs2 and rd values.
1731	 */
1732
1733	std	%d62, [%g1 + CPU_TMP1]		! save original value
1734
1735	srl	%g6, FITOS_RS2_SHIFT, %g7
1736	and	%g7, FITOS_REG_MASK, %g7
1737	set	_fitos_fitod_table, %g4
1738	sllx	%g7, 2, %g7
1739	jmp	%g4 + %g7
1740	  ba,pt	%xcc, _fitos_fitod_done
1741	.empty
1742
1743_fitos_fitod_table:
1744	  fitod	%f0, %d62
1745	  fitod	%f1, %d62
1746	  fitod	%f2, %d62
1747	  fitod	%f3, %d62
1748	  fitod	%f4, %d62
1749	  fitod	%f5, %d62
1750	  fitod	%f6, %d62
1751	  fitod	%f7, %d62
1752	  fitod	%f8, %d62
1753	  fitod	%f9, %d62
1754	  fitod	%f10, %d62
1755	  fitod	%f11, %d62
1756	  fitod	%f12, %d62
1757	  fitod	%f13, %d62
1758	  fitod	%f14, %d62
1759	  fitod	%f15, %d62
1760	  fitod	%f16, %d62
1761	  fitod	%f17, %d62
1762	  fitod	%f18, %d62
1763	  fitod	%f19, %d62
1764	  fitod	%f20, %d62
1765	  fitod	%f21, %d62
1766	  fitod	%f22, %d62
1767	  fitod	%f23, %d62
1768	  fitod	%f24, %d62
1769	  fitod	%f25, %d62
1770	  fitod	%f26, %d62
1771	  fitod	%f27, %d62
1772	  fitod	%f28, %d62
1773	  fitod	%f29, %d62
1774	  fitod	%f30, %d62
1775	  fitod	%f31, %d62
1776_fitos_fitod_done:
1777
1778	/*
1779	 * Now convert data back into single precision
1780	 */
1781	srl	%g6, FITOS_RD_SHIFT, %g7
1782	and	%g7, FITOS_REG_MASK, %g7
1783	set	_fitos_fdtos_table, %g4
1784	sllx	%g7, 2, %g7
1785	jmp	%g4 + %g7
1786	  ba,pt	%xcc, _fitos_fdtos_done
1787	.empty
1788
1789_fitos_fdtos_table:
1790	  fdtos	%d62, %f0
1791	  fdtos	%d62, %f1
1792	  fdtos	%d62, %f2
1793	  fdtos	%d62, %f3
1794	  fdtos	%d62, %f4
1795	  fdtos	%d62, %f5
1796	  fdtos	%d62, %f6
1797	  fdtos	%d62, %f7
1798	  fdtos	%d62, %f8
1799	  fdtos	%d62, %f9
1800	  fdtos	%d62, %f10
1801	  fdtos	%d62, %f11
1802	  fdtos	%d62, %f12
1803	  fdtos	%d62, %f13
1804	  fdtos	%d62, %f14
1805	  fdtos	%d62, %f15
1806	  fdtos	%d62, %f16
1807	  fdtos	%d62, %f17
1808	  fdtos	%d62, %f18
1809	  fdtos	%d62, %f19
1810	  fdtos	%d62, %f20
1811	  fdtos	%d62, %f21
1812	  fdtos	%d62, %f22
1813	  fdtos	%d62, %f23
1814	  fdtos	%d62, %f24
1815	  fdtos	%d62, %f25
1816	  fdtos	%d62, %f26
1817	  fdtos	%d62, %f27
1818	  fdtos	%d62, %f28
1819	  fdtos	%d62, %f29
1820	  fdtos	%d62, %f30
1821	  fdtos	%d62, %f31
1822_fitos_fdtos_done:
1823
1824	ldd	[%g1 + CPU_TMP1], %d62		! restore %d62
1825
1826#if DEBUG
1827	/*
1828	 * Update FPop_unfinished trap kstat
1829	 */
1830	set	fpustat+FPUSTAT_UNFIN_KSTAT, %g7
1831	ldx	[%g7], %g5
18321:
1833	add	%g5, 1, %g6
1834
1835	casxa	[%g7] ASI_N, %g5, %g6
1836	cmp	%g5, %g6
1837	bne,a,pn %xcc, 1b
1838	  or	%g0, %g6, %g5
1839
1840	/*
1841	 * Update fpu_sim_fitos kstat
1842	 */
1843	set	fpuinfo+FPUINFO_FITOS_KSTAT, %g7
1844	ldx	[%g7], %g5
18451:
1846	add	%g5, 1, %g6
1847
1848	casxa	[%g7] ASI_N, %g5, %g6
1849	cmp	%g5, %g6
1850	bne,a,pn %xcc, 1b
1851	  or	%g0, %g6, %g5
1852#endif /* DEBUG */
1853
1854	FAST_TRAP_DONE
1855
1856.fp_exception_cont:
1857	/*
1858	 * Let _fp_exception deal with simulating FPop instruction.
1859	 * Note that we need to pass %fsr in %g2 (already read above).
1860	 */
1861
1862	set	_fp_exception, %g1
1863	ba,pt	%xcc, sys_trap
1864	sub	%g0, 1, %g4
1865
1866
1867/*
1868 * Register windows
1869 */
1870
1871/*
1872 * FILL_32bit_flushw/FILL_64bit_flushw fills a 32/64-bit-wide register window
1873 * from a 32/64-bit * wide address space via the designated asi.
1874 * It is used to fill windows in user_flushw to avoid going above TL 2.
1875 */
1876/* TODO: Use the faster FILL based on FILL_32bit_asi/FILL_64bit_asi */
1877#define	FILL_32bit_flushw(asi_num)				\
1878	mov	asi_num, %asi					;\
1879	rdpr	%cwp, %g2					;\
1880	sub	%g2, 1, %g2					;\
1881	wrpr	%g2, %cwp					;\
18821:	srl	%sp, 0, %sp					;\
1883	lda	[%sp + 0]%asi, %l0				;\
1884	lda	[%sp + 4]%asi, %l1				;\
1885	lda	[%sp + 8]%asi, %l2				;\
1886	lda	[%sp + 12]%asi, %l3				;\
1887	lda	[%sp + 16]%asi, %l4				;\
1888	lda	[%sp + 20]%asi, %l5				;\
1889	lda	[%sp + 24]%asi, %l6				;\
1890	lda	[%sp + 28]%asi, %l7				;\
1891	lda	[%sp + 32]%asi, %i0				;\
1892	lda	[%sp + 36]%asi, %i1				;\
1893	lda	[%sp + 40]%asi, %i2				;\
1894	lda	[%sp + 44]%asi, %i3				;\
1895	lda	[%sp + 48]%asi, %i4				;\
1896	lda	[%sp + 52]%asi, %i5				;\
1897	lda	[%sp + 56]%asi, %i6				;\
1898	lda	[%sp + 60]%asi, %i7				;\
1899	restored						;\
1900	add	%g2, 1, %g2					;\
1901	wrpr	%g2, %cwp
1902
1903#define	FILL_64bit_flushw(asi_num)				\
1904	mov	asi_num, %asi					;\
1905	rdpr	%cwp, %g2					;\
1906	sub	%g2, 1, %g2					;\
1907	wrpr	%g2, %cwp					;\
1908	ldxa	[%sp + V9BIAS64 + 0]%asi, %l0			;\
1909	ldxa	[%sp + V9BIAS64 + 8]%asi, %l1			;\
1910	ldxa	[%sp + V9BIAS64 + 16]%asi, %l2			;\
1911	ldxa	[%sp + V9BIAS64 + 24]%asi, %l3			;\
1912	ldxa	[%sp + V9BIAS64 + 32]%asi, %l4			;\
1913	ldxa	[%sp + V9BIAS64 + 40]%asi, %l5			;\
1914	ldxa	[%sp + V9BIAS64 + 48]%asi, %l6			;\
1915	ldxa	[%sp + V9BIAS64 + 56]%asi, %l7			;\
1916	ldxa	[%sp + V9BIAS64 + 64]%asi, %i0			;\
1917	ldxa	[%sp + V9BIAS64 + 72]%asi, %i1			;\
1918	ldxa	[%sp + V9BIAS64 + 80]%asi, %i2			;\
1919	ldxa	[%sp + V9BIAS64 + 88]%asi, %i3			;\
1920	ldxa	[%sp + V9BIAS64 + 96]%asi, %i4			;\
1921	ldxa	[%sp + V9BIAS64 + 104]%asi, %i5			;\
1922	ldxa	[%sp + V9BIAS64 + 112]%asi, %i6			;\
1923	ldxa	[%sp + V9BIAS64 + 120]%asi, %i7			;\
1924	restored						;\
1925	add	%g2, 1, %g2					;\
1926	wrpr	%g2, %cwp
1927
1928.flushw:
1929	rdpr	%tnpc, %g1
1930	wrpr	%g1, %tpc
1931	add	%g1, 4, %g1
1932	wrpr	%g1, %tnpc
1933	set	trap, %g1
1934	mov	T_FLUSH_PCB, %g3
1935	ba,pt	%xcc, sys_trap
1936	sub	%g0, 1, %g4
1937
1938.clean_windows:
1939	set	trap, %g1
1940	mov	T_FLUSH_PCB, %g3
1941	sub	%g0, 1, %g4
1942	save
1943	flushw
1944	rdpr	%canrestore, %g2
1945	brnz	%g2, 1f
1946	nop
1947	rdpr	%wstate, %g2
1948	btst	1, %g2
1949	beq	2f
1950	nop
1951	FILL_32bit_flushw(ASI_AIUP)
1952	ba,a	1f
1953	 .empty
19542:
1955	FILL_64bit_flushw(ASI_AIUP)
19561:
1957	restore
1958	wrpr	%g0, %g0, %cleanwin	! no clean windows
1959
1960	CPU_ADDR(%g4, %g5)
1961	ldn	[%g4 + CPU_MPCB], %g4
1962	brz,a,pn %g4, 1f
1963	  nop
1964	ld	[%g4 + MPCB_WSTATE], %g5
1965	add	%g5, WSTATE_CLEAN_OFFSET, %g5
1966	wrpr	%g0, %g5, %wstate
19671:	FAST_TRAP_DONE
1968
1969/*
1970 * .spill_clean: clean the previous window, restore the wstate, and
1971 * "done".
1972 *
1973 * Entry: %g7 contains new wstate
1974 */
1975.spill_clean:
1976	sethi	%hi(nwin_minus_one), %g5
1977	ld	[%g5 + %lo(nwin_minus_one)], %g5 ! %g5 = nwin - 1
1978	rdpr	%cwp, %g6			! %g6 = %cwp
1979	deccc	%g6				! %g6--
1980	movneg	%xcc, %g5, %g6			! if (%g6<0) %g6 = nwin-1
1981	wrpr	%g6, %cwp
1982	TT_TRACE_L(trace_win)
1983	clr	%l0
1984	clr	%l1
1985	clr	%l2
1986	clr	%l3
1987	clr	%l4
1988	clr	%l5
1989	clr	%l6
1990	clr	%l7
1991	wrpr	%g0, %g7, %wstate
1992	saved
1993	retry			! restores correct %cwp
1994
1995.fix_alignment:
1996	CPU_ADDR(%g1, %g2)		! load CPU struct addr to %g1 using %g2
1997	ldn	[%g1 + CPU_THREAD], %g1	! load thread pointer
1998	ldn	[%g1 + T_PROCP], %g1
1999	mov	1, %g2
2000	stb	%g2, [%g1 + P_FIXALIGNMENT]
2001	FAST_TRAP_DONE
2002
2003#define	STDF_REG(REG, ADDR, TMP)		\
2004	sll	REG, 3, REG			;\
2005mark1:	set	start1, TMP			;\
2006	jmp	REG + TMP			;\
2007	  nop					;\
2008start1:	ba,pt	%xcc, done1			;\
2009	  std	%f0, [ADDR + CPU_TMP1]		;\
2010	ba,pt	%xcc, done1			;\
2011	  std	%f32, [ADDR + CPU_TMP1]		;\
2012	ba,pt	%xcc, done1			;\
2013	  std	%f2, [ADDR + CPU_TMP1]		;\
2014	ba,pt	%xcc, done1			;\
2015	  std	%f34, [ADDR + CPU_TMP1]		;\
2016	ba,pt	%xcc, done1			;\
2017	  std	%f4, [ADDR + CPU_TMP1]		;\
2018	ba,pt	%xcc, done1			;\
2019	  std	%f36, [ADDR + CPU_TMP1]		;\
2020	ba,pt	%xcc, done1			;\
2021	  std	%f6, [ADDR + CPU_TMP1]		;\
2022	ba,pt	%xcc, done1			;\
2023	  std	%f38, [ADDR + CPU_TMP1]		;\
2024	ba,pt	%xcc, done1			;\
2025	  std	%f8, [ADDR + CPU_TMP1]		;\
2026	ba,pt	%xcc, done1			;\
2027	  std	%f40, [ADDR + CPU_TMP1]		;\
2028	ba,pt	%xcc, done1			;\
2029	  std	%f10, [ADDR + CPU_TMP1]		;\
2030	ba,pt	%xcc, done1			;\
2031	  std	%f42, [ADDR + CPU_TMP1]		;\
2032	ba,pt	%xcc, done1			;\
2033	  std	%f12, [ADDR + CPU_TMP1]		;\
2034	ba,pt	%xcc, done1			;\
2035	  std	%f44, [ADDR + CPU_TMP1]		;\
2036	ba,pt	%xcc, done1			;\
2037	  std	%f14, [ADDR + CPU_TMP1]		;\
2038	ba,pt	%xcc, done1			;\
2039	  std	%f46, [ADDR + CPU_TMP1]		;\
2040	ba,pt	%xcc, done1			;\
2041	  std	%f16, [ADDR + CPU_TMP1]		;\
2042	ba,pt	%xcc, done1			;\
2043	  std	%f48, [ADDR + CPU_TMP1]		;\
2044	ba,pt	%xcc, done1			;\
2045	  std	%f18, [ADDR + CPU_TMP1]		;\
2046	ba,pt	%xcc, done1			;\
2047	  std	%f50, [ADDR + CPU_TMP1]		;\
2048	ba,pt	%xcc, done1			;\
2049	  std	%f20, [ADDR + CPU_TMP1]		;\
2050	ba,pt	%xcc, done1			;\
2051	  std	%f52, [ADDR + CPU_TMP1]		;\
2052	ba,pt	%xcc, done1			;\
2053	  std	%f22, [ADDR + CPU_TMP1]		;\
2054	ba,pt	%xcc, done1			;\
2055	  std	%f54, [ADDR + CPU_TMP1]		;\
2056	ba,pt	%xcc, done1			;\
2057	  std	%f24, [ADDR + CPU_TMP1]		;\
2058	ba,pt	%xcc, done1			;\
2059	  std	%f56, [ADDR + CPU_TMP1]		;\
2060	ba,pt	%xcc, done1			;\
2061	  std	%f26, [ADDR + CPU_TMP1]		;\
2062	ba,pt	%xcc, done1			;\
2063	  std	%f58, [ADDR + CPU_TMP1]		;\
2064	ba,pt	%xcc, done1			;\
2065	  std	%f28, [ADDR + CPU_TMP1]		;\
2066	ba,pt	%xcc, done1			;\
2067	  std	%f60, [ADDR + CPU_TMP1]		;\
2068	ba,pt	%xcc, done1			;\
2069	  std	%f30, [ADDR + CPU_TMP1]		;\
2070	ba,pt	%xcc, done1			;\
2071	  std	%f62, [ADDR + CPU_TMP1]		;\
2072done1:
2073
2074#define	LDDF_REG(REG, ADDR, TMP)		\
2075	sll	REG, 3, REG			;\
2076mark2:	set	start2, TMP			;\
2077	jmp	REG + TMP			;\
2078	  nop					;\
2079start2:	ba,pt	%xcc, done2			;\
2080	  ldd	[ADDR + CPU_TMP1], %f0		;\
2081	ba,pt	%xcc, done2			;\
2082	  ldd	[ADDR + CPU_TMP1], %f32		;\
2083	ba,pt	%xcc, done2			;\
2084	  ldd	[ADDR + CPU_TMP1], %f2		;\
2085	ba,pt	%xcc, done2			;\
2086	  ldd	[ADDR + CPU_TMP1], %f34		;\
2087	ba,pt	%xcc, done2			;\
2088	  ldd	[ADDR + CPU_TMP1], %f4		;\
2089	ba,pt	%xcc, done2			;\
2090	  ldd	[ADDR + CPU_TMP1], %f36		;\
2091	ba,pt	%xcc, done2			;\
2092	  ldd	[ADDR + CPU_TMP1], %f6		;\
2093	ba,pt	%xcc, done2			;\
2094	  ldd	[ADDR + CPU_TMP1], %f38		;\
2095	ba,pt	%xcc, done2			;\
2096	  ldd	[ADDR + CPU_TMP1], %f8		;\
2097	ba,pt	%xcc, done2			;\
2098	  ldd	[ADDR + CPU_TMP1], %f40		;\
2099	ba,pt	%xcc, done2			;\
2100	  ldd	[ADDR + CPU_TMP1], %f10		;\
2101	ba,pt	%xcc, done2			;\
2102	  ldd	[ADDR + CPU_TMP1], %f42		;\
2103	ba,pt	%xcc, done2			;\
2104	  ldd	[ADDR + CPU_TMP1], %f12		;\
2105	ba,pt	%xcc, done2			;\
2106	  ldd	[ADDR + CPU_TMP1], %f44		;\
2107	ba,pt	%xcc, done2			;\
2108	  ldd	[ADDR + CPU_TMP1], %f14		;\
2109	ba,pt	%xcc, done2			;\
2110	  ldd	[ADDR + CPU_TMP1], %f46		;\
2111	ba,pt	%xcc, done2			;\
2112	  ldd	[ADDR + CPU_TMP1], %f16		;\
2113	ba,pt	%xcc, done2			;\
2114	  ldd	[ADDR + CPU_TMP1], %f48		;\
2115	ba,pt	%xcc, done2			;\
2116	  ldd	[ADDR + CPU_TMP1], %f18		;\
2117	ba,pt	%xcc, done2			;\
2118	  ldd	[ADDR + CPU_TMP1], %f50		;\
2119	ba,pt	%xcc, done2			;\
2120	  ldd	[ADDR + CPU_TMP1], %f20		;\
2121	ba,pt	%xcc, done2			;\
2122	  ldd	[ADDR + CPU_TMP1], %f52		;\
2123	ba,pt	%xcc, done2			;\
2124	  ldd	[ADDR + CPU_TMP1], %f22		;\
2125	ba,pt	%xcc, done2			;\
2126	  ldd	[ADDR + CPU_TMP1], %f54		;\
2127	ba,pt	%xcc, done2			;\
2128	  ldd	[ADDR + CPU_TMP1], %f24		;\
2129	ba,pt	%xcc, done2			;\
2130	  ldd	[ADDR + CPU_TMP1], %f56		;\
2131	ba,pt	%xcc, done2			;\
2132	  ldd	[ADDR + CPU_TMP1], %f26		;\
2133	ba,pt	%xcc, done2			;\
2134	  ldd	[ADDR + CPU_TMP1], %f58		;\
2135	ba,pt	%xcc, done2			;\
2136	  ldd	[ADDR + CPU_TMP1], %f28		;\
2137	ba,pt	%xcc, done2			;\
2138	  ldd	[ADDR + CPU_TMP1], %f60		;\
2139	ba,pt	%xcc, done2			;\
2140	  ldd	[ADDR + CPU_TMP1], %f30		;\
2141	ba,pt	%xcc, done2			;\
2142	  ldd	[ADDR + CPU_TMP1], %f62		;\
2143done2:
2144
2145.lddf_exception_not_aligned:
2146	/* %g2 = sfar, %g3 = sfsr */
2147	mov	%g2, %g5		! stash sfar
2148#if defined(DEBUG) || defined(NEED_FPU_EXISTS)
2149	sethi	%hi(fpu_exists), %g2	! check fpu_exists
2150	ld	[%g2 + %lo(fpu_exists)], %g2
2151	brz,a,pn %g2, 4f
2152	  nop
2153#endif
2154	CPU_ADDR(%g1, %g4)
2155	or	%g0, 1, %g4
2156	st	%g4, [%g1 + CPU_TL1_HDLR] ! set tl1_hdlr flag
2157
2158	rdpr	%tpc, %g2
2159	lda	[%g2]ASI_AIUP, %g6	! get the user's lddf instruction
2160	srl	%g6, 23, %g1		! using ldda or not?
2161	and	%g1, 1, %g1
2162	brz,a,pt %g1, 2f		! check for ldda instruction
2163	  nop
2164	srl	%g6, 13, %g1		! check immflag
2165	and	%g1, 1, %g1
2166	rdpr	%tstate, %g2		! %tstate in %g2
2167	brnz,a,pn %g1, 1f
2168	  srl	%g2, 31, %g1		! get asi from %tstate
2169	srl	%g6, 5, %g1		! get asi from instruction
2170	and	%g1, 0xFF, %g1		! imm_asi field
21711:
2172	cmp	%g1, ASI_P		! primary address space
2173	be,a,pt %icc, 2f
2174	  nop
2175	cmp	%g1, ASI_PNF		! primary no fault address space
2176	be,a,pt %icc, 2f
2177	  nop
2178	cmp	%g1, ASI_S		! secondary address space
2179	be,a,pt %icc, 2f
2180	  nop
2181	cmp	%g1, ASI_SNF		! secondary no fault address space
2182	bne,a,pn %icc, 3f
2183	  nop
21842:
2185	lduwa	[%g5]ASI_USER, %g7	! get first half of misaligned data
2186	add	%g5, 4, %g5		! increment misaligned data address
2187	lduwa	[%g5]ASI_USER, %g5	! get second half of misaligned data
2188
2189	sllx	%g7, 32, %g7
2190	or	%g5, %g7, %g5		! combine data
2191	CPU_ADDR(%g7, %g1)		! save data on a per-cpu basis
2192	stx	%g5, [%g7 + CPU_TMP1]	! save in cpu_tmp1
2193
2194	srl	%g6, 25, %g3		! %g6 has the instruction
2195	and	%g3, 0x1F, %g3		! %g3 has rd
2196	LDDF_REG(%g3, %g7, %g4)
2197
2198	CPU_ADDR(%g1, %g4)
2199	st	%g0, [%g1 + CPU_TL1_HDLR] ! clear tl1_hdlr flag
2200	FAST_TRAP_DONE
22013:
2202	CPU_ADDR(%g1, %g4)
2203	st	%g0, [%g1 + CPU_TL1_HDLR] ! clear tl1_hdlr flag
22044:
2205	set	T_USER, %g3		! trap type in %g3
2206	or	%g3, T_LDDF_ALIGN, %g3
2207	mov	%g5, %g2		! misaligned vaddr in %g2
2208	set	fpu_trap, %g1		! goto C for the little and
2209	ba,pt	%xcc, sys_trap		! no fault little asi's
2210	  sub	%g0, 1, %g4
2211
2212.stdf_exception_not_aligned:
2213	/* %g2 = sfar, %g3 = sfsr */
2214	mov	%g2, %g5
2215
2216#if defined(DEBUG) || defined(NEED_FPU_EXISTS)
2217	sethi	%hi(fpu_exists), %g7		! check fpu_exists
2218	ld	[%g7 + %lo(fpu_exists)], %g3
2219	brz,a,pn %g3, 4f
2220	  nop
2221#endif
2222	CPU_ADDR(%g1, %g4)
2223	or	%g0, 1, %g4
2224	st	%g4, [%g1 + CPU_TL1_HDLR] ! set tl1_hdlr flag
2225
2226	rdpr	%tpc, %g2
2227	lda	[%g2]ASI_AIUP, %g6	! get the user's stdf instruction
2228
2229	srl	%g6, 23, %g1		! using stda or not?
2230	and	%g1, 1, %g1
2231	brz,a,pt %g1, 2f		! check for stda instruction
2232	  nop
2233	srl	%g6, 13, %g1		! check immflag
2234	and	%g1, 1, %g1
2235	rdpr	%tstate, %g2		! %tstate in %g2
2236	brnz,a,pn %g1, 1f
2237	  srl	%g2, 31, %g1		! get asi from %tstate
2238	srl	%g6, 5, %g1		! get asi from instruction
2239	and	%g1, 0xff, %g1		! imm_asi field
22401:
2241	cmp	%g1, ASI_P		! primary address space
2242	be,a,pt %icc, 2f
2243	  nop
2244	cmp	%g1, ASI_S		! secondary address space
2245	bne,a,pn %icc, 3f
2246	  nop
22472:
2248	srl	%g6, 25, %g6
2249	and	%g6, 0x1F, %g6		! %g6 has rd
2250	CPU_ADDR(%g7, %g1)
2251	STDF_REG(%g6, %g7, %g4)		! STDF_REG(REG, ADDR, TMP)
2252
2253	ldx	[%g7 + CPU_TMP1], %g6
2254	srlx	%g6, 32, %g7
2255	stuwa	%g7, [%g5]ASI_USER	! first half
2256	add	%g5, 4, %g5		! increment misaligned data address
2257	stuwa	%g6, [%g5]ASI_USER	! second half
2258
2259	CPU_ADDR(%g1, %g4)
2260	st	%g0, [%g1 + CPU_TL1_HDLR] ! clear tl1_hdlr flag
2261	FAST_TRAP_DONE
22623:
2263	CPU_ADDR(%g1, %g4)
2264	st	%g0, [%g1 + CPU_TL1_HDLR] ! clear tl1_hdlr flag
22654:
2266	set	T_USER, %g3		! trap type in %g3
2267	or	%g3, T_STDF_ALIGN, %g3
2268	mov	%g5, %g2		! misaligned vaddr in %g2
2269	set	fpu_trap, %g1		! goto C for the little and
2270	ba,pt	%xcc, sys_trap		! nofault little asi's
2271	  sub	%g0, 1, %g4
2272
2273#ifdef DEBUG_USER_TRAPTRACECTL
2274
2275.traptrace_freeze:
2276	mov	%l0, %g1 ; mov	%l1, %g2 ; mov	%l2, %g3 ; mov	%l4, %g4
2277	TT_TRACE_L(trace_win)
2278	mov	%g4, %l4 ; mov	%g3, %l2 ; mov	%g2, %l1 ; mov	%g1, %l0
2279	set	trap_freeze, %g1
2280	mov	1, %g2
2281	st	%g2, [%g1]
2282	FAST_TRAP_DONE
2283
2284.traptrace_unfreeze:
2285	set	trap_freeze, %g1
2286	st	%g0, [%g1]
2287	mov	%l0, %g1 ; mov	%l1, %g2 ; mov	%l2, %g3 ; mov	%l4, %g4
2288	TT_TRACE_L(trace_win)
2289	mov	%g4, %l4 ; mov	%g3, %l2 ; mov	%g2, %l1 ; mov	%g1, %l0
2290	FAST_TRAP_DONE
2291
2292#endif /* DEBUG_USER_TRAPTRACECTL */
2293
2294.getcc:
2295	CPU_ADDR(%g1, %g2)
2296	stx	%o0, [%g1 + CPU_TMP1]		! save %o0
2297	rdpr	%tstate, %g3			! get tstate
2298	srlx	%g3, PSR_TSTATE_CC_SHIFT, %o0	! shift ccr to V8 psr
2299	set	PSR_ICC, %g2
2300	and	%o0, %g2, %o0			! mask out the rest
2301	srl	%o0, PSR_ICC_SHIFT, %o0		! right justify
2302	wrpr	%g0, 0, %gl
2303	mov	%o0, %g1			! move ccr to normal %g1
2304	wrpr	%g0, 1, %gl
2305	! cannot assume globals retained their values after increasing %gl
2306	CPU_ADDR(%g1, %g2)
2307	ldx	[%g1 + CPU_TMP1], %o0		! restore %o0
2308	FAST_TRAP_DONE
2309
2310.setcc:
2311	CPU_ADDR(%g1, %g2)
2312	stx	%o0, [%g1 + CPU_TMP1]		! save %o0
2313	wrpr	%g0, 0, %gl
2314	mov	%g1, %o0
2315	wrpr	%g0, 1, %gl
2316	! cannot assume globals retained their values after increasing %gl
2317	CPU_ADDR(%g1, %g2)
2318	sll	%o0, PSR_ICC_SHIFT, %g2
2319	set	PSR_ICC, %g3
2320	and	%g2, %g3, %g2			! mask out rest
2321	sllx	%g2, PSR_TSTATE_CC_SHIFT, %g2
2322	rdpr	%tstate, %g3			! get tstate
2323	srl	%g3, 0, %g3			! clear upper word
2324	or	%g3, %g2, %g3			! or in new bits
2325	wrpr	%g3, %tstate
2326	ldx	[%g1 + CPU_TMP1], %o0		! restore %o0
2327	FAST_TRAP_DONE
2328
2329/*
2330 * getpsr(void)
2331 * Note that the xcc part of the ccr is not provided.
2332 * The V8 code shows why the V9 trap is not faster:
2333 * #define GETPSR_TRAP() \
2334 *      mov %psr, %i0; jmp %l2; rett %l2+4; nop;
2335 */
2336
2337	.type	.getpsr, #function
2338.getpsr:
2339	rdpr	%tstate, %g1			! get tstate
2340	srlx	%g1, PSR_TSTATE_CC_SHIFT, %o0	! shift ccr to V8 psr
2341	set	PSR_ICC, %g2
2342	and	%o0, %g2, %o0			! mask out the rest
2343
2344	rd	%fprs, %g1			! get fprs
2345	and	%g1, FPRS_FEF, %g2		! mask out dirty upper/lower
2346	sllx	%g2, PSR_FPRS_FEF_SHIFT, %g2	! shift fef to V8 psr.ef
2347	or	%o0, %g2, %o0			! or result into psr.ef
2348
2349	set	V9_PSR_IMPLVER, %g2		! SI assigned impl/ver: 0xef
2350	or	%o0, %g2, %o0			! or psr.impl/ver
2351	FAST_TRAP_DONE
2352	SET_SIZE(.getpsr)
2353
2354/*
2355 * setpsr(newpsr)
2356 * Note that there is no support for ccr.xcc in the V9 code.
2357 */
2358
2359	.type	.setpsr, #function
2360.setpsr:
2361	rdpr	%tstate, %g1			! get tstate
2362!	setx	TSTATE_V8_UBITS, %g2
2363	or 	%g0, CCR_ICC, %g3
2364	sllx	%g3, TSTATE_CCR_SHIFT, %g2
2365
2366	andn	%g1, %g2, %g1			! zero current user bits
2367	set	PSR_ICC, %g2
2368	and	%g2, %o0, %g2			! clear all but psr.icc bits
2369	sllx	%g2, PSR_TSTATE_CC_SHIFT, %g3	! shift to tstate.ccr.icc
2370	wrpr	%g1, %g3, %tstate		! write tstate
2371
2372	set	PSR_EF, %g2
2373	and	%g2, %o0, %g2			! clear all but fp enable bit
2374	srlx	%g2, PSR_FPRS_FEF_SHIFT, %g4	! shift ef to V9 fprs.fef
2375	wr	%g0, %g4, %fprs			! write fprs
2376
2377	CPU_ADDR(%g1, %g2)			! load CPU struct addr to %g1
2378	ldn	[%g1 + CPU_THREAD], %g2		! load thread pointer
2379	ldn	[%g2 + T_LWP], %g3		! load klwp pointer
2380	ldn	[%g3 + LWP_FPU], %g2		! get lwp_fpu pointer
2381	stuw	%g4, [%g2 + FPU_FPRS]		! write fef value to fpu_fprs
2382	srlx	%g4, 2, %g4			! shift fef value to bit 0
2383	stub	%g4, [%g2 + FPU_EN]		! write fef value to fpu_en
2384	FAST_TRAP_DONE
2385	SET_SIZE(.setpsr)
2386
2387/*
2388 * getlgrp
2389 * get home lgrpid on which the calling thread is currently executing.
2390 */
2391	.type	.getlgrp, #function
2392.getlgrp:
2393	! Thanks for the incredibly helpful comments
2394	CPU_ADDR(%g1, %g2)		! load CPU struct addr to %g1 using %g2
2395	ld	[%g1 + CPU_ID], %o0	! load cpu_id
2396	ldn	[%g1 + CPU_THREAD], %g2	! load thread pointer
2397	ldn	[%g2 + T_LPL], %g2	! load lpl pointer
2398	ld	[%g2 + LPL_LGRPID], %g1	! load lpl_lgrpid
2399	sra	%g1, 0, %o1
2400	FAST_TRAP_DONE
2401	SET_SIZE(.getlgrp)
2402
2403/*
2404 * Entry for old 4.x trap (trap 0).
2405 */
2406	ENTRY_NP(syscall_trap_4x)
2407	CPU_ADDR(%g1, %g2)		! load CPU struct addr to %g1 using %g2
2408	ldn	[%g1 + CPU_THREAD], %g2	! load thread pointer
2409	ldn	[%g2 + T_LWP], %g2	! load klwp pointer
2410	ld	[%g2 + PCB_TRAP0], %g2	! lwp->lwp_pcb.pcb_trap0addr
2411	brz,pn	%g2, 1f			! has it been set?
2412	st	%l0, [%g1 + CPU_TMP1]	! delay - save some locals
2413	st	%l1, [%g1 + CPU_TMP2]
2414	rdpr	%tnpc, %l1		! save old tnpc
2415	wrpr	%g0, %g2, %tnpc		! setup tnpc
2416
2417	mov	%g1, %l0		! save CPU struct addr
2418	wrpr	%g0, 0, %gl
2419	mov	%l1, %g6		! pass tnpc to user code in %g6
2420	wrpr	%g0, 1, %gl
2421	ld	[%l0 + CPU_TMP2], %l1	! restore locals
2422	ld	[%l0 + CPU_TMP1], %l0
2423	FAST_TRAP_DONE_CHK_INTR
24241:
2425	!
2426	! check for old syscall mmap which is the only different one which
2427	! must be the same.  Others are handled in the compatibility library.
2428	!
2429	mov	%g1, %l0		! save CPU struct addr
2430	wrpr	%g0, 0, %gl
2431	cmp	%g1, OSYS_mmap		! compare to old 4.x mmap
2432	movz	%icc, SYS_mmap, %g1
2433	wrpr	%g0, 1, %gl
2434	ld	[%l0 + CPU_TMP1], %l0
2435	SYSCALL(syscall_trap32)
2436	SET_SIZE(syscall_trap_4x)
2437
2438/*
2439 * Handler for software trap 9.
2440 * Set trap0 emulation address for old 4.x system call trap.
2441 * XXX - this should be a system call.
2442 */
2443	ENTRY_NP(set_trap0_addr)
2444	CPU_ADDR(%g1, %g2)		! load CPU struct addr to %g1 using %g2
2445	st	%l0, [%g1 + CPU_TMP1]	! save some locals
2446	st	%l1, [%g1 + CPU_TMP2]
2447	mov	%g1, %l0	! preserve CPU addr
2448	wrpr	%g0, 0, %gl
2449	mov	%g1, %l1
2450	wrpr	%g0, 1, %gl
2451	! cannot assume globals retained their values after increasing %gl
2452	ldn	[%l0 + CPU_THREAD], %g2	! load thread pointer
2453	ldn	[%g2 + T_LWP], %g2	! load klwp pointer
2454	andn	%l1, 3, %l1		! force alignment
2455	st	%l1, [%g2 + PCB_TRAP0]	! lwp->lwp_pcb.pcb_trap0addr
2456	ld	[%l0 + CPU_TMP2], %l1	! restore locals
2457	ld	[%l0 + CPU_TMP1], %l0
2458	FAST_TRAP_DONE
2459	SET_SIZE(set_trap0_addr)
2460
2461/*
2462 * mmu_trap_tl1
2463 * trap handler for unexpected mmu traps.
2464 * simply checks if the trap was a user lddf/stdf alignment trap, in which
2465 * case we go to fpu_trap or a user trap from the window handler, in which
2466 * case we go save the state on the pcb.  Otherwise, we go to ptl1_panic.
2467 */
2468	.type	mmu_trap_tl1, #function
2469mmu_trap_tl1:
2470#ifdef	TRAPTRACE
2471	TRACE_PTR(%g5, %g6)
2472	GET_TRACE_TICK(%g6)
2473	stxa	%g6, [%g5 + TRAP_ENT_TICK]%asi
2474	TRACE_SAVE_TL_GL_REGS(%g5, %g6)
2475	rdpr	%tt, %g6
2476	stha	%g6, [%g5 + TRAP_ENT_TT]%asi
2477	rdpr	%tstate, %g6
2478	stxa	%g6, [%g5 + TRAP_ENT_TSTATE]%asi
2479	stna	%sp, [%g5 + TRAP_ENT_SP]%asi
2480	stna	%g0, [%g5 + TRAP_ENT_TR]%asi
2481	rdpr	%tpc, %g6
2482	stna	%g6, [%g5 + TRAP_ENT_TPC]%asi
2483	MMU_FAULT_STATUS_AREA(%g6)
2484	ldx	[%g6 + MMFSA_D_ADDR], %g6
2485	stna	%g6, [%g5 + TRAP_ENT_F1]%asi !  MMU fault address
2486	CPU_PADDR(%g7, %g6);
2487	add	%g7, CPU_TL1_HDLR, %g7
2488	lda	[%g7]ASI_MEM, %g6
2489	stna	%g6, [%g5 + TRAP_ENT_F2]%asi
2490	MMU_FAULT_STATUS_AREA(%g6)
2491	ldx	[%g6 + MMFSA_D_TYPE], %g7 ! XXXQ should be a MMFSA_F_ constant?
2492	ldx	[%g6 + MMFSA_D_CTX], %g6
2493	sllx	%g6, SFSR_CTX_SHIFT, %g6
2494	or	%g6, %g7, %g6
2495	stna	%g6, [%g5 + TRAP_ENT_F3]%asi ! MMU context/type
2496	set	0xdeadbeef, %g6
2497	stna	%g6, [%g5 + TRAP_ENT_F4]%asi
2498	TRACE_NEXT(%g5, %g6, %g7)
2499#endif /* TRAPTRACE */
2500	CPU_PADDR(%g7, %g6);
2501	add     %g7, CPU_TL1_HDLR, %g7		! %g7 = &cpu_m.tl1_hdlr (PA)
2502	lda	[%g7]ASI_MEM, %g6
2503	brz,a,pt %g6, 1f
2504	  nop
2505	sta     %g0, [%g7]ASI_MEM
2506	! XXXQ need to setup registers for sfmmu_mmu_trap?
2507	ba,a,pt	%xcc, sfmmu_mmu_trap		! handle page faults
25081:
2509	rdpr	%tpc, %g7
2510	/* in user_rtt? */
2511	set	rtt_fill_start, %g6
2512	cmp	%g7, %g6
2513	blu,pn	%xcc, 6f
2514	 .empty
2515	set	rtt_fill_end, %g6
2516	cmp	%g7, %g6
2517	bgeu,pn %xcc, 6f
2518	 nop
2519	set	fault_rtt_fn1, %g7
2520	ba,a	7f
25216:
2522	! check to see if the trap pc is in a window spill/fill handling
2523	rdpr	%tpc, %g7
2524	/* tpc should be in the trap table */
2525	set	trap_table, %g6
2526	cmp	%g7, %g6
2527	blu,a,pn %xcc, ptl1_panic
2528	  mov	PTL1_BAD_MMUTRAP, %g1
2529	set	etrap_table, %g6
2530	cmp	%g7, %g6
2531	bgeu,a,pn %xcc, ptl1_panic
2532	  mov	PTL1_BAD_MMUTRAP, %g1
2533	! pc is inside the trap table, convert to trap type
2534	srl	%g7, 5, %g6		! XXXQ need #define
2535	and	%g6, 0x1ff, %g6		! XXXQ need #define
2536	! and check for a window trap type
2537	and	%g6, WTRAP_TTMASK, %g6
2538	cmp	%g6, WTRAP_TYPE
2539	bne,a,pn %xcc, ptl1_panic
2540	  mov	PTL1_BAD_MMUTRAP, %g1
2541	andn	%g7, WTRAP_ALIGN, %g7	/* 128 byte aligned */
2542	add	%g7, WTRAP_FAULTOFF, %g7
2543
25447:
2545	! Arguments are passed in the global set active after the
2546	! 'done' instruction. Before switching sets, must save
2547	! the calculated next pc
2548	wrpr	%g0, %g7, %tnpc
2549	wrpr	%g0, 1, %gl
2550	rdpr	%tt, %g5
2551	MMU_FAULT_STATUS_AREA(%g7)
2552	cmp	%g5, T_ALIGNMENT
2553	be,pn	%xcc, 1f
2554	ldx	[%g7 + MMFSA_D_ADDR], %g6
2555	ldx	[%g7 + MMFSA_D_CTX], %g7
2556	srlx	%g6, MMU_PAGESHIFT, %g6		/* align address */
2557	sllx	%g6, MMU_PAGESHIFT, %g6
2558	or	%g6, %g7, %g6			/* TAG_ACCESS */
25591:
2560	done
2561	SET_SIZE(mmu_trap_tl1)
2562
2563/*
2564 * Several traps use kmdb_trap and kmdb_trap_tl1 as their handlers.  These
2565 * traps are valid only when kmdb is loaded.  When the debugger is active,
2566 * the code below is rewritten to transfer control to the appropriate
2567 * debugger entry points.
2568 */
2569	.global	kmdb_trap
2570	.align	8
2571kmdb_trap:
2572	ba,a	trap_table0
2573	jmp	%g1 + 0
2574	nop
2575
2576	.global	kmdb_trap_tl1
2577	.align	8
2578kmdb_trap_tl1:
2579	ba,a	trap_table0
2580	jmp	%g1 + 0
2581	nop
2582
2583/*
2584 * This entry is copied from OBP's trap table during boot.
2585 */
2586	.global	obp_bpt
2587	.align	8
2588obp_bpt:
2589	NOT
2590
2591
2592
2593#ifdef	TRAPTRACE
2594/*
2595 * TRAPTRACE support.
2596 * labels here are branched to with "rd %pc, %g7" in the delay slot.
2597 * Return is done by "jmp %g7 + 4".
2598 */
2599
2600trace_dmmu:
2601	TRACE_PTR(%g3, %g6)
2602	GET_TRACE_TICK(%g6)
2603	stxa	%g6, [%g3 + TRAP_ENT_TICK]%asi
2604	TRACE_SAVE_TL_GL_REGS(%g3, %g6)
2605	rdpr	%tt, %g6
2606	stha	%g6, [%g3 + TRAP_ENT_TT]%asi
2607	rdpr	%tstate, %g6
2608	stxa	%g6, [%g3 + TRAP_ENT_TSTATE]%asi
2609	stna	%sp, [%g3 + TRAP_ENT_SP]%asi
2610	rdpr	%tpc, %g6
2611	stna	%g6, [%g3 + TRAP_ENT_TPC]%asi
2612	MMU_FAULT_STATUS_AREA(%g6)
2613	ldx	[%g6 + MMFSA_D_ADDR], %g4
2614	stxa	%g4, [%g3 + TRAP_ENT_TR]%asi
2615	ldx	[%g6 + MMFSA_D_CTX], %g4
2616	stxa	%g4, [%g3 + TRAP_ENT_F1]%asi
2617	ldx	[%g6 + MMFSA_D_TYPE], %g4
2618	stxa	%g4, [%g3 + TRAP_ENT_F2]%asi
2619	stxa	%g6, [%g3 + TRAP_ENT_F3]%asi
2620	stna	%g0, [%g3 + TRAP_ENT_F4]%asi
2621	TRACE_NEXT(%g3, %g4, %g5)
2622	jmp	%g7 + 4
2623	nop
2624
2625trace_immu:
2626	TRACE_PTR(%g3, %g6)
2627	GET_TRACE_TICK(%g6)
2628	stxa	%g6, [%g3 + TRAP_ENT_TICK]%asi
2629	TRACE_SAVE_TL_GL_REGS(%g3, %g6)
2630	rdpr	%tt, %g6
2631	stha	%g6, [%g3 + TRAP_ENT_TT]%asi
2632	rdpr	%tstate, %g6
2633	stxa	%g6, [%g3 + TRAP_ENT_TSTATE]%asi
2634	stna	%sp, [%g3 + TRAP_ENT_SP]%asi
2635	rdpr	%tpc, %g6
2636	stna	%g6, [%g3 + TRAP_ENT_TPC]%asi
2637	MMU_FAULT_STATUS_AREA(%g6)
2638	ldx	[%g6 + MMFSA_I_ADDR], %g4
2639	stxa	%g4, [%g3 + TRAP_ENT_TR]%asi
2640	ldx	[%g6 + MMFSA_I_CTX], %g4
2641	stxa	%g4, [%g3 + TRAP_ENT_F1]%asi
2642	ldx	[%g6 + MMFSA_I_TYPE], %g4
2643	stxa	%g4, [%g3 + TRAP_ENT_F2]%asi
2644	stxa	%g6, [%g3 + TRAP_ENT_F3]%asi
2645	stna	%g0, [%g3 + TRAP_ENT_F4]%asi
2646	TRACE_NEXT(%g3, %g4, %g5)
2647	jmp	%g7 + 4
2648	nop
2649
2650trace_gen:
2651	TRACE_PTR(%g3, %g6)
2652	GET_TRACE_TICK(%g6)
2653	stxa	%g6, [%g3 + TRAP_ENT_TICK]%asi
2654	TRACE_SAVE_TL_GL_REGS(%g3, %g6)
2655	rdpr	%tt, %g6
2656	stha	%g6, [%g3 + TRAP_ENT_TT]%asi
2657	rdpr	%tstate, %g6
2658	stxa	%g6, [%g3 + TRAP_ENT_TSTATE]%asi
2659	stna	%sp, [%g3 + TRAP_ENT_SP]%asi
2660	rdpr	%tpc, %g6
2661	stna	%g6, [%g3 + TRAP_ENT_TPC]%asi
2662	stna	%g0, [%g3 + TRAP_ENT_TR]%asi
2663	stna	%g0, [%g3 + TRAP_ENT_F1]%asi
2664	stna	%g0, [%g3 + TRAP_ENT_F2]%asi
2665	stna	%g0, [%g3 + TRAP_ENT_F3]%asi
2666	stna	%g0, [%g3 + TRAP_ENT_F4]%asi
2667	TRACE_NEXT(%g3, %g4, %g5)
2668	jmp	%g7 + 4
2669	nop
2670
2671trace_win:
2672	TRACE_WIN_INFO(0, %l0, %l1, %l2)
2673	! Keep the locals as clean as possible, caller cleans %l4
2674	clr	%l2
2675	clr	%l1
2676	jmp	%l4 + 4
2677	  clr	%l0
2678
2679/*
2680 * Trace a tsb hit
2681 * g1 = tsbe pointer (in/clobbered)
2682 * g2 = tag access register (in)
2683 * g3 - g4 = scratch (clobbered)
2684 * g5 = tsbe data (in)
2685 * g6 = scratch (clobbered)
2686 * g7 = pc we jumped here from (in)
2687 */
2688
2689	! Do not disturb %g5, it will be used after the trace
2690	ALTENTRY(trace_tsbhit)
2691	TRACE_TSBHIT(0)
2692	jmp	%g7 + 4
2693	nop
2694
2695/*
2696 * Trace a TSB miss
2697 *
2698 * g1 = tsb8k pointer (in)
2699 * g2 = tag access register (in)
2700 * g3 = tsb4m pointer (in)
2701 * g4 = tsbe tag (in/clobbered)
2702 * g5 - g6 = scratch (clobbered)
2703 * g7 = pc we jumped here from (in)
2704 */
2705	.global	trace_tsbmiss
2706trace_tsbmiss:
2707	membar	#Sync
2708	sethi	%hi(FLUSH_ADDR), %g6
2709	flush	%g6
2710	TRACE_PTR(%g5, %g6)
2711	GET_TRACE_TICK(%g6)
2712	stxa	%g6, [%g5 + TRAP_ENT_TICK]%asi
2713	stna	%g2, [%g5 + TRAP_ENT_SP]%asi		! tag access
2714	stna	%g4, [%g5 + TRAP_ENT_F1]%asi		! XXX? tsb tag
2715	rdpr	%tnpc, %g6
2716	stna	%g6, [%g5 + TRAP_ENT_F2]%asi
2717	stna	%g1, [%g5 + TRAP_ENT_F3]%asi		! tsb8k pointer
2718	srlx	%g1, 32, %g6
2719	stna	%g6, [%g5 + TRAP_ENT_F4]%asi		! huh?
2720	rdpr	%tpc, %g6
2721	stna	%g6, [%g5 + TRAP_ENT_TPC]%asi
2722	TRACE_SAVE_TL_GL_REGS(%g5, %g6)
2723	rdpr	%tt, %g6
2724	or	%g6, TT_MMU_MISS, %g4
2725	stha	%g4, [%g5 + TRAP_ENT_TT]%asi
2726	mov	MMFSA_D_ADDR, %g4
2727	cmp	%g6, FAST_IMMU_MISS_TT
2728	move	%xcc, MMFSA_I_ADDR, %g4
2729	cmp	%g6, T_INSTR_MMU_MISS
2730	move	%xcc, MMFSA_I_ADDR, %g4
2731	MMU_FAULT_STATUS_AREA(%g6)
2732	ldx	[%g6 + %g4], %g6
2733	stxa	%g6, [%g5 + TRAP_ENT_TSTATE]%asi	! tag target
2734	stna	%g3, [%g5 + TRAP_ENT_TR]%asi		! tsb4m pointer
2735	TRACE_NEXT(%g5, %g4, %g6)
2736	jmp	%g7 + 4
2737	nop
2738
2739/*
2740 * g2 = tag access register (in)
2741 * g3 = ctx number (in)
2742 */
2743trace_dataprot:
2744	membar	#Sync
2745	sethi	%hi(FLUSH_ADDR), %g6
2746	flush	%g6
2747	TRACE_PTR(%g1, %g6)
2748	GET_TRACE_TICK(%g6)
2749	stxa	%g6, [%g1 + TRAP_ENT_TICK]%asi
2750	rdpr	%tpc, %g6
2751	stna	%g6, [%g1 + TRAP_ENT_TPC]%asi
2752	rdpr	%tstate, %g6
2753	stxa	%g6, [%g1 + TRAP_ENT_TSTATE]%asi
2754	stna	%g2, [%g1 + TRAP_ENT_SP]%asi		! tag access reg
2755	stna	%g0, [%g1 + TRAP_ENT_TR]%asi
2756	stna	%g0, [%g1 + TRAP_ENT_F1]%asi
2757	stna	%g0, [%g1 + TRAP_ENT_F2]%asi
2758	stna	%g0, [%g1 + TRAP_ENT_F3]%asi
2759	stna	%g0, [%g1 + TRAP_ENT_F4]%asi
2760	TRACE_SAVE_TL_GL_REGS(%g1, %g6)
2761	rdpr	%tt, %g6
2762	stha	%g6, [%g1 + TRAP_ENT_TT]%asi
2763	TRACE_NEXT(%g1, %g4, %g5)
2764	jmp	%g7 + 4
2765	nop
2766
2767#endif /* TRAPTRACE */
2768
2769/*
2770 * synthesize for trap(): SFAR in %g2, SFSR in %g3
2771 */
2772	.type	.dmmu_exc_lddf_not_aligned, #function
2773.dmmu_exc_lddf_not_aligned:
2774	MMU_FAULT_STATUS_AREA(%g3)
2775	ldx	[%g3 + MMFSA_D_ADDR], %g2
2776	/* Fault type not available in MMU fault status area */
2777	mov	MMFSA_F_UNALIGN, %g1
2778	ldx	[%g3 + MMFSA_D_CTX], %g3
2779	sllx	%g3, SFSR_CTX_SHIFT, %g3
2780	btst	1, %sp
2781	bnz,pt	%xcc, .lddf_exception_not_aligned
2782	or	%g3, %g1, %g3			/* SFSR */
2783	ba,a,pt	%xcc, .mmu_exception_not_aligned
2784	SET_SIZE(.dmmu_exc_lddf_not_aligned)
2785
2786/*
2787 * synthesize for trap(): SFAR in %g2, SFSR in %g3
2788 */
2789	.type	.dmmu_exc_stdf_not_aligned, #function
2790.dmmu_exc_stdf_not_aligned:
2791	MMU_FAULT_STATUS_AREA(%g3)
2792	ldx	[%g3 + MMFSA_D_ADDR], %g2
2793	/* Fault type not available in MMU fault status area */
2794	mov	MMFSA_F_UNALIGN, %g1
2795	ldx	[%g3 + MMFSA_D_CTX], %g3
2796	sllx	%g3, SFSR_CTX_SHIFT, %g3
2797	btst	1, %sp
2798	bnz,pt	%xcc, .stdf_exception_not_aligned
2799	or	%g3, %g1, %g3			/* SFSR */
2800	ba,a,pt	%xcc, .mmu_exception_not_aligned
2801	SET_SIZE(.dmmu_exc_stdf_not_aligned)
2802
2803	.type	.dmmu_exception, #function
2804.dmmu_exception:
2805	MMU_FAULT_STATUS_AREA(%g3)
2806	ldx	[%g3 + MMFSA_D_ADDR], %g2
2807	ldx	[%g3 + MMFSA_D_TYPE], %g1
2808	ldx	[%g3 + MMFSA_D_CTX], %g3
2809	srlx	%g2, MMU_PAGESHIFT, %g2		/* align address */
2810	sllx	%g2, MMU_PAGESHIFT, %g2
2811	or	%g2, %g3, %g2			/* TAG_ACCESS */
2812	sllx	%g3, SFSR_CTX_SHIFT, %g3
2813	or	%g3, %g1, %g3			/* SFSR */
2814	ba,pt	%xcc, .mmu_exception_end
2815	mov	T_DATA_EXCEPTION, %g1
2816	SET_SIZE(.dmmu_exception)
2817/*
2818 * expects offset into tsbmiss area in %g1 and return pc in %g7
2819 */
2820stat_mmu:
2821	CPU_INDEX(%g5, %g6)
2822	sethi	%hi(tsbmiss_area), %g6
2823	sllx	%g5, TSBMISS_SHIFT, %g5
2824	or	%g6, %lo(tsbmiss_area), %g6
2825	add	%g6, %g5, %g6		/* g6 = tsbmiss area */
2826	ld	[%g6 + %g1], %g5
2827	add	%g5, 1, %g5
2828	jmp	%g7 + 4
2829	st	%g5, [%g6 + %g1]
2830
2831
2832/*
2833 * fast_trap_done, fast_trap_done_chk_intr:
2834 *
2835 * Due to the design of UltraSPARC pipeline, pending interrupts are not
2836 * taken immediately after a RETRY or DONE instruction which causes IE to
2837 * go from 0 to 1. Instead, the instruction at %tpc or %tnpc is allowed
2838 * to execute first before taking any interrupts. If that instruction
2839 * results in other traps, and if the corresponding trap handler runs
2840 * entirely at TL=1 with interrupts disabled, then pending interrupts
2841 * won't be taken until after yet another instruction following the %tpc
2842 * or %tnpc.
2843 *
2844 * A malicious user program can use this feature to block out interrupts
2845 * for extended durations, which can result in send_mondo_timeout kernel
2846 * panic.
2847 *
2848 * This problem is addressed by servicing any pending interrupts via
2849 * sys_trap before returning back to the user mode from a fast trap
2850 * handler. The "done" instruction within a fast trap handler, which
2851 * runs entirely at TL=1 with interrupts disabled, is replaced with the
2852 * FAST_TRAP_DONE macro, which branches control to this fast_trap_done
2853 * entry point.
2854 *
2855 * We check for any pending interrupts here and force a sys_trap to
2856 * service those interrupts, if any. To minimize overhead, pending
2857 * interrupts are checked if the %tpc happens to be at 16K boundary,
2858 * which allows a malicious program to execute at most 4K consecutive
2859 * instructions before we service any pending interrupts. If a worst
2860 * case fast trap handler takes about 2 usec, then interrupts will be
2861 * blocked for at most 8 msec, less than a clock tick.
2862 *
2863 * For the cases where we don't know if the %tpc will cross a 16K
2864 * boundary, we can't use the above optimization and always process
2865 * any pending interrupts via fast_frap_done_chk_intr entry point.
2866 *
2867 * Entry Conditions:
2868 * 	%pstate		am:0 priv:1 ie:0
2869 * 			globals are AG (not normal globals)
2870 */
2871
2872	.global	fast_trap_done, fast_trap_done_chk_intr
2873fast_trap_done:
2874	rdpr	%tpc, %g5
2875	sethi	%hi(0xffffc000), %g6	! 1's complement of 0x3fff
2876	andncc	%g5, %g6, %g0		! check lower 14 bits of %tpc
2877	bz,pn	%icc, 1f		! branch if zero (lower 32 bits only)
2878	nop
2879	done
2880
2881fast_trap_done_chk_intr:
28821:	rd	SOFTINT, %g6
2883	brnz,pn	%g6, 2f		! branch if any pending intr
2884	nop
2885	done
2886
28872:
2888	/*
2889	 * We get here if there are any pending interrupts.
2890	 * Adjust %tpc/%tnpc as we'll be resuming via "retry"
2891	 * instruction.
2892	 */
2893	rdpr	%tnpc, %g5
2894	wrpr	%g0, %g5, %tpc
2895	add	%g5, 4, %g5
2896	wrpr	%g0, %g5, %tnpc
2897
2898	/*
2899	 * Force a dummy sys_trap call so that interrupts can be serviced.
2900	 */
2901	set	fast_trap_dummy_call, %g1
2902	ba,pt	%xcc, sys_trap
2903	  mov	-1, %g4
2904
2905fast_trap_dummy_call:
2906	retl
2907	nop
2908
2909#endif	/* lint */
2910