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