uvm_glue.c revision 1.60 1 1.60 chs /* $NetBSD: uvm_glue.c,v 1.60 2002/09/22 07:20:32 chs Exp $ */
2 1.1 mrg
3 1.48 chs /*
4 1.1 mrg * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.48 chs * Copyright (c) 1991, 1993, The Regents of the University of California.
6 1.1 mrg *
7 1.1 mrg * All rights reserved.
8 1.1 mrg *
9 1.1 mrg * This code is derived from software contributed to Berkeley by
10 1.1 mrg * The Mach Operating System project at Carnegie-Mellon University.
11 1.1 mrg *
12 1.1 mrg * Redistribution and use in source and binary forms, with or without
13 1.1 mrg * modification, are permitted provided that the following conditions
14 1.1 mrg * are met:
15 1.1 mrg * 1. Redistributions of source code must retain the above copyright
16 1.1 mrg * notice, this list of conditions and the following disclaimer.
17 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 mrg * notice, this list of conditions and the following disclaimer in the
19 1.1 mrg * documentation and/or other materials provided with the distribution.
20 1.1 mrg * 3. All advertising materials mentioning features or use of this software
21 1.1 mrg * must display the following acknowledgement:
22 1.1 mrg * This product includes software developed by Charles D. Cranor,
23 1.48 chs * Washington University, the University of California, Berkeley and
24 1.1 mrg * its contributors.
25 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
26 1.1 mrg * may be used to endorse or promote products derived from this software
27 1.1 mrg * without specific prior written permission.
28 1.1 mrg *
29 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 1.1 mrg * SUCH DAMAGE.
40 1.1 mrg *
41 1.1 mrg * @(#)vm_glue.c 8.6 (Berkeley) 1/5/94
42 1.4 mrg * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp
43 1.1 mrg *
44 1.1 mrg *
45 1.1 mrg * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 1.1 mrg * All rights reserved.
47 1.48 chs *
48 1.1 mrg * Permission to use, copy, modify and distribute this software and
49 1.1 mrg * its documentation is hereby granted, provided that both the copyright
50 1.1 mrg * notice and this permission notice appear in all copies of the
51 1.1 mrg * software, derivative works or modified versions, and any portions
52 1.1 mrg * thereof, and that both notices appear in supporting documentation.
53 1.48 chs *
54 1.48 chs * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 1.48 chs * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56 1.1 mrg * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57 1.48 chs *
58 1.1 mrg * Carnegie Mellon requests users of this software to return to
59 1.1 mrg *
60 1.1 mrg * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
61 1.1 mrg * School of Computer Science
62 1.1 mrg * Carnegie Mellon University
63 1.1 mrg * Pittsburgh PA 15213-3890
64 1.1 mrg *
65 1.1 mrg * any improvements or extensions that they make and grant Carnegie the
66 1.1 mrg * rights to redistribute these changes.
67 1.1 mrg */
68 1.55 lukem
69 1.55 lukem #include <sys/cdefs.h>
70 1.60 chs __KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.60 2002/09/22 07:20:32 chs Exp $");
71 1.1 mrg
72 1.49 lukem #include "opt_kgdb.h"
73 1.59 yamt #include "opt_kstack.h"
74 1.49 lukem #include "opt_sysv.h"
75 1.5 mrg #include "opt_uvmhist.h"
76 1.5 mrg
77 1.1 mrg /*
78 1.1 mrg * uvm_glue.c: glue functions
79 1.1 mrg */
80 1.1 mrg
81 1.1 mrg #include <sys/param.h>
82 1.1 mrg #include <sys/systm.h>
83 1.1 mrg #include <sys/proc.h>
84 1.1 mrg #include <sys/resourcevar.h>
85 1.1 mrg #include <sys/buf.h>
86 1.1 mrg #include <sys/user.h>
87 1.1 mrg #ifdef SYSVSHM
88 1.1 mrg #include <sys/shm.h>
89 1.1 mrg #endif
90 1.1 mrg
91 1.1 mrg #include <uvm/uvm.h>
92 1.1 mrg
93 1.1 mrg #include <machine/cpu.h>
94 1.1 mrg
95 1.1 mrg /*
96 1.1 mrg * local prototypes
97 1.1 mrg */
98 1.1 mrg
99 1.1 mrg static void uvm_swapout __P((struct proc *));
100 1.1 mrg
101 1.60 chs #define UVM_NUAREA_MAX 16
102 1.60 chs void *uvm_uareas;
103 1.60 chs int uvm_nuarea;
104 1.60 chs
105 1.1 mrg /*
106 1.1 mrg * XXXCDC: do these really belong here?
107 1.1 mrg */
108 1.1 mrg
109 1.1 mrg int readbuffers = 0; /* allow KGDB to read kern buffer pool */
110 1.1 mrg /* XXX: see uvm_kernacc */
111 1.1 mrg
112 1.28 thorpej
113 1.28 thorpej /*
114 1.1 mrg * uvm_kernacc: can the kernel access a region of memory
115 1.1 mrg *
116 1.1 mrg * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c)
117 1.1 mrg */
118 1.1 mrg
119 1.6 mrg boolean_t
120 1.6 mrg uvm_kernacc(addr, len, rw)
121 1.6 mrg caddr_t addr;
122 1.11 kleink size_t len;
123 1.11 kleink int rw;
124 1.6 mrg {
125 1.6 mrg boolean_t rv;
126 1.13 eeh vaddr_t saddr, eaddr;
127 1.6 mrg vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
128 1.6 mrg
129 1.31 kleink saddr = trunc_page((vaddr_t)addr);
130 1.43 chs eaddr = round_page((vaddr_t)addr + len);
131 1.6 mrg vm_map_lock_read(kernel_map);
132 1.6 mrg rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
133 1.6 mrg vm_map_unlock_read(kernel_map);
134 1.6 mrg
135 1.6 mrg /*
136 1.6 mrg * XXX there are still some things (e.g. the buffer cache) that
137 1.6 mrg * are managed behind the VM system's back so even though an
138 1.6 mrg * address is accessible in the mind of the VM system, there may
139 1.6 mrg * not be physical pages where the VM thinks there is. This can
140 1.6 mrg * lead to bogus allocation of pages in the kernel address space
141 1.6 mrg * or worse, inconsistencies at the pmap level. We only worry
142 1.6 mrg * about the buffer cache for now.
143 1.6 mrg */
144 1.13 eeh if (!readbuffers && rv && (eaddr > (vaddr_t)buffers &&
145 1.13 eeh saddr < (vaddr_t)buffers + MAXBSIZE * nbuf))
146 1.6 mrg rv = FALSE;
147 1.6 mrg return(rv);
148 1.1 mrg }
149 1.1 mrg
150 1.1 mrg /*
151 1.1 mrg * uvm_useracc: can the user access it?
152 1.1 mrg *
153 1.1 mrg * - called from physio() and sys___sysctl().
154 1.1 mrg */
155 1.1 mrg
156 1.6 mrg boolean_t
157 1.6 mrg uvm_useracc(addr, len, rw)
158 1.6 mrg caddr_t addr;
159 1.11 kleink size_t len;
160 1.11 kleink int rw;
161 1.1 mrg {
162 1.50 chs struct vm_map *map;
163 1.6 mrg boolean_t rv;
164 1.6 mrg vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
165 1.1 mrg
166 1.25 thorpej /* XXX curproc */
167 1.25 thorpej map = &curproc->p_vmspace->vm_map;
168 1.25 thorpej
169 1.25 thorpej vm_map_lock_read(map);
170 1.31 kleink rv = uvm_map_checkprot(map, trunc_page((vaddr_t)addr),
171 1.43 chs round_page((vaddr_t)addr + len), prot);
172 1.25 thorpej vm_map_unlock_read(map);
173 1.25 thorpej
174 1.6 mrg return(rv);
175 1.1 mrg }
176 1.1 mrg
177 1.1 mrg #ifdef KGDB
178 1.1 mrg /*
179 1.1 mrg * Change protections on kernel pages from addr to addr+len
180 1.1 mrg * (presumably so debugger can plant a breakpoint).
181 1.1 mrg *
182 1.1 mrg * We force the protection change at the pmap level. If we were
183 1.1 mrg * to use vm_map_protect a change to allow writing would be lazily-
184 1.1 mrg * applied meaning we would still take a protection fault, something
185 1.1 mrg * we really don't want to do. It would also fragment the kernel
186 1.1 mrg * map unnecessarily. We cannot use pmap_protect since it also won't
187 1.1 mrg * enforce a write-enable request. Using pmap_enter is the only way
188 1.1 mrg * we can ensure the change takes place properly.
189 1.1 mrg */
190 1.6 mrg void
191 1.6 mrg uvm_chgkprot(addr, len, rw)
192 1.32 augustss caddr_t addr;
193 1.11 kleink size_t len;
194 1.11 kleink int rw;
195 1.6 mrg {
196 1.6 mrg vm_prot_t prot;
197 1.13 eeh paddr_t pa;
198 1.13 eeh vaddr_t sva, eva;
199 1.6 mrg
200 1.6 mrg prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
201 1.31 kleink eva = round_page((vaddr_t)addr + len);
202 1.31 kleink for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) {
203 1.6 mrg /*
204 1.6 mrg * Extract physical address for the page.
205 1.6 mrg */
206 1.27 thorpej if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE)
207 1.6 mrg panic("chgkprot: invalid page");
208 1.30 thorpej pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED);
209 1.6 mrg }
210 1.51 chris pmap_update(pmap_kernel());
211 1.1 mrg }
212 1.1 mrg #endif
213 1.1 mrg
214 1.1 mrg /*
215 1.52 chs * uvm_vslock: wire user memory for I/O
216 1.1 mrg *
217 1.1 mrg * - called from physio and sys___sysctl
218 1.1 mrg * - XXXCDC: consider nuking this (or making it a macro?)
219 1.1 mrg */
220 1.1 mrg
221 1.26 thorpej int
222 1.22 thorpej uvm_vslock(p, addr, len, access_type)
223 1.9 thorpej struct proc *p;
224 1.6 mrg caddr_t addr;
225 1.11 kleink size_t len;
226 1.22 thorpej vm_prot_t access_type;
227 1.1 mrg {
228 1.50 chs struct vm_map *map;
229 1.26 thorpej vaddr_t start, end;
230 1.45 chs int error;
231 1.26 thorpej
232 1.26 thorpej map = &p->p_vmspace->vm_map;
233 1.31 kleink start = trunc_page((vaddr_t)addr);
234 1.31 kleink end = round_page((vaddr_t)addr + len);
235 1.57 chs error = uvm_fault_wire(map, start, end, VM_FAULT_WIRE, access_type);
236 1.45 chs return error;
237 1.1 mrg }
238 1.1 mrg
239 1.1 mrg /*
240 1.52 chs * uvm_vsunlock: unwire user memory wired by uvm_vslock()
241 1.1 mrg *
242 1.1 mrg * - called from physio and sys___sysctl
243 1.1 mrg * - XXXCDC: consider nuking this (or making it a macro?)
244 1.1 mrg */
245 1.1 mrg
246 1.6 mrg void
247 1.9 thorpej uvm_vsunlock(p, addr, len)
248 1.9 thorpej struct proc *p;
249 1.6 mrg caddr_t addr;
250 1.11 kleink size_t len;
251 1.1 mrg {
252 1.43 chs uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr),
253 1.43 chs round_page((vaddr_t)addr + len));
254 1.1 mrg }
255 1.1 mrg
256 1.1 mrg /*
257 1.1 mrg * uvm_fork: fork a virtual address space
258 1.1 mrg *
259 1.1 mrg * - the address space is copied as per parent map's inherit values
260 1.1 mrg * - a new "user" structure is allocated for the child process
261 1.1 mrg * [filled in by MD layer...]
262 1.20 thorpej * - if specified, the child gets a new user stack described by
263 1.20 thorpej * stack and stacksize
264 1.1 mrg * - NOTE: the kernel stack may be at a different location in the child
265 1.1 mrg * process, and thus addresses of automatic variables may be invalid
266 1.1 mrg * after cpu_fork returns in the child process. We do nothing here
267 1.1 mrg * after cpu_fork returns.
268 1.1 mrg * - XXXCDC: we need a way for this to return a failure value rather
269 1.1 mrg * than just hang
270 1.1 mrg */
271 1.6 mrg void
272 1.34 thorpej uvm_fork(p1, p2, shared, stack, stacksize, func, arg)
273 1.6 mrg struct proc *p1, *p2;
274 1.6 mrg boolean_t shared;
275 1.20 thorpej void *stack;
276 1.20 thorpej size_t stacksize;
277 1.34 thorpej void (*func) __P((void *));
278 1.34 thorpej void *arg;
279 1.6 mrg {
280 1.7 thorpej struct user *up = p2->p_addr;
281 1.45 chs int error;
282 1.6 mrg
283 1.42 thorpej if (shared == TRUE) {
284 1.42 thorpej p2->p_vmspace = NULL;
285 1.52 chs uvmspace_share(p1, p2);
286 1.42 thorpej } else
287 1.52 chs p2->p_vmspace = uvmspace_fork(p1->p_vmspace);
288 1.1 mrg
289 1.6 mrg /*
290 1.7 thorpej * Wire down the U-area for the process, which contains the PCB
291 1.7 thorpej * and the kernel stack. Wired state is stored in p->p_flag's
292 1.7 thorpej * P_INMEM bit rather than in the vm_map_entry's wired count
293 1.7 thorpej * to prevent kernel_map fragmentation.
294 1.21 thorpej *
295 1.21 thorpej * Note the kernel stack gets read/write accesses right off
296 1.21 thorpej * the bat.
297 1.6 mrg */
298 1.57 chs error = uvm_fault_wire(kernel_map, (vaddr_t)up, (vaddr_t)up + USPACE,
299 1.57 chs VM_FAULT_WIRE, VM_PROT_READ | VM_PROT_WRITE);
300 1.45 chs if (error)
301 1.45 chs panic("uvm_fork: uvm_fault_wire failed: %d", error);
302 1.59 yamt
303 1.59 yamt #ifdef KSTACK_CHECK_MAGIC
304 1.59 yamt /*
305 1.59 yamt * fill stack with magic number
306 1.59 yamt */
307 1.59 yamt kstack_setup_magic(p2);
308 1.59 yamt #endif
309 1.6 mrg
310 1.6 mrg /*
311 1.19 thorpej * p_stats currently points at a field in the user struct. Copy
312 1.19 thorpej * parts of p_stats, and zero out the rest.
313 1.6 mrg */
314 1.6 mrg p2->p_stats = &up->u_stats;
315 1.12 perry memset(&up->u_stats.pstat_startzero, 0,
316 1.43 chs ((caddr_t)&up->u_stats.pstat_endzero -
317 1.43 chs (caddr_t)&up->u_stats.pstat_startzero));
318 1.12 perry memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy,
319 1.43 chs ((caddr_t)&up->u_stats.pstat_endcopy -
320 1.43 chs (caddr_t)&up->u_stats.pstat_startcopy));
321 1.48 chs
322 1.6 mrg /*
323 1.34 thorpej * cpu_fork() copy and update the pcb, and make the child ready
324 1.34 thorpej * to run. If this is a normal user fork, the child will exit
325 1.34 thorpej * directly to user mode via child_return() on its first time
326 1.34 thorpej * slice and will not return here. If this is a kernel thread,
327 1.34 thorpej * the specified entry point will be executed.
328 1.6 mrg */
329 1.34 thorpej cpu_fork(p1, p2, stack, stacksize, func, arg);
330 1.14 thorpej }
331 1.14 thorpej
332 1.14 thorpej /*
333 1.14 thorpej * uvm_exit: exit a virtual address space
334 1.14 thorpej *
335 1.14 thorpej * - the process passed to us is a dead (pre-zombie) process; we
336 1.14 thorpej * are running on a different context now (the reaper).
337 1.14 thorpej * - we must run in a separate thread because freeing the vmspace
338 1.14 thorpej * of the dead process may block.
339 1.14 thorpej */
340 1.60 chs
341 1.14 thorpej void
342 1.14 thorpej uvm_exit(p)
343 1.14 thorpej struct proc *p;
344 1.14 thorpej {
345 1.43 chs vaddr_t va = (vaddr_t)p->p_addr;
346 1.14 thorpej
347 1.14 thorpej uvmspace_free(p->p_vmspace);
348 1.43 chs p->p_flag &= ~P_INMEM;
349 1.60 chs uvm_uarea_free(va);
350 1.36 simonb p->p_addr = NULL;
351 1.1 mrg }
352 1.1 mrg
353 1.1 mrg /*
354 1.60 chs * uvm_uarea_alloc: allocate a u-area
355 1.60 chs */
356 1.60 chs
357 1.60 chs vaddr_t
358 1.60 chs uvm_uarea_alloc(void)
359 1.60 chs {
360 1.60 chs vaddr_t uaddr;
361 1.60 chs
362 1.60 chs #ifndef USPACE_ALIGN
363 1.60 chs #define USPACE_ALIGN 0
364 1.60 chs #endif
365 1.60 chs
366 1.60 chs uaddr = (vaddr_t)uvm_uareas;
367 1.60 chs if (uaddr) {
368 1.60 chs uvm_uareas = *(void **)uvm_uareas;
369 1.60 chs uvm_nuarea--;
370 1.60 chs } else {
371 1.60 chs uaddr = uvm_km_valloc_align(kernel_map, USPACE, USPACE_ALIGN);
372 1.60 chs }
373 1.60 chs return uaddr;
374 1.60 chs }
375 1.60 chs
376 1.60 chs /*
377 1.60 chs * uvm_uarea_free: free a u-area
378 1.60 chs */
379 1.60 chs
380 1.60 chs void
381 1.60 chs uvm_uarea_free(vaddr_t uaddr)
382 1.60 chs {
383 1.60 chs
384 1.60 chs if (uvm_nuarea < UVM_NUAREA_MAX) {
385 1.60 chs *(void **)uaddr = uvm_uareas;
386 1.60 chs uvm_uareas = (void *)uaddr;
387 1.60 chs uvm_nuarea++;
388 1.60 chs } else {
389 1.60 chs uvm_km_free(kernel_map, uaddr, USPACE);
390 1.60 chs }
391 1.60 chs }
392 1.60 chs
393 1.60 chs /*
394 1.1 mrg * uvm_init_limit: init per-process VM limits
395 1.1 mrg *
396 1.1 mrg * - called for process 0 and then inherited by all others.
397 1.1 mrg */
398 1.60 chs
399 1.6 mrg void
400 1.6 mrg uvm_init_limits(p)
401 1.6 mrg struct proc *p;
402 1.6 mrg {
403 1.6 mrg
404 1.6 mrg /*
405 1.6 mrg * Set up the initial limits on process VM. Set the maximum
406 1.6 mrg * resident set size to be all of (reasonably) available memory.
407 1.6 mrg * This causes any single, large process to start random page
408 1.6 mrg * replacement once it fills memory.
409 1.6 mrg */
410 1.6 mrg
411 1.6 mrg p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
412 1.6 mrg p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
413 1.6 mrg p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
414 1.6 mrg p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
415 1.6 mrg p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
416 1.1 mrg }
417 1.1 mrg
418 1.1 mrg #ifdef DEBUG
419 1.1 mrg int enableswap = 1;
420 1.1 mrg int swapdebug = 0;
421 1.1 mrg #define SDB_FOLLOW 1
422 1.1 mrg #define SDB_SWAPIN 2
423 1.1 mrg #define SDB_SWAPOUT 4
424 1.1 mrg #endif
425 1.1 mrg
426 1.1 mrg /*
427 1.1 mrg * uvm_swapin: swap in a process's u-area.
428 1.1 mrg */
429 1.1 mrg
430 1.6 mrg void
431 1.6 mrg uvm_swapin(p)
432 1.6 mrg struct proc *p;
433 1.6 mrg {
434 1.13 eeh vaddr_t addr;
435 1.52 chs int s, error;
436 1.6 mrg
437 1.13 eeh addr = (vaddr_t)p->p_addr;
438 1.6 mrg /* make P_INMEM true */
439 1.57 chs error = uvm_fault_wire(kernel_map, addr, addr + USPACE, VM_FAULT_WIRE,
440 1.21 thorpej VM_PROT_READ | VM_PROT_WRITE);
441 1.52 chs if (error) {
442 1.52 chs panic("uvm_swapin: rewiring stack failed: %d", error);
443 1.52 chs }
444 1.6 mrg
445 1.6 mrg /*
446 1.6 mrg * Some architectures need to be notified when the user area has
447 1.6 mrg * moved to new physical page(s) (e.g. see mips/mips/vm_machdep.c).
448 1.6 mrg */
449 1.6 mrg cpu_swapin(p);
450 1.41 enami SCHED_LOCK(s);
451 1.6 mrg if (p->p_stat == SRUN)
452 1.6 mrg setrunqueue(p);
453 1.6 mrg p->p_flag |= P_INMEM;
454 1.41 enami SCHED_UNLOCK(s);
455 1.6 mrg p->p_swtime = 0;
456 1.6 mrg ++uvmexp.swapins;
457 1.1 mrg }
458 1.1 mrg
459 1.1 mrg /*
460 1.1 mrg * uvm_scheduler: process zero main loop
461 1.1 mrg *
462 1.1 mrg * - attempt to swapin every swaped-out, runnable process in order of
463 1.1 mrg * priority.
464 1.1 mrg * - if not enough memory, wake the pagedaemon and let it clear space.
465 1.1 mrg */
466 1.1 mrg
467 1.6 mrg void
468 1.6 mrg uvm_scheduler()
469 1.1 mrg {
470 1.32 augustss struct proc *p;
471 1.32 augustss int pri;
472 1.6 mrg struct proc *pp;
473 1.6 mrg int ppri;
474 1.1 mrg
475 1.1 mrg loop:
476 1.1 mrg #ifdef DEBUG
477 1.6 mrg while (!enableswap)
478 1.43 chs tsleep(&proc0, PVM, "noswap", 0);
479 1.1 mrg #endif
480 1.6 mrg pp = NULL; /* process to choose */
481 1.6 mrg ppri = INT_MIN; /* its priority */
482 1.29 thorpej proclist_lock_read();
483 1.43 chs LIST_FOREACH(p, &allproc, p_list) {
484 1.6 mrg
485 1.6 mrg /* is it a runnable swapped out process? */
486 1.6 mrg if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) {
487 1.6 mrg pri = p->p_swtime + p->p_slptime -
488 1.6 mrg (p->p_nice - NZERO) * 8;
489 1.6 mrg if (pri > ppri) { /* higher priority? remember it. */
490 1.6 mrg pp = p;
491 1.6 mrg ppri = pri;
492 1.6 mrg }
493 1.6 mrg }
494 1.6 mrg }
495 1.39 sommerfe /*
496 1.39 sommerfe * XXXSMP: possible unlock/sleep race between here and the
497 1.39 sommerfe * "scheduler" tsleep below..
498 1.39 sommerfe */
499 1.28 thorpej proclist_unlock_read();
500 1.1 mrg
501 1.1 mrg #ifdef DEBUG
502 1.6 mrg if (swapdebug & SDB_FOLLOW)
503 1.6 mrg printf("scheduler: running, procp %p pri %d\n", pp, ppri);
504 1.1 mrg #endif
505 1.6 mrg /*
506 1.6 mrg * Nothing to do, back to sleep
507 1.6 mrg */
508 1.6 mrg if ((p = pp) == NULL) {
509 1.43 chs tsleep(&proc0, PVM, "scheduler", 0);
510 1.6 mrg goto loop;
511 1.6 mrg }
512 1.6 mrg
513 1.6 mrg /*
514 1.6 mrg * we have found swapped out process which we would like to bring
515 1.6 mrg * back in.
516 1.6 mrg *
517 1.6 mrg * XXX: this part is really bogus cuz we could deadlock on memory
518 1.6 mrg * despite our feeble check
519 1.6 mrg */
520 1.6 mrg if (uvmexp.free > atop(USPACE)) {
521 1.1 mrg #ifdef DEBUG
522 1.6 mrg if (swapdebug & SDB_SWAPIN)
523 1.6 mrg printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
524 1.1 mrg p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free);
525 1.1 mrg #endif
526 1.6 mrg uvm_swapin(p);
527 1.6 mrg goto loop;
528 1.6 mrg }
529 1.6 mrg /*
530 1.6 mrg * not enough memory, jab the pageout daemon and wait til the coast
531 1.6 mrg * is clear
532 1.6 mrg */
533 1.1 mrg #ifdef DEBUG
534 1.6 mrg if (swapdebug & SDB_FOLLOW)
535 1.6 mrg printf("scheduler: no room for pid %d(%s), free %d\n",
536 1.1 mrg p->p_pid, p->p_comm, uvmexp.free);
537 1.1 mrg #endif
538 1.6 mrg uvm_wait("schedpwait");
539 1.1 mrg #ifdef DEBUG
540 1.6 mrg if (swapdebug & SDB_FOLLOW)
541 1.6 mrg printf("scheduler: room again, free %d\n", uvmexp.free);
542 1.1 mrg #endif
543 1.6 mrg goto loop;
544 1.1 mrg }
545 1.1 mrg
546 1.1 mrg /*
547 1.1 mrg * swappable: is process "p" swappable?
548 1.1 mrg */
549 1.1 mrg
550 1.1 mrg #define swappable(p) \
551 1.1 mrg (((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM && \
552 1.1 mrg (p)->p_holdcnt == 0)
553 1.1 mrg
554 1.1 mrg /*
555 1.1 mrg * swapout_threads: find threads that can be swapped and unwire their
556 1.1 mrg * u-areas.
557 1.1 mrg *
558 1.1 mrg * - called by the pagedaemon
559 1.1 mrg * - try and swap at least one processs
560 1.1 mrg * - processes that are sleeping or stopped for maxslp or more seconds
561 1.1 mrg * are swapped... otherwise the longest-sleeping or stopped process
562 1.1 mrg * is swapped, otherwise the longest resident process...
563 1.1 mrg */
564 1.60 chs
565 1.6 mrg void
566 1.6 mrg uvm_swapout_threads()
567 1.1 mrg {
568 1.32 augustss struct proc *p;
569 1.6 mrg struct proc *outp, *outp2;
570 1.6 mrg int outpri, outpri2;
571 1.6 mrg int didswap = 0;
572 1.48 chs extern int maxslp;
573 1.6 mrg /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
574 1.1 mrg
575 1.1 mrg #ifdef DEBUG
576 1.6 mrg if (!enableswap)
577 1.6 mrg return;
578 1.1 mrg #endif
579 1.1 mrg
580 1.6 mrg /*
581 1.6 mrg * outp/outpri : stop/sleep process with largest sleeptime < maxslp
582 1.6 mrg * outp2/outpri2: the longest resident process (its swap time)
583 1.6 mrg */
584 1.6 mrg outp = outp2 = NULL;
585 1.6 mrg outpri = outpri2 = 0;
586 1.29 thorpej proclist_lock_read();
587 1.43 chs LIST_FOREACH(p, &allproc, p_list) {
588 1.6 mrg if (!swappable(p))
589 1.6 mrg continue;
590 1.6 mrg switch (p->p_stat) {
591 1.6 mrg case SRUN:
592 1.33 thorpej case SONPROC:
593 1.6 mrg if (p->p_swtime > outpri2) {
594 1.6 mrg outp2 = p;
595 1.6 mrg outpri2 = p->p_swtime;
596 1.6 mrg }
597 1.6 mrg continue;
598 1.48 chs
599 1.6 mrg case SSLEEP:
600 1.6 mrg case SSTOP:
601 1.6 mrg if (p->p_slptime >= maxslp) {
602 1.43 chs uvm_swapout(p);
603 1.6 mrg didswap++;
604 1.6 mrg } else if (p->p_slptime > outpri) {
605 1.6 mrg outp = p;
606 1.6 mrg outpri = p->p_slptime;
607 1.6 mrg }
608 1.6 mrg continue;
609 1.6 mrg }
610 1.6 mrg }
611 1.28 thorpej proclist_unlock_read();
612 1.6 mrg
613 1.6 mrg /*
614 1.6 mrg * If we didn't get rid of any real duds, toss out the next most
615 1.6 mrg * likely sleeping/stopped or running candidate. We only do this
616 1.6 mrg * if we are real low on memory since we don't gain much by doing
617 1.6 mrg * it (USPACE bytes).
618 1.6 mrg */
619 1.6 mrg if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
620 1.6 mrg if ((p = outp) == NULL)
621 1.6 mrg p = outp2;
622 1.1 mrg #ifdef DEBUG
623 1.6 mrg if (swapdebug & SDB_SWAPOUT)
624 1.6 mrg printf("swapout_threads: no duds, try procp %p\n", p);
625 1.1 mrg #endif
626 1.6 mrg if (p)
627 1.6 mrg uvm_swapout(p);
628 1.6 mrg }
629 1.1 mrg }
630 1.1 mrg
631 1.1 mrg /*
632 1.1 mrg * uvm_swapout: swap out process "p"
633 1.1 mrg *
634 1.48 chs * - currently "swapout" means "unwire U-area" and "pmap_collect()"
635 1.1 mrg * the pmap.
636 1.1 mrg * - XXXCDC: should deactivate all process' private anonymous memory
637 1.1 mrg */
638 1.1 mrg
639 1.6 mrg static void
640 1.6 mrg uvm_swapout(p)
641 1.32 augustss struct proc *p;
642 1.1 mrg {
643 1.13 eeh vaddr_t addr;
644 1.6 mrg int s;
645 1.1 mrg
646 1.1 mrg #ifdef DEBUG
647 1.6 mrg if (swapdebug & SDB_SWAPOUT)
648 1.6 mrg printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n",
649 1.1 mrg p->p_pid, p->p_comm, p->p_addr, p->p_stat,
650 1.1 mrg p->p_slptime, uvmexp.free);
651 1.1 mrg #endif
652 1.1 mrg
653 1.6 mrg /*
654 1.6 mrg * Do any machine-specific actions necessary before swapout.
655 1.6 mrg * This can include saving floating point state, etc.
656 1.6 mrg */
657 1.6 mrg cpu_swapout(p);
658 1.6 mrg
659 1.6 mrg /*
660 1.6 mrg * Mark it as (potentially) swapped out.
661 1.6 mrg */
662 1.41 enami SCHED_LOCK(s);
663 1.6 mrg p->p_flag &= ~P_INMEM;
664 1.6 mrg if (p->p_stat == SRUN)
665 1.6 mrg remrunqueue(p);
666 1.41 enami SCHED_UNLOCK(s);
667 1.6 mrg p->p_swtime = 0;
668 1.53 chs p->p_stats->p_ru.ru_nswap++;
669 1.6 mrg ++uvmexp.swapouts;
670 1.43 chs
671 1.43 chs /*
672 1.43 chs * Unwire the to-be-swapped process's user struct and kernel stack.
673 1.43 chs */
674 1.43 chs addr = (vaddr_t)p->p_addr;
675 1.43 chs uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */
676 1.43 chs pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
677 1.1 mrg }
678 1.1 mrg
679 1.56 thorpej /*
680 1.56 thorpej * uvm_coredump_walkmap: walk a process's map for the purpose of dumping
681 1.56 thorpej * a core file.
682 1.56 thorpej */
683 1.56 thorpej
684 1.56 thorpej int
685 1.56 thorpej uvm_coredump_walkmap(p, vp, cred, func, cookie)
686 1.56 thorpej struct proc *p;
687 1.56 thorpej struct vnode *vp;
688 1.56 thorpej struct ucred *cred;
689 1.56 thorpej int (*func)(struct proc *, struct vnode *, struct ucred *,
690 1.56 thorpej struct uvm_coredump_state *);
691 1.56 thorpej void *cookie;
692 1.56 thorpej {
693 1.56 thorpej struct uvm_coredump_state state;
694 1.56 thorpej struct vmspace *vm = p->p_vmspace;
695 1.56 thorpej struct vm_map *map = &vm->vm_map;
696 1.56 thorpej struct vm_map_entry *entry;
697 1.56 thorpej vaddr_t maxstack;
698 1.56 thorpej int error;
699 1.56 thorpej
700 1.56 thorpej maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
701 1.56 thorpej
702 1.56 thorpej for (entry = map->header.next; entry != &map->header;
703 1.56 thorpej entry = entry->next) {
704 1.56 thorpej /* Should never happen for a user process. */
705 1.56 thorpej if (UVM_ET_ISSUBMAP(entry))
706 1.56 thorpej panic("uvm_coredump_walkmap: user process with "
707 1.56 thorpej "submap?");
708 1.56 thorpej
709 1.56 thorpej state.cookie = cookie;
710 1.56 thorpej state.start = entry->start;
711 1.56 thorpej state.end = entry->end;
712 1.56 thorpej state.prot = entry->protection;
713 1.56 thorpej state.flags = 0;
714 1.56 thorpej
715 1.56 thorpej if (state.start >= VM_MAXUSER_ADDRESS)
716 1.56 thorpej continue;
717 1.56 thorpej
718 1.56 thorpej if (state.end > VM_MAXUSER_ADDRESS)
719 1.56 thorpej state.end = VM_MAXUSER_ADDRESS;
720 1.56 thorpej
721 1.56 thorpej if (state.start >= (vaddr_t)vm->vm_maxsaddr) {
722 1.56 thorpej if (state.end <= maxstack)
723 1.56 thorpej continue;
724 1.56 thorpej if (state.start < maxstack)
725 1.56 thorpej state.start = maxstack;
726 1.56 thorpej state.flags |= UVM_COREDUMP_STACK;
727 1.56 thorpej }
728 1.56 thorpej
729 1.56 thorpej if ((entry->protection & VM_PROT_WRITE) == 0)
730 1.58 matt state.flags |= UVM_COREDUMP_NODUMP;
731 1.58 matt
732 1.58 matt if (entry->object.uvm_obj != NULL &&
733 1.58 matt entry->object.uvm_obj->pgops == &uvm_deviceops)
734 1.56 thorpej state.flags |= UVM_COREDUMP_NODUMP;
735 1.56 thorpej
736 1.56 thorpej error = (*func)(p, vp, cred, &state);
737 1.56 thorpej if (error)
738 1.56 thorpej return (error);
739 1.56 thorpej }
740 1.56 thorpej
741 1.56 thorpej return (0);
742 1.56 thorpej }
743