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