]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/xpr.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / osfmk / kern / xpr.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 */
58 #include <mach_kdb.h>
59 /*
60 * xpr silent tracing circular buffer.
61 */
62
63 #include <mach/machine/vm_types.h>
64 #include <kern/xpr.h>
65 #include <kern/lock.h>
66 #include <kern/spl.h>
67 #include <kern/cpu_number.h>
68 #include <kern/misc_protos.h>
69 #include <kern/thread.h>
70 #include <vm/vm_kern.h>
71 #include <string.h>
72
73 /*
74 * After a spontaneous reboot, it is desirable to look
75 * at the old xpr buffer. Assuming xprbootstrap allocates
76 * the buffer in the same place in physical memory and
77 * the reboot doesn't clear memory, this should work.
78 * xprptr will be reset, but the saved value should be OK.
79 * Just set xprenable false so the buffer isn't overwritten.
80 */
81
82 decl_simple_lock_data(,xprlock)
83 boolean_t xprenable = TRUE; /* Enable xpr tracing */
84 int nxprbufs = 0; /* Number of contiguous xprbufs allocated */
85 int xprflags = 0; /* Bit mask of xpr flags enabled */
86 struct xprbuf *xprbase; /* Pointer to circular buffer nxprbufs*sizeof(xprbuf)*/
87 struct xprbuf *xprptr; /* Currently allocated xprbuf */
88 struct xprbuf *xprlast; /* Pointer to end of circular buffer */
89
90 void
91 xpr(
92 const char *msg,
93 long arg1,
94 long arg2,
95 long arg3,
96 long arg4,
97 long arg5)
98 {
99 spl_t s;
100 register struct xprbuf *x;
101
102 /* If we aren't initialized, ignore trace request */
103 if (!xprenable || (xprptr == 0))
104 return;
105 /* Guard against all interrupts and allocate next buffer. */
106
107 s = splhigh();
108 simple_lock(&xprlock);
109 x = xprptr++;
110 if (xprptr >= xprlast) {
111 /* wrap around */
112 xprptr = xprbase;
113 }
114 /* Save xprptr in allocated memory. */
115 *(struct xprbuf **)xprlast = xprptr;
116 simple_unlock(&xprlock);
117 x->timestamp = XPR_TIMESTAMP;
118 splx(s);
119 x->msg = msg;
120 x->arg1 = arg1;
121 x->arg2 = arg2;
122 x->arg3 = arg3;
123 x->arg4 = arg4;
124 x->arg5 = arg5;
125 mp_disable_preemption();
126 x->cpuinfo = cpu_number();
127 mp_enable_preemption();
128 }
129
130 void
131 xprbootstrap(void)
132 {
133 vm_offset_t addr;
134 vm_size_t size;
135 kern_return_t kr;
136
137 simple_lock_init(&xprlock, 0);
138 if (nxprbufs == 0)
139 return; /* assume XPR support not desired */
140
141 /* leave room at the end for a saved copy of xprptr */
142 size = nxprbufs * sizeof(struct xprbuf) + sizeof xprptr;
143
144 kr = kmem_alloc_wired(kernel_map, &addr, size);
145 if (kr != KERN_SUCCESS)
146 panic("xprbootstrap");
147
148 if (xprenable) {
149 /*
150 * If xprenable is set (the default) then we zero
151 * the buffer so xpr_dump doesn't encounter bad pointers.
152 * If xprenable isn't set, then we preserve
153 * the original contents of the buffer. This is useful
154 * if memory survives reboots, so xpr_dump can show
155 * the previous buffer contents.
156 */
157
158 (void) memset((void *) addr, 0, size);
159 }
160
161 xprbase = (struct xprbuf *) addr;
162 xprlast = &xprbase[nxprbufs];
163 xprptr = xprbase; /* setting xprptr enables tracing */
164 }
165
166 int xprinitial = 0;
167
168 void
169 xprinit(void)
170 {
171 xprflags |= xprinitial;
172 }
173
174 #if MACH_KDB
175 #include <ddb/db_output.h>
176
177 /*
178 * Prototypes for functions called from the debugger
179 */
180 void
181 xpr_dump(
182 struct xprbuf *base,
183 int nbufs);
184
185 void
186 xpr_search(
187 int arg_index,
188 int value);
189
190 extern jmp_buf_t *db_recover;
191
192 /*
193 * Print current content of xpr buffers (KDB's sake)
194 * Use stack order to make it understandable.
195 *
196 * Called as "!xpr_dump" this dumps the kernel's xpr buffer.
197 * Called with arguments, it can dump xpr buffers in user tasks,
198 * assuming they use the same format as the kernel.
199 */
200 void
201 xpr_dump(
202 struct xprbuf *base,
203 int nbufs)
204 {
205 jmp_buf_t db_jmpbuf;
206 jmp_buf_t *prev;
207 struct xprbuf *last, *ptr;
208 register struct xprbuf *x;
209 int i;
210 spl_t s;
211
212 if (base == 0) {
213 base = xprbase;
214 nbufs = nxprbufs;
215 }
216
217 if (nbufs == 0)
218 return;
219
220 if (base == xprbase) {
221 s = splhigh();
222 simple_lock(&xprlock);
223 }
224
225 last = base + nbufs;
226 ptr = * (struct xprbuf **) last;
227
228 prev = db_recover;
229 if (_setjmp(db_recover = &db_jmpbuf) == 0)
230 for (x = ptr, i = 0; i < nbufs; i++) {
231 if (--x < base)
232 x = last - 1;
233
234 if (x->msg == 0)
235 break;
236
237 db_printf("<%d:%x:%x> ", x - base, x->cpuinfo, x->timestamp);
238 db_printf(x->msg, x->arg1,x->arg2,x->arg3,x->arg4,x->arg5);
239 }
240 db_recover = prev;
241
242 if (base == xprbase) {
243 simple_unlock(&xprlock);
244 splx(s);
245 }
246 }
247
248 /*
249 * dump xpr table with a selection criteria.
250 * argument number "arg_index" must equal "value"
251 */
252
253 void
254 xpr_search(
255 int arg_index,
256 int value)
257 {
258 jmp_buf_t db_jmpbuf;
259 jmp_buf_t *prev;
260 register struct xprbuf *x;
261 spl_t s;
262 int n;
263
264 if (!nxprbufs)
265 return;
266
267 n = nxprbufs;
268
269 s = splhigh();
270 simple_lock(&xprlock);
271
272 prev = db_recover;
273 if (_setjmp(db_recover = &db_jmpbuf) == 0)
274 for (x = *(struct xprbuf **)xprlast ; n--; ) {
275 if (--x < xprbase)
276 x = xprlast - 1;
277
278 if (x->msg == 0) {
279 break;
280 }
281
282 if (*((&x->arg1)+arg_index) != value)
283 continue;
284
285 db_printf("<%d:%d:%x> ", x - xprbase,
286 x->cpuinfo, x->timestamp);
287 db_printf(x->msg, x->arg1,x->arg2,x->arg3,x->arg4,x->arg5);
288 }
289 db_recover = prev;
290
291 simple_unlock(&xprlock);
292 splx(s);
293 }
294 #endif /* MACH_KDB */