uvm_glue.c revision 1.110 1 /* $NetBSD: uvm_glue.c,v 1.110 2007/08/18 00:31:32 ad 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.110 2007/08/18 00:31:32 ad Exp $");
71
72 #include "opt_coredump.h"
73 #include "opt_kgdb.h"
74 #include "opt_kstack.h"
75 #include "opt_uvmhist.h"
76
77 /*
78 * uvm_glue.c: glue functions
79 */
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/proc.h>
84 #include <sys/resourcevar.h>
85 #include <sys/buf.h>
86 #include <sys/user.h>
87 #include <sys/syncobj.h>
88
89 #include <uvm/uvm.h>
90
91 #include <machine/cpu.h>
92
93 /*
94 * local prototypes
95 */
96
97 static void uvm_swapout(struct lwp *);
98
99 #define UVM_NUAREA_HIWAT 20
100 #define UVM_NUAREA_LOWAT 16
101
102 #define UAREA_NEXTFREE(uarea) (*(vaddr_t *)(UAREA_TO_USER(uarea)))
103
104 void uvm_uarea_free(vaddr_t);
105
106 /*
107 * XXXCDC: do these really belong here?
108 */
109
110 /*
111 * uvm_kernacc: can the kernel access a region of memory
112 *
113 * - used only by /dev/kmem driver (mem.c)
114 */
115
116 bool
117 uvm_kernacc(void *addr, size_t len, int rw)
118 {
119 bool rv;
120 vaddr_t saddr, eaddr;
121 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
122
123 saddr = trunc_page((vaddr_t)addr);
124 eaddr = round_page((vaddr_t)addr + len);
125 vm_map_lock_read(kernel_map);
126 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
127 vm_map_unlock_read(kernel_map);
128
129 return(rv);
130 }
131
132 #ifdef KGDB
133 /*
134 * Change protections on kernel pages from addr to addr+len
135 * (presumably so debugger can plant a breakpoint).
136 *
137 * We force the protection change at the pmap level. If we were
138 * to use vm_map_protect a change to allow writing would be lazily-
139 * applied meaning we would still take a protection fault, something
140 * we really don't want to do. It would also fragment the kernel
141 * map unnecessarily. We cannot use pmap_protect since it also won't
142 * enforce a write-enable request. Using pmap_enter is the only way
143 * we can ensure the change takes place properly.
144 */
145 void
146 uvm_chgkprot(void *addr, size_t len, int rw)
147 {
148 vm_prot_t prot;
149 paddr_t pa;
150 vaddr_t sva, eva;
151
152 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
153 eva = round_page((vaddr_t)addr + len);
154 for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) {
155 /*
156 * Extract physical address for the page.
157 */
158 if (pmap_extract(pmap_kernel(), sva, &pa) == false)
159 panic("chgkprot: invalid page");
160 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED);
161 }
162 pmap_update(pmap_kernel());
163 }
164 #endif
165
166 /*
167 * uvm_vslock: wire user memory for I/O
168 *
169 * - called from physio and sys___sysctl
170 * - XXXCDC: consider nuking this (or making it a macro?)
171 */
172
173 int
174 uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access_type)
175 {
176 struct vm_map *map;
177 vaddr_t start, end;
178 int error;
179
180 map = &vs->vm_map;
181 start = trunc_page((vaddr_t)addr);
182 end = round_page((vaddr_t)addr + len);
183 error = uvm_fault_wire(map, start, end, access_type, 0);
184 return error;
185 }
186
187 /*
188 * uvm_vsunlock: unwire user memory wired by uvm_vslock()
189 *
190 * - called from physio and sys___sysctl
191 * - XXXCDC: consider nuking this (or making it a macro?)
192 */
193
194 void
195 uvm_vsunlock(struct vmspace *vs, void *addr, size_t len)
196 {
197 uvm_fault_unwire(&vs->vm_map, trunc_page((vaddr_t)addr),
198 round_page((vaddr_t)addr + len));
199 }
200
201 /*
202 * uvm_proc_fork: fork a virtual address space
203 *
204 * - the address space is copied as per parent map's inherit values
205 */
206 void
207 uvm_proc_fork(struct proc *p1, struct proc *p2, bool shared)
208 {
209
210 if (shared == true) {
211 p2->p_vmspace = NULL;
212 uvmspace_share(p1, p2);
213 } else {
214 p2->p_vmspace = uvmspace_fork(p1->p_vmspace);
215 }
216
217 cpu_proc_fork(p1, p2);
218 }
219
220
221 /*
222 * uvm_lwp_fork: fork a thread
223 *
224 * - a new "user" structure is allocated for the child process
225 * [filled in by MD layer...]
226 * - if specified, the child gets a new user stack described by
227 * stack and stacksize
228 * - NOTE: the kernel stack may be at a different location in the child
229 * process, and thus addresses of automatic variables may be invalid
230 * after cpu_lwp_fork returns in the child process. We do nothing here
231 * after cpu_lwp_fork returns.
232 * - XXXCDC: we need a way for this to return a failure value rather
233 * than just hang
234 */
235 void
236 uvm_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
237 void (*func)(void *), void *arg)
238 {
239 int error;
240
241 /*
242 * Wire down the U-area for the process, which contains the PCB
243 * and the kernel stack. Wired state is stored in l->l_flag's
244 * L_INMEM bit rather than in the vm_map_entry's wired count
245 * to prevent kernel_map fragmentation. If we reused a cached U-area,
246 * L_INMEM will already be set and we don't need to do anything.
247 *
248 * Note the kernel stack gets read/write accesses right off the bat.
249 */
250
251 if ((l2->l_flag & LW_INMEM) == 0) {
252 vaddr_t uarea = USER_TO_UAREA(l2->l_addr);
253
254 error = uvm_fault_wire(kernel_map, uarea,
255 uarea + USPACE, VM_PROT_READ | VM_PROT_WRITE, 0);
256 if (error)
257 panic("uvm_lwp_fork: uvm_fault_wire failed: %d", error);
258 #ifdef PMAP_UAREA
259 /* Tell the pmap this is a u-area mapping */
260 PMAP_UAREA(uarea);
261 #endif
262 l2->l_flag |= LW_INMEM;
263 }
264
265 #ifdef KSTACK_CHECK_MAGIC
266 /*
267 * fill stack with magic number
268 */
269 kstack_setup_magic(l2);
270 #endif
271
272 /*
273 * cpu_lwp_fork() copy and update the pcb, and make the child ready
274 * to run. If this is a normal user fork, the child will exit
275 * directly to user mode via child_return() on its first time
276 * slice and will not return here. If this is a kernel thread,
277 * the specified entry point will be executed.
278 */
279 cpu_lwp_fork(l1, l2, stack, stacksize, func, arg);
280 }
281
282 /*
283 * uvm_cpu_attach: initialize per-CPU data structures.
284 */
285
286 void
287 uvm_cpu_attach(struct cpu_info *ci)
288 {
289
290 mutex_init(&ci->ci_data.cpu_uarea_lock, MUTEX_DEFAULT, IPL_NONE);
291 ci->ci_data.cpu_uarea_cnt = 0;
292 ci->ci_data.cpu_uarea_list = 0;
293 }
294
295 /*
296 * uvm_uarea_alloc: allocate a u-area
297 */
298
299 bool
300 uvm_uarea_alloc(vaddr_t *uaddrp)
301 {
302 struct cpu_info *ci;
303 vaddr_t uaddr;
304
305 #ifndef USPACE_ALIGN
306 #define USPACE_ALIGN 0
307 #endif
308
309 ci = curcpu();
310
311 if (ci->ci_data.cpu_uarea_cnt > 0) {
312 mutex_enter(&ci->ci_data.cpu_uarea_lock);
313 if (ci->ci_data.cpu_uarea_cnt == 0) {
314 mutex_exit(&ci->ci_data.cpu_uarea_lock);
315 } else {
316 uaddr = ci->ci_data.cpu_uarea_list;
317 ci->ci_data.cpu_uarea_list = UAREA_NEXTFREE(uaddr);
318 ci->ci_data.cpu_uarea_cnt--;
319 mutex_exit(&ci->ci_data.cpu_uarea_lock);
320 *uaddrp = uaddr;
321 return true;
322 }
323 }
324
325 *uaddrp = uvm_km_alloc(kernel_map, USPACE, USPACE_ALIGN,
326 UVM_KMF_PAGEABLE);
327 return false;
328 }
329
330 /*
331 * uvm_uarea_free: free a u-area
332 */
333
334 void
335 uvm_uarea_free(vaddr_t uaddr)
336 {
337 struct cpu_info *ci;
338
339 ci = curcpu();
340
341 mutex_enter(&ci->ci_data.cpu_uarea_lock);
342 UAREA_NEXTFREE(uaddr) = ci->ci_data.cpu_uarea_list;
343 ci->ci_data.cpu_uarea_list = uaddr;
344 ci->ci_data.cpu_uarea_cnt++;
345 mutex_exit(&ci->ci_data.cpu_uarea_lock);
346 }
347
348 /*
349 * uvm_uarea_drain: return memory of u-areas over limit
350 * back to system
351 *
352 * => if asked to drain as much as possible, drain all cpus.
353 * => if asked to drain to low water mark, drain local cpu only.
354 */
355
356 void
357 uvm_uarea_drain(bool empty)
358 {
359 CPU_INFO_ITERATOR cii;
360 struct cpu_info *ci;
361 vaddr_t uaddr, nuaddr;
362 int count;
363
364 if (empty) {
365 for (CPU_INFO_FOREACH(cii, ci)) {
366 mutex_enter(&ci->ci_data.cpu_uarea_lock);
367 count = ci->ci_data.cpu_uarea_cnt;
368 uaddr = ci->ci_data.cpu_uarea_list;
369 ci->ci_data.cpu_uarea_cnt = 0;
370 ci->ci_data.cpu_uarea_list = 0;
371 mutex_exit(&ci->ci_data.cpu_uarea_lock);
372
373 while (count != 0) {
374 nuaddr = UAREA_NEXTFREE(uaddr);
375 uvm_km_free(kernel_map, uaddr, USPACE,
376 UVM_KMF_PAGEABLE);
377 uaddr = nuaddr;
378 count--;
379 }
380 }
381 return;
382 }
383
384 ci = curcpu();
385 if (ci->ci_data.cpu_uarea_cnt > UVM_NUAREA_HIWAT) {
386 mutex_enter(&ci->ci_data.cpu_uarea_lock);
387 while (ci->ci_data.cpu_uarea_cnt > UVM_NUAREA_LOWAT) {
388 uaddr = ci->ci_data.cpu_uarea_list;
389 ci->ci_data.cpu_uarea_list = UAREA_NEXTFREE(uaddr);
390 ci->ci_data.cpu_uarea_cnt--;
391 mutex_exit(&ci->ci_data.cpu_uarea_lock);
392 uvm_km_free(kernel_map, uaddr, USPACE,
393 UVM_KMF_PAGEABLE);
394 mutex_enter(&ci->ci_data.cpu_uarea_lock);
395 }
396 mutex_exit(&ci->ci_data.cpu_uarea_lock);
397 }
398 }
399
400 /*
401 * uvm_exit: exit a virtual address space
402 *
403 * - the process passed to us is a dead (pre-zombie) process; we
404 * are running on a different context now (the reaper).
405 * - borrow proc0's address space because freeing the vmspace
406 * of the dead process may block.
407 */
408
409 void
410 uvm_proc_exit(struct proc *p)
411 {
412 struct lwp *l = curlwp; /* XXX */
413 struct vmspace *ovm;
414
415 KASSERT(p == l->l_proc);
416 ovm = p->p_vmspace;
417
418 /*
419 * borrow proc0's address space.
420 */
421 pmap_deactivate(l);
422 p->p_vmspace = proc0.p_vmspace;
423 pmap_activate(l);
424
425 uvmspace_free(ovm);
426 }
427
428 void
429 uvm_lwp_exit(struct lwp *l)
430 {
431 vaddr_t va = USER_TO_UAREA(l->l_addr);
432
433 l->l_flag &= ~LW_INMEM;
434 uvm_uarea_free(va);
435 l->l_addr = NULL;
436 }
437
438 /*
439 * uvm_init_limit: init per-process VM limits
440 *
441 * - called for process 0 and then inherited by all others.
442 */
443
444 void
445 uvm_init_limits(struct proc *p)
446 {
447
448 /*
449 * Set up the initial limits on process VM. Set the maximum
450 * resident set size to be all of (reasonably) available memory.
451 * This causes any single, large process to start random page
452 * replacement once it fills memory.
453 */
454
455 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
456 p->p_rlimit[RLIMIT_STACK].rlim_max = maxsmap;
457 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
458 p->p_rlimit[RLIMIT_DATA].rlim_max = maxdmap;
459 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
460 }
461
462 #ifdef DEBUG
463 int enableswap = 1;
464 int swapdebug = 0;
465 #define SDB_FOLLOW 1
466 #define SDB_SWAPIN 2
467 #define SDB_SWAPOUT 4
468 #endif
469
470 /*
471 * uvm_swapin: swap in an lwp's u-area.
472 *
473 * - must be called with the LWP's swap lock held.
474 * - naturally, must not be called with l == curlwp
475 */
476
477 void
478 uvm_swapin(struct lwp *l)
479 {
480 vaddr_t addr;
481 int error;
482
483 KASSERT(mutex_owned(&l->l_swaplock));
484 KASSERT(l != curlwp);
485
486 addr = USER_TO_UAREA(l->l_addr);
487 /* make L_INMEM true */
488 error = uvm_fault_wire(kernel_map, addr, addr + USPACE,
489 VM_PROT_READ | VM_PROT_WRITE, 0);
490 if (error) {
491 panic("uvm_swapin: rewiring stack failed: %d", error);
492 }
493
494 /*
495 * Some architectures need to be notified when the user area has
496 * moved to new physical page(s) (e.g. see mips/mips/vm_machdep.c).
497 */
498 cpu_swapin(l);
499 lwp_lock(l);
500 if (l->l_stat == LSRUN)
501 sched_enqueue(l, false);
502 l->l_flag |= LW_INMEM;
503 l->l_swtime = 0;
504 lwp_unlock(l);
505 ++uvmexp.swapins;
506 }
507
508 /*
509 * uvm_kick_scheduler: kick the scheduler into action if not running.
510 *
511 * - called when swapped out processes have been awoken.
512 */
513
514 void
515 uvm_kick_scheduler(void)
516 {
517
518 if (uvm.swap_running == false)
519 return;
520
521 mutex_enter(&uvm_scheduler_mutex);
522 uvm.scheduler_kicked = true;
523 cv_signal(&uvm.scheduler_cv);
524 mutex_exit(&uvm_scheduler_mutex);
525 }
526
527 /*
528 * uvm_scheduler: process zero main loop
529 *
530 * - attempt to swapin every swaped-out, runnable process in order of
531 * priority.
532 * - if not enough memory, wake the pagedaemon and let it clear space.
533 */
534
535 void
536 uvm_scheduler(void)
537 {
538 struct lwp *l, *ll;
539 int pri;
540 int ppri;
541
542 l = curlwp;
543 lwp_lock(l);
544 l->l_priority = PVM;
545 l->l_usrpri = PVM;
546 lwp_unlock(l);
547
548 for (;;) {
549 #ifdef DEBUG
550 mutex_enter(&uvm_scheduler_mutex);
551 while (!enableswap)
552 cv_wait(&uvm.scheduler_cv, &uvm_scheduler_mutex);
553 mutex_exit(&uvm_scheduler_mutex);
554 #endif
555 ll = NULL; /* process to choose */
556 ppri = INT_MIN; /* its priority */
557
558 mutex_enter(&proclist_lock);
559 LIST_FOREACH(l, &alllwp, l_list) {
560 /* is it a runnable swapped out process? */
561 if (l->l_stat == LSRUN && !(l->l_flag & LW_INMEM)) {
562 pri = l->l_swtime + l->l_slptime -
563 (l->l_proc->p_nice - NZERO) * 8;
564 if (pri > ppri) { /* higher priority? */
565 ll = l;
566 ppri = pri;
567 }
568 }
569 }
570 #ifdef DEBUG
571 if (swapdebug & SDB_FOLLOW)
572 printf("scheduler: running, procp %p pri %d\n", ll,
573 ppri);
574 #endif
575 /*
576 * Nothing to do, back to sleep
577 */
578 if ((l = ll) == NULL) {
579 mutex_exit(&proclist_lock);
580 mutex_enter(&uvm_scheduler_mutex);
581 if (uvm.scheduler_kicked == false)
582 cv_wait(&uvm.scheduler_cv,
583 &uvm_scheduler_mutex);
584 uvm.scheduler_kicked = false;
585 mutex_exit(&uvm_scheduler_mutex);
586 continue;
587 }
588
589 /*
590 * we have found swapped out process which we would like
591 * to bring back in.
592 *
593 * XXX: this part is really bogus cuz we could deadlock
594 * on memory despite our feeble check
595 */
596 if (uvmexp.free > atop(USPACE)) {
597 #ifdef DEBUG
598 if (swapdebug & SDB_SWAPIN)
599 printf("swapin: pid %d(%s)@%p, pri %d "
600 "free %d\n", l->l_proc->p_pid,
601 l->l_proc->p_comm, l->l_addr, ppri,
602 uvmexp.free);
603 #endif
604 mutex_enter(&l->l_swaplock);
605 mutex_exit(&proclist_lock);
606 uvm_swapin(l);
607 mutex_exit(&l->l_swaplock);
608 continue;
609 } else {
610 /*
611 * not enough memory, jab the pageout daemon and
612 * wait til the coast is clear
613 */
614 mutex_exit(&proclist_lock);
615 #ifdef DEBUG
616 if (swapdebug & SDB_FOLLOW)
617 printf("scheduler: no room for pid %d(%s),"
618 " free %d\n", l->l_proc->p_pid,
619 l->l_proc->p_comm, uvmexp.free);
620 #endif
621 uvm_wait("schedpwait");
622 #ifdef DEBUG
623 if (swapdebug & SDB_FOLLOW)
624 printf("scheduler: room again, free %d\n",
625 uvmexp.free);
626 #endif
627 }
628 }
629 }
630
631 /*
632 * swappable: is LWP "l" swappable?
633 */
634
635 static bool
636 swappable(struct lwp *l)
637 {
638
639 if ((l->l_flag & (LW_INMEM|LW_RUNNING|LW_SYSTEM|LW_WEXIT)) != LW_INMEM)
640 return false;
641 if (l->l_holdcnt != 0)
642 return false;
643 if (l->l_syncobj == &rw_syncobj || l->l_syncobj == &mutex_syncobj)
644 return false;
645 return true;
646 }
647
648 /*
649 * swapout_threads: find threads that can be swapped and unwire their
650 * u-areas.
651 *
652 * - called by the pagedaemon
653 * - try and swap at least one processs
654 * - processes that are sleeping or stopped for maxslp or more seconds
655 * are swapped... otherwise the longest-sleeping or stopped process
656 * is swapped, otherwise the longest resident process...
657 */
658
659 void
660 uvm_swapout_threads(void)
661 {
662 struct lwp *l;
663 struct lwp *outl, *outl2;
664 int outpri, outpri2;
665 int didswap = 0;
666 extern int maxslp;
667 bool gotit;
668
669 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
670
671 #ifdef DEBUG
672 if (!enableswap)
673 return;
674 #endif
675
676 /*
677 * outl/outpri : stop/sleep thread with largest sleeptime < maxslp
678 * outl2/outpri2: the longest resident thread (its swap time)
679 */
680 outl = outl2 = NULL;
681 outpri = outpri2 = 0;
682
683 restart:
684 mutex_enter(&proclist_lock);
685 LIST_FOREACH(l, &alllwp, l_list) {
686 KASSERT(l->l_proc != NULL);
687 if (!mutex_tryenter(&l->l_swaplock))
688 continue;
689 if (!swappable(l)) {
690 mutex_exit(&l->l_swaplock);
691 continue;
692 }
693 switch (l->l_stat) {
694 case LSONPROC:
695 break;
696
697 case LSRUN:
698 if (l->l_swtime > outpri2) {
699 outl2 = l;
700 outpri2 = l->l_swtime;
701 }
702 break;
703
704 case LSSLEEP:
705 case LSSTOP:
706 if (l->l_slptime >= maxslp) {
707 mutex_exit(&proclist_lock);
708 uvm_swapout(l);
709 /*
710 * Locking in the wrong direction -
711 * try to prevent the LWP from exiting.
712 */
713 gotit = mutex_tryenter(&proclist_lock);
714 mutex_exit(&l->l_swaplock);
715 didswap++;
716 if (!gotit)
717 goto restart;
718 continue;
719 } else if (l->l_slptime > outpri) {
720 outl = l;
721 outpri = l->l_slptime;
722 }
723 break;
724 }
725 mutex_exit(&l->l_swaplock);
726 }
727
728 /*
729 * If we didn't get rid of any real duds, toss out the next most
730 * likely sleeping/stopped or running candidate. We only do this
731 * if we are real low on memory since we don't gain much by doing
732 * it (USPACE bytes).
733 */
734 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
735 if ((l = outl) == NULL)
736 l = outl2;
737 #ifdef DEBUG
738 if (swapdebug & SDB_SWAPOUT)
739 printf("swapout_threads: no duds, try procp %p\n", l);
740 #endif
741 if (l) {
742 mutex_enter(&l->l_swaplock);
743 mutex_exit(&proclist_lock);
744 if (swappable(l))
745 uvm_swapout(l);
746 mutex_exit(&l->l_swaplock);
747 return;
748 }
749 }
750
751 mutex_exit(&proclist_lock);
752 }
753
754 /*
755 * uvm_swapout: swap out lwp "l"
756 *
757 * - currently "swapout" means "unwire U-area" and "pmap_collect()"
758 * the pmap.
759 * - must be called with l->l_swaplock held.
760 * - XXXCDC: should deactivate all process' private anonymous memory
761 */
762
763 static void
764 uvm_swapout(struct lwp *l)
765 {
766 vaddr_t addr;
767 struct proc *p = l->l_proc;
768
769 KASSERT(mutex_owned(&l->l_swaplock));
770
771 #ifdef DEBUG
772 if (swapdebug & SDB_SWAPOUT)
773 printf("swapout: lid %d.%d(%s)@%p, stat %x pri %d free %d\n",
774 p->p_pid, l->l_lid, p->p_comm, l->l_addr, l->l_stat,
775 l->l_slptime, uvmexp.free);
776 #endif
777
778 /*
779 * Mark it as (potentially) swapped out.
780 */
781 lwp_lock(l);
782 if (!swappable(l)) {
783 KDASSERT(l->l_cpu != curcpu());
784 lwp_unlock(l);
785 return;
786 }
787 l->l_flag &= ~LW_INMEM;
788 l->l_swtime = 0;
789 if (l->l_stat == LSRUN)
790 sched_dequeue(l);
791 lwp_unlock(l);
792 p->p_stats->p_ru.ru_nswap++; /* XXXSMP */
793 ++uvmexp.swapouts;
794
795 /*
796 * Do any machine-specific actions necessary before swapout.
797 * This can include saving floating point state, etc.
798 */
799 cpu_swapout(l);
800
801 /*
802 * Unwire the to-be-swapped process's user struct and kernel stack.
803 */
804 addr = USER_TO_UAREA(l->l_addr);
805 uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !L_INMEM */
806 pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
807 }
808
809 /*
810 * uvm_lwp_hold: prevent lwp "l" from being swapped out, and bring
811 * back into memory if it is currently swapped.
812 */
813
814 void
815 uvm_lwp_hold(struct lwp *l)
816 {
817
818 /* XXXSMP mutex_enter(&l->l_swaplock); */
819 if (l->l_holdcnt++ == 0 && (l->l_flag & LW_INMEM) == 0)
820 uvm_swapin(l);
821 /* XXXSMP mutex_exit(&l->l_swaplock); */
822 }
823
824 /*
825 * uvm_lwp_rele: release a hold on lwp "l". when the holdcount
826 * drops to zero, it's eligable to be swapped.
827 */
828
829 void
830 uvm_lwp_rele(struct lwp *l)
831 {
832
833 KASSERT(l->l_holdcnt != 0);
834
835 /* XXXSMP mutex_enter(&l->l_swaplock); */
836 l->l_holdcnt--;
837 /* XXXSMP mutex_exit(&l->l_swaplock); */
838 }
839
840 #ifdef COREDUMP
841 /*
842 * uvm_coredump_walkmap: walk a process's map for the purpose of dumping
843 * a core file.
844 */
845
846 int
847 uvm_coredump_walkmap(struct proc *p, void *iocookie,
848 int (*func)(struct proc *, void *, struct uvm_coredump_state *),
849 void *cookie)
850 {
851 struct uvm_coredump_state state;
852 struct vmspace *vm = p->p_vmspace;
853 struct vm_map *map = &vm->vm_map;
854 struct vm_map_entry *entry;
855 int error;
856
857 entry = NULL;
858 vm_map_lock_read(map);
859 state.end = 0;
860 for (;;) {
861 if (entry == NULL)
862 entry = map->header.next;
863 else if (!uvm_map_lookup_entry(map, state.end, &entry))
864 entry = entry->next;
865 if (entry == &map->header)
866 break;
867
868 state.cookie = cookie;
869 if (state.end > entry->start) {
870 state.start = state.end;
871 } else {
872 state.start = entry->start;
873 }
874 state.realend = entry->end;
875 state.end = entry->end;
876 state.prot = entry->protection;
877 state.flags = 0;
878
879 /*
880 * Dump the region unless one of the following is true:
881 *
882 * (1) the region has neither object nor amap behind it
883 * (ie. it has never been accessed).
884 *
885 * (2) the region has no amap and is read-only
886 * (eg. an executable text section).
887 *
888 * (3) the region's object is a device.
889 *
890 * (4) the region is unreadable by the process.
891 */
892
893 KASSERT(!UVM_ET_ISSUBMAP(entry));
894 KASSERT(state.start < VM_MAXUSER_ADDRESS);
895 KASSERT(state.end <= VM_MAXUSER_ADDRESS);
896 if (entry->object.uvm_obj == NULL &&
897 entry->aref.ar_amap == NULL) {
898 state.realend = state.start;
899 } else if ((entry->protection & VM_PROT_WRITE) == 0 &&
900 entry->aref.ar_amap == NULL) {
901 state.realend = state.start;
902 } else if (entry->object.uvm_obj != NULL &&
903 UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) {
904 state.realend = state.start;
905 } else if ((entry->protection & VM_PROT_READ) == 0) {
906 state.realend = state.start;
907 } else {
908 if (state.start >= (vaddr_t)vm->vm_maxsaddr)
909 state.flags |= UVM_COREDUMP_STACK;
910
911 /*
912 * If this an anonymous entry, only dump instantiated
913 * pages.
914 */
915 if (entry->object.uvm_obj == NULL) {
916 vaddr_t end;
917
918 amap_lock(entry->aref.ar_amap);
919 for (end = state.start;
920 end < state.end; end += PAGE_SIZE) {
921 struct vm_anon *anon;
922 anon = amap_lookup(&entry->aref,
923 end - entry->start);
924 /*
925 * If we have already encountered an
926 * uninstantiated page, stop at the
927 * first instantied page.
928 */
929 if (anon != NULL &&
930 state.realend != state.end) {
931 state.end = end;
932 break;
933 }
934
935 /*
936 * If this page is the first
937 * uninstantiated page, mark this as
938 * the real ending point. Continue to
939 * counting uninstantiated pages.
940 */
941 if (anon == NULL &&
942 state.realend == state.end) {
943 state.realend = end;
944 }
945 }
946 amap_unlock(entry->aref.ar_amap);
947 }
948 }
949
950
951 vm_map_unlock_read(map);
952 error = (*func)(p, iocookie, &state);
953 if (error)
954 return (error);
955 vm_map_lock_read(map);
956 }
957 vm_map_unlock_read(map);
958
959 return (0);
960 }
961 #endif /* COREDUMP */
962