xref: /netbsd/sys/kern/core_elf32.c (revision 5b880e99)
1*5b880e99Srin /*	$NetBSD: core_elf32.c,v 1.67 2021/01/02 02:13:42 rin Exp $	*/
251535d4bSthorpej 
351535d4bSthorpej /*
451535d4bSthorpej  * Copyright (c) 2001 Wasabi Systems, Inc.
551535d4bSthorpej  * All rights reserved.
651535d4bSthorpej  *
751535d4bSthorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
851535d4bSthorpej  *
951535d4bSthorpej  * Redistribution and use in source and binary forms, with or without
1051535d4bSthorpej  * modification, are permitted provided that the following conditions
1151535d4bSthorpej  * are met:
1251535d4bSthorpej  * 1. Redistributions of source code must retain the above copyright
1351535d4bSthorpej  *    notice, this list of conditions and the following disclaimer.
1451535d4bSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1551535d4bSthorpej  *    notice, this list of conditions and the following disclaimer in the
1651535d4bSthorpej  *    documentation and/or other materials provided with the distribution.
1751535d4bSthorpej  * 3. All advertising materials mentioning features or use of this software
1851535d4bSthorpej  *    must display the following acknowledgement:
1951535d4bSthorpej  *	This product includes software developed for the NetBSD Project by
2051535d4bSthorpej  *	Wasabi Systems, Inc.
2151535d4bSthorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2251535d4bSthorpej  *    or promote products derived from this software without specific prior
2351535d4bSthorpej  *    written permission.
2451535d4bSthorpej  *
2551535d4bSthorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2651535d4bSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2751535d4bSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2851535d4bSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
2951535d4bSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3051535d4bSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3151535d4bSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3251535d4bSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3351535d4bSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3451535d4bSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3551535d4bSthorpej  * POSSIBILITY OF SUCH DAMAGE.
3651535d4bSthorpej  */
3751535d4bSthorpej 
3851535d4bSthorpej /*
3951535d4bSthorpej  * core_elf32.c/core_elf64.c: Support for the Elf32/Elf64 core file format.
4051535d4bSthorpej  */
4151535d4bSthorpej 
4251535d4bSthorpej #include <sys/cdefs.h>
43*5b880e99Srin __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.67 2021/01/02 02:13:42 rin Exp $");
4451535d4bSthorpej 
4592ce8c6aSad #ifdef _KERNEL_OPT
4658045560Schristos #include "opt_compat_netbsd32.h"
4792ce8c6aSad #endif
4892ce8c6aSad 
4951535d4bSthorpej #ifndef ELFSIZE
5051535d4bSthorpej #define	ELFSIZE		32
5151535d4bSthorpej #endif
5251535d4bSthorpej 
5351535d4bSthorpej #include <sys/param.h>
5451535d4bSthorpej #include <sys/systm.h>
5551535d4bSthorpej #include <sys/proc.h>
5651535d4bSthorpej #include <sys/vnode.h>
57e1245a3cSmatt #include <sys/exec.h>
5851535d4bSthorpej #include <sys/exec_elf.h>
5951535d4bSthorpej #include <sys/ptrace.h>
6055b033b1Spara #include <sys/kmem.h>
61215bd95bSelad #include <sys/kauth.h>
6216e12c90Spgoyette #include <sys/compat_stub.h>
6351535d4bSthorpej 
6451535d4bSthorpej #include <machine/reg.h>
6551535d4bSthorpej 
6606920aefSthorpej #include <uvm/uvm_extern.h>
6706920aefSthorpej 
68ccbfb8c2Srin #ifdef COMPAT_NETBSD32
69*5b880e99Srin #include <compat/netbsd32/netbsd32.h>
70ccbfb8c2Srin #endif
71ccbfb8c2Srin 
7206920aefSthorpej struct writesegs_state {
7325a0e29aSmatt 	Elf_Phdr *psections;
74ce22e045Sdsl 	proc_t   *p;
7506920aefSthorpej 	off_t	 secoff;
76d40145bfSdsl 	size_t   npsections;
77d40145bfSdsl };
78d40145bfSdsl 
79d40145bfSdsl /*
80d40145bfSdsl  * We need to know how big the 'notes' are before we write the main header.
81d40145bfSdsl  * To avoid problems with double-processing we save the data.
82d40145bfSdsl  */
83d40145bfSdsl struct note_buf {
84d40145bfSdsl 	struct note_buf  *nb_next;
85d40145bfSdsl 	unsigned char    nb_data[4096 - sizeof (void *)];
86d40145bfSdsl };
87d40145bfSdsl 
88d40145bfSdsl struct note_state {
89d40145bfSdsl 	struct note_buf  *ns_first;
90d40145bfSdsl 	struct note_buf  *ns_last;
91d40145bfSdsl 	unsigned int     ns_count;       /* Of full buffers */
92d40145bfSdsl 	unsigned int     ns_offset;      /* Write point in last buffer */
9306920aefSthorpej };
9406920aefSthorpej 
95ce22e045Sdsl static int	ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *);
9651535d4bSthorpej 
97d40145bfSdsl static int	ELFNAMEEND(coredump_notes)(struct lwp *, struct note_state *);
98d40145bfSdsl static int	ELFNAMEEND(coredump_note)(struct lwp *, struct note_state *);
9951535d4bSthorpej 
100d40145bfSdsl /* The 'note' section names and data are always 4-byte aligned. */
10151535d4bSthorpej #define	ELFROUNDSIZE	4	/* XXX Should it be sizeof(Elf_Word)? */
10251535d4bSthorpej 
103ff456035Skamil #define elf_read_lwpstatus	CONCAT(process_read_lwpstatus, ELFSIZE)
104ff456035Skamil #define elf_lwpstatus		CONCAT(process_lwpstatus, ELFSIZE)
105ff456035Skamil 
106209be910Scube #define elf_process_read_regs	CONCAT(process_read_regs, ELFSIZE)
107209be910Scube #define elf_process_read_fpregs	CONCAT(process_read_fpregs, ELFSIZE)
108209be910Scube #define elf_reg			CONCAT(process_reg, ELFSIZE)
109209be910Scube #define elf_fpreg		CONCAT(process_fpreg, ELFSIZE)
110209be910Scube 
11151535d4bSthorpej int
ELFNAMEEND(real_coredump)1123d0d0adbSchristos ELFNAMEEND(real_coredump)(struct lwp *l, struct coredump_iostate *cookie)
11351535d4bSthorpej {
11451535d4bSthorpej 	Elf_Ehdr ehdr;
11509aa023cSmatt 	Elf_Shdr shdr;
116d40145bfSdsl 	Elf_Phdr *psections;
11755b033b1Spara 	size_t psectionssize;
118ac912300Sdsl 	int npsections;
11906920aefSthorpej 	struct writesegs_state ws;
120d40145bfSdsl 	off_t notestart;
1212de77aedSmatt 	size_t notesize;
12225a0e29aSmatt 	int error, i;
123b77cf5ebSpgoyette 	off_t offset __diagused;
12451535d4bSthorpej 
125d40145bfSdsl 	struct note_state ns;
126d40145bfSdsl 	struct note_buf *nb;
127d40145bfSdsl 
12825a0e29aSmatt 	psections = NULL;
129d40145bfSdsl 
130d40145bfSdsl 	/* Get all of the notes (mostly all the registers). */
131d40145bfSdsl 	ns.ns_first = kmem_alloc(sizeof *ns.ns_first, KM_SLEEP);
132d40145bfSdsl 	ns.ns_last = ns.ns_first;
133d40145bfSdsl 	ns.ns_count = 0;
134d40145bfSdsl 	ns.ns_offset = 0;
135d40145bfSdsl 	error = ELFNAMEEND(coredump_notes)(l, &ns);
136d40145bfSdsl 	ns.ns_last->nb_next = NULL;
137d40145bfSdsl 	if (error)
138d40145bfSdsl 		goto out;
139d40145bfSdsl 	notesize = ns.ns_count * sizeof nb->nb_data + ns.ns_offset;
140d40145bfSdsl 
14151535d4bSthorpej 	/*
14251535d4bSthorpej 	 * We have to make a total of 3 passes across the map:
14351535d4bSthorpej 	 *
14451535d4bSthorpej 	 *	1. Count the number of map entries (the number of
145ac912300Sdsl 	 *	   PT_LOAD sections in the dump).
14651535d4bSthorpej 	 *
14751535d4bSthorpej 	 *	2. Write the P-section headers.
14851535d4bSthorpej 	 *
14951535d4bSthorpej 	 *	3. Write the P-sections.
15051535d4bSthorpej 	 */
15151535d4bSthorpej 
15251535d4bSthorpej 	/* Pass 1: count the entries. */
15316e12c90Spgoyette 	MODULE_HOOK_CALL(uvm_coredump_count_segs_hook,
15416e12c90Spgoyette 	    (l->l_proc), 0, npsections);
155d40145bfSdsl 	/* Allow for the PT_NOTE section. */
156ac912300Sdsl 	npsections++;
15751535d4bSthorpej 
158d40145bfSdsl 	/* Build the main elf header */
159e1245a3cSmatt 	memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
16051535d4bSthorpej 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
16151535d4bSthorpej #if ELFSIZE == 32
16251535d4bSthorpej 	ehdr.e_ident[EI_CLASS] = ELFCLASS32;
16351535d4bSthorpej #elif ELFSIZE == 64
16451535d4bSthorpej 	ehdr.e_ident[EI_CLASS] = ELFCLASS64;
16551535d4bSthorpej #endif
16651535d4bSthorpej 	ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
16751535d4bSthorpej 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1686526b4abSkamil 	/*
1696526b4abSkamil 	 * NetBSD sets generic SYSV OSABI and ABI version 0
1706526b4abSkamil 	 * Native ELF files are distinguishable with NetBSD specific notes
1716526b4abSkamil 	 */
17251535d4bSthorpej 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
17306920aefSthorpej 	ehdr.e_ident[EI_ABIVERSION] = 0;
17451535d4bSthorpej 
17551535d4bSthorpej 	ehdr.e_type = ET_CORE;
17651535d4bSthorpej 	/* XXX This should be the e_machine of the executable. */
17751535d4bSthorpej 	ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
17851535d4bSthorpej 	ehdr.e_version = EV_CURRENT;
17951535d4bSthorpej 	ehdr.e_entry = 0;
18051535d4bSthorpej 	ehdr.e_flags = 0;
18151535d4bSthorpej 	ehdr.e_ehsize = sizeof(ehdr);
18251535d4bSthorpej 	ehdr.e_phentsize = sizeof(Elf_Phdr);
18309aa023cSmatt 	if (npsections < PN_XNUM) {
184ac912300Sdsl 		ehdr.e_phnum = npsections;
18551535d4bSthorpej 		ehdr.e_shentsize = 0;
18651535d4bSthorpej 		ehdr.e_shnum = 0;
18709aa023cSmatt 		ehdr.e_shoff = 0;
18809aa023cSmatt 		ehdr.e_phoff = sizeof(ehdr);
18909aa023cSmatt 	} else {
19009aa023cSmatt 		ehdr.e_phnum = PN_XNUM;
19109aa023cSmatt 		ehdr.e_shentsize = sizeof(Elf_Shdr);
19209aa023cSmatt 		ehdr.e_shnum = 1;
19309aa023cSmatt 		ehdr.e_shoff = sizeof(ehdr);
19409aa023cSmatt 		ehdr.e_phoff = sizeof(ehdr) + sizeof(shdr);
19509aa023cSmatt 	}
19651535d4bSthorpej 	ehdr.e_shstrndx = 0;
19751535d4bSthorpej 
198e110dba5Smatt #ifdef ELF_MD_COREDUMP_SETUP
199e110dba5Smatt 	ELF_MD_COREDUMP_SETUP(l, &ehdr);
200e110dba5Smatt #endif
201e110dba5Smatt 
20225a0e29aSmatt 	/* Write out the ELF header. */
20316e12c90Spgoyette 	MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE, &ehdr,
20416e12c90Spgoyette 	    sizeof(ehdr)), ENOSYS, error);
20525a0e29aSmatt 	if (error)
20625a0e29aSmatt 		goto out;
20725a0e29aSmatt 
20809aa023cSmatt 	/* Write out sections, if needed */
20909aa023cSmatt 	if (npsections >= PN_XNUM) {
21009aa023cSmatt 		memset(&shdr, 0, sizeof(shdr));
21109aa023cSmatt 		shdr.sh_type = SHT_NULL;
21209aa023cSmatt 		shdr.sh_info = npsections;
21316e12c90Spgoyette 		MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE,
21416e12c90Spgoyette 		    &shdr, sizeof(shdr)), ENOSYS, error);
21509aa023cSmatt 		if (error)
21609aa023cSmatt 			goto out;
21709aa023cSmatt 	}
21809aa023cSmatt 
219d40145bfSdsl 	psectionssize = npsections * sizeof(*psections);
22009aa023cSmatt 	notestart = ehdr.e_phoff + psectionssize;
22125a0e29aSmatt 
22255b033b1Spara 	psections = kmem_zalloc(psectionssize, KM_SLEEP);
22351535d4bSthorpej 
224ac912300Sdsl 	/* Pass 2: now find the P-section headers. */
225d40145bfSdsl 	ws.secoff = notestart + notesize;
22625a0e29aSmatt 	ws.psections = psections;
227d40145bfSdsl 	ws.npsections = npsections - 1;
22886bef90dSdsl 	ws.p = l->l_proc;
22916e12c90Spgoyette 	MODULE_HOOK_CALL(uvm_coredump_walkmap_hook,
23016e12c90Spgoyette 	    (l->l_proc, ELFNAMEEND(coredump_getseghdrs), &ws), ENOSYS, error);
23151535d4bSthorpej 	if (error)
23225a0e29aSmatt 		goto out;
233d40145bfSdsl 	if (ws.npsections != 0) {
234d40145bfSdsl 		/* A section went away */
235d40145bfSdsl 		error = ENOMEM;
236d40145bfSdsl 		goto out;
237d40145bfSdsl 	}
23851535d4bSthorpej 
239ac912300Sdsl 	/* Add the PT_NOTE header after the P-section headers. */
24025a0e29aSmatt 	ws.psections->p_type = PT_NOTE;
24125a0e29aSmatt 	ws.psections->p_offset = notestart;
24225a0e29aSmatt 	ws.psections->p_vaddr = 0;
24325a0e29aSmatt 	ws.psections->p_paddr = 0;
24425a0e29aSmatt 	ws.psections->p_filesz = notesize;
24525a0e29aSmatt 	ws.psections->p_memsz = 0;
24625a0e29aSmatt 	ws.psections->p_flags = PF_R;
24725a0e29aSmatt 	ws.psections->p_align = ELFROUNDSIZE;
24851535d4bSthorpej 
249d40145bfSdsl 	/* Write the P-section headers followed by the PT_NOTE header */
25016e12c90Spgoyette 	MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE, psections,
25116e12c90Spgoyette 	    psectionssize), ENOSYS, error);
25251535d4bSthorpej 	if (error)
25325a0e29aSmatt 		goto out;
25451535d4bSthorpej 
25506920aefSthorpej #ifdef DIAGNOSTIC
25616e12c90Spgoyette 	MODULE_HOOK_CALL(coredump_offset_hook, (cookie), 0, offset);
25716e12c90Spgoyette 	if (offset != notestart)
25806920aefSthorpej 		panic("coredump: offset %lld != notestart %lld",
25916e12c90Spgoyette 		    (long long) offset,
260d40145bfSdsl 		    (long long) notestart);
26106920aefSthorpej #endif
26251535d4bSthorpej 
26351535d4bSthorpej 	/* Write out the notes. */
264d40145bfSdsl 	for (nb = ns.ns_first; nb != NULL; nb = nb->nb_next) {
26516e12c90Spgoyette 		MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE,
26616e12c90Spgoyette 		    nb->nb_data,
26716e12c90Spgoyette 		    nb->nb_next == NULL ? ns.ns_offset : sizeof nb->nb_data),
26816e12c90Spgoyette 		    ENOSYS, error);
26925a0e29aSmatt 		if (error)
27025a0e29aSmatt 			goto out;
271d40145bfSdsl 	}
27206920aefSthorpej 
273d40145bfSdsl 	/* Finally, write the sections themselves. */
274ac912300Sdsl 	for (i = 0; i < npsections - 1; i++) {
27525a0e29aSmatt 		if (psections[i].p_filesz == 0)
27625a0e29aSmatt 			continue;
27751535d4bSthorpej 
27825a0e29aSmatt #ifdef DIAGNOSTIC
27916e12c90Spgoyette 		MODULE_HOOK_CALL(coredump_offset_hook, (cookie), 0, offset);
28016e12c90Spgoyette 		if (offset != psections[i].p_offset)
28125a0e29aSmatt 			panic("coredump: offset %lld != p_offset[%d] %lld",
28216e12c90Spgoyette 			    (long long) offset, i,
28325a0e29aSmatt 			    (long long) psections[i].p_filesz);
28425a0e29aSmatt #endif
28525a0e29aSmatt 
28616e12c90Spgoyette 		MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_USERSPACE,
287e1245a3cSmatt 		    (void *)(vaddr_t)psections[i].p_vaddr,
28816e12c90Spgoyette 		    psections[i].p_filesz), ENOSYS, error);
28925a0e29aSmatt 		if (error)
29025a0e29aSmatt 			goto out;
29125a0e29aSmatt 	}
29225a0e29aSmatt 
29325a0e29aSmatt   out:
29425a0e29aSmatt 	if (psections)
29555b033b1Spara 		kmem_free(psections, psectionssize);
2964e897b50Sdsl 	while ((nb = ns.ns_first) != NULL) {
2974e897b50Sdsl 		ns.ns_first = nb->nb_next;
298d40145bfSdsl 		kmem_free(nb, sizeof *nb);
2995418fb9cSmrg 	}
30006920aefSthorpej 	return (error);
30151535d4bSthorpej }
30251535d4bSthorpej 
30359466612Sthorpej static int
ELFNAMEEND(coredump_getseghdrs)304ce22e045Sdsl ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *us)
30506920aefSthorpej {
30606920aefSthorpej 	struct writesegs_state *ws = us->cookie;
30706920aefSthorpej 	Elf_Phdr phdr;
30825a0e29aSmatt 	vsize_t size, realsize;
30925a0e29aSmatt 	vaddr_t end;
31006920aefSthorpej 	int error;
31106920aefSthorpej 
312d40145bfSdsl 	/* Don't overrun if there are more sections */
313d40145bfSdsl 	if (ws->npsections == 0)
314d40145bfSdsl 		return ENOMEM;
315d40145bfSdsl 	ws->npsections--;
316d40145bfSdsl 
31706920aefSthorpej 	size = us->end - us->start;
31825a0e29aSmatt 	realsize = us->realend - us->start;
31925a0e29aSmatt 	end = us->realend;
32025a0e29aSmatt 
321ac912300Sdsl 	/* Don't bother writing out trailing zeros */
32225a0e29aSmatt 	while (realsize > 0) {
32325a0e29aSmatt 		long buf[1024 / sizeof(long)];
32425a0e29aSmatt 		size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
32525a0e29aSmatt 		const long *ep;
32625a0e29aSmatt 		int i;
32725a0e29aSmatt 
32825a0e29aSmatt 		end -= slen;
3291454a1abSkamil 		if ((error = copyin_proc(ws->p, (void *)end, buf, slen)) != 0) {
3301454a1abSkamil 			/*
3311454a1abSkamil 			 * In case of any errors of scanning the segments reset
3321454a1abSkamil 			 * their content to a default value with zeros. This is
3331454a1abSkamil 			 * achieved with shortening the p_filesz parameter.
3341454a1abSkamil 			 *
3351454a1abSkamil 			 * This allows to emit core(5) files for a process
3361454a1abSkamil 			 * regardless of its state of mappings, such as mapping
3371454a1abSkamil 			 * pages after EOF in a file.
3381454a1abSkamil 			 */
3391454a1abSkamil 			realsize -= slen;
3401454a1abSkamil 			continue;
3411454a1abSkamil 		}
34225a0e29aSmatt 
34325a0e29aSmatt 		ep = (const long *) &buf[slen / sizeof(buf[0])];
34425a0e29aSmatt 		for (i = 0, ep--; buf <= ep; ep--, i++) {
34525a0e29aSmatt 			if (*ep)
34625a0e29aSmatt 				break;
34725a0e29aSmatt 		}
34825a0e29aSmatt 		realsize -= i * sizeof(buf[0]);
34925a0e29aSmatt 		if (i * sizeof(buf[0]) < slen)
35025a0e29aSmatt 			break;
35125a0e29aSmatt 	}
35206920aefSthorpej 
35306920aefSthorpej 	phdr.p_type = PT_LOAD;
35406920aefSthorpej 	phdr.p_offset = ws->secoff;
35506920aefSthorpej 	phdr.p_vaddr = us->start;
35606920aefSthorpej 	phdr.p_paddr = 0;
35725a0e29aSmatt 	phdr.p_filesz = realsize;
35806920aefSthorpej 	phdr.p_memsz = size;
35906920aefSthorpej 	phdr.p_flags = 0;
36006920aefSthorpej 	if (us->prot & VM_PROT_READ)
36106920aefSthorpej 		phdr.p_flags |= PF_R;
36206920aefSthorpej 	if (us->prot & VM_PROT_WRITE)
36306920aefSthorpej 		phdr.p_flags |= PF_W;
36406920aefSthorpej 	if (us->prot & VM_PROT_EXECUTE)
36506920aefSthorpej 		phdr.p_flags |= PF_X;
36606920aefSthorpej 	phdr.p_align = PAGE_SIZE;
36706920aefSthorpej 
36806920aefSthorpej 	ws->secoff += phdr.p_filesz;
36925a0e29aSmatt 	*ws->psections++ = phdr;
37006920aefSthorpej 
37151535d4bSthorpej 	return (0);
37251535d4bSthorpej }
37351535d4bSthorpej 
3748d3dedcfSchristos static void
coredump_note_procinfo(struct lwp * l,struct note_state * ns)3758d3dedcfSchristos coredump_note_procinfo(struct lwp *l, struct note_state *ns)
37651535d4bSthorpej {
377d40145bfSdsl 	struct proc *p;
37851535d4bSthorpej 	struct netbsd_elfcore_procinfo cpi;
37951535d4bSthorpej 
380d40145bfSdsl 	p = l->l_proc;
38151535d4bSthorpej 
38251535d4bSthorpej 	/* First, write an elfcore_procinfo. */
38351535d4bSthorpej 	cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
38451535d4bSthorpej 	cpi.cpi_cpisize = sizeof(cpi);
385a0eca9c7Skamil 	cpi.cpi_signo = p->p_sigctx.ps_info._signo;
386a0eca9c7Skamil 	cpi.cpi_sigcode = p->p_sigctx.ps_info._code;
3878e02eedcSnathanw 	cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
38851535d4bSthorpej 
389b07ec3fcSad 	/*
390ff456035Skamil 	 * per-LWP pending signals are stored in PT_LWPSTATUS@nnn.
391b07ec3fcSad 	 */
392ff456035Skamil 	memcpy(&cpi.cpi_sigpend, &p->p_sigpend.sp_set, sizeof(cpi.cpi_sigpend));
393ff456035Skamil 
394ff456035Skamil 	/*
395ff456035Skamil 	 * Signal mask is stored on a per-LWP basis in PT_LWPSTATUS@nnn.
396ff456035Skamil 	 * For compatibility purposes, cpi_sigmask is present, but zeroed.
397ff456035Skamil 	 */
398ff456035Skamil 	memset(&cpi.cpi_sigmask, 0, sizeof(cpi.cpi_sigmask));
399ff456035Skamil 
40051535d4bSthorpej 	memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
40151535d4bSthorpej 	    sizeof(cpi.cpi_sigignore));
40251535d4bSthorpej 	memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
40351535d4bSthorpej 	    sizeof(cpi.cpi_sigcatch));
40451535d4bSthorpej 
40551535d4bSthorpej 	cpi.cpi_pid = p->p_pid;
40614b4bbb2Sad 	mutex_enter(&proc_lock);
40751535d4bSthorpej 	cpi.cpi_ppid = p->p_pptr->p_pid;
40851535d4bSthorpej 	cpi.cpi_pgrp = p->p_pgid;
40951535d4bSthorpej 	cpi.cpi_sid = p->p_session->s_sid;
41014b4bbb2Sad 	mutex_exit(&proc_lock);
41151535d4bSthorpej 
412f474dcebSad 	cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
413f474dcebSad 	cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
414f474dcebSad 	cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
41551535d4bSthorpej 
416f474dcebSad 	cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
417f474dcebSad 	cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
418f474dcebSad 	cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
41951535d4bSthorpej 
420e0d8d366Sthorpej 	cpi.cpi_nlwps = p->p_nlwps;
42145af66f7Schristos 	(void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
42245af66f7Schristos 	cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
42351535d4bSthorpej 
42486bef90dSdsl 	ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_PROCINFO,
42586bef90dSdsl 	    ELF_NOTE_NETBSD_CORE_NAME, &cpi, sizeof(cpi));
4268d3dedcfSchristos }
4278d3dedcfSchristos 
4288d3dedcfSchristos static int
coredump_note_auxv(struct lwp * l,struct note_state * ns)4298d3dedcfSchristos coredump_note_auxv(struct lwp *l, struct note_state *ns)
4308d3dedcfSchristos {
4318d3dedcfSchristos 	int error;
43272197416Schristos 	size_t len;
43372197416Schristos 	void *kauxv;
4348d3dedcfSchristos 
43572197416Schristos 	if ((error = proc_getauxv(l->l_proc, &kauxv, &len)) != 0)
4368d3dedcfSchristos 		return error;
4378d3dedcfSchristos 
4388d3dedcfSchristos 	ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_AUXV,
4398d3dedcfSchristos 	    ELF_NOTE_NETBSD_CORE_NAME, kauxv, len);
4408d3dedcfSchristos 
4418d3dedcfSchristos 	kmem_free(kauxv, len);
44272197416Schristos 	return 0;
4438d3dedcfSchristos }
4448d3dedcfSchristos 
4458d3dedcfSchristos static int
ELFNAMEEND(coredump_notes)4468d3dedcfSchristos ELFNAMEEND(coredump_notes)(struct lwp *l, struct note_state *ns)
4478d3dedcfSchristos {
4488d3dedcfSchristos 	int error;
4498d3dedcfSchristos 	struct lwp *l0;
4508d3dedcfSchristos 	struct proc *p = l->l_proc;
4518d3dedcfSchristos 
4528d3dedcfSchristos 	coredump_note_procinfo(l, ns);
4538d3dedcfSchristos 	error = coredump_note_auxv(l, ns);
4548d3dedcfSchristos 	if (error)
4558d3dedcfSchristos 		return error;
45651535d4bSthorpej 
45751535d4bSthorpej 	/* XXX Add hook for machdep per-proc notes. */
45851535d4bSthorpej 
45951535d4bSthorpej 	/*
4602de77aedSmatt 	 * Now write the register info for the thread that caused the
4612de77aedSmatt 	 * coredump.
4622de77aedSmatt 	 */
463d40145bfSdsl 	error = ELFNAMEEND(coredump_note)(l, ns);
4642de77aedSmatt 	if (error)
4658d3dedcfSchristos 		return error;
4662de77aedSmatt 
4672de77aedSmatt 	/*
46851535d4bSthorpej 	 * Now, for each LWP, write the register info and any other
469ce22e045Sdsl 	 * per-LWP notes.
470ce22e045Sdsl 	 * Lock in case this is a gcore requested dump.
47151535d4bSthorpej 	 */
472ce22e045Sdsl 	mutex_enter(p->p_lock);
4732de77aedSmatt 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
4742de77aedSmatt 		if (l0 == l)		/* we've taken care of this thread */
4752de77aedSmatt 			continue;
476d40145bfSdsl 		error = ELFNAMEEND(coredump_note)(l0, ns);
4772de77aedSmatt 		if (error)
478ce22e045Sdsl 			break;
4792de77aedSmatt 	}
480ce22e045Sdsl 	mutex_exit(p->p_lock);
4812de77aedSmatt 
482ce22e045Sdsl 	return error;
4832de77aedSmatt }
4842de77aedSmatt 
485853d8728Sthorpej struct elf_coredump_note_data {
486d40145bfSdsl 	char name[64];
487ff456035Skamil 	elf_lwpstatus els;
488209be910Scube 	elf_reg intreg;
4892de77aedSmatt #ifdef PT_GETFPREGS
490209be910Scube 	elf_fpreg freg;
491853d8728Sthorpej #endif
492853d8728Sthorpej };
493853d8728Sthorpej 
494853d8728Sthorpej static int
ELFNAMEEND(coredump_note)495853d8728Sthorpej ELFNAMEEND(coredump_note)(struct lwp *l, struct note_state *ns)
496853d8728Sthorpej {
497853d8728Sthorpej 	struct elf_coredump_note_data *d;
498853d8728Sthorpej #ifdef PT_GETFPREGS
499d40145bfSdsl 	size_t freglen;
5002de77aedSmatt #endif
501853d8728Sthorpej 	int error;
5022de77aedSmatt 
503853d8728Sthorpej 	d = kmem_alloc(sizeof(*d), KM_SLEEP);
504853d8728Sthorpej 
505853d8728Sthorpej 	snprintf(d->name, sizeof(d->name), "%s@%d",
506e1245a3cSmatt 	    ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
50751535d4bSthorpej 
508853d8728Sthorpej 	elf_read_lwpstatus(l, &d->els);
509ff456035Skamil 
510853d8728Sthorpej 	ELFNAMEEND(coredump_savenote)(ns, PT_LWPSTATUS, d->name, &d->els,
511853d8728Sthorpej 	    sizeof(d->els));
512ff456035Skamil 
513853d8728Sthorpej 	error = elf_process_read_regs(l, &d->intreg);
51451535d4bSthorpej 	if (error)
515853d8728Sthorpej 		goto out;
51651535d4bSthorpej 
517ccbfb8c2Srin 	ELFNAMEEND(coredump_savenote)(ns,
518ccbfb8c2Srin #if ELFSIZE == 32 && defined(PT32_GETREGS)
519ccbfb8c2Srin 	    PT32_GETREGS,
520ccbfb8c2Srin #else
521ccbfb8c2Srin 	    PT_GETREGS,
522ccbfb8c2Srin #endif
523ccbfb8c2Srin 	    d->name, &d->intreg, sizeof(d->intreg));
52451535d4bSthorpej 
52551535d4bSthorpej #ifdef PT_GETFPREGS
526853d8728Sthorpej 	freglen = sizeof(d->freg);
527853d8728Sthorpej 	error = elf_process_read_fpregs(l, &d->freg, &freglen);
52851535d4bSthorpej 	if (error)
529853d8728Sthorpej 		goto out;
53051535d4bSthorpej 
531ccbfb8c2Srin 	ELFNAMEEND(coredump_savenote)(ns,
532ccbfb8c2Srin #  if ELFSIZE == 32 && defined(PT32_GETFPREGS)
533ccbfb8c2Srin 	    PT32_GETFPREGS,
534ccbfb8c2Srin #  else
535ccbfb8c2Srin 	    PT_GETFPREGS,
536ccbfb8c2Srin #  endif
537ccbfb8c2Srin 	    d->name, &d->freg, freglen);
53851535d4bSthorpej #endif
539d63d1917Smgorny 
540d63d1917Smgorny #ifdef COREDUMP_MACHDEP_LWP_NOTES
541853d8728Sthorpej 	COREDUMP_MACHDEP_LWP_NOTES(l, ns, d->name);
542d63d1917Smgorny #endif
543d63d1917Smgorny 
544853d8728Sthorpej  out:
545853d8728Sthorpej 	kmem_free(d, sizeof(*d));
546853d8728Sthorpej 	return (error);
54751535d4bSthorpej }
54851535d4bSthorpej 
549d40145bfSdsl static void
save_note_bytes(struct note_state * ns,const void * data,size_t len)550d40145bfSdsl save_note_bytes(struct note_state *ns, const void *data, size_t len)
55151535d4bSthorpej {
552d40145bfSdsl 	struct note_buf *nb = ns->ns_last;
553d40145bfSdsl 	size_t copylen;
554d40145bfSdsl 	unsigned char *wp;
55551535d4bSthorpej 
556d40145bfSdsl 	/*
557d40145bfSdsl 	 * Just copy the data into a buffer list.
558d40145bfSdsl 	 * All but the last buffer is full.
559d40145bfSdsl 	 */
560d40145bfSdsl 	for (;;) {
561a8a5c538Sriastradh 		copylen = uimin(len, sizeof(nb->nb_data) - ns->ns_offset);
562d40145bfSdsl 		wp = nb->nb_data + ns->ns_offset;
563d40145bfSdsl 		memcpy(wp, data, copylen);
564d40145bfSdsl 		if (copylen == len)
565d40145bfSdsl 			break;
5662fbdc140Schristos 		nb->nb_next = kmem_alloc(sizeof(*nb->nb_next), KM_SLEEP);
567d40145bfSdsl 		nb = nb->nb_next;
568d40145bfSdsl 		ns->ns_last = nb;
569d40145bfSdsl 		ns->ns_count++;
570d40145bfSdsl 		ns->ns_offset = 0;
571d40145bfSdsl 		len -= copylen;
572d40145bfSdsl 		data = (const unsigned char *)data + copylen;
573d40145bfSdsl 	}
57451535d4bSthorpej 
5752fbdc140Schristos 	while ((copylen & (ELFROUNDSIZE - 1)) &&
5762fbdc140Schristos 	    wp + copylen < nb->nb_data + sizeof(nb->nb_data))
577d40145bfSdsl 		wp[copylen++] = 0;
57851535d4bSthorpej 
579d40145bfSdsl 	ns->ns_offset += copylen;
580d40145bfSdsl }
581d40145bfSdsl 
582d40145bfSdsl void
ELFNAMEEND(coredump_savenote)58386bef90dSdsl ELFNAMEEND(coredump_savenote)(struct note_state *ns, unsigned int type,
58486bef90dSdsl     const char *name, void *data, size_t data_len)
585d40145bfSdsl {
58686bef90dSdsl 	Elf_Nhdr nhdr;
58786bef90dSdsl 
58886bef90dSdsl 	nhdr.n_namesz = strlen(name) + 1;
58986bef90dSdsl 	nhdr.n_descsz = data_len;
59086bef90dSdsl 	nhdr.n_type = type;
59186bef90dSdsl 
59286bef90dSdsl 	save_note_bytes(ns, &nhdr, sizeof (nhdr));
59386bef90dSdsl 	save_note_bytes(ns, name, nhdr.n_namesz);
59486bef90dSdsl 	save_note_bytes(ns, data, data_len);
59551535d4bSthorpej }
596