2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2021 Edward Tomasz Napierala <trasz@FreeBSD.org>
5 * Copyright (c) 2018 Chuck Tuffli
6 * Copyright (c) 2017 Dell EMC
7 * Copyright (c) 2000 David O'Brien
8 * Copyright (c) 1995-1996 Søren Schmidt
9 * Copyright (c) 1996 Peter Wemm
10 * All rights reserved.
12 * This software was developed by the University of Cambridge Computer
13 * Laboratory as part of the CHERI for Hypervisors and Operating Systems
14 * (CHaOS) project, funded by EPSRC grant EP/V000292/1.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer
21 * in this position and unchanged.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. The name of the author may not be used to endorse or promote products
26 * derived from this software without specific prior written permission
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
45 #include <sys/imgact.h>
46 #include <sys/imgact_elf.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
51 #include <sys/procfs.h>
54 #include <sys/sysent.h>
58 #include <vm/vm_map.h>
60 #include <machine/elf.h>
63 #define linux_pt_regset linux_pt_regset32
64 #define bsd_to_linux_regset bsd_to_linux_regset32
65 #include <machine/../linux32/linux.h>
67 #include <machine/../linux/linux.h>
69 #include <compat/linux/linux_elf.h>
70 #include <compat/linux/linux_mib.h>
71 #include <compat/linux/linux_misc.h>
73 struct l_elf_siginfo {
79 typedef struct linux_pt_regset l_elf_gregset_t;
81 struct linux_elf_prstatus {
82 struct l_elf_siginfo pr_info;
94 l_elf_gregset_t pr_reg;
98 #define LINUX_NT_AUXV 6
100 static void __linuxN(note_fpregset)(void *, struct sbuf *, size_t *);
101 static void __linuxN(note_prpsinfo)(void *, struct sbuf *, size_t *);
102 static void __linuxN(note_prstatus)(void *, struct sbuf *, size_t *);
103 static void __linuxN(note_threadmd)(void *, struct sbuf *, size_t *);
104 static void __linuxN(note_nt_auxv)(void *, struct sbuf *, size_t *);
107 __linuxN(prepare_notes)(struct thread *td, struct note_info_list *list,
118 * To have the debugger select the right thread (LWP) as the initial
119 * thread, we dump the state of the thread passed to us in td first.
120 * This is the thread that causes the core dump and thus likely to
121 * be the right thread one wants to have selected in the debugger.
124 while (thr != NULL) {
125 size += __elfN(register_note)(td, list,
126 NT_PRSTATUS, __linuxN(note_prstatus), thr);
127 size += __elfN(register_note)(td, list,
128 NT_PRPSINFO, __linuxN(note_prpsinfo), p);
129 size += __elfN(register_note)(td, list,
130 LINUX_NT_AUXV, __linuxN(note_nt_auxv), p);
131 size += __elfN(register_note)(td, list,
132 NT_FPREGSET, __linuxN(note_fpregset), thr);
133 size += __elfN(register_note)(td, list,
134 -1, __linuxN(note_threadmd), thr);
136 thr = thr == td ? TAILQ_FIRST(&p->p_threads) :
137 TAILQ_NEXT(thr, td_plist);
139 thr = TAILQ_NEXT(thr, td_plist);
145 typedef struct linux_elf_prstatus linux_elf_prstatus_t;
146 #ifdef COMPAT_LINUX32
147 typedef struct prpsinfo32 linux_elf_prpsinfo_t;
148 typedef struct fpreg32 linux_elf_prfpregset_t;
150 typedef prpsinfo_t linux_elf_prpsinfo_t;
151 typedef prfpregset_t linux_elf_prfpregset_t;
155 __linuxN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
161 linux_elf_prpsinfo_t *psinfo;
166 KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
167 psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
168 psinfo->pr_version = PRPSINFO_VERSION;
169 psinfo->pr_psinfosz = sizeof(linux_elf_prpsinfo_t);
170 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
172 if (p->p_args != NULL) {
173 len = sizeof(psinfo->pr_psargs) - 1;
174 if (len > p->p_args->ar_length)
175 len = p->p_args->ar_length;
176 memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
182 sbuf_new(&sbarg, psinfo->pr_psargs,
183 sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
184 error = proc_getargv(curthread, p, &sbarg);
186 if (sbuf_finish(&sbarg) == 0) {
187 len = sbuf_len(&sbarg) - 1;
191 len = sizeof(psinfo->pr_psargs) - 1;
195 if (error != 0 || len == 0 || (ssize_t)len == -1)
196 strlcpy(psinfo->pr_psargs, p->p_comm,
197 sizeof(psinfo->pr_psargs));
199 KASSERT(len < sizeof(psinfo->pr_psargs),
200 ("len is too long: %zu vs %zu", len,
201 sizeof(psinfo->pr_psargs)));
202 cp = psinfo->pr_psargs;
205 cp = memchr(cp, '\0', end - cp);
211 psinfo->pr_pid = p->p_pid;
212 sbuf_bcat(sb, psinfo, sizeof(*psinfo));
213 free(psinfo, M_TEMP);
215 *sizep = sizeof(*psinfo);
219 __linuxN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
222 linux_elf_prstatus_t *status;
223 #ifdef COMPAT_LINUX32
231 KASSERT(*sizep == sizeof(*status), ("invalid size"));
232 status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
235 * XXX: Some fields missing.
237 status->pr_cursig = td->td_proc->p_sig;
238 status->pr_pid = td->td_tid;
240 #ifdef COMPAT_LINUX32
241 fill_regs32(td, &pr_reg);
243 fill_regs(td, &pr_reg);
245 bsd_to_linux_regset(&pr_reg, &status->pr_reg);
246 sbuf_bcat(sb, status, sizeof(*status));
247 free(status, M_TEMP);
249 *sizep = sizeof(*status);
253 __linuxN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
256 linux_elf_prfpregset_t *fpregset;
260 KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
261 fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
262 #ifdef COMPAT_LINUX32
263 fill_fpregs32(td, fpregset);
265 fill_fpregs(td, fpregset);
267 sbuf_bcat(sb, fpregset, sizeof(*fpregset));
268 free(fpregset, M_TEMP);
270 *sizep = sizeof(*fpregset);
274 * Allow for MD specific notes, as well as any MD
275 * specific preparations for writing MI notes.
278 __linuxN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
286 if (size != 0 && sb != NULL)
287 buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
291 __elfN(dump_thread)(td, buf, &size);
292 KASSERT(sb == NULL || *sizep == size, ("invalid size"));
293 if (size != 0 && sb != NULL)
294 sbuf_bcat(sb, buf, size);
300 __linuxN(note_nt_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
308 sb = sbuf_new(NULL, NULL, LINUX_AT_COUNT * sizeof(Elf_Auxinfo),
310 sbuf_set_drain(sb, sbuf_count_drain, &size);
312 proc_getauxv(curthread, p, sb);
319 proc_getauxv(curthread, p, sb);
325 * Copy strings out to the new process address space, constructing new arg
326 * and env vector tables. Return a pointer to the base so that it can be used
327 * as the initial stack pointer.
330 __linuxN(copyout_strings)(struct image_params *imgp, uintptr_t *stack_base)
332 char canary[LINUX_AT_RANDOM_LEN];
335 uintptr_t destp, ustringp;
336 struct ps_strings *arginfo;
343 destp = PROC_PS_STRINGS(p);
344 arginfo = imgp->ps_strings = (void *)destp;
347 * Copy the image path for the rtld.
349 if (imgp->execpath != NULL && imgp->auxargs != NULL) {
350 execpath_len = strlen(imgp->execpath) + 1;
351 destp -= execpath_len;
352 destp = rounddown2(destp, sizeof(void *));
353 imgp->execpathp = (void *)destp;
354 error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
360 * Prepare the canary for SSP.
362 arc4rand(canary, sizeof(canary), 0);
363 destp -= sizeof(canary);
364 imgp->canary = (void *)destp;
365 error = copyout(canary, imgp->canary, sizeof(canary));
368 imgp->canarylen = sizeof(canary);
371 * Allocate room for the argument and environment strings.
373 destp -= ARG_MAX - imgp->args->stringspace;
374 destp = rounddown2(destp, sizeof(void *));
379 * Allocate room on the stack for the ELF auxargs
380 * array. It has up to LINUX_AT_COUNT entries.
382 destp -= LINUX_AT_COUNT * sizeof(Elf_Auxinfo);
383 destp = rounddown2(destp, sizeof(void *));
386 vectp = (char **)destp;
389 * Allocate room for the argv[] and env vectors including the
390 * terminating NULL pointers.
392 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
395 * Starting with 2.24, glibc depends on a 16-byte stack alignment.
397 vectp = (char **)((((uintptr_t)vectp + 8) & ~0xF) - 8);
400 * vectp also becomes our initial stack base
402 *stack_base = (uintptr_t)vectp;
404 stringp = imgp->args->begin_argv;
405 argc = imgp->args->argc;
406 envc = imgp->args->envc;
409 * Copy out strings - arguments and environment.
411 error = copyout(stringp, (void *)ustringp,
412 ARG_MAX - imgp->args->stringspace);
417 * Fill in "ps_strings" struct for ps, w, etc.
420 if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
421 suword32(&arginfo->ps_nargvstr, argc) != 0)
425 * Fill in argument portion of vector table.
427 for (; argc > 0; --argc) {
428 if (suword(vectp++, ustringp) != 0)
430 while (*stringp++ != 0)
435 /* a null vector table pointer separates the argp's from the envp's */
436 if (suword(vectp++, 0) != 0)
440 if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
441 suword32(&arginfo->ps_nenvstr, envc) != 0)
445 * Fill in environment portion of vector table.
447 for (; envc > 0; --envc) {
448 if (suword(vectp++, ustringp) != 0)
450 while (*stringp++ != 0)
455 /* end of vector table is a null pointer */
456 if (suword(vectp, 0) != 0)
461 error = imgp->sysent->sv_copyout_auxargs(imgp,
471 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
473 const Elf32_Word *desc;
476 p = (uintptr_t)(note + 1);
477 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
479 desc = (const Elf32_Word *)p;
480 if (desc[0] != GNU_ABI_LINUX)
483 * For Linux we encode osrel using the Linux convention of
484 * (version << 16) | (major << 8) | (minor)
485 * See macro in linux_mib.h
487 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);