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