]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_core.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / bsd / kern / kern_core.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
A
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
29 *
30 * File: bsd/kern/kern_core.c
31 *
32 * This file contains machine independent code for performing core dumps.
33 *
1c79356b
A
34 */
35
36#include <mach/vm_param.h>
37#include <mach/thread_status.h>
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/signalvar.h>
42#include <sys/resourcevar.h>
43#include <sys/namei.h>
91447636
A
44#include <sys/vnode_internal.h>
45#include <sys/proc_internal.h>
46#include <sys/kauth.h>
1c79356b
A
47#include <sys/timeb.h>
48#include <sys/times.h>
1c79356b 49#include <sys/acct.h>
91447636 50#include <sys/file_internal.h>
1c79356b
A
51#include <sys/uio.h>
52#include <sys/kernel.h>
53#include <sys/stat.h>
54
55#include <mach-o/loader.h>
56#include <mach/vm_region.h>
765c9de3 57#include <mach/vm_statistics.h>
1c79356b
A
58
59#include <vm/vm_kern.h>
91447636
A
60#include <vm/vm_protos.h> /* last */
61#include <vm/vm_map.h> /* current_map() */
62#include <mach/mach_vm.h> /* mach_vm_region_recurse() */
63#include <mach/task.h> /* task_suspend() */
64#include <kern/task.h> /* get_task_numacts() */
1c79356b
A
65
66typedef struct {
67 int flavor; /* the number for this flavor */
68 int count; /* count of ints in this flavor */
69} mythread_state_flavor_t;
70
71#if defined (__ppc__)
72
73mythread_state_flavor_t thread_flavor_array[]={
74 {PPC_THREAD_STATE , PPC_THREAD_STATE_COUNT},
75 {PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT},
55e303ae
A
76 {PPC_EXCEPTION_STATE, PPC_EXCEPTION_STATE_COUNT},
77 {PPC_VECTOR_STATE, PPC_VECTOR_STATE_COUNT}
1c79356b 78 };
55e303ae 79int mynum_flavors=4;
1c79356b
A
80#elif defined (__i386__)
81mythread_state_flavor_t thread_flavor_array [] = {
8f6c56a5
A
82 {i386_THREAD_STATE, i386_THREAD_STATE_COUNT},
83 {i386_THREAD_FPSTATE, i386_THREAD_FPSTATE_COUNT},
84 {i386_THREAD_EXCEPTSTATE, i386_THREAD_EXCEPTSTATE_COUNT},
85 {i386_THREAD_CTHREADSTATE, i386_THREAD_CTHREADSTATE_COUNT},
86 {i386_NEW_THREAD_STATE, i386_NEW_THREAD_STATE_COUNT},
87 {i386_FLOAT_STATE, i386_FLOAT_STATE_COUNT},
88 {i386_ISA_PORT_MAP_STATE, i386_ISA_PORT_MAP_STATE_COUNT},
89 {i386_V86_ASSIST_STATE, i386_V86_ASSIST_STATE_COUNT},
90 {THREAD_SYSCALL_STATE, i386_THREAD_SYSCALL_STATE_COUNT}
1c79356b 91 };
8f6c56a5 92int mynum_flavors=9;
1c79356b
A
93
94#else
95#error architecture not supported
96#endif
97
98
99typedef struct {
100 vm_offset_t header;
101 int hoffset;
102 mythread_state_flavor_t *flavors;
103 int tstate_size;
104} tir_t;
105
e5568f75 106/* XXX should be static */
91447636 107void collectth_state(thread_t th_act, void *tirp);
e5568f75
A
108
109/* XXX not in a Mach header anywhere */
91447636 110kern_return_t thread_getstatus(register thread_t act, int flavor,
e5568f75 111 thread_state_t tstate, mach_msg_type_number_t *count);
91447636 112void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *);
e5568f75
A
113
114
91447636
A
115__private_extern__ int do_coredump = 1; /* default: dump cores */
116__private_extern__ int sugid_coredump = 0; /* default: but not SGUID binaries */
e5568f75 117
55e303ae 118void
91447636 119collectth_state(thread_t th_act, void *tirp)
1c79356b
A
120{
121 vm_offset_t header;
122 int hoffset, i ;
123 mythread_state_flavor_t *flavors;
124 struct thread_command *tc;
91447636
A
125 tir_t *t = (tir_t *)tirp;
126
1c79356b
A
127 /*
128 * Fill in thread command structure.
129 */
130 header = t->header;
131 hoffset = t->hoffset;
132 flavors = t->flavors;
133
134 tc = (struct thread_command *) (header + hoffset);
135 tc->cmd = LC_THREAD;
136 tc->cmdsize = sizeof(struct thread_command)
137 + t->tstate_size;
138 hoffset += sizeof(struct thread_command);
139 /*
140 * Follow with a struct thread_state_flavor and
141 * the appropriate thread state struct for each
142 * thread state flavor.
143 */
144 for (i = 0; i < mynum_flavors; i++) {
145 *(mythread_state_flavor_t *)(header+hoffset) =
146 flavors[i];
147 hoffset += sizeof(mythread_state_flavor_t);
148 thread_getstatus(th_act, flavors[i].flavor,
e5568f75 149 (thread_state_t)(header+hoffset),
1c79356b
A
150 &flavors[i].count);
151 hoffset += flavors[i].count*sizeof(int);
152 }
153
154 t->hoffset = hoffset;
155}
e5568f75 156
1c79356b
A
157/*
158 * Create a core image on the file "core".
159 */
160#define MAX_TSTATE_FLAVORS 10
161int
e5568f75 162coredump(struct proc *p)
1c79356b
A
163{
164 int error=0;
91447636
A
165 kauth_cred_t cred = kauth_cred_get();
166 struct vnode_attr va;
167 struct vfs_context context;
1c79356b
A
168 vm_map_t map;
169 int thread_count, segment_count;
170 int command_size, header_size, tstate_size;
91447636
A
171 int hoffset;
172 off_t foffset;
173 vm_map_offset_t vmoffset;
1c79356b 174 vm_offset_t header;
91447636 175 vm_map_size_t vmsize;
1c79356b
A
176 vm_prot_t prot;
177 vm_prot_t maxprot;
178 vm_inherit_t inherit;
1c79356b
A
179 int error1;
180 task_t task;
181 char core_name[MAXCOMLEN+6];
e5568f75 182 char *name;
1c79356b 183 mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS];
e5568f75 184 vm_size_t mapsize;
1c79356b
A
185 int i;
186 int nesting_depth = 0;
187 kern_return_t kret;
188 struct vm_region_submap_info_64 vbr;
189 int vbrcount=0;
190 tir_t tir1;
191 struct vnode * vp;
91447636
A
192 struct mach_header *mh;
193 struct mach_header_64 *mh64;
194 int is_64 = 0;
195 size_t mach_header_sz = sizeof(struct mach_header);
196 size_t segment_command_sz = sizeof(struct segment_command);
1c79356b 197
e5568f75
A
198 if (do_coredump == 0 || /* Not dumping at all */
199 ( (sugid_coredump == 0) && /* Not dumping SUID/SGID binaries */
91447636
A
200 ( (cred->cr_svuid != cred->cr_ruid) ||
201 (cred->cr_svgid != cred->cr_rgid)))) {
e5568f75 202
1c79356b 203 return (EFAULT);
e5568f75 204 }
1c79356b 205
91447636
A
206 if (IS_64BIT_PROCESS(p)) {
207 is_64 = 1;
208 mach_header_sz = sizeof(struct mach_header_64);
209 segment_command_sz = sizeof(struct segment_command_64);
210 }
211
1c79356b
A
212 task = current_task();
213 map = current_map();
214 mapsize = get_vmmap_size(map);
215
216 if (mapsize >= p->p_rlimit[RLIMIT_CORE].rlim_cur)
217 return (EFAULT);
218 (void) task_suspend(task);
219
e5568f75 220 /* create name according to sysctl'able format string */
91447636 221 name = proc_core_name(p->p_comm, kauth_cred_getuid(cred), p->p_pid);
e5568f75
A
222
223 /* if name creation fails, fall back to historical behaviour... */
224 if (name == NULL) {
91447636 225 sprintf(core_name, "/cores/core.%d", p->p_pid);
e5568f75
A
226 name = core_name;
227 }
91447636
A
228 context.vc_proc = p;
229 context.vc_ucred = cred;
e5568f75 230
3a60a9f5 231 if ((error = vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW), S_IRUSR, VNODE_LOOKUP_NOFOLLOW, &vp, &context)))
91447636
A
232 return (error);
233
234 VATTR_INIT(&va);
235 VATTR_WANTED(&va, va_nlink);
1c79356b
A
236 /* Don't dump to non-regular files or files with links. */
237 if (vp->v_type != VREG ||
91447636 238 vnode_getattr(vp, &va, &context) || va.va_nlink != 1) {
1c79356b
A
239 error = EFAULT;
240 goto out;
241 }
242
91447636
A
243 VATTR_INIT(&va); /* better to do it here than waste more stack in vnode_setsize */
244 VATTR_SET(&va, va_data_size, 0);
245 vnode_setattr(vp, &va, &context);
1c79356b
A
246 p->p_acflag |= ACORE;
247
248 /*
249 * If the task is modified while dumping the file
250 * (e.g., changes in threads or VM, the resulting
251 * file will not necessarily be correct.
252 */
253
254 thread_count = get_task_numacts(task);
255 segment_count = get_vmmap_entries(map); /* XXX */
1c79356b 256 bcopy(thread_flavor_array,flavors,sizeof(thread_flavor_array));
1c79356b 257 tstate_size = 0;
e5568f75 258 for (i = 0; i < mynum_flavors; i++)
1c79356b
A
259 tstate_size += sizeof(mythread_state_flavor_t) +
260 (flavors[i].count * sizeof(int));
261
91447636 262 command_size = segment_count * segment_command_sz +
1c79356b
A
263 thread_count*sizeof(struct thread_command) +
264 tstate_size*thread_count;
265
91447636 266 header_size = command_size + mach_header_sz;
1c79356b 267
91447636 268 (void) kmem_alloc(kernel_map,
1c79356b
A
269 (vm_offset_t *)&header,
270 (vm_size_t)header_size);
271
272 /*
273 * Set up Mach-O header.
274 */
91447636
A
275 if (is_64) {
276 mh64 = (struct mach_header_64 *)header;
277 mh64->magic = MH_MAGIC_64;
278 mh64->cputype = cpu_type();
279 mh64->cpusubtype = cpu_subtype();
280 mh64->filetype = MH_CORE;
281 mh64->ncmds = segment_count + thread_count;
282 mh64->sizeofcmds = command_size;
283 mh64->reserved = 0; /* 8 byte alignment */
284 } else {
285 mh = (struct mach_header *)header;
286 mh->magic = MH_MAGIC;
287 mh->cputype = cpu_type();
288 mh->cpusubtype = cpu_subtype();
289 mh->filetype = MH_CORE;
290 mh->ncmds = segment_count + thread_count;
291 mh->sizeofcmds = command_size;
292 }
293
294 hoffset = mach_header_sz; /* offset into header */
295 foffset = round_page(header_size); /* offset into file */
296 vmoffset = MACH_VM_MIN_ADDRESS; /* offset into VM */
297
55e303ae
A
298 /*
299 * We use to check for an error, here, now we try and get
1c79356b
A
300 * as much as we can
301 */
91447636
A
302 while (segment_count > 0) {
303 struct segment_command *sc;
304 struct segment_command_64 *sc64;
305
1c79356b
A
306 /*
307 * Get region information for next region.
308 */
309
310 while (1) {
311 vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64;
91447636
A
312 if((kret = mach_vm_region_recurse(map,
313 &vmoffset, &vmsize, &nesting_depth,
314 (vm_region_recurse_info_t)&vbr,
315 &vbrcount)) != KERN_SUCCESS) {
316 break;
317 }
318 /*
319 * If we get a valid mapping back, but we're dumping
320 * a 32 bit process, and it's over the allowable
321 * address space of a 32 bit process, it's the same
322 * as if mach_vm_region_recurse() failed.
323 */
324 if (!(is_64) &&
325 (vmoffset + vmsize > VM_MAX_ADDRESS)) {
326 kret = KERN_INVALID_ADDRESS;
1c79356b
A
327 break;
328 }
329 if(vbr.is_submap) {
330 nesting_depth++;
331 continue;
332 } else {
333 break;
334 }
335 }
336 if(kret != KERN_SUCCESS)
337 break;
338
339 prot = vbr.protection;
340 maxprot = vbr.max_protection;
341 inherit = vbr.inheritance;
342 /*
343 * Fill in segment command structure.
344 */
91447636
A
345 if (is_64) {
346 sc64 = (struct segment_command_64 *)(header + hoffset);
347 sc64->cmd = LC_SEGMENT_64;
348 sc64->cmdsize = sizeof(struct segment_command_64);
349 /* segment name is zeroed by kmem_alloc */
350 sc64->segname[0] = 0;
351 sc64->vmaddr = vmoffset;
352 sc64->vmsize = vmsize;
353 sc64->fileoff = foffset;
354 sc64->filesize = vmsize;
355 sc64->maxprot = maxprot;
356 sc64->initprot = prot;
357 sc64->nsects = 0;
358 } else {
359 sc = (struct segment_command *) (header + hoffset);
360 sc->cmd = LC_SEGMENT;
361 sc->cmdsize = sizeof(struct segment_command);
362 /* segment name is zeroed by kmem_alloc */
363 sc->segname[0] = 0;
364 sc->vmaddr = CAST_DOWN(vm_offset_t,vmoffset);
365 sc->vmsize = CAST_DOWN(vm_size_t,vmsize);
366 sc->fileoff = CAST_DOWN(uint32_t,foffset);
367 sc->filesize = CAST_DOWN(uint32_t,vmsize);
368 sc->maxprot = maxprot;
369 sc->initprot = prot;
370 sc->nsects = 0;
371 }
1c79356b
A
372
373 /*
374 * Write segment out. Try as hard as possible to
375 * get read access to the data.
376 */
377 if ((prot & VM_PROT_READ) == 0) {
91447636
A
378 mach_vm_protect(map, vmoffset, vmsize, FALSE,
379 prot|VM_PROT_READ);
1c79356b
A
380 }
381 /*
382 * Only actually perform write if we can read.
383 * Note: if we can't read, then we end up with
384 * a hole in the file.
385 */
55e303ae
A
386 if ((maxprot & VM_PROT_READ) == VM_PROT_READ
387 && vbr.user_tag != VM_MEMORY_IOKIT
388 && coredumpok(map,vmoffset)) {
91447636
A
389 vm_map_size_t tmp_vmsize = vmsize;
390 off_t xfer_foffset = foffset;
391
392 //LP64todo - works around vn_rdwr_64() 2G limit
393 while (tmp_vmsize > 0) {
394 vm_map_size_t xfer_vmsize = tmp_vmsize;
395 if (xfer_vmsize > INT_MAX)
396 xfer_vmsize = INT_MAX;
397 error = vn_rdwr_64(UIO_WRITE, vp,
398 vmoffset, xfer_vmsize, xfer_foffset,
399 (IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
400 IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
401 tmp_vmsize -= xfer_vmsize;
402 xfer_foffset += xfer_vmsize;
403 }
1c79356b
A
404 }
405
91447636
A
406 hoffset += segment_command_sz;
407 foffset += vmsize;
408 vmoffset += vmsize;
1c79356b
A
409 segment_count--;
410 }
411
91447636
A
412 /*
413 * If there are remaining segments which have not been written
414 * out because break in the loop above, then they were not counted
415 * because they exceed the real address space of the executable
416 * type: remove them from the header's count. This is OK, since
417 * we are allowed to have a sparse area following the segments.
418 */
419 if (is_64) {
420 mh64->ncmds -= segment_count;
421 } else {
422 mh->ncmds -= segment_count;
423 }
424
1c79356b
A
425 tir1.header = header;
426 tir1.hoffset = hoffset;
427 tir1.flavors = flavors;
428 tir1.tstate_size = tstate_size;
429 task_act_iterate_wth_args(task, collectth_state,&tir1);
430
1c79356b
A
431 /*
432 * Write out the Mach header at the beginning of the
91447636 433 * file. OK to use a 32 bit write for this.
1c79356b
A
434 */
435 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0,
91447636 436 UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
1c79356b
A
437 kmem_free(kernel_map, header, header_size);
438out:
91447636 439 error1 = vnode_close(vp, FWRITE, &context);
1c79356b
A
440 if (error == 0)
441 error = error1;
91447636 442
55e303ae 443 return (error);
1c79356b 444}