1 /* $NetBSD: core_machdep.c,v 1.7 2019/11/20 19:37:53 pgoyette Exp $ */
2
3 /*
4 * Copyright (c) 1996-2002 Eduardo Horvath. All rights reserved.
5 * Copyright (c) 1996
6 * The President and Fellows of Harvard College. All rights reserved.
7 * Copyright (c) 1992, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * This software was developed by the Computer Systems Engineering group
11 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
12 * contributed to Berkeley.
13 *
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Lawrence Berkeley Laboratory.
18 * This product includes software developed by Harvard University.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by Harvard University.
31 * This product includes software developed by the University of
32 * California, Berkeley and its contributors.
33 * 4. Neither the name of the University nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 *
49 * @(#)vm_machdep.c 8.2 (Berkeley) 9/23/93
50 */
51
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: core_machdep.c,v 1.7 2019/11/20 19:37:53 pgoyette Exp $");
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/proc.h>
58 #include <sys/core.h>
59 #include <sys/buf.h>
60 #include <sys/exec.h>
61 #include <sys/vnode.h>
62 #include <sys/compat_stub.h>
63
64 #include <sys/exec_aout.h>
65
66 #include <uvm/uvm_extern.h>
67
68 #include <machine/cpu.h>
69 #include <machine/frame.h>
70 #include <machine/pcb.h>
71 #include <machine/trap.h>
72 #include <sys/bus.h>
73
74 /*
75 * cpu_coredump is called to write a core dump header.
76 */
77 int
cpu_coredump(struct lwp * l,struct coredump_iostate * iocookie,struct core * chdr)78 cpu_coredump(struct lwp *l, struct coredump_iostate *iocookie,
79 struct core *chdr)
80 {
81 int error;
82 struct md_coredump md_core;
83 struct coreseg cseg;
84
85 if (iocookie == NULL) {
86 CORE_SETMAGIC(*chdr, COREMAGIC, MID_MACHINE, 0);
87 chdr->c_hdrsize = ALIGN(sizeof(*chdr));
88 chdr->c_seghdrsize = ALIGN(sizeof(cseg));
89 chdr->c_cpusize = sizeof(md_core);
90 chdr->c_nseg++;
91 return 0;
92 }
93
94 /* Copy important fields over. */
95 md_core.md_tf.tf_tstate = l->l_md.md_tf->tf_tstate;
96 md_core.md_tf.tf_pc = l->l_md.md_tf->tf_pc;
97 md_core.md_tf.tf_npc = l->l_md.md_tf->tf_npc;
98 md_core.md_tf.tf_y = l->l_md.md_tf->tf_y;
99 md_core.md_tf.tf_tt = l->l_md.md_tf->tf_tt;
100 md_core.md_tf.tf_pil = l->l_md.md_tf->tf_pil;
101 md_core.md_tf.tf_oldpil = l->l_md.md_tf->tf_oldpil;
102
103 md_core.md_tf.tf_global[0] = l->l_md.md_tf->tf_global[0];
104 md_core.md_tf.tf_global[1] = l->l_md.md_tf->tf_global[1];
105 md_core.md_tf.tf_global[2] = l->l_md.md_tf->tf_global[2];
106 md_core.md_tf.tf_global[3] = l->l_md.md_tf->tf_global[3];
107 md_core.md_tf.tf_global[4] = l->l_md.md_tf->tf_global[4];
108 md_core.md_tf.tf_global[5] = l->l_md.md_tf->tf_global[5];
109 md_core.md_tf.tf_global[6] = l->l_md.md_tf->tf_global[6];
110 md_core.md_tf.tf_global[7] = l->l_md.md_tf->tf_global[7];
111
112 md_core.md_tf.tf_out[0] = l->l_md.md_tf->tf_out[0];
113 md_core.md_tf.tf_out[1] = l->l_md.md_tf->tf_out[1];
114 md_core.md_tf.tf_out[2] = l->l_md.md_tf->tf_out[2];
115 md_core.md_tf.tf_out[3] = l->l_md.md_tf->tf_out[3];
116 md_core.md_tf.tf_out[4] = l->l_md.md_tf->tf_out[4];
117 md_core.md_tf.tf_out[5] = l->l_md.md_tf->tf_out[5];
118 md_core.md_tf.tf_out[6] = l->l_md.md_tf->tf_out[6];
119 md_core.md_tf.tf_out[7] = l->l_md.md_tf->tf_out[7];
120
121 #ifdef DEBUG
122 md_core.md_tf.tf_local[0] = l->l_md.md_tf->tf_local[0];
123 md_core.md_tf.tf_local[1] = l->l_md.md_tf->tf_local[1];
124 md_core.md_tf.tf_local[2] = l->l_md.md_tf->tf_local[2];
125 md_core.md_tf.tf_local[3] = l->l_md.md_tf->tf_local[3];
126 md_core.md_tf.tf_local[4] = l->l_md.md_tf->tf_local[4];
127 md_core.md_tf.tf_local[5] = l->l_md.md_tf->tf_local[5];
128 md_core.md_tf.tf_local[6] = l->l_md.md_tf->tf_local[6];
129 md_core.md_tf.tf_local[7] = l->l_md.md_tf->tf_local[7];
130
131 md_core.md_tf.tf_in[0] = l->l_md.md_tf->tf_in[0];
132 md_core.md_tf.tf_in[1] = l->l_md.md_tf->tf_in[1];
133 md_core.md_tf.tf_in[2] = l->l_md.md_tf->tf_in[2];
134 md_core.md_tf.tf_in[3] = l->l_md.md_tf->tf_in[3];
135 md_core.md_tf.tf_in[4] = l->l_md.md_tf->tf_in[4];
136 md_core.md_tf.tf_in[5] = l->l_md.md_tf->tf_in[5];
137 md_core.md_tf.tf_in[6] = l->l_md.md_tf->tf_in[6];
138 md_core.md_tf.tf_in[7] = l->l_md.md_tf->tf_in[7];
139 #endif
140 if (l->l_md.md_fpstate) {
141 fpusave_lwp(l, true);
142 md_core.md_fpstate = *l->l_md.md_fpstate;
143 } else
144 memset(&md_core.md_fpstate, 0,
145 sizeof(md_core.md_fpstate));
146
147 CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_MACHINE, CORE_CPU);
148 cseg.c_addr = 0;
149 cseg.c_size = chdr->c_cpusize;
150
151 MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE, &cseg,
152 chdr->c_seghdrsize), ENOSYS, error);
153 if (error)
154 return error;
155
156 MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE, &md_core,
157 sizeof(md_core)), ENOSYS, error);
158
159 return error;
160 }
161