xref: /freebsd/sys/dev/ksyms/ksyms.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
1 /*-
2  * Copyright (c) 2008-2009, Stacey Son <sson@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 
33 #include <sys/conf.h>
34 #include <sys/elf.h>
35 #include <sys/ksyms.h>
36 #include <sys/linker.h>
37 #include <sys/malloc.h>
38 #include <sys/mman.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/proc.h>
42 #include <sys/queue.h>
43 #include <sys/resourcevar.h>
44 #include <sys/stat.h>
45 #include <sys/uio.h>
46 
47 #include <machine/elf.h>
48 
49 #include <vm/pmap.h>
50 #include <vm/vm.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_map.h>
53 
54 #include "linker_if.h"
55 
56 #define SHDR_NULL	0
57 #define SHDR_SYMTAB	1
58 #define SHDR_STRTAB	2
59 #define SHDR_SHSTRTAB	3
60 
61 #define SHDR_NUM	4
62 
63 #define STR_SYMTAB	".symtab"
64 #define STR_STRTAB	".strtab"
65 #define STR_SHSTRTAB	".shstrtab"
66 
67 #define KSYMS_DNAME	"ksyms"
68 
69 static	d_open_t 	ksyms_open;
70 static	d_read_t	ksyms_read;
71 static	d_close_t	ksyms_close;
72 static	d_ioctl_t	ksyms_ioctl;
73 static	d_mmap_t	ksyms_mmap;
74 
75 static struct cdevsw ksyms_cdevsw = {
76     .d_version	=	D_VERSION,
77     .d_flags	=	D_PSEUDO | D_TRACKCLOSE,
78     .d_open	=	ksyms_open,
79     .d_close	=	ksyms_close,
80     .d_read	=	ksyms_read,
81     .d_ioctl	=	ksyms_ioctl,
82     .d_mmap	=	ksyms_mmap,
83     .d_name	=	KSYMS_DNAME
84 };
85 
86 struct ksyms_softc {
87 	LIST_ENTRY(ksyms_softc)	sc_list;
88 	vm_offset_t 		sc_uaddr;
89 	size_t 			sc_usize;
90 	pmap_t			sc_pmap;
91 	struct proc	       *sc_proc;
92 };
93 
94 static struct mtx 		 ksyms_mtx;
95 static struct cdev 		*ksyms_dev;
96 static LIST_HEAD(, ksyms_softc)	 ksyms_list =
97 	LIST_HEAD_INITIALIZER(&ksyms_list);
98 
99 static const char 	ksyms_shstrtab[] =
100 	"\0" STR_SYMTAB "\0" STR_STRTAB "\0" STR_SHSTRTAB "\0";
101 
102 struct ksyms_hdr {
103 	Elf_Ehdr	kh_ehdr;
104 	Elf_Phdr	kh_txtphdr;
105 	Elf_Phdr	kh_datphdr;
106 	Elf_Shdr	kh_shdr[SHDR_NUM];
107 	char		kh_shstrtab[sizeof(ksyms_shstrtab)];
108 };
109 
110 struct tsizes {
111 	size_t		ts_symsz;
112 	size_t		ts_strsz;
113 };
114 
115 struct toffsets {
116 	vm_offset_t	to_symoff;
117 	vm_offset_t	to_stroff;
118 	unsigned	to_stridx;
119 	size_t		to_resid;
120 };
121 
122 static MALLOC_DEFINE(M_KSYMS, "KSYMS", "Kernel Symbol Table");
123 
124 /*
125  * Get the symbol and string table sizes for a kernel module. Add it to the
126  * running total.
127  */
128 static int
129 ksyms_size_permod(linker_file_t lf, void *arg)
130 {
131 	struct tsizes *ts;
132 	Elf_Sym *symtab;
133 	caddr_t strtab;
134 	long syms;
135 
136 	ts = arg;
137 
138 	syms = LINKER_SYMTAB_GET(lf, &symtab);
139 	ts->ts_symsz += syms * sizeof(Elf_Sym);
140 	ts->ts_strsz += LINKER_STRTAB_GET(lf, &strtab);
141 
142 	return (0);
143 }
144 
145 /*
146  * For kernel module get the symbol and string table sizes, returning the
147  * totals in *ts.
148  */
149 static void
150 ksyms_size_calc(struct tsizes *ts)
151 {
152 	ts->ts_symsz = 0;
153 	ts->ts_strsz = 0;
154 
155 	(void) linker_file_foreach(ksyms_size_permod, ts);
156 }
157 
158 #define KSYMS_EMIT(src, des, sz) do {				\
159 		copyout(src, (void *)des, sz);			\
160 		des += sz;					\
161 	} while (0)
162 
163 #define SYMBLKSZ	256 * sizeof (Elf_Sym)
164 
165 /*
166  * For a kernel module, add the symbol and string tables into the
167  * snapshot buffer.  Fix up the offsets in the tables.
168  */
169 static int
170 ksyms_add(linker_file_t lf, void *arg)
171 {
172 	struct toffsets *to;
173 	Elf_Sym *symtab, *symp;
174 	caddr_t strtab;
175 	long symsz;
176 	size_t strsz, numsyms;
177 	linker_symval_t symval;
178 	char *buf;
179 	int i, nsyms, len;
180 
181 	to = arg;
182 
183 	MOD_SLOCK;
184 	numsyms =  LINKER_SYMTAB_GET(lf, &symtab);
185 	strsz = LINKER_STRTAB_GET(lf, &strtab);
186 	symsz = numsyms * sizeof(Elf_Sym);
187 
188 	buf = malloc(SYMBLKSZ, M_KSYMS, M_WAITOK);
189 
190 	while (symsz > 0) {
191 		len = min(SYMBLKSZ, symsz);
192 		bcopy(symtab, buf, len);
193 
194 		/*
195 		 * Fix up symbol table for kernel modules:
196 		 *   string offsets need adjusted
197 		 *   symbol values made absolute
198 		 */
199 		symp = (Elf_Sym *) buf;
200 		nsyms = len / sizeof (Elf_Sym);
201 		for (i = 0; i < nsyms; i++) {
202 			symp[i].st_name += to->to_stridx;
203 			if (lf->id > 1 && LINKER_SYMBOL_VALUES(lf,
204 				(c_linker_sym_t) &symtab[i], &symval) == 0) {
205 				symp[i].st_value = (uintptr_t) symval.value;
206 			}
207 		}
208 
209 		if (len > to->to_resid) {
210 			MOD_SUNLOCK;
211 			free(buf, M_KSYMS);
212 			return (ENXIO);
213 		} else
214 			to->to_resid -= len;
215 		KSYMS_EMIT(buf, to->to_symoff, len);
216 
217 		symtab += nsyms;
218 		symsz -= len;
219 	}
220 	free(buf, M_KSYMS);
221 	MOD_SUNLOCK;
222 
223 	if (strsz > to->to_resid)
224 		return (ENXIO);
225 	else
226 		to->to_resid -= strsz;
227 	KSYMS_EMIT(strtab, to->to_stroff, strsz);
228 	to->to_stridx += strsz;
229 
230 	return (0);
231 }
232 
233 /*
234  * Create a single ELF symbol table for the kernel and kernel modules loaded
235  * at this time. Write this snapshot out in the process address space. Return
236  * 0 on success, otherwise error.
237  */
238 static int
239 ksyms_snapshot(struct tsizes *ts, vm_offset_t uaddr, size_t resid)
240 {
241 
242 	struct ksyms_hdr *hdr;
243 	struct toffsets	 to;
244 	int error = 0;
245 
246 	/* Be kernel stack friendly */
247 	hdr = malloc(sizeof (*hdr), M_KSYMS, M_WAITOK|M_ZERO);
248 
249 	/*
250 	 * Create the ELF header.
251 	 */
252 	hdr->kh_ehdr.e_ident[EI_PAD] = 0;
253 	hdr->kh_ehdr.e_ident[EI_MAG0] = ELFMAG0;
254 	hdr->kh_ehdr.e_ident[EI_MAG1] = ELFMAG1;
255 	hdr->kh_ehdr.e_ident[EI_MAG2] = ELFMAG2;
256 	hdr->kh_ehdr.e_ident[EI_MAG3] = ELFMAG3;
257 	hdr->kh_ehdr.e_ident[EI_DATA] = ELF_DATA;
258 	hdr->kh_ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
259 	hdr->kh_ehdr.e_ident[EI_CLASS] = ELF_CLASS;
260 	hdr->kh_ehdr.e_ident[EI_VERSION] = EV_CURRENT;
261 	hdr->kh_ehdr.e_ident[EI_ABIVERSION] = 0;
262 	hdr->kh_ehdr.e_type = ET_EXEC;
263 	hdr->kh_ehdr.e_machine = ELF_ARCH;
264 	hdr->kh_ehdr.e_version = EV_CURRENT;
265 	hdr->kh_ehdr.e_entry = 0;
266 	hdr->kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_txtphdr);
267 	hdr->kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr);
268 	hdr->kh_ehdr.e_flags = 0;
269 	hdr->kh_ehdr.e_ehsize = sizeof(Elf_Ehdr);
270 	hdr->kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
271 	hdr->kh_ehdr.e_phnum = 2;	/* Text and Data */
272 	hdr->kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
273 	hdr->kh_ehdr.e_shnum = SHDR_NUM;
274 	hdr->kh_ehdr.e_shstrndx = SHDR_SHSTRTAB;
275 
276 	/*
277 	 * Add both the text and data Program headers.
278 	 */
279 	hdr->kh_txtphdr.p_type = PT_LOAD;
280 	/* XXX - is there a way to put the actual .text addr/size here? */
281 	hdr->kh_txtphdr.p_vaddr = 0;
282 	hdr->kh_txtphdr.p_memsz = 0;
283 	hdr->kh_txtphdr.p_flags = PF_R | PF_X;
284 
285 	hdr->kh_datphdr.p_type = PT_LOAD;
286 	/* XXX - is there a way to put the actual .data addr/size here? */
287 	hdr->kh_datphdr.p_vaddr = 0;
288 	hdr->kh_datphdr.p_memsz = 0;
289 	hdr->kh_datphdr.p_flags = PF_R | PF_W | PF_X;
290 
291 	/*
292 	 * Add the Section headers: null, symtab, strtab, shstrtab,
293 	 */
294 
295 	/* First section header - null */
296 
297 	/* Second section header - symtab */
298 	hdr->kh_shdr[SHDR_SYMTAB].sh_name = 1; /* String offset (skip null) */
299 	hdr->kh_shdr[SHDR_SYMTAB].sh_type = SHT_SYMTAB;
300 	hdr->kh_shdr[SHDR_SYMTAB].sh_flags = 0;
301 	hdr->kh_shdr[SHDR_SYMTAB].sh_addr = 0;
302 	hdr->kh_shdr[SHDR_SYMTAB].sh_offset = sizeof(*hdr);
303 	hdr->kh_shdr[SHDR_SYMTAB].sh_size = ts->ts_symsz;
304 	hdr->kh_shdr[SHDR_SYMTAB].sh_link = SHDR_STRTAB;
305 	hdr->kh_shdr[SHDR_SYMTAB].sh_info = ts->ts_symsz / sizeof(Elf_Sym);
306 	hdr->kh_shdr[SHDR_SYMTAB].sh_addralign = sizeof(long);
307 	hdr->kh_shdr[SHDR_SYMTAB].sh_entsize = sizeof(Elf_Sym);
308 
309 	/* Third section header - strtab */
310 	hdr->kh_shdr[SHDR_STRTAB].sh_name = 1 + sizeof(STR_SYMTAB);
311 	hdr->kh_shdr[SHDR_STRTAB].sh_type = SHT_STRTAB;
312 	hdr->kh_shdr[SHDR_STRTAB].sh_flags = 0;
313 	hdr->kh_shdr[SHDR_STRTAB].sh_addr = 0;
314 	hdr->kh_shdr[SHDR_STRTAB].sh_offset =
315 	    hdr->kh_shdr[SHDR_SYMTAB].sh_offset + ts->ts_symsz;
316 	hdr->kh_shdr[SHDR_STRTAB].sh_size = ts->ts_strsz;
317 	hdr->kh_shdr[SHDR_STRTAB].sh_link = 0;
318 	hdr->kh_shdr[SHDR_STRTAB].sh_info = 0;
319 	hdr->kh_shdr[SHDR_STRTAB].sh_addralign = sizeof(char);
320 	hdr->kh_shdr[SHDR_STRTAB].sh_entsize = 0;
321 
322 	/* Fourth section - shstrtab */
323 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_name = 1 + sizeof(STR_SYMTAB) +
324 	    sizeof(STR_STRTAB);
325 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_type = SHT_STRTAB;
326 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_flags = 0;
327 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_addr = 0;
328 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_offset =
329 	    offsetof(struct ksyms_hdr, kh_shstrtab);
330 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_size = sizeof(ksyms_shstrtab);
331 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_link = 0;
332 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_info = 0;
333 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_addralign = 0 /* sizeof(char) */;
334 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_entsize = 0;
335 
336 	/* Copy shstrtab into the header */
337 	bcopy(ksyms_shstrtab, hdr->kh_shstrtab, sizeof(ksyms_shstrtab));
338 
339 	to.to_symoff = uaddr + hdr->kh_shdr[SHDR_SYMTAB].sh_offset;
340 	to.to_stroff = uaddr + hdr->kh_shdr[SHDR_STRTAB].sh_offset;
341 	to.to_stridx = 0;
342 	if (sizeof(struct ksyms_hdr) > resid) {
343 		free(hdr, M_KSYMS);
344 		return (ENXIO);
345 	}
346 	to.to_resid = resid - sizeof(struct ksyms_hdr);
347 
348 	/* Emit Header */
349 	copyout(hdr, (void *)uaddr, sizeof(struct ksyms_hdr));
350 
351 	free(hdr, M_KSYMS);
352 
353 	/* Add symbol and string tables for each kernelmodule */
354 	error = linker_file_foreach(ksyms_add, &to);
355 
356 	if (to.to_resid != 0)
357 		return (ENXIO);
358 
359 	return (error);
360 }
361 
362 /*
363  * Map some anonymous memory in user space of size sz, rounded up to the page
364  * boundary.
365  */
366 static int
367 ksyms_map(struct thread *td, vm_offset_t *addr, size_t sz)
368 {
369 	struct vmspace *vms = td->td_proc->p_vmspace;
370 	int error;
371 	vm_size_t size;
372 
373 
374 	/*
375 	 * Map somewhere after heap in process memory.
376 	 */
377 	PROC_LOCK(td->td_proc);
378 	*addr = round_page((vm_offset_t)vms->vm_daddr +
379 	    lim_max(td->td_proc, RLIMIT_DATA));
380 	PROC_UNLOCK(td->td_proc);
381 
382 	/* round size up to page boundry */
383 	size = (vm_size_t) round_page(sz);
384 
385 	error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE,
386 	    VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0);
387 
388 	return (error);
389 }
390 
391 /*
392  * Unmap memory in user space.
393  */
394 static int
395 ksyms_unmap(struct thread *td, vm_offset_t addr, size_t sz)
396 {
397 	vm_map_t map;
398 	vm_size_t size;
399 
400 	map = &td->td_proc->p_vmspace->vm_map;
401 	size = (vm_size_t) round_page(sz);
402 
403 	if (!vm_map_remove(map, addr, addr + size))
404 		return (EINVAL);
405 
406 	return (0);
407 }
408 
409 static void
410 ksyms_cdevpriv_dtr(void *data)
411 {
412 	struct ksyms_softc *sc;
413 
414 	sc = (struct ksyms_softc *)data;
415 
416 	mtx_lock(&ksyms_mtx);
417 	LIST_REMOVE(sc, sc_list);
418 	mtx_unlock(&ksyms_mtx);
419 	free(sc, M_KSYMS);
420 }
421 
422 /* ARGSUSED */
423 static int
424 ksyms_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
425 {
426 	struct tsizes ts;
427 	size_t total_elf_sz;
428 	int error, try;
429 	struct ksyms_softc *sc;
430 
431 	/*
432 	 *  Limit one open() per process. The process must close()
433 	 *  before open()'ing again.
434 	 */
435 	mtx_lock(&ksyms_mtx);
436 	LIST_FOREACH(sc, &ksyms_list, sc_list) {
437 		if (sc->sc_proc == td->td_proc) {
438 			mtx_unlock(&ksyms_mtx);
439 			return (EBUSY);
440 		}
441 	}
442 
443 	sc = (struct ksyms_softc *) malloc(sizeof (*sc), M_KSYMS,
444 	    M_NOWAIT|M_ZERO);
445 
446 	if (sc == NULL) {
447 		mtx_unlock(&ksyms_mtx);
448 		return (ENOMEM);
449 	}
450 	sc->sc_proc = td->td_proc;
451 	sc->sc_pmap = &td->td_proc->p_vmspace->vm_pmap;
452 	LIST_INSERT_HEAD(&ksyms_list, sc, sc_list);
453 	mtx_unlock(&ksyms_mtx);
454 
455 	error = devfs_set_cdevpriv(sc, ksyms_cdevpriv_dtr);
456 	if (error)
457 		goto failed;
458 
459 	/*
460 	 * MOD_SLOCK doesn't work here (because of a lock reversal with
461 	 * KLD_SLOCK).  Therefore, simply try upto 3 times to get a "clean"
462 	 * snapshot of the kernel symbol table.  This should work fine in the
463 	 * rare case of a kernel module being loaded/unloaded at the same
464 	 * time.
465 	 */
466 	for(try = 0; try < 3; try++) {
467 		/*
468 	 	* Map a buffer in the calling process memory space and
469 	 	* create a snapshot of the kernel symbol table in it.
470 	 	*/
471 
472 		/* Compute the size of buffer needed. */
473 		ksyms_size_calc(&ts);
474 		total_elf_sz = sizeof(struct ksyms_hdr) + ts.ts_symsz +
475 			ts.ts_strsz;
476 
477 		error = ksyms_map(td, &(sc->sc_uaddr),
478 				(vm_size_t) total_elf_sz);
479 		if (error)
480 			break;
481 		sc->sc_usize = total_elf_sz;
482 
483 		error = ksyms_snapshot(&ts, sc->sc_uaddr, total_elf_sz);
484 		if (!error)  {
485 			/* Successful Snapshot */
486 			return (0);
487 		}
488 
489 		/* Snapshot failed, unmap the memory and try again */
490 		(void) ksyms_unmap(td, sc->sc_uaddr, sc->sc_usize);
491 	}
492 
493 failed:
494 	ksyms_cdevpriv_dtr(sc);
495 	return (error);
496 }
497 
498 /* ARGSUSED */
499 static int
500 ksyms_read(struct cdev *dev, struct uio *uio, int flags __unused)
501 {
502 	int error;
503 	size_t len, sz;
504 	struct ksyms_softc *sc;
505 	off_t off;
506 	char *buf;
507 	vm_size_t ubase;
508 
509 	error = devfs_get_cdevpriv((void **)&sc);
510 	if (error)
511 		return (error);
512 
513 	off = uio->uio_offset;
514 	len = uio->uio_resid;
515 
516 	if (off < 0 || off > sc->sc_usize)
517 		return (EFAULT);
518 
519 	if (len > (sc->sc_usize - off))
520 		len = sc->sc_usize - off;
521 
522 	if (len == 0)
523 		return (0);
524 
525 	/*
526 	 * Since the snapshot buffer is in the user space we have to copy it
527 	 * in to the kernel and then back out.  The extra copy saves valuable
528 	 * kernel memory.
529 	 */
530 	buf = malloc(PAGE_SIZE, M_KSYMS, M_WAITOK);
531 	ubase = sc->sc_uaddr + off;
532 
533 	while (len) {
534 
535 		sz = min(PAGE_SIZE, len);
536 		if (copyin((void *)ubase, buf, sz))
537 			error = EFAULT;
538 		else
539 			error = uiomove(buf, sz, uio);
540 
541 		if (error)
542 			break;
543 
544 		len -= sz;
545 		ubase += sz;
546 	}
547 	free(buf, M_KSYMS);
548 
549 	return (error);
550 }
551 
552 /* ARGSUSED */
553 static int
554 ksyms_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int32_t flag __unused,
555     struct thread *td __unused)
556 {
557 	int error = 0;
558 	struct ksyms_softc *sc;
559 
560 	error = devfs_get_cdevpriv((void **)&sc);
561 	if (error)
562 		return (error);
563 
564 	switch (cmd) {
565 	case KIOCGSIZE:
566 		/*
567 		 * Return the size (in bytes) of the symbol table
568 		 * snapshot.
569 		 */
570 		*(size_t *)data = sc->sc_usize;
571 		break;
572 
573 	case KIOCGADDR:
574 		/*
575 		 * Return the address of the symbol table snapshot.
576 		 * XXX - compat32 version of this?
577 		 */
578 		*(void **)data = (void *)sc->sc_uaddr;
579 		break;
580 
581 	default:
582 		error = ENOTTY;
583 		break;
584 	}
585 
586 	return (error);
587 }
588 
589 /* ARGUSED */
590 static int
591 ksyms_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr,
592 		int prot __unused)
593 {
594     	struct ksyms_softc *sc;
595 	int error;
596 
597 	error = devfs_get_cdevpriv((void **)&sc);
598 	if (error)
599 		return (error);
600 
601 	/*
602 	 * XXX mmap() will actually map the symbol table into the process
603 	 * address space again.
604 	 */
605 	if (offset > round_page(sc->sc_usize) ||
606 	    (*paddr = pmap_extract(sc->sc_pmap,
607 	    (vm_offset_t)sc->sc_uaddr + offset)) == 0)
608 		return (-1);
609 
610 	return (0);
611 }
612 
613 /* ARGUSED */
614 static int
615 ksyms_close(struct cdev *dev, int flags __unused, int fmt __unused,
616 		struct thread *td)
617 {
618 	int error = 0;
619 	struct ksyms_softc *sc;
620 
621 	error = devfs_get_cdevpriv((void **)&sc);
622 	if (error)
623 		return (error);
624 
625 	/* Unmap the buffer from the process address space. */
626 	error = ksyms_unmap(td, sc->sc_uaddr, sc->sc_usize);
627 
628 	devfs_clear_cdevpriv();
629 
630 	return (error);
631 }
632 
633 /* ARGSUSED */
634 static int
635 ksyms_modevent(module_t mod __unused, int type, void *data __unused)
636 {
637 	int error = 0;
638 
639 	switch (type) {
640 	case MOD_LOAD:
641 		mtx_init(&ksyms_mtx, "KSyms mtx", NULL, MTX_DEF);
642 		ksyms_dev = make_dev(&ksyms_cdevsw, 0, UID_ROOT, GID_WHEEL,
643 		    0444, KSYMS_DNAME);
644 		break;
645 
646 	case MOD_UNLOAD:
647 		if (!LIST_EMPTY(&ksyms_list))
648 			return (EBUSY);
649 		destroy_dev(ksyms_dev);
650 		mtx_destroy(&ksyms_mtx);
651 		break;
652 
653 	case MOD_SHUTDOWN:
654 		break;
655 
656 	default:
657 		error = EOPNOTSUPP;
658 		break;
659 	}
660 	return (error);
661 }
662 
663 DEV_MODULE(ksyms, ksyms_modevent, NULL);
664 MODULE_VERSION(ksyms, 1);
665