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