]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_panicinfo.c
xnu-792.6.22.tar.gz
[apple/xnu.git] / bsd / kern / kern_panicinfo.c
1 /*
2 * Copyright (c) 2002 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 #include <sys/param.h>
24 #include <sys/fcntl.h>
25 #include <sys/malloc.h>
26 #include <sys/proc.h>
27 #include <sys/sysctl.h>
28 #include <sys/vnode.h>
29 #include <sys/vm.h>
30 #include <sys/systm.h>
31
32 #include <mach/mach_types.h>
33 #include <mach/kern_return.h>
34 #include <kern/kern_types.h>
35 #include <vm/vm_kern.h>
36
37
38 /* prototypes not exported by osfmk/console. */
39 extern void panic_dialog_test( void );
40 extern int panic_dialog_set_image( const unsigned char * ptr, unsigned int size );
41 extern void panic_dialog_get_image( unsigned char ** ptr, unsigned int * size );
42
43 /* make the compiler happy */
44 extern int sysctl_dopanicinfo(int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, struct proc *);
45
46
47 #define PANIC_IMAGE_SIZE_LIMIT (32 * 4096) /* 128K - Maximum amount of memory consumed for the panic UI */
48 #define KERN_PANICINFO_TEST (KERN_PANICINFO_IMAGE+2) /* Allow the panic UI to be tested by root without causing a panic */
49
50 /* Local data */
51 static int image_size_limit = PANIC_IMAGE_SIZE_LIMIT;
52
53 __private_extern__ int
54 sysctl_dopanicinfo(name, namelen, oldp, oldlenp, newp, newlen, p)
55 int *name;
56 u_int namelen;
57 user_addr_t oldp;
58 size_t *oldlenp;
59 user_addr_t newp;
60 size_t newlen;
61 struct proc *p;
62 {
63 int error = 0;
64 vm_offset_t newimage = (vm_offset_t )NULL;
65 kern_return_t kret;
66 unsigned char * prev_image_ptr;
67 unsigned int prev_image_size;
68
69
70 /* all sysctl names at this level are terminal */
71 if (namelen != 1)
72 return (ENOTDIR); /* overloaded */
73
74 if ( (error = proc_suser(p)) ) /* must be super user to muck with image */
75 return (error);
76
77 switch (name[0]) {
78 default:
79 return (ENOTSUP);
80
81 case KERN_PANICINFO_TEST:
82
83 panic_dialog_test();
84 return (0);
85
86 case KERN_PANICINFO_MAXSIZE:
87
88 /* return the image size limits */
89
90 newlen = 0;
91 newp = USER_ADDR_NULL;
92
93 error = sysctl_int(oldp, oldlenp, newp, newlen, &image_size_limit);
94
95 return (error);
96
97 case KERN_PANICINFO_IMAGE:
98
99 /* If we have a new image, allocate wired kernel memory and copy it in from user space */
100 if ( newp != USER_ADDR_NULL ) {
101
102 /* check the length of the incoming image before allocating space for it. */
103 if ( newlen > (size_t)image_size_limit )
104 return (ENOMEM);
105
106 /* allocate some kernel wired memory for the new image */
107 kret = kmem_alloc(kernel_map, &newimage, (vm_size_t)round_page_32(newlen));
108
109 if (kret != KERN_SUCCESS) {
110 switch (kret) {
111 default:
112 error = EINVAL;
113 break;
114 case KERN_NO_SPACE:
115 case KERN_RESOURCE_SHORTAGE:
116 error = ENOMEM;
117 break;
118 case KERN_PROTECTION_FAILURE:
119 error = EPERM;
120 break;
121 }
122
123 return (error);
124 }
125
126 /* copy the image in from user space */
127 if ( (error = copyin(newp, (char *) newimage, newlen)) )
128 goto errout;
129
130 } else { /* setup to make the default image active */
131
132 newimage = (vm_offset_t )NULL;
133 newlen = 0;
134 }
135
136 /* get the current image location and size */
137 panic_dialog_get_image( &prev_image_ptr, &prev_image_size );
138
139 /* did the caller request a copy of the previous image ? */
140 if ( oldp != USER_ADDR_NULL ) {
141 if ( *oldlenp < prev_image_size ) {
142 error = ERANGE;
143 goto errout;
144 }
145
146 /* copy the image to user space or zero the size if the default image is active */
147 if ( prev_image_ptr != NULL ) {
148 if ( (error = copyout( prev_image_ptr, oldp, prev_image_size )) )
149 goto errout;
150
151 *oldlenp = prev_image_size;
152 }
153 else /* tell the user that the default image is active */
154 *oldlenp = 0;
155 }
156
157 /* Make the new image active, or reactivate the default image.
158 But, handle the special case of asking for the current image
159 without changing the current image.
160 */
161
162 if ( !(oldp && newp == USER_ADDR_NULL) ) {
163 if ( (error = panic_dialog_set_image( (unsigned char *) newimage, newlen )) )
164 goto errout;
165
166 /* free the wired memory used by the previous image */
167 if ( prev_image_ptr != NULL ) {
168 (void)kmem_free(kernel_map, (vm_offset_t) prev_image_ptr, (vm_size_t)round_page_32(prev_image_size));
169 printf("Panic UI memory freed (%d)\n", round_page_32(prev_image_size));
170 }
171 }
172
173 return (0);
174
175 errout:
176 if ( newimage != (vm_offset_t )NULL )
177 (void)kmem_free(kernel_map, newimage, (vm_size_t)round_page_32(newlen));
178
179 return (error);
180 }
181 }