netbsd32_core.c revision 1.2
11.2Sthorpej/*	$NetBSD: netbsd32_core.c,v 1.2 2001/12/09 23:08:34 thorpej Exp $	*/
21.1Sthorpej
31.1Sthorpej/*
41.1Sthorpej * Copyright (c) 1997 Charles D. Cranor and Washington University.
51.1Sthorpej * Copyright (c) 1991, 1993 The Regents of the University of California.
61.1Sthorpej * Copyright (c) 1988 University of Utah.
71.1Sthorpej *
81.1Sthorpej * All rights reserved.
91.1Sthorpej *
101.1Sthorpej * This code is derived from software contributed to Berkeley by
111.1Sthorpej * the Systems Programming Group of the University of Utah Computer
121.1Sthorpej * Science Department.
131.1Sthorpej *
141.1Sthorpej * Redistribution and use in source and binary forms, with or without
151.1Sthorpej * modification, are permitted provided that the following conditions
161.1Sthorpej * are met:
171.1Sthorpej * 1. Redistributions of source code must retain the above copyright
181.1Sthorpej *    notice, this list of conditions and the following disclaimer.
191.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
201.1Sthorpej *    notice, this list of conditions and the following disclaimer in the
211.1Sthorpej *    documentation and/or other materials provided with the distribution.
221.1Sthorpej * 3. All advertising materials mentioning features or use of this software
231.1Sthorpej *    must display the following acknowledgement:
241.1Sthorpej *      This product includes software developed by Charles D. Cranor,
251.1Sthorpej *	Washington University, the University of California, Berkeley and
261.1Sthorpej *	its contributors.
271.1Sthorpej * 4. Neither the name of the University nor the names of its contributors
281.1Sthorpej *    may be used to endorse or promote products derived from this software
291.1Sthorpej *    without specific prior written permission.
301.1Sthorpej *
311.1Sthorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
321.1Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
331.1Sthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
341.1Sthorpej * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
351.1Sthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
361.1Sthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
371.1Sthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
381.1Sthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
391.1Sthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
401.1Sthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
411.1Sthorpej * SUCH DAMAGE.
421.1Sthorpej *
431.1Sthorpej * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
441.1Sthorpej *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
451.1Sthorpej * from: NetBSD: uvm_unix.c,v 1.25 2001/11/10 07:37:01 lukem Exp
461.1Sthorpej */
471.1Sthorpej
481.1Sthorpej/*
491.1Sthorpej * netbsd32_core.c: Support for the historic NetBSD core file format,
501.1Sthorpej * 32-bit version.
511.1Sthorpej */
521.1Sthorpej
531.1Sthorpej#include <sys/cdefs.h>
541.2Sthorpej__KERNEL_RCSID(0, "$NetBSD: netbsd32_core.c,v 1.2 2001/12/09 23:08:34 thorpej Exp $");
551.1Sthorpej
561.1Sthorpej#include <sys/param.h>
571.1Sthorpej#include <sys/systm.h>
581.1Sthorpej#include <sys/proc.h>
591.1Sthorpej#include <sys/vnode.h>
601.1Sthorpej#include <sys/core.h>
611.1Sthorpej
621.1Sthorpej#include <uvm/uvm.h>
631.1Sthorpej
641.2Sthorpej#include "netbsd32.h"
651.2Sthorpej
661.1Sthorpejint
671.1Sthorpejcoredump_netbsd32(struct proc *p, struct vnode *vp, struct ucred *cred)
681.1Sthorpej{
691.1Sthorpej	struct core32 core;
701.1Sthorpej	struct coreseg32 cseg;
711.1Sthorpej	struct vmspace *vm = p->p_vmspace;
721.1Sthorpej	struct vm_map *map = &vm->vm_map;
731.1Sthorpej	struct vm_map_entry *entry;
741.1Sthorpej	vaddr_t start, end, maxstack;
751.1Sthorpej	off_t offset;
761.1Sthorpej	int flag, error;
771.1Sthorpej
781.1Sthorpej	core.c_midmag = 0;
791.1Sthorpej	strncpy(core.c_name, p->p_comm, MAXCOMLEN);
801.1Sthorpej	core.c_nseg = 0;
811.1Sthorpej	core.c_signo = p->p_sigctx.ps_sig;
821.1Sthorpej	core.c_ucode = p->p_sigctx.ps_code;
831.1Sthorpej	core.c_cpusize = 0;
841.1Sthorpej	core.c_tsize = (u_long)ctob(vm->vm_tsize);
851.1Sthorpej	core.c_dsize = (u_long)ctob(vm->vm_dsize);
861.1Sthorpej	core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
871.2Sthorpej	error = cpu_coredump32(p, vp, cred, &core);
881.1Sthorpej	if (error)
891.1Sthorpej		return (error);
901.1Sthorpej
911.1Sthorpej#if 0
921.1Sthorpej	/*
931.1Sthorpej	 * XXX
941.1Sthorpej	 * It would be nice if we at least dumped the signal state (and made it
951.1Sthorpej	 * available at run time to the debugger, as well), but this code
961.1Sthorpej	 * hasn't actually had any effect for a long time, since we don't dump
971.1Sthorpej	 * the user area.  For now, it's dead.
981.1Sthorpej	 */
991.1Sthorpej#endif
1001.1Sthorpej
1011.1Sthorpej	offset = core.c_hdrsize + core.c_seghdrsize + core.c_cpusize;
1021.1Sthorpej	maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
1031.1Sthorpej
1041.1Sthorpej	for (entry = map->header.next; entry != &map->header;
1051.1Sthorpej	     entry = entry->next) {
1061.1Sthorpej		/* Should never happen for a user process. */
1071.1Sthorpej		if (UVM_ET_ISSUBMAP(entry))
1081.1Sthorpej			panic("coredump_netbsd: user process with submap?");
1091.1Sthorpej
1101.1Sthorpej		if ((entry->protection & VM_PROT_WRITE) == 0)
1111.1Sthorpej			continue;
1121.1Sthorpej
1131.1Sthorpej		start = entry->start;
1141.1Sthorpej		end = entry->end;
1151.1Sthorpej
1161.1Sthorpej		if (start >= VM_MAXUSER_ADDRESS)
1171.1Sthorpej			continue;
1181.1Sthorpej
1191.1Sthorpej		if (end > VM_MAXUSER_ADDRESS)
1201.1Sthorpej			end = VM_MAXUSER_ADDRESS;
1211.1Sthorpej
1221.1Sthorpej		if (start >= (vaddr_t)vm->vm_maxsaddr) {
1231.1Sthorpej			if (end <= maxstack)
1241.1Sthorpej				continue;
1251.1Sthorpej			if (start < maxstack)
1261.1Sthorpej				start = maxstack;
1271.1Sthorpej			flag = CORE_STACK;
1281.1Sthorpej		} else
1291.1Sthorpej			flag = CORE_DATA;
1301.1Sthorpej
1311.1Sthorpej		/*
1321.1Sthorpej		 * Set up a new core file segment.
1331.1Sthorpej		 */
1341.1Sthorpej		CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(core), flag);
1351.1Sthorpej		cseg.c_addr = start;
1361.1Sthorpej		cseg.c_size = end - start;
1371.1Sthorpej
1381.1Sthorpej		error = vn_rdwr(UIO_WRITE, vp,
1391.1Sthorpej		    (caddr_t)&cseg, core.c_seghdrsize,
1401.1Sthorpej		    offset, UIO_SYSSPACE,
1411.1Sthorpej		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1421.1Sthorpej		if (error)
1431.1Sthorpej			return (error);
1441.1Sthorpej
1451.1Sthorpej		offset += core.c_seghdrsize;
1461.1Sthorpej		error = vn_rdwr(UIO_WRITE, vp,
1471.2Sthorpej		    (caddr_t)(long)cseg.c_addr, (int)cseg.c_size,
1481.1Sthorpej		    offset, UIO_USERSPACE,
1491.1Sthorpej		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1501.1Sthorpej		if (error)
1511.1Sthorpej			return (error);
1521.1Sthorpej
1531.1Sthorpej		offset += cseg.c_size;
1541.1Sthorpej		core.c_nseg++;
1551.1Sthorpej	}
1561.1Sthorpej
1571.1Sthorpej	/* Now write out the core header. */
1581.1Sthorpej	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
1591.1Sthorpej	    (int)core.c_hdrsize, (off_t)0,
1601.1Sthorpej	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1611.1Sthorpej
1621.1Sthorpej	return (error);
1631.1Sthorpej}
164