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