uvm_glue.c revision 1.44.2.15 1 /* $NetBSD: uvm_glue.c,v 1.44.2.15 2002/07/12 01:40:43 nathanw 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.44.2.15 2002/07/12 01:40:43 nathanw Exp $");
71
72 #include "opt_kgdb.h"
73 #include "opt_sysv.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 #ifdef SYSVSHM
87 #include <sys/shm.h>
88 #endif
89
90 #include <uvm/uvm.h>
91
92 #include <machine/cpu.h>
93
94 /*
95 * local prototypes
96 */
97
98 static void uvm_swapout __P((struct lwp *));
99
100 /*
101 * XXXCDC: do these really belong here?
102 */
103
104 int readbuffers = 0; /* allow KGDB to read kern buffer pool */
105 /* XXX: see uvm_kernacc */
106
107
108 /*
109 * uvm_kernacc: can the kernel access a region of memory
110 *
111 * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c)
112 */
113
114 boolean_t
115 uvm_kernacc(addr, len, rw)
116 caddr_t addr;
117 size_t len;
118 int rw;
119 {
120 boolean_t rv;
121 vaddr_t saddr, eaddr;
122 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
123
124 saddr = trunc_page((vaddr_t)addr);
125 eaddr = round_page((vaddr_t)addr + len);
126 vm_map_lock_read(kernel_map);
127 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
128 vm_map_unlock_read(kernel_map);
129
130 /*
131 * XXX there are still some things (e.g. the buffer cache) that
132 * are managed behind the VM system's back so even though an
133 * address is accessible in the mind of the VM system, there may
134 * not be physical pages where the VM thinks there is. This can
135 * lead to bogus allocation of pages in the kernel address space
136 * or worse, inconsistencies at the pmap level. We only worry
137 * about the buffer cache for now.
138 */
139 if (!readbuffers && rv && (eaddr > (vaddr_t)buffers &&
140 saddr < (vaddr_t)buffers + MAXBSIZE * nbuf))
141 rv = FALSE;
142 return(rv);
143 }
144
145 /*
146 * uvm_useracc: can the user access it?
147 *
148 * - called from physio() and sys___sysctl().
149 */
150
151 boolean_t
152 uvm_useracc(addr, len, rw)
153 caddr_t addr;
154 size_t len;
155 int rw;
156 {
157 struct vm_map *map;
158 boolean_t rv;
159 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
160
161 /* XXX curlwp */
162 map = &curproc->p_vmspace->vm_map;
163
164 vm_map_lock_read(map);
165 rv = uvm_map_checkprot(map, trunc_page((vaddr_t)addr),
166 round_page((vaddr_t)addr + len), prot);
167 vm_map_unlock_read(map);
168
169 return(rv);
170 }
171
172 #ifdef KGDB
173 /*
174 * Change protections on kernel pages from addr to addr+len
175 * (presumably so debugger can plant a breakpoint).
176 *
177 * We force the protection change at the pmap level. If we were
178 * to use vm_map_protect a change to allow writing would be lazily-
179 * applied meaning we would still take a protection fault, something
180 * we really don't want to do. It would also fragment the kernel
181 * map unnecessarily. We cannot use pmap_protect since it also won't
182 * enforce a write-enable request. Using pmap_enter is the only way
183 * we can ensure the change takes place properly.
184 */
185 void
186 uvm_chgkprot(addr, len, rw)
187 caddr_t addr;
188 size_t len;
189 int rw;
190 {
191 vm_prot_t prot;
192 paddr_t pa;
193 vaddr_t sva, eva;
194
195 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
196 eva = round_page((vaddr_t)addr + len);
197 for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) {
198 /*
199 * Extract physical address for the page.
200 */
201 if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE)
202 panic("chgkprot: invalid page");
203 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED);
204 }
205 pmap_update(pmap_kernel());
206 }
207 #endif
208
209 /*
210 * uvm_vslock: wire user memory for I/O
211 *
212 * - called from physio and sys___sysctl
213 * - XXXCDC: consider nuking this (or making it a macro?)
214 */
215
216 int
217 uvm_vslock(p, addr, len, access_type)
218 struct proc *p;
219 caddr_t addr;
220 size_t len;
221 vm_prot_t access_type;
222 {
223 struct vm_map *map;
224 vaddr_t start, end;
225 int error;
226
227 map = &p->p_vmspace->vm_map;
228 start = trunc_page((vaddr_t)addr);
229 end = round_page((vaddr_t)addr + len);
230 error = uvm_fault_wire(map, start, end, VM_FAULT_WIRE, access_type);
231 return error;
232 }
233
234 /*
235 * uvm_vsunlock: unwire user memory wired by uvm_vslock()
236 *
237 * - called from physio and sys___sysctl
238 * - XXXCDC: consider nuking this (or making it a macro?)
239 */
240
241 void
242 uvm_vsunlock(p, addr, len)
243 struct proc *p;
244 caddr_t addr;
245 size_t len;
246 {
247 uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr),
248 round_page((vaddr_t)addr + len));
249 }
250
251 /*
252 * uvm_proc_fork: fork a virtual address space
253 *
254 * - the address space is copied as per parent map's inherit values
255 */
256 void
257 uvm_proc_fork(p1, p2, shared)
258 struct proc *p1, *p2;
259 boolean_t shared;
260 {
261
262 if (shared == TRUE) {
263 p2->p_vmspace = NULL;
264 uvmspace_share(p1, p2);
265 } else {
266 p2->p_vmspace = uvmspace_fork(p1->p_vmspace);
267 }
268
269 cpu_proc_fork(p1, p2);
270 }
271
272
273 /*
274 * uvm_lwp_fork: fork a thread
275 *
276 * - a new "user" structure is allocated for the child process
277 * [filled in by MD layer...]
278 * - if specified, the child gets a new user stack described by
279 * stack and stacksize
280 * - NOTE: the kernel stack may be at a different location in the child
281 * process, and thus addresses of automatic variables may be invalid
282 * after cpu_lwp_fork returns in the child process. We do nothing here
283 * after cpu_lwp_fork returns.
284 * - XXXCDC: we need a way for this to return a failure value rather
285 * than just hang
286 */
287 void
288 uvm_lwp_fork(l1, l2, stack, stacksize, func, arg)
289 struct lwp *l1, *l2;
290 void *stack;
291 size_t stacksize;
292 void (*func) __P((void *));
293 void *arg;
294 {
295 struct user *up = l2->l_addr;
296 int error;
297
298 /*
299 * Wire down the U-area for the process, which contains the PCB
300 * and the kernel stack. Wired state is stored in p->p_flag's
301 * P_INMEM bit rather than in the vm_map_entry's wired count
302 * to prevent kernel_map fragmentation.
303 *
304 * Note the kernel stack gets read/write accesses right off
305 * the bat.
306 */
307 error = uvm_fault_wire(kernel_map, (vaddr_t)up, (vaddr_t)up + USPACE,
308 VM_FAULT_WIRE, VM_PROT_READ | VM_PROT_WRITE);
309 if (error)
310 panic("uvm_lwp_fork: uvm_fault_wire failed: %d", error);
311
312 /*
313 * cpu_lwp_fork() copy and update the pcb, and make the child ready
314 * to run. If this is a normal user fork, the child will exit
315 * directly to user mode via child_return() on its first time
316 * slice and will not return here. If this is a kernel thread,
317 * the specified entry point will be executed.
318 */
319 cpu_lwp_fork(l1, l2, stack, stacksize, func, arg);
320 }
321
322 /*
323 * uvm_exit: exit a virtual address space
324 *
325 * - the process passed to us is a dead (pre-zombie) process; we
326 * are running on a different context now (the reaper).
327 * - we must run in a separate thread because freeing the vmspace
328 * of the dead process may block.
329 */
330 void
331 uvm_proc_exit(p)
332 struct proc *p;
333 {
334 uvmspace_free(p->p_vmspace);
335 }
336
337 void
338 uvm_lwp_exit(l)
339 struct lwp *l;
340 {
341 vaddr_t va = (vaddr_t)l->l_addr;
342
343 uvm_km_free(kernel_map, va, USPACE);
344
345 l->l_flag &= ~L_INMEM;
346 l->l_addr = NULL;
347 }
348
349 /*
350 * uvm_init_limit: init per-process VM limits
351 *
352 * - called for process 0 and then inherited by all others.
353 */
354 void
355 uvm_init_limits(p)
356 struct proc *p;
357 {
358
359 /*
360 * Set up the initial limits on process VM. Set the maximum
361 * resident set size to be all of (reasonably) available memory.
362 * This causes any single, large process to start random page
363 * replacement once it fills memory.
364 */
365
366 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
367 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
368 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
369 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
370 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
371 }
372
373 #ifdef DEBUG
374 int enableswap = 1;
375 int swapdebug = 0;
376 #define SDB_FOLLOW 1
377 #define SDB_SWAPIN 2
378 #define SDB_SWAPOUT 4
379 #endif
380
381 /*
382 * uvm_swapin: swap in a process's u-area.
383 */
384
385 void
386 uvm_swapin(l)
387 struct lwp *l;
388 {
389 vaddr_t addr;
390 int s, error;
391
392 addr = (vaddr_t)l->l_addr;
393 /* make L_INMEM true */
394 error = uvm_fault_wire(kernel_map, addr, addr + USPACE, VM_FAULT_WIRE,
395 VM_PROT_READ | VM_PROT_WRITE);
396 if (error) {
397 panic("uvm_swapin: rewiring stack failed: %d", error);
398 }
399
400 /*
401 * Some architectures need to be notified when the user area has
402 * moved to new physical page(s) (e.g. see mips/mips/vm_machdep.c).
403 */
404 cpu_swapin(l);
405 SCHED_LOCK(s);
406 if (l->l_stat == LSRUN)
407 setrunqueue(l);
408 l->l_flag |= L_INMEM;
409 SCHED_UNLOCK(s);
410 l->l_swtime = 0;
411 ++uvmexp.swapins;
412 }
413
414 /*
415 * uvm_scheduler: process zero main loop
416 *
417 * - attempt to swapin every swaped-out, runnable process in order of
418 * priority.
419 * - if not enough memory, wake the pagedaemon and let it clear space.
420 */
421
422 void
423 uvm_scheduler()
424 {
425 struct lwp *l, *ll;
426 int pri;
427 int ppri;
428
429 loop:
430 #ifdef DEBUG
431 while (!enableswap)
432 tsleep(&proc0, PVM, "noswap", 0);
433 #endif
434 ll = NULL; /* process to choose */
435 ppri = INT_MIN; /* its priority */
436 proclist_lock_read();
437
438 LIST_FOREACH(l, &alllwp, l_list) {
439 /* is it a runnable swapped out process? */
440 if (l->l_stat == LSRUN && (l->l_flag & L_INMEM) == 0) {
441 pri = l->l_swtime + l->l_slptime -
442 (l->l_proc->p_nice - NZERO) * 8;
443 if (pri > ppri) { /* higher priority? remember it. */
444 ll = l;
445 ppri = pri;
446 }
447 }
448 }
449 /*
450 * XXXSMP: possible unlock/sleep race between here and the
451 * "scheduler" tsleep below..
452 */
453 proclist_unlock_read();
454
455 #ifdef DEBUG
456 if (swapdebug & SDB_FOLLOW)
457 printf("scheduler: running, procp %p pri %d\n", ll, ppri);
458 #endif
459 /*
460 * Nothing to do, back to sleep
461 */
462 if ((l = ll) == NULL) {
463 tsleep(&proc0, PVM, "scheduler", 0);
464 goto loop;
465 }
466
467 /*
468 * we have found swapped out process which we would like to bring
469 * back in.
470 *
471 * XXX: this part is really bogus cuz we could deadlock on memory
472 * despite our feeble check
473 */
474 if (uvmexp.free > atop(USPACE)) {
475 #ifdef DEBUG
476 if (swapdebug & SDB_SWAPIN)
477 printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
478 l->l_proc->p_pid, l->l_proc->p_comm, l->l_addr, ppri, uvmexp.free);
479 #endif
480 uvm_swapin(l);
481 goto loop;
482 }
483 /*
484 * not enough memory, jab the pageout daemon and wait til the coast
485 * is clear
486 */
487 #ifdef DEBUG
488 if (swapdebug & SDB_FOLLOW)
489 printf("scheduler: no room for pid %d(%s), free %d\n",
490 l->l_proc->p_pid, l->l_proc->p_comm, uvmexp.free);
491 #endif
492 uvm_wait("schedpwait");
493 #ifdef DEBUG
494 if (swapdebug & SDB_FOLLOW)
495 printf("scheduler: room again, free %d\n", uvmexp.free);
496 #endif
497 goto loop;
498 }
499
500 /*
501 * swappable: is LWP "l" swappable?
502 */
503
504 #define swappable(l) \
505 (((l)->l_flag & (L_INMEM)) && \
506 ((((l)->l_proc->p_flag) & (P_SYSTEM | P_WEXIT)) == 0) && \
507 (l)->l_holdcnt == 0)
508
509 /*
510 * swapout_threads: find threads that can be swapped and unwire their
511 * u-areas.
512 *
513 * - called by the pagedaemon
514 * - try and swap at least one processs
515 * - processes that are sleeping or stopped for maxslp or more seconds
516 * are swapped... otherwise the longest-sleeping or stopped process
517 * is swapped, otherwise the longest resident process...
518 */
519 void
520 uvm_swapout_threads()
521 {
522 struct lwp *l;
523 struct lwp *outl, *outl2;
524 int outpri, outpri2;
525 int didswap = 0;
526 extern int maxslp;
527 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
528
529 #ifdef DEBUG
530 if (!enableswap)
531 return;
532 #endif
533
534 /*
535 * outl/outpri : stop/sleep thread with largest sleeptime < maxslp
536 * outl2/outpri2: the longest resident thread (its swap time)
537 */
538 outl = outl2 = NULL;
539 outpri = outpri2 = 0;
540 proclist_lock_read();
541 LIST_FOREACH(l, &alllwp, l_list) {
542 if (!swappable(l))
543 continue;
544 switch (l->l_stat) {
545 case LSRUN:
546 case LSONPROC:
547 if (l->l_swtime > outpri2) {
548 outl2 = l;
549 outpri2 = l->l_swtime;
550 }
551 continue;
552
553 case LSSLEEP:
554 case LSSTOP:
555 if (l->l_slptime >= maxslp) {
556 uvm_swapout(l);
557 didswap++;
558 } else if (l->l_slptime > outpri) {
559 outl = l;
560 outpri = l->l_slptime;
561 }
562 continue;
563 }
564 }
565 proclist_unlock_read();
566
567 /*
568 * If we didn't get rid of any real duds, toss out the next most
569 * likely sleeping/stopped or running candidate. We only do this
570 * if we are real low on memory since we don't gain much by doing
571 * it (USPACE bytes).
572 */
573 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
574 if ((l = outl) == NULL)
575 l = outl2;
576 #ifdef DEBUG
577 if (swapdebug & SDB_SWAPOUT)
578 printf("swapout_threads: no duds, try procp %p\n", l);
579 #endif
580 if (l)
581 uvm_swapout(l);
582 }
583 }
584
585 /*
586 * uvm_swapout: swap out lwp "l"
587 *
588 * - currently "swapout" means "unwire U-area" and "pmap_collect()"
589 * the pmap.
590 * - XXXCDC: should deactivate all process' private anonymous memory
591 */
592
593 static void
594 uvm_swapout(l)
595 struct lwp *l;
596 {
597 vaddr_t addr;
598 int s;
599 struct proc *p = l->l_proc;
600
601 #ifdef DEBUG
602 if (swapdebug & SDB_SWAPOUT)
603 printf("swapout: lid %d.%d(%s)@%p, stat %x pri %d free %d\n",
604 p->p_pid, l->l_lid, p->p_comm, l->l_addr, l->l_stat,
605 l->l_slptime, uvmexp.free);
606 #endif
607
608 /*
609 * Do any machine-specific actions necessary before swapout.
610 * This can include saving floating point state, etc.
611 */
612 cpu_swapout(l);
613
614 /*
615 * Mark it as (potentially) swapped out.
616 */
617 SCHED_LOCK(s);
618 l->l_flag &= ~L_INMEM;
619 if (l->l_stat == LSRUN)
620 remrunqueue(l);
621 SCHED_UNLOCK(s);
622 l->l_swtime = 0;
623 p->p_stats->p_ru.ru_nswap++;
624 ++uvmexp.swapouts;
625
626 /*
627 * Unwire the to-be-swapped process's user struct and kernel stack.
628 */
629 addr = (vaddr_t)l->l_addr;
630 uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */
631 pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
632 }
633
634 /*
635 * uvm_coredump_walkmap: walk a process's map for the purpose of dumping
636 * a core file.
637 */
638
639 int
640 uvm_coredump_walkmap(p, vp, cred, func, cookie)
641 struct proc *p;
642 struct vnode *vp;
643 struct ucred *cred;
644 int (*func)(struct proc *, struct vnode *, struct ucred *,
645 struct uvm_coredump_state *);
646 void *cookie;
647 {
648 struct uvm_coredump_state state;
649 struct vmspace *vm = p->p_vmspace;
650 struct vm_map *map = &vm->vm_map;
651 struct vm_map_entry *entry;
652 vaddr_t maxstack;
653 int error;
654
655 maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
656
657 for (entry = map->header.next; entry != &map->header;
658 entry = entry->next) {
659 /* Should never happen for a user process. */
660 if (UVM_ET_ISSUBMAP(entry))
661 panic("uvm_coredump_walkmap: user process with "
662 "submap?");
663
664 state.cookie = cookie;
665 state.start = entry->start;
666 state.end = entry->end;
667 state.prot = entry->protection;
668 state.flags = 0;
669
670 if (state.start >= VM_MAXUSER_ADDRESS)
671 continue;
672
673 if (state.end > VM_MAXUSER_ADDRESS)
674 state.end = VM_MAXUSER_ADDRESS;
675
676 if (state.start >= (vaddr_t)vm->vm_maxsaddr) {
677 if (state.end <= maxstack)
678 continue;
679 if (state.start < maxstack)
680 state.start = maxstack;
681 state.flags |= UVM_COREDUMP_STACK;
682 }
683
684 if ((entry->protection & VM_PROT_WRITE) == 0)
685 state.flags |= UVM_COREDUMP_NODUMP;
686
687 if (entry->object.uvm_obj != NULL &&
688 entry->object.uvm_obj->pgops == &uvm_deviceops)
689 state.flags |= UVM_COREDUMP_NODUMP;
690
691 error = (*func)(p, vp, cred, &state);
692 if (error)
693 return (error);
694 }
695
696 return (0);
697 }
698