Home | History | Annotate | Line # | Download | only in kern
core_elf32.c revision 1.41
      1 /*	$NetBSD: core_elf32.c,v 1.41 2014/01/03 21:34:40 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * core_elf32.c/core_elf64.c: Support for the Elf32/Elf64 core file format.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.41 2014/01/03 21:34:40 dsl Exp $");
     44 
     45 #ifdef _KERNEL_OPT
     46 #include "opt_coredump.h"
     47 #endif
     48 
     49 #ifndef ELFSIZE
     50 #define	ELFSIZE		32
     51 #endif
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/proc.h>
     56 #include <sys/vnode.h>
     57 #include <sys/exec.h>
     58 #include <sys/exec_elf.h>
     59 #include <sys/ptrace.h>
     60 #include <sys/kmem.h>
     61 #include <sys/kauth.h>
     62 
     63 #include <machine/reg.h>
     64 
     65 #include <uvm/uvm_extern.h>
     66 
     67 #ifdef COREDUMP
     68 
     69 struct writesegs_state {
     70 	Elf_Phdr *psections;
     71 	proc_t   *p;
     72 	off_t	 secoff;
     73 	size_t   npsections;
     74 };
     75 
     76 /*
     77  * We need to know how big the 'notes' are before we write the main header.
     78  * To avoid problems with double-processing we save the data.
     79  */
     80 struct note_buf {
     81 	struct note_buf  *nb_next;
     82 	unsigned char    nb_data[4096 - sizeof (void *)];
     83 };
     84 
     85 struct note_state {
     86 	struct note_buf  *ns_first;
     87 	struct note_buf  *ns_last;
     88 	unsigned int     ns_count;       /* Of full buffers */
     89 	unsigned int     ns_offset;      /* Write point in last buffer */
     90 };
     91 
     92 static int	ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *);
     93 
     94 static int	ELFNAMEEND(coredump_notes)(struct lwp *, struct note_state *);
     95 static int	ELFNAMEEND(coredump_note)(struct lwp *, struct note_state *);
     96 
     97 /* The 'note' section names and data are always 4-byte aligned. */
     98 #define	ELFROUNDSIZE	4	/* XXX Should it be sizeof(Elf_Word)? */
     99 
    100 #define elf_process_read_regs	CONCAT(process_read_regs, ELFSIZE)
    101 #ifdef __HAVE_PROCESS_XFPREGS
    102 #define elf_process_read_xfpregs CONCAT(process_read_xfpregs, ELFSIZE)
    103 #else
    104 #define elf_process_read_fpregs	CONCAT(process_read_fpregs, ELFSIZE)
    105 #endif
    106 #define elf_reg			CONCAT(process_reg, ELFSIZE)
    107 #define elf_fpreg		CONCAT(process_fpreg, ELFSIZE)
    108 
    109 int
    110 ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
    111 {
    112 	Elf_Ehdr ehdr;
    113 	Elf_Phdr *psections;
    114 	size_t psectionssize;
    115 	int npsections;
    116 	struct writesegs_state ws;
    117 	off_t notestart;
    118 	size_t notesize;
    119 	int error, i;
    120 
    121 	struct note_state ns;
    122 	struct note_buf *nb;
    123 
    124 	psections = NULL;
    125 
    126 	/* Get all of the notes (mostly all the registers). */
    127 	ns.ns_first = kmem_alloc(sizeof *ns.ns_first, KM_SLEEP);
    128 	ns.ns_last = ns.ns_first;
    129 	ns.ns_count = 0;
    130 	ns.ns_offset = 0;
    131 	error = ELFNAMEEND(coredump_notes)(l, &ns);
    132 	ns.ns_last->nb_next = NULL;
    133 	if (error)
    134 		goto out;
    135 	notesize = ns.ns_count * sizeof nb->nb_data + ns.ns_offset;
    136 
    137 	/*
    138 	 * We have to make a total of 3 passes across the map:
    139 	 *
    140 	 *	1. Count the number of map entries (the number of
    141 	 *	   PT_LOAD sections in the dump).
    142 	 *
    143 	 *	2. Write the P-section headers.
    144 	 *
    145 	 *	3. Write the P-sections.
    146 	 */
    147 
    148 	/* Pass 1: count the entries. */
    149 	npsections = uvm_coredump_count_segs(l->l_proc);
    150 	/* Allow for the PT_NOTE section. */
    151 	npsections++;
    152 
    153 	/* Build the main elf header */
    154 	memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
    155 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
    156 #if ELFSIZE == 32
    157 	ehdr.e_ident[EI_CLASS] = ELFCLASS32;
    158 #elif ELFSIZE == 64
    159 	ehdr.e_ident[EI_CLASS] = ELFCLASS64;
    160 #endif
    161 	ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
    162 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
    163 	/* XXX Should be the OSABI/ABI version of the executable. */
    164 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
    165 	ehdr.e_ident[EI_ABIVERSION] = 0;
    166 
    167 	ehdr.e_type = ET_CORE;
    168 	/* XXX This should be the e_machine of the executable. */
    169 	ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
    170 	ehdr.e_version = EV_CURRENT;
    171 	ehdr.e_entry = 0;
    172 	ehdr.e_phoff = sizeof(ehdr);
    173 	ehdr.e_shoff = 0;
    174 	ehdr.e_flags = 0;
    175 	ehdr.e_ehsize = sizeof(ehdr);
    176 	ehdr.e_phentsize = sizeof(Elf_Phdr);
    177 	ehdr.e_phnum = npsections;
    178 	ehdr.e_shentsize = 0;
    179 	ehdr.e_shnum = 0;
    180 	ehdr.e_shstrndx = 0;
    181 
    182 #ifdef ELF_MD_COREDUMP_SETUP
    183 	ELF_MD_COREDUMP_SETUP(l, &ehdr);
    184 #endif
    185 
    186 	/* Write out the ELF header. */
    187 	error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
    188 	if (error)
    189 		goto out;
    190 
    191 	psectionssize = npsections * sizeof(*psections);
    192 	notestart = sizeof(ehdr) + psectionssize;
    193 
    194 	psections = kmem_zalloc(psectionssize, KM_SLEEP);
    195 
    196 	/* Pass 2: now find the P-section headers. */
    197 	ws.secoff = notestart + notesize;
    198 	ws.psections = psections;
    199 	ws.npsections = npsections - 1;
    200 	ws.p = l->l_proc;
    201 	error = uvm_coredump_walkmap(l->l_proc, ELFNAMEEND(coredump_getseghdrs),
    202 	    &ws);
    203 	if (error)
    204 		goto out;
    205 	if (ws.npsections != 0) {
    206 		/* A section went away */
    207 		error = ENOMEM;
    208 		goto out;
    209 	}
    210 
    211 	/* Add the PT_NOTE header after the P-section headers. */
    212 	ws.psections->p_type = PT_NOTE;
    213 	ws.psections->p_offset = notestart;
    214 	ws.psections->p_vaddr = 0;
    215 	ws.psections->p_paddr = 0;
    216 	ws.psections->p_filesz = notesize;
    217 	ws.psections->p_memsz = 0;
    218 	ws.psections->p_flags = PF_R;
    219 	ws.psections->p_align = ELFROUNDSIZE;
    220 
    221 	/* Write the P-section headers followed by the PT_NOTE header */
    222 	error = coredump_write(cookie, UIO_SYSSPACE, psections, psectionssize);
    223 	if (error)
    224 		goto out;
    225 
    226 #ifdef DIAGNOSTIC
    227 	if (coredump_offset(cookie) != notestart)
    228 		panic("coredump: offset %lld != notestart %lld",
    229 		    (long long) coredump_offset(cookie),
    230 		    (long long) notestart);
    231 #endif
    232 
    233 	/* Write out the notes. */
    234 	for (nb = ns.ns_first; nb != NULL; nb = nb->nb_next) {
    235 		error = coredump_write(cookie, UIO_SYSSPACE, nb->nb_data,
    236 		    nb->nb_next == NULL ? ns.ns_offset : sizeof nb->nb_data);
    237 		if (error)
    238 			goto out;
    239 	}
    240 
    241 	/* Finally, write the sections themselves. */
    242 	for (i = 0; i < npsections - 1; i++) {
    243 		if (psections[i].p_filesz == 0)
    244 			continue;
    245 
    246 #ifdef DIAGNOSTIC
    247 		if (coredump_offset(cookie) != psections[i].p_offset)
    248 			panic("coredump: offset %lld != p_offset[%d] %lld",
    249 			    (long long) coredump_offset(cookie), i,
    250 			    (long long) psections[i].p_filesz);
    251 #endif
    252 
    253 		error = coredump_write(cookie, UIO_USERSPACE,
    254 		    (void *)(vaddr_t)psections[i].p_vaddr,
    255 		    psections[i].p_filesz);
    256 		if (error)
    257 			goto out;
    258 	}
    259 
    260   out:
    261 	if (psections)
    262 		kmem_free(psections, psectionssize);
    263 	for (; (nb = ns.ns_first) != NULL; ns.ns_first = nb->nb_next)
    264 		kmem_free(nb, sizeof *nb);
    265 	return (error);
    266 }
    267 
    268 static int
    269 ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *us)
    270 {
    271 	struct writesegs_state *ws = us->cookie;
    272 	Elf_Phdr phdr;
    273 	vsize_t size, realsize;
    274 	vaddr_t end;
    275 	int error;
    276 
    277 	/* Don't overrun if there are more sections */
    278 	if (ws->npsections == 0)
    279 		return ENOMEM;
    280 	ws->npsections--;
    281 
    282 	size = us->end - us->start;
    283 	realsize = us->realend - us->start;
    284 	end = us->realend;
    285 
    286 	/* Don't bother writing out trailing zeros */
    287 	while (realsize > 0) {
    288 		long buf[1024 / sizeof(long)];
    289 		size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
    290 		const long *ep;
    291 		int i;
    292 
    293 		end -= slen;
    294 		if ((error = copyin_proc(ws->p, (void *)end, buf, slen)) != 0)
    295 			return error;
    296 
    297 		ep = (const long *) &buf[slen / sizeof(buf[0])];
    298 		for (i = 0, ep--; buf <= ep; ep--, i++) {
    299 			if (*ep)
    300 				break;
    301 		}
    302 		realsize -= i * sizeof(buf[0]);
    303 		if (i * sizeof(buf[0]) < slen)
    304 			break;
    305 	}
    306 
    307 	phdr.p_type = PT_LOAD;
    308 	phdr.p_offset = ws->secoff;
    309 	phdr.p_vaddr = us->start;
    310 	phdr.p_paddr = 0;
    311 	phdr.p_filesz = realsize;
    312 	phdr.p_memsz = size;
    313 	phdr.p_flags = 0;
    314 	if (us->prot & VM_PROT_READ)
    315 		phdr.p_flags |= PF_R;
    316 	if (us->prot & VM_PROT_WRITE)
    317 		phdr.p_flags |= PF_W;
    318 	if (us->prot & VM_PROT_EXECUTE)
    319 		phdr.p_flags |= PF_X;
    320 	phdr.p_align = PAGE_SIZE;
    321 
    322 	ws->secoff += phdr.p_filesz;
    323 	*ws->psections++ = phdr;
    324 
    325 	return (0);
    326 }
    327 
    328 static int
    329 ELFNAMEEND(coredump_notes)(struct lwp *l, struct note_state *ns)
    330 {
    331 	struct proc *p;
    332 	struct netbsd_elfcore_procinfo cpi;
    333 	int error;
    334 	struct lwp *l0;
    335 	sigset_t ss1, ss2;
    336 
    337 	p = l->l_proc;
    338 
    339 	/* First, write an elfcore_procinfo. */
    340 	cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
    341 	cpi.cpi_cpisize = sizeof(cpi);
    342 	cpi.cpi_signo = p->p_sigctx.ps_signo;
    343 	cpi.cpi_sigcode = p->p_sigctx.ps_code;
    344 	cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
    345 
    346 	/*
    347 	 * XXX This should be per-LWP.
    348 	 */
    349 	ss1 = p->p_sigpend.sp_set;
    350 	sigemptyset(&ss2);
    351 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    352 		sigplusset(&l0->l_sigpend.sp_set, &ss1);
    353 		sigplusset(&l0->l_sigmask, &ss2);
    354 	}
    355 	memcpy(&cpi.cpi_sigpend, &ss1, sizeof(cpi.cpi_sigpend));
    356 	memcpy(&cpi.cpi_sigmask, &ss2, sizeof(cpi.cpi_sigmask));
    357 	memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
    358 	    sizeof(cpi.cpi_sigignore));
    359 	memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
    360 	    sizeof(cpi.cpi_sigcatch));
    361 
    362 	cpi.cpi_pid = p->p_pid;
    363 	mutex_enter(proc_lock);
    364 	cpi.cpi_ppid = p->p_pptr->p_pid;
    365 	cpi.cpi_pgrp = p->p_pgid;
    366 	cpi.cpi_sid = p->p_session->s_sid;
    367 	mutex_exit(proc_lock);
    368 
    369 	cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
    370 	cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
    371 	cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
    372 
    373 	cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
    374 	cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
    375 	cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
    376 
    377 	cpi.cpi_nlwps = p->p_nlwps;
    378 	(void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
    379 	cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
    380 
    381 	ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_PROCINFO,
    382 	    ELF_NOTE_NETBSD_CORE_NAME, &cpi, sizeof(cpi));
    383 
    384 	/* XXX Add hook for machdep per-proc notes. */
    385 
    386 	/*
    387 	 * Now write the register info for the thread that caused the
    388 	 * coredump.
    389 	 */
    390 	error = ELFNAMEEND(coredump_note)(l, ns);
    391 	if (error)
    392 		return (error);
    393 
    394 	/*
    395 	 * Now, for each LWP, write the register info and any other
    396 	 * per-LWP notes.
    397 	 * Lock in case this is a gcore requested dump.
    398 	 */
    399 	mutex_enter(p->p_lock);
    400 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    401 		if (l0 == l)		/* we've taken care of this thread */
    402 			continue;
    403 		error = ELFNAMEEND(coredump_note)(l0, ns);
    404 		if (error)
    405 			break;
    406 	}
    407 	mutex_exit(p->p_lock);
    408 
    409 	return error;
    410 }
    411 
    412 static int
    413 ELFNAMEEND(coredump_note)(struct lwp *l, struct note_state *ns)
    414 {
    415 	int error;
    416 	char name[64];
    417 	elf_reg intreg;
    418 #ifdef PT_GETFPREGS
    419 	elf_fpreg freg;
    420 	size_t freglen;
    421 #endif
    422 
    423 	snprintf(name, sizeof(name), "%s@%d",
    424 	    ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
    425 
    426 	error = elf_process_read_regs(l, &intreg);
    427 	if (error)
    428 		return (error);
    429 
    430 	ELFNAMEEND(coredump_savenote)(ns, PT_GETREGS, name, &intreg,
    431 	    sizeof(intreg));
    432 
    433 #ifdef PT_GETFPREGS
    434 	freglen = sizeof(freg);
    435 #ifdef __HAVE_PROCESS_XFPREGS
    436 	error = elf_process_read_xfpregs(l, &freg, &freglen);
    437 #else
    438 	error = elf_process_read_fpregs(l, &freg);
    439 #endif
    440 	if (error)
    441 		return (error);
    442 
    443 	ELFNAMEEND(coredump_savenote)(ns, PT_GETFPREGS, name, &freg, freglen);
    444 #endif
    445 	/* XXX Add hook for machdep per-LWP notes. */
    446 	return (0);
    447 }
    448 
    449 static void
    450 save_note_bytes(struct note_state *ns, const void *data, size_t len)
    451 {
    452 	struct note_buf *nb = ns->ns_last;
    453 	size_t copylen;
    454 	unsigned char *wp;
    455 
    456 	/*
    457 	 * Just copy the data into a buffer list.
    458 	 * All but the last buffer is full.
    459 	 */
    460 	for (;;) {
    461 		copylen = min(len, sizeof nb->nb_data - ns->ns_offset);
    462 		wp = nb->nb_data + ns->ns_offset;
    463 		memcpy(wp, data, copylen);
    464 		if (copylen == len)
    465 			break;
    466 		nb->nb_next = kmem_alloc(sizeof *nb->nb_next, KM_SLEEP);
    467 		nb = nb->nb_next;
    468 		ns->ns_last = nb;
    469 		ns->ns_count++;
    470 		ns->ns_offset = 0;
    471 		len -= copylen;
    472 		data = (const unsigned char *)data + copylen;
    473 	}
    474 
    475 	while (copylen & (ELFROUNDSIZE - 1))
    476 	    wp[copylen++] = 0;
    477 
    478 	ns->ns_offset += copylen;
    479 }
    480 
    481 void
    482 ELFNAMEEND(coredump_savenote)(struct note_state *ns, unsigned int type,
    483     const char *name, void *data, size_t data_len)
    484 {
    485 	Elf_Nhdr nhdr;
    486 
    487 	nhdr.n_namesz = strlen(name) + 1;
    488 	nhdr.n_descsz = data_len;
    489 	nhdr.n_type = type;
    490 
    491 	save_note_bytes(ns, &nhdr, sizeof (nhdr));
    492 	save_note_bytes(ns, name, nhdr.n_namesz);
    493 	save_note_bytes(ns, data, data_len);
    494 }
    495 
    496 #else	/* COREDUMP */
    497 
    498 int
    499 ELFNAMEEND(coredump)(struct lwp *l, void *cookie)
    500 {
    501 
    502 	return ENOSYS;
    503 }
    504 
    505 #endif	/* COREDUMP */
    506