Home | History | Annotate | Line # | Download | only in uvm
uvm_unix.c revision 1.31.2.4
      1  1.31.2.4      yamt /*	$NetBSD: uvm_unix.c,v 1.31.2.4 2008/01/21 09:48:26 yamt Exp $	*/
      2       1.1       mrg 
      3       1.1       mrg /*
      4       1.1       mrg  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5      1.22       chs  * Copyright (c) 1991, 1993 The Regents of the University of California.
      6       1.1       mrg  * Copyright (c) 1988 University of Utah.
      7       1.1       mrg  *
      8       1.1       mrg  * All rights reserved.
      9       1.1       mrg  *
     10       1.1       mrg  * This code is derived from software contributed to Berkeley by
     11       1.1       mrg  * the Systems Programming Group of the University of Utah Computer
     12       1.1       mrg  * Science Department.
     13       1.1       mrg  *
     14       1.1       mrg  * Redistribution and use in source and binary forms, with or without
     15       1.1       mrg  * modification, are permitted provided that the following conditions
     16       1.1       mrg  * are met:
     17       1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     18       1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     19       1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     20       1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     21       1.1       mrg  *    documentation and/or other materials provided with the distribution.
     22       1.1       mrg  * 3. All advertising materials mentioning features or use of this software
     23       1.1       mrg  *    must display the following acknowledgement:
     24       1.1       mrg  *      This product includes software developed by Charles D. Cranor,
     25      1.22       chs  *	Washington University, the University of California, Berkeley and
     26       1.1       mrg  *	its contributors.
     27       1.1       mrg  * 4. Neither the name of the University nor the names of its contributors
     28       1.1       mrg  *    may be used to endorse or promote products derived from this software
     29       1.1       mrg  *    without specific prior written permission.
     30       1.1       mrg  *
     31       1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     32       1.1       mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     33       1.1       mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     34       1.1       mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     35       1.1       mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36       1.1       mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37       1.1       mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38       1.1       mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     39       1.1       mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     40       1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     41       1.1       mrg  * SUCH DAMAGE.
     42       1.1       mrg  *
     43       1.1       mrg  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
     44       1.1       mrg  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
     45       1.3       mrg  * from: Id: uvm_unix.c,v 1.1.2.2 1997/08/25 18:52:30 chuck Exp
     46       1.1       mrg  */
     47       1.1       mrg 
     48       1.1       mrg /*
     49       1.1       mrg  * uvm_unix.c: traditional sbrk/grow interface to vm.
     50       1.1       mrg  */
     51      1.25     lukem 
     52      1.25     lukem #include <sys/cdefs.h>
     53  1.31.2.4      yamt __KERNEL_RCSID(0, "$NetBSD: uvm_unix.c,v 1.31.2.4 2008/01/21 09:48:26 yamt Exp $");
     54  1.31.2.1      yamt 
     55  1.31.2.1      yamt #include "opt_pax.h"
     56       1.1       mrg 
     57       1.1       mrg #include <sys/param.h>
     58       1.1       mrg #include <sys/systm.h>
     59       1.1       mrg #include <sys/proc.h>
     60       1.1       mrg #include <sys/resourcevar.h>
     61       1.1       mrg 
     62       1.1       mrg #include <sys/mount.h>
     63       1.1       mrg #include <sys/syscallargs.h>
     64       1.1       mrg 
     65  1.31.2.1      yamt #ifdef PAX_MPROTECT
     66  1.31.2.1      yamt #include <sys/pax.h>
     67  1.31.2.1      yamt #endif /* PAX_MPROTECT */
     68  1.31.2.1      yamt 
     69       1.1       mrg #include <uvm/uvm.h>
     70       1.1       mrg 
     71       1.1       mrg /*
     72       1.1       mrg  * sys_obreak: set break
     73       1.1       mrg  */
     74       1.1       mrg 
     75       1.4       mrg int
     76  1.31.2.4      yamt sys_obreak(struct lwp *l, const struct sys_obreak_args *uap, register_t *retval)
     77       1.1       mrg {
     78  1.31.2.4      yamt 	/* {
     79       1.4       mrg 		syscallarg(char *) nsize;
     80  1.31.2.4      yamt 	} */
     81      1.27   thorpej 	struct proc *p = l->l_proc;
     82      1.12  augustss 	struct vmspace *vm = p->p_vmspace;
     83       1.6       eeh 	vaddr_t new, old;
     84      1.19       chs 	int error;
     85       1.4       mrg 
     86  1.31.2.4      yamt 	mutex_enter(&p->p_auxlock);
     87       1.6       eeh 	old = (vaddr_t)vm->vm_daddr;
     88      1.11    kleink 	new = round_page((vaddr_t)SCARG(uap, nsize));
     89  1.31.2.4      yamt 	if ((new - old) > p->p_rlimit[RLIMIT_DATA].rlim_cur && new > old) {
     90  1.31.2.4      yamt 		mutex_exit(&p->p_auxlock);
     91      1.14   thorpej 		return (ENOMEM);
     92  1.31.2.4      yamt 	}
     93       1.4       mrg 
     94      1.14   thorpej 	old = round_page(old + ptoa(vm->vm_dsize));
     95       1.4       mrg 
     96  1.31.2.4      yamt 	if (new == old) {
     97  1.31.2.4      yamt 		mutex_exit(&p->p_auxlock);
     98      1.14   thorpej 		return (0);
     99  1.31.2.4      yamt 	}
    100      1.14   thorpej 
    101       1.4       mrg 	/*
    102       1.4       mrg 	 * grow or shrink?
    103       1.4       mrg 	 */
    104      1.29       chs 
    105      1.21      ross 	if (new > old) {
    106  1.31.2.1      yamt 		vm_prot_t prot = UVM_PROT_READ | UVM_PROT_WRITE;
    107  1.31.2.1      yamt 		vm_prot_t maxprot = UVM_PROT_ALL;
    108  1.31.2.1      yamt 
    109  1.31.2.1      yamt #ifdef PAX_MPROTECT
    110  1.31.2.1      yamt 		pax_mprotect(l, &prot, &maxprot);
    111  1.31.2.1      yamt #endif /* PAX_MPROTECT */
    112  1.31.2.1      yamt 
    113      1.21      ross 		error = uvm_map(&vm->vm_map, &old, new - old, NULL,
    114      1.19       chs 		    UVM_UNKNOWN_OFFSET, 0,
    115  1.31.2.1      yamt 		    UVM_MAPFLAG(prot, maxprot,
    116      1.29       chs 				UVM_INH_COPY,
    117      1.29       chs 				UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
    118      1.29       chs 				UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
    119      1.19       chs 		if (error) {
    120      1.19       chs 			uprintf("sbrk: grow %ld failed, error = %d\n",
    121      1.21      ross 				new - old, error);
    122  1.31.2.4      yamt 			mutex_exit(&p->p_auxlock);
    123      1.28    simonb 			return (error);
    124       1.4       mrg 		}
    125      1.21      ross 		vm->vm_dsize += atop(new - old);
    126      1.14   thorpej 	} else {
    127      1.21      ross 		uvm_deallocate(&vm->vm_map, new, old - new);
    128      1.21      ross 		vm->vm_dsize -= atop(old - new);
    129      1.14   thorpej 	}
    130  1.31.2.4      yamt 	mutex_exit(&p->p_auxlock);
    131  1.31.2.4      yamt 
    132      1.28    simonb 	return (0);
    133       1.1       mrg }
    134       1.1       mrg 
    135       1.1       mrg /*
    136       1.1       mrg  * uvm_grow: enlarge the "stack segment" to include sp.
    137       1.1       mrg  */
    138       1.1       mrg 
    139       1.4       mrg int
    140      1.31   thorpej uvm_grow(struct proc *p, vaddr_t sp)
    141       1.1       mrg {
    142      1.12  augustss 	struct vmspace *vm = p->p_vmspace;
    143      1.30  jdolecek 	vsize_t nss;
    144       1.1       mrg 
    145       1.4       mrg 	/*
    146       1.4       mrg 	 * For user defined stacks (from sendsig).
    147       1.4       mrg 	 */
    148  1.31.2.2      yamt #ifdef __MACHINE_STACK_GROWS_UP
    149  1.31.2.2      yamt 	if (sp < (vaddr_t)vm->vm_minsaddr)
    150  1.31.2.2      yamt #else
    151       1.6       eeh 	if (sp < (vaddr_t)vm->vm_maxsaddr)
    152  1.31.2.2      yamt #endif
    153       1.4       mrg 		return (0);
    154       1.4       mrg 
    155       1.4       mrg 	/*
    156       1.4       mrg 	 * For common case of already allocated (from trap).
    157       1.4       mrg 	 */
    158  1.31.2.2      yamt #ifdef __MACHINE_STACK_GROWS_UP
    159  1.31.2.2      yamt 	if (sp < USRSTACK + ctob(vm->vm_ssize))
    160  1.31.2.2      yamt #else
    161       1.4       mrg 	if (sp >= USRSTACK - ctob(vm->vm_ssize))
    162  1.31.2.2      yamt #endif
    163       1.4       mrg 		return (1);
    164       1.4       mrg 
    165       1.4       mrg 	/*
    166       1.4       mrg 	 * Really need to check vs limit and increment stack size if ok.
    167       1.4       mrg 	 */
    168  1.31.2.2      yamt #ifdef __MACHINE_STACK_GROWS_UP
    169  1.31.2.2      yamt 	nss = btoc(sp - USRSTACK);
    170  1.31.2.2      yamt #else
    171      1.30  jdolecek 	nss = btoc(USRSTACK - sp);
    172  1.31.2.2      yamt #endif
    173      1.30  jdolecek 	if (nss > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur))
    174       1.4       mrg 		return (0);
    175      1.30  jdolecek 	vm->vm_ssize = nss;
    176       1.4       mrg 	return (1);
    177       1.1       mrg }
    178       1.1       mrg 
    179       1.1       mrg /*
    180       1.1       mrg  * sys_oadvise: old advice system call
    181       1.1       mrg  */
    182       1.1       mrg 
    183       1.1       mrg /* ARGSUSED */
    184       1.4       mrg int
    185  1.31.2.4      yamt sys_ovadvise(struct lwp *l, const struct sys_ovadvise_args *uap, register_t *retval)
    186       1.1       mrg {
    187       1.1       mrg #if 0
    188  1.31.2.4      yamt 	/* {
    189       1.4       mrg 		syscallarg(int) anom;
    190  1.31.2.4      yamt 	} */
    191       1.1       mrg #endif
    192       1.4       mrg 
    193       1.4       mrg 	return (EINVAL);
    194       1.1       mrg }
    195