2 * Copyright (c) 2008-2009, Stacey Son <sson@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
35 #include <sys/ksyms.h>
36 #include <sys/linker.h>
37 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
42 #include <sys/queue.h>
43 #include <sys/resourcevar.h>
47 #include <machine/elf.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_map.h>
54 #include "linker_if.h"
59 #define SHDR_SHSTRTAB 3
63 #define STR_SYMTAB ".symtab"
64 #define STR_STRTAB ".strtab"
65 #define STR_SHSTRTAB ".shstrtab"
67 #define KSYMS_DNAME "ksyms"
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;
75 static struct cdevsw ksyms_cdevsw = {
76 .d_version = D_VERSION,
77 .d_flags = D_PSEUDO | D_TRACKCLOSE,
79 .d_close = ksyms_close,
81 .d_ioctl = ksyms_ioctl,
87 LIST_ENTRY(ksyms_softc) sc_list;
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);
99 static const char ksyms_shstrtab[] =
100 "\0" STR_SYMTAB "\0" STR_STRTAB "\0" STR_SHSTRTAB "\0";
106 Elf_Shdr kh_shdr[SHDR_NUM];
107 char kh_shstrtab[sizeof(ksyms_shstrtab)];
116 vm_offset_t to_symoff;
117 vm_offset_t to_stroff;
122 static MALLOC_DEFINE(M_KSYMS, "KSYMS", "Kernel Symbol Table");
125 * Get the symbol and string table sizes for a kernel module. Add it to the
129 ksyms_size_permod(linker_file_t lf, void *arg)
132 const Elf_Sym *symtab;
138 syms = LINKER_SYMTAB_GET(lf, &symtab);
139 ts->ts_symsz += syms * sizeof(Elf_Sym);
140 ts->ts_strsz += LINKER_STRTAB_GET(lf, &strtab);
146 * For kernel module get the symbol and string table sizes, returning the
150 ksyms_size_calc(struct tsizes *ts)
155 (void) linker_file_foreach(ksyms_size_permod, ts);
158 #define KSYMS_EMIT(src, des, sz) do { \
159 copyout(src, (void *)des, sz); \
163 #define SYMBLKSZ 256 * sizeof (Elf_Sym)
166 * For a kernel module, add the symbol and string tables into the
167 * snapshot buffer. Fix up the offsets in the tables.
170 ksyms_add(linker_file_t lf, void *arg)
173 const Elf_Sym *symtab;
177 size_t strsz, numsyms;
178 linker_symval_t symval;
185 numsyms = LINKER_SYMTAB_GET(lf, &symtab);
186 strsz = LINKER_STRTAB_GET(lf, &strtab);
187 symsz = numsyms * sizeof(Elf_Sym);
189 buf = malloc(SYMBLKSZ, M_KSYMS, M_WAITOK);
192 len = min(SYMBLKSZ, symsz);
193 bcopy(symtab, buf, len);
196 * Fix up symbol table for kernel modules:
197 * string offsets need adjusted
198 * symbol values made absolute
200 symp = (Elf_Sym *) buf;
201 nsyms = len / sizeof (Elf_Sym);
202 for (i = 0; i < nsyms; i++) {
203 symp[i].st_name += to->to_stridx;
204 if (lf->id > 1 && LINKER_SYMBOL_VALUES(lf,
205 (c_linker_sym_t) &symtab[i], &symval) == 0) {
206 symp[i].st_value = (uintptr_t) symval.value;
210 if (len > to->to_resid) {
216 KSYMS_EMIT(buf, to->to_symoff, len);
224 if (strsz > to->to_resid)
227 to->to_resid -= strsz;
228 KSYMS_EMIT(strtab, to->to_stroff, strsz);
229 to->to_stridx += strsz;
235 * Create a single ELF symbol table for the kernel and kernel modules loaded
236 * at this time. Write this snapshot out in the process address space. Return
237 * 0 on success, otherwise error.
240 ksyms_snapshot(struct tsizes *ts, vm_offset_t uaddr, size_t resid)
243 struct ksyms_hdr *hdr;
247 /* Be kernel stack friendly */
248 hdr = malloc(sizeof (*hdr), M_KSYMS, M_WAITOK|M_ZERO);
251 * Create the ELF header.
253 hdr->kh_ehdr.e_ident[EI_PAD] = 0;
254 hdr->kh_ehdr.e_ident[EI_MAG0] = ELFMAG0;
255 hdr->kh_ehdr.e_ident[EI_MAG1] = ELFMAG1;
256 hdr->kh_ehdr.e_ident[EI_MAG2] = ELFMAG2;
257 hdr->kh_ehdr.e_ident[EI_MAG3] = ELFMAG3;
258 hdr->kh_ehdr.e_ident[EI_DATA] = ELF_DATA;
259 hdr->kh_ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
260 hdr->kh_ehdr.e_ident[EI_CLASS] = ELF_CLASS;
261 hdr->kh_ehdr.e_ident[EI_VERSION] = EV_CURRENT;
262 hdr->kh_ehdr.e_ident[EI_ABIVERSION] = 0;
263 hdr->kh_ehdr.e_type = ET_EXEC;
264 hdr->kh_ehdr.e_machine = ELF_ARCH;
265 hdr->kh_ehdr.e_version = EV_CURRENT;
266 hdr->kh_ehdr.e_entry = 0;
267 hdr->kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_txtphdr);
268 hdr->kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr);
269 hdr->kh_ehdr.e_flags = 0;
270 hdr->kh_ehdr.e_ehsize = sizeof(Elf_Ehdr);
271 hdr->kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
272 hdr->kh_ehdr.e_phnum = 2; /* Text and Data */
273 hdr->kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
274 hdr->kh_ehdr.e_shnum = SHDR_NUM;
275 hdr->kh_ehdr.e_shstrndx = SHDR_SHSTRTAB;
278 * Add both the text and data Program headers.
280 hdr->kh_txtphdr.p_type = PT_LOAD;
281 /* XXX - is there a way to put the actual .text addr/size here? */
282 hdr->kh_txtphdr.p_vaddr = 0;
283 hdr->kh_txtphdr.p_memsz = 0;
284 hdr->kh_txtphdr.p_flags = PF_R | PF_X;
286 hdr->kh_datphdr.p_type = PT_LOAD;
287 /* XXX - is there a way to put the actual .data addr/size here? */
288 hdr->kh_datphdr.p_vaddr = 0;
289 hdr->kh_datphdr.p_memsz = 0;
290 hdr->kh_datphdr.p_flags = PF_R | PF_W | PF_X;
293 * Add the Section headers: null, symtab, strtab, shstrtab,
296 /* First section header - null */
298 /* Second section header - symtab */
299 hdr->kh_shdr[SHDR_SYMTAB].sh_name = 1; /* String offset (skip null) */
300 hdr->kh_shdr[SHDR_SYMTAB].sh_type = SHT_SYMTAB;
301 hdr->kh_shdr[SHDR_SYMTAB].sh_flags = 0;
302 hdr->kh_shdr[SHDR_SYMTAB].sh_addr = 0;
303 hdr->kh_shdr[SHDR_SYMTAB].sh_offset = sizeof(*hdr);
304 hdr->kh_shdr[SHDR_SYMTAB].sh_size = ts->ts_symsz;
305 hdr->kh_shdr[SHDR_SYMTAB].sh_link = SHDR_STRTAB;
306 hdr->kh_shdr[SHDR_SYMTAB].sh_info = ts->ts_symsz / sizeof(Elf_Sym);
307 hdr->kh_shdr[SHDR_SYMTAB].sh_addralign = sizeof(long);
308 hdr->kh_shdr[SHDR_SYMTAB].sh_entsize = sizeof(Elf_Sym);
310 /* Third section header - strtab */
311 hdr->kh_shdr[SHDR_STRTAB].sh_name = 1 + sizeof(STR_SYMTAB);
312 hdr->kh_shdr[SHDR_STRTAB].sh_type = SHT_STRTAB;
313 hdr->kh_shdr[SHDR_STRTAB].sh_flags = 0;
314 hdr->kh_shdr[SHDR_STRTAB].sh_addr = 0;
315 hdr->kh_shdr[SHDR_STRTAB].sh_offset =
316 hdr->kh_shdr[SHDR_SYMTAB].sh_offset + ts->ts_symsz;
317 hdr->kh_shdr[SHDR_STRTAB].sh_size = ts->ts_strsz;
318 hdr->kh_shdr[SHDR_STRTAB].sh_link = 0;
319 hdr->kh_shdr[SHDR_STRTAB].sh_info = 0;
320 hdr->kh_shdr[SHDR_STRTAB].sh_addralign = sizeof(char);
321 hdr->kh_shdr[SHDR_STRTAB].sh_entsize = 0;
323 /* Fourth section - shstrtab */
324 hdr->kh_shdr[SHDR_SHSTRTAB].sh_name = 1 + sizeof(STR_SYMTAB) +
326 hdr->kh_shdr[SHDR_SHSTRTAB].sh_type = SHT_STRTAB;
327 hdr->kh_shdr[SHDR_SHSTRTAB].sh_flags = 0;
328 hdr->kh_shdr[SHDR_SHSTRTAB].sh_addr = 0;
329 hdr->kh_shdr[SHDR_SHSTRTAB].sh_offset =
330 offsetof(struct ksyms_hdr, kh_shstrtab);
331 hdr->kh_shdr[SHDR_SHSTRTAB].sh_size = sizeof(ksyms_shstrtab);
332 hdr->kh_shdr[SHDR_SHSTRTAB].sh_link = 0;
333 hdr->kh_shdr[SHDR_SHSTRTAB].sh_info = 0;
334 hdr->kh_shdr[SHDR_SHSTRTAB].sh_addralign = 0 /* sizeof(char) */;
335 hdr->kh_shdr[SHDR_SHSTRTAB].sh_entsize = 0;
337 /* Copy shstrtab into the header */
338 bcopy(ksyms_shstrtab, hdr->kh_shstrtab, sizeof(ksyms_shstrtab));
340 to.to_symoff = uaddr + hdr->kh_shdr[SHDR_SYMTAB].sh_offset;
341 to.to_stroff = uaddr + hdr->kh_shdr[SHDR_STRTAB].sh_offset;
343 if (sizeof(struct ksyms_hdr) > resid) {
347 to.to_resid = resid - sizeof(struct ksyms_hdr);
350 copyout(hdr, (void *)uaddr, sizeof(struct ksyms_hdr));
354 /* Add symbol and string tables for each kernelmodule */
355 error = linker_file_foreach(ksyms_add, &to);
357 if (to.to_resid != 0)
364 * Map some anonymous memory in user space of size sz, rounded up to the page
368 ksyms_map(struct thread *td, vm_offset_t *addr, size_t sz)
370 struct vmspace *vms = td->td_proc->p_vmspace;
376 * Map somewhere after heap in process memory.
378 PROC_LOCK(td->td_proc);
379 *addr = round_page((vm_offset_t)vms->vm_daddr +
380 lim_max(td->td_proc, RLIMIT_DATA));
381 PROC_UNLOCK(td->td_proc);
383 /* round size up to page boundry */
384 size = (vm_size_t) round_page(sz);
386 error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE,
387 VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0);
393 * Unmap memory in user space.
396 ksyms_unmap(struct thread *td, vm_offset_t addr, size_t sz)
401 map = &td->td_proc->p_vmspace->vm_map;
402 size = (vm_size_t) round_page(sz);
404 if (!vm_map_remove(map, addr, addr + size))
411 ksyms_cdevpriv_dtr(void *data)
413 struct ksyms_softc *sc;
415 sc = (struct ksyms_softc *)data;
417 mtx_lock(&ksyms_mtx);
418 LIST_REMOVE(sc, sc_list);
419 mtx_unlock(&ksyms_mtx);
425 ksyms_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
430 struct ksyms_softc *sc;
433 * Limit one open() per process. The process must close()
434 * before open()'ing again.
436 mtx_lock(&ksyms_mtx);
437 LIST_FOREACH(sc, &ksyms_list, sc_list) {
438 if (sc->sc_proc == td->td_proc) {
439 mtx_unlock(&ksyms_mtx);
444 sc = (struct ksyms_softc *) malloc(sizeof (*sc), M_KSYMS,
448 mtx_unlock(&ksyms_mtx);
451 sc->sc_proc = td->td_proc;
452 sc->sc_pmap = &td->td_proc->p_vmspace->vm_pmap;
453 LIST_INSERT_HEAD(&ksyms_list, sc, sc_list);
454 mtx_unlock(&ksyms_mtx);
456 error = devfs_set_cdevpriv(sc, ksyms_cdevpriv_dtr);
461 * MOD_SLOCK doesn't work here (because of a lock reversal with
462 * KLD_SLOCK). Therefore, simply try upto 3 times to get a "clean"
463 * snapshot of the kernel symbol table. This should work fine in the
464 * rare case of a kernel module being loaded/unloaded at the same
467 for(try = 0; try < 3; try++) {
469 * Map a buffer in the calling process memory space and
470 * create a snapshot of the kernel symbol table in it.
473 /* Compute the size of buffer needed. */
474 ksyms_size_calc(&ts);
475 total_elf_sz = sizeof(struct ksyms_hdr) + ts.ts_symsz +
478 error = ksyms_map(td, &(sc->sc_uaddr),
479 (vm_size_t) total_elf_sz);
482 sc->sc_usize = total_elf_sz;
484 error = ksyms_snapshot(&ts, sc->sc_uaddr, total_elf_sz);
486 /* Successful Snapshot */
490 /* Snapshot failed, unmap the memory and try again */
491 (void) ksyms_unmap(td, sc->sc_uaddr, sc->sc_usize);
495 ksyms_cdevpriv_dtr(sc);
501 ksyms_read(struct cdev *dev, struct uio *uio, int flags __unused)
505 struct ksyms_softc *sc;
510 error = devfs_get_cdevpriv((void **)&sc);
514 off = uio->uio_offset;
515 len = uio->uio_resid;
517 if (off < 0 || off > sc->sc_usize)
520 if (len > (sc->sc_usize - off))
521 len = sc->sc_usize - off;
527 * Since the snapshot buffer is in the user space we have to copy it
528 * in to the kernel and then back out. The extra copy saves valuable
531 buf = malloc(PAGE_SIZE, M_KSYMS, M_WAITOK);
532 ubase = sc->sc_uaddr + off;
536 sz = min(PAGE_SIZE, len);
537 if (copyin((void *)ubase, buf, sz))
540 error = uiomove(buf, sz, uio);
555 ksyms_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int32_t flag __unused,
556 struct thread *td __unused)
559 struct ksyms_softc *sc;
561 error = devfs_get_cdevpriv((void **)&sc);
568 * Return the size (in bytes) of the symbol table
571 *(size_t *)data = sc->sc_usize;
576 * Return the address of the symbol table snapshot.
577 * XXX - compat32 version of this?
579 *(void **)data = (void *)sc->sc_uaddr;
592 ksyms_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr,
595 struct ksyms_softc *sc;
598 error = devfs_get_cdevpriv((void **)&sc);
603 * XXX mmap() will actually map the symbol table into the process
604 * address space again.
606 if (offset > round_page(sc->sc_usize) ||
607 (*paddr = pmap_extract(sc->sc_pmap,
608 (vm_offset_t)sc->sc_uaddr + offset)) == 0)
616 ksyms_close(struct cdev *dev, int flags __unused, int fmt __unused,
620 struct ksyms_softc *sc;
622 error = devfs_get_cdevpriv((void **)&sc);
626 /* Unmap the buffer from the process address space. */
627 error = ksyms_unmap(td, sc->sc_uaddr, sc->sc_usize);
629 devfs_clear_cdevpriv();
636 ksyms_modevent(module_t mod __unused, int type, void *data __unused)
642 mtx_init(&ksyms_mtx, "KSyms mtx", NULL, MTX_DEF);
643 ksyms_dev = make_dev(&ksyms_cdevsw, 0, UID_ROOT, GID_WHEEL,
648 if (!LIST_EMPTY(&ksyms_list))
650 destroy_dev(ksyms_dev);
651 mtx_destroy(&ksyms_mtx);
664 DEV_MODULE(ksyms, ksyms_modevent, NULL);
665 MODULE_VERSION(ksyms, 1);