uvm_glue.c revision 1.145 1 1.145 rmind /* $NetBSD: uvm_glue.c,v 1.145 2010/04/16 03:21:49 rmind 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.1 mrg * 3. All advertising materials mentioning features or use of this software
21 1.1 mrg * must display the following acknowledgement:
22 1.1 mrg * This product includes software developed by Charles D. Cranor,
23 1.48 chs * Washington University, the University of California, Berkeley and
24 1.1 mrg * its contributors.
25 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
26 1.1 mrg * may be used to endorse or promote products derived from this software
27 1.1 mrg * without specific prior written permission.
28 1.1 mrg *
29 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 1.1 mrg * SUCH DAMAGE.
40 1.1 mrg *
41 1.1 mrg * @(#)vm_glue.c 8.6 (Berkeley) 1/5/94
42 1.4 mrg * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp
43 1.1 mrg *
44 1.1 mrg *
45 1.1 mrg * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 1.1 mrg * All rights reserved.
47 1.48 chs *
48 1.1 mrg * Permission to use, copy, modify and distribute this software and
49 1.1 mrg * its documentation is hereby granted, provided that both the copyright
50 1.1 mrg * notice and this permission notice appear in all copies of the
51 1.1 mrg * software, derivative works or modified versions, and any portions
52 1.1 mrg * thereof, and that both notices appear in supporting documentation.
53 1.48 chs *
54 1.48 chs * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 1.48 chs * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56 1.1 mrg * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57 1.48 chs *
58 1.1 mrg * Carnegie Mellon requests users of this software to return to
59 1.1 mrg *
60 1.1 mrg * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
61 1.1 mrg * School of Computer Science
62 1.1 mrg * Carnegie Mellon University
63 1.1 mrg * Pittsburgh PA 15213-3890
64 1.1 mrg *
65 1.1 mrg * any improvements or extensions that they make and grant Carnegie the
66 1.1 mrg * rights to redistribute these changes.
67 1.1 mrg */
68 1.55 lukem
69 1.55 lukem #include <sys/cdefs.h>
70 1.145 rmind __KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.145 2010/04/16 03:21:49 rmind Exp $");
71 1.1 mrg
72 1.49 lukem #include "opt_kgdb.h"
73 1.59 yamt #include "opt_kstack.h"
74 1.5 mrg #include "opt_uvmhist.h"
75 1.5 mrg
76 1.1 mrg /*
77 1.1 mrg * uvm_glue.c: glue functions
78 1.1 mrg */
79 1.1 mrg
80 1.1 mrg #include <sys/param.h>
81 1.145 rmind #include <sys/kernel.h>
82 1.145 rmind
83 1.1 mrg #include <sys/systm.h>
84 1.1 mrg #include <sys/proc.h>
85 1.1 mrg #include <sys/resourcevar.h>
86 1.1 mrg #include <sys/buf.h>
87 1.1 mrg #include <sys/user.h>
88 1.106 yamt #include <sys/syncobj.h>
89 1.111 ad #include <sys/cpu.h>
90 1.114 ad #include <sys/atomic.h>
91 1.1 mrg
92 1.1 mrg #include <uvm/uvm.h>
93 1.1 mrg
94 1.1 mrg /*
95 1.1 mrg * XXXCDC: do these really belong here?
96 1.1 mrg */
97 1.1 mrg
98 1.28 thorpej /*
99 1.1 mrg * uvm_kernacc: can the kernel access a region of memory
100 1.1 mrg *
101 1.83 yamt * - used only by /dev/kmem driver (mem.c)
102 1.1 mrg */
103 1.1 mrg
104 1.102 thorpej bool
105 1.104 christos uvm_kernacc(void *addr, size_t len, int rw)
106 1.6 mrg {
107 1.102 thorpej bool rv;
108 1.13 eeh vaddr_t saddr, eaddr;
109 1.6 mrg vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
110 1.6 mrg
111 1.31 kleink saddr = trunc_page((vaddr_t)addr);
112 1.43 chs eaddr = round_page((vaddr_t)addr + len);
113 1.6 mrg vm_map_lock_read(kernel_map);
114 1.6 mrg rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
115 1.6 mrg vm_map_unlock_read(kernel_map);
116 1.6 mrg
117 1.6 mrg return(rv);
118 1.1 mrg }
119 1.1 mrg
120 1.1 mrg #ifdef KGDB
121 1.1 mrg /*
122 1.1 mrg * Change protections on kernel pages from addr to addr+len
123 1.1 mrg * (presumably so debugger can plant a breakpoint).
124 1.1 mrg *
125 1.1 mrg * We force the protection change at the pmap level. If we were
126 1.1 mrg * to use vm_map_protect a change to allow writing would be lazily-
127 1.1 mrg * applied meaning we would still take a protection fault, something
128 1.1 mrg * we really don't want to do. It would also fragment the kernel
129 1.1 mrg * map unnecessarily. We cannot use pmap_protect since it also won't
130 1.1 mrg * enforce a write-enable request. Using pmap_enter is the only way
131 1.1 mrg * we can ensure the change takes place properly.
132 1.1 mrg */
133 1.6 mrg void
134 1.104 christos uvm_chgkprot(void *addr, size_t len, int rw)
135 1.6 mrg {
136 1.6 mrg vm_prot_t prot;
137 1.13 eeh paddr_t pa;
138 1.13 eeh vaddr_t sva, eva;
139 1.6 mrg
140 1.6 mrg prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
141 1.31 kleink eva = round_page((vaddr_t)addr + len);
142 1.31 kleink for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) {
143 1.6 mrg /*
144 1.6 mrg * Extract physical address for the page.
145 1.6 mrg */
146 1.103 thorpej if (pmap_extract(pmap_kernel(), sva, &pa) == false)
147 1.123 christos panic("%s: invalid page", __func__);
148 1.30 thorpej pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED);
149 1.6 mrg }
150 1.51 chris pmap_update(pmap_kernel());
151 1.1 mrg }
152 1.1 mrg #endif
153 1.1 mrg
154 1.1 mrg /*
155 1.52 chs * uvm_vslock: wire user memory for I/O
156 1.1 mrg *
157 1.1 mrg * - called from physio and sys___sysctl
158 1.1 mrg * - XXXCDC: consider nuking this (or making it a macro?)
159 1.1 mrg */
160 1.1 mrg
161 1.26 thorpej int
162 1.97 chs uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access_type)
163 1.1 mrg {
164 1.50 chs struct vm_map *map;
165 1.26 thorpej vaddr_t start, end;
166 1.45 chs int error;
167 1.26 thorpej
168 1.97 chs map = &vs->vm_map;
169 1.31 kleink start = trunc_page((vaddr_t)addr);
170 1.31 kleink end = round_page((vaddr_t)addr + len);
171 1.93 drochner error = uvm_fault_wire(map, start, end, access_type, 0);
172 1.45 chs return error;
173 1.1 mrg }
174 1.1 mrg
175 1.1 mrg /*
176 1.52 chs * uvm_vsunlock: unwire user memory wired by uvm_vslock()
177 1.1 mrg *
178 1.1 mrg * - called from physio and sys___sysctl
179 1.1 mrg * - XXXCDC: consider nuking this (or making it a macro?)
180 1.1 mrg */
181 1.1 mrg
182 1.6 mrg void
183 1.97 chs uvm_vsunlock(struct vmspace *vs, void *addr, size_t len)
184 1.1 mrg {
185 1.97 chs uvm_fault_unwire(&vs->vm_map, trunc_page((vaddr_t)addr),
186 1.43 chs round_page((vaddr_t)addr + len));
187 1.1 mrg }
188 1.1 mrg
189 1.1 mrg /*
190 1.62 thorpej * uvm_proc_fork: fork a virtual address space
191 1.1 mrg *
192 1.1 mrg * - the address space is copied as per parent map's inherit values
193 1.62 thorpej */
194 1.62 thorpej void
195 1.102 thorpej uvm_proc_fork(struct proc *p1, struct proc *p2, bool shared)
196 1.62 thorpej {
197 1.62 thorpej
198 1.103 thorpej if (shared == true) {
199 1.62 thorpej p2->p_vmspace = NULL;
200 1.62 thorpej uvmspace_share(p1, p2);
201 1.62 thorpej } else {
202 1.62 thorpej p2->p_vmspace = uvmspace_fork(p1->p_vmspace);
203 1.62 thorpej }
204 1.62 thorpej
205 1.62 thorpej cpu_proc_fork(p1, p2);
206 1.62 thorpej }
207 1.62 thorpej
208 1.62 thorpej /*
209 1.62 thorpej * uvm_lwp_fork: fork a thread
210 1.62 thorpej *
211 1.1 mrg * - a new "user" structure is allocated for the child process
212 1.1 mrg * [filled in by MD layer...]
213 1.20 thorpej * - if specified, the child gets a new user stack described by
214 1.20 thorpej * stack and stacksize
215 1.1 mrg * - NOTE: the kernel stack may be at a different location in the child
216 1.1 mrg * process, and thus addresses of automatic variables may be invalid
217 1.62 thorpej * after cpu_lwp_fork returns in the child process. We do nothing here
218 1.62 thorpej * after cpu_lwp_fork returns.
219 1.1 mrg */
220 1.6 mrg void
221 1.89 thorpej uvm_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
222 1.89 thorpej void (*func)(void *), void *arg)
223 1.6 mrg {
224 1.6 mrg
225 1.137 rmind /* Fill stack with magic number. */
226 1.63 yamt kstack_setup_magic(l2);
227 1.6 mrg
228 1.6 mrg /*
229 1.62 thorpej * cpu_lwp_fork() copy and update the pcb, and make the child ready
230 1.62 thorpej * to run. If this is a normal user fork, the child will exit
231 1.34 thorpej * directly to user mode via child_return() on its first time
232 1.34 thorpej * slice and will not return here. If this is a kernel thread,
233 1.34 thorpej * the specified entry point will be executed.
234 1.6 mrg */
235 1.62 thorpej cpu_lwp_fork(l1, l2, stack, stacksize, func, arg);
236 1.138 rmind
237 1.138 rmind /* Inactive emap for new LWP. */
238 1.138 rmind l2->l_emap_gen = UVM_EMAP_INACTIVE;
239 1.14 thorpej }
240 1.14 thorpej
241 1.60 chs #ifndef USPACE_ALIGN
242 1.115 yamt #define USPACE_ALIGN 0
243 1.60 chs #endif
244 1.60 chs
245 1.115 yamt static pool_cache_t uvm_uarea_cache;
246 1.115 yamt
247 1.115 yamt static void *
248 1.115 yamt uarea_poolpage_alloc(struct pool *pp, int flags)
249 1.115 yamt {
250 1.141 rmind #if defined(PMAP_MAP_POOLPAGE)
251 1.139 matt if (USPACE == PAGE_SIZE && USPACE_ALIGN == 0) {
252 1.139 matt struct vm_page *pg;
253 1.139 matt vaddr_t va;
254 1.139 matt
255 1.139 matt pg = uvm_pagealloc(NULL, 0, NULL,
256 1.139 matt ((flags & PR_WAITOK) == 0 ? UVM_KMF_NOWAIT : 0));
257 1.139 matt if (pg == NULL)
258 1.139 matt return NULL;
259 1.139 matt va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
260 1.139 matt if (va == 0)
261 1.139 matt uvm_pagefree(pg);
262 1.139 matt return (void *)va;
263 1.139 matt }
264 1.139 matt #endif
265 1.115 yamt return (void *)uvm_km_alloc(kernel_map, pp->pr_alloc->pa_pagesz,
266 1.141 rmind USPACE_ALIGN, UVM_KMF_WIRED |
267 1.141 rmind ((flags & PR_WAITOK) ? UVM_KMF_WAITVA :
268 1.115 yamt (UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)));
269 1.115 yamt }
270 1.109 ad
271 1.115 yamt static void
272 1.115 yamt uarea_poolpage_free(struct pool *pp, void *addr)
273 1.115 yamt {
274 1.141 rmind #if defined(PMAP_MAP_POOLPAGE)
275 1.139 matt if (USPACE == PAGE_SIZE && USPACE_ALIGN == 0) {
276 1.139 matt paddr_t pa;
277 1.139 matt
278 1.139 matt pa = PMAP_UNMAP_POOLPAGE((vaddr_t) addr);
279 1.139 matt KASSERT(pa != 0);
280 1.139 matt uvm_pagefree(PHYS_TO_VM_PAGE(pa));
281 1.139 matt return;
282 1.139 matt }
283 1.139 matt #endif
284 1.115 yamt uvm_km_free(kernel_map, (vaddr_t)addr, pp->pr_alloc->pa_pagesz,
285 1.141 rmind UVM_KMF_WIRED);
286 1.115 yamt }
287 1.115 yamt
288 1.115 yamt static struct pool_allocator uvm_uarea_allocator = {
289 1.115 yamt .pa_alloc = uarea_poolpage_alloc,
290 1.115 yamt .pa_free = uarea_poolpage_free,
291 1.115 yamt .pa_pagesz = USPACE,
292 1.115 yamt };
293 1.115 yamt
294 1.115 yamt void
295 1.115 yamt uvm_uarea_init(void)
296 1.115 yamt {
297 1.117 yamt int flags = PR_NOTOUCH;
298 1.115 yamt
299 1.116 yamt /*
300 1.116 yamt * specify PR_NOALIGN unless the alignment provided by
301 1.116 yamt * the backend (USPACE_ALIGN) is sufficient to provide
302 1.116 yamt * pool page size (UPSACE) alignment.
303 1.116 yamt */
304 1.116 yamt
305 1.117 yamt if ((USPACE_ALIGN == 0 && USPACE != PAGE_SIZE) ||
306 1.117 yamt (USPACE_ALIGN % USPACE) != 0) {
307 1.117 yamt flags |= PR_NOALIGN;
308 1.117 yamt }
309 1.117 yamt
310 1.117 yamt uvm_uarea_cache = pool_cache_init(USPACE, USPACE_ALIGN, 0, flags,
311 1.141 rmind "uarea", &uvm_uarea_allocator, IPL_NONE, NULL, NULL, NULL);
312 1.60 chs }
313 1.60 chs
314 1.60 chs /*
315 1.115 yamt * uvm_uarea_alloc: allocate a u-area
316 1.75 jdolecek */
317 1.75 jdolecek
318 1.141 rmind vaddr_t
319 1.141 rmind uvm_uarea_alloc(void)
320 1.75 jdolecek {
321 1.109 ad
322 1.141 rmind return (vaddr_t)pool_cache_get(uvm_uarea_cache, PR_WAITOK);
323 1.75 jdolecek }
324 1.75 jdolecek
325 1.75 jdolecek /*
326 1.115 yamt * uvm_uarea_free: free a u-area
327 1.60 chs */
328 1.60 chs
329 1.60 chs void
330 1.141 rmind uvm_uarea_free(vaddr_t uaddr)
331 1.60 chs {
332 1.60 chs
333 1.115 yamt pool_cache_put(uvm_uarea_cache, (void *)uaddr);
334 1.60 chs }
335 1.60 chs
336 1.142 rmind vaddr_t
337 1.142 rmind uvm_lwp_getuarea(lwp_t *l)
338 1.142 rmind {
339 1.142 rmind
340 1.143 rmind return (vaddr_t)l->l_addr - UAREA_USER_OFFSET;
341 1.142 rmind }
342 1.142 rmind
343 1.142 rmind void
344 1.142 rmind uvm_lwp_setuarea(lwp_t *l, vaddr_t addr)
345 1.142 rmind {
346 1.142 rmind
347 1.143 rmind l->l_addr = (void *)(addr + UAREA_USER_OFFSET);
348 1.142 rmind }
349 1.142 rmind
350 1.60 chs /*
351 1.118 yamt * uvm_proc_exit: exit a virtual address space
352 1.80 pk *
353 1.80 pk * - borrow proc0's address space because freeing the vmspace
354 1.80 pk * of the dead process may block.
355 1.80 pk */
356 1.80 pk
357 1.80 pk void
358 1.89 thorpej uvm_proc_exit(struct proc *p)
359 1.80 pk {
360 1.80 pk struct lwp *l = curlwp; /* XXX */
361 1.80 pk struct vmspace *ovm;
362 1.80 pk
363 1.80 pk KASSERT(p == l->l_proc);
364 1.80 pk ovm = p->p_vmspace;
365 1.80 pk
366 1.80 pk /*
367 1.80 pk * borrow proc0's address space.
368 1.80 pk */
369 1.129 ad KPREEMPT_DISABLE(l);
370 1.80 pk pmap_deactivate(l);
371 1.80 pk p->p_vmspace = proc0.p_vmspace;
372 1.80 pk pmap_activate(l);
373 1.129 ad KPREEMPT_ENABLE(l);
374 1.80 pk
375 1.80 pk uvmspace_free(ovm);
376 1.80 pk }
377 1.80 pk
378 1.80 pk void
379 1.80 pk uvm_lwp_exit(struct lwp *l)
380 1.80 pk {
381 1.143 rmind vaddr_t va = uvm_lwp_getuarea(l);
382 1.80 pk
383 1.141 rmind uvm_uarea_free(va);
384 1.143 rmind #ifdef DIAGNOSTIC
385 1.143 rmind uvm_lwp_setuarea(l, (vaddr_t)NULL);
386 1.143 rmind #endif
387 1.80 pk }
388 1.80 pk
389 1.80 pk /*
390 1.1 mrg * uvm_init_limit: init per-process VM limits
391 1.1 mrg *
392 1.1 mrg * - called for process 0 and then inherited by all others.
393 1.1 mrg */
394 1.60 chs
395 1.6 mrg void
396 1.89 thorpej uvm_init_limits(struct proc *p)
397 1.6 mrg {
398 1.6 mrg
399 1.6 mrg /*
400 1.6 mrg * Set up the initial limits on process VM. Set the maximum
401 1.6 mrg * resident set size to be all of (reasonably) available memory.
402 1.6 mrg * This causes any single, large process to start random page
403 1.6 mrg * replacement once it fills memory.
404 1.6 mrg */
405 1.6 mrg
406 1.6 mrg p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
407 1.79 pk p->p_rlimit[RLIMIT_STACK].rlim_max = maxsmap;
408 1.6 mrg p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
409 1.79 pk p->p_rlimit[RLIMIT_DATA].rlim_max = maxdmap;
410 1.136 mrg p->p_rlimit[RLIMIT_AS].rlim_cur = RLIM_INFINITY;
411 1.136 mrg p->p_rlimit[RLIMIT_AS].rlim_max = RLIM_INFINITY;
412 1.144 jym p->p_rlimit[RLIMIT_RSS].rlim_cur = MIN(
413 1.144 jym VM_MAXUSER_ADDRESS, ctob((rlim_t)uvmexp.free));
414 1.1 mrg }
415 1.1 mrg
416 1.99 ad /*
417 1.141 rmind * uvm_scheduler: process zero main loop.
418 1.1 mrg */
419 1.145 rmind
420 1.145 rmind extern struct loadavg averunnable;
421 1.145 rmind
422 1.6 mrg void
423 1.89 thorpej uvm_scheduler(void)
424 1.1 mrg {
425 1.141 rmind lwp_t *l = curlwp;
426 1.1 mrg
427 1.99 ad lwp_lock(l);
428 1.113 ad l->l_priority = PRI_VM;
429 1.113 ad l->l_class = SCHED_FIFO;
430 1.99 ad lwp_unlock(l);
431 1.99 ad
432 1.99 ad for (;;) {
433 1.145 rmind sched_pstats();
434 1.145 rmind (void)kpause("uvm", false, hz, NULL);
435 1.114 ad }
436 1.107 ad }
437