uvm_glue.c revision 1.26 1 /* $NetBSD: uvm_glue.c,v 1.26 1999/06/17 15:47:22 thorpej 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 "opt_uvmhist.h"
70 #include "opt_sysv.h"
71
72 /*
73 * uvm_glue.c: glue functions
74 */
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/proc.h>
79 #include <sys/resourcevar.h>
80 #include <sys/buf.h>
81 #include <sys/user.h>
82 #ifdef SYSVSHM
83 #include <sys/shm.h>
84 #endif
85
86 #include <vm/vm.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_kern.h>
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 proc *));
99
100 /*
101 * XXXCDC: do these really belong here?
102 */
103
104 unsigned maxdmap = MAXDSIZ; /* kern_resource.c: RLIMIT_DATA max */
105 unsigned maxsmap = MAXSSIZ; /* kern_resource.c: RLIMIT_STACK max */
106
107 int readbuffers = 0; /* allow KGDB to read kern buffer pool */
108 /* XXX: see uvm_kernacc */
109
110
111 /*
112 * uvm_kernacc: can the kernel access a region of memory
113 *
114 * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c)
115 */
116
117 boolean_t
118 uvm_kernacc(addr, len, rw)
119 caddr_t addr;
120 size_t len;
121 int rw;
122 {
123 boolean_t rv;
124 vaddr_t saddr, eaddr;
125 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
126
127 saddr = trunc_page(addr);
128 eaddr = round_page(addr+len);
129 vm_map_lock_read(kernel_map);
130 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
131 vm_map_unlock_read(kernel_map);
132
133 /*
134 * XXX there are still some things (e.g. the buffer cache) that
135 * are managed behind the VM system's back so even though an
136 * address is accessible in the mind of the VM system, there may
137 * not be physical pages where the VM thinks there is. This can
138 * lead to bogus allocation of pages in the kernel address space
139 * or worse, inconsistencies at the pmap level. We only worry
140 * about the buffer cache for now.
141 */
142 if (!readbuffers && rv && (eaddr > (vaddr_t)buffers &&
143 saddr < (vaddr_t)buffers + MAXBSIZE * nbuf))
144 rv = FALSE;
145 return(rv);
146 }
147
148 /*
149 * uvm_useracc: can the user access it?
150 *
151 * - called from physio() and sys___sysctl().
152 */
153
154 boolean_t
155 uvm_useracc(addr, len, rw)
156 caddr_t addr;
157 size_t len;
158 int rw;
159 {
160 vm_map_t map;
161 boolean_t rv;
162 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
163
164 /* XXX curproc */
165 map = &curproc->p_vmspace->vm_map;
166
167 vm_map_lock_read(map);
168 rv = uvm_map_checkprot(map, trunc_page(addr), round_page(addr+len),
169 prot);
170 vm_map_unlock_read(map);
171
172 return(rv);
173 }
174
175 #ifdef KGDB
176 /*
177 * Change protections on kernel pages from addr to addr+len
178 * (presumably so debugger can plant a breakpoint).
179 *
180 * We force the protection change at the pmap level. If we were
181 * to use vm_map_protect a change to allow writing would be lazily-
182 * applied meaning we would still take a protection fault, something
183 * we really don't want to do. It would also fragment the kernel
184 * map unnecessarily. We cannot use pmap_protect since it also won't
185 * enforce a write-enable request. Using pmap_enter is the only way
186 * we can ensure the change takes place properly.
187 */
188 void
189 uvm_chgkprot(addr, len, rw)
190 register caddr_t addr;
191 size_t len;
192 int rw;
193 {
194 vm_prot_t prot;
195 paddr_t pa;
196 vaddr_t sva, eva;
197
198 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
199 eva = round_page(addr + len);
200 for (sva = trunc_page(addr); sva < eva; sva += PAGE_SIZE) {
201 /*
202 * Extract physical address for the page.
203 * We use a cheezy hack to differentiate physical
204 * page 0 from an invalid mapping, not that it
205 * really matters...
206 */
207 pa = pmap_extract(pmap_kernel(), sva|1);
208 if (pa == 0)
209 panic("chgkprot: invalid page");
210 pmap_enter(pmap_kernel(), sva, pa&~1, prot, TRUE, 0);
211 }
212 }
213 #endif
214
215 /*
216 * vslock: wire user memory for I/O
217 *
218 * - called from physio and sys___sysctl
219 * - XXXCDC: consider nuking this (or making it a macro?)
220 */
221
222 int
223 uvm_vslock(p, addr, len, access_type)
224 struct proc *p;
225 caddr_t addr;
226 size_t len;
227 vm_prot_t access_type;
228 {
229 vm_map_t map;
230 vaddr_t start, end;
231 int rv;
232
233 map = &p->p_vmspace->vm_map;
234 start = trunc_page(addr);
235 end = round_page(addr + len);
236
237 rv = uvm_fault_wire(map, start, end, access_type);
238
239 return (rv);
240 }
241
242 /*
243 * vslock: wire user memory for I/O
244 *
245 * - called from physio and sys___sysctl
246 * - XXXCDC: consider nuking this (or making it a macro?)
247 */
248
249 void
250 uvm_vsunlock(p, addr, len)
251 struct proc *p;
252 caddr_t addr;
253 size_t len;
254 {
255 uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page(addr),
256 round_page(addr+len));
257 }
258
259 /*
260 * uvm_fork: fork a virtual address space
261 *
262 * - the address space is copied as per parent map's inherit values
263 * - a new "user" structure is allocated for the child process
264 * [filled in by MD layer...]
265 * - if specified, the child gets a new user stack described by
266 * stack and stacksize
267 * - NOTE: the kernel stack may be at a different location in the child
268 * process, and thus addresses of automatic variables may be invalid
269 * after cpu_fork returns in the child process. We do nothing here
270 * after cpu_fork returns.
271 * - XXXCDC: we need a way for this to return a failure value rather
272 * than just hang
273 */
274 void
275 uvm_fork(p1, p2, shared, stack, stacksize)
276 struct proc *p1, *p2;
277 boolean_t shared;
278 void *stack;
279 size_t stacksize;
280 {
281 struct user *up = p2->p_addr;
282 int rv;
283
284 if (shared == TRUE)
285 uvmspace_share(p1, p2); /* share vmspace */
286 else
287 p2->p_vmspace = uvmspace_fork(p1->p_vmspace); /* fork vmspace */
288
289 /*
290 * Wire down the U-area for the process, which contains the PCB
291 * and the kernel stack. Wired state is stored in p->p_flag's
292 * P_INMEM bit rather than in the vm_map_entry's wired count
293 * to prevent kernel_map fragmentation.
294 *
295 * Note the kernel stack gets read/write accesses right off
296 * the bat.
297 */
298 rv = uvm_fault_wire(kernel_map, (vaddr_t)up,
299 (vaddr_t)up + USPACE, VM_PROT_READ | VM_PROT_WRITE);
300 if (rv != KERN_SUCCESS)
301 panic("uvm_fork: uvm_fault_wire failed: %d", rv);
302
303 /*
304 * p_stats currently points at a field in the user struct. Copy
305 * parts of p_stats, and zero out the rest.
306 */
307 p2->p_stats = &up->u_stats;
308 memset(&up->u_stats.pstat_startzero, 0,
309 (unsigned) ((caddr_t)&up->u_stats.pstat_endzero -
310 (caddr_t)&up->u_stats.pstat_startzero));
311 memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy,
312 ((caddr_t)&up->u_stats.pstat_endcopy -
313 (caddr_t)&up->u_stats.pstat_startcopy));
314
315 /*
316 * cpu_fork will copy and update the kernel stack and pcb, and make
317 * the child ready to run. The child will exit directly to user
318 * mode on its first time slice, and will not return here.
319 */
320 cpu_fork(p1, p2, stack, stacksize);
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_exit(p)
333 struct proc *p;
334 {
335
336 uvmspace_free(p->p_vmspace);
337 uvm_km_free(kernel_map, (vaddr_t)p->p_addr, USPACE);
338 }
339
340 /*
341 * uvm_init_limit: init per-process VM limits
342 *
343 * - called for process 0 and then inherited by all others.
344 */
345 void
346 uvm_init_limits(p)
347 struct proc *p;
348 {
349
350 /*
351 * Set up the initial limits on process VM. Set the maximum
352 * resident set size to be all of (reasonably) available memory.
353 * This causes any single, large process to start random page
354 * replacement once it fills memory.
355 */
356
357 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
358 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
359 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
360 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
361 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
362 }
363
364 #ifdef DEBUG
365 int enableswap = 1;
366 int swapdebug = 0;
367 #define SDB_FOLLOW 1
368 #define SDB_SWAPIN 2
369 #define SDB_SWAPOUT 4
370 #endif
371
372 /*
373 * uvm_swapin: swap in a process's u-area.
374 */
375
376 void
377 uvm_swapin(p)
378 struct proc *p;
379 {
380 vaddr_t addr;
381 int s;
382
383 addr = (vaddr_t)p->p_addr;
384 /* make P_INMEM true */
385 uvm_fault_wire(kernel_map, addr, addr + USPACE,
386 VM_PROT_READ | VM_PROT_WRITE);
387
388 /*
389 * Some architectures need to be notified when the user area has
390 * moved to new physical page(s) (e.g. see mips/mips/vm_machdep.c).
391 */
392 cpu_swapin(p);
393 s = splstatclock();
394 if (p->p_stat == SRUN)
395 setrunqueue(p);
396 p->p_flag |= P_INMEM;
397 splx(s);
398 p->p_swtime = 0;
399 ++uvmexp.swapins;
400 }
401
402 /*
403 * uvm_scheduler: process zero main loop
404 *
405 * - attempt to swapin every swaped-out, runnable process in order of
406 * priority.
407 * - if not enough memory, wake the pagedaemon and let it clear space.
408 */
409
410 void
411 uvm_scheduler()
412 {
413 register struct proc *p;
414 register int pri;
415 struct proc *pp;
416 int ppri;
417 UVMHIST_FUNC("uvm_scheduler"); UVMHIST_CALLED(maphist);
418
419 loop:
420 #ifdef DEBUG
421 while (!enableswap)
422 tsleep((caddr_t)&proc0, PVM, "noswap", 0);
423 #endif
424 pp = NULL; /* process to choose */
425 ppri = INT_MIN; /* its priority */
426 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
427
428 /* is it a runnable swapped out process? */
429 if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) {
430 pri = p->p_swtime + p->p_slptime -
431 (p->p_nice - NZERO) * 8;
432 if (pri > ppri) { /* higher priority? remember it. */
433 pp = p;
434 ppri = pri;
435 }
436 }
437 }
438
439 #ifdef DEBUG
440 if (swapdebug & SDB_FOLLOW)
441 printf("scheduler: running, procp %p pri %d\n", pp, ppri);
442 #endif
443 /*
444 * Nothing to do, back to sleep
445 */
446 if ((p = pp) == NULL) {
447 tsleep((caddr_t)&proc0, PVM, "scheduler", 0);
448 goto loop;
449 }
450
451 /*
452 * we have found swapped out process which we would like to bring
453 * back in.
454 *
455 * XXX: this part is really bogus cuz we could deadlock on memory
456 * despite our feeble check
457 */
458 if (uvmexp.free > atop(USPACE)) {
459 #ifdef DEBUG
460 if (swapdebug & SDB_SWAPIN)
461 printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
462 p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free);
463 #endif
464 uvm_swapin(p);
465 goto loop;
466 }
467 /*
468 * not enough memory, jab the pageout daemon and wait til the coast
469 * is clear
470 */
471 #ifdef DEBUG
472 if (swapdebug & SDB_FOLLOW)
473 printf("scheduler: no room for pid %d(%s), free %d\n",
474 p->p_pid, p->p_comm, uvmexp.free);
475 #endif
476 (void) splhigh();
477 uvm_wait("schedpwait");
478 (void) spl0();
479 #ifdef DEBUG
480 if (swapdebug & SDB_FOLLOW)
481 printf("scheduler: room again, free %d\n", uvmexp.free);
482 #endif
483 goto loop;
484 }
485
486 /*
487 * swappable: is process "p" swappable?
488 */
489
490 #define swappable(p) \
491 (((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM && \
492 (p)->p_holdcnt == 0)
493
494 /*
495 * swapout_threads: find threads that can be swapped and unwire their
496 * u-areas.
497 *
498 * - called by the pagedaemon
499 * - try and swap at least one processs
500 * - processes that are sleeping or stopped for maxslp or more seconds
501 * are swapped... otherwise the longest-sleeping or stopped process
502 * is swapped, otherwise the longest resident process...
503 */
504 void
505 uvm_swapout_threads()
506 {
507 register struct proc *p;
508 struct proc *outp, *outp2;
509 int outpri, outpri2;
510 int didswap = 0;
511 extern int maxslp;
512 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
513
514 #ifdef DEBUG
515 if (!enableswap)
516 return;
517 #endif
518
519 /*
520 * outp/outpri : stop/sleep process with largest sleeptime < maxslp
521 * outp2/outpri2: the longest resident process (its swap time)
522 */
523 outp = outp2 = NULL;
524 outpri = outpri2 = 0;
525 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
526 if (!swappable(p))
527 continue;
528 switch (p->p_stat) {
529 case SRUN:
530 if (p->p_swtime > outpri2) {
531 outp2 = p;
532 outpri2 = p->p_swtime;
533 }
534 continue;
535
536 case SSLEEP:
537 case SSTOP:
538 if (p->p_slptime >= maxslp) {
539 uvm_swapout(p); /* zap! */
540 didswap++;
541 } else if (p->p_slptime > outpri) {
542 outp = p;
543 outpri = p->p_slptime;
544 }
545 continue;
546 }
547 }
548
549 /*
550 * If we didn't get rid of any real duds, toss out the next most
551 * likely sleeping/stopped or running candidate. We only do this
552 * if we are real low on memory since we don't gain much by doing
553 * it (USPACE bytes).
554 */
555 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
556 if ((p = outp) == NULL)
557 p = outp2;
558 #ifdef DEBUG
559 if (swapdebug & SDB_SWAPOUT)
560 printf("swapout_threads: no duds, try procp %p\n", p);
561 #endif
562 if (p)
563 uvm_swapout(p);
564 }
565 }
566
567 /*
568 * uvm_swapout: swap out process "p"
569 *
570 * - currently "swapout" means "unwire U-area" and "pmap_collect()"
571 * the pmap.
572 * - XXXCDC: should deactivate all process' private anonymous memory
573 */
574
575 static void
576 uvm_swapout(p)
577 register struct proc *p;
578 {
579 vaddr_t addr;
580 int s;
581
582 #ifdef DEBUG
583 if (swapdebug & SDB_SWAPOUT)
584 printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n",
585 p->p_pid, p->p_comm, p->p_addr, p->p_stat,
586 p->p_slptime, uvmexp.free);
587 #endif
588
589 /*
590 * Do any machine-specific actions necessary before swapout.
591 * This can include saving floating point state, etc.
592 */
593 cpu_swapout(p);
594
595 /*
596 * Unwire the to-be-swapped process's user struct and kernel stack.
597 */
598 addr = (vaddr_t)p->p_addr;
599 uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */
600 pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
601
602 /*
603 * Mark it as (potentially) swapped out.
604 */
605 s = splstatclock();
606 p->p_flag &= ~P_INMEM;
607 if (p->p_stat == SRUN)
608 remrunqueue(p);
609 splx(s);
610 p->p_swtime = 0;
611 ++uvmexp.swapouts;
612 }
613
614