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