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