Home | History | Annotate | Line # | Download | only in uvm
uvm_unix.c revision 1.43.4.1
      1 /*	$NetBSD: uvm_unix.c,v 1.43.4.1 2011/03/05 20:56:38 rmind 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. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
     39  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
     40  * from: Id: uvm_unix.c,v 1.1.2.2 1997/08/25 18:52:30 chuck Exp
     41  */
     42 
     43 /*
     44  * uvm_unix.c: traditional sbrk/grow interface to vm.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: uvm_unix.c,v 1.43.4.1 2011/03/05 20:56:38 rmind Exp $");
     49 
     50 #include "opt_pax.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/proc.h>
     55 #include <sys/resourcevar.h>
     56 
     57 #include <sys/mount.h>
     58 #include <sys/syscallargs.h>
     59 
     60 #ifdef PAX_MPROTECT
     61 #include <sys/pax.h>
     62 #endif /* PAX_MPROTECT */
     63 
     64 #include <uvm/uvm.h>
     65 
     66 /*
     67  * sys_obreak: set break
     68  */
     69 
     70 int
     71 sys_obreak(struct lwp *l, const struct sys_obreak_args *uap, register_t *retval)
     72 {
     73 	/* {
     74 		syscallarg(char *) nsize;
     75 	} */
     76 	struct proc *p = l->l_proc;
     77 	struct vmspace *vm = p->p_vmspace;
     78 	vaddr_t new, old;
     79 	int error;
     80 
     81 	mutex_enter(&p->p_auxlock);
     82 	old = (vaddr_t)vm->vm_daddr;
     83 	new = round_page((vaddr_t)SCARG(uap, nsize));
     84 	if (new == 0 ||
     85 	    ((new - old) > p->p_rlimit[RLIMIT_DATA].rlim_cur && new > old)) {
     86 		mutex_exit(&p->p_auxlock);
     87 		return (ENOMEM);
     88 	}
     89 
     90 	old = round_page(old + ptoa(vm->vm_dsize));
     91 
     92 	if (new == old) {
     93 		mutex_exit(&p->p_auxlock);
     94 		return (0);
     95 	}
     96 
     97 	/*
     98 	 * grow or shrink?
     99 	 */
    100 
    101 	if (new > old) {
    102 		vm_prot_t prot = UVM_PROT_READ | UVM_PROT_WRITE;
    103 		vm_prot_t maxprot = UVM_PROT_ALL;
    104 
    105 #ifdef PAX_MPROTECT
    106 		pax_mprotect(l, &prot, &maxprot);
    107 #endif /* PAX_MPROTECT */
    108 
    109 		error = uvm_map(&vm->vm_map, &old, new - old, NULL,
    110 		    UVM_UNKNOWN_OFFSET, 0,
    111 		    UVM_MAPFLAG(prot, maxprot,
    112 				UVM_INH_COPY,
    113 				UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
    114 				UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
    115 		if (error) {
    116 #ifdef DEBUG
    117 			uprintf("sbrk: grow %#"PRIxVADDR" failed, error = %d\n",
    118 			    new - old, error);
    119 #endif
    120 			mutex_exit(&p->p_auxlock);
    121 			return (error);
    122 		}
    123 		vm->vm_dsize += atop(new - old);
    124 	} else {
    125 		uvm_deallocate(&vm->vm_map, new, old - new);
    126 		vm->vm_dsize -= atop(old - new);
    127 	}
    128 	mutex_exit(&p->p_auxlock);
    129 
    130 	return (0);
    131 }
    132 
    133 /*
    134  * uvm_grow: enlarge the "stack segment" to include sp.
    135  */
    136 
    137 int
    138 uvm_grow(struct proc *p, vaddr_t sp)
    139 {
    140 	struct vmspace *vm = p->p_vmspace;
    141 	vsize_t nss;
    142 
    143 	/*
    144 	 * For user defined stacks (from sendsig).
    145 	 */
    146 #ifdef __MACHINE_STACK_GROWS_UP
    147 	if (sp < (vaddr_t)vm->vm_minsaddr)
    148 #else
    149 	if (sp < (vaddr_t)vm->vm_maxsaddr)
    150 #endif
    151 		return (0);
    152 
    153 	/*
    154 	 * For common case of already allocated (from trap).
    155 	 */
    156 #ifdef __MACHINE_STACK_GROWS_UP
    157 	if (sp < USRSTACK + ctob(vm->vm_ssize))
    158 #else
    159 	if (sp >= USRSTACK - ctob(vm->vm_ssize))
    160 #endif
    161 		return (1);
    162 
    163 	/*
    164 	 * Really need to check vs limit and increment stack size if ok.
    165 	 */
    166 #ifdef __MACHINE_STACK_GROWS_UP
    167 	nss = btoc(sp - USRSTACK);
    168 #else
    169 	nss = btoc(USRSTACK - sp);
    170 #endif
    171 	if (nss > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur))
    172 		return (0);
    173 	vm->vm_ssize = nss;
    174 	return (1);
    175 }
    176 
    177 /*
    178  * sys_oadvise: old advice system call
    179  */
    180 
    181 /* ARGSUSED */
    182 int
    183 sys_ovadvise(struct lwp *l, const struct sys_ovadvise_args *uap, register_t *retval)
    184 {
    185 #if 0
    186 	/* {
    187 		syscallarg(int) anom;
    188 	} */
    189 #endif
    190 
    191 	return (EINVAL);
    192 }
    193