]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_symfile.c
xnu-792.6.61.tar.gz
[apple/xnu.git] / bsd / kern / kern_symfile.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 /* Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
23 *
24 * File: bsd/kern/kern_symfile.c
25 *
26 * This file contains creates a dummy symbol file for mach_kernel
27 * based on the symbol table information passed by the
28 * SecondaryLoader/PlatformExpert. This allows us to correctly
29 * link other executables (drivers, etc) against the the kernel in
30 * cases where the kernel image on the root device does not match
31 * the live kernel. This can occur during net-booting where the
32 * actual kernel image is obtained from the network via tftp rather
33 * than the root device.
34 *
35 * If a symbol table is available, then the file /mach.sym will be
36 * created containing a Mach Header and a LC_SYMTAB load command
37 * followed by the the symbol table data for mach_kernel.
38 *
39 * NOTE: This file supports only 32 bit kernels at the present time;
40 * adding support for 64 bit kernels is possible, but is not
41 * necessary at the present time.
42 *
43 * HISTORY
44 *
45 * .
46 */
47
48 #include <mach/vm_param.h>
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/signalvar.h>
53 #include <sys/resourcevar.h>
54 #include <sys/namei.h>
55 #include <sys/vnode_internal.h>
56 #include <sys/proc_internal.h>
57 #include <sys/kauth.h>
58 #include <sys/timeb.h>
59 #include <sys/times.h>
60 #include <sys/acct.h>
61 #include <sys/file_internal.h>
62 #include <sys/uio.h>
63 #include <sys/kernel.h>
64 #include <sys/stat.h>
65 #include <sys/disk.h>
66 #include <sys/conf.h>
67
68 #include <mach-o/loader.h>
69 #include <mach-o/nlist.h>
70
71 #include <kern/kalloc.h>
72 #include <vm/vm_kern.h>
73 #include <pexpert/pexpert.h>
74 #include <IOKit/IOHibernatePrivate.h>
75
76 extern unsigned char rootdevice[];
77 extern struct mach_header _mh_execute_header;
78
79 static int kernel_symfile_opened = 0;
80 static int error_code = 0;
81
82 extern int IODTGetLoaderInfo(char *key, void **infoAddr, int *infoSize);
83 extern void IODTFreeLoaderInfo(char *key, void *infoAddr, int infoSize);
84
85 /*
86 * Can only operate against currently running 32 bit mach_kernel
87 */
88 static int
89 output_kernel_symbols(struct proc *p)
90 {
91 struct vnode *vp;
92 kauth_cred_t cred = p->p_ucred; /* XXX */
93 struct vnode_attr va;
94 struct vfs_context context;
95 struct load_command *cmd;
96 struct mach_header *orig_mh, *mh;
97 struct segment_command *orig_ds, *orig_ts, *orig_le, *sg;
98 struct section *se, *const_text;
99 struct symtab_command *st, *orig_st;
100 struct nlist *sym;
101 vm_size_t orig_mhsize, orig_st_size;
102 vm_offset_t header;
103 vm_size_t header_size = 0; /* out: protected by header */
104 int error, error1;
105 unsigned int i, j;
106 caddr_t addr;
107 vm_offset_t offset;
108 int rc_mh, rc_sc;
109
110 error = EFAULT;
111
112 vp = NULL;
113 header = NULL;
114 orig_mh = NULL;
115 orig_st = NULL;
116
117 // Dispose of unnecessary gumf, the booter doesn't need to load these
118 rc_mh = IODTGetLoaderInfo("Kernel-__HEADER",
119 (void **)&orig_mh, &orig_mhsize);
120 if (rc_mh == 0 && orig_mh)
121 IODTFreeLoaderInfo("Kernel-__HEADER",
122 (void *)orig_mh, round_page_32(orig_mhsize));
123
124 rc_sc = IODTGetLoaderInfo("Kernel-__SYMTAB",
125 (void **) &orig_st, &orig_st_size);
126 if (rc_sc == 0 && orig_st)
127 IODTFreeLoaderInfo("Kernel-__SYMTAB",
128 (void *)orig_st, round_page_32(orig_st_size));
129
130 if (cred->cr_svuid != cred->cr_ruid || cred->cr_svgid != cred->cr_rgid)
131 goto out;
132
133 // Check to see if the root is 'e' or 'n', is this a test for network?
134 if (rootdevice[0] == 'e' && rootdevice[1] == 'n')
135 goto out;
136
137 context.vc_proc = p;
138 context.vc_ucred = cred;
139
140 if ((error = vnode_open("mach.sym", (O_CREAT | FWRITE), (S_IRUSR | S_IRGRP | S_IROTH), 0, &vp, &context)))
141 goto out;
142
143 /* Don't dump to non-regular files or files with links. */
144 error = EFAULT;
145 VATTR_INIT(&va);
146 VATTR_WANTED(&va, va_nlink);
147 if ((vp->v_type != VREG) || vnode_getattr(vp, &va, &context) || (va.va_nlink != 1))
148 goto out;
149
150 VATTR_INIT(&va); /* better to do it here than waste more stack in vnode_getsize */
151 VATTR_SET(&va, va_data_size, 0);
152 vnode_setattr(vp, &va, &context);
153 p->p_acflag |= ACORE;
154
155 // If the file type is MH_EXECUTE then this must be a kernel
156 // as all Kernel extensions must be of type MH_OBJECT
157 orig_ds = orig_ts = orig_le = NULL;
158 orig_st = NULL;
159 orig_mh = &_mh_execute_header;
160 cmd = (struct load_command *) &orig_mh[1];
161 for (i = 0; i < orig_mh->ncmds; i++) {
162 if (cmd->cmd == LC_SEGMENT) {
163 struct segment_command *orig_sg = (struct segment_command *) cmd;
164
165 if (!strcmp(SEG_TEXT, orig_sg->segname))
166 orig_ts = orig_sg;
167 else if (!strcmp(SEG_DATA, orig_sg->segname))
168 orig_ds = orig_sg;
169 else if (!strcmp(SEG_LINKEDIT, orig_sg->segname))
170 orig_le = orig_sg;
171 }
172 else if (cmd->cmd == LC_SYMTAB)
173 orig_st = (struct symtab_command *) cmd;
174
175 cmd = (struct load_command *) ((caddr_t) cmd + cmd->cmdsize);
176 }
177
178 if (!orig_ts || !orig_ds || !orig_le || !orig_st)
179 goto out;
180
181 const_text = NULL;
182 se = (struct section *) &orig_ts[1];
183 for (i = 0; i < orig_ts->nsects; i++, se++) {
184 if (!strcmp("__const", se->sectname)) {
185 const_text = se;
186 break;
187 }
188 }
189 if (!const_text)
190 goto out;
191
192 header_size = sizeof(struct mach_header)
193 + orig_ts->cmdsize
194 + orig_ds->cmdsize
195 + sizeof(struct symtab_command);
196
197 (void) kmem_alloc(kernel_map,
198 (vm_offset_t *) &header,
199 (vm_size_t) header_size);
200 if (header)
201 bzero((void *) header, header_size);
202 else
203 goto out;
204
205 /*
206 * Set up Mach-O header.
207 */
208 mh = (struct mach_header *) header;
209 mh->magic = orig_mh->magic;
210 mh->cputype = orig_mh->cputype;
211 mh->cpusubtype = orig_mh->cpusubtype;
212 mh->filetype = orig_mh->filetype;
213 mh->ncmds = 3;
214 mh->sizeofcmds = header_size - sizeof(struct mach_header);
215 mh->flags = orig_mh->flags;
216
217 // Initialise the current file offset and addr
218 offset = round_page(header_size);
219 addr = (caddr_t) const_text->addr; // Load address of __TEXT,__const
220
221 /*
222 * Construct a TEXT segment load command
223 * the only part of the TEXT segment we keep is the __TEXT,__const
224 * which contains the kernel vtables.
225 */
226 sg = (struct segment_command *) &mh[1];
227 bcopy(orig_ts, sg, orig_ts->cmdsize);
228 sg->vmaddr = (unsigned long) addr;
229 sg->vmsize = const_text->size;
230 sg->fileoff = 0;
231 sg->filesize = const_text->size + round_page(header_size);
232 sg->maxprot = 0;
233 sg->initprot = 0;
234 sg->flags = 0;
235 se = (struct section *)(sg+1);
236 for ( j = 0; j < sg->nsects; j++, se++ ) {
237 se->addr = (unsigned long) addr;
238 se->size = 0;
239 se->offset = offset;
240 se->nreloc = 0;
241 if (!strcmp("__const", se->sectname)) {
242 se->size = const_text->size;
243 addr += const_text->size;
244 offset += const_text->size;
245 const_text = se;
246 }
247 }
248 offset = round_page(offset);
249
250 // Now copy of the __DATA segment load command, the image need
251 // not be stored to disk nobody needs it, yet!
252 sg = (struct segment_command *)((int)sg + sg->cmdsize);
253 bcopy(orig_ds, sg, orig_ds->cmdsize);
254
255 sg->vmaddr = (unsigned long) addr;
256 sg->vmsize = 0x1000; // One page for some reason?
257 sg->fileoff = offset;
258 sg->filesize = 0;
259 sg->maxprot = 0;
260 sg->initprot = 0;
261 sg->flags = 0;
262 se = (struct section *)(sg+1);
263 for ( j = 0; j < sg->nsects; j++, se++ ) {
264 se->addr = (unsigned long) addr;
265 se->size = 0;
266 se->offset = offset;
267 se->nreloc = 0;
268 }
269 offset = round_page(offset);
270
271
272 /*
273 * Set up LC_SYMTAB command
274 */
275 st = (struct symtab_command *)((int)sg + sg->cmdsize);
276 st->cmd = LC_SYMTAB;
277 st->cmdsize = sizeof(struct symtab_command);
278 st->symoff = offset;
279 st->nsyms = orig_st->nsyms;
280 st->strsize = orig_st->strsize;
281 st->stroff = offset + st->nsyms * sizeof(struct nlist);
282
283 /*
284 * Convert the symbol table in place from section references
285 * to absolute references.
286 */
287 sym = (struct nlist *) orig_le->vmaddr;
288 for (i = 0; i < st->nsyms; i++, sym++ ) {
289 if ( (sym->n_type & N_TYPE) == N_SECT) {
290 sym->n_sect = NO_SECT;
291 sym->n_type = (sym->n_type & ~N_TYPE) | N_ABS;
292 }
293 }
294
295 /*
296 * Write out the load commands at the beginning of the file.
297 */
298 error = vn_rdwr(UIO_WRITE, vp, (caddr_t) mh, header_size, (off_t) 0,
299 UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
300 if (error)
301 goto out;
302
303 /*
304 * Write out the __TEXT,__const data segment.
305 */
306 error = vn_rdwr(UIO_WRITE, vp, (caddr_t) const_text->addr,
307 const_text->size, const_text->offset,
308 UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
309 if (error)
310 goto out;
311
312 /*
313 * Write out kernel symbols
314 */
315 offset = st->nsyms * sizeof(struct nlist) + st->strsize; // symtab size
316 error = vn_rdwr(UIO_WRITE, vp,
317 (caddr_t) orig_le->vmaddr, offset, st->symoff,
318 UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
319 out:
320 if (header)
321 kmem_free(kernel_map, header, header_size);
322
323 if (vp) {
324 error1 = vnode_close(vp, FWRITE, &context);
325 if (!error) error = error1;
326 }
327
328 return(error);
329 }
330 /*
331 *
332 */
333 int get_kernel_symfile(struct proc *p, char **symfile)
334 {
335 if (!kernel_symfile_opened) {
336 kernel_symfile_opened = 1;
337 error_code = output_kernel_symbols(p);
338 }
339 if (!error_code)
340 *symfile = "\\mach.sym";
341
342 return error_code;
343 }
344
345 struct kern_direct_file_io_ref_t
346 {
347 struct vfs_context context;
348 struct vnode *vp;
349 };
350
351
352 static int file_ioctl(void * p1, void * p2, int theIoctl, caddr_t result)
353 {
354 dev_t device = (dev_t) p1;
355
356 return ((*bdevsw[major(device)].d_ioctl)
357 (device, theIoctl, result, S_IFBLK, p2));
358 }
359
360 static int device_ioctl(void * p1, __unused void * p2, int theIoctl, caddr_t result)
361 {
362 return (VNOP_IOCTL(p1, theIoctl, result, 0, p2));
363 }
364
365 struct kern_direct_file_io_ref_t *
366 kern_open_file_for_direct_io(const char * name,
367 kern_get_file_extents_callback_t callback,
368 void * callback_ref,
369 dev_t * device_result,
370 uint64_t * partitionbase_result,
371 uint64_t * maxiocount_result)
372 {
373 struct kern_direct_file_io_ref_t * ref;
374
375 struct proc *p;
376 struct ucred *cred;
377 struct vnode_attr va;
378 int error;
379 off_t f_offset;
380 uint32_t blksize;
381 uint64_t size;
382 dev_t device;
383 off_t maxiocount, count;
384
385 int (*do_ioctl)(void * p1, void * p2, int theIoctl, caddr_t result);
386 void * p1;
387 void * p2;
388
389 error = EFAULT;
390
391 ref = (struct kern_direct_file_io_ref_t *) kalloc(sizeof(struct kern_direct_file_io_ref_t));
392 if (!ref)
393 {
394 error = EFAULT;
395 goto out;
396 }
397
398 ref->vp = NULL;
399 p = current_proc(); // kernproc;
400 cred = p->p_ucred;
401 ref->context.vc_proc = p;
402 ref->context.vc_ucred = cred;
403
404 if ((error = vnode_open(name, (O_CREAT | FWRITE), (0), 0, &ref->vp, &ref->context)))
405 goto out;
406
407 VATTR_INIT(&va);
408 VATTR_WANTED(&va, va_rdev);
409 VATTR_WANTED(&va, va_fsid);
410 VATTR_WANTED(&va, va_data_size);
411 VATTR_WANTED(&va, va_nlink);
412 error = EFAULT;
413 if (vnode_getattr(ref->vp, &va, &ref->context))
414 goto out;
415
416 kprintf("vp va_rdev major %d minor %d\n", major(va.va_rdev), minor(va.va_rdev));
417 kprintf("vp va_fsid major %d minor %d\n", major(va.va_fsid), minor(va.va_fsid));
418 kprintf("vp size %qd\n", va.va_data_size);
419
420 if (ref->vp->v_type == VREG)
421 {
422 /* Don't dump files with links. */
423 if (va.va_nlink != 1)
424 goto out;
425
426 device = va.va_fsid;
427 p1 = (void *) device;
428 p2 = p;
429 do_ioctl = &file_ioctl;
430 }
431 else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
432 {
433 /* Partition. */
434 device = va.va_rdev;
435
436 p1 = ref->vp;
437 p2 = &ref->context;
438 do_ioctl = &device_ioctl;
439 }
440 else
441 {
442 /* Don't dump to non-regular files. */
443 error = EFAULT;
444 goto out;
445 }
446
447 // get partition base
448
449 error = do_ioctl(p1, p2, DKIOCGETBASE, (caddr_t) partitionbase_result);
450 if (error)
451 goto out;
452
453 // get block size & constraints
454
455 error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &blksize);
456 if (error)
457 goto out;
458
459 maxiocount = 1*1024*1024*1024;
460
461 error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTREAD, (caddr_t) &count);
462 if (error)
463 count = 0;
464 count *= blksize;
465 if (count && (count < maxiocount))
466 maxiocount = count;
467
468 error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTWRITE, (caddr_t) &count);
469 if (error)
470 count = 0;
471 count *= blksize;
472 if (count && (count < maxiocount))
473 maxiocount = count;
474
475 error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTREAD, (caddr_t) &count);
476 if (error)
477 count = 0;
478 if (count && (count < maxiocount))
479 maxiocount = count;
480
481 error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTWRITE, (caddr_t) &count);
482 if (error)
483 count = 0;
484 if (count && (count < maxiocount))
485 maxiocount = count;
486
487 error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTREAD, (caddr_t) &count);
488 if (error)
489 count = 0;
490 if (count && (count < maxiocount))
491 maxiocount = count;
492
493 error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTWRITE, (caddr_t) &count);
494 if (error)
495 count = 0;
496 if (count && (count < maxiocount))
497 maxiocount = count;
498
499 kprintf("max io 0x%qx bytes\n", maxiocount);
500 if (maxiocount_result)
501 *maxiocount_result = maxiocount;
502
503 // generate the block list
504
505 error = 0;
506 if (ref->vp->v_type == VREG)
507 {
508 f_offset = 0;
509 while(f_offset < (off_t) va.va_data_size)
510 {
511 size_t io_size = 1*1024*1024*1024;
512 daddr64_t blkno;
513
514 error = VNOP_BLOCKMAP(ref->vp, f_offset, io_size, &blkno, (size_t *)&io_size, NULL, 0, NULL);
515 if (error)
516 goto out;
517 callback(callback_ref, ((uint64_t) blkno) * blksize, (uint64_t) io_size);
518 f_offset += io_size;
519 }
520 callback(callback_ref, 0ULL, 0ULL);
521 }
522 else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
523 {
524 error = do_ioctl(p1, p2, DKIOCGETBLOCKCOUNT, (caddr_t) &size);
525 if (error)
526 goto out;
527 size *= blksize;
528 callback(callback_ref, 0ULL, size);
529 callback(callback_ref, size, 0ULL);
530 }
531
532 if (device_result)
533 *device_result = device;
534
535 out:
536 kprintf("kern_open_file_for_direct_io(%d)\n", error);
537
538 if (error && ref) {
539 if (ref->vp)
540 vnode_close(ref->vp, FWRITE, &ref->context);
541
542 kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
543 }
544
545 return(ref);
546 }
547
548 int
549 kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, caddr_t addr, vm_size_t len)
550 {
551 return (vn_rdwr(UIO_WRITE, ref->vp,
552 addr, len, offset,
553 UIO_SYSSPACE32, IO_SYNC|IO_NODELOCKED|IO_UNIT,
554 ref->context.vc_ucred, (int *) 0, ref->context.vc_proc));
555 }
556
557 void
558 kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref)
559 {
560 kprintf("kern_close_file_for_direct_io\n");
561
562 if (ref) {
563 int error;
564
565 if (ref->vp) {
566 error = vnode_close(ref->vp, FWRITE, &ref->context);
567 kprintf("vnode_close(%d)\n", error);
568 }
569 kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
570 }
571 }