xref: /netbsd-src/sys/compat/netbsd32/netbsd32_core.c (revision d0e51ba7daefd002e9816e64c8d8de29ab2eb15d)
1 /*	$NetBSD: netbsd32_core.c,v 1.2 2001/12/09 23:08:34 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5  * Copyright (c) 1991, 1993 The Regents of the University of California.
6  * Copyright (c) 1988 University of Utah.
7  *
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * the Systems Programming Group of the University of Utah Computer
12  * Science Department.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *      This product includes software developed by Charles D. Cranor,
25  *	Washington University, the University of California, Berkeley and
26  *	its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
44  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
45  * from: NetBSD: uvm_unix.c,v 1.25 2001/11/10 07:37:01 lukem Exp
46  */
47 
48 /*
49  * netbsd32_core.c: Support for the historic NetBSD core file format,
50  * 32-bit version.
51  */
52 
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: netbsd32_core.c,v 1.2 2001/12/09 23:08:34 thorpej Exp $");
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/proc.h>
59 #include <sys/vnode.h>
60 #include <sys/core.h>
61 
62 #include <uvm/uvm.h>
63 
64 #include "netbsd32.h"
65 
66 int
67 coredump_netbsd32(struct proc *p, struct vnode *vp, struct ucred *cred)
68 {
69 	struct core32 core;
70 	struct coreseg32 cseg;
71 	struct vmspace *vm = p->p_vmspace;
72 	struct vm_map *map = &vm->vm_map;
73 	struct vm_map_entry *entry;
74 	vaddr_t start, end, maxstack;
75 	off_t offset;
76 	int flag, error;
77 
78 	core.c_midmag = 0;
79 	strncpy(core.c_name, p->p_comm, MAXCOMLEN);
80 	core.c_nseg = 0;
81 	core.c_signo = p->p_sigctx.ps_sig;
82 	core.c_ucode = p->p_sigctx.ps_code;
83 	core.c_cpusize = 0;
84 	core.c_tsize = (u_long)ctob(vm->vm_tsize);
85 	core.c_dsize = (u_long)ctob(vm->vm_dsize);
86 	core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
87 	error = cpu_coredump32(p, vp, cred, &core);
88 	if (error)
89 		return (error);
90 
91 #if 0
92 	/*
93 	 * XXX
94 	 * It would be nice if we at least dumped the signal state (and made it
95 	 * available at run time to the debugger, as well), but this code
96 	 * hasn't actually had any effect for a long time, since we don't dump
97 	 * the user area.  For now, it's dead.
98 	 */
99 #endif
100 
101 	offset = core.c_hdrsize + core.c_seghdrsize + core.c_cpusize;
102 	maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
103 
104 	for (entry = map->header.next; entry != &map->header;
105 	     entry = entry->next) {
106 		/* Should never happen for a user process. */
107 		if (UVM_ET_ISSUBMAP(entry))
108 			panic("coredump_netbsd: user process with submap?");
109 
110 		if ((entry->protection & VM_PROT_WRITE) == 0)
111 			continue;
112 
113 		start = entry->start;
114 		end = entry->end;
115 
116 		if (start >= VM_MAXUSER_ADDRESS)
117 			continue;
118 
119 		if (end > VM_MAXUSER_ADDRESS)
120 			end = VM_MAXUSER_ADDRESS;
121 
122 		if (start >= (vaddr_t)vm->vm_maxsaddr) {
123 			if (end <= maxstack)
124 				continue;
125 			if (start < maxstack)
126 				start = maxstack;
127 			flag = CORE_STACK;
128 		} else
129 			flag = CORE_DATA;
130 
131 		/*
132 		 * Set up a new core file segment.
133 		 */
134 		CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(core), flag);
135 		cseg.c_addr = start;
136 		cseg.c_size = end - start;
137 
138 		error = vn_rdwr(UIO_WRITE, vp,
139 		    (caddr_t)&cseg, core.c_seghdrsize,
140 		    offset, UIO_SYSSPACE,
141 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
142 		if (error)
143 			return (error);
144 
145 		offset += core.c_seghdrsize;
146 		error = vn_rdwr(UIO_WRITE, vp,
147 		    (caddr_t)(long)cseg.c_addr, (int)cseg.c_size,
148 		    offset, UIO_USERSPACE,
149 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
150 		if (error)
151 			return (error);
152 
153 		offset += cseg.c_size;
154 		core.c_nseg++;
155 	}
156 
157 	/* Now write out the core header. */
158 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
159 	    (int)core.c_hdrsize, (off_t)0,
160 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
161 
162 	return (error);
163 }
164