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