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