Home | History | Annotate | Line # | Download | only in kern
core_elf32.c revision 1.17
      1  1.17        he /*	$NetBSD: core_elf32.c,v 1.17 2005/06/03 13:30:10 he 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.17        he __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.17 2005/06/03 13:30:10 he Exp $");
     44   1.1   thorpej 
     45   1.1   thorpej /* If not included by core_elf64.c, ELFSIZE won't be defined. */
     46   1.1   thorpej #ifndef ELFSIZE
     47   1.1   thorpej #define	ELFSIZE		32
     48   1.1   thorpej #endif
     49   1.1   thorpej 
     50   1.1   thorpej #include <sys/param.h>
     51   1.1   thorpej #include <sys/systm.h>
     52   1.1   thorpej #include <sys/proc.h>
     53   1.1   thorpej #include <sys/vnode.h>
     54   1.1   thorpej #include <sys/exec_elf.h>
     55   1.1   thorpej #include <sys/ptrace.h>
     56  1.16      matt #include <sys/malloc.h>
     57   1.1   thorpej 
     58   1.1   thorpej #include <machine/reg.h>
     59   1.1   thorpej 
     60   1.2   thorpej #include <uvm/uvm_extern.h>
     61   1.2   thorpej 
     62   1.2   thorpej struct countsegs_state {
     63   1.2   thorpej 	int	npsections;
     64   1.2   thorpej };
     65   1.2   thorpej 
     66  1.10      fvdl int	ELFNAMEEND(coredump_countsegs)(struct proc *, struct vnode *,
     67   1.2   thorpej 	    struct ucred *, struct uvm_coredump_state *);
     68   1.2   thorpej 
     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.10      fvdl int	ELFNAMEEND(coredump_writeseghdrs)(struct proc *, struct vnode *,
     75   1.2   thorpej 	    struct ucred *, struct uvm_coredump_state *);
     76  1.10      fvdl int	ELFNAMEEND(coredump_writesegs)(struct proc *, struct vnode *,
     77   1.2   thorpej 	    struct ucred *, struct uvm_coredump_state *);
     78   1.1   thorpej 
     79   1.6      matt int	ELFNAMEEND(coredump_notes)(struct proc *, struct lwp *, struct vnode *,
     80   1.6      matt 	    struct ucred *, size_t *, off_t);
     81   1.6      matt int	ELFNAMEEND(coredump_note)(struct proc *, struct lwp *, struct vnode *,
     82   1.6      matt 	    struct ucred *, size_t *, off_t);
     83   1.1   thorpej 
     84   1.1   thorpej #define	ELFROUNDSIZE	4	/* XXX Should it be sizeof(Elf_Word)? */
     85   1.1   thorpej #define	elfround(x)	roundup((x), ELFROUNDSIZE)
     86   1.1   thorpej 
     87   1.1   thorpej int
     88   1.4   thorpej ELFNAMEEND(coredump)(struct lwp *l, struct vnode *vp, struct ucred *cred)
     89   1.1   thorpej {
     90   1.4   thorpej 	struct proc *p;
     91   1.1   thorpej 	Elf_Ehdr ehdr;
     92  1.16      matt 	Elf_Phdr phdr, *psections;
     93   1.2   thorpej 	struct countsegs_state cs;
     94   1.2   thorpej 	struct writesegs_state ws;
     95  1.16      matt 	off_t notestart, secstart, offset;
     96   1.6      matt 	size_t notesize;
     97  1.16      matt 	int error, i;
     98   1.1   thorpej 
     99  1.16      matt 	psections = NULL;
    100   1.4   thorpej 	p = l->l_proc;
    101   1.1   thorpej 	/*
    102   1.1   thorpej 	 * We have to make a total of 3 passes across the map:
    103   1.1   thorpej 	 *
    104   1.1   thorpej 	 *	1. Count the number of map entries (the number of
    105   1.1   thorpej 	 *	   PT_LOAD sections).
    106   1.1   thorpej 	 *
    107   1.1   thorpej 	 *	2. Write the P-section headers.
    108   1.1   thorpej 	 *
    109   1.1   thorpej 	 *	3. Write the P-sections.
    110   1.1   thorpej 	 */
    111   1.1   thorpej 
    112   1.1   thorpej 	/* Pass 1: count the entries. */
    113   1.2   thorpej 	cs.npsections = 0;
    114  1.10      fvdl 	error = uvm_coredump_walkmap(p, vp, cred,
    115   1.2   thorpej 	    ELFNAMEEND(coredump_countsegs), &cs);
    116   1.2   thorpej 	if (error)
    117  1.16      matt 		goto out;
    118  1.16      matt 
    119  1.16      matt 	/* Count the PT_NOTE section. */
    120  1.16      matt 	cs.npsections++;
    121   1.1   thorpej 
    122   1.1   thorpej 	/* Get the size of the notes. */
    123   1.6      matt 	error = ELFNAMEEND(coredump_notes)(p, l, vp, cred, &notesize, 0);
    124   1.1   thorpej 	if (error)
    125  1.16      matt 		goto out;
    126   1.1   thorpej 
    127   1.1   thorpej 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
    128   1.1   thorpej #if ELFSIZE == 32
    129   1.1   thorpej 	ehdr.e_ident[EI_CLASS] = ELFCLASS32;
    130   1.1   thorpej #elif ELFSIZE == 64
    131   1.1   thorpej 	ehdr.e_ident[EI_CLASS] = ELFCLASS64;
    132   1.1   thorpej #endif
    133   1.1   thorpej 	ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
    134   1.1   thorpej 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
    135   1.1   thorpej 	/* XXX Should be the OSABI/ABI version of the executable. */
    136   1.1   thorpej 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
    137   1.2   thorpej 	ehdr.e_ident[EI_ABIVERSION] = 0;
    138   1.1   thorpej 
    139   1.1   thorpej 	ehdr.e_type = ET_CORE;
    140   1.1   thorpej 	/* XXX This should be the e_machine of the executable. */
    141   1.1   thorpej 	ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
    142   1.1   thorpej 	ehdr.e_version = EV_CURRENT;
    143   1.1   thorpej 	ehdr.e_entry = 0;
    144   1.1   thorpej 	ehdr.e_phoff = sizeof(ehdr);
    145   1.1   thorpej 	ehdr.e_shoff = 0;
    146   1.1   thorpej 	ehdr.e_flags = 0;
    147   1.1   thorpej 	ehdr.e_ehsize = sizeof(ehdr);
    148   1.1   thorpej 	ehdr.e_phentsize = sizeof(Elf_Phdr);
    149   1.2   thorpej 	ehdr.e_phnum = cs.npsections;
    150   1.1   thorpej 	ehdr.e_shentsize = 0;
    151   1.1   thorpej 	ehdr.e_shnum = 0;
    152   1.1   thorpej 	ehdr.e_shstrndx = 0;
    153   1.1   thorpej 
    154  1.16      matt 	offset = 0;
    155  1.16      matt 
    156   1.1   thorpej 	/* Write out the ELF header. */
    157  1.16      matt 	error = vn_rdwr(UIO_WRITE, vp,
    158  1.16      matt 	    (caddr_t)&ehdr, (int)sizeof(ehdr), (off_t)offset,
    159  1.14     skrll 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, NULL);
    160  1.16      matt 	if (error)
    161  1.16      matt 		goto out;
    162  1.16      matt 
    163  1.16      matt 	offset += sizeof(ehdr);
    164   1.1   thorpej 
    165  1.16      matt 	notestart = offset + sizeof(phdr) * cs.npsections;
    166  1.16      matt 	secstart = notestart + notesize;
    167  1.16      matt 
    168  1.16      matt 	psections = malloc(cs.npsections * sizeof(Elf_Phdr),
    169  1.16      matt 	    M_TEMP, M_WAITOK|M_ZERO);
    170   1.1   thorpej 
    171   1.5    atatat 	/* Pass 2: now write the P-section headers. */
    172   1.2   thorpej 	ws.secoff = secstart;
    173  1.16      matt 	ws.psections = psections;
    174  1.10      fvdl 	error = uvm_coredump_walkmap(p, vp, cred,
    175   1.2   thorpej 	    ELFNAMEEND(coredump_writeseghdrs), &ws);
    176   1.2   thorpej 	if (error)
    177  1.16      matt 		goto out;
    178   1.1   thorpej 
    179   1.1   thorpej 	/* Write out the PT_NOTE header. */
    180  1.16      matt 	ws.psections->p_type = PT_NOTE;
    181  1.16      matt 	ws.psections->p_offset = notestart;
    182  1.16      matt 	ws.psections->p_vaddr = 0;
    183  1.16      matt 	ws.psections->p_paddr = 0;
    184  1.16      matt 	ws.psections->p_filesz = notesize;
    185  1.16      matt 	ws.psections->p_memsz = 0;
    186  1.16      matt 	ws.psections->p_flags = PF_R;
    187  1.16      matt 	ws.psections->p_align = ELFROUNDSIZE;
    188   1.1   thorpej 
    189   1.1   thorpej 	error = vn_rdwr(UIO_WRITE, vp,
    190  1.16      matt 	    (caddr_t)psections, cs.npsections * sizeof(Elf_Phdr),
    191  1.16      matt 	    offset, UIO_SYSSPACE,
    192  1.14     skrll 	    IO_NODELOCKED|IO_UNIT, cred, NULL, NULL);
    193   1.1   thorpej 	if (error)
    194  1.16      matt 		goto out;
    195   1.1   thorpej 
    196  1.16      matt 	offset += cs.npsections * sizeof(Elf_Phdr);
    197   1.1   thorpej 
    198   1.2   thorpej #ifdef DIAGNOSTIC
    199  1.16      matt 	if (offset != notestart)
    200   1.2   thorpej 		panic("coredump: offset %lld != notestart %lld",
    201  1.16      matt 		    (long long) offset, (long long) notestart);
    202   1.2   thorpej #endif
    203   1.1   thorpej 
    204   1.1   thorpej 	/* Write out the notes. */
    205  1.16      matt 	error = ELFNAMEEND(coredump_notes)(p, l, vp, cred, &notesize, offset);
    206  1.16      matt 	if (error)
    207  1.16      matt 		goto out;
    208   1.2   thorpej 
    209   1.2   thorpej #ifdef DIAGNOSTIC
    210  1.16      matt 	offset += notesize;
    211  1.16      matt 	if (offset != secstart)
    212   1.2   thorpej 		panic("coredump: offset %lld != secstart %lld",
    213  1.16      matt 		    (long long) offset, (long long) secstart);
    214   1.2   thorpej #endif
    215   1.2   thorpej 
    216   1.5    atatat 	/* Pass 3: finally, write the sections themselves. */
    217  1.16      matt 	for (i = 0; i < cs.npsections - 1; i++) {
    218  1.16      matt 		if (psections[i].p_filesz == 0)
    219  1.16      matt 			continue;
    220  1.16      matt 
    221  1.16      matt #ifdef DIAGNOSTIC
    222  1.16      matt 		if (offset != psections[i].p_offset)
    223  1.16      matt 			panic("coredump: offset %lld != p_offset[%d] %lld",
    224  1.16      matt 			    (long long) offset, i,
    225  1.16      matt 			    (long long) psections[i].p_filesz);
    226  1.16      matt #endif
    227  1.16      matt 
    228  1.16      matt 		error = vn_rdwr(UIO_WRITE, vp,
    229  1.17        he 		    (caddr_t)((intptr_t) psections[i].p_vaddr),
    230  1.17        he 		    psections[i].p_filesz,
    231  1.16      matt 		    psections[i].p_offset, UIO_USERSPACE,
    232  1.16      matt 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    233  1.16      matt 		if (error)
    234  1.16      matt 			goto out;
    235  1.16      matt 
    236  1.16      matt #ifdef DIAGNOSTIC
    237  1.16      matt 		offset += psections[i].p_filesz;
    238  1.16      matt #endif
    239  1.16      matt 	}
    240   1.1   thorpej 
    241  1.16      matt   out:
    242  1.16      matt 	if (psections)
    243  1.16      matt 		free(psections, M_TEMP);
    244   1.2   thorpej 	return (error);
    245   1.2   thorpej }
    246   1.2   thorpej 
    247   1.2   thorpej int
    248  1.10      fvdl ELFNAMEEND(coredump_countsegs)(struct proc *p, struct vnode *vp,
    249   1.2   thorpej     struct ucred *cred, struct uvm_coredump_state *us)
    250   1.2   thorpej {
    251   1.2   thorpej 	struct countsegs_state *cs = us->cookie;
    252   1.1   thorpej 
    253   1.2   thorpej 	cs->npsections++;
    254   1.2   thorpej 	return (0);
    255   1.2   thorpej }
    256   1.2   thorpej 
    257   1.2   thorpej int
    258  1.10      fvdl ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, struct vnode *vp,
    259   1.2   thorpej     struct ucred *cred, struct uvm_coredump_state *us)
    260   1.2   thorpej {
    261   1.2   thorpej 	struct writesegs_state *ws = us->cookie;
    262   1.2   thorpej 	Elf_Phdr phdr;
    263  1.16      matt 	vsize_t size, realsize;
    264  1.16      matt 	vaddr_t end;
    265   1.2   thorpej 	int error;
    266   1.2   thorpej 
    267   1.2   thorpej 	size = us->end - us->start;
    268  1.16      matt 	realsize = us->realend - us->start;
    269  1.16      matt 	end = us->realend;
    270  1.16      matt 
    271  1.16      matt 	while (realsize > 0) {
    272  1.16      matt 		long buf[1024 / sizeof(long)];
    273  1.16      matt 		size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
    274  1.16      matt 		const long *ep;
    275  1.16      matt 		int i;
    276  1.16      matt 
    277  1.16      matt 		end -= slen;
    278  1.16      matt 		error = copyin((void *) end, buf, slen);
    279  1.16      matt 		if (error)
    280  1.16      matt 			return (error);
    281  1.16      matt 
    282  1.16      matt 		ep = (const long *) &buf[slen / sizeof(buf[0])];
    283  1.16      matt 		for (i = 0, ep--; buf <= ep; ep--, i++) {
    284  1.16      matt 			if (*ep)
    285  1.16      matt 				break;
    286  1.16      matt 		}
    287  1.16      matt 		realsize -= i * sizeof(buf[0]);
    288  1.16      matt 		if (i * sizeof(buf[0]) < slen)
    289  1.16      matt 			break;
    290  1.16      matt 	}
    291   1.2   thorpej 
    292   1.2   thorpej 	phdr.p_type = PT_LOAD;
    293   1.2   thorpej 	phdr.p_offset = ws->secoff;
    294   1.2   thorpej 	phdr.p_vaddr = us->start;
    295   1.2   thorpej 	phdr.p_paddr = 0;
    296  1.16      matt 	phdr.p_filesz = realsize;
    297   1.2   thorpej 	phdr.p_memsz = size;
    298   1.2   thorpej 	phdr.p_flags = 0;
    299   1.2   thorpej 	if (us->prot & VM_PROT_READ)
    300   1.2   thorpej 		phdr.p_flags |= PF_R;
    301   1.2   thorpej 	if (us->prot & VM_PROT_WRITE)
    302   1.2   thorpej 		phdr.p_flags |= PF_W;
    303   1.2   thorpej 	if (us->prot & VM_PROT_EXECUTE)
    304   1.2   thorpej 		phdr.p_flags |= PF_X;
    305   1.2   thorpej 	phdr.p_align = PAGE_SIZE;
    306   1.2   thorpej 
    307   1.2   thorpej 	ws->secoff += phdr.p_filesz;
    308  1.16      matt 	*ws->psections++ = phdr;
    309   1.1   thorpej 
    310   1.1   thorpej 	return (0);
    311   1.1   thorpej }
    312   1.1   thorpej 
    313   1.1   thorpej int
    314   1.6      matt ELFNAMEEND(coredump_notes)(struct proc *p, struct lwp *l, struct vnode *vp,
    315   1.6      matt     struct ucred *cred, size_t *sizep, off_t offset)
    316   1.1   thorpej {
    317   1.1   thorpej 	struct netbsd_elfcore_procinfo cpi;
    318   1.1   thorpej 	Elf_Nhdr nhdr;
    319   1.6      matt 	size_t size, notesize;
    320   1.6      matt 	int error;
    321   1.6      matt 	struct lwp *l0;
    322   1.1   thorpej 
    323   1.1   thorpej 	size = 0;
    324   1.1   thorpej 
    325   1.1   thorpej 	/* First, write an elfcore_procinfo. */
    326   1.1   thorpej 	notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
    327   1.1   thorpej 	    elfround(sizeof(cpi));
    328   1.1   thorpej 	if (offset) {
    329   1.1   thorpej 		cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
    330   1.1   thorpej 		cpi.cpi_cpisize = sizeof(cpi);
    331  1.12  christos 		cpi.cpi_signo = p->p_sigctx.ps_signo;
    332  1.12  christos 		cpi.cpi_sigcode = p->p_sigctx.ps_code;
    333   1.8   nathanw 		cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
    334   1.1   thorpej 
    335   1.1   thorpej 		memcpy(&cpi.cpi_sigpend, &p->p_sigctx.ps_siglist,
    336   1.1   thorpej 		    sizeof(cpi.cpi_sigpend));
    337   1.1   thorpej 		memcpy(&cpi.cpi_sigmask, &p->p_sigctx.ps_sigmask,
    338   1.1   thorpej 		    sizeof(cpi.cpi_sigmask));
    339   1.1   thorpej 		memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
    340   1.1   thorpej 		    sizeof(cpi.cpi_sigignore));
    341   1.1   thorpej 		memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
    342   1.1   thorpej 		    sizeof(cpi.cpi_sigcatch));
    343   1.1   thorpej 
    344   1.1   thorpej 		cpi.cpi_pid = p->p_pid;
    345   1.1   thorpej 		cpi.cpi_ppid = p->p_pptr->p_pid;
    346   1.1   thorpej 		cpi.cpi_pgrp = p->p_pgid;
    347   1.1   thorpej 		cpi.cpi_sid = p->p_session->s_sid;
    348   1.1   thorpej 
    349   1.1   thorpej 		cpi.cpi_ruid = p->p_cred->p_ruid;
    350   1.1   thorpej 		cpi.cpi_euid = p->p_ucred->cr_uid;
    351   1.1   thorpej 		cpi.cpi_svuid = p->p_cred->p_svuid;
    352   1.1   thorpej 
    353   1.1   thorpej 		cpi.cpi_rgid = p->p_cred->p_rgid;
    354   1.1   thorpej 		cpi.cpi_egid = p->p_ucred->cr_gid;
    355   1.1   thorpej 		cpi.cpi_svgid = p->p_cred->p_svgid;
    356   1.1   thorpej 
    357   1.4   thorpej 		cpi.cpi_nlwps = p->p_nlwps;
    358   1.7    itojun 		strlcpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
    359   1.1   thorpej 
    360   1.1   thorpej 		nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
    361   1.1   thorpej 		nhdr.n_descsz = sizeof(cpi);
    362   1.1   thorpej 		nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
    363   1.1   thorpej 
    364  1.10      fvdl 		error = ELFNAMEEND(coredump_writenote)(p, vp, cred, offset,
    365   1.1   thorpej 		    &nhdr, ELF_NOTE_NETBSD_CORE_NAME, &cpi);
    366   1.1   thorpej 		if (error)
    367   1.1   thorpej 			return (error);
    368   1.1   thorpej 
    369   1.1   thorpej 		offset += notesize;
    370   1.1   thorpej 	}
    371   1.1   thorpej 
    372   1.1   thorpej 	size += notesize;
    373   1.1   thorpej 
    374   1.1   thorpej 	/* XXX Add hook for machdep per-proc notes. */
    375   1.1   thorpej 
    376   1.1   thorpej 	/*
    377   1.6      matt 	 * Now write the register info for the thread that caused the
    378   1.6      matt 	 * coredump.
    379   1.6      matt 	 */
    380   1.6      matt 	error = ELFNAMEEND(coredump_note)(p, l, vp, cred, &notesize, offset);
    381   1.6      matt 	if (error)
    382   1.6      matt 		return (error);
    383   1.6      matt 	if (offset)
    384   1.6      matt 		offset += notesize;
    385   1.6      matt 	size += notesize;
    386   1.6      matt 
    387   1.6      matt 	/*
    388   1.1   thorpej 	 * Now, for each LWP, write the register info and any other
    389   1.1   thorpej 	 * per-LWP notes.
    390   1.1   thorpej 	 */
    391   1.6      matt 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    392   1.6      matt 		if (l0 == l)		/* we've taken care of this thread */
    393   1.6      matt 			continue;
    394   1.6      matt 		error = ELFNAMEEND(coredump_note)(p, l0, vp, cred,
    395   1.6      matt 		    &notesize, offset);
    396   1.6      matt 		if (error)
    397   1.6      matt 			return (error);
    398   1.6      matt 		if (offset)
    399   1.1   thorpej 			offset += notesize;
    400   1.1   thorpej 		size += notesize;
    401   1.6      matt 	}
    402   1.6      matt 
    403   1.6      matt 	*sizep = size;
    404   1.6      matt 	return (0);
    405   1.6      matt }
    406   1.1   thorpej 
    407   1.6      matt int
    408   1.6      matt ELFNAMEEND(coredump_note)(struct proc *p, struct lwp *l, struct vnode *vp,
    409   1.6      matt     struct ucred *cred, size_t *sizep, off_t offset)
    410   1.6      matt {
    411   1.6      matt 	Elf_Nhdr nhdr;
    412   1.6      matt 	int size, notesize, error;
    413   1.6      matt 	int namesize;
    414   1.6      matt 	char name[64];
    415   1.6      matt 	struct reg intreg;
    416   1.1   thorpej #ifdef PT_GETFPREGS
    417   1.6      matt 	struct fpreg freg;
    418   1.6      matt #endif
    419   1.6      matt 
    420   1.6      matt 	size = 0;
    421   1.6      matt 
    422  1.13    itojun 	snprintf(name, sizeof(name), "%s@%d", ELF_NOTE_NETBSD_CORE_NAME,
    423  1.13    itojun 	    l->l_lid);
    424   1.6      matt 	namesize = strlen(name) + 1;
    425   1.6      matt 
    426   1.6      matt 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
    427   1.6      matt 	if (offset) {
    428   1.6      matt 		PHOLD(l);
    429   1.6      matt 		error = process_read_regs(l, &intreg);
    430   1.6      matt 		PRELE(l);
    431   1.6      matt 		if (error)
    432   1.6      matt 			return (error);
    433   1.6      matt 
    434   1.6      matt 		nhdr.n_namesz = namesize;
    435   1.6      matt 		nhdr.n_descsz = sizeof(intreg);
    436   1.6      matt 		nhdr.n_type = PT_GETREGS;
    437   1.6      matt 
    438  1.10      fvdl 		error = ELFNAMEEND(coredump_writenote)(p, vp, cred,
    439   1.6      matt 		    offset, &nhdr, name, &intreg);
    440   1.6      matt 		if (error)
    441   1.6      matt 			return (error);
    442   1.1   thorpej 
    443   1.6      matt 		offset += notesize;
    444   1.4   thorpej 	}
    445   1.6      matt 	size += notesize;
    446   1.6      matt 
    447   1.6      matt #ifdef PT_GETFPREGS
    448   1.6      matt 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
    449   1.6      matt 	if (offset) {
    450   1.6      matt 		PHOLD(l);
    451   1.6      matt 		error = process_read_fpregs(l, &freg);
    452   1.6      matt 		PRELE(l);
    453   1.6      matt 		if (error)
    454   1.6      matt 			return (error);
    455   1.6      matt 
    456   1.6      matt 		nhdr.n_namesz = namesize;
    457   1.6      matt 		nhdr.n_descsz = sizeof(freg);
    458   1.6      matt 		nhdr.n_type = PT_GETFPREGS;
    459   1.1   thorpej 
    460  1.10      fvdl 		error = ELFNAMEEND(coredump_writenote)(p, vp, cred,
    461   1.6      matt 		    offset, &nhdr, name, &freg);
    462   1.6      matt 		if (error)
    463   1.6      matt 			return (error);
    464   1.6      matt 
    465   1.6      matt 		offset += notesize;
    466   1.6      matt 	}
    467   1.6      matt 	size += notesize;
    468   1.6      matt #endif
    469   1.1   thorpej 	*sizep = size;
    470   1.6      matt 	/* XXX Add hook for machdep per-LWP notes. */
    471   1.1   thorpej 	return (0);
    472   1.1   thorpej }
    473   1.1   thorpej 
    474   1.1   thorpej int
    475  1.10      fvdl ELFNAMEEND(coredump_writenote)(struct proc *p, struct vnode *vp,
    476   1.1   thorpej     struct ucred *cred, off_t offset, Elf_Nhdr *nhdr, const char *name,
    477   1.1   thorpej     void *data)
    478   1.1   thorpej {
    479   1.1   thorpej 	int error;
    480   1.1   thorpej 
    481   1.1   thorpej 	error = vn_rdwr(UIO_WRITE, vp,
    482   1.1   thorpej 	    (caddr_t) nhdr, sizeof(*nhdr),
    483   1.1   thorpej 	    offset, UIO_SYSSPACE,
    484  1.14     skrll 	    IO_NODELOCKED|IO_UNIT, cred, NULL, NULL);
    485   1.1   thorpej 	if (error)
    486   1.1   thorpej 		return (error);
    487   1.1   thorpej 
    488   1.1   thorpej 	offset += sizeof(*nhdr);
    489   1.1   thorpej 
    490   1.1   thorpej 	error = vn_rdwr(UIO_WRITE, vp,
    491  1.15  christos 	    /*XXXUNCONST*/
    492  1.15  christos 	    __UNCONST(name), nhdr->n_namesz,
    493   1.1   thorpej 	    offset, UIO_SYSSPACE,
    494  1.14     skrll 	    IO_NODELOCKED|IO_UNIT, cred, NULL, NULL);
    495   1.1   thorpej 	if (error)
    496   1.1   thorpej 		return (error);
    497   1.1   thorpej 
    498   1.1   thorpej 	offset += elfround(nhdr->n_namesz);
    499   1.1   thorpej 
    500   1.1   thorpej 	error = vn_rdwr(UIO_WRITE, vp,
    501   1.1   thorpej 	    data, nhdr->n_descsz,
    502   1.1   thorpej 	    offset, UIO_SYSSPACE,
    503  1.14     skrll 	    IO_NODELOCKED|IO_UNIT, cred, NULL, NULL);
    504   1.1   thorpej 
    505   1.1   thorpej 	return (error);
    506   1.1   thorpej }
    507