xref: /netbsd-src/sys/arch/x86/x86/core_machdep.c (revision 1d577fe3790e662ec592913bd86905a501cfc612)
1 /*	$NetBSD: core_machdep.c,v 1.6 2019/11/20 19:37:53 pgoyette Exp $	*/
2 
3 /*-
4  * Copyright (c) 1982, 1986 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * the Systems Programming Group of the University of Utah Computer
9  * Science Department, and William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
36  */
37 
38 /*-
39  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
40  * Copyright (c) 1989, 1990 William Jolitz
41  * All rights reserved.
42  *
43  * This code is derived from software contributed to Berkeley by
44  * the Systems Programming Group of the University of Utah Computer
45  * Science Department, and William Jolitz.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. All advertising materials mentioning features or use of this software
56  *    must display the following acknowledgement:
57  *	This product includes software developed by the University of
58  *	California, Berkeley and its contributors.
59  * 4. Neither the name of the University nor the names of its contributors
60  *    may be used to endorse or promote products derived from this software
61  *    without specific prior written permission.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73  * SUCH DAMAGE.
74  *
75  *	@(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
76  */
77 
78 /*
79  *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
80  */
81 
82 #include <sys/cdefs.h>
83 __KERNEL_RCSID(0, "$NetBSD: core_machdep.c,v 1.6 2019/11/20 19:37:53 pgoyette Exp $");
84 
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/proc.h>
88 #include <sys/vnode.h>
89 #include <sys/buf.h>
90 #include <sys/core.h>
91 #include <sys/exec.h>
92 #include <sys/ptrace.h>
93 #include <sys/compat_stub.h>
94 
95 #include <sys/exec_aout.h>
96 
97 #include <uvm/uvm_extern.h>
98 
99 #include <machine/cpu.h>
100 #include <machine/gdt.h>
101 #include <machine/reg.h>
102 #include <machine/specialreg.h>
103 
104 /*
105  * Dump the machine specific segment at the start of a core dump.
106  */
107 
108 struct md_core {
109 	struct reg intreg;
110 	struct fpreg freg;
111 };
112 
113 int
cpu_coredump(struct lwp * l,struct coredump_iostate * iocookie,struct core * chdr)114 cpu_coredump(struct lwp *l, struct coredump_iostate *iocookie,
115     struct core *chdr)
116 {
117 	struct md_core md_core;
118 	struct coreseg cseg;
119 	size_t fp_size;
120 	int error;
121 
122 	if (iocookie == NULL) {
123 		CORE_SETMAGIC(*chdr, COREMAGIC, MID_MACHINE, 0);
124 		chdr->c_hdrsize = ALIGN(sizeof(*chdr));
125 		chdr->c_seghdrsize = ALIGN(sizeof(cseg));
126 		chdr->c_cpusize = sizeof(md_core);
127 		chdr->c_nseg++;
128 		return 0;
129 	}
130 
131 	/* Save integer registers. */
132 	error = process_read_regs(l, &md_core.intreg);
133 	if (error)
134 		return error;
135 
136 	/* Save floating point registers. */
137 	fp_size = sizeof md_core.freg;
138 	error = process_read_fpregs(l, &md_core.freg, &fp_size);
139 	if (error)
140 		return error;
141 
142 	CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_MACHINE, CORE_CPU);
143 	cseg.c_addr = 0;
144 	cseg.c_size = chdr->c_cpusize;
145 
146 	MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE, &cseg,
147 	    chdr->c_seghdrsize), ENOSYS, error);
148 	if (error)
149 		return error;
150 
151 	MODULE_HOOK_CALL(coredump_write_hook, (iocookie, UIO_SYSSPACE,
152 	    &md_core, sizeof(md_core)), ENOSYS, error);
153 
154 	return error;
155 }
156