Home | History | Annotate | Line # | Download | only in uvm
uvm_unix.c revision 1.14
      1 /*	$NetBSD: uvm_unix.c,v 1.14 2000/07/02 17:40:08 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: Id: uvm_unix.c,v 1.1.2.2 1997/08/25 18:52:30 chuck Exp
     46  */
     47 
     48 /*
     49  * uvm_unix.c: traditional sbrk/grow interface to vm.
     50  */
     51 #include "opt_compat_netbsd32.h"
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/proc.h>
     56 #include <sys/resourcevar.h>
     57 #include <sys/vnode.h>
     58 #include <sys/core.h>
     59 
     60 #include <sys/mount.h>
     61 #include <sys/syscallargs.h>
     62 
     63 #include <uvm/uvm.h>
     64 
     65 /*
     66  * sys_obreak: set break
     67  */
     68 
     69 int
     70 sys_obreak(p, v, retval)
     71 	struct proc *p;
     72 	void *v;
     73 	register_t *retval;
     74 {
     75 	struct sys_obreak_args /* {
     76 		syscallarg(char *) nsize;
     77 	} */ *uap = v;
     78 	struct vmspace *vm = p->p_vmspace;
     79 	vaddr_t new, old;
     80 	ssize_t diff;
     81 	int rv;
     82 
     83 	old = (vaddr_t)vm->vm_daddr;
     84 	new = round_page((vaddr_t)SCARG(uap, nsize));
     85 	if ((new - old) > p->p_rlimit[RLIMIT_DATA].rlim_cur)
     86 		return (ENOMEM);
     87 
     88 	old = round_page(old + ptoa(vm->vm_dsize));
     89 	diff = new - old;
     90 
     91 	if (diff == 0)
     92 		return (0);
     93 
     94 	/*
     95 	 * grow or shrink?
     96 	 */
     97 	if (diff > 0) {
     98 		rv = uvm_map(&vm->vm_map, &old, diff, NULL, UVM_UNKNOWN_OFFSET,
     99 		    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    100 		    UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
    101 		    UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
    102 		if (rv == KERN_SUCCESS) {
    103 			vm->vm_dsize += atop(diff);
    104 			return (0);
    105 		}
    106 	} else {
    107 		rv = uvm_deallocate(&vm->vm_map, new, -diff);
    108 		if (rv == KERN_SUCCESS) {
    109 			vm->vm_dsize -= atop(-diff);
    110 			return (0);
    111 		}
    112 	}
    113 
    114 	uprintf("sbrk: %s %ld failed, return = %d\n",
    115 	    diff > 0 ? "grow" : "shrink",
    116 	    (long)(diff > 0 ? diff : -diff), rv);
    117 	return (ENOMEM);
    118 }
    119 
    120 /*
    121  * uvm_grow: enlarge the "stack segment" to include sp.
    122  */
    123 
    124 int
    125 uvm_grow(p, sp)
    126 	struct proc *p;
    127 	vaddr_t sp;
    128 {
    129 	struct vmspace *vm = p->p_vmspace;
    130 	int si;
    131 
    132 	/*
    133 	 * For user defined stacks (from sendsig).
    134 	 */
    135 	if (sp < (vaddr_t)vm->vm_maxsaddr)
    136 		return (0);
    137 
    138 	/*
    139 	 * For common case of already allocated (from trap).
    140 	 */
    141 	if (sp >= USRSTACK - ctob(vm->vm_ssize))
    142 		return (1);
    143 
    144 	/*
    145 	 * Really need to check vs limit and increment stack size if ok.
    146 	 */
    147 	si = btoc(USRSTACK-sp) - vm->vm_ssize;
    148 	if (vm->vm_ssize + si > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur))
    149 		return (0);
    150 	vm->vm_ssize += si;
    151 	return (1);
    152 }
    153 
    154 /*
    155  * sys_oadvise: old advice system call
    156  */
    157 
    158 /* ARGSUSED */
    159 int
    160 sys_ovadvise(p, v, retval)
    161 	struct proc *p;
    162 	void *v;
    163 	register_t *retval;
    164 {
    165 #if 0
    166 	struct sys_ovadvise_args /* {
    167 		syscallarg(int) anom;
    168 	} */ *uap = v;
    169 #endif
    170 
    171 	return (EINVAL);
    172 }
    173 
    174 /*
    175  * uvm_coredump: dump core!
    176  */
    177 
    178 int
    179 uvm_coredump(p, vp, cred, chdr)
    180 	struct proc *p;
    181 	struct vnode *vp;
    182 	struct ucred *cred;
    183 	struct core *chdr;
    184 {
    185 	struct vmspace *vm = p->p_vmspace;
    186 	vm_map_t map = &vm->vm_map;
    187 	vm_map_entry_t entry;
    188 	vaddr_t start, end;
    189 	struct coreseg cseg;
    190 	off_t offset;
    191 	int flag, error = 0;
    192 
    193 	offset = chdr->c_hdrsize + chdr->c_seghdrsize + chdr->c_cpusize;
    194 
    195 	for (entry = map->header.next; entry != &map->header;
    196 	    entry = entry->next) {
    197 
    198 		/* should never happen for a user process */
    199 		if (UVM_ET_ISSUBMAP(entry)) {
    200 			panic("uvm_coredump: user process with submap?");
    201 		}
    202 
    203 		if (!(entry->protection & VM_PROT_WRITE))
    204 			continue;
    205 
    206 		start = entry->start;
    207 		end = entry->end;
    208 
    209 		if (start >= VM_MAXUSER_ADDRESS)
    210 			continue;
    211 
    212 		if (end > VM_MAXUSER_ADDRESS)
    213 			end = VM_MAXUSER_ADDRESS;
    214 
    215 		if (start >= (vaddr_t)vm->vm_maxsaddr) {
    216 			flag = CORE_STACK;
    217 			start = trunc_page(USRSTACK - ctob(vm->vm_ssize));
    218 			if (start >= end)
    219 				continue;
    220 		} else
    221 			flag = CORE_DATA;
    222 
    223 		/*
    224 		 * Set up a new core file segment.
    225 		 */
    226 		CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(*chdr), flag);
    227 		cseg.c_addr = start;
    228 		cseg.c_size = end - start;
    229 
    230 		error = vn_rdwr(UIO_WRITE, vp,
    231 		    (caddr_t)&cseg, chdr->c_seghdrsize,
    232 		    offset, UIO_SYSSPACE,
    233 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    234 		if (error)
    235 			break;
    236 
    237 		offset += chdr->c_seghdrsize;
    238 		error = vn_rdwr(UIO_WRITE, vp,
    239 		    (caddr_t)cseg.c_addr, (int)cseg.c_size,
    240 		    offset, UIO_USERSPACE,
    241 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    242 		if (error)
    243 			break;
    244 
    245 		offset += cseg.c_size;
    246 		chdr->c_nseg++;
    247 	}
    248 
    249 	return (error);
    250 }
    251 
    252 #if COMPAT_NETBSD32
    253 /*
    254  * uvm_coredump32: dump 32-bit core!
    255  */
    256 
    257 int
    258 uvm_coredump32(p, vp, cred, chdr)
    259 	struct proc *p;
    260 	struct vnode *vp;
    261 	struct ucred *cred;
    262 	struct core32 *chdr;
    263 {
    264 	struct vmspace *vm = p->p_vmspace;
    265 	vm_map_t map = &vm->vm_map;
    266 	vm_map_entry_t entry;
    267 	vaddr_t start, end;
    268 	struct coreseg32 cseg;
    269 	off_t offset;
    270 	int flag, error = 0;
    271 
    272 	offset = chdr->c_hdrsize + chdr->c_seghdrsize + chdr->c_cpusize;
    273 
    274 	for (entry = map->header.next; entry != &map->header;
    275 	    entry = entry->next) {
    276 
    277 		/* should never happen for a user process */
    278 		if (UVM_ET_ISSUBMAP(entry)) {
    279 			panic("uvm_coredump: user process with submap?");
    280 		}
    281 
    282 		if (!(entry->protection & VM_PROT_WRITE))
    283 			continue;
    284 
    285 		start = entry->start;
    286 		end = entry->end;
    287 
    288 		if (start >= VM_MAXUSER_ADDRESS)
    289 			continue;
    290 
    291 		if (end > VM_MAXUSER_ADDRESS)
    292 			end = VM_MAXUSER_ADDRESS;
    293 
    294 		if (start >= (vaddr_t)vm->vm_maxsaddr) {
    295 			flag = CORE_STACK;
    296 			start = trunc_page(USRSTACK - ctob(vm->vm_ssize));
    297 			if (start >= end)
    298 				continue;
    299 		} else
    300 			flag = CORE_DATA;
    301 
    302 		/*
    303 		 * Set up a new core file segment.
    304 		 */
    305 		CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(*chdr), flag);
    306 		cseg.c_addr = start;
    307 		cseg.c_size = end - start;
    308 
    309 		error = vn_rdwr(UIO_WRITE, vp,
    310 		    (caddr_t)&cseg, chdr->c_seghdrsize,
    311 		    offset, UIO_SYSSPACE,
    312 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    313 		if (error)
    314 			break;
    315 
    316 		offset += chdr->c_seghdrsize;
    317 		error = vn_rdwr(UIO_WRITE, vp,
    318 		    (caddr_t)cseg.c_addr, (int)cseg.c_size,
    319 		    offset, UIO_USERSPACE,
    320 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
    321 		if (error)
    322 			break;
    323 
    324 		offset += cseg.c_size;
    325 		chdr->c_nseg++;
    326 	}
    327 
    328 	return (error);
    329 }
    330 
    331 #endif
    332