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