1 /* $NetBSD: core_machdep.c,v 1.8 2023/12/20 00:40:43 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1986, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: Utah $Hdr: vm_machdep.c 1.21 91/04/06$
37 *
38 * @(#)vm_machdep.c 8.6 (Berkeley) 1/12/94
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: core_machdep.c,v 1.8 2023/12/20 00:40:43 thorpej Exp $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/buf.h>
48 #include <sys/vnode.h>
49 #include <sys/core.h>
50 #include <sys/exec.h>
51 #include <sys/compat_stub.h>
52
53 #include <sys/exec_aout.h>
54
55 #include <machine/frame.h>
56 #include <machine/cpu.h>
57 #include <machine/pte.h>
58 #include <machine/reg.h>
59
60 #include <uvm/uvm_extern.h>
61
62 /*
63 * Dump the machine specific header information at the start of a core dump.
64 */
65 struct md_core {
66 struct reg intreg;
67 struct fpreg freg;
68 };
69
70 int
cpu_coredump(struct lwp * l,struct coredump_iostate * iocookie,struct core * chdr)71 cpu_coredump(struct lwp *l, struct coredump_iostate *iocookie,
72 struct core *chdr)
73 {
74 struct md_core md_core;
75 struct coreseg cseg;
76 int error;
77
78 if (iocookie == NULL) {
79 CORE_SETMAGIC(*chdr, COREMAGIC, MID_MACHINE, 0);
80 chdr->c_hdrsize = ALIGN(sizeof(*chdr));
81 chdr->c_seghdrsize = ALIGN(sizeof(cseg));
82 chdr->c_cpusize = sizeof(md_core);
83 chdr->c_nseg++;
84 return 0;
85 }
86
87 /* Save integer registers. */
88 error = process_read_regs(l, &md_core.intreg);
89 if (error)
90 return error;
91
92 if (fputype) {
93 /* Save floating point registers. */
94 error = process_read_fpregs(l, &md_core.freg, NULL);
95 if (error)
96 return error;
97 } else {
98 /* Make sure these are clear. */
99 memset((void *)&md_core.freg, 0, sizeof(md_core.freg));
100 }
101
102 CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_MACHINE, CORE_CPU);
103 cseg.c_addr = 0;
104 cseg.c_size = chdr->c_cpusize;
105
106 MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE, &cseg,
107 chdr->c_seghdrsize), ENOSYS, error);
108 if (error)
109 return error;
110
111 MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE, &md_core,
112 sizeof(md_core)), ENOSYS, error);
113
114 return error;
115 }
116