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