]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_panicinfo.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / bsd / kern / kern_panicinfo.c
1 /*
2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <sys/param.h>
30 #include <sys/fcntl.h>
31 #include <sys/malloc.h>
32 #include <sys/proc.h>
33 #include <sys/sysctl.h>
34 #include <sys/vnode.h>
35 #include <sys/vm.h>
36 #include <sys/systm.h>
37
38 #include <mach/mach_types.h>
39 #include <mach/kern_return.h>
40 #include <kern/kern_types.h>
41 #include <vm/vm_kern.h>
42
43
44 /* prototypes not exported by osfmk/console. */
45 extern void panic_dialog_test( void );
46 extern int panic_dialog_set_image( const unsigned char * ptr, unsigned int size );
47 extern void panic_dialog_get_image( unsigned char ** ptr, unsigned int * size );
48
49 /* make the compiler happy */
50 extern int sysctl_dopanicinfo(int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, struct proc *);
51
52
53 #define PANIC_IMAGE_SIZE_LIMIT (32 * 4096) /* 128K - Maximum amount of memory consumed for the panic UI */
54 #define KERN_PANICINFO_TEST (KERN_PANICINFO_IMAGE+2) /* Allow the panic UI to be tested by root without causing a panic */
55
56 /* Local data */
57 static int image_size_limit = PANIC_IMAGE_SIZE_LIMIT;
58
59 __private_extern__ int
60 sysctl_dopanicinfo(name, namelen, oldp, oldlenp, newp, newlen, p)
61 int *name;
62 u_int namelen;
63 user_addr_t oldp;
64 size_t *oldlenp;
65 user_addr_t newp;
66 size_t newlen;
67 struct proc *p;
68 {
69 int error = 0;
70 vm_offset_t newimage = (vm_offset_t )NULL;
71 kern_return_t kret;
72 unsigned char * prev_image_ptr;
73 unsigned int prev_image_size;
74
75
76 /* all sysctl names at this level are terminal */
77 if (namelen != 1)
78 return (ENOTDIR); /* overloaded */
79
80 if ( (error = proc_suser(p)) ) /* must be super user to muck with image */
81 return (error);
82
83 switch (name[0]) {
84 default:
85 return (ENOTSUP);
86
87 case KERN_PANICINFO_TEST:
88
89 panic_dialog_test();
90 return (0);
91
92 case KERN_PANICINFO_MAXSIZE:
93
94 /* return the image size limits */
95
96 newlen = 0;
97 newp = USER_ADDR_NULL;
98
99 error = sysctl_int(oldp, oldlenp, newp, newlen, &image_size_limit);
100
101 return (error);
102
103 case KERN_PANICINFO_IMAGE:
104
105 /* If we have a new image, allocate wired kernel memory and copy it in from user space */
106 if ( newp != USER_ADDR_NULL ) {
107
108 /* check the length of the incoming image before allocating space for it. */
109 if ( newlen > (size_t)image_size_limit )
110 return (ENOMEM);
111
112 /* allocate some kernel wired memory for the new image */
113 kret = kmem_alloc(kernel_map, &newimage, (vm_size_t)round_page_32(newlen));
114
115 if (kret != KERN_SUCCESS) {
116 switch (kret) {
117 default:
118 error = EINVAL;
119 break;
120 case KERN_NO_SPACE:
121 case KERN_RESOURCE_SHORTAGE:
122 error = ENOMEM;
123 break;
124 case KERN_PROTECTION_FAILURE:
125 error = EPERM;
126 break;
127 }
128
129 return (error);
130 }
131
132 /* copy the image in from user space */
133 if ( (error = copyin(newp, (char *) newimage, newlen)) )
134 goto errout;
135
136 } else { /* setup to make the default image active */
137
138 newimage = (vm_offset_t )NULL;
139 newlen = 0;
140 }
141
142 /* get the current image location and size */
143 panic_dialog_get_image( &prev_image_ptr, &prev_image_size );
144
145 /* did the caller request a copy of the previous image ? */
146 if ( oldp != USER_ADDR_NULL ) {
147 if ( *oldlenp < prev_image_size ) {
148 error = ERANGE;
149 goto errout;
150 }
151
152 /* copy the image to user space or zero the size if the default image is active */
153 if ( prev_image_ptr != NULL ) {
154 if ( (error = copyout( prev_image_ptr, oldp, prev_image_size )) )
155 goto errout;
156
157 *oldlenp = prev_image_size;
158 }
159 else /* tell the user that the default image is active */
160 *oldlenp = 0;
161 }
162
163 /* Make the new image active, or reactivate the default image.
164 But, handle the special case of asking for the current image
165 without changing the current image.
166 */
167
168 if ( !(oldp && newp == USER_ADDR_NULL) ) {
169 if ( (error = panic_dialog_set_image( (unsigned char *) newimage, newlen )) )
170 goto errout;
171
172 /* free the wired memory used by the previous image */
173 if ( prev_image_ptr != NULL ) {
174 (void)kmem_free(kernel_map, (vm_offset_t) prev_image_ptr, (vm_size_t)round_page_32(prev_image_size));
175 printf("Panic UI memory freed (%d)\n", round_page_32(prev_image_size));
176 }
177 }
178
179 return (0);
180
181 errout:
182 if ( newimage != (vm_offset_t )NULL )
183 (void)kmem_free(kernel_map, newimage, (vm_size_t)round_page_32(newlen));
184
185 return (error);
186 }
187 }