Home | History | Annotate | Line # | Download | only in kern
core_elf32.c revision 1.38
      1  1.38       dsl /*	$NetBSD: core_elf32.c,v 1.38 2014/01/03 15:15:02 dsl 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.38       dsl __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.38 2014/01/03 15:15:02 dsl Exp $");
     44  1.33        ad 
     45  1.33        ad #ifdef _KERNEL_OPT
     46  1.33        ad #include "opt_coredump.h"
     47  1.33        ad #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.36      para #include <sys/kmem.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.33        ad #ifdef COREDUMP
     68  1.33        ad 
     69   1.2   thorpej struct writesegs_state {
     70  1.16      matt 	Elf_Phdr *psections;
     71   1.2   thorpej 	off_t	secoff;
     72   1.2   thorpej };
     73   1.2   thorpej 
     74  1.38       dsl static int	ELFNAMEEND(coredump_getseghdrs)(struct proc *,
     75  1.38       dsl 		    struct uvm_coredump_state *);
     76  1.22   thorpej 
     77  1.37       dsl static int	ELFNAMEEND(coredump_notes)(struct proc *, struct lwp *,
     78  1.37       dsl 		    struct coredump_iostate *, size_t *);
     79  1.37       dsl static int	ELFNAMEEND(coredump_note)(struct proc *, struct lwp *,
     80  1.37       dsl 		    struct coredump_iostate *, size_t *);
     81   1.1   thorpej 
     82   1.1   thorpej #define	ELFROUNDSIZE	4	/* XXX Should it be sizeof(Elf_Word)? */
     83   1.1   thorpej #define	elfround(x)	roundup((x), ELFROUNDSIZE)
     84   1.1   thorpej 
     85  1.23      cube #define elf_process_read_regs	CONCAT(process_read_regs, ELFSIZE)
     86  1.35      matt #ifdef __HAVE_PROCESS_XFPREGS
     87  1.35      matt #define elf_process_read_xfpregs CONCAT(process_read_xfpregs, ELFSIZE)
     88  1.35      matt #else
     89  1.23      cube #define elf_process_read_fpregs	CONCAT(process_read_fpregs, ELFSIZE)
     90  1.35      matt #endif
     91  1.23      cube #define elf_reg			CONCAT(process_reg, ELFSIZE)
     92  1.23      cube #define elf_fpreg		CONCAT(process_fpreg, ELFSIZE)
     93  1.23      cube 
     94   1.1   thorpej int
     95  1.37       dsl ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
     96   1.1   thorpej {
     97   1.4   thorpej 	struct proc *p;
     98   1.1   thorpej 	Elf_Ehdr ehdr;
     99  1.16      matt 	Elf_Phdr phdr, *psections;
    100  1.36      para 	size_t psectionssize;
    101  1.38       dsl 	int npsections;
    102   1.2   thorpej 	struct writesegs_state ws;
    103  1.16      matt 	off_t notestart, secstart, offset;
    104   1.6      matt 	size_t notesize;
    105  1.16      matt 	int error, i;
    106   1.1   thorpej 
    107  1.16      matt 	psections = NULL;
    108   1.4   thorpej 	p = l->l_proc;
    109   1.1   thorpej 	/*
    110   1.1   thorpej 	 * We have to make a total of 3 passes across the map:
    111   1.1   thorpej 	 *
    112   1.1   thorpej 	 *	1. Count the number of map entries (the number of
    113  1.38       dsl 	 *	   PT_LOAD sections in the dump).
    114   1.1   thorpej 	 *
    115   1.1   thorpej 	 *	2. Write the P-section headers.
    116   1.1   thorpej 	 *
    117   1.1   thorpej 	 *	3. Write the P-sections.
    118   1.1   thorpej 	 */
    119   1.1   thorpej 
    120   1.1   thorpej 	/* Pass 1: count the entries. */
    121  1.38       dsl 	npsections = uvm_coredump_count_segs(p);
    122  1.16      matt 	/* Count the PT_NOTE section. */
    123  1.38       dsl 	npsections++;
    124   1.1   thorpej 
    125  1.38       dsl 	/* Get the size of the notes (mostly all the registers). */
    126  1.18      matt 	error = ELFNAMEEND(coredump_notes)(p, l, NULL, &notesize);
    127   1.1   thorpej 	if (error)
    128  1.16      matt 		goto out;
    129   1.1   thorpej 
    130  1.18      matt 	memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
    131   1.1   thorpej 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
    132   1.1   thorpej #if ELFSIZE == 32
    133   1.1   thorpej 	ehdr.e_ident[EI_CLASS] = ELFCLASS32;
    134   1.1   thorpej #elif ELFSIZE == 64
    135   1.1   thorpej 	ehdr.e_ident[EI_CLASS] = ELFCLASS64;
    136   1.1   thorpej #endif
    137   1.1   thorpej 	ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
    138   1.1   thorpej 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
    139   1.1   thorpej 	/* XXX Should be the OSABI/ABI version of the executable. */
    140   1.1   thorpej 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
    141   1.2   thorpej 	ehdr.e_ident[EI_ABIVERSION] = 0;
    142   1.1   thorpej 
    143   1.1   thorpej 	ehdr.e_type = ET_CORE;
    144   1.1   thorpej 	/* XXX This should be the e_machine of the executable. */
    145   1.1   thorpej 	ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
    146   1.1   thorpej 	ehdr.e_version = EV_CURRENT;
    147   1.1   thorpej 	ehdr.e_entry = 0;
    148   1.1   thorpej 	ehdr.e_phoff = sizeof(ehdr);
    149   1.1   thorpej 	ehdr.e_shoff = 0;
    150   1.1   thorpej 	ehdr.e_flags = 0;
    151   1.1   thorpej 	ehdr.e_ehsize = sizeof(ehdr);
    152   1.1   thorpej 	ehdr.e_phentsize = sizeof(Elf_Phdr);
    153  1.38       dsl 	ehdr.e_phnum = npsections;
    154   1.1   thorpej 	ehdr.e_shentsize = 0;
    155   1.1   thorpej 	ehdr.e_shnum = 0;
    156   1.1   thorpej 	ehdr.e_shstrndx = 0;
    157   1.1   thorpej 
    158  1.35      matt #ifdef ELF_MD_COREDUMP_SETUP
    159  1.35      matt 	ELF_MD_COREDUMP_SETUP(l, &ehdr);
    160  1.35      matt #endif
    161  1.35      matt 
    162   1.1   thorpej 	/* Write out the ELF header. */
    163  1.18      matt 	error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
    164  1.16      matt 	if (error)
    165  1.16      matt 		goto out;
    166  1.16      matt 
    167  1.18      matt 	offset = sizeof(ehdr);
    168   1.1   thorpej 
    169  1.38       dsl 	notestart = offset + sizeof(phdr) * npsections;
    170  1.16      matt 	secstart = notestart + notesize;
    171  1.16      matt 
    172  1.38       dsl 	psectionssize = npsections * sizeof(Elf_Phdr);
    173  1.36      para 	psections = kmem_zalloc(psectionssize, KM_SLEEP);
    174   1.1   thorpej 
    175  1.38       dsl 	/* Pass 2: now find the P-section headers. */
    176   1.2   thorpej 	ws.secoff = secstart;
    177  1.16      matt 	ws.psections = psections;
    178  1.38       dsl 	error = uvm_coredump_walkmap(p, ELFNAMEEND(coredump_getseghdrs), &ws);
    179   1.2   thorpej 	if (error)
    180  1.16      matt 		goto out;
    181   1.1   thorpej 
    182  1.38       dsl 	/* Add the PT_NOTE header after the P-section headers. */
    183  1.16      matt 	ws.psections->p_type = PT_NOTE;
    184  1.16      matt 	ws.psections->p_offset = notestart;
    185  1.16      matt 	ws.psections->p_vaddr = 0;
    186  1.16      matt 	ws.psections->p_paddr = 0;
    187  1.16      matt 	ws.psections->p_filesz = notesize;
    188  1.16      matt 	ws.psections->p_memsz = 0;
    189  1.16      matt 	ws.psections->p_flags = PF_R;
    190  1.16      matt 	ws.psections->p_align = ELFROUNDSIZE;
    191   1.1   thorpej 
    192  1.38       dsl 	/* Write the P-section headers followed by the PT_NOTR header */
    193  1.18      matt 	error = coredump_write(cookie, UIO_SYSSPACE, psections,
    194  1.38       dsl 	    npsections * sizeof(Elf_Phdr));
    195   1.1   thorpej 	if (error)
    196  1.16      matt 		goto out;
    197   1.1   thorpej 
    198  1.18      matt #ifdef DIAGNOSTIC
    199  1.38       dsl 	offset += npsections * sizeof(Elf_Phdr);
    200  1.16      matt 	if (offset != notestart)
    201   1.2   thorpej 		panic("coredump: offset %lld != notestart %lld",
    202  1.16      matt 		    (long long) offset, (long long) notestart);
    203   1.2   thorpej #endif
    204   1.1   thorpej 
    205   1.1   thorpej 	/* Write out the notes. */
    206  1.18      matt 	error = ELFNAMEEND(coredump_notes)(p, l, cookie, &notesize);
    207  1.16      matt 	if (error)
    208  1.16      matt 		goto out;
    209   1.2   thorpej 
    210   1.2   thorpej #ifdef DIAGNOSTIC
    211  1.16      matt 	offset += notesize;
    212  1.16      matt 	if (offset != secstart)
    213   1.2   thorpej 		panic("coredump: offset %lld != secstart %lld",
    214  1.16      matt 		    (long long) offset, (long long) secstart);
    215   1.2   thorpej #endif
    216   1.2   thorpej 
    217   1.5    atatat 	/* Pass 3: finally, write the sections themselves. */
    218  1.38       dsl 	for (i = 0; i < npsections - 1; i++) {
    219  1.16      matt 		if (psections[i].p_filesz == 0)
    220  1.16      matt 			continue;
    221  1.16      matt 
    222  1.16      matt #ifdef DIAGNOSTIC
    223  1.16      matt 		if (offset != psections[i].p_offset)
    224  1.16      matt 			panic("coredump: offset %lld != p_offset[%d] %lld",
    225  1.16      matt 			    (long long) offset, i,
    226  1.16      matt 			    (long long) psections[i].p_filesz);
    227  1.16      matt #endif
    228  1.16      matt 
    229  1.18      matt 		error = coredump_write(cookie, UIO_USERSPACE,
    230  1.18      matt 		    (void *)(vaddr_t)psections[i].p_vaddr,
    231  1.18      matt 		    psections[i].p_filesz);
    232  1.16      matt 		if (error)
    233  1.16      matt 			goto out;
    234  1.16      matt 
    235  1.16      matt #ifdef DIAGNOSTIC
    236  1.16      matt 		offset += psections[i].p_filesz;
    237  1.16      matt #endif
    238  1.16      matt 	}
    239   1.1   thorpej 
    240  1.16      matt   out:
    241  1.16      matt 	if (psections)
    242  1.36      para 		kmem_free(psections, psectionssize);
    243   1.2   thorpej 	return (error);
    244   1.2   thorpej }
    245   1.2   thorpej 
    246  1.22   thorpej static int
    247  1.38       dsl ELFNAMEEND(coredump_getseghdrs)(struct proc *p, struct uvm_coredump_state *us)
    248   1.2   thorpej {
    249   1.2   thorpej 	struct writesegs_state *ws = us->cookie;
    250   1.2   thorpej 	Elf_Phdr phdr;
    251  1.16      matt 	vsize_t size, realsize;
    252  1.16      matt 	vaddr_t end;
    253   1.2   thorpej 	int error;
    254   1.2   thorpej 
    255   1.2   thorpej 	size = us->end - us->start;
    256  1.16      matt 	realsize = us->realend - us->start;
    257  1.16      matt 	end = us->realend;
    258  1.16      matt 
    259  1.38       dsl 	/* Don't bother writing out trailing zeros */
    260  1.16      matt 	while (realsize > 0) {
    261  1.16      matt 		long buf[1024 / sizeof(long)];
    262  1.16      matt 		size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
    263  1.16      matt 		const long *ep;
    264  1.16      matt 		int i;
    265  1.16      matt 
    266  1.16      matt 		end -= slen;
    267  1.21  christos 		if ((error = copyin_proc(p, (void *)end, buf, slen)) != 0)
    268  1.21  christos 			return error;
    269  1.16      matt 
    270  1.16      matt 		ep = (const long *) &buf[slen / sizeof(buf[0])];
    271  1.16      matt 		for (i = 0, ep--; buf <= ep; ep--, i++) {
    272  1.16      matt 			if (*ep)
    273  1.16      matt 				break;
    274  1.16      matt 		}
    275  1.16      matt 		realsize -= i * sizeof(buf[0]);
    276  1.16      matt 		if (i * sizeof(buf[0]) < slen)
    277  1.16      matt 			break;
    278  1.16      matt 	}
    279   1.2   thorpej 
    280   1.2   thorpej 	phdr.p_type = PT_LOAD;
    281   1.2   thorpej 	phdr.p_offset = ws->secoff;
    282   1.2   thorpej 	phdr.p_vaddr = us->start;
    283   1.2   thorpej 	phdr.p_paddr = 0;
    284  1.16      matt 	phdr.p_filesz = realsize;
    285   1.2   thorpej 	phdr.p_memsz = size;
    286   1.2   thorpej 	phdr.p_flags = 0;
    287   1.2   thorpej 	if (us->prot & VM_PROT_READ)
    288   1.2   thorpej 		phdr.p_flags |= PF_R;
    289   1.2   thorpej 	if (us->prot & VM_PROT_WRITE)
    290   1.2   thorpej 		phdr.p_flags |= PF_W;
    291   1.2   thorpej 	if (us->prot & VM_PROT_EXECUTE)
    292   1.2   thorpej 		phdr.p_flags |= PF_X;
    293   1.2   thorpej 	phdr.p_align = PAGE_SIZE;
    294   1.2   thorpej 
    295   1.2   thorpej 	ws->secoff += phdr.p_filesz;
    296  1.16      matt 	*ws->psections++ = phdr;
    297   1.1   thorpej 
    298   1.1   thorpej 	return (0);
    299   1.1   thorpej }
    300   1.1   thorpej 
    301  1.22   thorpej static int
    302  1.18      matt ELFNAMEEND(coredump_notes)(struct proc *p, struct lwp *l,
    303  1.37       dsl     struct coredump_iostate *iocookie, size_t *sizep)
    304   1.1   thorpej {
    305   1.1   thorpej 	struct netbsd_elfcore_procinfo cpi;
    306   1.1   thorpej 	Elf_Nhdr nhdr;
    307   1.6      matt 	size_t size, notesize;
    308   1.6      matt 	int error;
    309   1.6      matt 	struct lwp *l0;
    310  1.29        ad 	sigset_t ss1, ss2;
    311   1.1   thorpej 
    312   1.1   thorpej 	size = 0;
    313   1.1   thorpej 
    314   1.1   thorpej 	/* First, write an elfcore_procinfo. */
    315   1.1   thorpej 	notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
    316   1.1   thorpej 	    elfround(sizeof(cpi));
    317  1.18      matt 	if (iocookie) {
    318   1.1   thorpej 		cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
    319   1.1   thorpej 		cpi.cpi_cpisize = sizeof(cpi);
    320  1.12  christos 		cpi.cpi_signo = p->p_sigctx.ps_signo;
    321  1.12  christos 		cpi.cpi_sigcode = p->p_sigctx.ps_code;
    322   1.8   nathanw 		cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
    323   1.1   thorpej 
    324  1.29        ad 		/*
    325  1.29        ad 		 * XXX This should be per-LWP.
    326  1.29        ad 		 */
    327  1.29        ad 		ss1 = p->p_sigpend.sp_set;
    328  1.29        ad 		sigemptyset(&ss2);
    329  1.29        ad 		LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    330  1.29        ad 			sigplusset(&l0->l_sigpend.sp_set, &ss1);
    331  1.29        ad 			sigplusset(&l0->l_sigmask, &ss2);
    332  1.29        ad 		}
    333  1.29        ad 		memcpy(&cpi.cpi_sigpend, &ss1, sizeof(cpi.cpi_sigpend));
    334  1.29        ad 		memcpy(&cpi.cpi_sigmask, &ss2, sizeof(cpi.cpi_sigmask));
    335   1.1   thorpej 		memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
    336   1.1   thorpej 		    sizeof(cpi.cpi_sigignore));
    337   1.1   thorpej 		memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
    338   1.1   thorpej 		    sizeof(cpi.cpi_sigcatch));
    339   1.1   thorpej 
    340   1.1   thorpej 		cpi.cpi_pid = p->p_pid;
    341  1.32        ad 		mutex_enter(proc_lock);
    342   1.1   thorpej 		cpi.cpi_ppid = p->p_pptr->p_pid;
    343   1.1   thorpej 		cpi.cpi_pgrp = p->p_pgid;
    344   1.1   thorpej 		cpi.cpi_sid = p->p_session->s_sid;
    345  1.32        ad 		mutex_exit(proc_lock);
    346   1.1   thorpej 
    347  1.26        ad 		cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
    348  1.26        ad 		cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
    349  1.26        ad 		cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
    350  1.26        ad 
    351  1.26        ad 		cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
    352  1.26        ad 		cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
    353  1.26        ad 		cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
    354   1.1   thorpej 
    355   1.4   thorpej 		cpi.cpi_nlwps = p->p_nlwps;
    356  1.24  christos 		(void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
    357  1.24  christos 		cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
    358   1.1   thorpej 
    359   1.1   thorpej 		nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
    360   1.1   thorpej 		nhdr.n_descsz = sizeof(cpi);
    361   1.1   thorpej 		nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
    362   1.1   thorpej 
    363  1.18      matt 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    364  1.18      matt 		    ELF_NOTE_NETBSD_CORE_NAME "\0\0\0", &cpi);
    365   1.1   thorpej 		if (error)
    366   1.1   thorpej 			return (error);
    367   1.1   thorpej 	}
    368   1.1   thorpej 
    369   1.1   thorpej 	size += notesize;
    370   1.1   thorpej 
    371   1.1   thorpej 	/* XXX Add hook for machdep per-proc notes. */
    372   1.1   thorpej 
    373   1.1   thorpej 	/*
    374   1.6      matt 	 * Now write the register info for the thread that caused the
    375   1.6      matt 	 * coredump.
    376   1.6      matt 	 */
    377  1.18      matt 	error = ELFNAMEEND(coredump_note)(p, l, iocookie, &notesize);
    378   1.6      matt 	if (error)
    379   1.6      matt 		return (error);
    380   1.6      matt 	size += notesize;
    381   1.6      matt 
    382   1.6      matt 	/*
    383   1.1   thorpej 	 * Now, for each LWP, write the register info and any other
    384  1.29        ad 	 * per-LWP notes.  Since we're dumping core, we don't bother
    385  1.29        ad 	 * locking.
    386   1.1   thorpej 	 */
    387   1.6      matt 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    388   1.6      matt 		if (l0 == l)		/* we've taken care of this thread */
    389   1.6      matt 			continue;
    390  1.18      matt 		error = ELFNAMEEND(coredump_note)(p, l0, iocookie, &notesize);
    391   1.6      matt 		if (error)
    392   1.6      matt 			return (error);
    393   1.1   thorpej 		size += notesize;
    394   1.6      matt 	}
    395   1.6      matt 
    396   1.6      matt 	*sizep = size;
    397   1.6      matt 	return (0);
    398   1.6      matt }
    399   1.1   thorpej 
    400  1.22   thorpej static int
    401  1.37       dsl ELFNAMEEND(coredump_note)(struct proc *p, struct lwp *l,
    402  1.37       dsl     struct coredump_iostate *iocookie, size_t *sizep)
    403   1.6      matt {
    404   1.6      matt 	Elf_Nhdr nhdr;
    405   1.6      matt 	int size, notesize, error;
    406   1.6      matt 	int namesize;
    407  1.18      matt 	char name[64+ELFROUNDSIZE];
    408  1.23      cube 	elf_reg intreg;
    409   1.1   thorpej #ifdef PT_GETFPREGS
    410  1.23      cube 	elf_fpreg freg;
    411   1.6      matt #endif
    412   1.6      matt 
    413   1.6      matt 	size = 0;
    414   1.6      matt 
    415  1.18      matt 	snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d",
    416  1.18      matt 	    ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
    417   1.6      matt 	namesize = strlen(name) + 1;
    418  1.18      matt 	memset(name + namesize, 0, elfround(namesize) - namesize);
    419   1.6      matt 
    420   1.6      matt 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
    421  1.18      matt 	if (iocookie) {
    422  1.23      cube 		error = elf_process_read_regs(l, &intreg);
    423   1.6      matt 		if (error)
    424   1.6      matt 			return (error);
    425   1.6      matt 
    426   1.6      matt 		nhdr.n_namesz = namesize;
    427   1.6      matt 		nhdr.n_descsz = sizeof(intreg);
    428   1.6      matt 		nhdr.n_type = PT_GETREGS;
    429   1.6      matt 
    430  1.18      matt 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    431  1.18      matt 		    name, &intreg);
    432   1.6      matt 		if (error)
    433   1.6      matt 			return (error);
    434   1.1   thorpej 
    435   1.4   thorpej 	}
    436   1.6      matt 	size += notesize;
    437   1.6      matt 
    438   1.6      matt #ifdef PT_GETFPREGS
    439   1.6      matt 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
    440  1.18      matt 	if (iocookie) {
    441  1.35      matt 		size_t freglen = sizeof(freg);
    442  1.35      matt #ifdef __HAVE_PROCESS_XFPREGS
    443  1.35      matt 		error = elf_process_read_xfpregs(l, &freg, &freglen);
    444  1.35      matt #else
    445  1.23      cube 		error = elf_process_read_fpregs(l, &freg);
    446  1.35      matt #endif
    447   1.6      matt 		if (error)
    448   1.6      matt 			return (error);
    449   1.6      matt 
    450   1.6      matt 		nhdr.n_namesz = namesize;
    451  1.35      matt 		nhdr.n_descsz = freglen;
    452   1.6      matt 		nhdr.n_type = PT_GETFPREGS;
    453   1.1   thorpej 
    454  1.18      matt 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    455  1.18      matt 		    name, &freg);
    456   1.6      matt 		if (error)
    457   1.6      matt 			return (error);
    458   1.6      matt 	}
    459   1.6      matt 	size += notesize;
    460   1.6      matt #endif
    461   1.1   thorpej 	*sizep = size;
    462   1.6      matt 	/* XXX Add hook for machdep per-LWP notes. */
    463   1.1   thorpej 	return (0);
    464   1.1   thorpej }
    465   1.1   thorpej 
    466   1.1   thorpej int
    467  1.37       dsl ELFNAMEEND(coredump_writenote)(struct proc *p, struct coredump_iostate *cookie,
    468  1.37       dsl     Elf_Nhdr *nhdr, const char *name, void *data)
    469   1.1   thorpej {
    470   1.1   thorpej 	int error;
    471   1.1   thorpej 
    472  1.18      matt 	error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr));
    473   1.1   thorpej 	if (error)
    474  1.18      matt 		return error;
    475   1.1   thorpej 
    476  1.18      matt 	error = coredump_write(cookie, UIO_SYSSPACE, name,
    477  1.18      matt 	    elfround(nhdr->n_namesz));
    478   1.1   thorpej 	if (error)
    479  1.18      matt 		return error;
    480   1.1   thorpej 
    481  1.18      matt 	return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->n_descsz);
    482   1.1   thorpej }
    483  1.33        ad 
    484  1.33        ad #else	/* COREDUMP */
    485  1.33        ad 
    486  1.33        ad int
    487  1.33        ad ELFNAMEEND(coredump)(struct lwp *l, void *cookie)
    488  1.33        ad {
    489  1.33        ad 
    490  1.33        ad 	return ENOSYS;
    491  1.33        ad }
    492  1.33        ad 
    493  1.33        ad #endif	/* COREDUMP */
    494