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