2 * Copyright (c) 2002-2006 Rice University
3 * Copyright (c) 2007 Alan L. Cox <alc@cs.rice.edu>
6 * This software was developed for the FreeBSD Project by Alan L. Cox,
7 * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
28 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Physical memory system implementation
35 * Any external functions defined by this module are only to be used by the
36 * virtual memory system.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
54 #include <sys/queue.h>
55 #include <sys/rwlock.h>
57 #include <sys/sysctl.h>
59 #include <sys/vmmeter.h>
65 #include <vm/vm_param.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_page.h>
69 #include <vm/vm_phys.h>
71 #include <vm/vm_domain.h>
73 _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX,
74 "Too many physsegs.");
76 struct mem_affinity *mem_affinity;
81 struct vm_phys_seg vm_phys_segs[VM_PHYSSEG_MAX];
84 struct vm_phys_fictitious_seg;
85 static int vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *,
86 struct vm_phys_fictitious_seg *);
88 RB_HEAD(fict_tree, vm_phys_fictitious_seg) vm_phys_fictitious_tree =
89 RB_INITIALIZER(_vm_phys_fictitious_tree);
91 struct vm_phys_fictitious_seg {
92 RB_ENTRY(vm_phys_fictitious_seg) node;
93 /* Memory region data */
99 RB_GENERATE_STATIC(fict_tree, vm_phys_fictitious_seg, node,
100 vm_phys_fictitious_cmp);
102 static struct rwlock vm_phys_fictitious_reg_lock;
103 MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages");
105 static struct vm_freelist
106 vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL][VM_NFREEORDER];
108 static int vm_nfreelists;
111 * Provides the mapping from VM_FREELIST_* to free list indices (flind).
113 static int vm_freelist_to_flind[VM_NFREELIST];
115 CTASSERT(VM_FREELIST_DEFAULT == 0);
117 #ifdef VM_FREELIST_ISADMA
118 #define VM_ISADMA_BOUNDARY 16777216
120 #ifdef VM_FREELIST_DMA32
121 #define VM_DMA32_BOUNDARY ((vm_paddr_t)1 << 32)
125 * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about
126 * the ordering of the free list boundaries.
128 #if defined(VM_ISADMA_BOUNDARY) && defined(VM_LOWMEM_BOUNDARY)
129 CTASSERT(VM_ISADMA_BOUNDARY < VM_LOWMEM_BOUNDARY);
131 #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY)
132 CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY);
135 static int cnt_prezero;
136 SYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
137 &cnt_prezero, 0, "The number of physical pages prezeroed at idle time");
139 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
140 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
141 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
143 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
144 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
145 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
148 static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS);
149 SYSCTL_OID(_vm, OID_AUTO, phys_locality, CTLTYPE_STRING | CTLFLAG_RD,
150 NULL, 0, sysctl_vm_phys_locality, "A", "Phys Locality Info");
153 SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
154 &vm_ndomains, 0, "Number of physical memory domains available.");
157 * Default to first-touch + round-robin.
159 static struct mtx vm_default_policy_mtx;
160 MTX_SYSINIT(vm_default_policy, &vm_default_policy_mtx, "default policy mutex",
163 static struct vm_domain_policy vm_default_policy =
164 VM_DOMAIN_POLICY_STATIC_INITIALISER(VM_POLICY_FIRST_TOUCH_ROUND_ROBIN, 0);
166 /* Use round-robin so the domain policy code will only try once per allocation */
167 static struct vm_domain_policy vm_default_policy =
168 VM_DOMAIN_POLICY_STATIC_INITIALISER(VM_POLICY_ROUND_ROBIN, 0);
171 static vm_page_t vm_phys_alloc_domain_pages(int domain, int flind, int pool,
173 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain);
174 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end);
175 static int vm_phys_paddr_to_segind(vm_paddr_t pa);
176 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
180 sysctl_vm_default_policy(SYSCTL_HANDLER_ARGS)
182 char policy_name[32];
185 mtx_lock(&vm_default_policy_mtx);
187 /* Map policy to output string */
188 switch (vm_default_policy.p.policy) {
189 case VM_POLICY_FIRST_TOUCH:
190 strcpy(policy_name, "first-touch");
192 case VM_POLICY_FIRST_TOUCH_ROUND_ROBIN:
193 strcpy(policy_name, "first-touch-rr");
195 case VM_POLICY_ROUND_ROBIN:
197 strcpy(policy_name, "rr");
200 mtx_unlock(&vm_default_policy_mtx);
202 error = sysctl_handle_string(oidp, &policy_name[0],
203 sizeof(policy_name), req);
204 if (error != 0 || req->newptr == NULL)
207 mtx_lock(&vm_default_policy_mtx);
208 /* Set: match on the subset of policies that make sense as a default */
209 if (strcmp("first-touch-rr", policy_name) == 0) {
210 vm_domain_policy_set(&vm_default_policy,
211 VM_POLICY_FIRST_TOUCH_ROUND_ROBIN, 0);
212 } else if (strcmp("first-touch", policy_name) == 0) {
213 vm_domain_policy_set(&vm_default_policy,
214 VM_POLICY_FIRST_TOUCH, 0);
215 } else if (strcmp("rr", policy_name) == 0) {
216 vm_domain_policy_set(&vm_default_policy,
217 VM_POLICY_ROUND_ROBIN, 0);
225 mtx_unlock(&vm_default_policy_mtx);
229 SYSCTL_PROC(_vm, OID_AUTO, default_policy, CTLTYPE_STRING | CTLFLAG_RW,
230 0, 0, sysctl_vm_default_policy, "A",
231 "Default policy (rr, first-touch, first-touch-rr");
234 * Red-black tree helpers for vm fictitious range management.
237 vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p,
238 struct vm_phys_fictitious_seg *range)
241 KASSERT(range->start != 0 && range->end != 0,
242 ("Invalid range passed on search for vm_fictitious page"));
243 if (p->start >= range->end)
245 if (p->start < range->start)
252 vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1,
253 struct vm_phys_fictitious_seg *p2)
256 /* Check if this is a search for a page */
258 return (vm_phys_fictitious_in_range(p1, p2));
260 KASSERT(p2->end != 0,
261 ("Invalid range passed as second parameter to vm fictitious comparison"));
263 /* Searching to add a new range */
264 if (p1->end <= p2->start)
266 if (p1->start >= p2->end)
269 panic("Trying to add overlapping vm fictitious ranges:\n"
270 "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start,
271 (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end);
275 vm_rr_selectdomain(void)
283 td->td_dom_rr_idx %= vm_ndomains;
284 return (td->td_dom_rr_idx);
291 * Initialise a VM domain iterator.
293 * Check the thread policy, then the proc policy,
294 * then default to the system policy.
296 * Later on the various layers will have this logic
297 * plumbed into them and the phys code will be explicitly
298 * handed a VM domain policy to use.
301 vm_policy_iterator_init(struct vm_domain_iterator *vi)
304 struct vm_domain_policy lcl;
307 vm_domain_iterator_init(vi);
310 /* Copy out the thread policy */
311 vm_domain_policy_localcopy(&lcl, &curthread->td_vm_dom_policy);
312 if (lcl.p.policy != VM_POLICY_NONE) {
313 /* Thread policy is present; use it */
314 vm_domain_iterator_set_policy(vi, &lcl);
318 vm_domain_policy_localcopy(&lcl,
319 &curthread->td_proc->p_vm_dom_policy);
320 if (lcl.p.policy != VM_POLICY_NONE) {
321 /* Process policy is present; use it */
322 vm_domain_iterator_set_policy(vi, &lcl);
326 /* Use system default policy */
327 vm_domain_iterator_set_policy(vi, &vm_default_policy);
331 vm_policy_iterator_finish(struct vm_domain_iterator *vi)
334 vm_domain_iterator_cleanup(vi);
338 vm_phys_domain_intersects(long mask, vm_paddr_t low, vm_paddr_t high)
340 struct vm_phys_seg *s;
343 while ((idx = ffsl(mask)) != 0) {
344 idx--; /* ffsl counts from 1 */
345 mask &= ~(1UL << idx);
346 s = &vm_phys_segs[idx];
347 if (low < s->end && high > s->start)
354 * Outputs the state of the physical memory allocator, specifically,
355 * the amount of physical memory in each free list.
358 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
361 struct vm_freelist *fl;
362 int dom, error, flind, oind, pind;
364 error = sysctl_wire_old_buffer(req, 0);
367 sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req);
368 for (dom = 0; dom < vm_ndomains; dom++) {
369 sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom);
370 for (flind = 0; flind < vm_nfreelists; flind++) {
371 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
372 "\n ORDER (SIZE) | NUMBER"
374 for (pind = 0; pind < VM_NFREEPOOL; pind++)
375 sbuf_printf(&sbuf, " | POOL %d", pind);
376 sbuf_printf(&sbuf, "\n-- ");
377 for (pind = 0; pind < VM_NFREEPOOL; pind++)
378 sbuf_printf(&sbuf, "-- -- ");
379 sbuf_printf(&sbuf, "--\n");
380 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
381 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
382 1 << (PAGE_SHIFT - 10 + oind));
383 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
384 fl = vm_phys_free_queues[dom][flind][pind];
385 sbuf_printf(&sbuf, " | %6d",
388 sbuf_printf(&sbuf, "\n");
392 error = sbuf_finish(&sbuf);
398 * Outputs the set of physical memory segments.
401 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
404 struct vm_phys_seg *seg;
407 error = sysctl_wire_old_buffer(req, 0);
410 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
411 for (segind = 0; segind < vm_phys_nsegs; segind++) {
412 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
413 seg = &vm_phys_segs[segind];
414 sbuf_printf(&sbuf, "start: %#jx\n",
415 (uintmax_t)seg->start);
416 sbuf_printf(&sbuf, "end: %#jx\n",
417 (uintmax_t)seg->end);
418 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
419 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
421 error = sbuf_finish(&sbuf);
427 * Return affinity, or -1 if there's no affinity information.
430 vm_phys_mem_affinity(int f, int t)
434 if (mem_locality == NULL)
436 if (f >= vm_ndomains || t >= vm_ndomains)
438 return (mem_locality[f * vm_ndomains + t]);
446 * Outputs the VM locality table.
449 sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS)
454 error = sysctl_wire_old_buffer(req, 0);
457 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
459 sbuf_printf(&sbuf, "\n");
461 for (i = 0; i < vm_ndomains; i++) {
462 sbuf_printf(&sbuf, "%d: ", i);
463 for (j = 0; j < vm_ndomains; j++) {
464 sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j));
466 sbuf_printf(&sbuf, "\n");
468 error = sbuf_finish(&sbuf);
475 vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
480 TAILQ_INSERT_TAIL(&fl[order].pl, m, plinks.q);
482 TAILQ_INSERT_HEAD(&fl[order].pl, m, plinks.q);
487 vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
490 TAILQ_REMOVE(&fl[order].pl, m, plinks.q);
492 m->order = VM_NFREEORDER;
496 * Create a physical memory segment.
499 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain)
501 struct vm_phys_seg *seg;
503 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
504 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
505 KASSERT(domain < vm_ndomains,
506 ("vm_phys_create_seg: invalid domain provided"));
507 seg = &vm_phys_segs[vm_phys_nsegs++];
508 while (seg > vm_phys_segs && (seg - 1)->start >= end) {
514 seg->domain = domain;
518 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end)
522 if (mem_affinity == NULL) {
523 _vm_phys_create_seg(start, end, 0);
528 if (mem_affinity[i].end == 0)
529 panic("Reached end of affinity info");
530 if (mem_affinity[i].end <= start)
532 if (mem_affinity[i].start > start)
533 panic("No affinity info for start %jx",
535 if (mem_affinity[i].end >= end) {
536 _vm_phys_create_seg(start, end,
537 mem_affinity[i].domain);
540 _vm_phys_create_seg(start, mem_affinity[i].end,
541 mem_affinity[i].domain);
542 start = mem_affinity[i].end;
547 * Add a physical memory segment.
550 vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
554 KASSERT((start & PAGE_MASK) == 0,
555 ("vm_phys_define_seg: start is not page aligned"));
556 KASSERT((end & PAGE_MASK) == 0,
557 ("vm_phys_define_seg: end is not page aligned"));
560 * Split the physical memory segment if it spans two or more free
564 #ifdef VM_FREELIST_ISADMA
565 if (paddr < VM_ISADMA_BOUNDARY && end > VM_ISADMA_BOUNDARY) {
566 vm_phys_create_seg(paddr, VM_ISADMA_BOUNDARY);
567 paddr = VM_ISADMA_BOUNDARY;
570 #ifdef VM_FREELIST_LOWMEM
571 if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) {
572 vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY);
573 paddr = VM_LOWMEM_BOUNDARY;
576 #ifdef VM_FREELIST_DMA32
577 if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) {
578 vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY);
579 paddr = VM_DMA32_BOUNDARY;
582 vm_phys_create_seg(paddr, end);
586 * Initialize the physical memory allocator.
588 * Requires that vm_page_array is initialized!
593 struct vm_freelist *fl;
594 struct vm_phys_seg *seg;
596 int dom, flind, freelist, oind, pind, segind;
599 * Compute the number of free lists, and generate the mapping from the
600 * manifest constants VM_FREELIST_* to the free list indices.
602 * Initially, the entries of vm_freelist_to_flind[] are set to either
603 * 0 or 1 to indicate which free lists should be created.
606 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
607 seg = &vm_phys_segs[segind];
608 #ifdef VM_FREELIST_ISADMA
609 if (seg->end <= VM_ISADMA_BOUNDARY)
610 vm_freelist_to_flind[VM_FREELIST_ISADMA] = 1;
613 #ifdef VM_FREELIST_LOWMEM
614 if (seg->end <= VM_LOWMEM_BOUNDARY)
615 vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1;
618 #ifdef VM_FREELIST_DMA32
620 #ifdef VM_DMA32_NPAGES_THRESHOLD
622 * Create the DMA32 free list only if the amount of
623 * physical memory above physical address 4G exceeds the
626 npages > VM_DMA32_NPAGES_THRESHOLD &&
628 seg->end <= VM_DMA32_BOUNDARY)
629 vm_freelist_to_flind[VM_FREELIST_DMA32] = 1;
633 npages += atop(seg->end - seg->start);
634 vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1;
637 /* Change each entry into a running total of the free lists. */
638 for (freelist = 1; freelist < VM_NFREELIST; freelist++) {
639 vm_freelist_to_flind[freelist] +=
640 vm_freelist_to_flind[freelist - 1];
642 vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1];
643 KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists"));
644 /* Change each entry into a free list index. */
645 for (freelist = 0; freelist < VM_NFREELIST; freelist++)
646 vm_freelist_to_flind[freelist]--;
649 * Initialize the first_page and free_queues fields of each physical
652 #ifdef VM_PHYSSEG_SPARSE
655 for (segind = 0; segind < vm_phys_nsegs; segind++) {
656 seg = &vm_phys_segs[segind];
657 #ifdef VM_PHYSSEG_SPARSE
658 seg->first_page = &vm_page_array[npages];
659 npages += atop(seg->end - seg->start);
661 seg->first_page = PHYS_TO_VM_PAGE(seg->start);
663 #ifdef VM_FREELIST_ISADMA
664 if (seg->end <= VM_ISADMA_BOUNDARY) {
665 flind = vm_freelist_to_flind[VM_FREELIST_ISADMA];
667 ("vm_phys_init: ISADMA flind < 0"));
670 #ifdef VM_FREELIST_LOWMEM
671 if (seg->end <= VM_LOWMEM_BOUNDARY) {
672 flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM];
674 ("vm_phys_init: LOWMEM flind < 0"));
677 #ifdef VM_FREELIST_DMA32
678 if (seg->end <= VM_DMA32_BOUNDARY) {
679 flind = vm_freelist_to_flind[VM_FREELIST_DMA32];
681 ("vm_phys_init: DMA32 flind < 0"));
685 flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT];
687 ("vm_phys_init: DEFAULT flind < 0"));
689 seg->free_queues = &vm_phys_free_queues[seg->domain][flind];
693 * Initialize the free queues.
695 for (dom = 0; dom < vm_ndomains; dom++) {
696 for (flind = 0; flind < vm_nfreelists; flind++) {
697 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
698 fl = vm_phys_free_queues[dom][flind][pind];
699 for (oind = 0; oind < VM_NFREEORDER; oind++)
700 TAILQ_INIT(&fl[oind].pl);
705 rw_init(&vm_phys_fictitious_reg_lock, "vmfctr");
709 * Split a contiguous, power of two-sized set of physical pages.
712 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
716 while (oind > order) {
718 m_buddy = &m[1 << oind];
719 KASSERT(m_buddy->order == VM_NFREEORDER,
720 ("vm_phys_split_pages: page %p has unexpected order %d",
721 m_buddy, m_buddy->order));
722 vm_freelist_add(fl, m_buddy, oind, 0);
727 * Initialize a physical page and add it to the free lists.
730 vm_phys_add_page(vm_paddr_t pa)
733 struct vm_domain *vmd;
735 vm_cnt.v_page_count++;
736 m = vm_phys_paddr_to_vm_page(pa);
739 m->segind = vm_phys_paddr_to_segind(pa);
740 vmd = vm_phys_domain(m);
741 vmd->vmd_page_count++;
742 vmd->vmd_segs |= 1UL << m->segind;
743 KASSERT(m->order == VM_NFREEORDER,
744 ("vm_phys_add_page: page %p has unexpected order %d",
746 m->pool = VM_FREEPOOL_DEFAULT;
748 mtx_lock(&vm_page_queue_free_mtx);
749 vm_phys_freecnt_adj(m, 1);
750 vm_phys_free_pages(m, 0);
751 mtx_unlock(&vm_page_queue_free_mtx);
755 * Allocate a contiguous, power of two-sized set of physical pages
756 * from the free lists.
758 * The free page queues must be locked.
761 vm_phys_alloc_pages(int pool, int order)
765 struct vm_domain_iterator vi;
767 KASSERT(pool < VM_NFREEPOOL,
768 ("vm_phys_alloc_pages: pool %d is out of range", pool));
769 KASSERT(order < VM_NFREEORDER,
770 ("vm_phys_alloc_pages: order %d is out of range", order));
772 vm_policy_iterator_init(&vi);
774 while ((vm_domain_iterator_run(&vi, &domain)) == 0) {
775 for (flind = 0; flind < vm_nfreelists; flind++) {
776 m = vm_phys_alloc_domain_pages(domain, flind, pool,
783 vm_policy_iterator_finish(&vi);
788 * Allocate a contiguous, power of two-sized set of physical pages from the
789 * specified free list. The free list must be specified using one of the
790 * manifest constants VM_FREELIST_*.
792 * The free page queues must be locked.
795 vm_phys_alloc_freelist_pages(int freelist, int pool, int order)
798 struct vm_domain_iterator vi;
801 KASSERT(freelist < VM_NFREELIST,
802 ("vm_phys_alloc_freelist_pages: freelist %d is out of range",
804 KASSERT(pool < VM_NFREEPOOL,
805 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
806 KASSERT(order < VM_NFREEORDER,
807 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
809 vm_policy_iterator_init(&vi);
811 while ((vm_domain_iterator_run(&vi, &domain)) == 0) {
812 m = vm_phys_alloc_domain_pages(domain,
813 vm_freelist_to_flind[freelist], pool, order);
818 vm_policy_iterator_finish(&vi);
823 vm_phys_alloc_domain_pages(int domain, int flind, int pool, int order)
825 struct vm_freelist *fl;
826 struct vm_freelist *alt;
830 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
831 fl = &vm_phys_free_queues[domain][flind][pool][0];
832 for (oind = order; oind < VM_NFREEORDER; oind++) {
833 m = TAILQ_FIRST(&fl[oind].pl);
835 vm_freelist_rem(fl, m, oind);
836 vm_phys_split_pages(m, oind, fl, order);
842 * The given pool was empty. Find the largest
843 * contiguous, power-of-two-sized set of pages in any
844 * pool. Transfer these pages to the given pool, and
845 * use them to satisfy the allocation.
847 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
848 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
849 alt = &vm_phys_free_queues[domain][flind][pind][0];
850 m = TAILQ_FIRST(&alt[oind].pl);
852 vm_freelist_rem(alt, m, oind);
853 vm_phys_set_pool(pool, m, oind);
854 vm_phys_split_pages(m, oind, fl, order);
863 * Find the vm_page corresponding to the given physical address.
866 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
868 struct vm_phys_seg *seg;
871 for (segind = 0; segind < vm_phys_nsegs; segind++) {
872 seg = &vm_phys_segs[segind];
873 if (pa >= seg->start && pa < seg->end)
874 return (&seg->first_page[atop(pa - seg->start)]);
880 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
882 struct vm_phys_fictitious_seg tmp, *seg;
889 rw_rlock(&vm_phys_fictitious_reg_lock);
890 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
891 rw_runlock(&vm_phys_fictitious_reg_lock);
895 m = &seg->first_page[atop(pa - seg->start)];
896 KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m));
902 vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start,
903 long page_count, vm_memattr_t memattr)
907 for (i = 0; i < page_count; i++) {
908 vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr);
909 range[i].oflags &= ~VPO_UNMANAGED;
910 range[i].busy_lock = VPB_UNBUSIED;
915 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
916 vm_memattr_t memattr)
918 struct vm_phys_fictitious_seg *seg;
921 #ifdef VM_PHYSSEG_DENSE
927 ("Start of segment isn't less than end (start: %jx end: %jx)",
928 (uintmax_t)start, (uintmax_t)end));
930 page_count = (end - start) / PAGE_SIZE;
932 #ifdef VM_PHYSSEG_DENSE
935 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
936 fp = &vm_page_array[pi - first_page];
937 if ((pe - first_page) > vm_page_array_size) {
939 * We have a segment that starts inside
940 * of vm_page_array, but ends outside of it.
942 * Use vm_page_array pages for those that are
943 * inside of the vm_page_array range, and
944 * allocate the remaining ones.
946 dpage_count = vm_page_array_size - (pi - first_page);
947 vm_phys_fictitious_init_range(fp, start, dpage_count,
949 page_count -= dpage_count;
950 start += ptoa(dpage_count);
954 * We can allocate the full range from vm_page_array,
955 * so there's no need to register the range in the tree.
957 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
959 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
961 * We have a segment that ends inside of vm_page_array,
962 * but starts outside of it.
964 fp = &vm_page_array[0];
965 dpage_count = pe - first_page;
966 vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count,
968 end -= ptoa(dpage_count);
969 page_count -= dpage_count;
971 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
973 * Trying to register a fictitious range that expands before
974 * and after vm_page_array.
980 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
982 #ifdef VM_PHYSSEG_DENSE
985 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
987 seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO);
990 seg->first_page = fp;
992 rw_wlock(&vm_phys_fictitious_reg_lock);
993 RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg);
994 rw_wunlock(&vm_phys_fictitious_reg_lock);
1000 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
1002 struct vm_phys_fictitious_seg *seg, tmp;
1003 #ifdef VM_PHYSSEG_DENSE
1007 KASSERT(start < end,
1008 ("Start of segment isn't less than end (start: %jx end: %jx)",
1009 (uintmax_t)start, (uintmax_t)end));
1011 #ifdef VM_PHYSSEG_DENSE
1014 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1015 if ((pe - first_page) <= vm_page_array_size) {
1017 * This segment was allocated using vm_page_array
1018 * only, there's nothing to do since those pages
1019 * were never added to the tree.
1024 * We have a segment that starts inside
1025 * of vm_page_array, but ends outside of it.
1027 * Calculate how many pages were added to the
1028 * tree and free them.
1030 start = ptoa(first_page + vm_page_array_size);
1031 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
1033 * We have a segment that ends inside of vm_page_array,
1034 * but starts outside of it.
1036 end = ptoa(first_page);
1037 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
1038 /* Since it's not possible to register such a range, panic. */
1040 "Unregistering not registered fictitious range [%#jx:%#jx]",
1041 (uintmax_t)start, (uintmax_t)end);
1047 rw_wlock(&vm_phys_fictitious_reg_lock);
1048 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
1049 if (seg->start != start || seg->end != end) {
1050 rw_wunlock(&vm_phys_fictitious_reg_lock);
1052 "Unregistering not registered fictitious range [%#jx:%#jx]",
1053 (uintmax_t)start, (uintmax_t)end);
1055 RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg);
1056 rw_wunlock(&vm_phys_fictitious_reg_lock);
1057 free(seg->first_page, M_FICT_PAGES);
1058 free(seg, M_FICT_PAGES);
1062 * Find the segment containing the given physical address.
1065 vm_phys_paddr_to_segind(vm_paddr_t pa)
1067 struct vm_phys_seg *seg;
1070 for (segind = 0; segind < vm_phys_nsegs; segind++) {
1071 seg = &vm_phys_segs[segind];
1072 if (pa >= seg->start && pa < seg->end)
1075 panic("vm_phys_paddr_to_segind: paddr %#jx is not in any segment" ,
1080 * Free a contiguous, power of two-sized set of physical pages.
1082 * The free page queues must be locked.
1085 vm_phys_free_pages(vm_page_t m, int order)
1087 struct vm_freelist *fl;
1088 struct vm_phys_seg *seg;
1092 KASSERT(m->order == VM_NFREEORDER,
1093 ("vm_phys_free_pages: page %p has unexpected order %d",
1095 KASSERT(m->pool < VM_NFREEPOOL,
1096 ("vm_phys_free_pages: page %p has unexpected pool %d",
1098 KASSERT(order < VM_NFREEORDER,
1099 ("vm_phys_free_pages: order %d is out of range", order));
1100 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1101 seg = &vm_phys_segs[m->segind];
1102 if (order < VM_NFREEORDER - 1) {
1103 pa = VM_PAGE_TO_PHYS(m);
1105 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
1106 if (pa < seg->start || pa >= seg->end)
1108 m_buddy = &seg->first_page[atop(pa - seg->start)];
1109 if (m_buddy->order != order)
1111 fl = (*seg->free_queues)[m_buddy->pool];
1112 vm_freelist_rem(fl, m_buddy, order);
1113 if (m_buddy->pool != m->pool)
1114 vm_phys_set_pool(m->pool, m_buddy, order);
1116 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
1117 m = &seg->first_page[atop(pa - seg->start)];
1118 } while (order < VM_NFREEORDER - 1);
1120 fl = (*seg->free_queues)[m->pool];
1121 vm_freelist_add(fl, m, order, 1);
1125 * Free a contiguous, arbitrarily sized set of physical pages.
1127 * The free page queues must be locked.
1130 vm_phys_free_contig(vm_page_t m, u_long npages)
1136 * Avoid unnecessary coalescing by freeing the pages in the largest
1137 * possible power-of-two-sized subsets.
1139 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1140 for (;; npages -= n) {
1142 * Unsigned "min" is used here so that "order" is assigned
1143 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
1144 * or the low-order bits of its physical address are zero
1145 * because the size of a physical address exceeds the size of
1148 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
1153 vm_phys_free_pages(m, order);
1156 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
1157 for (; npages > 0; npages -= n) {
1158 order = flsl(npages) - 1;
1160 vm_phys_free_pages(m, order);
1166 * Set the pool for a contiguous, power of two-sized set of physical pages.
1169 vm_phys_set_pool(int pool, vm_page_t m, int order)
1173 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
1178 * Search for the given physical page "m" in the free lists. If the search
1179 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
1180 * FALSE, indicating that "m" is not in the free lists.
1182 * The free page queues must be locked.
1185 vm_phys_unfree_page(vm_page_t m)
1187 struct vm_freelist *fl;
1188 struct vm_phys_seg *seg;
1189 vm_paddr_t pa, pa_half;
1190 vm_page_t m_set, m_tmp;
1193 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1196 * First, find the contiguous, power of two-sized set of free
1197 * physical pages containing the given physical page "m" and
1198 * assign it to "m_set".
1200 seg = &vm_phys_segs[m->segind];
1201 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
1202 order < VM_NFREEORDER - 1; ) {
1204 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
1205 if (pa >= seg->start)
1206 m_set = &seg->first_page[atop(pa - seg->start)];
1210 if (m_set->order < order)
1212 if (m_set->order == VM_NFREEORDER)
1214 KASSERT(m_set->order < VM_NFREEORDER,
1215 ("vm_phys_unfree_page: page %p has unexpected order %d",
1216 m_set, m_set->order));
1219 * Next, remove "m_set" from the free lists. Finally, extract
1220 * "m" from "m_set" using an iterative algorithm: While "m_set"
1221 * is larger than a page, shrink "m_set" by returning the half
1222 * of "m_set" that does not contain "m" to the free lists.
1224 fl = (*seg->free_queues)[m_set->pool];
1225 order = m_set->order;
1226 vm_freelist_rem(fl, m_set, order);
1229 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
1230 if (m->phys_addr < pa_half)
1231 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
1234 m_set = &seg->first_page[atop(pa_half - seg->start)];
1236 vm_freelist_add(fl, m_tmp, order, 0);
1238 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
1243 * Try to zero one physical page. Used by an idle priority thread.
1246 vm_phys_zero_pages_idle(void)
1248 static struct vm_freelist *fl;
1249 static int flind, oind, pind;
1253 domain = vm_rr_selectdomain();
1254 fl = vm_phys_free_queues[domain][0][0];
1255 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1257 TAILQ_FOREACH_REVERSE(m, &fl[oind].pl, pglist, plinks.q) {
1258 for (m_tmp = m; m_tmp < &m[1 << oind]; m_tmp++) {
1259 if ((m_tmp->flags & (PG_CACHED | PG_ZERO)) == 0) {
1260 vm_phys_unfree_page(m_tmp);
1261 vm_phys_freecnt_adj(m, -1);
1262 mtx_unlock(&vm_page_queue_free_mtx);
1263 pmap_zero_page_idle(m_tmp);
1264 m_tmp->flags |= PG_ZERO;
1265 mtx_lock(&vm_page_queue_free_mtx);
1266 vm_phys_freecnt_adj(m, 1);
1267 vm_phys_free_pages(m_tmp, 0);
1268 vm_page_zero_count++;
1275 if (oind == VM_NFREEORDER) {
1278 if (pind == VM_NFREEPOOL) {
1281 if (flind == vm_nfreelists)
1284 fl = vm_phys_free_queues[domain][flind][pind];
1290 * Allocate a contiguous set of physical pages of the given size
1291 * "npages" from the free lists. All of the physical pages must be at
1292 * or above the given physical address "low" and below the given
1293 * physical address "high". The given value "alignment" determines the
1294 * alignment of the first physical page in the set. If the given value
1295 * "boundary" is non-zero, then the set of physical pages cannot cross
1296 * any physical address boundary that is a multiple of that value. Both
1297 * "alignment" and "boundary" must be a power of two.
1300 vm_phys_alloc_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
1301 u_long alignment, vm_paddr_t boundary)
1303 struct vm_freelist *fl;
1304 struct vm_phys_seg *seg;
1305 vm_paddr_t pa, pa_last, size;
1308 int domain, flind, oind, order, pind;
1309 struct vm_domain_iterator vi;
1311 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1312 size = npages << PAGE_SHIFT;
1314 ("vm_phys_alloc_contig: size must not be 0"));
1315 KASSERT((alignment & (alignment - 1)) == 0,
1316 ("vm_phys_alloc_contig: alignment must be a power of 2"));
1317 KASSERT((boundary & (boundary - 1)) == 0,
1318 ("vm_phys_alloc_contig: boundary must be a power of 2"));
1319 /* Compute the queue that is the best fit for npages. */
1320 for (order = 0; (1 << order) < npages; order++);
1322 vm_policy_iterator_init(&vi);
1325 if (vm_domain_iterator_run(&vi, &domain) != 0) {
1326 vm_policy_iterator_finish(&vi);
1330 for (flind = 0; flind < vm_nfreelists; flind++) {
1331 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER; oind++) {
1332 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1333 fl = &vm_phys_free_queues[domain][flind][pind][0];
1334 TAILQ_FOREACH(m_ret, &fl[oind].pl, plinks.q) {
1336 * A free list may contain physical pages
1337 * from one or more segments.
1339 seg = &vm_phys_segs[m_ret->segind];
1340 if (seg->start > high ||
1345 * Is the size of this allocation request
1346 * larger than the largest block size?
1348 if (order >= VM_NFREEORDER) {
1350 * Determine if a sufficient number
1351 * of subsequent blocks to satisfy
1352 * the allocation request are free.
1354 pa = VM_PAGE_TO_PHYS(m_ret);
1355 pa_last = pa + size;
1357 pa += 1 << (PAGE_SHIFT + VM_NFREEORDER - 1);
1360 if (pa < seg->start ||
1363 m = &seg->first_page[atop(pa - seg->start)];
1364 if (m->order != VM_NFREEORDER - 1)
1367 /* If not, continue to the next block. */
1373 * Determine if the blocks are within the given range,
1374 * satisfy the given alignment, and do not cross the
1377 pa = VM_PAGE_TO_PHYS(m_ret);
1379 pa + size <= high &&
1380 (pa & (alignment - 1)) == 0 &&
1381 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) == 0)
1387 if (!vm_domain_iterator_isdone(&vi))
1389 vm_policy_iterator_finish(&vi);
1392 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
1393 fl = (*seg->free_queues)[m->pool];
1394 vm_freelist_rem(fl, m, m->order);
1396 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
1397 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
1398 fl = (*seg->free_queues)[m_ret->pool];
1399 vm_phys_split_pages(m_ret, oind, fl, order);
1400 /* Return excess pages to the free lists. */
1401 npages_end = roundup2(npages, 1 << imin(oind, order));
1402 if (npages < npages_end)
1403 vm_phys_free_contig(&m_ret[npages], npages_end - npages);
1409 * Show the number of physical pages in each of the free lists.
1411 DB_SHOW_COMMAND(freepages, db_show_freepages)
1413 struct vm_freelist *fl;
1414 int flind, oind, pind, dom;
1416 for (dom = 0; dom < vm_ndomains; dom++) {
1417 db_printf("DOMAIN: %d\n", dom);
1418 for (flind = 0; flind < vm_nfreelists; flind++) {
1419 db_printf("FREE LIST %d:\n"
1420 "\n ORDER (SIZE) | NUMBER"
1422 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1423 db_printf(" | POOL %d", pind);
1425 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1426 db_printf("-- -- ");
1428 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
1429 db_printf(" %2.2d (%6.6dK)", oind,
1430 1 << (PAGE_SHIFT - 10 + oind));
1431 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1432 fl = vm_phys_free_queues[dom][flind][pind];
1433 db_printf(" | %6.6d", fl[oind].lcnt);