1 /* $NetBSD: core_machdep.c,v 1.11 2020/07/06 11:05:05 rin Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: core_machdep.c,v 1.11 2020/07/06 11:05:05 rin Exp $");
36
37 #ifdef _KERNEL_OPT
38 #include "opt_altivec.h"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/core.h>
43 #include <sys/exec.h>
44 #include <sys/proc.h>
45 #include <sys/systm.h>
46 #include <sys/vnode.h>
47 #include <sys/compat_stub.h>
48
49 #include <sys/exec_aout.h>
50
51 #include <uvm/uvm_extern.h>
52
53 #if defined(ALTIVEC) || defined(PPC_HAVE_SPE)
54 #include <powerpc/altivec.h>
55 #endif
56 #include <machine/fpu.h>
57 #include <machine/pcb.h>
58
59 /*
60 * Write the machine-dependent part of a core dump.
61 */
62 int
cpu_coredump(struct lwp * l,struct coredump_iostate * iocookie,struct core * chdr)63 cpu_coredump(struct lwp *l, struct coredump_iostate *iocookie,
64 struct core *chdr)
65 {
66 struct coreseg cseg;
67 struct md_coredump md_core;
68 struct pcb * const pcb = lwp_getpcb(l);
69 int error;
70
71 if (iocookie == NULL) {
72 CORE_SETMAGIC(*chdr, COREMAGIC, MID_POWERPC, 0);
73 chdr->c_hdrsize = ALIGN(sizeof *chdr);
74 chdr->c_seghdrsize = ALIGN(sizeof cseg);
75 chdr->c_cpusize = sizeof md_core;
76 chdr->c_nseg++;
77 return 0;
78 }
79
80 md_core.frame = *l->l_md.md_utf;
81 pcu_save_all(l);
82 if (fpu_used_p(l)) {
83 md_core.fpstate = pcb->pcb_fpu;
84 } else
85 memset(&md_core.fpstate, 0, sizeof(md_core.fpstate));
86
87 #if defined(ALTIVEC) || defined(PPC_HAVE_SPE)
88 if (vec_used_p(l)) {
89 md_core.vstate = pcb->pcb_vr;
90 } else
91 #endif
92 memset(&md_core.vstate, 0, sizeof(md_core.vstate));
93
94 CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_MACHINE, CORE_CPU);
95 cseg.c_addr = 0;
96 cseg.c_size = chdr->c_cpusize;
97
98 MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE, &cseg,
99 chdr->c_seghdrsize), ENOSYS, error);
100 if (error)
101 return error;
102
103 MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE, &md_core,
104 sizeof(md_core)), ENOSYS, error);
105
106 return error;
107 }
108