uvm_page.c revision 1.43 1 /* $NetBSD: uvm_page.c,v 1.43 2000/11/09 19:15:28 christos 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/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
76 #include <sys/sched.h>
77
78 #define UVM_PAGE /* pull in uvm_page.h functions */
79 #include <uvm/uvm.h>
80
81 /*
82 * global vars... XXXCDC: move to uvm. structure.
83 */
84
85 /*
86 * physical memory config is stored in vm_physmem.
87 */
88
89 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX]; /* XXXCDC: uvm.physmem */
90 int vm_nphysseg = 0; /* XXXCDC: uvm.nphysseg */
91
92 /*
93 * Some supported CPUs in a given architecture don't support all
94 * of the things necessary to do idle page zero'ing efficiently.
95 * We therefore provide a way to disable it from machdep code here.
96 */
97
98 boolean_t vm_page_zero_enable = TRUE;
99
100 /*
101 * local variables
102 */
103
104 /*
105 * these variables record the values returned by vm_page_bootstrap,
106 * for debugging purposes. The implementation of uvm_pageboot_alloc
107 * and pmap_startup here also uses them internally.
108 */
109
110 static vaddr_t virtual_space_start;
111 static vaddr_t virtual_space_end;
112
113 /*
114 * we use a hash table with only one bucket during bootup. we will
115 * later rehash (resize) the hash table once the allocator is ready.
116 * we static allocate the one bootstrap bucket below...
117 */
118
119 static struct pglist uvm_bootbucket;
120
121 /*
122 * local prototypes
123 */
124
125 static void uvm_pageinsert __P((struct vm_page *));
126
127
128 /*
129 * inline functions
130 */
131
132 /*
133 * uvm_pageinsert: insert a page in the object and the hash table
134 *
135 * => caller must lock object
136 * => caller must lock page queues
137 * => call should have already set pg's object and offset pointers
138 * and bumped the version counter
139 */
140
141 __inline static void
142 uvm_pageinsert(pg)
143 struct vm_page *pg;
144 {
145 struct pglist *buck;
146 int s;
147
148 #ifdef DIAGNOSTIC
149 if (pg->flags & PG_TABLED)
150 panic("uvm_pageinsert: already inserted");
151 #endif
152
153 buck = &uvm.page_hash[uvm_pagehash(pg->uobject,pg->offset)];
154 s = splimp();
155 simple_lock(&uvm.hashlock);
156 TAILQ_INSERT_TAIL(buck, pg, hashq); /* put in hash */
157 simple_unlock(&uvm.hashlock);
158 splx(s);
159
160 TAILQ_INSERT_TAIL(&pg->uobject->memq, pg, listq); /* put in object */
161 pg->flags |= PG_TABLED;
162 pg->uobject->uo_npages++;
163
164 }
165
166 /*
167 * uvm_page_remove: remove page from object and hash
168 *
169 * => caller must lock object
170 * => caller must lock page queues
171 */
172
173 void __inline
174 uvm_pageremove(pg)
175 struct vm_page *pg;
176 {
177 struct pglist *buck;
178 int s;
179
180 #ifdef DIAGNOSTIC
181 if ((pg->flags & (PG_FAULTING)) != 0)
182 panic("uvm_pageremove: page is faulting");
183 #endif
184
185 if ((pg->flags & PG_TABLED) == 0)
186 return; /* XXX: log */
187
188 buck = &uvm.page_hash[uvm_pagehash(pg->uobject,pg->offset)];
189 s = splimp();
190 simple_lock(&uvm.hashlock);
191 TAILQ_REMOVE(buck, pg, hashq);
192 simple_unlock(&uvm.hashlock);
193 splx(s);
194
195 /* object should be locked */
196 TAILQ_REMOVE(&pg->uobject->memq, pg, listq);
197
198 pg->flags &= ~PG_TABLED;
199 pg->uobject->uo_npages--;
200 pg->uobject = NULL;
201 pg->version++;
202
203 }
204
205 /*
206 * uvm_page_init: init the page system. called from uvm_init().
207 *
208 * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
209 */
210
211 void
212 uvm_page_init(kvm_startp, kvm_endp)
213 vaddr_t *kvm_startp, *kvm_endp;
214 {
215 vsize_t freepages, pagecount, n;
216 vm_page_t pagearray;
217 int lcv, i;
218 paddr_t paddr;
219
220
221 /*
222 * step 1: init the page queues and page queue locks
223 */
224 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
225 for (i = 0; i < PGFL_NQUEUES; i++)
226 TAILQ_INIT(&uvm.page_free[lcv].pgfl_queues[i]);
227 }
228 TAILQ_INIT(&uvm.page_active);
229 TAILQ_INIT(&uvm.page_inactive_swp);
230 TAILQ_INIT(&uvm.page_inactive_obj);
231 simple_lock_init(&uvm.pageqlock);
232 simple_lock_init(&uvm.fpageqlock);
233
234 /*
235 * step 2: init the <obj,offset> => <page> hash table. for now
236 * we just have one bucket (the bootstrap bucket). later on we
237 * will allocate new buckets as we dynamically resize the hash table.
238 */
239
240 uvm.page_nhash = 1; /* 1 bucket */
241 uvm.page_hashmask = 0; /* mask for hash function */
242 uvm.page_hash = &uvm_bootbucket; /* install bootstrap bucket */
243 TAILQ_INIT(uvm.page_hash); /* init hash table */
244 simple_lock_init(&uvm.hashlock); /* init hash table lock */
245
246 /*
247 * step 3: allocate vm_page structures.
248 */
249
250 /*
251 * sanity check:
252 * before calling this function the MD code is expected to register
253 * some free RAM with the uvm_page_physload() function. our job
254 * now is to allocate vm_page structures for this memory.
255 */
256
257 if (vm_nphysseg == 0)
258 panic("uvm_page_bootstrap: no memory pre-allocated");
259
260 /*
261 * first calculate the number of free pages...
262 *
263 * note that we use start/end rather than avail_start/avail_end.
264 * this allows us to allocate extra vm_page structures in case we
265 * want to return some memory to the pool after booting.
266 */
267
268 freepages = 0;
269 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
270 freepages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
271
272 /*
273 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
274 * use. for each page of memory we use we need a vm_page structure.
275 * thus, the total number of pages we can use is the total size of
276 * the memory divided by the PAGE_SIZE plus the size of the vm_page
277 * structure. we add one to freepages as a fudge factor to avoid
278 * truncation errors (since we can only allocate in terms of whole
279 * pages).
280 */
281
282 pagecount = ((freepages + 1) << PAGE_SHIFT) /
283 (PAGE_SIZE + sizeof(struct vm_page));
284 pagearray = (vm_page_t)uvm_pageboot_alloc(pagecount *
285 sizeof(struct vm_page));
286 memset(pagearray, 0, pagecount * sizeof(struct vm_page));
287
288 /*
289 * step 4: init the vm_page structures and put them in the correct
290 * place...
291 */
292
293 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
294
295 n = vm_physmem[lcv].end - vm_physmem[lcv].start;
296 if (n > pagecount) {
297 printf("uvm_page_init: lost %ld page(s) in init\n",
298 (long)(n - pagecount));
299 panic("uvm_page_init"); /* XXXCDC: shouldn't happen? */
300 /* n = pagecount; */
301 }
302 /* set up page array pointers */
303 vm_physmem[lcv].pgs = pagearray;
304 pagearray += n;
305 pagecount -= n;
306 vm_physmem[lcv].lastpg = vm_physmem[lcv].pgs + (n - 1);
307
308 /* init and free vm_pages (we've already zeroed them) */
309 paddr = ptoa(vm_physmem[lcv].start);
310 for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
311 vm_physmem[lcv].pgs[i].phys_addr = paddr;
312 if (atop(paddr) >= vm_physmem[lcv].avail_start &&
313 atop(paddr) <= vm_physmem[lcv].avail_end) {
314 uvmexp.npages++;
315 /* add page to free pool */
316 uvm_pagefree(&vm_physmem[lcv].pgs[i]);
317 }
318 }
319 }
320 /*
321 * step 5: pass up the values of virtual_space_start and
322 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
323 * layers of the VM.
324 */
325
326 *kvm_startp = round_page(virtual_space_start);
327 *kvm_endp = trunc_page(virtual_space_end);
328
329 /*
330 * step 6: init pagedaemon lock
331 */
332
333 simple_lock_init(&uvm.pagedaemon_lock);
334
335 /*
336 * step 7: init reserve thresholds
337 * XXXCDC - values may need adjusting
338 */
339 uvmexp.reserve_pagedaemon = 1;
340 uvmexp.reserve_kernel = 5;
341
342 /*
343 * step 8: determine if we should zero pages in the idle
344 * loop.
345 *
346 * XXXJRT - might consider zero'ing up to the target *now*,
347 * but that could take an awfully long time if you
348 * have a lot of memory.
349 */
350 uvm.page_idle_zero = vm_page_zero_enable;
351
352 /*
353 * done!
354 */
355
356 uvm.page_init_done = TRUE;
357 }
358
359 /*
360 * uvm_setpagesize: set the page size
361 *
362 * => sets page_shift and page_mask from uvmexp.pagesize.
363 * => XXXCDC: move global vars.
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 #ifdef DIAGNOSTIC
893 /* sanity check */
894 if (obj && anon)
895 panic("uvm_pagealloc: obj and anon != NULL");
896 #endif
897
898 s = uvm_lock_fpageq(); /* lock free page queue */
899
900 /*
901 * check to see if we need to generate some free pages waking
902 * the pagedaemon.
903 */
904
905 if (uvmexp.free < uvmexp.freemin || (uvmexp.free < uvmexp.freetarg &&
906 uvmexp.inactive < uvmexp.inactarg))
907 wakeup(&uvm.pagedaemon);
908
909 /*
910 * fail if any of these conditions is true:
911 * [1] there really are no free pages, or
912 * [2] only kernel "reserved" pages remain and
913 * the page isn't being allocated to a kernel object.
914 * [3] only pagedaemon "reserved" pages remain and
915 * the requestor isn't the pagedaemon.
916 */
917
918 use_reserve = (flags & UVM_PGA_USERESERVE) ||
919 (obj && UVM_OBJ_IS_KERN_OBJECT(obj));
920 if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) ||
921 (uvmexp.free <= uvmexp.reserve_pagedaemon &&
922 !(use_reserve && curproc == uvm.pagedaemon_proc)))
923 goto fail;
924
925 #if PGFL_NQUEUES != 2
926 #error uvm_pagealloc_strat needs to be updated
927 #endif
928
929 /*
930 * If we want a zero'd page, try the ZEROS queue first, otherwise
931 * we try the UNKNOWN queue first.
932 */
933 if (flags & UVM_PGA_ZERO) {
934 try1 = PGFL_ZEROS;
935 try2 = PGFL_UNKNOWN;
936 } else {
937 try1 = PGFL_UNKNOWN;
938 try2 = PGFL_ZEROS;
939 }
940
941 again:
942 switch (strat) {
943 case UVM_PGA_STRAT_NORMAL:
944 /* Check all freelists in descending priority order. */
945 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
946 pgfl = &uvm.page_free[lcv];
947 if ((pg = TAILQ_FIRST((freeq =
948 &pgfl->pgfl_queues[try1]))) != NULL ||
949 (pg = TAILQ_FIRST((freeq =
950 &pgfl->pgfl_queues[try2]))) != NULL)
951 goto gotit;
952 }
953
954 /* No pages free! */
955 goto fail;
956
957 case UVM_PGA_STRAT_ONLY:
958 case UVM_PGA_STRAT_FALLBACK:
959 /* Attempt to allocate from the specified free list. */
960 #ifdef DIAGNOSTIC
961 if (free_list >= VM_NFREELIST || free_list < 0)
962 panic("uvm_pagealloc_strat: bad free list %d",
963 free_list);
964 #endif
965 pgfl = &uvm.page_free[free_list];
966 if ((pg = TAILQ_FIRST((freeq =
967 &pgfl->pgfl_queues[try1]))) != NULL ||
968 (pg = TAILQ_FIRST((freeq =
969 &pgfl->pgfl_queues[try2]))) != NULL)
970 goto gotit;
971
972 /* Fall back, if possible. */
973 if (strat == UVM_PGA_STRAT_FALLBACK) {
974 strat = UVM_PGA_STRAT_NORMAL;
975 goto again;
976 }
977
978 /* No pages free! */
979 goto fail;
980
981 default:
982 panic("uvm_pagealloc_strat: bad strat %d", strat);
983 /* NOTREACHED */
984 }
985
986 gotit:
987 TAILQ_REMOVE(freeq, pg, pageq);
988 uvmexp.free--;
989
990 /* update zero'd page count */
991 if (pg->flags & PG_ZERO)
992 uvmexp.zeropages--;
993
994 /*
995 * update allocation statistics and remember if we have to
996 * zero the page
997 */
998 if (flags & UVM_PGA_ZERO) {
999 if (pg->flags & PG_ZERO) {
1000 uvmexp.pga_zerohit++;
1001 zeroit = 0;
1002 } else {
1003 uvmexp.pga_zeromiss++;
1004 zeroit = 1;
1005 }
1006 }
1007
1008 uvm_unlock_fpageq(s); /* unlock free page queue */
1009
1010 pg->offset = off;
1011 pg->uobject = obj;
1012 pg->uanon = anon;
1013 pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
1014 pg->version++;
1015 pg->wire_count = 0;
1016 pg->loan_count = 0;
1017 if (anon) {
1018 anon->u.an_page = pg;
1019 pg->pqflags = PQ_ANON;
1020 } else {
1021 if (obj)
1022 uvm_pageinsert(pg);
1023 pg->pqflags = 0;
1024 }
1025 #if defined(UVM_PAGE_TRKOWN)
1026 pg->owner_tag = NULL;
1027 #endif
1028 UVM_PAGE_OWN(pg, "new alloc");
1029
1030 if (flags & UVM_PGA_ZERO) {
1031 /*
1032 * A zero'd page is not clean. If we got a page not already
1033 * zero'd, then we have to zero it ourselves.
1034 */
1035 pg->flags &= ~PG_CLEAN;
1036 if (zeroit)
1037 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1038 }
1039
1040 return(pg);
1041
1042 fail:
1043 uvm_unlock_fpageq(s);
1044 return (NULL);
1045 }
1046
1047 /*
1048 * uvm_pagerealloc: reallocate a page from one object to another
1049 *
1050 * => both objects must be locked
1051 */
1052
1053 void
1054 uvm_pagerealloc(pg, newobj, newoff)
1055 struct vm_page *pg;
1056 struct uvm_object *newobj;
1057 voff_t newoff;
1058 {
1059 /*
1060 * remove it from the old object
1061 */
1062
1063 if (pg->uobject) {
1064 uvm_pageremove(pg);
1065 }
1066
1067 /*
1068 * put it in the new object
1069 */
1070
1071 if (newobj) {
1072 pg->uobject = newobj;
1073 pg->offset = newoff;
1074 pg->version++;
1075 uvm_pageinsert(pg);
1076 }
1077
1078 return;
1079 }
1080
1081
1082 /*
1083 * uvm_pagefree: free page
1084 *
1085 * => erase page's identity (i.e. remove from hash/object)
1086 * => put page on free list
1087 * => caller must lock owning object (either anon or uvm_object)
1088 * => caller must lock page queues
1089 * => assumes all valid mappings of pg are gone
1090 */
1091
1092 void uvm_pagefree(pg)
1093
1094 struct vm_page *pg;
1095
1096 {
1097 int s;
1098 int saved_loan_count = pg->loan_count;
1099
1100 /*
1101 * if the page was an object page (and thus "TABLED"), remove it
1102 * from the object.
1103 */
1104
1105 if (pg->flags & PG_TABLED) {
1106
1107 /*
1108 * if the object page is on loan we are going to drop ownership.
1109 * it is possible that an anon will take over as owner for this
1110 * page later on. the anon will want a !PG_CLEAN page so that
1111 * it knows it needs to allocate swap if it wants to page the
1112 * page out.
1113 */
1114
1115 if (saved_loan_count)
1116 pg->flags &= ~PG_CLEAN; /* in case an anon takes over */
1117
1118 uvm_pageremove(pg);
1119
1120 /*
1121 * if our page was on loan, then we just lost control over it
1122 * (in fact, if it was loaned to an anon, the anon may have
1123 * already taken over ownership of the page by now and thus
1124 * changed the loan_count [e.g. in uvmfault_anonget()]) we just
1125 * return (when the last loan is dropped, then the page can be
1126 * freed by whatever was holding the last loan).
1127 */
1128 if (saved_loan_count)
1129 return;
1130
1131 } else if (saved_loan_count && (pg->pqflags & PQ_ANON)) {
1132
1133 /*
1134 * if our page is owned by an anon and is loaned out to the
1135 * kernel then we just want to drop ownership and return.
1136 * the kernel must free the page when all its loans clear ...
1137 * note that the kernel can't change the loan status of our
1138 * page as long as we are holding PQ lock.
1139 */
1140 pg->pqflags &= ~PQ_ANON;
1141 pg->uanon = NULL;
1142 return;
1143 }
1144
1145 #ifdef DIAGNOSTIC
1146 if (saved_loan_count) {
1147 printf("uvm_pagefree: warning: freeing page with a loan "
1148 "count of %d\n", saved_loan_count);
1149 panic("uvm_pagefree: loan count");
1150 }
1151 #endif
1152
1153
1154 /*
1155 * now remove the page from the queues
1156 */
1157
1158 if (pg->pqflags & PQ_ACTIVE) {
1159 TAILQ_REMOVE(&uvm.page_active, pg, pageq);
1160 pg->pqflags &= ~PQ_ACTIVE;
1161 uvmexp.active--;
1162 }
1163 if (pg->pqflags & PQ_INACTIVE) {
1164 if (pg->pqflags & PQ_SWAPBACKED)
1165 TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
1166 else
1167 TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
1168 pg->pqflags &= ~PQ_INACTIVE;
1169 uvmexp.inactive--;
1170 }
1171
1172 /*
1173 * if the page was wired, unwire it now.
1174 */
1175 if (pg->wire_count) {
1176 pg->wire_count = 0;
1177 uvmexp.wired--;
1178 }
1179
1180 /*
1181 * and put on free queue
1182 */
1183
1184 pg->flags &= ~PG_ZERO;
1185
1186 s = uvm_lock_fpageq();
1187 TAILQ_INSERT_TAIL(&uvm.page_free[
1188 uvm_page_lookup_freelist(pg)].pgfl_queues[PGFL_UNKNOWN], pg, pageq);
1189 pg->pqflags = PQ_FREE;
1190 #ifdef DEBUG
1191 pg->uobject = (void *)0xdeadbeef;
1192 pg->offset = 0xdeadbeef;
1193 pg->uanon = (void *)0xdeadbeef;
1194 #endif
1195 uvmexp.free++;
1196
1197 if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
1198 uvm.page_idle_zero = vm_page_zero_enable;
1199
1200 uvm_unlock_fpageq(s);
1201 }
1202
1203 #if defined(UVM_PAGE_TRKOWN)
1204 /*
1205 * uvm_page_own: set or release page ownership
1206 *
1207 * => this is a debugging function that keeps track of who sets PG_BUSY
1208 * and where they do it. it can be used to track down problems
1209 * such a process setting "PG_BUSY" and never releasing it.
1210 * => page's object [if any] must be locked
1211 * => if "tag" is NULL then we are releasing page ownership
1212 */
1213 void
1214 uvm_page_own(pg, tag)
1215 struct vm_page *pg;
1216 char *tag;
1217 {
1218 /* gain ownership? */
1219 if (tag) {
1220 if (pg->owner_tag) {
1221 printf("uvm_page_own: page %p already owned "
1222 "by proc %d [%s]\n", pg,
1223 pg->owner, pg->owner_tag);
1224 panic("uvm_page_own");
1225 }
1226 pg->owner = (curproc) ? curproc->p_pid : (pid_t) -1;
1227 pg->owner_tag = tag;
1228 return;
1229 }
1230
1231 /* drop ownership */
1232 if (pg->owner_tag == NULL) {
1233 printf("uvm_page_own: dropping ownership of an non-owned "
1234 "page (%p)\n", pg);
1235 panic("uvm_page_own");
1236 }
1237 pg->owner_tag = NULL;
1238 return;
1239 }
1240 #endif
1241
1242 /*
1243 * uvm_pageidlezero: zero free pages while the system is idle.
1244 *
1245 * => we do at least one iteration per call, if we are below the target.
1246 * => we loop until we either reach the target or whichqs indicates that
1247 * there is a process ready to run.
1248 */
1249 void
1250 uvm_pageidlezero()
1251 {
1252 struct vm_page *pg;
1253 struct pgfreelist *pgfl;
1254 int free_list, s;
1255
1256 do {
1257 s = uvm_lock_fpageq();
1258
1259 if (uvmexp.zeropages >= UVM_PAGEZERO_TARGET) {
1260 uvm.page_idle_zero = FALSE;
1261 uvm_unlock_fpageq(s);
1262 return;
1263 }
1264
1265 for (free_list = 0; free_list < VM_NFREELIST; free_list++) {
1266 pgfl = &uvm.page_free[free_list];
1267 if ((pg = TAILQ_FIRST(&pgfl->pgfl_queues[
1268 PGFL_UNKNOWN])) != NULL)
1269 break;
1270 }
1271
1272 if (pg == NULL) {
1273 /*
1274 * No non-zero'd pages; don't bother trying again
1275 * until we know we have non-zero'd pages free.
1276 */
1277 uvm.page_idle_zero = FALSE;
1278 uvm_unlock_fpageq(s);
1279 return;
1280 }
1281
1282 TAILQ_REMOVE(&pgfl->pgfl_queues[PGFL_UNKNOWN], pg, pageq);
1283 uvmexp.free--;
1284 uvm_unlock_fpageq(s);
1285
1286 #ifdef PMAP_PAGEIDLEZERO
1287 if (PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg)) == FALSE) {
1288 /*
1289 * The machine-dependent code detected some
1290 * reason for us to abort zeroing pages,
1291 * probably because there is a process now
1292 * ready to run.
1293 */
1294 s = uvm_lock_fpageq();
1295 TAILQ_INSERT_HEAD(&pgfl->pgfl_queues[PGFL_UNKNOWN],
1296 pg, pageq);
1297 uvmexp.free++;
1298 uvmexp.zeroaborts++;
1299 uvm_unlock_fpageq(s);
1300 return;
1301 }
1302 #else
1303 /*
1304 * XXX This will toast the cache unless the pmap_zero_page()
1305 * XXX implementation does uncached access.
1306 */
1307 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1308 #endif
1309 pg->flags |= PG_ZERO;
1310
1311 s = uvm_lock_fpageq();
1312 TAILQ_INSERT_HEAD(&pgfl->pgfl_queues[PGFL_ZEROS], pg, pageq);
1313 uvmexp.free++;
1314 uvmexp.zeropages++;
1315 uvm_unlock_fpageq(s);
1316 } while (sched_whichqs == 0);
1317 }
1318