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