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>
52 #include <sys/queue.h>
53 #include <sys/rwlock.h>
55 #include <sys/sysctl.h>
57 #include <sys/vmmeter.h>
63 #include <vm/vm_param.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_page.h>
67 #include <vm/vm_phys.h>
69 #include <vm/vm_domain.h>
71 _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX,
72 "Too many physsegs.");
75 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 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
136 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
137 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
139 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
140 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
141 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
144 static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS);
145 SYSCTL_OID(_vm, OID_AUTO, phys_locality, CTLTYPE_STRING | CTLFLAG_RD,
146 NULL, 0, sysctl_vm_phys_locality, "A", "Phys Locality Info");
149 SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
150 &vm_ndomains, 0, "Number of physical memory domains available.");
153 * Default to first-touch + round-robin.
155 static struct mtx vm_default_policy_mtx;
156 MTX_SYSINIT(vm_default_policy, &vm_default_policy_mtx, "default policy mutex",
159 static struct vm_domain_policy vm_default_policy =
160 VM_DOMAIN_POLICY_STATIC_INITIALISER(VM_POLICY_FIRST_TOUCH_ROUND_ROBIN, 0);
162 /* Use round-robin so the domain policy code will only try once per allocation */
163 static struct vm_domain_policy vm_default_policy =
164 VM_DOMAIN_POLICY_STATIC_INITIALISER(VM_POLICY_ROUND_ROBIN, 0);
167 static vm_page_t vm_phys_alloc_domain_pages(int domain, int flind, int pool,
169 static vm_page_t vm_phys_alloc_seg_contig(struct vm_phys_seg *seg,
170 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
171 vm_paddr_t boundary);
172 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain);
173 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end);
174 static int vm_phys_paddr_to_segind(vm_paddr_t pa);
175 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
179 sysctl_vm_default_policy(SYSCTL_HANDLER_ARGS)
181 char policy_name[32];
184 mtx_lock(&vm_default_policy_mtx);
186 /* Map policy to output string */
187 switch (vm_default_policy.p.policy) {
188 case VM_POLICY_FIRST_TOUCH:
189 strcpy(policy_name, "first-touch");
191 case VM_POLICY_FIRST_TOUCH_ROUND_ROBIN:
192 strcpy(policy_name, "first-touch-rr");
194 case VM_POLICY_ROUND_ROBIN:
196 strcpy(policy_name, "rr");
199 mtx_unlock(&vm_default_policy_mtx);
201 error = sysctl_handle_string(oidp, &policy_name[0],
202 sizeof(policy_name), req);
203 if (error != 0 || req->newptr == NULL)
206 mtx_lock(&vm_default_policy_mtx);
207 /* Set: match on the subset of policies that make sense as a default */
208 if (strcmp("first-touch-rr", policy_name) == 0) {
209 vm_domain_policy_set(&vm_default_policy,
210 VM_POLICY_FIRST_TOUCH_ROUND_ROBIN, 0);
211 } else if (strcmp("first-touch", policy_name) == 0) {
212 vm_domain_policy_set(&vm_default_policy,
213 VM_POLICY_FIRST_TOUCH, 0);
214 } else if (strcmp("rr", policy_name) == 0) {
215 vm_domain_policy_set(&vm_default_policy,
216 VM_POLICY_ROUND_ROBIN, 0);
224 mtx_unlock(&vm_default_policy_mtx);
228 SYSCTL_PROC(_vm, OID_AUTO, default_policy, CTLTYPE_STRING | CTLFLAG_RW,
229 0, 0, sysctl_vm_default_policy, "A",
230 "Default policy (rr, first-touch, first-touch-rr");
233 * Red-black tree helpers for vm fictitious range management.
236 vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p,
237 struct vm_phys_fictitious_seg *range)
240 KASSERT(range->start != 0 && range->end != 0,
241 ("Invalid range passed on search for vm_fictitious page"));
242 if (p->start >= range->end)
244 if (p->start < range->start)
251 vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1,
252 struct vm_phys_fictitious_seg *p2)
255 /* Check if this is a search for a page */
257 return (vm_phys_fictitious_in_range(p1, p2));
259 KASSERT(p2->end != 0,
260 ("Invalid range passed as second parameter to vm fictitious comparison"));
262 /* Searching to add a new range */
263 if (p1->end <= p2->start)
265 if (p1->start >= p2->end)
268 panic("Trying to add overlapping vm fictitious ranges:\n"
269 "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start,
270 (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end);
274 vm_rr_selectdomain(void)
282 td->td_dom_rr_idx %= vm_ndomains;
283 return (td->td_dom_rr_idx);
290 * Initialise a VM domain iterator.
292 * Check the thread policy, then the proc policy,
293 * then default to the system policy.
295 * Later on the various layers will have this logic
296 * plumbed into them and the phys code will be explicitly
297 * handed a VM domain policy to use.
300 vm_policy_iterator_init(struct vm_domain_iterator *vi)
303 struct vm_domain_policy lcl;
306 vm_domain_iterator_init(vi);
309 /* Copy out the thread policy */
310 vm_domain_policy_localcopy(&lcl, &curthread->td_vm_dom_policy);
311 if (lcl.p.policy != VM_POLICY_NONE) {
312 /* Thread policy is present; use it */
313 vm_domain_iterator_set_policy(vi, &lcl);
317 vm_domain_policy_localcopy(&lcl,
318 &curthread->td_proc->p_vm_dom_policy);
319 if (lcl.p.policy != VM_POLICY_NONE) {
320 /* Process policy is present; use it */
321 vm_domain_iterator_set_policy(vi, &lcl);
325 /* Use system default policy */
326 vm_domain_iterator_set_policy(vi, &vm_default_policy);
330 vm_policy_iterator_finish(struct vm_domain_iterator *vi)
333 vm_domain_iterator_cleanup(vi);
337 vm_phys_domain_intersects(long mask, vm_paddr_t low, vm_paddr_t high)
339 struct vm_phys_seg *s;
342 while ((idx = ffsl(mask)) != 0) {
343 idx--; /* ffsl counts from 1 */
344 mask &= ~(1UL << idx);
345 s = &vm_phys_segs[idx];
346 if (low < s->end && high > s->start)
353 * Outputs the state of the physical memory allocator, specifically,
354 * the amount of physical memory in each free list.
357 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
360 struct vm_freelist *fl;
361 int dom, error, flind, oind, pind;
363 error = sysctl_wire_old_buffer(req, 0);
366 sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req);
367 for (dom = 0; dom < vm_ndomains; dom++) {
368 sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom);
369 for (flind = 0; flind < vm_nfreelists; flind++) {
370 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
371 "\n ORDER (SIZE) | NUMBER"
373 for (pind = 0; pind < VM_NFREEPOOL; pind++)
374 sbuf_printf(&sbuf, " | POOL %d", pind);
375 sbuf_printf(&sbuf, "\n-- ");
376 for (pind = 0; pind < VM_NFREEPOOL; pind++)
377 sbuf_printf(&sbuf, "-- -- ");
378 sbuf_printf(&sbuf, "--\n");
379 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
380 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
381 1 << (PAGE_SHIFT - 10 + oind));
382 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
383 fl = vm_phys_free_queues[dom][flind][pind];
384 sbuf_printf(&sbuf, " | %6d",
387 sbuf_printf(&sbuf, "\n");
391 error = sbuf_finish(&sbuf);
397 * Outputs the set of physical memory segments.
400 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
403 struct vm_phys_seg *seg;
406 error = sysctl_wire_old_buffer(req, 0);
409 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
410 for (segind = 0; segind < vm_phys_nsegs; segind++) {
411 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
412 seg = &vm_phys_segs[segind];
413 sbuf_printf(&sbuf, "start: %#jx\n",
414 (uintmax_t)seg->start);
415 sbuf_printf(&sbuf, "end: %#jx\n",
416 (uintmax_t)seg->end);
417 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
418 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
420 error = sbuf_finish(&sbuf);
426 * Return affinity, or -1 if there's no affinity information.
429 vm_phys_mem_affinity(int f, int t)
433 if (mem_locality == NULL)
435 if (f >= vm_ndomains || t >= vm_ndomains)
437 return (mem_locality[f * vm_ndomains + t]);
445 * Outputs the VM locality table.
448 sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS)
453 error = sysctl_wire_old_buffer(req, 0);
456 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
458 sbuf_printf(&sbuf, "\n");
460 for (i = 0; i < vm_ndomains; i++) {
461 sbuf_printf(&sbuf, "%d: ", i);
462 for (j = 0; j < vm_ndomains; j++) {
463 sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j));
465 sbuf_printf(&sbuf, "\n");
467 error = sbuf_finish(&sbuf);
474 vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
479 TAILQ_INSERT_TAIL(&fl[order].pl, m, plinks.q);
481 TAILQ_INSERT_HEAD(&fl[order].pl, m, plinks.q);
486 vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
489 TAILQ_REMOVE(&fl[order].pl, m, plinks.q);
491 m->order = VM_NFREEORDER;
495 * Create a physical memory segment.
498 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain)
500 struct vm_phys_seg *seg;
502 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
503 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
504 KASSERT(domain < vm_ndomains,
505 ("vm_phys_create_seg: invalid domain provided"));
506 seg = &vm_phys_segs[vm_phys_nsegs++];
507 while (seg > vm_phys_segs && (seg - 1)->start >= end) {
513 seg->domain = domain;
517 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;
545 _vm_phys_create_seg(start, end, 0);
550 * Add a physical memory segment.
553 vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
557 KASSERT((start & PAGE_MASK) == 0,
558 ("vm_phys_define_seg: start is not page aligned"));
559 KASSERT((end & PAGE_MASK) == 0,
560 ("vm_phys_define_seg: end is not page aligned"));
563 * Split the physical memory segment if it spans two or more free
567 #ifdef VM_FREELIST_ISADMA
568 if (paddr < VM_ISADMA_BOUNDARY && end > VM_ISADMA_BOUNDARY) {
569 vm_phys_create_seg(paddr, VM_ISADMA_BOUNDARY);
570 paddr = VM_ISADMA_BOUNDARY;
573 #ifdef VM_FREELIST_LOWMEM
574 if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) {
575 vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY);
576 paddr = VM_LOWMEM_BOUNDARY;
579 #ifdef VM_FREELIST_DMA32
580 if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) {
581 vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY);
582 paddr = VM_DMA32_BOUNDARY;
585 vm_phys_create_seg(paddr, end);
589 * Initialize the physical memory allocator.
591 * Requires that vm_page_array is initialized!
596 struct vm_freelist *fl;
597 struct vm_phys_seg *seg;
599 int dom, flind, freelist, oind, pind, segind;
602 * Compute the number of free lists, and generate the mapping from the
603 * manifest constants VM_FREELIST_* to the free list indices.
605 * Initially, the entries of vm_freelist_to_flind[] are set to either
606 * 0 or 1 to indicate which free lists should be created.
609 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
610 seg = &vm_phys_segs[segind];
611 #ifdef VM_FREELIST_ISADMA
612 if (seg->end <= VM_ISADMA_BOUNDARY)
613 vm_freelist_to_flind[VM_FREELIST_ISADMA] = 1;
616 #ifdef VM_FREELIST_LOWMEM
617 if (seg->end <= VM_LOWMEM_BOUNDARY)
618 vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1;
621 #ifdef VM_FREELIST_DMA32
623 #ifdef VM_DMA32_NPAGES_THRESHOLD
625 * Create the DMA32 free list only if the amount of
626 * physical memory above physical address 4G exceeds the
629 npages > VM_DMA32_NPAGES_THRESHOLD &&
631 seg->end <= VM_DMA32_BOUNDARY)
632 vm_freelist_to_flind[VM_FREELIST_DMA32] = 1;
636 npages += atop(seg->end - seg->start);
637 vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1;
640 /* Change each entry into a running total of the free lists. */
641 for (freelist = 1; freelist < VM_NFREELIST; freelist++) {
642 vm_freelist_to_flind[freelist] +=
643 vm_freelist_to_flind[freelist - 1];
645 vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1];
646 KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists"));
647 /* Change each entry into a free list index. */
648 for (freelist = 0; freelist < VM_NFREELIST; freelist++)
649 vm_freelist_to_flind[freelist]--;
652 * Initialize the first_page and free_queues fields of each physical
655 #ifdef VM_PHYSSEG_SPARSE
658 for (segind = 0; segind < vm_phys_nsegs; segind++) {
659 seg = &vm_phys_segs[segind];
660 #ifdef VM_PHYSSEG_SPARSE
661 seg->first_page = &vm_page_array[npages];
662 npages += atop(seg->end - seg->start);
664 seg->first_page = PHYS_TO_VM_PAGE(seg->start);
666 #ifdef VM_FREELIST_ISADMA
667 if (seg->end <= VM_ISADMA_BOUNDARY) {
668 flind = vm_freelist_to_flind[VM_FREELIST_ISADMA];
670 ("vm_phys_init: ISADMA flind < 0"));
673 #ifdef VM_FREELIST_LOWMEM
674 if (seg->end <= VM_LOWMEM_BOUNDARY) {
675 flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM];
677 ("vm_phys_init: LOWMEM flind < 0"));
680 #ifdef VM_FREELIST_DMA32
681 if (seg->end <= VM_DMA32_BOUNDARY) {
682 flind = vm_freelist_to_flind[VM_FREELIST_DMA32];
684 ("vm_phys_init: DMA32 flind < 0"));
688 flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT];
690 ("vm_phys_init: DEFAULT flind < 0"));
692 seg->free_queues = &vm_phys_free_queues[seg->domain][flind];
696 * Initialize the free queues.
698 for (dom = 0; dom < vm_ndomains; dom++) {
699 for (flind = 0; flind < vm_nfreelists; flind++) {
700 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
701 fl = vm_phys_free_queues[dom][flind][pind];
702 for (oind = 0; oind < VM_NFREEORDER; oind++)
703 TAILQ_INIT(&fl[oind].pl);
708 rw_init(&vm_phys_fictitious_reg_lock, "vmfctr");
712 * Split a contiguous, power of two-sized set of physical pages.
715 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
719 while (oind > order) {
721 m_buddy = &m[1 << oind];
722 KASSERT(m_buddy->order == VM_NFREEORDER,
723 ("vm_phys_split_pages: page %p has unexpected order %d",
724 m_buddy, m_buddy->order));
725 vm_freelist_add(fl, m_buddy, oind, 0);
730 * Initialize a physical page and add it to the free lists.
733 vm_phys_add_page(vm_paddr_t pa)
736 struct vm_domain *vmd;
738 vm_cnt.v_page_count++;
739 m = vm_phys_paddr_to_vm_page(pa);
740 m->busy_lock = VPB_UNBUSIED;
743 m->segind = vm_phys_paddr_to_segind(pa);
744 vmd = vm_phys_domain(m);
745 vmd->vmd_page_count++;
746 vmd->vmd_segs |= 1UL << m->segind;
747 KASSERT(m->order == VM_NFREEORDER,
748 ("vm_phys_add_page: page %p has unexpected order %d",
750 m->pool = VM_FREEPOOL_DEFAULT;
752 mtx_lock(&vm_page_queue_free_mtx);
753 vm_phys_freecnt_adj(m, 1);
754 vm_phys_free_pages(m, 0);
755 mtx_unlock(&vm_page_queue_free_mtx);
759 * Allocate a contiguous, power of two-sized set of physical pages
760 * from the free lists.
762 * The free page queues must be locked.
765 vm_phys_alloc_pages(int pool, int order)
769 struct vm_domain_iterator vi;
771 KASSERT(pool < VM_NFREEPOOL,
772 ("vm_phys_alloc_pages: pool %d is out of range", pool));
773 KASSERT(order < VM_NFREEORDER,
774 ("vm_phys_alloc_pages: order %d is out of range", order));
776 vm_policy_iterator_init(&vi);
778 while ((vm_domain_iterator_run(&vi, &domain)) == 0) {
779 for (flind = 0; flind < vm_nfreelists; flind++) {
780 m = vm_phys_alloc_domain_pages(domain, flind, pool,
787 vm_policy_iterator_finish(&vi);
792 * Allocate a contiguous, power of two-sized set of physical pages from the
793 * specified free list. The free list must be specified using one of the
794 * manifest constants VM_FREELIST_*.
796 * The free page queues must be locked.
799 vm_phys_alloc_freelist_pages(int freelist, int pool, int order)
802 struct vm_domain_iterator vi;
805 KASSERT(freelist < VM_NFREELIST,
806 ("vm_phys_alloc_freelist_pages: freelist %d is out of range",
808 KASSERT(pool < VM_NFREEPOOL,
809 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
810 KASSERT(order < VM_NFREEORDER,
811 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
813 vm_policy_iterator_init(&vi);
815 while ((vm_domain_iterator_run(&vi, &domain)) == 0) {
816 m = vm_phys_alloc_domain_pages(domain,
817 vm_freelist_to_flind[freelist], pool, order);
822 vm_policy_iterator_finish(&vi);
827 vm_phys_alloc_domain_pages(int domain, int flind, int pool, int order)
829 struct vm_freelist *fl;
830 struct vm_freelist *alt;
834 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
835 fl = &vm_phys_free_queues[domain][flind][pool][0];
836 for (oind = order; oind < VM_NFREEORDER; oind++) {
837 m = TAILQ_FIRST(&fl[oind].pl);
839 vm_freelist_rem(fl, m, oind);
840 vm_phys_split_pages(m, oind, fl, order);
846 * The given pool was empty. Find the largest
847 * contiguous, power-of-two-sized set of pages in any
848 * pool. Transfer these pages to the given pool, and
849 * use them to satisfy the allocation.
851 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
852 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
853 alt = &vm_phys_free_queues[domain][flind][pind][0];
854 m = TAILQ_FIRST(&alt[oind].pl);
856 vm_freelist_rem(alt, m, oind);
857 vm_phys_set_pool(pool, m, oind);
858 vm_phys_split_pages(m, oind, fl, order);
867 * Find the vm_page corresponding to the given physical address.
870 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
872 struct vm_phys_seg *seg;
875 for (segind = 0; segind < vm_phys_nsegs; segind++) {
876 seg = &vm_phys_segs[segind];
877 if (pa >= seg->start && pa < seg->end)
878 return (&seg->first_page[atop(pa - seg->start)]);
884 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
886 struct vm_phys_fictitious_seg tmp, *seg;
893 rw_rlock(&vm_phys_fictitious_reg_lock);
894 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
895 rw_runlock(&vm_phys_fictitious_reg_lock);
899 m = &seg->first_page[atop(pa - seg->start)];
900 KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m));
906 vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start,
907 long page_count, vm_memattr_t memattr)
911 for (i = 0; i < page_count; i++) {
912 vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr);
913 range[i].oflags &= ~VPO_UNMANAGED;
914 range[i].busy_lock = VPB_UNBUSIED;
919 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
920 vm_memattr_t memattr)
922 struct vm_phys_fictitious_seg *seg;
925 #ifdef VM_PHYSSEG_DENSE
931 ("Start of segment isn't less than end (start: %jx end: %jx)",
932 (uintmax_t)start, (uintmax_t)end));
934 page_count = (end - start) / PAGE_SIZE;
936 #ifdef VM_PHYSSEG_DENSE
939 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
940 fp = &vm_page_array[pi - first_page];
941 if ((pe - first_page) > vm_page_array_size) {
943 * We have a segment that starts inside
944 * of vm_page_array, but ends outside of it.
946 * Use vm_page_array pages for those that are
947 * inside of the vm_page_array range, and
948 * allocate the remaining ones.
950 dpage_count = vm_page_array_size - (pi - first_page);
951 vm_phys_fictitious_init_range(fp, start, dpage_count,
953 page_count -= dpage_count;
954 start += ptoa(dpage_count);
958 * We can allocate the full range from vm_page_array,
959 * so there's no need to register the range in the tree.
961 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
963 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
965 * We have a segment that ends inside of vm_page_array,
966 * but starts outside of it.
968 fp = &vm_page_array[0];
969 dpage_count = pe - first_page;
970 vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count,
972 end -= ptoa(dpage_count);
973 page_count -= dpage_count;
975 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
977 * Trying to register a fictitious range that expands before
978 * and after vm_page_array.
984 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
986 #ifdef VM_PHYSSEG_DENSE
989 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
991 seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO);
994 seg->first_page = fp;
996 rw_wlock(&vm_phys_fictitious_reg_lock);
997 RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg);
998 rw_wunlock(&vm_phys_fictitious_reg_lock);
1004 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
1006 struct vm_phys_fictitious_seg *seg, tmp;
1007 #ifdef VM_PHYSSEG_DENSE
1011 KASSERT(start < end,
1012 ("Start of segment isn't less than end (start: %jx end: %jx)",
1013 (uintmax_t)start, (uintmax_t)end));
1015 #ifdef VM_PHYSSEG_DENSE
1018 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1019 if ((pe - first_page) <= vm_page_array_size) {
1021 * This segment was allocated using vm_page_array
1022 * only, there's nothing to do since those pages
1023 * were never added to the tree.
1028 * We have a segment that starts inside
1029 * of vm_page_array, but ends outside of it.
1031 * Calculate how many pages were added to the
1032 * tree and free them.
1034 start = ptoa(first_page + vm_page_array_size);
1035 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
1037 * We have a segment that ends inside of vm_page_array,
1038 * but starts outside of it.
1040 end = ptoa(first_page);
1041 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
1042 /* Since it's not possible to register such a range, panic. */
1044 "Unregistering not registered fictitious range [%#jx:%#jx]",
1045 (uintmax_t)start, (uintmax_t)end);
1051 rw_wlock(&vm_phys_fictitious_reg_lock);
1052 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
1053 if (seg->start != start || seg->end != end) {
1054 rw_wunlock(&vm_phys_fictitious_reg_lock);
1056 "Unregistering not registered fictitious range [%#jx:%#jx]",
1057 (uintmax_t)start, (uintmax_t)end);
1059 RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg);
1060 rw_wunlock(&vm_phys_fictitious_reg_lock);
1061 free(seg->first_page, M_FICT_PAGES);
1062 free(seg, M_FICT_PAGES);
1066 * Find the segment containing the given physical address.
1069 vm_phys_paddr_to_segind(vm_paddr_t pa)
1071 struct vm_phys_seg *seg;
1074 for (segind = 0; segind < vm_phys_nsegs; segind++) {
1075 seg = &vm_phys_segs[segind];
1076 if (pa >= seg->start && pa < seg->end)
1079 panic("vm_phys_paddr_to_segind: paddr %#jx is not in any segment" ,
1084 * Free a contiguous, power of two-sized set of physical pages.
1086 * The free page queues must be locked.
1089 vm_phys_free_pages(vm_page_t m, int order)
1091 struct vm_freelist *fl;
1092 struct vm_phys_seg *seg;
1096 KASSERT(m->order == VM_NFREEORDER,
1097 ("vm_phys_free_pages: page %p has unexpected order %d",
1099 KASSERT(m->pool < VM_NFREEPOOL,
1100 ("vm_phys_free_pages: page %p has unexpected pool %d",
1102 KASSERT(order < VM_NFREEORDER,
1103 ("vm_phys_free_pages: order %d is out of range", order));
1104 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1105 seg = &vm_phys_segs[m->segind];
1106 if (order < VM_NFREEORDER - 1) {
1107 pa = VM_PAGE_TO_PHYS(m);
1109 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
1110 if (pa < seg->start || pa >= seg->end)
1112 m_buddy = &seg->first_page[atop(pa - seg->start)];
1113 if (m_buddy->order != order)
1115 fl = (*seg->free_queues)[m_buddy->pool];
1116 vm_freelist_rem(fl, m_buddy, order);
1117 if (m_buddy->pool != m->pool)
1118 vm_phys_set_pool(m->pool, m_buddy, order);
1120 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
1121 m = &seg->first_page[atop(pa - seg->start)];
1122 } while (order < VM_NFREEORDER - 1);
1124 fl = (*seg->free_queues)[m->pool];
1125 vm_freelist_add(fl, m, order, 1);
1129 * Free a contiguous, arbitrarily sized set of physical pages.
1131 * The free page queues must be locked.
1134 vm_phys_free_contig(vm_page_t m, u_long npages)
1140 * Avoid unnecessary coalescing by freeing the pages in the largest
1141 * possible power-of-two-sized subsets.
1143 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1144 for (;; npages -= n) {
1146 * Unsigned "min" is used here so that "order" is assigned
1147 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
1148 * or the low-order bits of its physical address are zero
1149 * because the size of a physical address exceeds the size of
1152 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
1157 vm_phys_free_pages(m, order);
1160 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
1161 for (; npages > 0; npages -= n) {
1162 order = flsl(npages) - 1;
1164 vm_phys_free_pages(m, order);
1170 * Scan physical memory between the specified addresses "low" and "high" for a
1171 * run of contiguous physical pages that satisfy the specified conditions, and
1172 * return the lowest page in the run. The specified "alignment" determines
1173 * the alignment of the lowest physical page in the run. If the specified
1174 * "boundary" is non-zero, then the run of physical pages cannot span a
1175 * physical address that is a multiple of "boundary".
1177 * "npages" must be greater than zero. Both "alignment" and "boundary" must
1178 * be a power of two.
1181 vm_phys_scan_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
1182 u_long alignment, vm_paddr_t boundary, int options)
1185 vm_page_t m_end, m_run, m_start;
1186 struct vm_phys_seg *seg;
1189 KASSERT(npages > 0, ("npages is 0"));
1190 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1191 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1194 for (segind = 0; segind < vm_phys_nsegs; segind++) {
1195 seg = &vm_phys_segs[segind];
1196 if (seg->start >= high)
1198 if (low >= seg->end)
1200 if (low <= seg->start)
1201 m_start = seg->first_page;
1203 m_start = &seg->first_page[atop(low - seg->start)];
1204 if (high < seg->end)
1208 if (pa_end - VM_PAGE_TO_PHYS(m_start) < ptoa(npages))
1210 m_end = &seg->first_page[atop(pa_end - seg->start)];
1211 m_run = vm_page_scan_contig(npages, m_start, m_end,
1212 alignment, boundary, options);
1220 * Set the pool for a contiguous, power of two-sized set of physical pages.
1223 vm_phys_set_pool(int pool, vm_page_t m, int order)
1227 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
1232 * Search for the given physical page "m" in the free lists. If the search
1233 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
1234 * FALSE, indicating that "m" is not in the free lists.
1236 * The free page queues must be locked.
1239 vm_phys_unfree_page(vm_page_t m)
1241 struct vm_freelist *fl;
1242 struct vm_phys_seg *seg;
1243 vm_paddr_t pa, pa_half;
1244 vm_page_t m_set, m_tmp;
1247 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1250 * First, find the contiguous, power of two-sized set of free
1251 * physical pages containing the given physical page "m" and
1252 * assign it to "m_set".
1254 seg = &vm_phys_segs[m->segind];
1255 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
1256 order < VM_NFREEORDER - 1; ) {
1258 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
1259 if (pa >= seg->start)
1260 m_set = &seg->first_page[atop(pa - seg->start)];
1264 if (m_set->order < order)
1266 if (m_set->order == VM_NFREEORDER)
1268 KASSERT(m_set->order < VM_NFREEORDER,
1269 ("vm_phys_unfree_page: page %p has unexpected order %d",
1270 m_set, m_set->order));
1273 * Next, remove "m_set" from the free lists. Finally, extract
1274 * "m" from "m_set" using an iterative algorithm: While "m_set"
1275 * is larger than a page, shrink "m_set" by returning the half
1276 * of "m_set" that does not contain "m" to the free lists.
1278 fl = (*seg->free_queues)[m_set->pool];
1279 order = m_set->order;
1280 vm_freelist_rem(fl, m_set, order);
1283 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
1284 if (m->phys_addr < pa_half)
1285 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
1288 m_set = &seg->first_page[atop(pa_half - seg->start)];
1290 vm_freelist_add(fl, m_tmp, order, 0);
1292 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
1297 * Allocate a contiguous set of physical pages of the given size
1298 * "npages" from the free lists. All of the physical pages must be at
1299 * or above the given physical address "low" and below the given
1300 * physical address "high". The given value "alignment" determines the
1301 * alignment of the first physical page in the set. If the given value
1302 * "boundary" is non-zero, then the set of physical pages cannot cross
1303 * any physical address boundary that is a multiple of that value. Both
1304 * "alignment" and "boundary" must be a power of two.
1307 vm_phys_alloc_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
1308 u_long alignment, vm_paddr_t boundary)
1310 vm_paddr_t pa_end, pa_start;
1312 struct vm_domain_iterator vi;
1313 struct vm_phys_seg *seg;
1316 KASSERT(npages > 0, ("npages is 0"));
1317 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1318 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1319 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1322 vm_policy_iterator_init(&vi);
1324 if (vm_domain_iterator_run(&vi, &domain) != 0) {
1325 vm_policy_iterator_finish(&vi);
1329 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
1330 seg = &vm_phys_segs[segind];
1331 if (seg->start >= high || seg->domain != domain)
1333 if (low >= seg->end)
1335 if (low <= seg->start)
1336 pa_start = seg->start;
1339 if (high < seg->end)
1343 if (pa_end - pa_start < ptoa(npages))
1345 m_run = vm_phys_alloc_seg_contig(seg, npages, low, high,
1346 alignment, boundary);
1350 if (m_run == NULL && !vm_domain_iterator_isdone(&vi))
1352 vm_policy_iterator_finish(&vi);
1357 * Allocate a run of contiguous physical pages from the free list for the
1358 * specified segment.
1361 vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, u_long npages,
1362 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1364 struct vm_freelist *fl;
1365 vm_paddr_t pa, pa_end, size;
1368 int oind, order, pind;
1370 KASSERT(npages > 0, ("npages is 0"));
1371 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1372 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1373 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1374 /* Compute the queue that is the best fit for npages. */
1375 for (order = 0; (1 << order) < npages; order++);
1376 /* Search for a run satisfying the specified conditions. */
1377 size = npages << PAGE_SHIFT;
1378 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER;
1380 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1381 fl = (*seg->free_queues)[pind];
1382 TAILQ_FOREACH(m_ret, &fl[oind].pl, plinks.q) {
1384 * Is the size of this allocation request
1385 * larger than the largest block size?
1387 if (order >= VM_NFREEORDER) {
1389 * Determine if a sufficient number of
1390 * subsequent blocks to satisfy the
1391 * allocation request are free.
1393 pa = VM_PAGE_TO_PHYS(m_ret);
1396 pa += 1 << (PAGE_SHIFT +
1402 m = &seg->first_page[atop(pa -
1404 if (m->order != VM_NFREEORDER -
1408 /* If not, go to the next block. */
1414 * Determine if the blocks are within the
1415 * given range, satisfy the given alignment,
1416 * and do not cross the given boundary.
1418 pa = VM_PAGE_TO_PHYS(m_ret);
1420 if (pa >= low && pa_end <= high &&
1421 (pa & (alignment - 1)) == 0 &&
1422 rounddown2(pa ^ (pa_end - 1), boundary) == 0)
1429 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
1430 fl = (*seg->free_queues)[m->pool];
1431 vm_freelist_rem(fl, m, m->order);
1433 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
1434 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
1435 fl = (*seg->free_queues)[m_ret->pool];
1436 vm_phys_split_pages(m_ret, oind, fl, order);
1437 /* Return excess pages to the free lists. */
1438 npages_end = roundup2(npages, 1 << imin(oind, order));
1439 if (npages < npages_end)
1440 vm_phys_free_contig(&m_ret[npages], npages_end - npages);
1446 * Show the number of physical pages in each of the free lists.
1448 DB_SHOW_COMMAND(freepages, db_show_freepages)
1450 struct vm_freelist *fl;
1451 int flind, oind, pind, dom;
1453 for (dom = 0; dom < vm_ndomains; dom++) {
1454 db_printf("DOMAIN: %d\n", dom);
1455 for (flind = 0; flind < vm_nfreelists; flind++) {
1456 db_printf("FREE LIST %d:\n"
1457 "\n ORDER (SIZE) | NUMBER"
1459 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1460 db_printf(" | POOL %d", pind);
1462 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1463 db_printf("-- -- ");
1465 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
1466 db_printf(" %2.2d (%6.6dK)", oind,
1467 1 << (PAGE_SHIFT - 10 + oind));
1468 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1469 fl = vm_phys_free_queues[dom][flind][pind];
1470 db_printf(" | %6.6d", fl[oind].lcnt);