uvm_page.c revision 1.9 1 /* $NetBSD: uvm_page.c,v 1.9 1998/04/16 03:54:35 thorpej Exp $ */
2
3 /*
4 * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
5 * >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
6 */
7 /*
8 * Copyright (c) 1997 Charles D. Cranor and Washington University.
9 * Copyright (c) 1991, 1993, The Regents of the University of California.
10 *
11 * All rights reserved.
12 *
13 * This code is derived from software contributed to Berkeley by
14 * The Mach Operating System project at Carnegie-Mellon University.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by Charles D. Cranor,
27 * Washington University, the University of California, Berkeley and
28 * its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 *
45 * @(#)vm_page.c 8.3 (Berkeley) 3/21/94
46 * from: Id: uvm_page.c,v 1.1.2.18 1998/02/06 05:24:42 chs Exp
47 *
48 *
49 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
50 * All rights reserved.
51 *
52 * Permission to use, copy, modify and distribute this software and
53 * its documentation is hereby granted, provided that both the copyright
54 * notice and this permission notice appear in all copies of the
55 * software, derivative works or modified versions, and any portions
56 * thereof, and that both notices appear in supporting documentation.
57 *
58 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
59 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
60 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
61 *
62 * Carnegie Mellon requests users of this software to return to
63 *
64 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
65 * School of Computer Science
66 * Carnegie Mellon University
67 * Pittsburgh PA 15213-3890
68 *
69 * any improvements or extensions that they make and grant Carnegie the
70 * rights to redistribute these changes.
71 */
72
73 /*
74 * uvm_page.c: page ops.
75 */
76
77 #include "opt_pmap_new.h"
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/malloc.h>
82 #include <sys/mount.h>
83 #include <sys/proc.h>
84
85 #include <vm/vm.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_kern.h>
88
89 #include <sys/syscallargs.h>
90
91 #define UVM_PAGE /* pull in uvm_page.h functions */
92 #include <uvm/uvm.h>
93
94 /*
95 * global vars... XXXCDC: move to uvm. structure.
96 */
97
98 /*
99 * physical memory config is stored in vm_physmem.
100 */
101
102 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX]; /* XXXCDC: uvm.physmem */
103 int vm_nphysseg = 0; /* XXXCDC: uvm.nphysseg */
104
105 /*
106 * local variables
107 */
108
109 /*
110 * these variables record the values returned by vm_page_bootstrap,
111 * for debugging purposes. The implementation of uvm_pageboot_alloc
112 * and pmap_startup here also uses them internally.
113 */
114
115 static vm_offset_t virtual_space_start;
116 static vm_offset_t virtual_space_end;
117
118 /*
119 * we use a hash table with only one bucket during bootup. we will
120 * later rehash (resize) the hash table once malloc() is ready.
121 * we static allocate the bootstrap bucket below...
122 */
123
124 static struct pglist uvm_bootbucket;
125
126 /*
127 * local prototypes
128 */
129
130 static void uvm_pageinsert __P((struct vm_page *));
131 #if !defined(PMAP_STEAL_MEMORY)
132 static boolean_t uvm_page_physget __P((vm_offset_t *));
133 #endif
134
135
136 /*
137 * inline functions
138 */
139
140 /*
141 * uvm_pageinsert: insert a page in the object and the hash table
142 *
143 * => caller must lock object
144 * => caller must lock page queues
145 * => call should have already set pg's object and offset pointers
146 * and bumped the version counter
147 */
148
149 __inline static void
150 uvm_pageinsert(pg)
151 struct vm_page *pg;
152 {
153 struct pglist *buck;
154 int s;
155
156 #ifdef DIAGNOSTIC
157 if (pg->flags & PG_TABLED)
158 panic("uvm_pageinsert: already inserted");
159 #endif
160
161 buck = &uvm.page_hash[uvm_pagehash(pg->uobject,pg->offset)];
162 s = splimp();
163 simple_lock(&uvm.hashlock);
164 TAILQ_INSERT_TAIL(buck, pg, hashq); /* put in hash */
165 simple_unlock(&uvm.hashlock);
166 splx(s);
167
168 TAILQ_INSERT_TAIL(&pg->uobject->memq, pg, listq); /* put in object */
169 pg->flags |= PG_TABLED;
170 pg->uobject->uo_npages++;
171
172 }
173
174 /*
175 * uvm_page_remove: remove page from object and hash
176 *
177 * => caller must lock object
178 * => caller must lock page queues
179 */
180
181 void __inline
182 uvm_pageremove(pg)
183 struct vm_page *pg;
184 {
185 struct pglist *buck;
186 int s;
187
188 #ifdef DIAGNOSTIC
189 if ((pg->flags & (PG_FAULTING)) != 0)
190 panic("uvm_pageremove: page is faulting");
191 #endif
192
193 if ((pg->flags & PG_TABLED) == 0)
194 return; /* XXX: log */
195
196 buck = &uvm.page_hash[uvm_pagehash(pg->uobject,pg->offset)];
197 s = splimp();
198 simple_lock(&uvm.hashlock);
199 TAILQ_REMOVE(buck, pg, hashq);
200 simple_unlock(&uvm.hashlock);
201 splx(s);
202
203 /* object should be locked */
204 TAILQ_REMOVE(&pg->uobject->memq, pg, listq);
205
206 pg->flags &= ~PG_TABLED;
207 pg->uobject->uo_npages--;
208 pg->uobject = NULL;
209 pg->version++;
210
211 }
212
213 /*
214 * uvm_page_init: init the page system. called from uvm_init().
215 *
216 * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
217 */
218
219 void
220 uvm_page_init(kvm_startp, kvm_endp)
221 vm_offset_t *kvm_startp, *kvm_endp;
222 {
223 int freepages, pagecount;
224 vm_page_t pagearray;
225 int lcv, n, i;
226 vm_offset_t paddr;
227
228
229 /*
230 * step 1: init the page queues and page queue locks
231 */
232
233 TAILQ_INIT(&uvm.page_free);
234 TAILQ_INIT(&uvm.page_active);
235 TAILQ_INIT(&uvm.page_inactive_swp);
236 TAILQ_INIT(&uvm.page_inactive_obj);
237 simple_lock_init(&uvm.pageqlock);
238 simple_lock_init(&uvm.fpageqlock);
239
240 /*
241 * step 2: init the <obj,offset> => <page> hash table. for now
242 * we just have one bucket (the bootstrap bucket). later on we
243 * will malloc() new buckets as we dynamically resize the hash table.
244 */
245
246 uvm.page_nhash = 1; /* 1 bucket */
247 uvm.page_hashmask = 0; /* mask for hash function */
248 uvm.page_hash = &uvm_bootbucket; /* install bootstrap bucket */
249 TAILQ_INIT(uvm.page_hash); /* init hash table */
250 simple_lock_init(&uvm.hashlock); /* init hash table lock */
251
252 /*
253 * step 3: allocate vm_page structures.
254 */
255
256 /*
257 * sanity check:
258 * before calling this function the MD code is expected to register
259 * some free RAM with the uvm_page_physload() function. our job
260 * now is to allocate vm_page structures for this memory.
261 */
262
263 if (vm_nphysseg == 0)
264 panic("vm_page_bootstrap: no memory pre-allocated");
265
266 /*
267 * first calculate the number of free pages...
268 *
269 * note that we use start/end rather than avail_start/avail_end.
270 * this allows us to allocate extra vm_page structures in case we
271 * want to return some memory to the pool after booting.
272 */
273
274 freepages = 0;
275 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
276 freepages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
277
278 /*
279 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
280 * use. for each page of memory we use we need a vm_page structure.
281 * thus, the total number of pages we can use is the total size of
282 * the memory divided by the PAGE_SIZE plus the size of the vm_page
283 * structure. we add one to freepages as a fudge factor to avoid
284 * truncation errors (since we can only allocate in terms of whole
285 * pages).
286 */
287
288 pagecount = (PAGE_SIZE * (freepages + 1)) /
289 (PAGE_SIZE + sizeof(struct vm_page));
290 pagearray = (vm_page_t)uvm_pageboot_alloc(pagecount *
291 sizeof(struct vm_page));
292 bzero(pagearray, pagecount * sizeof(struct vm_page));
293
294 /*
295 * step 4: init the vm_page structures and put them in the correct
296 * place...
297 */
298
299 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
300
301 n = vm_physmem[lcv].end - vm_physmem[lcv].start;
302 if (n > pagecount) {
303 printf("uvm_page_init: lost %d page(s) in init\n",
304 n - pagecount);
305 panic("uvm_page_init"); /* XXXCDC: shouldn't happen? */
306 /* n = pagecount; */
307 }
308 /* set up page array pointers */
309 vm_physmem[lcv].pgs = pagearray;
310 pagearray += n;
311 pagecount -= n;
312 vm_physmem[lcv].lastpg = vm_physmem[lcv].pgs + (n - 1);
313
314 /* init and free vm_pages (we've already bzero'd them) */
315 paddr = ptoa(vm_physmem[lcv].start);
316 for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
317 vm_physmem[lcv].pgs[i].phys_addr = paddr;
318 if (atop(paddr) >= vm_physmem[lcv].avail_start &&
319 atop(paddr) <= vm_physmem[lcv].avail_end) {
320 uvmexp.npages++;
321 /* add page to free pool */
322 uvm_pagefree(&vm_physmem[lcv].pgs[i]);
323 }
324 }
325 }
326 /*
327 * step 5: pass up the values of virtual_space_start and
328 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
329 * layers of the VM.
330 */
331
332 *kvm_startp = round_page(virtual_space_start);
333 *kvm_endp = trunc_page(virtual_space_end);
334
335 /*
336 * step 6: init pagedaemon lock
337 */
338
339 simple_lock_init(&uvm.pagedaemon_lock);
340
341 /*
342 * step 7: init reserve thresholds
343 * XXXCDC - values may need adjusting
344 */
345 uvmexp.reserve_pagedaemon = 1;
346 uvmexp.reserve_kernel = 5;
347
348 /*
349 * done!
350 */
351
352 }
353
354 /*
355 * uvm_setpagesize: set the page size
356 *
357 * => sets page_shift and page_mask from uvmexp.pagesize.
358 * => XXXCDC: move global vars.
359 */
360
361 void
362 uvm_setpagesize()
363 {
364 if (uvmexp.pagesize == 0)
365 uvmexp.pagesize = DEFAULT_PAGE_SIZE;
366 uvmexp.pagemask = uvmexp.pagesize - 1;
367 if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
368 panic("uvm_setpagesize: page size not a power of two");
369 for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
370 if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
371 break;
372 }
373
374 /*
375 * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
376 */
377
378 vm_offset_t
379 uvm_pageboot_alloc(size)
380 vm_size_t size;
381 {
382 #if defined(PMAP_STEAL_MEMORY)
383 vm_offset_t addr;
384
385 /*
386 * defer bootstrap allocation to MD code (it may want to allocate
387 * from a direct-mapped segment). pmap_steal_memory should round
388 * off virtual_space_start/virtual_space_end.
389 */
390
391 addr = pmap_steal_memory(size, &virtual_space_start,
392 &virtual_space_end);
393
394 return(addr);
395
396 #else /* !PMAP_STEAL_MEMORY */
397
398 vm_offset_t addr, vaddr, paddr;
399
400 /* round to page size */
401 size = round_page(size);
402
403 /*
404 * on first call to this function init ourselves. we detect this
405 * by checking virtual_space_start/end which are in the zero'd BSS area.
406 */
407
408 if (virtual_space_start == virtual_space_end) {
409 pmap_virtual_space(&virtual_space_start, &virtual_space_end);
410
411 /* round it the way we like it */
412 virtual_space_start = round_page(virtual_space_start);
413 virtual_space_end = trunc_page(virtual_space_end);
414 }
415
416 /*
417 * allocate virtual memory for this request
418 */
419
420 addr = virtual_space_start;
421 virtual_space_start += size;
422
423 /*
424 * allocate and mapin physical pages to back new virtual pages
425 */
426
427 for (vaddr = round_page(addr) ; vaddr < addr + size ;
428 vaddr += PAGE_SIZE) {
429
430 if (!uvm_page_physget(&paddr))
431 panic("uvm_pageboot_alloc: out of memory");
432
433 /* XXX: should be wired, but some pmaps don't like that ... */
434 #if defined(PMAP_NEW)
435 pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE);
436 #else
437 pmap_enter(pmap_kernel(), vaddr, paddr,
438 VM_PROT_READ|VM_PROT_WRITE, FALSE);
439 #endif
440
441 }
442
443 return(addr);
444 #endif /* PMAP_STEAL_MEMORY */
445 }
446
447 #if !defined(PMAP_STEAL_MEMORY)
448 /*
449 * uvm_page_physget: "steal" one page from the vm_physmem structure.
450 *
451 * => attempt to allocate it off the end of a segment in which the "avail"
452 * values match the start/end values. if we can't do that, then we
453 * will advance both values (making them equal, and removing some
454 * vm_page structures from the non-avail area).
455 * => return false if out of memory.
456 */
457
458 static boolean_t
459 uvm_page_physget(paddrp)
460 vm_offset_t *paddrp;
461 {
462 int lcv, x;
463
464 /* pass 1: try allocating from a matching end */
465 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
466 for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
467 #else
468 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
469 #endif
470 {
471
472 if (vm_physmem[lcv].pgs)
473 panic("vm_page_physget: called _after_ bootstrap");
474
475 /* try from front */
476 if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start &&
477 vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
478 *paddrp = ptoa(vm_physmem[lcv].avail_start);
479 vm_physmem[lcv].avail_start++;
480 vm_physmem[lcv].start++;
481 /* nothing left? nuke it */
482 if (vm_physmem[lcv].avail_start ==
483 vm_physmem[lcv].end) {
484 if (vm_nphysseg == 1)
485 panic("vm_page_physget: out of memory!");
486 vm_nphysseg--;
487 for (x = lcv ; x < vm_nphysseg ; x++)
488 /* structure copy */
489 vm_physmem[x] = vm_physmem[x+1];
490 }
491 return (TRUE);
492 }
493
494 /* try from rear */
495 if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end &&
496 vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
497 *paddrp = ptoa(vm_physmem[lcv].avail_end - 1);
498 vm_physmem[lcv].avail_end--;
499 vm_physmem[lcv].end--;
500 /* nothing left? nuke it */
501 if (vm_physmem[lcv].avail_end ==
502 vm_physmem[lcv].start) {
503 if (vm_nphysseg == 1)
504 panic("vm_page_physget: out of memory!");
505 vm_nphysseg--;
506 for (x = lcv ; x < vm_nphysseg ; x++)
507 /* structure copy */
508 vm_physmem[x] = vm_physmem[x+1];
509 }
510 return (TRUE);
511 }
512 }
513
514 /* pass2: forget about matching ends, just allocate something */
515 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
516 for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
517 #else
518 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
519 #endif
520 {
521
522 /* any room in this bank? */
523 if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end)
524 continue; /* nope */
525
526 *paddrp = ptoa(vm_physmem[lcv].avail_start);
527 vm_physmem[lcv].avail_start++;
528 /* truncate! */
529 vm_physmem[lcv].start = vm_physmem[lcv].avail_start;
530
531 /* nothing left? nuke it */
532 if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) {
533 if (vm_nphysseg == 1)
534 panic("vm_page_physget: out of memory!");
535 vm_nphysseg--;
536 for (x = lcv ; x < vm_nphysseg ; x++)
537 /* structure copy */
538 vm_physmem[x] = vm_physmem[x+1];
539 }
540 return (TRUE);
541 }
542
543 return (FALSE); /* whoops! */
544 }
545 #endif /* PMAP_STEAL_MEMORY */
546
547 /*
548 * uvm_page_physload: load physical memory into VM system
549 *
550 * => all args are PFs
551 * => all pages in start/end get vm_page structures
552 * => areas marked by avail_start/avail_end get added to the free page pool
553 * => we are limited to VM_PHYSSEG_MAX physical memory segments
554 */
555
556 void
557 uvm_page_physload(start, end, avail_start, avail_end)
558 vm_offset_t start, end, avail_start, avail_end;
559 {
560 int preload, lcv, npages;
561 struct vm_page *pgs;
562 struct vm_physseg *ps;
563
564 if (uvmexp.pagesize == 0)
565 panic("vm_page_physload: page size not set!");
566
567 /*
568 * do we have room?
569 */
570 if (vm_nphysseg == VM_PHYSSEG_MAX) {
571 printf("vm_page_physload: unable to load physical memory "
572 "segment\n");
573 printf("\t%d segments allocated, ignoring 0x%lx -> 0x%lx\n",
574 VM_PHYSSEG_MAX, start, end);
575 return;
576 }
577
578 /*
579 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been
580 * called yet, so malloc is not available).
581 */
582 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
583 if (vm_physmem[lcv].pgs)
584 break;
585 }
586 preload = (lcv == vm_nphysseg);
587
588 /*
589 * if VM is already running, attempt to malloc() vm_page structures
590 */
591 if (!preload) {
592 #if defined(VM_PHYSSEG_NOADD)
593 panic("vm_page_physload: tried to add RAM after vm_mem_init");
594 #else
595 /* XXXCDC: need some sort of lockout for this case */
596 vm_offset_t paddr;
597 npages = end - start; /* # of pages */
598 MALLOC(pgs, struct vm_page *, sizeof(struct vm_page) * npages,
599 M_VMPAGE, M_NOWAIT);
600 if (pgs == NULL) {
601 printf("vm_page_physload: can not malloc vm_page "
602 "structs for segment\n");
603 printf("\tignoring 0x%lx -> 0x%lx\n", start, end);
604 return;
605 }
606 /* zero data, init phys_addr, and free pages */
607 bzero(pgs, sizeof(struct vm_page) * npages);
608 for (lcv = 0, paddr = ptoa(start) ;
609 lcv < npages ; lcv++, paddr += PAGE_SIZE) {
610 pgs[lcv].phys_addr = paddr;
611 if (atop(paddr) >= avail_start &&
612 atop(paddr) <= avail_end)
613 uvm_pagefree(&pgs[lcv]);
614 }
615 /* XXXCDC: incomplete: need to update uvmexp.free, what else? */
616 /* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */
617 #endif
618 } else {
619
620 /* gcc complains if these don't get init'd */
621 pgs = NULL;
622 npages = 0;
623
624 }
625
626 /*
627 * now insert us in the proper place in vm_physmem[]
628 */
629
630 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
631
632 /* random: put it at the end (easy!) */
633 ps = &vm_physmem[vm_nphysseg];
634
635 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
636
637 {
638 int x;
639 /* sort by address for binary search */
640 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
641 if (start < vm_physmem[lcv].start)
642 break;
643 ps = &vm_physmem[lcv];
644 /* move back other entries, if necessary ... */
645 for (x = vm_nphysseg ; x > lcv ; x--)
646 /* structure copy */
647 vm_physmem[x] = vm_physmem[x - 1];
648 }
649
650 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
651
652 {
653 int x;
654 /* sort by largest segment first */
655 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
656 if ((end - start) >
657 (vm_physmem[lcv].end - vm_physmem[lcv].start))
658 break;
659 ps = &vm_physmem[lcv];
660 /* move back other entries, if necessary ... */
661 for (x = vm_nphysseg ; x > lcv ; x--)
662 /* structure copy */
663 vm_physmem[x] = vm_physmem[x - 1];
664 }
665
666 #else
667
668 panic("vm_page_physload: unknown physseg strategy selected!");
669
670 #endif
671
672 ps->start = start;
673 ps->end = end;
674 ps->avail_start = avail_start;
675 ps->avail_end = avail_end;
676 if (preload) {
677 ps->pgs = NULL;
678 } else {
679 ps->pgs = pgs;
680 ps->lastpg = pgs + npages - 1;
681 }
682 vm_nphysseg++;
683
684 /*
685 * done!
686 */
687
688 if (!preload)
689 uvm_page_rehash();
690
691 return;
692 }
693
694 /*
695 * uvm_page_rehash: reallocate hash table based on number of free pages.
696 */
697
698 void
699 uvm_page_rehash()
700 {
701 int freepages, lcv, bucketcount, s, oldcount;
702 struct pglist *newbuckets, *oldbuckets;
703 struct vm_page *pg;
704
705 /*
706 * compute number of pages that can go in the free pool
707 */
708
709 freepages = 0;
710 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
711 freepages +=
712 (vm_physmem[lcv].avail_end - vm_physmem[lcv].avail_start);
713
714 /*
715 * compute number of buckets needed for this number of pages
716 */
717
718 bucketcount = 1;
719 while (bucketcount < freepages)
720 bucketcount = bucketcount * 2;
721
722 /*
723 * malloc new buckets
724 */
725
726 MALLOC(newbuckets, struct pglist *, sizeof(struct pglist) * bucketcount,
727 M_VMPBUCKET, M_NOWAIT);
728 if (newbuckets == NULL) {
729 printf("vm_page_physrehash: WARNING: could not grow page "
730 "hash table\n");
731 return;
732 }
733 for (lcv = 0 ; lcv < bucketcount ; lcv++)
734 TAILQ_INIT(&newbuckets[lcv]);
735
736 /*
737 * now replace the old buckets with the new ones and rehash everything
738 */
739
740 s = splimp();
741 simple_lock(&uvm.hashlock);
742 /* swap old for new ... */
743 oldbuckets = uvm.page_hash;
744 oldcount = uvm.page_nhash;
745 uvm.page_hash = newbuckets;
746 uvm.page_nhash = bucketcount;
747 uvm.page_hashmask = bucketcount - 1; /* power of 2 */
748
749 /* ... and rehash */
750 for (lcv = 0 ; lcv < oldcount ; lcv++) {
751 while ((pg = oldbuckets[lcv].tqh_first) != NULL) {
752 TAILQ_REMOVE(&oldbuckets[lcv], pg, hashq);
753 TAILQ_INSERT_TAIL(
754 &uvm.page_hash[uvm_pagehash(pg->uobject, pg->offset)],
755 pg, hashq);
756 }
757 }
758 simple_unlock(&uvm.hashlock);
759 splx(s);
760
761 /*
762 * free old bucket array if we malloc'd it previously
763 */
764
765 if (oldbuckets != &uvm_bootbucket)
766 FREE(oldbuckets, M_VMPBUCKET);
767
768 /*
769 * done
770 */
771 return;
772 }
773
774
775 #if 1 /* XXXCDC: TMP TMP TMP DEBUG DEBUG DEBUG */
776
777 void uvm_page_physdump __P((void)); /* SHUT UP GCC */
778
779 /* call from DDB */
780 void
781 uvm_page_physdump()
782 {
783 int lcv;
784
785 printf("rehash: physical memory config [segs=%d of %d]:\n",
786 vm_nphysseg, VM_PHYSSEG_MAX);
787 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
788 printf("0x%lx->0x%lx [0x%lx->0x%lx]\n", vm_physmem[lcv].start,
789 vm_physmem[lcv].end, vm_physmem[lcv].avail_start,
790 vm_physmem[lcv].avail_end);
791 printf("STRATEGY = ");
792 switch (VM_PHYSSEG_STRAT) {
793 case VM_PSTRAT_RANDOM: printf("RANDOM\n"); break;
794 case VM_PSTRAT_BSEARCH: printf("BSEARCH\n"); break;
795 case VM_PSTRAT_BIGFIRST: printf("BIGFIRST\n"); break;
796 default: printf("<<UNKNOWN>>!!!!\n");
797 }
798 printf("number of buckets = %d\n", uvm.page_nhash);
799 }
800 #endif
801
802 /*
803 * uvm_pagealloc: allocate vm_page.
804 *
805 * => return null if no pages free
806 * => wake up pagedaemon if number of free pages drops below low water mark
807 * => if obj != NULL, obj must be locked (to put in hash)
808 * => if anon != NULL, anon must be locked (to put in anon)
809 * => only one of obj or anon can be non-null
810 * => caller must activate/deactivate page if it is not wired.
811 */
812
813 struct vm_page *
814 uvm_pagealloc(obj, off, anon)
815 struct uvm_object *obj;
816 vm_offset_t off;
817 struct vm_anon *anon;
818 {
819 int s;
820 struct vm_page *pg;
821
822 #ifdef DIAGNOSTIC
823 /* sanity check */
824 if (obj && anon)
825 panic("uvm_pagealloc: obj and anon != NULL");
826 #endif
827
828 s = splimp();
829
830 uvm_lock_fpageq(); /* lock free page queue */
831
832 /*
833 * check to see if we need to generate some free pages waking
834 * the pagedaemon.
835 */
836
837 if (uvmexp.free < uvmexp.freemin || (uvmexp.free < uvmexp.freetarg &&
838 uvmexp.inactive < uvmexp.inactarg))
839 thread_wakeup(&uvm.pagedaemon);
840
841 /*
842 * fail if any of these conditions is true:
843 * [1] there really are no free pages, or
844 * [2] only kernel "reserved" pages remain and
845 * the page isn't being allocated to a kernel object.
846 * [3] only pagedaemon "reserved" pages remain and
847 * the requestor isn't the pagedaemon.
848 */
849
850 pg = uvm.page_free.tqh_first;
851 if (pg == NULL ||
852 (uvmexp.free <= uvmexp.reserve_kernel &&
853 !(obj && obj->uo_refs == UVM_OBJ_KERN)) ||
854 (uvmexp.free <= uvmexp.reserve_pagedaemon &&
855 !(obj == uvmexp.kmem_object && curproc == uvm.pagedaemon_proc))) {
856 uvm_unlock_fpageq();
857 splx(s);
858 return(NULL);
859 }
860
861 TAILQ_REMOVE(&uvm.page_free, pg, pageq);
862 uvmexp.free--;
863
864 uvm_unlock_fpageq(); /* unlock free page queue */
865 splx(s);
866
867 pg->offset = off;
868 pg->uobject = obj;
869 pg->uanon = anon;
870 pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
871 pg->version++;
872 pg->wire_count = 0;
873 pg->loan_count = 0;
874 if (anon) {
875 anon->u.an_page = pg;
876 pg->pqflags = PQ_ANON;
877 } else {
878 if (obj)
879 uvm_pageinsert(pg);
880 pg->pqflags = 0;
881 }
882 #if defined(UVM_PAGE_TRKOWN)
883 pg->owner_tag = NULL;
884 #endif
885 UVM_PAGE_OWN(pg, "new alloc");
886
887 return(pg);
888 }
889
890 /*
891 * uvm_pagerealloc: reallocate a page from one object to another
892 *
893 * => both objects must be locked
894 */
895
896 void
897 uvm_pagerealloc(pg, newobj, newoff)
898 struct vm_page *pg;
899 struct uvm_object *newobj;
900 vm_offset_t newoff;
901 {
902 /*
903 * remove it from the old object
904 */
905
906 if (pg->uobject) {
907 uvm_pageremove(pg);
908 }
909
910 /*
911 * put it in the new object
912 */
913
914 if (newobj) {
915 pg->uobject = newobj;
916 pg->offset = newoff;
917 pg->version++;
918 uvm_pageinsert(pg);
919 }
920
921 return;
922 }
923
924
925 /*
926 * uvm_pagefree: free page
927 *
928 * => erase page's identity (i.e. remove from hash/object)
929 * => put page on free list
930 * => caller must lock owning object (either anon or uvm_object)
931 * => caller must lock page queues
932 * => assumes all valid mappings of pg are gone
933 */
934
935 void uvm_pagefree(pg)
936
937 struct vm_page *pg;
938
939 {
940 int s;
941 int saved_loan_count = pg->loan_count;
942
943 /*
944 * if the page was an object page (and thus "TABLED"), remove it
945 * from the object.
946 */
947
948 if (pg->flags & PG_TABLED) {
949
950 /*
951 * if the object page is on loan we are going to drop ownership.
952 * it is possible that an anon will take over as owner for this
953 * page later on. the anon will want a !PG_CLEAN page so that
954 * it knows it needs to allocate swap if it wants to page the
955 * page out.
956 */
957
958 if (saved_loan_count)
959 pg->flags &= ~PG_CLEAN; /* in case an anon takes over */
960
961 uvm_pageremove(pg);
962
963 /*
964 * if our page was on loan, then we just lost control over it
965 * (in fact, if it was loaned to an anon, the anon may have
966 * already taken over ownership of the page by now and thus
967 * changed the loan_count [e.g. in uvmfault_anonget()]) we just
968 * return (when the last loan is dropped, then the page can be
969 * freed by whatever was holding the last loan).
970 */
971 if (saved_loan_count)
972 return;
973
974 } else if (saved_loan_count && (pg->pqflags & PQ_ANON)) {
975
976 /*
977 * if our page is owned by an anon and is loaned out to the
978 * kernel then we just want to drop ownership and return.
979 * the kernel must free the page when all its loans clear ...
980 * note that the kernel can't change the loan status of our
981 * page as long as we are holding PQ lock.
982 */
983 pg->pqflags &= ~PQ_ANON;
984 pg->uanon = NULL;
985 return;
986 }
987
988 #ifdef DIAGNOSTIC
989 if (saved_loan_count) {
990 printf("uvm_pagefree: warning: freeing page with a loan "
991 "count of %d\n", saved_loan_count);
992 panic("uvm_pagefree: loan count");
993 }
994 #endif
995
996
997 /*
998 * now remove the page from the queues
999 */
1000
1001 if (pg->pqflags & PQ_ACTIVE) {
1002 TAILQ_REMOVE(&uvm.page_active, pg, pageq);
1003 pg->pqflags &= ~PQ_ACTIVE;
1004 uvmexp.active--;
1005 }
1006 if (pg->pqflags & PQ_INACTIVE) {
1007 if (pg->pqflags & PQ_SWAPBACKED)
1008 TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
1009 else
1010 TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
1011 pg->pqflags &= ~PQ_INACTIVE;
1012 uvmexp.inactive--;
1013 }
1014
1015 /*
1016 * if the page was wired, unwire it now.
1017 */
1018 if (pg->wire_count)
1019 {
1020 pg->wire_count = 0;
1021 uvmexp.wired--;
1022 }
1023
1024 /*
1025 * and put on free queue
1026 */
1027
1028 s = splimp();
1029 uvm_lock_fpageq();
1030 TAILQ_INSERT_TAIL(&uvm.page_free, pg, pageq);
1031 pg->pqflags = PQ_FREE;
1032 #ifdef DEBUG
1033 pg->uobject = (void *)0xdeadbeef;
1034 pg->offset = 0xdeadbeef;
1035 pg->uanon = (void *)0xdeadbeef;
1036 #endif
1037 uvmexp.free++;
1038 uvm_unlock_fpageq();
1039 splx(s);
1040 }
1041
1042 #if defined(UVM_PAGE_TRKOWN)
1043 /*
1044 * uvm_page_own: set or release page ownership
1045 *
1046 * => this is a debugging function that keeps track of who sets PG_BUSY
1047 * and where they do it. it can be used to track down problems
1048 * such a process setting "PG_BUSY" and never releasing it.
1049 * => page's object [if any] must be locked
1050 * => if "tag" is NULL then we are releasing page ownership
1051 */
1052 void
1053 uvm_page_own(pg, tag)
1054 struct vm_page *pg;
1055 char *tag;
1056 {
1057 /* gain ownership? */
1058 if (tag) {
1059 if (pg->owner_tag) {
1060 printf("uvm_page_own: page %p already owned "
1061 "by proc %d [%s]\n", pg,
1062 pg->owner, pg->owner_tag);
1063 panic("uvm_page_own");
1064 }
1065 pg->owner = (curproc) ? curproc->p_pid : (pid_t) -1;
1066 pg->owner_tag = tag;
1067 return;
1068 }
1069
1070 /* drop ownership */
1071 if (pg->owner_tag == NULL) {
1072 printf("uvm_page_own: dropping ownership of an non-owned "
1073 "page (%p)\n", pg);
1074 panic("uvm_page_own");
1075 }
1076 pg->owner_tag = NULL;
1077 return;
1078 }
1079 #endif
1080