Home | History | Annotate | Line # | Download | only in uvm
uvm_glue.c revision 1.175.2.1
      1  1.175.2.1        ad /*	$NetBSD: uvm_glue.c,v 1.175.2.1 2020/01/17 21:47:38 ad Exp $	*/
      2        1.1       mrg 
      3       1.48       chs /*
      4        1.1       mrg  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5       1.48       chs  * Copyright (c) 1991, 1993, The Regents of the University of California.
      6        1.1       mrg  *
      7        1.1       mrg  * All rights reserved.
      8        1.1       mrg  *
      9        1.1       mrg  * This code is derived from software contributed to Berkeley by
     10        1.1       mrg  * The Mach Operating System project at Carnegie-Mellon University.
     11        1.1       mrg  *
     12        1.1       mrg  * Redistribution and use in source and binary forms, with or without
     13        1.1       mrg  * modification, are permitted provided that the following conditions
     14        1.1       mrg  * are met:
     15        1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     16        1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     17        1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     18        1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     19        1.1       mrg  *    documentation and/or other materials provided with the distribution.
     20      1.147     chuck  * 3. Neither the name of the University nor the names of its contributors
     21        1.1       mrg  *    may be used to endorse or promote products derived from this software
     22        1.1       mrg  *    without specific prior written permission.
     23        1.1       mrg  *
     24        1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25        1.1       mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26        1.1       mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27        1.1       mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28        1.1       mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29        1.1       mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30        1.1       mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31        1.1       mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32        1.1       mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33        1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34        1.1       mrg  * SUCH DAMAGE.
     35        1.1       mrg  *
     36        1.1       mrg  *	@(#)vm_glue.c	8.6 (Berkeley) 1/5/94
     37        1.4       mrg  * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp
     38        1.1       mrg  *
     39        1.1       mrg  *
     40        1.1       mrg  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
     41        1.1       mrg  * All rights reserved.
     42       1.48       chs  *
     43        1.1       mrg  * Permission to use, copy, modify and distribute this software and
     44        1.1       mrg  * its documentation is hereby granted, provided that both the copyright
     45        1.1       mrg  * notice and this permission notice appear in all copies of the
     46        1.1       mrg  * software, derivative works or modified versions, and any portions
     47        1.1       mrg  * thereof, and that both notices appear in supporting documentation.
     48       1.48       chs  *
     49       1.48       chs  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     50       1.48       chs  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     51        1.1       mrg  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     52       1.48       chs  *
     53        1.1       mrg  * Carnegie Mellon requests users of this software to return to
     54        1.1       mrg  *
     55        1.1       mrg  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     56        1.1       mrg  *  School of Computer Science
     57        1.1       mrg  *  Carnegie Mellon University
     58        1.1       mrg  *  Pittsburgh PA 15213-3890
     59        1.1       mrg  *
     60        1.1       mrg  * any improvements or extensions that they make and grant Carnegie the
     61        1.1       mrg  * rights to redistribute these changes.
     62        1.1       mrg  */
     63       1.55     lukem 
     64       1.55     lukem #include <sys/cdefs.h>
     65  1.175.2.1        ad __KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.175.2.1 2020/01/17 21:47:38 ad Exp $");
     66        1.1       mrg 
     67       1.49     lukem #include "opt_kgdb.h"
     68       1.59      yamt #include "opt_kstack.h"
     69        1.5       mrg #include "opt_uvmhist.h"
     70        1.5       mrg 
     71        1.1       mrg /*
     72        1.1       mrg  * uvm_glue.c: glue functions
     73        1.1       mrg  */
     74        1.1       mrg 
     75        1.1       mrg #include <sys/param.h>
     76      1.145     rmind #include <sys/kernel.h>
     77      1.145     rmind 
     78        1.1       mrg #include <sys/systm.h>
     79        1.1       mrg #include <sys/proc.h>
     80        1.1       mrg #include <sys/resourcevar.h>
     81        1.1       mrg #include <sys/buf.h>
     82      1.106      yamt #include <sys/syncobj.h>
     83      1.111        ad #include <sys/cpu.h>
     84      1.114        ad #include <sys/atomic.h>
     85      1.146     rmind #include <sys/lwp.h>
     86      1.164      maxv #include <sys/asan.h>
     87        1.1       mrg 
     88        1.1       mrg #include <uvm/uvm.h>
     89      1.175        ad #include <uvm/uvm_pdpolicy.h>
     90      1.173        ad #include <uvm/uvm_pgflcache.h>
     91        1.1       mrg 
     92        1.1       mrg /*
     93      1.150     rmind  * uvm_kernacc: test if kernel can access a memory region.
     94        1.1       mrg  *
     95      1.150     rmind  * => Currently used only by /dev/kmem driver (dev/mm.c).
     96        1.1       mrg  */
     97      1.102   thorpej bool
     98      1.150     rmind uvm_kernacc(void *addr, size_t len, vm_prot_t prot)
     99        1.6       mrg {
    100      1.150     rmind 	vaddr_t saddr = trunc_page((vaddr_t)addr);
    101      1.150     rmind 	vaddr_t eaddr = round_page(saddr + len);
    102      1.102   thorpej 	bool rv;
    103        1.6       mrg 
    104        1.6       mrg 	vm_map_lock_read(kernel_map);
    105        1.6       mrg 	rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
    106        1.6       mrg 	vm_map_unlock_read(kernel_map);
    107        1.6       mrg 
    108      1.150     rmind 	return rv;
    109        1.1       mrg }
    110        1.1       mrg 
    111        1.1       mrg #ifdef KGDB
    112        1.1       mrg /*
    113        1.1       mrg  * Change protections on kernel pages from addr to addr+len
    114        1.1       mrg  * (presumably so debugger can plant a breakpoint).
    115        1.1       mrg  *
    116        1.1       mrg  * We force the protection change at the pmap level.  If we were
    117        1.1       mrg  * to use vm_map_protect a change to allow writing would be lazily-
    118        1.1       mrg  * applied meaning we would still take a protection fault, something
    119        1.1       mrg  * we really don't want to do.  It would also fragment the kernel
    120        1.1       mrg  * map unnecessarily.  We cannot use pmap_protect since it also won't
    121        1.1       mrg  * enforce a write-enable request.  Using pmap_enter is the only way
    122        1.1       mrg  * we can ensure the change takes place properly.
    123        1.1       mrg  */
    124        1.6       mrg void
    125      1.104  christos uvm_chgkprot(void *addr, size_t len, int rw)
    126        1.6       mrg {
    127        1.6       mrg 	vm_prot_t prot;
    128       1.13       eeh 	paddr_t pa;
    129       1.13       eeh 	vaddr_t sva, eva;
    130        1.6       mrg 
    131        1.6       mrg 	prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
    132       1.31    kleink 	eva = round_page((vaddr_t)addr + len);
    133       1.31    kleink 	for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) {
    134        1.6       mrg 		/*
    135        1.6       mrg 		 * Extract physical address for the page.
    136        1.6       mrg 		 */
    137      1.103   thorpej 		if (pmap_extract(pmap_kernel(), sva, &pa) == false)
    138      1.123  christos 			panic("%s: invalid page", __func__);
    139       1.30   thorpej 		pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED);
    140        1.6       mrg 	}
    141       1.51     chris 	pmap_update(pmap_kernel());
    142        1.1       mrg }
    143        1.1       mrg #endif
    144        1.1       mrg 
    145        1.1       mrg /*
    146       1.52       chs  * uvm_vslock: wire user memory for I/O
    147        1.1       mrg  *
    148        1.1       mrg  * - called from physio and sys___sysctl
    149        1.1       mrg  * - XXXCDC: consider nuking this (or making it a macro?)
    150        1.1       mrg  */
    151        1.1       mrg 
    152       1.26   thorpej int
    153       1.97       chs uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access_type)
    154        1.1       mrg {
    155       1.50       chs 	struct vm_map *map;
    156       1.26   thorpej 	vaddr_t start, end;
    157       1.45       chs 	int error;
    158       1.26   thorpej 
    159       1.97       chs 	map = &vs->vm_map;
    160       1.31    kleink 	start = trunc_page((vaddr_t)addr);
    161       1.31    kleink 	end = round_page((vaddr_t)addr + len);
    162       1.93  drochner 	error = uvm_fault_wire(map, start, end, access_type, 0);
    163       1.45       chs 	return error;
    164        1.1       mrg }
    165        1.1       mrg 
    166        1.1       mrg /*
    167       1.52       chs  * uvm_vsunlock: unwire user memory wired by uvm_vslock()
    168        1.1       mrg  *
    169        1.1       mrg  * - called from physio and sys___sysctl
    170        1.1       mrg  * - XXXCDC: consider nuking this (or making it a macro?)
    171        1.1       mrg  */
    172        1.1       mrg 
    173        1.6       mrg void
    174       1.97       chs uvm_vsunlock(struct vmspace *vs, void *addr, size_t len)
    175        1.1       mrg {
    176       1.97       chs 	uvm_fault_unwire(&vs->vm_map, trunc_page((vaddr_t)addr),
    177       1.43       chs 		round_page((vaddr_t)addr + len));
    178        1.1       mrg }
    179        1.1       mrg 
    180        1.1       mrg /*
    181       1.62   thorpej  * uvm_proc_fork: fork a virtual address space
    182        1.1       mrg  *
    183        1.1       mrg  * - the address space is copied as per parent map's inherit values
    184       1.62   thorpej  */
    185       1.62   thorpej void
    186      1.102   thorpej uvm_proc_fork(struct proc *p1, struct proc *p2, bool shared)
    187       1.62   thorpej {
    188       1.62   thorpej 
    189      1.103   thorpej 	if (shared == true) {
    190       1.62   thorpej 		p2->p_vmspace = NULL;
    191       1.62   thorpej 		uvmspace_share(p1, p2);
    192       1.62   thorpej 	} else {
    193       1.62   thorpej 		p2->p_vmspace = uvmspace_fork(p1->p_vmspace);
    194       1.62   thorpej 	}
    195       1.62   thorpej 
    196       1.62   thorpej 	cpu_proc_fork(p1, p2);
    197       1.62   thorpej }
    198       1.62   thorpej 
    199       1.62   thorpej /*
    200       1.62   thorpej  * uvm_lwp_fork: fork a thread
    201       1.62   thorpej  *
    202      1.146     rmind  * - a new PCB structure is allocated for the child process,
    203      1.146     rmind  *	and filled in by MD layer
    204       1.20   thorpej  * - if specified, the child gets a new user stack described by
    205       1.20   thorpej  *	stack and stacksize
    206        1.1       mrg  * - NOTE: the kernel stack may be at a different location in the child
    207        1.1       mrg  *	process, and thus addresses of automatic variables may be invalid
    208       1.62   thorpej  *	after cpu_lwp_fork returns in the child process.  We do nothing here
    209       1.62   thorpej  *	after cpu_lwp_fork returns.
    210        1.1       mrg  */
    211        1.6       mrg void
    212       1.89   thorpej uvm_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    213       1.89   thorpej     void (*func)(void *), void *arg)
    214        1.6       mrg {
    215        1.6       mrg 
    216      1.137     rmind 	/* Fill stack with magic number. */
    217       1.63      yamt 	kstack_setup_magic(l2);
    218        1.6       mrg 
    219        1.6       mrg 	/*
    220       1.62   thorpej 	 * cpu_lwp_fork() copy and update the pcb, and make the child ready
    221       1.62   thorpej  	 * to run.  If this is a normal user fork, the child will exit
    222       1.34   thorpej 	 * directly to user mode via child_return() on its first time
    223       1.34   thorpej 	 * slice and will not return here.  If this is a kernel thread,
    224       1.34   thorpej 	 * the specified entry point will be executed.
    225        1.6       mrg 	 */
    226       1.62   thorpej 	cpu_lwp_fork(l1, l2, stack, stacksize, func, arg);
    227       1.14   thorpej }
    228       1.14   thorpej 
    229       1.60       chs #ifndef USPACE_ALIGN
    230      1.115      yamt #define	USPACE_ALIGN	0
    231       1.60       chs #endif
    232       1.60       chs 
    233      1.115      yamt static pool_cache_t uvm_uarea_cache;
    234      1.148      matt #if defined(__HAVE_CPU_UAREA_ROUTINES)
    235      1.148      matt static pool_cache_t uvm_uarea_system_cache;
    236      1.148      matt #else
    237      1.148      matt #define uvm_uarea_system_cache uvm_uarea_cache
    238      1.148      matt #endif
    239      1.115      yamt 
    240      1.115      yamt static void *
    241      1.115      yamt uarea_poolpage_alloc(struct pool *pp, int flags)
    242      1.115      yamt {
    243      1.168       chs 
    244      1.168       chs 	KASSERT((flags & PR_WAITOK) != 0);
    245      1.168       chs 
    246      1.163      maxv #if defined(PMAP_MAP_POOLPAGE)
    247      1.168       chs 	while (USPACE == PAGE_SIZE && USPACE_ALIGN == 0) {
    248      1.163      maxv 		struct vm_page *pg;
    249      1.163      maxv 		vaddr_t va;
    250      1.154      para #if defined(PMAP_ALLOC_POOLPAGE)
    251      1.168       chs 		pg = PMAP_ALLOC_POOLPAGE(0);
    252      1.154      para #else
    253      1.168       chs 		pg = uvm_pagealloc(NULL, 0, NULL, 0);
    254      1.163      maxv #endif
    255      1.168       chs 		if (pg == NULL) {
    256      1.168       chs 			uvm_wait("uarea");
    257      1.168       chs 			continue;
    258      1.168       chs 		}
    259      1.163      maxv 		va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
    260      1.165   mlelstv 		KASSERT(va != 0);
    261      1.163      maxv 		return (void *)va;
    262      1.163      maxv 	}
    263      1.163      maxv #endif
    264      1.163      maxv #if defined(__HAVE_CPU_UAREA_ROUTINES)
    265      1.148      matt 	void *va = cpu_uarea_alloc(false);
    266      1.148      matt 	if (va)
    267      1.148      matt 		return (void *)va;
    268      1.163      maxv #endif
    269      1.115      yamt 	return (void *)uvm_km_alloc(kernel_map, pp->pr_alloc->pa_pagesz,
    270      1.168       chs 	    USPACE_ALIGN, UVM_KMF_WIRED | UVM_KMF_WAITVA);
    271      1.115      yamt }
    272      1.109        ad 
    273      1.115      yamt static void
    274      1.115      yamt uarea_poolpage_free(struct pool *pp, void *addr)
    275      1.115      yamt {
    276      1.154      para #if defined(PMAP_MAP_POOLPAGE)
    277      1.154      para 	if (USPACE == PAGE_SIZE && USPACE_ALIGN == 0) {
    278      1.154      para 		paddr_t pa;
    279      1.154      para 
    280      1.154      para 		pa = PMAP_UNMAP_POOLPAGE((vaddr_t) addr);
    281      1.154      para 		KASSERT(pa != 0);
    282      1.154      para 		uvm_pagefree(PHYS_TO_VM_PAGE(pa));
    283      1.139      matt 		return;
    284      1.139      matt 	}
    285      1.154      para #endif
    286      1.148      matt #if defined(__HAVE_CPU_UAREA_ROUTINES)
    287      1.148      matt 	if (cpu_uarea_free(addr))
    288      1.148      matt 		return;
    289      1.148      matt #endif
    290      1.115      yamt 	uvm_km_free(kernel_map, (vaddr_t)addr, pp->pr_alloc->pa_pagesz,
    291      1.141     rmind 	    UVM_KMF_WIRED);
    292      1.115      yamt }
    293      1.115      yamt 
    294      1.115      yamt static struct pool_allocator uvm_uarea_allocator = {
    295      1.115      yamt 	.pa_alloc = uarea_poolpage_alloc,
    296      1.115      yamt 	.pa_free = uarea_poolpage_free,
    297      1.115      yamt 	.pa_pagesz = USPACE,
    298      1.115      yamt };
    299      1.115      yamt 
    300      1.148      matt #if defined(__HAVE_CPU_UAREA_ROUTINES)
    301      1.148      matt static void *
    302      1.148      matt uarea_system_poolpage_alloc(struct pool *pp, int flags)
    303      1.148      matt {
    304      1.148      matt 	void * const va = cpu_uarea_alloc(true);
    305      1.151      matt 	if (va != NULL)
    306      1.151      matt 		return va;
    307      1.151      matt 
    308      1.151      matt 	return (void *)uvm_km_alloc(kernel_map, pp->pr_alloc->pa_pagesz,
    309      1.151      matt 	    USPACE_ALIGN, UVM_KMF_WIRED |
    310      1.151      matt 	    ((flags & PR_WAITOK) ? UVM_KMF_WAITVA :
    311      1.151      matt 	    (UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)));
    312      1.148      matt }
    313      1.148      matt 
    314      1.148      matt static void
    315      1.148      matt uarea_system_poolpage_free(struct pool *pp, void *addr)
    316      1.148      matt {
    317      1.158       chs 	if (cpu_uarea_free(addr))
    318      1.158       chs 		return;
    319      1.158       chs 
    320      1.158       chs 	uvm_km_free(kernel_map, (vaddr_t)addr, pp->pr_alloc->pa_pagesz,
    321      1.158       chs 	    UVM_KMF_WIRED);
    322      1.148      matt }
    323      1.148      matt 
    324      1.148      matt static struct pool_allocator uvm_uarea_system_allocator = {
    325      1.148      matt 	.pa_alloc = uarea_system_poolpage_alloc,
    326      1.148      matt 	.pa_free = uarea_system_poolpage_free,
    327      1.148      matt 	.pa_pagesz = USPACE,
    328      1.148      matt };
    329      1.148      matt #endif /* __HAVE_CPU_UAREA_ROUTINES */
    330      1.148      matt 
    331      1.115      yamt void
    332      1.115      yamt uvm_uarea_init(void)
    333      1.115      yamt {
    334      1.117      yamt 	int flags = PR_NOTOUCH;
    335      1.115      yamt 
    336      1.116      yamt 	/*
    337      1.116      yamt 	 * specify PR_NOALIGN unless the alignment provided by
    338      1.116      yamt 	 * the backend (USPACE_ALIGN) is sufficient to provide
    339      1.116      yamt 	 * pool page size (UPSACE) alignment.
    340      1.116      yamt 	 */
    341      1.116      yamt 
    342      1.117      yamt 	if ((USPACE_ALIGN == 0 && USPACE != PAGE_SIZE) ||
    343      1.117      yamt 	    (USPACE_ALIGN % USPACE) != 0) {
    344      1.117      yamt 		flags |= PR_NOALIGN;
    345      1.117      yamt 	}
    346      1.117      yamt 
    347      1.117      yamt 	uvm_uarea_cache = pool_cache_init(USPACE, USPACE_ALIGN, 0, flags,
    348      1.141     rmind 	    "uarea", &uvm_uarea_allocator, IPL_NONE, NULL, NULL, NULL);
    349      1.149  drochner #if defined(__HAVE_CPU_UAREA_ROUTINES)
    350      1.149  drochner 	uvm_uarea_system_cache = pool_cache_init(USPACE, USPACE_ALIGN,
    351      1.149  drochner 	    0, flags, "uareasys", &uvm_uarea_system_allocator,
    352      1.149  drochner 	    IPL_NONE, NULL, NULL, NULL);
    353      1.149  drochner #endif
    354       1.60       chs }
    355       1.60       chs 
    356       1.60       chs /*
    357      1.115      yamt  * uvm_uarea_alloc: allocate a u-area
    358       1.75  jdolecek  */
    359       1.75  jdolecek 
    360      1.141     rmind vaddr_t
    361      1.141     rmind uvm_uarea_alloc(void)
    362       1.75  jdolecek {
    363      1.109        ad 
    364      1.141     rmind 	return (vaddr_t)pool_cache_get(uvm_uarea_cache, PR_WAITOK);
    365       1.75  jdolecek }
    366       1.75  jdolecek 
    367      1.148      matt vaddr_t
    368      1.160      matt uvm_uarea_system_alloc(struct cpu_info *ci)
    369      1.148      matt {
    370      1.160      matt #ifdef __HAVE_CPU_UAREA_ALLOC_IDLELWP
    371      1.160      matt 	if (__predict_false(ci != NULL))
    372      1.160      matt 		return cpu_uarea_alloc_idlelwp(ci);
    373      1.160      matt #endif
    374      1.148      matt 
    375      1.148      matt 	return (vaddr_t)pool_cache_get(uvm_uarea_system_cache, PR_WAITOK);
    376      1.148      matt }
    377      1.148      matt 
    378       1.75  jdolecek /*
    379      1.115      yamt  * uvm_uarea_free: free a u-area
    380       1.60       chs  */
    381       1.60       chs 
    382       1.60       chs void
    383      1.141     rmind uvm_uarea_free(vaddr_t uaddr)
    384       1.60       chs {
    385       1.60       chs 
    386      1.167      maxv 	kasan_mark((void *)uaddr, USPACE, USPACE, 0);
    387      1.115      yamt 	pool_cache_put(uvm_uarea_cache, (void *)uaddr);
    388       1.60       chs }
    389       1.60       chs 
    390      1.148      matt void
    391      1.148      matt uvm_uarea_system_free(vaddr_t uaddr)
    392      1.148      matt {
    393      1.148      matt 
    394      1.167      maxv 	kasan_mark((void *)uaddr, USPACE, USPACE, 0);
    395      1.148      matt 	pool_cache_put(uvm_uarea_system_cache, (void *)uaddr);
    396      1.148      matt }
    397      1.148      matt 
    398      1.142     rmind vaddr_t
    399      1.142     rmind uvm_lwp_getuarea(lwp_t *l)
    400      1.142     rmind {
    401      1.142     rmind 
    402      1.146     rmind 	return (vaddr_t)l->l_addr - UAREA_PCB_OFFSET;
    403      1.142     rmind }
    404      1.142     rmind 
    405      1.142     rmind void
    406      1.142     rmind uvm_lwp_setuarea(lwp_t *l, vaddr_t addr)
    407      1.142     rmind {
    408      1.142     rmind 
    409      1.146     rmind 	l->l_addr = (void *)(addr + UAREA_PCB_OFFSET);
    410      1.142     rmind }
    411      1.142     rmind 
    412       1.60       chs /*
    413      1.118      yamt  * uvm_proc_exit: exit a virtual address space
    414       1.80        pk  *
    415       1.80        pk  * - borrow proc0's address space because freeing the vmspace
    416       1.80        pk  *   of the dead process may block.
    417       1.80        pk  */
    418       1.80        pk 
    419       1.80        pk void
    420       1.89   thorpej uvm_proc_exit(struct proc *p)
    421       1.80        pk {
    422       1.80        pk 	struct lwp *l = curlwp; /* XXX */
    423       1.80        pk 	struct vmspace *ovm;
    424       1.80        pk 
    425       1.80        pk 	KASSERT(p == l->l_proc);
    426       1.80        pk 	ovm = p->p_vmspace;
    427      1.159    martin 	KASSERT(ovm != NULL);
    428      1.159    martin 
    429      1.159    martin 	if (__predict_false(ovm == proc0.p_vmspace))
    430      1.159    martin 		return;
    431       1.80        pk 
    432       1.80        pk 	/*
    433       1.80        pk 	 * borrow proc0's address space.
    434       1.80        pk 	 */
    435      1.161  uebayasi 	kpreempt_disable();
    436      1.159    martin 	pmap_deactivate(l);
    437       1.80        pk 	p->p_vmspace = proc0.p_vmspace;
    438       1.80        pk 	pmap_activate(l);
    439      1.161  uebayasi 	kpreempt_enable();
    440       1.80        pk 
    441      1.159    martin 	uvmspace_free(ovm);
    442       1.80        pk }
    443       1.80        pk 
    444       1.80        pk void
    445       1.80        pk uvm_lwp_exit(struct lwp *l)
    446       1.80        pk {
    447      1.143     rmind 	vaddr_t va = uvm_lwp_getuarea(l);
    448      1.148      matt 	bool system = (l->l_flag & LW_SYSTEM) != 0;
    449       1.80        pk 
    450      1.148      matt 	if (system)
    451      1.148      matt 		uvm_uarea_system_free(va);
    452      1.148      matt 	else
    453      1.148      matt 		uvm_uarea_free(va);
    454      1.143     rmind #ifdef DIAGNOSTIC
    455      1.143     rmind 	uvm_lwp_setuarea(l, (vaddr_t)NULL);
    456      1.143     rmind #endif
    457       1.80        pk }
    458       1.80        pk 
    459       1.80        pk /*
    460        1.1       mrg  * uvm_init_limit: init per-process VM limits
    461        1.1       mrg  *
    462        1.1       mrg  * - called for process 0 and then inherited by all others.
    463        1.1       mrg  */
    464       1.60       chs 
    465        1.6       mrg void
    466       1.89   thorpej uvm_init_limits(struct proc *p)
    467        1.6       mrg {
    468        1.6       mrg 
    469        1.6       mrg 	/*
    470        1.6       mrg 	 * Set up the initial limits on process VM.  Set the maximum
    471        1.6       mrg 	 * resident set size to be all of (reasonably) available memory.
    472        1.6       mrg 	 * This causes any single, large process to start random page
    473        1.6       mrg 	 * replacement once it fills memory.
    474        1.6       mrg 	 */
    475        1.6       mrg 
    476        1.6       mrg 	p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
    477       1.79        pk 	p->p_rlimit[RLIMIT_STACK].rlim_max = maxsmap;
    478        1.6       mrg 	p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
    479       1.79        pk 	p->p_rlimit[RLIMIT_DATA].rlim_max = maxdmap;
    480      1.136       mrg 	p->p_rlimit[RLIMIT_AS].rlim_cur = RLIM_INFINITY;
    481      1.136       mrg 	p->p_rlimit[RLIMIT_AS].rlim_max = RLIM_INFINITY;
    482      1.172        ad 	p->p_rlimit[RLIMIT_RSS].rlim_cur = MIN(VM_MAXUSER_ADDRESS,
    483      1.174        ad 	    ctob((rlim_t)uvm_availmem()));
    484        1.1       mrg }
    485        1.1       mrg 
    486       1.99        ad /*
    487      1.141     rmind  * uvm_scheduler: process zero main loop.
    488        1.1       mrg  */
    489      1.145     rmind 
    490      1.145     rmind extern struct loadavg averunnable;
    491      1.145     rmind 
    492        1.6       mrg void
    493       1.89   thorpej uvm_scheduler(void)
    494        1.1       mrg {
    495      1.141     rmind 	lwp_t *l = curlwp;
    496        1.1       mrg 
    497       1.99        ad 	lwp_lock(l);
    498      1.113        ad 	l->l_class = SCHED_FIFO;
    499      1.170        ad 	lwp_changepri(l, PRI_VM);
    500       1.99        ad 	lwp_unlock(l);
    501       1.99        ad 
    502      1.173        ad 	/* Start the freelist cache. */
    503      1.173        ad 	uvm_pgflcache_start();
    504      1.173        ad 
    505       1.99        ad 	for (;;) {
    506      1.171        ad 		/* Update legacy stats for post-mortem debugging. */
    507      1.171        ad 		uvm_update_uvmexp();
    508      1.173        ad 
    509      1.173        ad 		/* See if the pagedaemon needs to generate some free pages. */
    510      1.173        ad 		uvm_kick_pdaemon();
    511      1.173        ad 
    512      1.173        ad 		/* Calculate process statistics. */
    513      1.145     rmind 		sched_pstats();
    514      1.145     rmind 		(void)kpause("uvm", false, hz, NULL);
    515      1.114        ad 	}
    516      1.107        ad }
    517      1.175        ad 
    518      1.175        ad /*
    519      1.175        ad  * uvm_idle: called from the idle loop.
    520      1.175        ad  */
    521      1.175        ad 
    522      1.175        ad void
    523      1.175        ad uvm_idle(void)
    524      1.175        ad {
    525      1.175        ad 	struct cpu_info *ci = curcpu();
    526      1.175        ad 	struct uvm_cpu *ucpu = ci->ci_data.cpu_uvm;
    527      1.175        ad 
    528      1.175        ad 	KASSERT(kpreempt_disabled());
    529      1.175        ad 
    530      1.175        ad 	if (!ci->ci_want_resched)
    531      1.175        ad 		uvmpdpol_idle(ucpu);
    532      1.175        ad 	if (!ci->ci_want_resched)
    533      1.175        ad 		uvm_pageidlezero();
    534      1.175        ad 
    535      1.175        ad }
    536