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