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