]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/xpr.c
xnu-517.3.15.tar.gz
[apple/xnu.git] / osfmk / kern / xpr.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * @OSF_COPYRIGHT@
27 */
28/*
29 * HISTORY
30 *
31 * Revision 1.1.1.1 1998/09/22 21:05:32 wsanchez
32 * Import of Mac OS X kernel (~semeria)
33 *
34 * Revision 1.1.1.1 1998/03/07 02:25:57 wsanchez
35 * Import of OSF Mach kernel (~mburg)
36 *
37 * Revision 1.2.46.1 1997/09/22 17:41:21 barbou
38 * MP+RT: protect cpu_number() usage against preemption.
39 * [97/09/16 barbou]
40 *
41 * Revision 1.2.25.5 1996/07/31 09:56:06 paire
42 * Merged with nmk20b7_shared (1.2.41.1)
43 * [96/06/10 paire]
44 *
45 * Revision 1.2.41.1 1996/04/15 14:35:12 bernadat
46 * Keep interrupts disabled while accessing XPR_TIMESTAMP.
47 * [96/04/12 bernadat]
48 *
49 * Revision 1.2.25.4 1995/02/24 15:22:42 alanl
50 * DIPC: Merge from nmk17b2 to nmk18b8.
51 * Notes: Restore portable locks package, derived from nmk17b2.
52 * [95/02/07 alanl]
53 *
54 * Revision 1.2.28.3 1994/12/09 22:25:16 dwm
55 * mk6 CR801 - merge up from nmk18b4 to nmk18b7
56 * * Rev 1.2.25.2 1994/10/21 18:30:41 joe
57 * Added ETAP support
58 * [1994/12/09 21:10:59 dwm]
59 *
60 * Revision 1.2.28.2 1994/11/10 06:15:29 dwm
61 * mk6 CR764 - s/spinlock/simple_lock/ (name change only)
62 * [1994/11/10 05:58:48 dwm]
63 *
64 * Revision 1.2.28.1 1994/11/04 10:10:56 dwm
65 * mk6 CR668 - 1.3b26 merge
66 * * Revision 1.2.5.7 1994/05/06 18:54:13 tmt
67 * Merge in DEC Alpha changes to osc1.3b19.
68 * include <mach/machine/vm_types.h>
69 * 64 bit cleanup.
70 * * End1.3merge
71 * [1994/11/04 09:39:17 dwm]
72 *
73 * Revision 1.2.25.1 1994/09/23 02:32:39 ezf
74 * change marker to not FREE
75 * [1994/09/22 21:38:29 ezf]
76 *
77 * Revision 1.2.22.1 1994/06/09 14:14:11 dswartz
78 * Preemption merge.
79 * [1994/06/09 14:08:38 dswartz]
80 *
81 * Revision 1.2.5.5 1993/08/12 20:16:51 bernard
82 * Last pass for ANSI prototypes - CR#9523
83 * [1993/08/12 15:43:24 bernard]
84 *
85 * Revision 1.2.5.4 1993/08/02 17:26:05 rod
86 * ANSI prototypes: zap explicit include of machine/setjmp.h. CR #9523.
87 * [1993/08/01 13:36:31 rod]
88 *
89 * Revision 1.2.5.3 1993/07/27 18:09:05 rod
90 * Add ANSI prototypes. CR #9523.
91 * [1993/07/27 14:33:23 rod]
92 *
93 * Revision 1.2.5.2 1993/06/09 02:39:13 gm
94 * CR9176 - ANSI C violations: trailing tokens on CPP
95 * directives, extra semicolons after decl_ ..., asm keywords
96 * [1993/06/07 19:07:51 jeffc]
97 *
98 * Added to OSF/1 R1.3 from NMK15.0.
99 * [1993/06/02 21:15:13 jeffc]
100 *
101 * Revision 1.2 1993/04/19 16:31:21 devrcs
102 * Added void to fcns that still needed it.
103 * [93/02/05 bruel]
104 *
105 * Revision 1.1 1992/09/30 02:10:39 robert
106 * Initial revision
107 *
108 * $EndLog$
109 */
110/* CMU_HIST */
111/*
112 * Revision 2.9.5.2 92/03/03 16:20:57 jeffreyh
113 * Fix Log.
114 * [92/02/24 13:24:44 jeffreyh]
115 *
116 * Revision 2.9.5.1 92/02/18 19:13:03 jeffreyh
117 * Added an xpr_search function to which you can give
118 * a selection function.
119 * [92/02/11 08:13:23 bernadat]
120 *
121 * Revision 2.9.4.1 92/02/13 18:53:47 jeffreyh
122 * Added an xpr_search function to which you can give
123 * a selection function.
124 * [92/02/11 08:13:23 bernadat]
125 *
126 * Revision 2.9.3.1 92/02/11 17:19:59 jeffreyh
127 * Added an xpr_search function to which you can give
128 * a selection function.
129 * [92/02/11 08:13:23 bernadat]
130 *
131 * Revision 2.9.2.1 92/02/11 08:13:23 bernadat
132 * Added an xpr_search function to which you can give
133 * a selection function.
134 *
135 *
136 * Revision 2.9 91/10/09 16:11:50 af
137 * Removed xpr_save. Modified xpr_dump to make it useful
138 * for dumping xpr buffers in user space tasks.
139 * [91/09/20 rpd]
140 *
141 * Turned on xprenable by default. xprbootstrap now preserves
142 * the original contents of the buffer if xprenable is off.
143 * [91/09/18 rpd]
144 *
145 * Revision 2.8 91/08/28 11:14:56 jsb
146 * Fixed xprbootstrap to zero the allocate memory.
147 * [91/08/18 rpd]
148 *
149 * Revision 2.7 91/05/18 14:34:37 rpd
150 * Added xprenable and other minor changes so that the xpr buffer
151 * may be examined after a spontaneous reboot.
152 * [91/05/03 rpd]
153 * Fixed the initialization check in xpr.
154 * Fixed xpr_dump.
155 * [91/04/02 rpd]
156 *
157 * Revision 2.6 91/05/14 16:50:09 mrt
158 * Correcting copyright
159 *
160 * Revision 2.5 91/03/16 14:53:24 rpd
161 * Updated for new kmem_alloc interface.
162 * [91/03/03 rpd]
163 *
164 * Revision 2.4 91/02/05 17:31:13 mrt
165 * Changed to new Mach copyright
166 * [91/02/01 16:21:17 mrt]
167 *
168 * Revision 2.3 90/09/09 14:33:04 rpd
169 * Use decl_simple_lock_data.
170 * [90/08/30 rpd]
171 *
172 * Revision 2.2 89/11/29 14:09:21 af
173 * Added xpr_dump() to print on console the content of the buffer,
174 * only valid for KDB usage.
175 * [89/11/12 af]
176 *
177 * MACH_KERNEL: include sys/cpu_number.h instead of machine/cpu.h.
178 * Clean up comments.
179 * [88/12/19 dbg]
180 *
181 * Revision 2.1 89/08/03 15:49:11 rwd
182 * Created.
183 *
184 * Revision 2.2 88/12/19 02:48:30 mwyoung
185 * Fix include file references.
186 * [88/11/22 02:17:01 mwyoung]
187 *
188 * Separate initialization into two phases.
189 * [88/11/22 01:13:11 mwyoung]
190 *
191 * 6-Jan-88 Michael Young (mwyoung) at Carnegie-Mellon University
192 * Eliminate use of arg6 in order to allow a more shapely event structure.
193 *
194 * 30-Dec-87 David Golub (dbg) at Carnegie-Mellon University
195 * Delinted.
196 *
197 * 7-Dec-87 Richard Sanzi (sanzi) at Carnegie-Mellon University
198 * Added xpr_save() routine.
199 *
200 */
201/* CMU_ENDHIST */
202/*
203 * Mach Operating System
204 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
205 * All Rights Reserved.
206 *
207 * Permission to use, copy, modify and distribute this software and its
208 * documentation is hereby granted, provided that both the copyright
209 * notice and this permission notice appear in all copies of the
210 * software, derivative works or modified versions, and any portions
211 * thereof, and that both notices appear in supporting documentation.
212 *
213 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
214 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
215 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
216 *
217 * Carnegie Mellon requests users of this software to return to
218 *
219 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
220 * School of Computer Science
221 * Carnegie Mellon University
222 * Pittsburgh PA 15213-3890
223 *
224 * any improvements or extensions that they make and grant Carnegie Mellon
225 * the rights to redistribute these changes.
226 */
227#include <mach_kdb.h>
228/*
229 * xpr silent tracing circular buffer.
230 */
231
232#include <cpus.h>
233
234#include <mach/machine/vm_types.h>
235#include <kern/xpr.h>
236#include <kern/lock.h>
237#include <kern/spl.h>
238#include <kern/cpu_number.h>
239#include <kern/misc_protos.h>
240#include <kern/thread.h>
241#include <vm/vm_kern.h>
242#include <string.h>
243
244/*
245 * After a spontaneous reboot, it is desirable to look
246 * at the old xpr buffer. Assuming xprbootstrap allocates
247 * the buffer in the same place in physical memory and
248 * the reboot doesn't clear memory, this should work.
249 * xprptr will be reset, but the saved value should be OK.
250 * Just set xprenable false so the buffer isn't overwritten.
251 */
252
253decl_simple_lock_data(,xprlock)
254boolean_t xprenable = TRUE; /* Enable xpr tracing */
255int nxprbufs = 0; /* Number of contiguous xprbufs allocated */
256int xprflags = 0; /* Bit mask of xpr flags enabled */
257struct xprbuf *xprbase; /* Pointer to circular buffer nxprbufs*sizeof(xprbuf)*/
258struct xprbuf *xprptr; /* Currently allocated xprbuf */
259struct xprbuf *xprlast; /* Pointer to end of circular buffer */
260
261void
262xpr(
263 char *msg,
264 long arg1,
265 long arg2,
266 long arg3,
267 long arg4,
268 long arg5)
269{
270 spl_t s;
271 register struct xprbuf *x;
272
273 /* If we aren't initialized, ignore trace request */
274 if (!xprenable || (xprptr == 0))
275 return;
276 /* Guard against all interrupts and allocate next buffer. */
277
278 s = splhigh();
279 simple_lock(&xprlock);
280 x = xprptr++;
281 if (xprptr >= xprlast) {
282 /* wrap around */
283 xprptr = xprbase;
284 }
285 /* Save xprptr in allocated memory. */
286 *(struct xprbuf **)xprlast = xprptr;
287 simple_unlock(&xprlock);
288 x->timestamp = XPR_TIMESTAMP;
289 splx(s);
290 x->msg = msg;
291 x->arg1 = arg1;
292 x->arg2 = arg2;
293 x->arg3 = arg3;
294 x->arg4 = arg4;
295 x->arg5 = arg5;
296 mp_disable_preemption();
297 x->cpuinfo = cpu_number();
298 mp_enable_preemption();
299}
300
301void
302xprbootstrap(void)
303{
304 vm_offset_t addr;
305 vm_size_t size;
306 kern_return_t kr;
307
308 simple_lock_init(&xprlock, ETAP_MISC_XPR);
309 if (nxprbufs == 0)
310 return; /* assume XPR support not desired */
311
312 /* leave room at the end for a saved copy of xprptr */
313 size = nxprbufs * sizeof(struct xprbuf) + sizeof xprptr;
314
315 kr = kmem_alloc_wired(kernel_map, &addr, size);
316 if (kr != KERN_SUCCESS)
317 panic("xprbootstrap");
318
319 if (xprenable) {
320 /*
321 * If xprenable is set (the default) then we zero
322 * the buffer so xpr_dump doesn't encounter bad pointers.
323 * If xprenable isn't set, then we preserve
324 * the original contents of the buffer. This is useful
325 * if memory survives reboots, so xpr_dump can show
326 * the previous buffer contents.
327 */
328
329 (void) memset((void *) addr, 0, size);
330 }
331
332 xprbase = (struct xprbuf *) addr;
333 xprlast = &xprbase[nxprbufs];
334 xprptr = xprbase; /* setting xprptr enables tracing */
335}
336
337int xprinitial = 0;
338
339void
340xprinit(void)
341{
342 xprflags |= xprinitial;
343}
344
345#if MACH_KDB
346#include <ddb/db_output.h>
347
348/*
349 * Prototypes for functions called from the debugger
350 */
351void
352xpr_dump(
353 struct xprbuf *base,
354 int nbufs);
355
356void
357xpr_search(
358 int arg_index,
359 int value);
360
361extern jmp_buf_t *db_recover;
362
363/*
364 * Print current content of xpr buffers (KDB's sake)
365 * Use stack order to make it understandable.
366 *
367 * Called as "!xpr_dump" this dumps the kernel's xpr buffer.
368 * Called with arguments, it can dump xpr buffers in user tasks,
369 * assuming they use the same format as the kernel.
370 */
371void
372xpr_dump(
373 struct xprbuf *base,
374 int nbufs)
375{
376 jmp_buf_t db_jmpbuf;
377 jmp_buf_t *prev;
378 struct xprbuf *last, *ptr;
379 register struct xprbuf *x;
380 int i;
381 spl_t s;
382
383 if (base == 0) {
384 base = xprbase;
385 nbufs = nxprbufs;
386 }
387
388 if (nbufs == 0)
389 return;
390
391 if (base == xprbase) {
392 s = splhigh();
393 simple_lock(&xprlock);
394 }
395
396 last = base + nbufs;
397 ptr = * (struct xprbuf **) last;
398
399 prev = db_recover;
400 if (_setjmp(db_recover = &db_jmpbuf) == 0)
401 for (x = ptr, i = 0; i < nbufs; i++) {
402 if (--x < base)
403 x = last - 1;
404
405 if (x->msg == 0)
406 break;
407
408 db_printf("<%d:%x:%x> ", x - base, x->cpuinfo, x->timestamp);
409 db_printf(x->msg, x->arg1,x->arg2,x->arg3,x->arg4,x->arg5);
410 }
411 db_recover = prev;
412
413 if (base == xprbase) {
414 simple_unlock(&xprlock);
415 splx(s);
416 }
417}
418
419/*
420 * dump xpr table with a selection criteria.
421 * argument number "arg_index" must equal "value"
422 */
423
424void
425xpr_search(
426 int arg_index,
427 int value)
428{
429 jmp_buf_t db_jmpbuf;
430 jmp_buf_t *prev;
431 register struct xprbuf *x;
432 spl_t s;
433 int n;
434
435 if (!nxprbufs)
436 return;
437
438 n = nxprbufs;
439
440 s = splhigh();
441 simple_lock(&xprlock);
442
443 prev = db_recover;
444 if (_setjmp(db_recover = &db_jmpbuf) == 0)
445 for (x = *(struct xprbuf **)xprlast ; n--; ) {
446 if (--x < xprbase)
447 x = xprlast - 1;
448
449 if (x->msg == 0) {
450 break;
451 }
452
453 if (*((&x->arg1)+arg_index) != value)
454 continue;
455
456 db_printf("<%d:%d:%x> ", x - xprbase,
457 x->cpuinfo, x->timestamp);
458 db_printf(x->msg, x->arg1,x->arg2,x->arg3,x->arg4,x->arg5);
459 }
460 db_recover = prev;
461
462 simple_unlock(&xprlock);
463 splx(s);
464}
465#endif /* MACH_KDB */