uvm_page.c revision 1.117 1 /* $NetBSD: uvm_page.c,v 1.117 2007/02/09 21:55:43 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_page.c 8.3 (Berkeley) 3/21/94
42 * from: Id: uvm_page.c,v 1.1.2.18 1998/02/06 05:24:42 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 /*
70 * uvm_page.c: page ops.
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: uvm_page.c,v 1.117 2007/02/09 21:55:43 ad Exp $");
75
76 #include "opt_uvmhist.h"
77 #include "opt_readahead.h"
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/malloc.h>
82 #include <sys/sched.h>
83 #include <sys/kernel.h>
84 #include <sys/vnode.h>
85 #include <sys/proc.h>
86
87 #include <uvm/uvm.h>
88 #include <uvm/uvm_pdpolicy.h>
89
90 /*
91 * global vars... XXXCDC: move to uvm. structure.
92 */
93
94 /*
95 * physical memory config is stored in vm_physmem.
96 */
97
98 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX]; /* XXXCDC: uvm.physmem */
99 int vm_nphysseg = 0; /* XXXCDC: uvm.nphysseg */
100
101 /*
102 * Some supported CPUs in a given architecture don't support all
103 * of the things necessary to do idle page zero'ing efficiently.
104 * We therefore provide a way to disable it from machdep code here.
105 */
106 /*
107 * XXX disabled until we can find a way to do this without causing
108 * problems for either CPU caches or DMA latency.
109 */
110 boolean_t vm_page_zero_enable = FALSE;
111
112 /*
113 * local variables
114 */
115
116 /*
117 * these variables record the values returned by vm_page_bootstrap,
118 * for debugging purposes. The implementation of uvm_pageboot_alloc
119 * and pmap_startup here also uses them internally.
120 */
121
122 static vaddr_t virtual_space_start;
123 static vaddr_t virtual_space_end;
124
125 /*
126 * we use a hash table with only one bucket during bootup. we will
127 * later rehash (resize) the hash table once the allocator is ready.
128 * we static allocate the one bootstrap bucket below...
129 */
130
131 static struct pglist uvm_bootbucket;
132
133 /*
134 * we allocate an initial number of page colors in uvm_page_init(),
135 * and remember them. We may re-color pages as cache sizes are
136 * discovered during the autoconfiguration phase. But we can never
137 * free the initial set of buckets, since they are allocated using
138 * uvm_pageboot_alloc().
139 */
140
141 static boolean_t have_recolored_pages /* = FALSE */;
142
143 MALLOC_DEFINE(M_VMPAGE, "VM page", "VM page");
144
145 #ifdef DEBUG
146 vaddr_t uvm_zerocheckkva;
147 #endif /* DEBUG */
148
149 /*
150 * local prototypes
151 */
152
153 static void uvm_pageinsert(struct vm_page *);
154 static void uvm_pageinsert_after(struct vm_page *, struct vm_page *);
155 static void uvm_pageremove(struct vm_page *);
156
157 /*
158 * inline functions
159 */
160
161 /*
162 * uvm_pageinsert: insert a page in the object and the hash table
163 * uvm_pageinsert_after: insert a page into the specified place in listq
164 *
165 * => caller must lock object
166 * => caller must lock page queues
167 * => call should have already set pg's object and offset pointers
168 * and bumped the version counter
169 */
170
171 inline static void
172 uvm_pageinsert_after(struct vm_page *pg, struct vm_page *where)
173 {
174 struct pglist *buck;
175 struct uvm_object *uobj = pg->uobject;
176
177 KASSERT((pg->flags & PG_TABLED) == 0);
178 KASSERT(where == NULL || (where->flags & PG_TABLED));
179 KASSERT(where == NULL || (where->uobject == uobj));
180 buck = &uvm.page_hash[uvm_pagehash(uobj, pg->offset)];
181 simple_lock(&uvm.hashlock);
182 TAILQ_INSERT_TAIL(buck, pg, hashq);
183 simple_unlock(&uvm.hashlock);
184
185 if (UVM_OBJ_IS_VNODE(uobj)) {
186 if (uobj->uo_npages == 0) {
187 struct vnode *vp = (struct vnode *)uobj;
188
189 vholdl(vp);
190 }
191 if (UVM_OBJ_IS_VTEXT(uobj)) {
192 uvmexp.execpages++;
193 } else {
194 uvmexp.filepages++;
195 }
196 } else if (UVM_OBJ_IS_AOBJ(uobj)) {
197 uvmexp.anonpages++;
198 }
199
200 if (where)
201 TAILQ_INSERT_AFTER(&uobj->memq, where, pg, listq);
202 else
203 TAILQ_INSERT_TAIL(&uobj->memq, pg, listq);
204 pg->flags |= PG_TABLED;
205 uobj->uo_npages++;
206 }
207
208 inline static void
209 uvm_pageinsert(struct vm_page *pg)
210 {
211
212 uvm_pageinsert_after(pg, NULL);
213 }
214
215 /*
216 * uvm_page_remove: remove page from object and hash
217 *
218 * => caller must lock object
219 * => caller must lock page queues
220 */
221
222 static inline void
223 uvm_pageremove(struct vm_page *pg)
224 {
225 struct pglist *buck;
226 struct uvm_object *uobj = pg->uobject;
227
228 KASSERT(pg->flags & PG_TABLED);
229 buck = &uvm.page_hash[uvm_pagehash(uobj, pg->offset)];
230 simple_lock(&uvm.hashlock);
231 TAILQ_REMOVE(buck, pg, hashq);
232 simple_unlock(&uvm.hashlock);
233
234 if (UVM_OBJ_IS_VNODE(uobj)) {
235 if (uobj->uo_npages == 1) {
236 struct vnode *vp = (struct vnode *)uobj;
237
238 holdrelel(vp);
239 }
240 if (UVM_OBJ_IS_VTEXT(uobj)) {
241 uvmexp.execpages--;
242 } else {
243 uvmexp.filepages--;
244 }
245 } else if (UVM_OBJ_IS_AOBJ(uobj)) {
246 uvmexp.anonpages--;
247 }
248
249 /* object should be locked */
250 uobj->uo_npages--;
251 TAILQ_REMOVE(&uobj->memq, pg, listq);
252 pg->flags &= ~PG_TABLED;
253 pg->uobject = NULL;
254 }
255
256 static void
257 uvm_page_init_buckets(struct pgfreelist *pgfl)
258 {
259 int color, i;
260
261 for (color = 0; color < uvmexp.ncolors; color++) {
262 for (i = 0; i < PGFL_NQUEUES; i++) {
263 TAILQ_INIT(&pgfl->pgfl_buckets[color].pgfl_queues[i]);
264 }
265 }
266 }
267
268 /*
269 * uvm_page_init: init the page system. called from uvm_init().
270 *
271 * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
272 */
273
274 void
275 uvm_page_init(vaddr_t *kvm_startp, vaddr_t *kvm_endp)
276 {
277 vsize_t freepages, pagecount, bucketcount, n;
278 struct pgflbucket *bucketarray;
279 struct vm_page *pagearray;
280 int lcv;
281 u_int i;
282 paddr_t paddr;
283
284 /*
285 * init the page queues and page queue locks, except the free
286 * list; we allocate that later (with the initial vm_page
287 * structures).
288 */
289
290 uvmpdpol_init();
291 simple_lock_init(&uvm.pageqlock);
292 simple_lock_init(&uvm.fpageqlock);
293
294 /*
295 * init the <obj,offset> => <page> hash table. for now
296 * we just have one bucket (the bootstrap bucket). later on we
297 * will allocate new buckets as we dynamically resize the hash table.
298 */
299
300 uvm.page_nhash = 1; /* 1 bucket */
301 uvm.page_hashmask = 0; /* mask for hash function */
302 uvm.page_hash = &uvm_bootbucket; /* install bootstrap bucket */
303 TAILQ_INIT(uvm.page_hash); /* init hash table */
304 simple_lock_init(&uvm.hashlock); /* init hash table lock */
305
306 /*
307 * allocate vm_page structures.
308 */
309
310 /*
311 * sanity check:
312 * before calling this function the MD code is expected to register
313 * some free RAM with the uvm_page_physload() function. our job
314 * now is to allocate vm_page structures for this memory.
315 */
316
317 if (vm_nphysseg == 0)
318 panic("uvm_page_bootstrap: no memory pre-allocated");
319
320 /*
321 * first calculate the number of free pages...
322 *
323 * note that we use start/end rather than avail_start/avail_end.
324 * this allows us to allocate extra vm_page structures in case we
325 * want to return some memory to the pool after booting.
326 */
327
328 freepages = 0;
329 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
330 freepages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
331
332 /*
333 * Let MD code initialize the number of colors, or default
334 * to 1 color if MD code doesn't care.
335 */
336 if (uvmexp.ncolors == 0)
337 uvmexp.ncolors = 1;
338 uvmexp.colormask = uvmexp.ncolors - 1;
339
340 /*
341 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
342 * use. for each page of memory we use we need a vm_page structure.
343 * thus, the total number of pages we can use is the total size of
344 * the memory divided by the PAGE_SIZE plus the size of the vm_page
345 * structure. we add one to freepages as a fudge factor to avoid
346 * truncation errors (since we can only allocate in terms of whole
347 * pages).
348 */
349
350 bucketcount = uvmexp.ncolors * VM_NFREELIST;
351 pagecount = ((freepages + 1) << PAGE_SHIFT) /
352 (PAGE_SIZE + sizeof(struct vm_page));
353
354 bucketarray = (void *)uvm_pageboot_alloc((bucketcount *
355 sizeof(struct pgflbucket)) + (pagecount *
356 sizeof(struct vm_page)));
357 pagearray = (struct vm_page *)(bucketarray + bucketcount);
358
359 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
360 uvm.page_free[lcv].pgfl_buckets =
361 (bucketarray + (lcv * uvmexp.ncolors));
362 uvm_page_init_buckets(&uvm.page_free[lcv]);
363 }
364 memset(pagearray, 0, pagecount * sizeof(struct vm_page));
365
366 /*
367 * init the vm_page structures and put them in the correct place.
368 */
369
370 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
371 n = vm_physmem[lcv].end - vm_physmem[lcv].start;
372
373 /* set up page array pointers */
374 vm_physmem[lcv].pgs = pagearray;
375 pagearray += n;
376 pagecount -= n;
377 vm_physmem[lcv].lastpg = vm_physmem[lcv].pgs + (n - 1);
378
379 /* init and free vm_pages (we've already zeroed them) */
380 paddr = ptoa(vm_physmem[lcv].start);
381 for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
382 vm_physmem[lcv].pgs[i].phys_addr = paddr;
383 #ifdef __HAVE_VM_PAGE_MD
384 VM_MDPAGE_INIT(&vm_physmem[lcv].pgs[i]);
385 #endif
386 if (atop(paddr) >= vm_physmem[lcv].avail_start &&
387 atop(paddr) <= vm_physmem[lcv].avail_end) {
388 uvmexp.npages++;
389 /* add page to free pool */
390 uvm_pagefree(&vm_physmem[lcv].pgs[i]);
391 }
392 }
393 }
394
395 /*
396 * pass up the values of virtual_space_start and
397 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
398 * layers of the VM.
399 */
400
401 *kvm_startp = round_page(virtual_space_start);
402 *kvm_endp = trunc_page(virtual_space_end);
403 #ifdef DEBUG
404 /*
405 * steal kva for uvm_pagezerocheck().
406 */
407 uvm_zerocheckkva = *kvm_startp;
408 *kvm_startp += PAGE_SIZE;
409 #endif /* DEBUG */
410
411 /*
412 * init locks for kernel threads
413 */
414
415 simple_lock_init(&uvm.pagedaemon_lock);
416
417 /*
418 * init various thresholds.
419 */
420
421 uvmexp.reserve_pagedaemon = 1;
422 uvmexp.reserve_kernel = 5;
423
424 /*
425 * determine if we should zero pages in the idle loop.
426 */
427
428 uvm.page_idle_zero = vm_page_zero_enable;
429
430 /*
431 * done!
432 */
433
434 uvm.page_init_done = TRUE;
435 }
436
437 /*
438 * uvm_setpagesize: set the page size
439 *
440 * => sets page_shift and page_mask from uvmexp.pagesize.
441 */
442
443 void
444 uvm_setpagesize(void)
445 {
446
447 /*
448 * If uvmexp.pagesize is 0 at this point, we expect PAGE_SIZE
449 * to be a constant (indicated by being a non-zero value).
450 */
451 if (uvmexp.pagesize == 0) {
452 if (PAGE_SIZE == 0)
453 panic("uvm_setpagesize: uvmexp.pagesize not set");
454 uvmexp.pagesize = PAGE_SIZE;
455 }
456 uvmexp.pagemask = uvmexp.pagesize - 1;
457 if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
458 panic("uvm_setpagesize: page size not a power of two");
459 for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
460 if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
461 break;
462 }
463
464 /*
465 * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
466 */
467
468 vaddr_t
469 uvm_pageboot_alloc(vsize_t size)
470 {
471 static boolean_t initialized = FALSE;
472 vaddr_t addr;
473 #if !defined(PMAP_STEAL_MEMORY)
474 vaddr_t vaddr;
475 paddr_t paddr;
476 #endif
477
478 /*
479 * on first call to this function, initialize ourselves.
480 */
481 if (initialized == FALSE) {
482 pmap_virtual_space(&virtual_space_start, &virtual_space_end);
483
484 /* round it the way we like it */
485 virtual_space_start = round_page(virtual_space_start);
486 virtual_space_end = trunc_page(virtual_space_end);
487
488 initialized = TRUE;
489 }
490
491 /* round to page size */
492 size = round_page(size);
493
494 #if defined(PMAP_STEAL_MEMORY)
495
496 /*
497 * defer bootstrap allocation to MD code (it may want to allocate
498 * from a direct-mapped segment). pmap_steal_memory should adjust
499 * virtual_space_start/virtual_space_end if necessary.
500 */
501
502 addr = pmap_steal_memory(size, &virtual_space_start,
503 &virtual_space_end);
504
505 return(addr);
506
507 #else /* !PMAP_STEAL_MEMORY */
508
509 /*
510 * allocate virtual memory for this request
511 */
512 if (virtual_space_start == virtual_space_end ||
513 (virtual_space_end - virtual_space_start) < size)
514 panic("uvm_pageboot_alloc: out of virtual space");
515
516 addr = virtual_space_start;
517
518 #ifdef PMAP_GROWKERNEL
519 /*
520 * If the kernel pmap can't map the requested space,
521 * then allocate more resources for it.
522 */
523 if (uvm_maxkaddr < (addr + size)) {
524 uvm_maxkaddr = pmap_growkernel(addr + size);
525 if (uvm_maxkaddr < (addr + size))
526 panic("uvm_pageboot_alloc: pmap_growkernel() failed");
527 }
528 #endif
529
530 virtual_space_start += size;
531
532 /*
533 * allocate and mapin physical pages to back new virtual pages
534 */
535
536 for (vaddr = round_page(addr) ; vaddr < addr + size ;
537 vaddr += PAGE_SIZE) {
538
539 if (!uvm_page_physget(&paddr))
540 panic("uvm_pageboot_alloc: out of memory");
541
542 /*
543 * Note this memory is no longer managed, so using
544 * pmap_kenter is safe.
545 */
546 pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE);
547 }
548 pmap_update(pmap_kernel());
549 return(addr);
550 #endif /* PMAP_STEAL_MEMORY */
551 }
552
553 #if !defined(PMAP_STEAL_MEMORY)
554 /*
555 * uvm_page_physget: "steal" one page from the vm_physmem structure.
556 *
557 * => attempt to allocate it off the end of a segment in which the "avail"
558 * values match the start/end values. if we can't do that, then we
559 * will advance both values (making them equal, and removing some
560 * vm_page structures from the non-avail area).
561 * => return false if out of memory.
562 */
563
564 /* subroutine: try to allocate from memory chunks on the specified freelist */
565 static boolean_t uvm_page_physget_freelist(paddr_t *, int);
566
567 static boolean_t
568 uvm_page_physget_freelist(paddr_t *paddrp, int freelist)
569 {
570 int lcv, x;
571
572 /* pass 1: try allocating from a matching end */
573 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
574 for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
575 #else
576 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
577 #endif
578 {
579
580 if (uvm.page_init_done == TRUE)
581 panic("uvm_page_physget: called _after_ bootstrap");
582
583 if (vm_physmem[lcv].free_list != freelist)
584 continue;
585
586 /* try from front */
587 if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start &&
588 vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
589 *paddrp = ptoa(vm_physmem[lcv].avail_start);
590 vm_physmem[lcv].avail_start++;
591 vm_physmem[lcv].start++;
592 /* nothing left? nuke it */
593 if (vm_physmem[lcv].avail_start ==
594 vm_physmem[lcv].end) {
595 if (vm_nphysseg == 1)
596 panic("uvm_page_physget: out of memory!");
597 vm_nphysseg--;
598 for (x = lcv ; x < vm_nphysseg ; x++)
599 /* structure copy */
600 vm_physmem[x] = vm_physmem[x+1];
601 }
602 return (TRUE);
603 }
604
605 /* try from rear */
606 if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end &&
607 vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
608 *paddrp = ptoa(vm_physmem[lcv].avail_end - 1);
609 vm_physmem[lcv].avail_end--;
610 vm_physmem[lcv].end--;
611 /* nothing left? nuke it */
612 if (vm_physmem[lcv].avail_end ==
613 vm_physmem[lcv].start) {
614 if (vm_nphysseg == 1)
615 panic("uvm_page_physget: out of memory!");
616 vm_nphysseg--;
617 for (x = lcv ; x < vm_nphysseg ; x++)
618 /* structure copy */
619 vm_physmem[x] = vm_physmem[x+1];
620 }
621 return (TRUE);
622 }
623 }
624
625 /* pass2: forget about matching ends, just allocate something */
626 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
627 for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
628 #else
629 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
630 #endif
631 {
632
633 /* any room in this bank? */
634 if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end)
635 continue; /* nope */
636
637 *paddrp = ptoa(vm_physmem[lcv].avail_start);
638 vm_physmem[lcv].avail_start++;
639 /* truncate! */
640 vm_physmem[lcv].start = vm_physmem[lcv].avail_start;
641
642 /* nothing left? nuke it */
643 if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) {
644 if (vm_nphysseg == 1)
645 panic("uvm_page_physget: out of memory!");
646 vm_nphysseg--;
647 for (x = lcv ; x < vm_nphysseg ; x++)
648 /* structure copy */
649 vm_physmem[x] = vm_physmem[x+1];
650 }
651 return (TRUE);
652 }
653
654 return (FALSE); /* whoops! */
655 }
656
657 boolean_t
658 uvm_page_physget(paddr_t *paddrp)
659 {
660 int i;
661
662 /* try in the order of freelist preference */
663 for (i = 0; i < VM_NFREELIST; i++)
664 if (uvm_page_physget_freelist(paddrp, i) == TRUE)
665 return (TRUE);
666 return (FALSE);
667 }
668 #endif /* PMAP_STEAL_MEMORY */
669
670 /*
671 * uvm_page_physload: load physical memory into VM system
672 *
673 * => all args are PFs
674 * => all pages in start/end get vm_page structures
675 * => areas marked by avail_start/avail_end get added to the free page pool
676 * => we are limited to VM_PHYSSEG_MAX physical memory segments
677 */
678
679 void
680 uvm_page_physload(paddr_t start, paddr_t end, paddr_t avail_start,
681 paddr_t avail_end, int free_list)
682 {
683 int preload, lcv;
684 psize_t npages;
685 struct vm_page *pgs;
686 struct vm_physseg *ps;
687
688 if (uvmexp.pagesize == 0)
689 panic("uvm_page_physload: page size not set!");
690 if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT)
691 panic("uvm_page_physload: bad free list %d", free_list);
692 if (start >= end)
693 panic("uvm_page_physload: start >= end");
694
695 /*
696 * do we have room?
697 */
698
699 if (vm_nphysseg == VM_PHYSSEG_MAX) {
700 printf("uvm_page_physload: unable to load physical memory "
701 "segment\n");
702 printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n",
703 VM_PHYSSEG_MAX, (long long)start, (long long)end);
704 printf("\tincrease VM_PHYSSEG_MAX\n");
705 return;
706 }
707
708 /*
709 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been
710 * called yet, so malloc is not available).
711 */
712
713 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
714 if (vm_physmem[lcv].pgs)
715 break;
716 }
717 preload = (lcv == vm_nphysseg);
718
719 /*
720 * if VM is already running, attempt to malloc() vm_page structures
721 */
722
723 if (!preload) {
724 #if defined(VM_PHYSSEG_NOADD)
725 panic("uvm_page_physload: tried to add RAM after vm_mem_init");
726 #else
727 /* XXXCDC: need some sort of lockout for this case */
728 paddr_t paddr;
729 npages = end - start; /* # of pages */
730 pgs = malloc(sizeof(struct vm_page) * npages,
731 M_VMPAGE, M_NOWAIT);
732 if (pgs == NULL) {
733 printf("uvm_page_physload: can not malloc vm_page "
734 "structs for segment\n");
735 printf("\tignoring 0x%lx -> 0x%lx\n", start, end);
736 return;
737 }
738 /* zero data, init phys_addr and free_list, and free pages */
739 memset(pgs, 0, sizeof(struct vm_page) * npages);
740 for (lcv = 0, paddr = ptoa(start) ;
741 lcv < npages ; lcv++, paddr += PAGE_SIZE) {
742 pgs[lcv].phys_addr = paddr;
743 pgs[lcv].free_list = free_list;
744 if (atop(paddr) >= avail_start &&
745 atop(paddr) <= avail_end)
746 uvm_pagefree(&pgs[lcv]);
747 }
748 /* XXXCDC: incomplete: need to update uvmexp.free, what else? */
749 /* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */
750 #endif
751 } else {
752 pgs = NULL;
753 npages = 0;
754 }
755
756 /*
757 * now insert us in the proper place in vm_physmem[]
758 */
759
760 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
761 /* random: put it at the end (easy!) */
762 ps = &vm_physmem[vm_nphysseg];
763 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
764 {
765 int x;
766 /* sort by address for binary search */
767 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
768 if (start < vm_physmem[lcv].start)
769 break;
770 ps = &vm_physmem[lcv];
771 /* move back other entries, if necessary ... */
772 for (x = vm_nphysseg ; x > lcv ; x--)
773 /* structure copy */
774 vm_physmem[x] = vm_physmem[x - 1];
775 }
776 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
777 {
778 int x;
779 /* sort by largest segment first */
780 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
781 if ((end - start) >
782 (vm_physmem[lcv].end - vm_physmem[lcv].start))
783 break;
784 ps = &vm_physmem[lcv];
785 /* move back other entries, if necessary ... */
786 for (x = vm_nphysseg ; x > lcv ; x--)
787 /* structure copy */
788 vm_physmem[x] = vm_physmem[x - 1];
789 }
790 #else
791 panic("uvm_page_physload: unknown physseg strategy selected!");
792 #endif
793
794 ps->start = start;
795 ps->end = end;
796 ps->avail_start = avail_start;
797 ps->avail_end = avail_end;
798 if (preload) {
799 ps->pgs = NULL;
800 } else {
801 ps->pgs = pgs;
802 ps->lastpg = pgs + npages - 1;
803 }
804 ps->free_list = free_list;
805 vm_nphysseg++;
806
807 if (!preload) {
808 uvm_page_rehash();
809 uvmpdpol_reinit();
810 }
811 }
812
813 /*
814 * uvm_page_rehash: reallocate hash table based on number of free pages.
815 */
816
817 void
818 uvm_page_rehash(void)
819 {
820 int freepages, lcv, bucketcount, oldcount;
821 struct pglist *newbuckets, *oldbuckets;
822 struct vm_page *pg;
823 size_t newsize, oldsize;
824
825 /*
826 * compute number of pages that can go in the free pool
827 */
828
829 freepages = 0;
830 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
831 freepages +=
832 (vm_physmem[lcv].avail_end - vm_physmem[lcv].avail_start);
833
834 /*
835 * compute number of buckets needed for this number of pages
836 */
837
838 bucketcount = 1;
839 while (bucketcount < freepages)
840 bucketcount = bucketcount * 2;
841
842 /*
843 * compute the size of the current table and new table.
844 */
845
846 oldbuckets = uvm.page_hash;
847 oldcount = uvm.page_nhash;
848 oldsize = round_page(sizeof(struct pglist) * oldcount);
849 newsize = round_page(sizeof(struct pglist) * bucketcount);
850
851 /*
852 * allocate the new buckets
853 */
854
855 newbuckets = (struct pglist *) uvm_km_alloc(kernel_map, newsize,
856 0, UVM_KMF_WIRED);
857 if (newbuckets == NULL) {
858 printf("uvm_page_physrehash: WARNING: could not grow page "
859 "hash table\n");
860 return;
861 }
862 for (lcv = 0 ; lcv < bucketcount ; lcv++)
863 TAILQ_INIT(&newbuckets[lcv]);
864
865 /*
866 * now replace the old buckets with the new ones and rehash everything
867 */
868
869 simple_lock(&uvm.hashlock);
870 uvm.page_hash = newbuckets;
871 uvm.page_nhash = bucketcount;
872 uvm.page_hashmask = bucketcount - 1; /* power of 2 */
873
874 /* ... and rehash */
875 for (lcv = 0 ; lcv < oldcount ; lcv++) {
876 while ((pg = oldbuckets[lcv].tqh_first) != NULL) {
877 TAILQ_REMOVE(&oldbuckets[lcv], pg, hashq);
878 TAILQ_INSERT_TAIL(
879 &uvm.page_hash[uvm_pagehash(pg->uobject, pg->offset)],
880 pg, hashq);
881 }
882 }
883 simple_unlock(&uvm.hashlock);
884
885 /*
886 * free old bucket array if is not the boot-time table
887 */
888
889 if (oldbuckets != &uvm_bootbucket)
890 uvm_km_free(kernel_map, (vaddr_t) oldbuckets, oldsize,
891 UVM_KMF_WIRED);
892 }
893
894 /*
895 * uvm_page_recolor: Recolor the pages if the new bucket count is
896 * larger than the old one.
897 */
898
899 void
900 uvm_page_recolor(int newncolors)
901 {
902 struct pgflbucket *bucketarray, *oldbucketarray;
903 struct pgfreelist pgfl;
904 struct vm_page *pg;
905 vsize_t bucketcount;
906 int s, lcv, color, i, ocolors;
907
908 if (newncolors <= uvmexp.ncolors)
909 return;
910
911 if (uvm.page_init_done == FALSE) {
912 uvmexp.ncolors = newncolors;
913 return;
914 }
915
916 bucketcount = newncolors * VM_NFREELIST;
917 bucketarray = malloc(bucketcount * sizeof(struct pgflbucket),
918 M_VMPAGE, M_NOWAIT);
919 if (bucketarray == NULL) {
920 printf("WARNING: unable to allocate %ld page color buckets\n",
921 (long) bucketcount);
922 return;
923 }
924
925 s = uvm_lock_fpageq();
926
927 /* Make sure we should still do this. */
928 if (newncolors <= uvmexp.ncolors) {
929 uvm_unlock_fpageq(s);
930 free(bucketarray, M_VMPAGE);
931 return;
932 }
933
934 oldbucketarray = uvm.page_free[0].pgfl_buckets;
935 ocolors = uvmexp.ncolors;
936
937 uvmexp.ncolors = newncolors;
938 uvmexp.colormask = uvmexp.ncolors - 1;
939
940 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
941 pgfl.pgfl_buckets = (bucketarray + (lcv * newncolors));
942 uvm_page_init_buckets(&pgfl);
943 for (color = 0; color < ocolors; color++) {
944 for (i = 0; i < PGFL_NQUEUES; i++) {
945 while ((pg = TAILQ_FIRST(&uvm.page_free[
946 lcv].pgfl_buckets[color].pgfl_queues[i]))
947 != NULL) {
948 TAILQ_REMOVE(&uvm.page_free[
949 lcv].pgfl_buckets[
950 color].pgfl_queues[i], pg, pageq);
951 TAILQ_INSERT_TAIL(&pgfl.pgfl_buckets[
952 VM_PGCOLOR_BUCKET(pg)].pgfl_queues[
953 i], pg, pageq);
954 }
955 }
956 }
957 uvm.page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets;
958 }
959
960 if (have_recolored_pages) {
961 uvm_unlock_fpageq(s);
962 free(oldbucketarray, M_VMPAGE);
963 return;
964 }
965
966 have_recolored_pages = TRUE;
967 uvm_unlock_fpageq(s);
968 }
969
970 /*
971 * uvm_pagealloc_pgfl: helper routine for uvm_pagealloc_strat
972 */
973
974 static struct vm_page *
975 uvm_pagealloc_pgfl(struct pgfreelist *pgfl, int try1, int try2,
976 int *trycolorp)
977 {
978 struct pglist *freeq;
979 struct vm_page *pg;
980 int color, trycolor = *trycolorp;
981
982 color = trycolor;
983 do {
984 if ((pg = TAILQ_FIRST((freeq =
985 &pgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL)
986 goto gotit;
987 if ((pg = TAILQ_FIRST((freeq =
988 &pgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL)
989 goto gotit;
990 color = (color + 1) & uvmexp.colormask;
991 } while (color != trycolor);
992
993 return (NULL);
994
995 gotit:
996 TAILQ_REMOVE(freeq, pg, pageq);
997 uvmexp.free--;
998
999 /* update zero'd page count */
1000 if (pg->flags & PG_ZERO)
1001 uvmexp.zeropages--;
1002
1003 if (color == trycolor)
1004 uvmexp.colorhit++;
1005 else {
1006 uvmexp.colormiss++;
1007 *trycolorp = color;
1008 }
1009
1010 return (pg);
1011 }
1012
1013 /*
1014 * uvm_pagealloc_strat: allocate vm_page from a particular free list.
1015 *
1016 * => return null if no pages free
1017 * => wake up pagedaemon if number of free pages drops below low water mark
1018 * => if obj != NULL, obj must be locked (to put in hash)
1019 * => if anon != NULL, anon must be locked (to put in anon)
1020 * => only one of obj or anon can be non-null
1021 * => caller must activate/deactivate page if it is not wired.
1022 * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
1023 * => policy decision: it is more important to pull a page off of the
1024 * appropriate priority free list than it is to get a zero'd or
1025 * unknown contents page. This is because we live with the
1026 * consequences of a bad free list decision for the entire
1027 * lifetime of the page, e.g. if the page comes from memory that
1028 * is slower to access.
1029 */
1030
1031 struct vm_page *
1032 uvm_pagealloc_strat(struct uvm_object *obj, voff_t off, struct vm_anon *anon,
1033 int flags, int strat, int free_list)
1034 {
1035 int lcv, try1, try2, s, zeroit = 0, color;
1036 struct vm_page *pg;
1037 boolean_t use_reserve;
1038
1039 KASSERT(obj == NULL || anon == NULL);
1040 KASSERT(anon == NULL || off == 0);
1041 KASSERT(off == trunc_page(off));
1042 LOCK_ASSERT(obj == NULL || simple_lock_held(&obj->vmobjlock));
1043 LOCK_ASSERT(anon == NULL || simple_lock_held(&anon->an_lock));
1044
1045 s = uvm_lock_fpageq();
1046
1047 /*
1048 * This implements a global round-robin page coloring
1049 * algorithm.
1050 *
1051 * XXXJRT: Should we make the `nextcolor' per-CPU?
1052 * XXXJRT: What about virtually-indexed caches?
1053 */
1054
1055 color = uvm.page_free_nextcolor;
1056
1057 /*
1058 * check to see if we need to generate some free pages waking
1059 * the pagedaemon.
1060 */
1061
1062 uvm_kick_pdaemon();
1063
1064 /*
1065 * fail if any of these conditions is true:
1066 * [1] there really are no free pages, or
1067 * [2] only kernel "reserved" pages remain and
1068 * the page isn't being allocated to a kernel object.
1069 * [3] only pagedaemon "reserved" pages remain and
1070 * the requestor isn't the pagedaemon.
1071 */
1072
1073 use_reserve = (flags & UVM_PGA_USERESERVE) ||
1074 (obj && UVM_OBJ_IS_KERN_OBJECT(obj));
1075 if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) ||
1076 (uvmexp.free <= uvmexp.reserve_pagedaemon &&
1077 !(use_reserve && curproc == uvm.pagedaemon_proc)))
1078 goto fail;
1079
1080 #if PGFL_NQUEUES != 2
1081 #error uvm_pagealloc_strat needs to be updated
1082 #endif
1083
1084 /*
1085 * If we want a zero'd page, try the ZEROS queue first, otherwise
1086 * we try the UNKNOWN queue first.
1087 */
1088 if (flags & UVM_PGA_ZERO) {
1089 try1 = PGFL_ZEROS;
1090 try2 = PGFL_UNKNOWN;
1091 } else {
1092 try1 = PGFL_UNKNOWN;
1093 try2 = PGFL_ZEROS;
1094 }
1095
1096 again:
1097 switch (strat) {
1098 case UVM_PGA_STRAT_NORMAL:
1099 /* Check all freelists in descending priority order. */
1100 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
1101 pg = uvm_pagealloc_pgfl(&uvm.page_free[lcv],
1102 try1, try2, &color);
1103 if (pg != NULL)
1104 goto gotit;
1105 }
1106
1107 /* No pages free! */
1108 goto fail;
1109
1110 case UVM_PGA_STRAT_ONLY:
1111 case UVM_PGA_STRAT_FALLBACK:
1112 /* Attempt to allocate from the specified free list. */
1113 KASSERT(free_list >= 0 && free_list < VM_NFREELIST);
1114 pg = uvm_pagealloc_pgfl(&uvm.page_free[free_list],
1115 try1, try2, &color);
1116 if (pg != NULL)
1117 goto gotit;
1118
1119 /* Fall back, if possible. */
1120 if (strat == UVM_PGA_STRAT_FALLBACK) {
1121 strat = UVM_PGA_STRAT_NORMAL;
1122 goto again;
1123 }
1124
1125 /* No pages free! */
1126 goto fail;
1127
1128 default:
1129 panic("uvm_pagealloc_strat: bad strat %d", strat);
1130 /* NOTREACHED */
1131 }
1132
1133 gotit:
1134 /*
1135 * We now know which color we actually allocated from; set
1136 * the next color accordingly.
1137 */
1138
1139 uvm.page_free_nextcolor = (color + 1) & uvmexp.colormask;
1140
1141 /*
1142 * update allocation statistics and remember if we have to
1143 * zero the page
1144 */
1145
1146 if (flags & UVM_PGA_ZERO) {
1147 if (pg->flags & PG_ZERO) {
1148 uvmexp.pga_zerohit++;
1149 zeroit = 0;
1150 } else {
1151 uvmexp.pga_zeromiss++;
1152 zeroit = 1;
1153 }
1154 }
1155 uvm_unlock_fpageq(s);
1156
1157 pg->offset = off;
1158 pg->uobject = obj;
1159 pg->uanon = anon;
1160 pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
1161 if (anon) {
1162 anon->an_page = pg;
1163 pg->pqflags = PQ_ANON;
1164 uvmexp.anonpages++;
1165 } else {
1166 if (obj) {
1167 uvm_pageinsert(pg);
1168 }
1169 pg->pqflags = 0;
1170 }
1171 #if defined(UVM_PAGE_TRKOWN)
1172 pg->owner_tag = NULL;
1173 #endif
1174 UVM_PAGE_OWN(pg, "new alloc");
1175
1176 if (flags & UVM_PGA_ZERO) {
1177 /*
1178 * A zero'd page is not clean. If we got a page not already
1179 * zero'd, then we have to zero it ourselves.
1180 */
1181 pg->flags &= ~PG_CLEAN;
1182 if (zeroit)
1183 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1184 }
1185
1186 return(pg);
1187
1188 fail:
1189 uvm_unlock_fpageq(s);
1190 return (NULL);
1191 }
1192
1193 /*
1194 * uvm_pagereplace: replace a page with another
1195 *
1196 * => object must be locked
1197 */
1198
1199 void
1200 uvm_pagereplace(struct vm_page *oldpg, struct vm_page *newpg)
1201 {
1202
1203 KASSERT((oldpg->flags & PG_TABLED) != 0);
1204 KASSERT(oldpg->uobject != NULL);
1205 KASSERT((newpg->flags & PG_TABLED) == 0);
1206 KASSERT(newpg->uobject == NULL);
1207 LOCK_ASSERT(simple_lock_held(&oldpg->uobject->vmobjlock));
1208
1209 newpg->uobject = oldpg->uobject;
1210 newpg->offset = oldpg->offset;
1211
1212 uvm_pageinsert_after(newpg, oldpg);
1213 uvm_pageremove(oldpg);
1214 }
1215
1216 /*
1217 * uvm_pagerealloc: reallocate a page from one object to another
1218 *
1219 * => both objects must be locked
1220 */
1221
1222 void
1223 uvm_pagerealloc(struct vm_page *pg, struct uvm_object *newobj, voff_t newoff)
1224 {
1225 /*
1226 * remove it from the old object
1227 */
1228
1229 if (pg->uobject) {
1230 uvm_pageremove(pg);
1231 }
1232
1233 /*
1234 * put it in the new object
1235 */
1236
1237 if (newobj) {
1238 pg->uobject = newobj;
1239 pg->offset = newoff;
1240 uvm_pageinsert(pg);
1241 }
1242 }
1243
1244 #ifdef DEBUG
1245 /*
1246 * check if page is zero-filled
1247 *
1248 * - called with free page queue lock held.
1249 */
1250 void
1251 uvm_pagezerocheck(struct vm_page *pg)
1252 {
1253 int *p, *ep;
1254
1255 KASSERT(uvm_zerocheckkva != 0);
1256 LOCK_ASSERT(simple_lock_held(&uvm.fpageqlock));
1257
1258 /*
1259 * XXX assuming pmap_kenter_pa and pmap_kremove never call
1260 * uvm page allocator.
1261 *
1262 * it might be better to have "CPU-local temporary map" pmap interface.
1263 */
1264 pmap_kenter_pa(uvm_zerocheckkva, VM_PAGE_TO_PHYS(pg), VM_PROT_READ);
1265 p = (int *)uvm_zerocheckkva;
1266 ep = (int *)((char *)p + PAGE_SIZE);
1267 pmap_update(pmap_kernel());
1268 while (p < ep) {
1269 if (*p != 0)
1270 panic("PG_ZERO page isn't zero-filled");
1271 p++;
1272 }
1273 pmap_kremove(uvm_zerocheckkva, PAGE_SIZE);
1274 }
1275 #endif /* DEBUG */
1276
1277 /*
1278 * uvm_pagefree: free page
1279 *
1280 * => erase page's identity (i.e. remove from hash/object)
1281 * => put page on free list
1282 * => caller must lock owning object (either anon or uvm_object)
1283 * => caller must lock page queues
1284 * => assumes all valid mappings of pg are gone
1285 */
1286
1287 void
1288 uvm_pagefree(struct vm_page *pg)
1289 {
1290 int s;
1291 struct pglist *pgfl;
1292 boolean_t iszero;
1293
1294 KASSERT((pg->flags & PG_PAGEOUT) == 0);
1295 LOCK_ASSERT(simple_lock_held(&uvm.pageqlock) ||
1296 !uvmpdpol_pageisqueued_p(pg));
1297 LOCK_ASSERT(pg->uobject == NULL ||
1298 simple_lock_held(&pg->uobject->vmobjlock));
1299 LOCK_ASSERT(pg->uobject != NULL || pg->uanon == NULL ||
1300 simple_lock_held(&pg->uanon->an_lock));
1301
1302 #ifdef DEBUG
1303 if (pg->uobject == (void *)0xdeadbeef &&
1304 pg->uanon == (void *)0xdeadbeef) {
1305 panic("uvm_pagefree: freeing free page %p", pg);
1306 }
1307 #endif /* DEBUG */
1308
1309 /*
1310 * if the page is loaned, resolve the loan instead of freeing.
1311 */
1312
1313 if (pg->loan_count) {
1314 KASSERT(pg->wire_count == 0);
1315
1316 /*
1317 * if the page is owned by an anon then we just want to
1318 * drop anon ownership. the kernel will free the page when
1319 * it is done with it. if the page is owned by an object,
1320 * remove it from the object and mark it dirty for the benefit
1321 * of possible anon owners.
1322 *
1323 * regardless of previous ownership, wakeup any waiters,
1324 * unbusy the page, and we're done.
1325 */
1326
1327 if (pg->uobject != NULL) {
1328 uvm_pageremove(pg);
1329 pg->flags &= ~PG_CLEAN;
1330 } else if (pg->uanon != NULL) {
1331 if ((pg->pqflags & PQ_ANON) == 0) {
1332 pg->loan_count--;
1333 } else {
1334 pg->pqflags &= ~PQ_ANON;
1335 uvmexp.anonpages--;
1336 }
1337 pg->uanon->an_page = NULL;
1338 pg->uanon = NULL;
1339 }
1340 if (pg->flags & PG_WANTED) {
1341 wakeup(pg);
1342 }
1343 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_RELEASED|PG_PAGER1);
1344 #ifdef UVM_PAGE_TRKOWN
1345 pg->owner_tag = NULL;
1346 #endif
1347 if (pg->loan_count) {
1348 KASSERT(pg->uobject == NULL);
1349 if (pg->uanon == NULL) {
1350 uvm_pagedequeue(pg);
1351 }
1352 return;
1353 }
1354 }
1355
1356 /*
1357 * remove page from its object or anon.
1358 */
1359
1360 if (pg->uobject != NULL) {
1361 uvm_pageremove(pg);
1362 } else if (pg->uanon != NULL) {
1363 pg->uanon->an_page = NULL;
1364 uvmexp.anonpages--;
1365 }
1366
1367 /*
1368 * now remove the page from the queues.
1369 */
1370
1371 uvm_pagedequeue(pg);
1372
1373 /*
1374 * if the page was wired, unwire it now.
1375 */
1376
1377 if (pg->wire_count) {
1378 pg->wire_count = 0;
1379 uvmexp.wired--;
1380 }
1381
1382 /*
1383 * and put on free queue
1384 */
1385
1386 iszero = (pg->flags & PG_ZERO);
1387 pgfl = &uvm.page_free[uvm_page_lookup_freelist(pg)].
1388 pgfl_buckets[VM_PGCOLOR_BUCKET(pg)].
1389 pgfl_queues[iszero ? PGFL_ZEROS : PGFL_UNKNOWN];
1390
1391 pg->pqflags = PQ_FREE;
1392 #ifdef DEBUG
1393 pg->uobject = (void *)0xdeadbeef;
1394 pg->offset = 0xdeadbeef;
1395 pg->uanon = (void *)0xdeadbeef;
1396 #endif
1397
1398 s = uvm_lock_fpageq();
1399
1400 #ifdef DEBUG
1401 if (iszero)
1402 uvm_pagezerocheck(pg);
1403 #endif /* DEBUG */
1404
1405 TAILQ_INSERT_HEAD(pgfl, pg, pageq);
1406 uvmexp.free++;
1407 if (iszero)
1408 uvmexp.zeropages++;
1409
1410 if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
1411 uvm.page_idle_zero = vm_page_zero_enable;
1412
1413 uvm_unlock_fpageq(s);
1414 }
1415
1416 /*
1417 * uvm_page_unbusy: unbusy an array of pages.
1418 *
1419 * => pages must either all belong to the same object, or all belong to anons.
1420 * => if pages are object-owned, object must be locked.
1421 * => if pages are anon-owned, anons must be locked.
1422 * => caller must lock page queues if pages may be released.
1423 * => caller must make sure that anon-owned pages are not PG_RELEASED.
1424 */
1425
1426 void
1427 uvm_page_unbusy(struct vm_page **pgs, int npgs)
1428 {
1429 struct vm_page *pg;
1430 int i;
1431 UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist);
1432
1433 for (i = 0; i < npgs; i++) {
1434 pg = pgs[i];
1435 if (pg == NULL || pg == PGO_DONTCARE) {
1436 continue;
1437 }
1438
1439 LOCK_ASSERT(pg->uobject == NULL ||
1440 simple_lock_held(&pg->uobject->vmobjlock));
1441 LOCK_ASSERT(pg->uobject != NULL ||
1442 (pg->uanon != NULL &&
1443 simple_lock_held(&pg->uanon->an_lock)));
1444
1445 KASSERT(pg->flags & PG_BUSY);
1446 KASSERT((pg->flags & PG_PAGEOUT) == 0);
1447 if (pg->flags & PG_WANTED) {
1448 wakeup(pg);
1449 }
1450 if (pg->flags & PG_RELEASED) {
1451 UVMHIST_LOG(ubchist, "releasing pg %p", pg,0,0,0);
1452 KASSERT(pg->uobject != NULL ||
1453 (pg->uanon != NULL && pg->uanon->an_ref > 0));
1454 pg->flags &= ~PG_RELEASED;
1455 uvm_pagefree(pg);
1456 } else {
1457 UVMHIST_LOG(ubchist, "unbusying pg %p", pg,0,0,0);
1458 pg->flags &= ~(PG_WANTED|PG_BUSY);
1459 UVM_PAGE_OWN(pg, NULL);
1460 }
1461 }
1462 }
1463
1464 #if defined(UVM_PAGE_TRKOWN)
1465 /*
1466 * uvm_page_own: set or release page ownership
1467 *
1468 * => this is a debugging function that keeps track of who sets PG_BUSY
1469 * and where they do it. it can be used to track down problems
1470 * such a process setting "PG_BUSY" and never releasing it.
1471 * => page's object [if any] must be locked
1472 * => if "tag" is NULL then we are releasing page ownership
1473 */
1474 void
1475 uvm_page_own(struct vm_page *pg, const char *tag)
1476 {
1477 struct uvm_object *uobj;
1478 struct vm_anon *anon;
1479
1480 KASSERT((pg->flags & (PG_PAGEOUT|PG_RELEASED)) == 0);
1481
1482 uobj = pg->uobject;
1483 anon = pg->uanon;
1484 if (uobj != NULL) {
1485 LOCK_ASSERT(simple_lock_held(&uobj->vmobjlock));
1486 } else if (anon != NULL) {
1487 LOCK_ASSERT(simple_lock_held(&anon->an_lock));
1488 }
1489
1490 KASSERT((pg->flags & PG_WANTED) == 0);
1491
1492 /* gain ownership? */
1493 if (tag) {
1494 KASSERT((pg->flags & PG_BUSY) != 0);
1495 if (pg->owner_tag) {
1496 printf("uvm_page_own: page %p already owned "
1497 "by proc %d [%s]\n", pg,
1498 pg->owner, pg->owner_tag);
1499 panic("uvm_page_own");
1500 }
1501 pg->owner = (curproc) ? curproc->p_pid : (pid_t) -1;
1502 pg->owner_tag = tag;
1503 return;
1504 }
1505
1506 /* drop ownership */
1507 KASSERT((pg->flags & PG_BUSY) == 0);
1508 if (pg->owner_tag == NULL) {
1509 printf("uvm_page_own: dropping ownership of an non-owned "
1510 "page (%p)\n", pg);
1511 panic("uvm_page_own");
1512 }
1513 if (!uvmpdpol_pageisqueued_p(pg)) {
1514 KASSERT((pg->uanon == NULL && pg->uobject == NULL) ||
1515 pg->wire_count > 0);
1516 } else {
1517 KASSERT(pg->wire_count == 0);
1518 }
1519 pg->owner_tag = NULL;
1520 }
1521 #endif
1522
1523 /*
1524 * uvm_pageidlezero: zero free pages while the system is idle.
1525 *
1526 * => try to complete one color bucket at a time, to reduce our impact
1527 * on the CPU cache.
1528 * => we loop until we either reach the target or whichqs indicates that
1529 * there is a process ready to run.
1530 */
1531 void
1532 uvm_pageidlezero(void)
1533 {
1534 struct vm_page *pg;
1535 struct pgfreelist *pgfl;
1536 int free_list, s, firstbucket;
1537 static int nextbucket;
1538
1539 KERNEL_LOCK(1, NULL);
1540 s = uvm_lock_fpageq();
1541 firstbucket = nextbucket;
1542 do {
1543 if (sched_whichqs != 0)
1544 goto quit;
1545 if (uvmexp.zeropages >= UVM_PAGEZERO_TARGET) {
1546 uvm.page_idle_zero = FALSE;
1547 goto quit;
1548 }
1549 for (free_list = 0; free_list < VM_NFREELIST; free_list++) {
1550 pgfl = &uvm.page_free[free_list];
1551 while ((pg = TAILQ_FIRST(&pgfl->pgfl_buckets[
1552 nextbucket].pgfl_queues[PGFL_UNKNOWN])) != NULL) {
1553 if (sched_whichqs != 0)
1554 goto quit;
1555
1556 TAILQ_REMOVE(&pgfl->pgfl_buckets[
1557 nextbucket].pgfl_queues[PGFL_UNKNOWN],
1558 pg, pageq);
1559 uvmexp.free--;
1560 uvm_unlock_fpageq(s);
1561 KERNEL_UNLOCK_LAST(NULL);
1562 #ifdef PMAP_PAGEIDLEZERO
1563 if (!PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg))) {
1564
1565 /*
1566 * The machine-dependent code detected
1567 * some reason for us to abort zeroing
1568 * pages, probably because there is a
1569 * process now ready to run.
1570 */
1571
1572 KERNEL_LOCK(1, NULL);
1573 s = uvm_lock_fpageq();
1574 TAILQ_INSERT_HEAD(&pgfl->pgfl_buckets[
1575 nextbucket].pgfl_queues[
1576 PGFL_UNKNOWN], pg, pageq);
1577 uvmexp.free++;
1578 uvmexp.zeroaborts++;
1579 goto quit;
1580 }
1581 #else
1582 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1583 #endif /* PMAP_PAGEIDLEZERO */
1584 pg->flags |= PG_ZERO;
1585
1586 KERNEL_LOCK(1, NULL);
1587 s = uvm_lock_fpageq();
1588 TAILQ_INSERT_HEAD(&pgfl->pgfl_buckets[
1589 nextbucket].pgfl_queues[PGFL_ZEROS],
1590 pg, pageq);
1591 uvmexp.free++;
1592 uvmexp.zeropages++;
1593 }
1594 }
1595 nextbucket = (nextbucket + 1) & uvmexp.colormask;
1596 } while (nextbucket != firstbucket);
1597 quit:
1598 uvm_unlock_fpageq(s);
1599 KERNEL_UNLOCK_LAST(NULL);
1600 }
1601
1602 /*
1603 * uvm_lock_fpageq: lock the free page queue
1604 *
1605 * => free page queue can be accessed in interrupt context, so this
1606 * blocks all interrupts that can cause memory allocation, and
1607 * returns the previous interrupt level.
1608 */
1609
1610 int
1611 uvm_lock_fpageq(void)
1612 {
1613 int s;
1614
1615 s = splvm();
1616 simple_lock(&uvm.fpageqlock);
1617 return (s);
1618 }
1619
1620 /*
1621 * uvm_unlock_fpageq: unlock the free page queue
1622 *
1623 * => caller must supply interrupt level returned by uvm_lock_fpageq()
1624 * so that it may be restored.
1625 */
1626
1627 void
1628 uvm_unlock_fpageq(int s)
1629 {
1630
1631 simple_unlock(&uvm.fpageqlock);
1632 splx(s);
1633 }
1634
1635 /*
1636 * uvm_pagelookup: look up a page
1637 *
1638 * => caller should lock object to keep someone from pulling the page
1639 * out from under it
1640 */
1641
1642 struct vm_page *
1643 uvm_pagelookup(struct uvm_object *obj, voff_t off)
1644 {
1645 struct vm_page *pg;
1646 struct pglist *buck;
1647
1648 buck = &uvm.page_hash[uvm_pagehash(obj,off)];
1649 simple_lock(&uvm.hashlock);
1650 TAILQ_FOREACH(pg, buck, hashq) {
1651 if (pg->uobject == obj && pg->offset == off) {
1652 break;
1653 }
1654 }
1655 simple_unlock(&uvm.hashlock);
1656 KASSERT(pg == NULL || obj->uo_npages != 0);
1657 KASSERT(pg == NULL || (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
1658 (pg->flags & PG_BUSY) != 0);
1659 return(pg);
1660 }
1661
1662 /*
1663 * uvm_pagewire: wire the page, thus removing it from the daemon's grasp
1664 *
1665 * => caller must lock page queues
1666 */
1667
1668 void
1669 uvm_pagewire(struct vm_page *pg)
1670 {
1671 UVM_LOCK_ASSERT_PAGEQ();
1672 #if defined(READAHEAD_STATS)
1673 if ((pg->pqflags & PQ_READAHEAD) != 0) {
1674 uvm_ra_hit.ev_count++;
1675 pg->pqflags &= ~PQ_READAHEAD;
1676 }
1677 #endif /* defined(READAHEAD_STATS) */
1678 if (pg->wire_count == 0) {
1679 uvm_pagedequeue(pg);
1680 uvmexp.wired++;
1681 }
1682 pg->wire_count++;
1683 }
1684
1685 /*
1686 * uvm_pageunwire: unwire the page.
1687 *
1688 * => activate if wire count goes to zero.
1689 * => caller must lock page queues
1690 */
1691
1692 void
1693 uvm_pageunwire(struct vm_page *pg)
1694 {
1695 UVM_LOCK_ASSERT_PAGEQ();
1696 pg->wire_count--;
1697 if (pg->wire_count == 0) {
1698 uvm_pageactivate(pg);
1699 uvmexp.wired--;
1700 }
1701 }
1702
1703 /*
1704 * uvm_pagedeactivate: deactivate page
1705 *
1706 * => caller must lock page queues
1707 * => caller must check to make sure page is not wired
1708 * => object that page belongs to must be locked (so we can adjust pg->flags)
1709 * => caller must clear the reference on the page before calling
1710 */
1711
1712 void
1713 uvm_pagedeactivate(struct vm_page *pg)
1714 {
1715
1716 UVM_LOCK_ASSERT_PAGEQ();
1717 KASSERT(pg->wire_count != 0 || uvmpdpol_pageisqueued_p(pg));
1718 uvmpdpol_pagedeactivate(pg);
1719 }
1720
1721 /*
1722 * uvm_pageactivate: activate page
1723 *
1724 * => caller must lock page queues
1725 */
1726
1727 void
1728 uvm_pageactivate(struct vm_page *pg)
1729 {
1730
1731 UVM_LOCK_ASSERT_PAGEQ();
1732 #if defined(READAHEAD_STATS)
1733 if ((pg->pqflags & PQ_READAHEAD) != 0) {
1734 uvm_ra_hit.ev_count++;
1735 pg->pqflags &= ~PQ_READAHEAD;
1736 }
1737 #endif /* defined(READAHEAD_STATS) */
1738 if (pg->wire_count != 0) {
1739 return;
1740 }
1741 uvmpdpol_pageactivate(pg);
1742 }
1743
1744 /*
1745 * uvm_pagedequeue: remove a page from any paging queue
1746 */
1747
1748 void
1749 uvm_pagedequeue(struct vm_page *pg)
1750 {
1751
1752 #if defined(LOCKDEBUG)
1753 if (uvmpdpol_pageisqueued_p(pg)) {
1754 UVM_LOCK_ASSERT_PAGEQ();
1755 }
1756 #endif /* defined(LOCKDEBUG) */
1757 uvmpdpol_pagedequeue(pg);
1758 }
1759
1760 /*
1761 * uvm_pageenqueue: add a page to a paging queue without activating.
1762 * used where a page is not really demanded (yet). eg. read-ahead
1763 */
1764
1765 void
1766 uvm_pageenqueue(struct vm_page *pg)
1767 {
1768
1769 UVM_LOCK_ASSERT_PAGEQ();
1770 if (pg->wire_count != 0) {
1771 return;
1772 }
1773 uvmpdpol_pageenqueue(pg);
1774 }
1775
1776 /*
1777 * uvm_pagezero: zero fill a page
1778 *
1779 * => if page is part of an object then the object should be locked
1780 * to protect pg->flags.
1781 */
1782
1783 void
1784 uvm_pagezero(struct vm_page *pg)
1785 {
1786 pg->flags &= ~PG_CLEAN;
1787 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1788 }
1789
1790 /*
1791 * uvm_pagecopy: copy a page
1792 *
1793 * => if page is part of an object then the object should be locked
1794 * to protect pg->flags.
1795 */
1796
1797 void
1798 uvm_pagecopy(struct vm_page *src, struct vm_page *dst)
1799 {
1800
1801 dst->flags &= ~PG_CLEAN;
1802 pmap_copy_page(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst));
1803 }
1804
1805 /*
1806 * uvm_page_lookup_freelist: look up the free list for the specified page
1807 */
1808
1809 int
1810 uvm_page_lookup_freelist(struct vm_page *pg)
1811 {
1812 int lcv;
1813
1814 lcv = vm_physseg_find(atop(VM_PAGE_TO_PHYS(pg)), NULL);
1815 KASSERT(lcv != -1);
1816 return (vm_physmem[lcv].free_list);
1817 }
1818