1 /* $NetBSD: core_machdep.c,v 1.6 2023/05/07 12:41:48 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33
34 #ifndef CORENAME
35 __RCSID("$NetBSD: core_machdep.c,v 1.6 2023/05/07 12:41:48 skrll Exp $");
36 #endif
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/core.h>
42 #include <sys/exec.h>
43 #include <sys/cpu.h>
44 #include <sys/compat_stub.h>
45
46 #include <riscv/frame.h>
47 #include <riscv/locore.h>
48
49 #ifndef CORENAME
50 #define CORENAME(n) n
51 #endif
52 #ifdef COREINC
53 #include COREINC
54 #endif
55
56 /*
57 * Dump the machine specific segment at the start of a core dump.
58 */
59 int
CORENAME(cpu_coredump)60 CORENAME(cpu_coredump)(struct lwp *l, struct coredump_iostate *iocookie,
61 struct CORENAME(core) *chdr)
62 {
63 int error;
64 struct CORENAME(coreseg) cseg;
65 struct cpustate {
66 struct CORENAME(trapframe) tf;
67 struct fpreg fpregs;
68 } cpustate;
69
70 if (iocookie == NULL) {
71 CORE_SETMAGIC(*chdr, COREMAGIC, MID_MACHINE, 0);
72 chdr->c_hdrsize = ALIGN(sizeof(*chdr));
73 chdr->c_seghdrsize = ALIGN(sizeof(cseg));
74 chdr->c_cpusize = sizeof(struct cpustate);
75 chdr->c_nseg++;
76 return 0;
77 }
78
79 pcu_save_all(l);
80
81 // Can't use structure assignment if this is doing COMPAT_NETBSD32
82 const struct trapframe * const tf = l->l_md.md_utf;
83 for (size_t i = _X_RA; i <= _X_GP; i++) {
84 cpustate.tf.tf_reg[i] = tf->tf_reg[i];
85 }
86 cpustate.tf.tf_pc = tf->tf_pc;
87 cpustate.tf.tf_tval = tf->tf_tval;
88 cpustate.tf.tf_cause = tf->tf_cause;
89 cpustate.tf.tf_sr = tf->tf_sr;
90 if (fpu_valid_p(l)) {
91 cpustate.fpregs = ((struct pcb *)lwp_getpcb(l))->pcb_fpregs;
92 } else {
93 memset(&cpustate.fpregs, 0, sizeof(cpustate.fpregs));
94 }
95 CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_MACHINE, CORE_CPU);
96 cseg.c_addr = 0;
97 cseg.c_size = chdr->c_cpusize;
98
99 MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE, &cseg,
100 chdr->c_seghdrsize), ENOSYS, error);
101 if (error)
102 return error;
103
104 MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE,
105 &cpustate, chdr->c_cpusize), ENOSYS, error);
106
107 return error;
108 }
109