xref: /titanic_50/usr/src/uts/sun4u/sys/traptrace.h (revision 884be64dd8e3b9a5e39540ec90da0a94008f4eb1)
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 #ifndef _SYS_TRAPTRACE_H
28 #define	_SYS_TRAPTRACE_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 /*
37  * Trap tracing. If TRAPTRACE is defined, every trap records info
38  * in a circular buffer.  Define TRAPTRACE in Makefile.$ARCH.
39  *
40  * Trap trace records are TRAP_ENT_SIZE bytes, consisting of the
41  * %tick, %tl, %tt, %tpc, %tstate, %sp, and a few other words:
42  *
43  * struct trap_trace_record {
44  *	ushort_t tl, tt;
45  *	long	pc;
46  *	int64_t	tstate, tick;
47  *	long	sp, tr, f1, f2, f3, f4;
48  * };
49  *
50  * Note that for UltraSparc III and beyond %stick is used in place of %tick
51  * unless compiled with TRAPTRACE_FORCE_TICK.
52  *
53  * Auxilliary entries (not of just a trap), have obvious non-%tt values in
54  * the TRAP_ENT_TT field
55  */
56 
57 #define	TRAP_TPGS	(2 * PAGESIZE)	/* default size is two pages */
58 
59 #ifndef	_ASM
60 
61 struct trap_trace_record {
62 	uint16_t	tt_tl;
63 	uint16_t	tt_tt;
64 	uintptr_t	tt_tpc;
65 	uint64_t	tt_tstate;
66 	uint64_t	tt_tick;
67 	uintptr_t	tt_sp;
68 	uintptr_t	tt_tr;
69 	uintptr_t	tt_f1;
70 	uintptr_t	tt_f2;
71 	uintptr_t	tt_f3;
72 	uintptr_t	tt_f4;
73 };
74 
75 #define	TRAP_TSIZE	((TRAP_TPGS / sizeof (struct trap_trace_record)) * \
76 			sizeof (struct trap_trace_record))
77 
78 #else
79 
80 #define	TRAP_TSIZE	((TRAP_TPGS / TRAP_ENT_SIZE) * TRAP_ENT_SIZE)
81 
82 #endif
83 
84 #define	HTRAP_TSIZE	0
85 
86 /*
87  * Trap tracing buffer header.
88  */
89 
90 #ifndef	_ASM
91 
92 /*
93  * Example buffer header stored in locore.s:
94  *
95  * (the actual implementation could be .skip TRAPTR_SIZE*NCPU)
96  */
97 typedef union {
98     struct {
99 	caddr_t		vaddr_base;	/* virtual address of top of buffer */
100 	uint64_t	paddr_base;	/* physical address of buffer */
101 	uint_t		last_offset;	/* to "know" what trace completed */
102 	uint_t		offset;		/* current index into buffer (bytes) */
103 	uint_t		limit;		/* upper limit on index */
104 	uchar_t		asi;		/* cache for real asi */
105 	} d;
106     char		cache_linesize[64];
107 } TRAP_TRACE_CTL;
108 
109 #ifdef _KERNEL
110 
111 extern TRAP_TRACE_CTL	trap_trace_ctl[];	/* allocated in locore.s */
112 extern int		trap_trace_bufsize;	/* default buffer size */
113 extern char		trap_tr0[];		/* prealloc buf for boot cpu */
114 extern int		trap_freeze;		/* freeze the trap trace */
115 extern caddr_t		ttrace_buf;		/* buffer bop alloced */
116 extern int		ttrace_index;		/* index used */
117 extern caddr_t		trap_trace_alloc(caddr_t);
118 extern void mach_htraptrace_setup(int);
119 extern void mach_htraptrace_configure(int);
120 extern void mach_htraptrace_init(void);
121 extern void mach_htraptrace_cleanup(int);
122 
123 #endif
124 
125 /*
126  * freeze the trap trace
127  */
128 #define	TRAPTRACE_FREEZE	trap_freeze = 1;
129 #define	TRAPTRACE_UNFREEZE	trap_freeze = 0;
130 
131 #else /* _ASM */
132 
133 #include <sys/machthread.h>
134 
135 /*
136  * Offsets of words in trap_trace_ctl:
137  */
138 /*
139  * XXX This should be done with genassym
140  */
141 #define	TRAPTR_VBASE	0		/* virtual address of buffer */
142 #define	TRAPTR_LAST_OFFSET 16		/* last completed trace entry */
143 #define	TRAPTR_OFFSET	20		/* next trace entry pointer */
144 #define	TRAPTR_LIMIT	24		/* pointer past end of buffer */
145 #define	TRAPTR_PBASE	8		/* start of buffer */
146 #define	TRAPTR_ASIBUF	28		/* cache of current asi */
147 #define	TRAPTR_SIZE_SHIFT	6	/* shift count -- per CPU indexing */
148 #define	TRAPTR_SIZE		(1<<TRAPTR_SIZE_SHIFT)
149 
150 #define	TRAPTR_ASI	ASI_MEM		/* ASI to use for TRAPTR access */
151 
152 /*
153  * Use new %stick register for UltraSparc III and beyond for
154  * sane debugging of mixed speed CPU systems. Use TRAPTRACE_FORCE_TICK
155  * for finer granularity on same speed systems.
156  *
157  * Note the label-less branches used due to contraints of where
158  * and when trap trace macros are used.
159  */
160 #ifdef	TRAPTRACE_FORCE_TICK
161 #define	GET_TRACE_TICK(reg)				\
162 	rdpr	%tick, reg;
163 #else
164 #define	GET_TRACE_TICK(reg)				\
165 	sethi	%hi(traptrace_use_stick), reg;		\
166 	lduw	[reg + %lo(traptrace_use_stick)], reg;	\
167 	/* CSTYLED */					\
168         brz,a	reg, .+12;				\
169 	rdpr	%tick, reg;				\
170 	rd	%asr24, reg;
171 #endif
172 
173 /*
174  * TRACE_PTR(ptr, scr1) - get trap trace entry physical pointer.
175  *	ptr is the register to receive the trace pointer.
176  *	scr1 is a different register to be used as scratch.
177  * TRACING now needs a known processor state.  Hence the assertion.
178  *	NOTE: this caches and resets %asi
179  */
180 #define	TRACE_PTR(ptr, scr1)				\
181 	sethi	%hi(trap_freeze), ptr;			\
182 	ld	[ptr + %lo(trap_freeze)], ptr;		\
183 	/* CSTYLED */					\
184 	brnz,pn	ptr, .+20; /* skip assertion */		\
185 	rdpr	%pstate, scr1;				\
186 	andcc	scr1, PSTATE_IE | PSTATE_AM, scr1;	\
187 	/* CSTYLED */					\
188 	bne,a,pn %icc, trace_ptr_panic;			\
189 	rd	%pc, %g1;				\
190 	CPU_INDEX(scr1, ptr);				\
191 	sll	scr1, TRAPTR_SIZE_SHIFT, scr1;		\
192 	set	trap_trace_ctl, ptr; 			\
193 	add	ptr, scr1, scr1;			\
194 	rd	%asi, ptr;				\
195 	stb	ptr, [scr1 + TRAPTR_ASIBUF];		\
196 	sethi	%hi(trap_freeze), ptr;			\
197 	ld	[ptr + %lo(trap_freeze)], ptr;		\
198 	/* CSTYLED */					\
199 	brnz,pn	ptr, .+20; /* skip assertion */		\
200 	ld	[scr1 + TRAPTR_LIMIT], ptr;		\
201 	tst	ptr;					\
202 	/* CSTYLED */					\
203 	be,a,pn	%icc, trace_ptr_panic;			\
204 	rd	%pc, %g1;				\
205 	ldx	[scr1 + TRAPTR_PBASE], ptr;		\
206 	ld	[scr1 + TRAPTR_OFFSET], scr1;		\
207 	wr	%g0, TRAPTR_ASI, %asi;			\
208 	add	ptr, scr1, ptr;
209 
210 /*
211  * TRACE_NEXT(scr1, scr2, scr3) - advance the trap trace pointer.
212  *	scr1, scr2, scr3 are scratch registers.
213  *	This routine will skip updating the trap pointers if the
214  *	global freeze register is set (e.g. in panic).
215  *	(we also restore the asi register)
216  */
217 #define	TRACE_NEXT(scr1, scr2, scr3)			\
218 	CPU_INDEX(scr2, scr1);				\
219 	sll	scr2, TRAPTR_SIZE_SHIFT, scr2;		\
220 	set	trap_trace_ctl, scr1; 			\
221 	add	scr1, scr2, scr2;			\
222 	ldub	[scr2 + TRAPTR_ASIBUF], scr1;		\
223 	wr	%g0, scr1, %asi;			\
224 	sethi	%hi(trap_freeze), scr1;			\
225 	ld	[scr1 + %lo(trap_freeze)], scr1;	\
226 	/* CSTYLED */					\
227 	brnz	scr1, .+36; /* skip update on freeze */	\
228 	ld	[scr2 + TRAPTR_OFFSET], scr1;		\
229 	ld	[scr2 + TRAPTR_LIMIT], scr3;		\
230 	st	scr1, [scr2 + TRAPTR_LAST_OFFSET];	\
231 	add	scr1, TRAP_ENT_SIZE, scr1;		\
232 	sub	scr3, TRAP_ENT_SIZE, scr3;		\
233 	cmp	scr1, scr3;				\
234 	movge	%icc, 0, scr1;				\
235 	st	scr1, [scr2 + TRAPTR_OFFSET];
236 
237 /*
238  * macro to save %tl to trap trace record at addr
239  */
240 #define	TRACE_SAVE_TL_GL_REGS(addr, scr1)		\
241 	rdpr	%tl, scr1;				\
242 	stha	scr1, [addr + TRAP_ENT_TL]%asi
243 
244 /*
245  * macro to save tl to trap trace record at addr
246  */
247 #define	TRACE_SAVE_TL_VAL(addr, tl)			\
248 	stha	tl, [addr + TRAP_ENT_TL]%asi
249 
250 /*
251  * dummy macro
252  */
253 #define	TRACE_SAVE_GL_VAL(addr, gl)
254 
255 
256 /*
257  * Trace macro for sys_trap return entries:
258  *	prom_rtt, priv_rtt, and user_rtt
259  *	%l7 - regs
260  *	%l6 - trap %pil for prom_rtt and priv_rtt; THREAD_REG for user_rtt
261  */
262 #define	TRACE_RTT(code, scr1, scr2, scr3, scr4)		\
263 	rdpr	%pstate, scr4;				\
264 	andn	scr4, PSTATE_IE | PSTATE_AM, scr3;	\
265 	wrpr	%g0, scr3, %pstate;			\
266 	TRACE_PTR(scr1, scr2);				\
267 	GET_TRACE_TICK(scr2);				\
268 	stxa	scr2, [scr1 + TRAP_ENT_TICK]%asi;	\
269 	rdpr	%tl, scr2;				\
270 	stha	scr2, [scr1 + TRAP_ENT_TL]%asi;		\
271 	set	code, scr2;				\
272 	stha	scr2, [scr1 + TRAP_ENT_TT]%asi;		\
273 	ldn	[%l7 + PC_OFF], scr2;			\
274 	stna	scr2, [scr1 + TRAP_ENT_TPC]%asi;	\
275 	ldx	[%l7 + TSTATE_OFF], scr2;		\
276 	stxa	scr2, [scr1 + TRAP_ENT_TSTATE]%asi;	\
277 	stna	%sp, [scr1 + TRAP_ENT_SP]%asi;		\
278 	stna	%l6, [scr1 + TRAP_ENT_TR]%asi;		\
279 	stna	%l7, [scr1 + TRAP_ENT_F1]%asi;		\
280 	ldn	[THREAD_REG + T_CPU], scr2;		\
281 	ld	[scr2 + CPU_BASE_SPL], scr2;		\
282 	stna	scr2, [scr1 + TRAP_ENT_F2]%asi;		\
283 	mov	MMU_SCONTEXT, scr2;			\
284 	ldxa	[scr2]ASI_DMMU, scr2;			\
285 	stna	scr2, [scr1 + TRAP_ENT_F3]%asi;		\
286 	rdpr	%cwp, scr2;				\
287 	stna	scr2, [scr1 + TRAP_ENT_F4]%asi;		\
288 	TRACE_NEXT(scr1, scr2, scr3);			\
289 	wrpr	%g0, scr4, %pstate
290 
291 /*
292  * Trace macro for spill and fill trap handlers
293  *	tl and tt fields indicate which spill handler is entered
294  */
295 #define	TRACE_WIN_INFO(code, scr1, scr2, scr3)		\
296 	TRACE_PTR(scr1, scr2);				\
297 	GET_TRACE_TICK(scr2);				\
298 	stxa	scr2, [scr1 + TRAP_ENT_TICK]%asi;	\
299 	rdpr	%tl, scr2;				\
300 	stha	scr2, [scr1 + TRAP_ENT_TL]%asi;		\
301 	rdpr	%tt, scr2;				\
302 	set	code, scr3;				\
303 	or	scr2, scr3, scr2;			\
304 	stha	scr2, [scr1 + TRAP_ENT_TT]%asi;		\
305 	rdpr	%tstate, scr2;				\
306 	stxa	scr2, [scr1 + TRAP_ENT_TSTATE]%asi;	\
307 	stna	%sp, [scr1 + TRAP_ENT_SP]%asi;		\
308 	rdpr	%tpc, scr2;				\
309 	stna	scr2, [scr1 + TRAP_ENT_TPC]%asi;	\
310 	set	TT_FSPILL_DEBUG, scr2;			\
311 	stna	scr2, [scr1 + TRAP_ENT_TR]%asi;		\
312 	rdpr	%pstate, scr2;				\
313 	stna	scr2, [scr1 + TRAP_ENT_F1]%asi;		\
314 	rdpr	%cwp, scr2;				\
315 	sll	scr2, 24, scr2;				\
316 	rdpr	%cansave, scr3;				\
317 	sll	scr3, 16, scr3;				\
318 	or	scr2, scr3, scr2;			\
319 	rdpr	%canrestore, scr3;			\
320 	or	scr2, scr3, scr2;			\
321 	stna	scr2, [scr1 + TRAP_ENT_F2]%asi;		\
322 	rdpr	%otherwin, scr2;			\
323 	sll	scr2, 24, scr2;				\
324 	rdpr	%cleanwin, scr3;			\
325 	sll	scr3, 16, scr3;				\
326 	or	scr2, scr3, scr2;			\
327 	rdpr	%wstate, scr3;				\
328 	or	scr2, scr3, scr2;			\
329 	stna	scr2, [scr1 + TRAP_ENT_F3]%asi;		\
330 	stna	%o7, [scr1 + TRAP_ENT_F4]%asi;		\
331 	TRACE_NEXT(scr1, scr2, scr3)
332 
333 #ifdef TRAPTRACE
334 
335 #define	FAULT_WINTRACE(scr1, scr2, scr3, type)		\
336 	TRACE_PTR(scr1, scr2);				\
337 	GET_TRACE_TICK(scr2);				\
338 	stxa	scr2, [scr1 + TRAP_ENT_TICK]%asi;	\
339 	rdpr	%tl, scr2;				\
340 	stha	scr2, [scr1 + TRAP_ENT_TL]%asi;		\
341 	set	type, scr2;				\
342 	stha	scr2, [scr1 + TRAP_ENT_TT]%asi;		\
343 	rdpr	%tpc, scr2;				\
344 	stna	scr2, [scr1 + TRAP_ENT_TPC]%asi;	\
345 	rdpr	%tstate, scr2;				\
346 	stxa	scr2, [scr1 + TRAP_ENT_TSTATE]%asi;	\
347 	stna	%sp, [scr1 + TRAP_ENT_SP]%asi;		\
348 	stna	%g0, [scr1 + TRAP_ENT_TR]%asi;		\
349 	stna	%g0, [scr1 + TRAP_ENT_F1]%asi;		\
350 	stna	%g4, [scr1 + TRAP_ENT_F2]%asi;		\
351 	rdpr	%pil, scr2;				\
352 	stna	scr2, [scr1 + TRAP_ENT_F3]%asi;		\
353 	stna	%g0, [scr1 + TRAP_ENT_F4]%asi;		\
354 	TRACE_NEXT(scr1, scr2, scr3)
355 
356 #define	SYSTRAP_TT	0x1300
357 
358 #define	SYSTRAP_TRACE(scr1, scr2, scr3)			\
359 	TRACE_PTR(scr1, scr2);				\
360 	GET_TRACE_TICK(scr2);				\
361 	stxa	scr2, [scr1 + TRAP_ENT_TICK]%asi;	\
362 	rdpr	%tl, scr2;				\
363 	stha	scr2, [scr1 + TRAP_ENT_TL]%asi;		\
364 	set	SYSTRAP_TT, scr3;			\
365 	rdpr	%tt, scr2;				\
366 	or	scr3, scr2, scr2;			\
367 	stha	scr2, [scr1 + TRAP_ENT_TT]%asi;		\
368 	rdpr	%tpc, scr2;				\
369 	stna	scr2, [scr1 + TRAP_ENT_TPC]%asi;	\
370 	rdpr	%tstate, scr2;				\
371 	stxa	scr2, [scr1 + TRAP_ENT_TSTATE]%asi;	\
372 	stna	%g1, [scr1 + TRAP_ENT_SP]%asi;		\
373 	stna	%g2, [scr1 + TRAP_ENT_TR]%asi;		\
374 	stna	%g3, [scr1 + TRAP_ENT_F1]%asi;		\
375 	stna	%g4, [scr1 + TRAP_ENT_F2]%asi;		\
376 	rdpr	%pil, scr2;				\
377 	stna	scr2, [scr1 + TRAP_ENT_F3]%asi;		\
378 	rdpr	%cwp, scr2;				\
379 	stna	scr2, [scr1 + TRAP_ENT_F4]%asi;		\
380 	TRACE_NEXT(scr1, scr2, scr3)
381 
382 #else /* TRAPTRACE */
383 
384 #define	FAULT_WINTRACE(scr1, scr2, scr3, type)
385 #define	SYSTRAP_TRACE(scr1, scr2, scr3)
386 
387 #endif /* TRAPTRACE */
388 
389 #endif	/* _ASM */
390 
391 /*
392  * Trap trace codes used in place of a %tbr value when more than one
393  * entry is made by a trap.  The general scheme is that the trap-type is
394  * in the same position as in the TT, and the low-order bits indicate
395  * which precise entry is being made.
396  */
397 
398 #define	TT_F32_SN0	0x1084
399 #define	TT_F64_SN0	0x1088
400 #define	TT_F32_NT0	0x1094
401 #define	TT_F64_NT0	0x1098
402 #define	TT_F32_SO0	0x10A4
403 #define	TT_F64_SO0	0x10A8
404 #define	TT_F32_FN0	0x10C4
405 #define	TT_F64_FN0	0x10C8
406 #define	TT_F32_SN1	0x1284
407 #define	TT_F64_SN1	0x1288
408 #define	TT_F32_NT1	0x1294
409 #define	TT_F64_NT1	0x1298
410 #define	TT_F32_SO1	0x12A4
411 #define	TT_F64_SO1	0x12A8
412 #define	TT_F32_FN1	0x12C4
413 #define	TT_F64_FN1	0x12C8
414 
415 #define	TT_SC_ENTR	0x880	/* enter system call */
416 #define	TT_SC_RET	0x881	/* system call normal return */
417 
418 #define	TT_SYS_RTT_PROM	0x5555	/* return from trap to prom */
419 #define	TT_SYS_RTT_PRIV	0x6666	/* return from trap to privilege */
420 #define	TT_SYS_RTT_USER	0x7777	/* return from trap to user */
421 
422 #define	TT_INTR_EXIT	0x8888	/* interrupt thread exit (no pinned thread) */
423 #define	TT_FSPILL_DEBUG	0x9999	/* fill/spill debugging */
424 
425 #define	TT_SERVE_INTR	0x6000	/* SERVE_INTR */
426 #define	TT_XCALL	0xd000	/* xcall/xtrap */
427 #define	TT_XCALL_CONT	0xdc00	/* continuation of an xcall/xtrap record */
428 
429 #define	TT_MMU_MISS	0x200	/* or'd into %tt to indicate a miss */
430 #define	TT_SPURIOUS_INT	0x400	/* or'd into %tt for spurious intr. */
431 
432 
433 #ifdef	__cplusplus
434 }
435 #endif
436 
437 #endif	/* _SYS_TRAPTRACE_H */
438