Home | History | Annotate | Line # | Download | only in uvm
uvm_unix.c revision 1.46
      1 /*	$NetBSD: uvm_unix.c,v 1.46 2016/04/07 03:31:12 christos 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.46 2016/04/07 03:31:12 christos 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 nbreak, obreak;
     79 	int error;
     80 
     81 	mutex_enter(&p->p_auxlock);
     82 	obreak = (vaddr_t)vm->vm_daddr;
     83 	nbreak = round_page((vaddr_t)SCARG(uap, nsize));
     84 	if (nbreak == 0
     85 	    || ((nbreak - obreak) > p->p_rlimit[RLIMIT_DATA].rlim_cur
     86 		&& nbreak > obreak)) {
     87 		mutex_exit(&p->p_auxlock);
     88 		return (ENOMEM);
     89 	}
     90 
     91 	obreak = round_page(obreak + ptoa(vm->vm_dsize));
     92 
     93 	if (nbreak == obreak) {
     94 		mutex_exit(&p->p_auxlock);
     95 		return (0);
     96 	}
     97 
     98 	/*
     99 	 * grow or shrink?
    100 	 */
    101 
    102 	if (nbreak > obreak) {
    103 		vm_prot_t prot = UVM_PROT_READ | UVM_PROT_WRITE;
    104 		vm_prot_t maxprot = UVM_PROT_ALL;
    105 
    106 		PAX_MPROTECT_ADJUST(l, &prot, &maxprot);
    107 
    108 		error = uvm_map(&vm->vm_map, &obreak, nbreak - obreak, NULL,
    109 		    UVM_UNKNOWN_OFFSET, 0,
    110 		    UVM_MAPFLAG(prot, maxprot,
    111 				UVM_INH_COPY,
    112 				UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
    113 				UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
    114 		if (error) {
    115 #ifdef DEBUG
    116 			uprintf("sbrk: grow %#"PRIxVADDR" failed, error = %d\n",
    117 			    nbreak - obreak, error);
    118 #endif
    119 			mutex_exit(&p->p_auxlock);
    120 			return (error);
    121 		}
    122 		vm->vm_dsize += atop(nbreak - obreak);
    123 	} else {
    124 		uvm_deallocate(&vm->vm_map, nbreak, obreak - nbreak);
    125 		vm->vm_dsize -= atop(obreak - nbreak);
    126 	}
    127 	mutex_exit(&p->p_auxlock);
    128 
    129 	return (0);
    130 }
    131 
    132 /*
    133  * uvm_grow: enlarge the "stack segment" to include sp.
    134  */
    135 
    136 int
    137 uvm_grow(struct proc *p, vaddr_t sp)
    138 {
    139 	struct vmspace *vm = p->p_vmspace;
    140 	vsize_t nss;
    141 
    142 	/*
    143 	 * For user defined stacks (from sendsig).
    144 	 */
    145 #ifdef __MACHINE_STACK_GROWS_UP
    146 	if (sp < (vaddr_t)vm->vm_minsaddr)
    147 #else
    148 	if (sp < (vaddr_t)vm->vm_maxsaddr)
    149 #endif
    150 		return (0);
    151 
    152 	/*
    153 	 * For common case of already allocated (from trap).
    154 	 */
    155 #ifdef __MACHINE_STACK_GROWS_UP
    156 	if (sp < USRSTACK + ctob(vm->vm_ssize))
    157 #else
    158 	if (sp >= USRSTACK - ctob(vm->vm_ssize))
    159 #endif
    160 		return (1);
    161 
    162 	/*
    163 	 * Really need to check vs limit and increment stack size if ok.
    164 	 */
    165 #ifdef __MACHINE_STACK_GROWS_UP
    166 	nss = btoc(sp - USRSTACK);
    167 #else
    168 	nss = btoc(USRSTACK - sp);
    169 #endif
    170 	if (nss > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur))
    171 		return (0);
    172 	vm->vm_ssize = nss;
    173 	return (1);
    174 }
    175 
    176 /*
    177  * sys_oadvise: old advice system call
    178  */
    179 
    180 /* ARGSUSED */
    181 int
    182 sys_ovadvise(struct lwp *l, const struct sys_ovadvise_args *uap, register_t *retval)
    183 {
    184 #if 0
    185 	/* {
    186 		syscallarg(int) anom;
    187 	} */
    188 #endif
    189 
    190 	return (EINVAL);
    191 }
    192