100a5db46SStacey Son /*- 200a5db46SStacey Son * Copyright (c) 2008-2009, Stacey Son <sson@freebsd.org> 300a5db46SStacey Son * All rights reserved. 400a5db46SStacey Son * 500a5db46SStacey Son * Redistribution and use in source and binary forms, with or without 600a5db46SStacey Son * modification, are permitted provided that the following conditions 700a5db46SStacey Son * are met: 800a5db46SStacey Son * 1. Redistributions of source code must retain the above copyright 900a5db46SStacey Son * notice, this list of conditions and the following disclaimer. 1000a5db46SStacey Son * 2. Redistributions in binary form must reproduce the above copyright 1100a5db46SStacey Son * notice, this list of conditions and the following disclaimer in the 1200a5db46SStacey Son * documentation and/or other materials provided with the distribution. 1300a5db46SStacey Son * 1400a5db46SStacey Son * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1500a5db46SStacey Son * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1600a5db46SStacey Son * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1700a5db46SStacey Son * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1800a5db46SStacey Son * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1900a5db46SStacey Son * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2000a5db46SStacey Son * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2100a5db46SStacey Son * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2200a5db46SStacey Son * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2300a5db46SStacey Son * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2400a5db46SStacey Son * SUCH DAMAGE. 2500a5db46SStacey Son * 2600a5db46SStacey Son * $FreeBSD$ 2700a5db46SStacey Son */ 2800a5db46SStacey Son 2900a5db46SStacey Son #include <sys/param.h> 3000a5db46SStacey Son #include <sys/systm.h> 3100a5db46SStacey Son #include <sys/kernel.h> 3200a5db46SStacey Son 3300a5db46SStacey Son #include <sys/conf.h> 3400a5db46SStacey Son #include <sys/elf.h> 3500a5db46SStacey Son #include <sys/ksyms.h> 3600a5db46SStacey Son #include <sys/linker.h> 3700a5db46SStacey Son #include <sys/malloc.h> 3800a5db46SStacey Son #include <sys/mman.h> 3900a5db46SStacey Son #include <sys/module.h> 4000a5db46SStacey Son #include <sys/mutex.h> 4100a5db46SStacey Son #include <sys/proc.h> 4200a5db46SStacey Son #include <sys/queue.h> 4300a5db46SStacey Son #include <sys/resourcevar.h> 4400a5db46SStacey Son #include <sys/stat.h> 4500a5db46SStacey Son #include <sys/uio.h> 4600a5db46SStacey Son 4700a5db46SStacey Son #include <machine/elf.h> 4800a5db46SStacey Son 4900a5db46SStacey Son #include <vm/pmap.h> 5000a5db46SStacey Son #include <vm/vm.h> 5100a5db46SStacey Son #include <vm/vm_extern.h> 5200a5db46SStacey Son #include <vm/vm_map.h> 5300a5db46SStacey Son 5400a5db46SStacey Son #include "linker_if.h" 5500a5db46SStacey Son 5600a5db46SStacey Son #define SHDR_NULL 0 5700a5db46SStacey Son #define SHDR_SYMTAB 1 5800a5db46SStacey Son #define SHDR_STRTAB 2 5900a5db46SStacey Son #define SHDR_SHSTRTAB 3 6000a5db46SStacey Son 6100a5db46SStacey Son #define SHDR_NUM 4 6200a5db46SStacey Son 6300a5db46SStacey Son #define STR_SYMTAB ".symtab" 6400a5db46SStacey Son #define STR_STRTAB ".strtab" 6500a5db46SStacey Son #define STR_SHSTRTAB ".shstrtab" 6600a5db46SStacey Son 6700a5db46SStacey Son #define KSYMS_DNAME "ksyms" 6800a5db46SStacey Son 6900a5db46SStacey Son static d_open_t ksyms_open; 7000a5db46SStacey Son static d_read_t ksyms_read; 7100a5db46SStacey Son static d_close_t ksyms_close; 7200a5db46SStacey Son static d_ioctl_t ksyms_ioctl; 7300a5db46SStacey Son static d_mmap_t ksyms_mmap; 7400a5db46SStacey Son 7500a5db46SStacey Son static struct cdevsw ksyms_cdevsw = { 7600a5db46SStacey Son .d_version = D_VERSION, 7700a5db46SStacey Son .d_flags = D_PSEUDO | D_TRACKCLOSE, 7800a5db46SStacey Son .d_open = ksyms_open, 7900a5db46SStacey Son .d_close = ksyms_close, 8000a5db46SStacey Son .d_read = ksyms_read, 8100a5db46SStacey Son .d_ioctl = ksyms_ioctl, 8200a5db46SStacey Son .d_mmap = ksyms_mmap, 8300a5db46SStacey Son .d_name = KSYMS_DNAME 8400a5db46SStacey Son }; 8500a5db46SStacey Son 8600a5db46SStacey Son struct ksyms_softc { 8700a5db46SStacey Son LIST_ENTRY(ksyms_softc) sc_list; 8800a5db46SStacey Son vm_offset_t sc_uaddr; 8900a5db46SStacey Son size_t sc_usize; 9000a5db46SStacey Son pmap_t sc_pmap; 9100a5db46SStacey Son struct proc *sc_proc; 9200a5db46SStacey Son }; 9300a5db46SStacey Son 9400a5db46SStacey Son static struct mtx ksyms_mtx; 9500a5db46SStacey Son static struct cdev *ksyms_dev; 9600a5db46SStacey Son static LIST_HEAD(, ksyms_softc) ksyms_list = 9700a5db46SStacey Son LIST_HEAD_INITIALIZER(&ksyms_list); 9800a5db46SStacey Son 9900a5db46SStacey Son static const char ksyms_shstrtab[] = 10000a5db46SStacey Son "\0" STR_SYMTAB "\0" STR_STRTAB "\0" STR_SHSTRTAB "\0"; 10100a5db46SStacey Son 10200a5db46SStacey Son struct ksyms_hdr { 10300a5db46SStacey Son Elf_Ehdr kh_ehdr; 10400a5db46SStacey Son Elf_Phdr kh_txtphdr; 10500a5db46SStacey Son Elf_Phdr kh_datphdr; 10600a5db46SStacey Son Elf_Shdr kh_shdr[SHDR_NUM]; 10700a5db46SStacey Son char kh_shstrtab[sizeof(ksyms_shstrtab)]; 10800a5db46SStacey Son }; 10900a5db46SStacey Son 11000a5db46SStacey Son struct tsizes { 11100a5db46SStacey Son size_t ts_symsz; 11200a5db46SStacey Son size_t ts_strsz; 11300a5db46SStacey Son }; 11400a5db46SStacey Son 11500a5db46SStacey Son struct toffsets { 11600a5db46SStacey Son vm_offset_t to_symoff; 11700a5db46SStacey Son vm_offset_t to_stroff; 11800a5db46SStacey Son unsigned to_stridx; 11900a5db46SStacey Son size_t to_resid; 12000a5db46SStacey Son }; 12100a5db46SStacey Son 12200a5db46SStacey Son static MALLOC_DEFINE(M_KSYMS, "KSYMS", "Kernel Symbol Table"); 12300a5db46SStacey Son 12400a5db46SStacey Son /* 12500a5db46SStacey Son * Get the symbol and string table sizes for a kernel module. Add it to the 12600a5db46SStacey Son * running total. 12700a5db46SStacey Son */ 12800a5db46SStacey Son static int 12900a5db46SStacey Son ksyms_size_permod(linker_file_t lf, void *arg) 13000a5db46SStacey Son { 13100a5db46SStacey Son struct tsizes *ts; 132e76f11f4SAndriy Gapon const Elf_Sym *symtab; 13300a5db46SStacey Son caddr_t strtab; 13400a5db46SStacey Son long syms; 13500a5db46SStacey Son 13600a5db46SStacey Son ts = arg; 13700a5db46SStacey Son 13800a5db46SStacey Son syms = LINKER_SYMTAB_GET(lf, &symtab); 13900a5db46SStacey Son ts->ts_symsz += syms * sizeof(Elf_Sym); 14000a5db46SStacey Son ts->ts_strsz += LINKER_STRTAB_GET(lf, &strtab); 14100a5db46SStacey Son 14200a5db46SStacey Son return (0); 14300a5db46SStacey Son } 14400a5db46SStacey Son 14500a5db46SStacey Son /* 14600a5db46SStacey Son * For kernel module get the symbol and string table sizes, returning the 14700a5db46SStacey Son * totals in *ts. 14800a5db46SStacey Son */ 14900a5db46SStacey Son static void 15000a5db46SStacey Son ksyms_size_calc(struct tsizes *ts) 15100a5db46SStacey Son { 15200a5db46SStacey Son ts->ts_symsz = 0; 15300a5db46SStacey Son ts->ts_strsz = 0; 15400a5db46SStacey Son 15500a5db46SStacey Son (void) linker_file_foreach(ksyms_size_permod, ts); 15600a5db46SStacey Son } 15700a5db46SStacey Son 15800a5db46SStacey Son #define KSYMS_EMIT(src, des, sz) do { \ 15900a5db46SStacey Son copyout(src, (void *)des, sz); \ 16000a5db46SStacey Son des += sz; \ 16100a5db46SStacey Son } while (0) 16200a5db46SStacey Son 16300a5db46SStacey Son #define SYMBLKSZ 256 * sizeof (Elf_Sym) 16400a5db46SStacey Son 16500a5db46SStacey Son /* 16600a5db46SStacey Son * For a kernel module, add the symbol and string tables into the 16700a5db46SStacey Son * snapshot buffer. Fix up the offsets in the tables. 16800a5db46SStacey Son */ 16900a5db46SStacey Son static int 17000a5db46SStacey Son ksyms_add(linker_file_t lf, void *arg) 17100a5db46SStacey Son { 17200a5db46SStacey Son struct toffsets *to; 173e76f11f4SAndriy Gapon const Elf_Sym *symtab; 174e76f11f4SAndriy Gapon Elf_Sym *symp; 17500a5db46SStacey Son caddr_t strtab; 17600a5db46SStacey Son long symsz; 17700a5db46SStacey Son size_t strsz, numsyms; 17800a5db46SStacey Son linker_symval_t symval; 17900a5db46SStacey Son char *buf; 18000a5db46SStacey Son int i, nsyms, len; 18100a5db46SStacey Son 18200a5db46SStacey Son to = arg; 18300a5db46SStacey Son 18400a5db46SStacey Son MOD_SLOCK; 18500a5db46SStacey Son numsyms = LINKER_SYMTAB_GET(lf, &symtab); 18600a5db46SStacey Son strsz = LINKER_STRTAB_GET(lf, &strtab); 18700a5db46SStacey Son symsz = numsyms * sizeof(Elf_Sym); 18800a5db46SStacey Son 18900a5db46SStacey Son buf = malloc(SYMBLKSZ, M_KSYMS, M_WAITOK); 19000a5db46SStacey Son 19100a5db46SStacey Son while (symsz > 0) { 19200a5db46SStacey Son len = min(SYMBLKSZ, symsz); 19300a5db46SStacey Son bcopy(symtab, buf, len); 19400a5db46SStacey Son 19500a5db46SStacey Son /* 19600a5db46SStacey Son * Fix up symbol table for kernel modules: 19700a5db46SStacey Son * string offsets need adjusted 19800a5db46SStacey Son * symbol values made absolute 19900a5db46SStacey Son */ 20000a5db46SStacey Son symp = (Elf_Sym *) buf; 20100a5db46SStacey Son nsyms = len / sizeof (Elf_Sym); 20200a5db46SStacey Son for (i = 0; i < nsyms; i++) { 20300a5db46SStacey Son symp[i].st_name += to->to_stridx; 20400a5db46SStacey Son if (lf->id > 1 && LINKER_SYMBOL_VALUES(lf, 20500a5db46SStacey Son (c_linker_sym_t) &symtab[i], &symval) == 0) { 20600a5db46SStacey Son symp[i].st_value = (uintptr_t) symval.value; 20700a5db46SStacey Son } 20800a5db46SStacey Son } 20900a5db46SStacey Son 21000a5db46SStacey Son if (len > to->to_resid) { 21100a5db46SStacey Son MOD_SUNLOCK; 21200a5db46SStacey Son free(buf, M_KSYMS); 21300a5db46SStacey Son return (ENXIO); 21400a5db46SStacey Son } else 21500a5db46SStacey Son to->to_resid -= len; 21600a5db46SStacey Son KSYMS_EMIT(buf, to->to_symoff, len); 21700a5db46SStacey Son 21800a5db46SStacey Son symtab += nsyms; 21900a5db46SStacey Son symsz -= len; 22000a5db46SStacey Son } 22100a5db46SStacey Son free(buf, M_KSYMS); 22200a5db46SStacey Son MOD_SUNLOCK; 22300a5db46SStacey Son 22400a5db46SStacey Son if (strsz > to->to_resid) 22500a5db46SStacey Son return (ENXIO); 22600a5db46SStacey Son else 22700a5db46SStacey Son to->to_resid -= strsz; 22800a5db46SStacey Son KSYMS_EMIT(strtab, to->to_stroff, strsz); 22900a5db46SStacey Son to->to_stridx += strsz; 23000a5db46SStacey Son 23100a5db46SStacey Son return (0); 23200a5db46SStacey Son } 23300a5db46SStacey Son 23400a5db46SStacey Son /* 23500a5db46SStacey Son * Create a single ELF symbol table for the kernel and kernel modules loaded 23600a5db46SStacey Son * at this time. Write this snapshot out in the process address space. Return 23700a5db46SStacey Son * 0 on success, otherwise error. 23800a5db46SStacey Son */ 23900a5db46SStacey Son static int 24000a5db46SStacey Son ksyms_snapshot(struct tsizes *ts, vm_offset_t uaddr, size_t resid) 24100a5db46SStacey Son { 24200a5db46SStacey Son 24300a5db46SStacey Son struct ksyms_hdr *hdr; 24400a5db46SStacey Son struct toffsets to; 24500a5db46SStacey Son int error = 0; 24600a5db46SStacey Son 24700a5db46SStacey Son /* Be kernel stack friendly */ 24800a5db46SStacey Son hdr = malloc(sizeof (*hdr), M_KSYMS, M_WAITOK|M_ZERO); 24900a5db46SStacey Son 25000a5db46SStacey Son /* 25100a5db46SStacey Son * Create the ELF header. 25200a5db46SStacey Son */ 25300a5db46SStacey Son hdr->kh_ehdr.e_ident[EI_PAD] = 0; 25400a5db46SStacey Son hdr->kh_ehdr.e_ident[EI_MAG0] = ELFMAG0; 25500a5db46SStacey Son hdr->kh_ehdr.e_ident[EI_MAG1] = ELFMAG1; 25600a5db46SStacey Son hdr->kh_ehdr.e_ident[EI_MAG2] = ELFMAG2; 25700a5db46SStacey Son hdr->kh_ehdr.e_ident[EI_MAG3] = ELFMAG3; 25800a5db46SStacey Son hdr->kh_ehdr.e_ident[EI_DATA] = ELF_DATA; 25900a5db46SStacey Son hdr->kh_ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 26000a5db46SStacey Son hdr->kh_ehdr.e_ident[EI_CLASS] = ELF_CLASS; 26100a5db46SStacey Son hdr->kh_ehdr.e_ident[EI_VERSION] = EV_CURRENT; 26200a5db46SStacey Son hdr->kh_ehdr.e_ident[EI_ABIVERSION] = 0; 26300a5db46SStacey Son hdr->kh_ehdr.e_type = ET_EXEC; 26400a5db46SStacey Son hdr->kh_ehdr.e_machine = ELF_ARCH; 26500a5db46SStacey Son hdr->kh_ehdr.e_version = EV_CURRENT; 26600a5db46SStacey Son hdr->kh_ehdr.e_entry = 0; 26700a5db46SStacey Son hdr->kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_txtphdr); 26800a5db46SStacey Son hdr->kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr); 26900a5db46SStacey Son hdr->kh_ehdr.e_flags = 0; 27000a5db46SStacey Son hdr->kh_ehdr.e_ehsize = sizeof(Elf_Ehdr); 27100a5db46SStacey Son hdr->kh_ehdr.e_phentsize = sizeof(Elf_Phdr); 27200a5db46SStacey Son hdr->kh_ehdr.e_phnum = 2; /* Text and Data */ 27300a5db46SStacey Son hdr->kh_ehdr.e_shentsize = sizeof(Elf_Shdr); 27400a5db46SStacey Son hdr->kh_ehdr.e_shnum = SHDR_NUM; 27500a5db46SStacey Son hdr->kh_ehdr.e_shstrndx = SHDR_SHSTRTAB; 27600a5db46SStacey Son 27700a5db46SStacey Son /* 27800a5db46SStacey Son * Add both the text and data Program headers. 27900a5db46SStacey Son */ 28000a5db46SStacey Son hdr->kh_txtphdr.p_type = PT_LOAD; 28100a5db46SStacey Son /* XXX - is there a way to put the actual .text addr/size here? */ 28200a5db46SStacey Son hdr->kh_txtphdr.p_vaddr = 0; 28300a5db46SStacey Son hdr->kh_txtphdr.p_memsz = 0; 28400a5db46SStacey Son hdr->kh_txtphdr.p_flags = PF_R | PF_X; 28500a5db46SStacey Son 28600a5db46SStacey Son hdr->kh_datphdr.p_type = PT_LOAD; 28700a5db46SStacey Son /* XXX - is there a way to put the actual .data addr/size here? */ 28800a5db46SStacey Son hdr->kh_datphdr.p_vaddr = 0; 28900a5db46SStacey Son hdr->kh_datphdr.p_memsz = 0; 29000a5db46SStacey Son hdr->kh_datphdr.p_flags = PF_R | PF_W | PF_X; 29100a5db46SStacey Son 29200a5db46SStacey Son /* 29300a5db46SStacey Son * Add the Section headers: null, symtab, strtab, shstrtab, 29400a5db46SStacey Son */ 29500a5db46SStacey Son 29600a5db46SStacey Son /* First section header - null */ 29700a5db46SStacey Son 29800a5db46SStacey Son /* Second section header - symtab */ 29900a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_name = 1; /* String offset (skip null) */ 30000a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_type = SHT_SYMTAB; 30100a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_flags = 0; 30200a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_addr = 0; 30300a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_offset = sizeof(*hdr); 30400a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_size = ts->ts_symsz; 30500a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_link = SHDR_STRTAB; 30600a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_info = ts->ts_symsz / sizeof(Elf_Sym); 30700a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_addralign = sizeof(long); 30800a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_entsize = sizeof(Elf_Sym); 30900a5db46SStacey Son 31000a5db46SStacey Son /* Third section header - strtab */ 31100a5db46SStacey Son hdr->kh_shdr[SHDR_STRTAB].sh_name = 1 + sizeof(STR_SYMTAB); 31200a5db46SStacey Son hdr->kh_shdr[SHDR_STRTAB].sh_type = SHT_STRTAB; 31300a5db46SStacey Son hdr->kh_shdr[SHDR_STRTAB].sh_flags = 0; 31400a5db46SStacey Son hdr->kh_shdr[SHDR_STRTAB].sh_addr = 0; 31500a5db46SStacey Son hdr->kh_shdr[SHDR_STRTAB].sh_offset = 31600a5db46SStacey Son hdr->kh_shdr[SHDR_SYMTAB].sh_offset + ts->ts_symsz; 31700a5db46SStacey Son hdr->kh_shdr[SHDR_STRTAB].sh_size = ts->ts_strsz; 31800a5db46SStacey Son hdr->kh_shdr[SHDR_STRTAB].sh_link = 0; 31900a5db46SStacey Son hdr->kh_shdr[SHDR_STRTAB].sh_info = 0; 32000a5db46SStacey Son hdr->kh_shdr[SHDR_STRTAB].sh_addralign = sizeof(char); 32100a5db46SStacey Son hdr->kh_shdr[SHDR_STRTAB].sh_entsize = 0; 32200a5db46SStacey Son 32300a5db46SStacey Son /* Fourth section - shstrtab */ 32400a5db46SStacey Son hdr->kh_shdr[SHDR_SHSTRTAB].sh_name = 1 + sizeof(STR_SYMTAB) + 32500a5db46SStacey Son sizeof(STR_STRTAB); 32600a5db46SStacey Son hdr->kh_shdr[SHDR_SHSTRTAB].sh_type = SHT_STRTAB; 32700a5db46SStacey Son hdr->kh_shdr[SHDR_SHSTRTAB].sh_flags = 0; 32800a5db46SStacey Son hdr->kh_shdr[SHDR_SHSTRTAB].sh_addr = 0; 32900a5db46SStacey Son hdr->kh_shdr[SHDR_SHSTRTAB].sh_offset = 33000a5db46SStacey Son offsetof(struct ksyms_hdr, kh_shstrtab); 33100a5db46SStacey Son hdr->kh_shdr[SHDR_SHSTRTAB].sh_size = sizeof(ksyms_shstrtab); 33200a5db46SStacey Son hdr->kh_shdr[SHDR_SHSTRTAB].sh_link = 0; 33300a5db46SStacey Son hdr->kh_shdr[SHDR_SHSTRTAB].sh_info = 0; 33400a5db46SStacey Son hdr->kh_shdr[SHDR_SHSTRTAB].sh_addralign = 0 /* sizeof(char) */; 33500a5db46SStacey Son hdr->kh_shdr[SHDR_SHSTRTAB].sh_entsize = 0; 33600a5db46SStacey Son 33700a5db46SStacey Son /* Copy shstrtab into the header */ 33800a5db46SStacey Son bcopy(ksyms_shstrtab, hdr->kh_shstrtab, sizeof(ksyms_shstrtab)); 33900a5db46SStacey Son 34000a5db46SStacey Son to.to_symoff = uaddr + hdr->kh_shdr[SHDR_SYMTAB].sh_offset; 34100a5db46SStacey Son to.to_stroff = uaddr + hdr->kh_shdr[SHDR_STRTAB].sh_offset; 34200a5db46SStacey Son to.to_stridx = 0; 34300a5db46SStacey Son if (sizeof(struct ksyms_hdr) > resid) { 34400a5db46SStacey Son free(hdr, M_KSYMS); 34500a5db46SStacey Son return (ENXIO); 34600a5db46SStacey Son } 34700a5db46SStacey Son to.to_resid = resid - sizeof(struct ksyms_hdr); 34800a5db46SStacey Son 34900a5db46SStacey Son /* Emit Header */ 35000a5db46SStacey Son copyout(hdr, (void *)uaddr, sizeof(struct ksyms_hdr)); 35100a5db46SStacey Son 35200a5db46SStacey Son free(hdr, M_KSYMS); 35300a5db46SStacey Son 35400a5db46SStacey Son /* Add symbol and string tables for each kernelmodule */ 35500a5db46SStacey Son error = linker_file_foreach(ksyms_add, &to); 35600a5db46SStacey Son 35700a5db46SStacey Son if (to.to_resid != 0) 35800a5db46SStacey Son return (ENXIO); 35900a5db46SStacey Son 36000a5db46SStacey Son return (error); 36100a5db46SStacey Son } 36200a5db46SStacey Son 36300a5db46SStacey Son /* 36400a5db46SStacey Son * Map some anonymous memory in user space of size sz, rounded up to the page 36500a5db46SStacey Son * boundary. 36600a5db46SStacey Son */ 36700a5db46SStacey Son static int 36800a5db46SStacey Son ksyms_map(struct thread *td, vm_offset_t *addr, size_t sz) 36900a5db46SStacey Son { 37000a5db46SStacey Son struct vmspace *vms = td->td_proc->p_vmspace; 37100a5db46SStacey Son int error; 37200a5db46SStacey Son vm_size_t size; 37300a5db46SStacey Son 37400a5db46SStacey Son 37500a5db46SStacey Son /* 37600a5db46SStacey Son * Map somewhere after heap in process memory. 37700a5db46SStacey Son */ 37800a5db46SStacey Son PROC_LOCK(td->td_proc); 37900a5db46SStacey Son *addr = round_page((vm_offset_t)vms->vm_daddr + 38000a5db46SStacey Son lim_max(td->td_proc, RLIMIT_DATA)); 38100a5db46SStacey Son PROC_UNLOCK(td->td_proc); 38200a5db46SStacey Son 38300a5db46SStacey Son /* round size up to page boundry */ 38400a5db46SStacey Son size = (vm_size_t) round_page(sz); 38500a5db46SStacey Son 38600a5db46SStacey Son error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE, 38700a5db46SStacey Son VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0); 38800a5db46SStacey Son 38900a5db46SStacey Son return (error); 39000a5db46SStacey Son } 39100a5db46SStacey Son 39200a5db46SStacey Son /* 39300a5db46SStacey Son * Unmap memory in user space. 39400a5db46SStacey Son */ 39500a5db46SStacey Son static int 39600a5db46SStacey Son ksyms_unmap(struct thread *td, vm_offset_t addr, size_t sz) 39700a5db46SStacey Son { 39800a5db46SStacey Son vm_map_t map; 39900a5db46SStacey Son vm_size_t size; 40000a5db46SStacey Son 40100a5db46SStacey Son map = &td->td_proc->p_vmspace->vm_map; 40200a5db46SStacey Son size = (vm_size_t) round_page(sz); 40300a5db46SStacey Son 4042f98b86bSStacey Son if (!vm_map_remove(map, addr, addr + size)) 40500a5db46SStacey Son return (EINVAL); 40600a5db46SStacey Son 4072f98b86bSStacey Son return (0); 40800a5db46SStacey Son } 40900a5db46SStacey Son 41000a5db46SStacey Son static void 41100a5db46SStacey Son ksyms_cdevpriv_dtr(void *data) 41200a5db46SStacey Son { 41300a5db46SStacey Son struct ksyms_softc *sc; 41400a5db46SStacey Son 41500a5db46SStacey Son sc = (struct ksyms_softc *)data; 41600a5db46SStacey Son 41700a5db46SStacey Son mtx_lock(&ksyms_mtx); 41800a5db46SStacey Son LIST_REMOVE(sc, sc_list); 41900a5db46SStacey Son mtx_unlock(&ksyms_mtx); 42000a5db46SStacey Son free(sc, M_KSYMS); 42100a5db46SStacey Son } 42200a5db46SStacey Son 42300a5db46SStacey Son /* ARGSUSED */ 42400a5db46SStacey Son static int 42500a5db46SStacey Son ksyms_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td) 42600a5db46SStacey Son { 42700a5db46SStacey Son struct tsizes ts; 42800a5db46SStacey Son size_t total_elf_sz; 42900a5db46SStacey Son int error, try; 43000a5db46SStacey Son struct ksyms_softc *sc; 43100a5db46SStacey Son 43200a5db46SStacey Son /* 43300a5db46SStacey Son * Limit one open() per process. The process must close() 43400a5db46SStacey Son * before open()'ing again. 43500a5db46SStacey Son */ 43600a5db46SStacey Son mtx_lock(&ksyms_mtx); 43700a5db46SStacey Son LIST_FOREACH(sc, &ksyms_list, sc_list) { 43800a5db46SStacey Son if (sc->sc_proc == td->td_proc) { 43900a5db46SStacey Son mtx_unlock(&ksyms_mtx); 44000a5db46SStacey Son return (EBUSY); 44100a5db46SStacey Son } 44200a5db46SStacey Son } 44300a5db46SStacey Son 44400a5db46SStacey Son sc = (struct ksyms_softc *) malloc(sizeof (*sc), M_KSYMS, 44500a5db46SStacey Son M_NOWAIT|M_ZERO); 44600a5db46SStacey Son 44700a5db46SStacey Son if (sc == NULL) { 44800a5db46SStacey Son mtx_unlock(&ksyms_mtx); 44900a5db46SStacey Son return (ENOMEM); 45000a5db46SStacey Son } 45100a5db46SStacey Son sc->sc_proc = td->td_proc; 45200a5db46SStacey Son sc->sc_pmap = &td->td_proc->p_vmspace->vm_pmap; 45300a5db46SStacey Son LIST_INSERT_HEAD(&ksyms_list, sc, sc_list); 45400a5db46SStacey Son mtx_unlock(&ksyms_mtx); 45500a5db46SStacey Son 45600a5db46SStacey Son error = devfs_set_cdevpriv(sc, ksyms_cdevpriv_dtr); 45700a5db46SStacey Son if (error) 45800a5db46SStacey Son goto failed; 45900a5db46SStacey Son 46000a5db46SStacey Son /* 46100a5db46SStacey Son * MOD_SLOCK doesn't work here (because of a lock reversal with 46200a5db46SStacey Son * KLD_SLOCK). Therefore, simply try upto 3 times to get a "clean" 46300a5db46SStacey Son * snapshot of the kernel symbol table. This should work fine in the 46400a5db46SStacey Son * rare case of a kernel module being loaded/unloaded at the same 46500a5db46SStacey Son * time. 46600a5db46SStacey Son */ 46700a5db46SStacey Son for(try = 0; try < 3; try++) { 46800a5db46SStacey Son /* 46900a5db46SStacey Son * Map a buffer in the calling process memory space and 47000a5db46SStacey Son * create a snapshot of the kernel symbol table in it. 47100a5db46SStacey Son */ 47200a5db46SStacey Son 47300a5db46SStacey Son /* Compute the size of buffer needed. */ 47400a5db46SStacey Son ksyms_size_calc(&ts); 47500a5db46SStacey Son total_elf_sz = sizeof(struct ksyms_hdr) + ts.ts_symsz + 47600a5db46SStacey Son ts.ts_strsz; 47700a5db46SStacey Son 47800a5db46SStacey Son error = ksyms_map(td, &(sc->sc_uaddr), 47900a5db46SStacey Son (vm_size_t) total_elf_sz); 48000a5db46SStacey Son if (error) 48100a5db46SStacey Son break; 48200a5db46SStacey Son sc->sc_usize = total_elf_sz; 48300a5db46SStacey Son 48400a5db46SStacey Son error = ksyms_snapshot(&ts, sc->sc_uaddr, total_elf_sz); 48500a5db46SStacey Son if (!error) { 48600a5db46SStacey Son /* Successful Snapshot */ 48700a5db46SStacey Son return (0); 48800a5db46SStacey Son } 48900a5db46SStacey Son 49000a5db46SStacey Son /* Snapshot failed, unmap the memory and try again */ 49100a5db46SStacey Son (void) ksyms_unmap(td, sc->sc_uaddr, sc->sc_usize); 49200a5db46SStacey Son } 49300a5db46SStacey Son 49400a5db46SStacey Son failed: 49500a5db46SStacey Son ksyms_cdevpriv_dtr(sc); 49600a5db46SStacey Son return (error); 49700a5db46SStacey Son } 49800a5db46SStacey Son 49900a5db46SStacey Son /* ARGSUSED */ 50000a5db46SStacey Son static int 50100a5db46SStacey Son ksyms_read(struct cdev *dev, struct uio *uio, int flags __unused) 50200a5db46SStacey Son { 50300a5db46SStacey Son int error; 50400a5db46SStacey Son size_t len, sz; 50500a5db46SStacey Son struct ksyms_softc *sc; 50600a5db46SStacey Son off_t off; 50700a5db46SStacey Son char *buf; 50800a5db46SStacey Son vm_size_t ubase; 50900a5db46SStacey Son 51000a5db46SStacey Son error = devfs_get_cdevpriv((void **)&sc); 51100a5db46SStacey Son if (error) 51200a5db46SStacey Son return (error); 51300a5db46SStacey Son 51400a5db46SStacey Son off = uio->uio_offset; 51500a5db46SStacey Son len = uio->uio_resid; 51600a5db46SStacey Son 51700a5db46SStacey Son if (off < 0 || off > sc->sc_usize) 51800a5db46SStacey Son return (EFAULT); 51900a5db46SStacey Son 52000a5db46SStacey Son if (len > (sc->sc_usize - off)) 52100a5db46SStacey Son len = sc->sc_usize - off; 52200a5db46SStacey Son 52300a5db46SStacey Son if (len == 0) 52400a5db46SStacey Son return (0); 52500a5db46SStacey Son 52600a5db46SStacey Son /* 52700a5db46SStacey Son * Since the snapshot buffer is in the user space we have to copy it 52800a5db46SStacey Son * in to the kernel and then back out. The extra copy saves valuable 52900a5db46SStacey Son * kernel memory. 53000a5db46SStacey Son */ 53100a5db46SStacey Son buf = malloc(PAGE_SIZE, M_KSYMS, M_WAITOK); 53200a5db46SStacey Son ubase = sc->sc_uaddr + off; 53300a5db46SStacey Son 53400a5db46SStacey Son while (len) { 53500a5db46SStacey Son 53600a5db46SStacey Son sz = min(PAGE_SIZE, len); 53700a5db46SStacey Son if (copyin((void *)ubase, buf, sz)) 53800a5db46SStacey Son error = EFAULT; 53900a5db46SStacey Son else 54000a5db46SStacey Son error = uiomove(buf, sz, uio); 54100a5db46SStacey Son 54200a5db46SStacey Son if (error) 54300a5db46SStacey Son break; 54400a5db46SStacey Son 54500a5db46SStacey Son len -= sz; 54600a5db46SStacey Son ubase += sz; 54700a5db46SStacey Son } 54800a5db46SStacey Son free(buf, M_KSYMS); 54900a5db46SStacey Son 55000a5db46SStacey Son return (error); 55100a5db46SStacey Son } 55200a5db46SStacey Son 55300a5db46SStacey Son /* ARGSUSED */ 55400a5db46SStacey Son static int 55500a5db46SStacey Son ksyms_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int32_t flag __unused, 556e090d0b6SJohn Baldwin struct thread *td __unused) 55700a5db46SStacey Son { 55800a5db46SStacey Son int error = 0; 55900a5db46SStacey Son struct ksyms_softc *sc; 56000a5db46SStacey Son 56100a5db46SStacey Son error = devfs_get_cdevpriv((void **)&sc); 56200a5db46SStacey Son if (error) 56300a5db46SStacey Son return (error); 56400a5db46SStacey Son 56500a5db46SStacey Son switch (cmd) { 56600a5db46SStacey Son case KIOCGSIZE: 56700a5db46SStacey Son /* 56800a5db46SStacey Son * Return the size (in bytes) of the symbol table 56900a5db46SStacey Son * snapshot. 57000a5db46SStacey Son */ 57100a5db46SStacey Son *(size_t *)data = sc->sc_usize; 57200a5db46SStacey Son break; 57300a5db46SStacey Son 57400a5db46SStacey Son case KIOCGADDR: 57500a5db46SStacey Son /* 57600a5db46SStacey Son * Return the address of the symbol table snapshot. 57700a5db46SStacey Son * XXX - compat32 version of this? 57800a5db46SStacey Son */ 57900a5db46SStacey Son *(void **)data = (void *)sc->sc_uaddr; 58000a5db46SStacey Son break; 58100a5db46SStacey Son 58200a5db46SStacey Son default: 58300a5db46SStacey Son error = ENOTTY; 58400a5db46SStacey Son break; 58500a5db46SStacey Son } 58600a5db46SStacey Son 58700a5db46SStacey Son return (error); 58800a5db46SStacey Son } 58900a5db46SStacey Son 59000a5db46SStacey Son /* ARGUSED */ 59100a5db46SStacey Son static int 59200a5db46SStacey Son ksyms_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, 59300a5db46SStacey Son int prot __unused) 59400a5db46SStacey Son { 59500a5db46SStacey Son struct ksyms_softc *sc; 59600a5db46SStacey Son int error; 59700a5db46SStacey Son 59800a5db46SStacey Son error = devfs_get_cdevpriv((void **)&sc); 59900a5db46SStacey Son if (error) 60000a5db46SStacey Son return (error); 60100a5db46SStacey Son 60200a5db46SStacey Son /* 60300a5db46SStacey Son * XXX mmap() will actually map the symbol table into the process 60400a5db46SStacey Son * address space again. 60500a5db46SStacey Son */ 60600a5db46SStacey Son if (offset > round_page(sc->sc_usize) || 60700a5db46SStacey Son (*paddr = pmap_extract(sc->sc_pmap, 60800a5db46SStacey Son (vm_offset_t)sc->sc_uaddr + offset)) == 0) 60900a5db46SStacey Son return (-1); 61000a5db46SStacey Son 61100a5db46SStacey Son return (0); 61200a5db46SStacey Son } 61300a5db46SStacey Son 61400a5db46SStacey Son /* ARGUSED */ 61500a5db46SStacey Son static int 61600a5db46SStacey Son ksyms_close(struct cdev *dev, int flags __unused, int fmt __unused, 61700a5db46SStacey Son struct thread *td) 61800a5db46SStacey Son { 61900a5db46SStacey Son int error = 0; 62000a5db46SStacey Son struct ksyms_softc *sc; 62100a5db46SStacey Son 62200a5db46SStacey Son error = devfs_get_cdevpriv((void **)&sc); 62300a5db46SStacey Son if (error) 62400a5db46SStacey Son return (error); 62500a5db46SStacey Son 62600a5db46SStacey Son /* Unmap the buffer from the process address space. */ 62700a5db46SStacey Son error = ksyms_unmap(td, sc->sc_uaddr, sc->sc_usize); 62800a5db46SStacey Son 62900a5db46SStacey Son devfs_clear_cdevpriv(); 63000a5db46SStacey Son 63100a5db46SStacey Son return (error); 63200a5db46SStacey Son } 63300a5db46SStacey Son 63400a5db46SStacey Son /* ARGSUSED */ 63500a5db46SStacey Son static int 63600a5db46SStacey Son ksyms_modevent(module_t mod __unused, int type, void *data __unused) 63700a5db46SStacey Son { 63800a5db46SStacey Son int error = 0; 63900a5db46SStacey Son 64000a5db46SStacey Son switch (type) { 64100a5db46SStacey Son case MOD_LOAD: 64200a5db46SStacey Son mtx_init(&ksyms_mtx, "KSyms mtx", NULL, MTX_DEF); 64300a5db46SStacey Son ksyms_dev = make_dev(&ksyms_cdevsw, 0, UID_ROOT, GID_WHEEL, 64400a5db46SStacey Son 0444, KSYMS_DNAME); 64500a5db46SStacey Son break; 64600a5db46SStacey Son 64700a5db46SStacey Son case MOD_UNLOAD: 64800a5db46SStacey Son if (!LIST_EMPTY(&ksyms_list)) 64900a5db46SStacey Son return (EBUSY); 65000a5db46SStacey Son destroy_dev(ksyms_dev); 65100a5db46SStacey Son mtx_destroy(&ksyms_mtx); 65200a5db46SStacey Son break; 65300a5db46SStacey Son 65400a5db46SStacey Son case MOD_SHUTDOWN: 65500a5db46SStacey Son break; 65600a5db46SStacey Son 65700a5db46SStacey Son default: 65800a5db46SStacey Son error = EOPNOTSUPP; 65900a5db46SStacey Son break; 66000a5db46SStacey Son } 66100a5db46SStacey Son return (error); 66200a5db46SStacey Son } 66300a5db46SStacey Son 66400a5db46SStacey Son DEV_MODULE(ksyms, ksyms_modevent, NULL); 66500a5db46SStacey Son MODULE_VERSION(ksyms, 1); 666