2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2006 Rice University
5 * Copyright (c) 2007 Alan L. Cox <alc@cs.rice.edu>
8 * This software was developed for the FreeBSD Project by Alan L. Cox,
9 * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
30 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
35 * Physical memory system implementation
37 * Any external functions defined by this module are only to be used by the
38 * virtual memory system.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #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>
70 #include <vm/vm_pagequeue.h>
72 _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX,
73 "Too many physsegs.");
76 struct mem_affinity __read_mostly *mem_affinity;
77 int __read_mostly *mem_locality;
80 int __read_mostly vm_ndomains = 1;
82 struct vm_phys_seg __read_mostly vm_phys_segs[VM_PHYSSEG_MAX];
83 int __read_mostly vm_phys_nsegs;
85 struct vm_phys_fictitious_seg;
86 static int vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *,
87 struct vm_phys_fictitious_seg *);
89 RB_HEAD(fict_tree, vm_phys_fictitious_seg) vm_phys_fictitious_tree =
90 RB_INITIALIZER(_vm_phys_fictitious_tree);
92 struct vm_phys_fictitious_seg {
93 RB_ENTRY(vm_phys_fictitious_seg) node;
94 /* Memory region data */
100 RB_GENERATE_STATIC(fict_tree, vm_phys_fictitious_seg, node,
101 vm_phys_fictitious_cmp);
103 static struct rwlock_padalign vm_phys_fictitious_reg_lock;
104 MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages");
106 static struct vm_freelist __aligned(CACHE_LINE_SIZE)
107 vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL][VM_NFREEORDER];
109 static int __read_mostly vm_nfreelists;
112 * Provides the mapping from VM_FREELIST_* to free list indices (flind).
114 static int __read_mostly vm_freelist_to_flind[VM_NFREELIST];
116 CTASSERT(VM_FREELIST_DEFAULT == 0);
118 #ifdef VM_FREELIST_ISADMA
119 #define VM_ISADMA_BOUNDARY 16777216
121 #ifdef VM_FREELIST_DMA32
122 #define VM_DMA32_BOUNDARY ((vm_paddr_t)1 << 32)
126 * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about
127 * the ordering of the free list boundaries.
129 #if defined(VM_ISADMA_BOUNDARY) && defined(VM_LOWMEM_BOUNDARY)
130 CTASSERT(VM_ISADMA_BOUNDARY < VM_LOWMEM_BOUNDARY);
132 #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY)
133 CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY);
136 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
137 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
138 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
140 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
141 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
142 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
145 static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS);
146 SYSCTL_OID(_vm, OID_AUTO, phys_locality, CTLTYPE_STRING | CTLFLAG_RD,
147 NULL, 0, sysctl_vm_phys_locality, "A", "Phys Locality Info");
150 SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
151 &vm_ndomains, 0, "Number of physical memory domains available.");
153 static vm_page_t vm_phys_alloc_seg_contig(struct vm_phys_seg *seg,
154 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
155 vm_paddr_t boundary);
156 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain);
157 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end);
158 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
162 * Red-black tree helpers for vm fictitious range management.
165 vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p,
166 struct vm_phys_fictitious_seg *range)
169 KASSERT(range->start != 0 && range->end != 0,
170 ("Invalid range passed on search for vm_fictitious page"));
171 if (p->start >= range->end)
173 if (p->start < range->start)
180 vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1,
181 struct vm_phys_fictitious_seg *p2)
184 /* Check if this is a search for a page */
186 return (vm_phys_fictitious_in_range(p1, p2));
188 KASSERT(p2->end != 0,
189 ("Invalid range passed as second parameter to vm fictitious comparison"));
191 /* Searching to add a new range */
192 if (p1->end <= p2->start)
194 if (p1->start >= p2->end)
197 panic("Trying to add overlapping vm fictitious ranges:\n"
198 "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start,
199 (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end);
203 vm_phys_domain_match(int prefer, vm_paddr_t low, vm_paddr_t high)
209 if (vm_ndomains == 1 || mem_affinity == NULL)
212 DOMAINSET_ZERO(&mask);
214 * Check for any memory that overlaps low, high.
216 for (i = 0; mem_affinity[i].end != 0; i++)
217 if (mem_affinity[i].start <= high &&
218 mem_affinity[i].end >= low)
219 DOMAINSET_SET(mem_affinity[i].domain, &mask);
220 if (prefer != -1 && DOMAINSET_ISSET(prefer, &mask))
222 if (DOMAINSET_EMPTY(&mask))
223 panic("vm_phys_domain_match: Impossible constraint");
224 return (DOMAINSET_FFS(&mask) - 1);
231 * Outputs the state of the physical memory allocator, specifically,
232 * the amount of physical memory in each free list.
235 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
238 struct vm_freelist *fl;
239 int dom, error, flind, oind, pind;
241 error = sysctl_wire_old_buffer(req, 0);
244 sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req);
245 for (dom = 0; dom < vm_ndomains; dom++) {
246 sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom);
247 for (flind = 0; flind < vm_nfreelists; flind++) {
248 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
249 "\n ORDER (SIZE) | NUMBER"
251 for (pind = 0; pind < VM_NFREEPOOL; pind++)
252 sbuf_printf(&sbuf, " | POOL %d", pind);
253 sbuf_printf(&sbuf, "\n-- ");
254 for (pind = 0; pind < VM_NFREEPOOL; pind++)
255 sbuf_printf(&sbuf, "-- -- ");
256 sbuf_printf(&sbuf, "--\n");
257 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
258 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
259 1 << (PAGE_SHIFT - 10 + oind));
260 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
261 fl = vm_phys_free_queues[dom][flind][pind];
262 sbuf_printf(&sbuf, " | %6d",
265 sbuf_printf(&sbuf, "\n");
269 error = sbuf_finish(&sbuf);
275 * Outputs the set of physical memory segments.
278 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
281 struct vm_phys_seg *seg;
284 error = sysctl_wire_old_buffer(req, 0);
287 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
288 for (segind = 0; segind < vm_phys_nsegs; segind++) {
289 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
290 seg = &vm_phys_segs[segind];
291 sbuf_printf(&sbuf, "start: %#jx\n",
292 (uintmax_t)seg->start);
293 sbuf_printf(&sbuf, "end: %#jx\n",
294 (uintmax_t)seg->end);
295 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
296 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
298 error = sbuf_finish(&sbuf);
304 * Return affinity, or -1 if there's no affinity information.
307 vm_phys_mem_affinity(int f, int t)
311 if (mem_locality == NULL)
313 if (f >= vm_ndomains || t >= vm_ndomains)
315 return (mem_locality[f * vm_ndomains + t]);
323 * Outputs the VM locality table.
326 sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS)
331 error = sysctl_wire_old_buffer(req, 0);
334 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
336 sbuf_printf(&sbuf, "\n");
338 for (i = 0; i < vm_ndomains; i++) {
339 sbuf_printf(&sbuf, "%d: ", i);
340 for (j = 0; j < vm_ndomains; j++) {
341 sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j));
343 sbuf_printf(&sbuf, "\n");
345 error = sbuf_finish(&sbuf);
352 vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
357 TAILQ_INSERT_TAIL(&fl[order].pl, m, listq);
359 TAILQ_INSERT_HEAD(&fl[order].pl, m, listq);
364 vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
367 TAILQ_REMOVE(&fl[order].pl, m, listq);
369 m->order = VM_NFREEORDER;
373 * Create a physical memory segment.
376 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain)
378 struct vm_phys_seg *seg;
380 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
381 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
382 KASSERT(domain >= 0 && domain < vm_ndomains,
383 ("vm_phys_create_seg: invalid domain provided"));
384 seg = &vm_phys_segs[vm_phys_nsegs++];
385 while (seg > vm_phys_segs && (seg - 1)->start >= end) {
391 seg->domain = domain;
395 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end)
400 if (mem_affinity == NULL) {
401 _vm_phys_create_seg(start, end, 0);
406 if (mem_affinity[i].end == 0)
407 panic("Reached end of affinity info");
408 if (mem_affinity[i].end <= start)
410 if (mem_affinity[i].start > start)
411 panic("No affinity info for start %jx",
413 if (mem_affinity[i].end >= end) {
414 _vm_phys_create_seg(start, end,
415 mem_affinity[i].domain);
418 _vm_phys_create_seg(start, mem_affinity[i].end,
419 mem_affinity[i].domain);
420 start = mem_affinity[i].end;
423 _vm_phys_create_seg(start, end, 0);
428 * Add a physical memory segment.
431 vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
435 KASSERT((start & PAGE_MASK) == 0,
436 ("vm_phys_define_seg: start is not page aligned"));
437 KASSERT((end & PAGE_MASK) == 0,
438 ("vm_phys_define_seg: end is not page aligned"));
441 * Split the physical memory segment if it spans two or more free
445 #ifdef VM_FREELIST_ISADMA
446 if (paddr < VM_ISADMA_BOUNDARY && end > VM_ISADMA_BOUNDARY) {
447 vm_phys_create_seg(paddr, VM_ISADMA_BOUNDARY);
448 paddr = VM_ISADMA_BOUNDARY;
451 #ifdef VM_FREELIST_LOWMEM
452 if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) {
453 vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY);
454 paddr = VM_LOWMEM_BOUNDARY;
457 #ifdef VM_FREELIST_DMA32
458 if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) {
459 vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY);
460 paddr = VM_DMA32_BOUNDARY;
463 vm_phys_create_seg(paddr, end);
467 * Initialize the physical memory allocator.
469 * Requires that vm_page_array is initialized!
474 struct vm_freelist *fl;
475 struct vm_phys_seg *seg;
477 int dom, flind, freelist, oind, pind, segind;
480 * Compute the number of free lists, and generate the mapping from the
481 * manifest constants VM_FREELIST_* to the free list indices.
483 * Initially, the entries of vm_freelist_to_flind[] are set to either
484 * 0 or 1 to indicate which free lists should be created.
487 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
488 seg = &vm_phys_segs[segind];
489 #ifdef VM_FREELIST_ISADMA
490 if (seg->end <= VM_ISADMA_BOUNDARY)
491 vm_freelist_to_flind[VM_FREELIST_ISADMA] = 1;
494 #ifdef VM_FREELIST_LOWMEM
495 if (seg->end <= VM_LOWMEM_BOUNDARY)
496 vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1;
499 #ifdef VM_FREELIST_DMA32
501 #ifdef VM_DMA32_NPAGES_THRESHOLD
503 * Create the DMA32 free list only if the amount of
504 * physical memory above physical address 4G exceeds the
507 npages > VM_DMA32_NPAGES_THRESHOLD &&
509 seg->end <= VM_DMA32_BOUNDARY)
510 vm_freelist_to_flind[VM_FREELIST_DMA32] = 1;
514 npages += atop(seg->end - seg->start);
515 vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1;
518 /* Change each entry into a running total of the free lists. */
519 for (freelist = 1; freelist < VM_NFREELIST; freelist++) {
520 vm_freelist_to_flind[freelist] +=
521 vm_freelist_to_flind[freelist - 1];
523 vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1];
524 KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists"));
525 /* Change each entry into a free list index. */
526 for (freelist = 0; freelist < VM_NFREELIST; freelist++)
527 vm_freelist_to_flind[freelist]--;
530 * Initialize the first_page and free_queues fields of each physical
533 #ifdef VM_PHYSSEG_SPARSE
536 for (segind = 0; segind < vm_phys_nsegs; segind++) {
537 seg = &vm_phys_segs[segind];
538 #ifdef VM_PHYSSEG_SPARSE
539 seg->first_page = &vm_page_array[npages];
540 npages += atop(seg->end - seg->start);
542 seg->first_page = PHYS_TO_VM_PAGE(seg->start);
544 #ifdef VM_FREELIST_ISADMA
545 if (seg->end <= VM_ISADMA_BOUNDARY) {
546 flind = vm_freelist_to_flind[VM_FREELIST_ISADMA];
548 ("vm_phys_init: ISADMA flind < 0"));
551 #ifdef VM_FREELIST_LOWMEM
552 if (seg->end <= VM_LOWMEM_BOUNDARY) {
553 flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM];
555 ("vm_phys_init: LOWMEM flind < 0"));
558 #ifdef VM_FREELIST_DMA32
559 if (seg->end <= VM_DMA32_BOUNDARY) {
560 flind = vm_freelist_to_flind[VM_FREELIST_DMA32];
562 ("vm_phys_init: DMA32 flind < 0"));
566 flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT];
568 ("vm_phys_init: DEFAULT flind < 0"));
570 seg->free_queues = &vm_phys_free_queues[seg->domain][flind];
574 * Initialize the free queues.
576 for (dom = 0; dom < vm_ndomains; dom++) {
577 for (flind = 0; flind < vm_nfreelists; flind++) {
578 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
579 fl = vm_phys_free_queues[dom][flind][pind];
580 for (oind = 0; oind < VM_NFREEORDER; oind++)
581 TAILQ_INIT(&fl[oind].pl);
586 rw_init(&vm_phys_fictitious_reg_lock, "vmfctr");
590 * Split a contiguous, power of two-sized set of physical pages.
593 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
597 while (oind > order) {
599 m_buddy = &m[1 << oind];
600 KASSERT(m_buddy->order == VM_NFREEORDER,
601 ("vm_phys_split_pages: page %p has unexpected order %d",
602 m_buddy, m_buddy->order));
603 vm_freelist_add(fl, m_buddy, oind, 0);
608 * Allocate a contiguous, power of two-sized set of physical pages
609 * from the free lists.
611 * The free page queues must be locked.
614 vm_phys_alloc_pages(int domain, int pool, int order)
619 for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
620 m = vm_phys_alloc_freelist_pages(domain, freelist, pool, order);
628 vm_phys_alloc_npages(int domain, int pool, vm_page_t *mp, int cnt)
633 for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
634 for (order = fls(cnt) -1; order >= 0; order--) {
635 m = vm_phys_alloc_freelist_pages(domain, freelist,
648 * Allocate a contiguous, power of two-sized set of physical pages from the
649 * specified free list. The free list must be specified using one of the
650 * manifest constants VM_FREELIST_*.
652 * The free page queues must be locked.
655 vm_phys_alloc_freelist_pages(int domain, int freelist, int pool, int order)
657 struct vm_freelist *alt, *fl;
659 int oind, pind, flind;
661 KASSERT(domain >= 0 && domain < vm_ndomains,
662 ("vm_phys_alloc_freelist_pages: domain %d is out of range",
664 KASSERT(freelist < VM_NFREELIST,
665 ("vm_phys_alloc_freelist_pages: freelist %d is out of range",
667 KASSERT(pool < VM_NFREEPOOL,
668 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
669 KASSERT(order < VM_NFREEORDER,
670 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
672 flind = vm_freelist_to_flind[freelist];
673 /* Check if freelist is present */
677 vm_domain_free_assert_locked(VM_DOMAIN(domain));
678 fl = &vm_phys_free_queues[domain][flind][pool][0];
679 for (oind = order; oind < VM_NFREEORDER; oind++) {
680 m = TAILQ_FIRST(&fl[oind].pl);
682 vm_freelist_rem(fl, m, oind);
683 vm_phys_split_pages(m, oind, fl, order);
689 * The given pool was empty. Find the largest
690 * contiguous, power-of-two-sized set of pages in any
691 * pool. Transfer these pages to the given pool, and
692 * use them to satisfy the allocation.
694 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
695 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
696 alt = &vm_phys_free_queues[domain][flind][pind][0];
697 m = TAILQ_FIRST(&alt[oind].pl);
699 vm_freelist_rem(alt, m, oind);
700 vm_phys_set_pool(pool, m, oind);
701 vm_phys_split_pages(m, oind, fl, order);
710 * Find the vm_page corresponding to the given physical address.
713 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
715 struct vm_phys_seg *seg;
718 for (segind = 0; segind < vm_phys_nsegs; segind++) {
719 seg = &vm_phys_segs[segind];
720 if (pa >= seg->start && pa < seg->end)
721 return (&seg->first_page[atop(pa - seg->start)]);
727 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
729 struct vm_phys_fictitious_seg tmp, *seg;
736 rw_rlock(&vm_phys_fictitious_reg_lock);
737 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
738 rw_runlock(&vm_phys_fictitious_reg_lock);
742 m = &seg->first_page[atop(pa - seg->start)];
743 KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m));
749 vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start,
750 long page_count, vm_memattr_t memattr)
754 bzero(range, page_count * sizeof(*range));
755 for (i = 0; i < page_count; i++) {
756 vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr);
757 range[i].oflags &= ~VPO_UNMANAGED;
758 range[i].busy_lock = VPB_UNBUSIED;
763 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
764 vm_memattr_t memattr)
766 struct vm_phys_fictitious_seg *seg;
769 #ifdef VM_PHYSSEG_DENSE
775 ("Start of segment isn't less than end (start: %jx end: %jx)",
776 (uintmax_t)start, (uintmax_t)end));
778 page_count = (end - start) / PAGE_SIZE;
780 #ifdef VM_PHYSSEG_DENSE
783 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
784 fp = &vm_page_array[pi - first_page];
785 if ((pe - first_page) > vm_page_array_size) {
787 * We have a segment that starts inside
788 * of vm_page_array, but ends outside of it.
790 * Use vm_page_array pages for those that are
791 * inside of the vm_page_array range, and
792 * allocate the remaining ones.
794 dpage_count = vm_page_array_size - (pi - first_page);
795 vm_phys_fictitious_init_range(fp, start, dpage_count,
797 page_count -= dpage_count;
798 start += ptoa(dpage_count);
802 * We can allocate the full range from vm_page_array,
803 * so there's no need to register the range in the tree.
805 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
807 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
809 * We have a segment that ends inside of vm_page_array,
810 * but starts outside of it.
812 fp = &vm_page_array[0];
813 dpage_count = pe - first_page;
814 vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count,
816 end -= ptoa(dpage_count);
817 page_count -= dpage_count;
819 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
821 * Trying to register a fictitious range that expands before
822 * and after vm_page_array.
828 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
830 #ifdef VM_PHYSSEG_DENSE
833 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
835 seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO);
838 seg->first_page = fp;
840 rw_wlock(&vm_phys_fictitious_reg_lock);
841 RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg);
842 rw_wunlock(&vm_phys_fictitious_reg_lock);
848 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
850 struct vm_phys_fictitious_seg *seg, tmp;
851 #ifdef VM_PHYSSEG_DENSE
856 ("Start of segment isn't less than end (start: %jx end: %jx)",
857 (uintmax_t)start, (uintmax_t)end));
859 #ifdef VM_PHYSSEG_DENSE
862 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
863 if ((pe - first_page) <= vm_page_array_size) {
865 * This segment was allocated using vm_page_array
866 * only, there's nothing to do since those pages
867 * were never added to the tree.
872 * We have a segment that starts inside
873 * of vm_page_array, but ends outside of it.
875 * Calculate how many pages were added to the
876 * tree and free them.
878 start = ptoa(first_page + vm_page_array_size);
879 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
881 * We have a segment that ends inside of vm_page_array,
882 * but starts outside of it.
884 end = ptoa(first_page);
885 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
886 /* Since it's not possible to register such a range, panic. */
888 "Unregistering not registered fictitious range [%#jx:%#jx]",
889 (uintmax_t)start, (uintmax_t)end);
895 rw_wlock(&vm_phys_fictitious_reg_lock);
896 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
897 if (seg->start != start || seg->end != end) {
898 rw_wunlock(&vm_phys_fictitious_reg_lock);
900 "Unregistering not registered fictitious range [%#jx:%#jx]",
901 (uintmax_t)start, (uintmax_t)end);
903 RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg);
904 rw_wunlock(&vm_phys_fictitious_reg_lock);
905 free(seg->first_page, M_FICT_PAGES);
906 free(seg, M_FICT_PAGES);
910 * Free a contiguous, power of two-sized set of physical pages.
912 * The free page queues must be locked.
915 vm_phys_free_pages(vm_page_t m, int order)
917 struct vm_freelist *fl;
918 struct vm_phys_seg *seg;
922 KASSERT(m->order == VM_NFREEORDER,
923 ("vm_phys_free_pages: page %p has unexpected order %d",
925 KASSERT(m->pool < VM_NFREEPOOL,
926 ("vm_phys_free_pages: page %p has unexpected pool %d",
928 KASSERT(order < VM_NFREEORDER,
929 ("vm_phys_free_pages: order %d is out of range", order));
930 seg = &vm_phys_segs[m->segind];
931 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
932 if (order < VM_NFREEORDER - 1) {
933 pa = VM_PAGE_TO_PHYS(m);
935 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
936 if (pa < seg->start || pa >= seg->end)
938 m_buddy = &seg->first_page[atop(pa - seg->start)];
939 if (m_buddy->order != order)
941 fl = (*seg->free_queues)[m_buddy->pool];
942 vm_freelist_rem(fl, m_buddy, order);
943 if (m_buddy->pool != m->pool)
944 vm_phys_set_pool(m->pool, m_buddy, order);
946 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
947 m = &seg->first_page[atop(pa - seg->start)];
948 } while (order < VM_NFREEORDER - 1);
950 fl = (*seg->free_queues)[m->pool];
951 vm_freelist_add(fl, m, order, 1);
955 * Free a contiguous, arbitrarily sized set of physical pages.
957 * The free page queues must be locked.
960 vm_phys_free_contig(vm_page_t m, u_long npages)
966 * Avoid unnecessary coalescing by freeing the pages in the largest
967 * possible power-of-two-sized subsets.
969 vm_domain_free_assert_locked(vm_pagequeue_domain(m));
970 for (;; npages -= n) {
972 * Unsigned "min" is used here so that "order" is assigned
973 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
974 * or the low-order bits of its physical address are zero
975 * because the size of a physical address exceeds the size of
978 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
983 vm_phys_free_pages(m, order);
986 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
987 for (; npages > 0; npages -= n) {
988 order = flsl(npages) - 1;
990 vm_phys_free_pages(m, order);
996 * Scan physical memory between the specified addresses "low" and "high" for a
997 * run of contiguous physical pages that satisfy the specified conditions, and
998 * return the lowest page in the run. The specified "alignment" determines
999 * the alignment of the lowest physical page in the run. If the specified
1000 * "boundary" is non-zero, then the run of physical pages cannot span a
1001 * physical address that is a multiple of "boundary".
1003 * "npages" must be greater than zero. Both "alignment" and "boundary" must
1004 * be a power of two.
1007 vm_phys_scan_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1008 u_long alignment, vm_paddr_t boundary, int options)
1011 vm_page_t m_end, m_run, m_start;
1012 struct vm_phys_seg *seg;
1015 KASSERT(npages > 0, ("npages is 0"));
1016 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1017 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1020 for (segind = 0; segind < vm_phys_nsegs; segind++) {
1021 seg = &vm_phys_segs[segind];
1022 if (seg->domain != domain)
1024 if (seg->start >= high)
1026 if (low >= seg->end)
1028 if (low <= seg->start)
1029 m_start = seg->first_page;
1031 m_start = &seg->first_page[atop(low - seg->start)];
1032 if (high < seg->end)
1036 if (pa_end - VM_PAGE_TO_PHYS(m_start) < ptoa(npages))
1038 m_end = &seg->first_page[atop(pa_end - seg->start)];
1039 m_run = vm_page_scan_contig(npages, m_start, m_end,
1040 alignment, boundary, options);
1048 * Set the pool for a contiguous, power of two-sized set of physical pages.
1051 vm_phys_set_pool(int pool, vm_page_t m, int order)
1055 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
1060 * Search for the given physical page "m" in the free lists. If the search
1061 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
1062 * FALSE, indicating that "m" is not in the free lists.
1064 * The free page queues must be locked.
1067 vm_phys_unfree_page(vm_page_t m)
1069 struct vm_freelist *fl;
1070 struct vm_phys_seg *seg;
1071 vm_paddr_t pa, pa_half;
1072 vm_page_t m_set, m_tmp;
1076 * First, find the contiguous, power of two-sized set of free
1077 * physical pages containing the given physical page "m" and
1078 * assign it to "m_set".
1080 seg = &vm_phys_segs[m->segind];
1081 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1082 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
1083 order < VM_NFREEORDER - 1; ) {
1085 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
1086 if (pa >= seg->start)
1087 m_set = &seg->first_page[atop(pa - seg->start)];
1091 if (m_set->order < order)
1093 if (m_set->order == VM_NFREEORDER)
1095 KASSERT(m_set->order < VM_NFREEORDER,
1096 ("vm_phys_unfree_page: page %p has unexpected order %d",
1097 m_set, m_set->order));
1100 * Next, remove "m_set" from the free lists. Finally, extract
1101 * "m" from "m_set" using an iterative algorithm: While "m_set"
1102 * is larger than a page, shrink "m_set" by returning the half
1103 * of "m_set" that does not contain "m" to the free lists.
1105 fl = (*seg->free_queues)[m_set->pool];
1106 order = m_set->order;
1107 vm_freelist_rem(fl, m_set, order);
1110 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
1111 if (m->phys_addr < pa_half)
1112 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
1115 m_set = &seg->first_page[atop(pa_half - seg->start)];
1117 vm_freelist_add(fl, m_tmp, order, 0);
1119 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
1124 * Allocate a contiguous set of physical pages of the given size
1125 * "npages" from the free lists. All of the physical pages must be at
1126 * or above the given physical address "low" and below the given
1127 * physical address "high". The given value "alignment" determines the
1128 * alignment of the first physical page in the set. If the given value
1129 * "boundary" is non-zero, then the set of physical pages cannot cross
1130 * any physical address boundary that is a multiple of that value. Both
1131 * "alignment" and "boundary" must be a power of two.
1134 vm_phys_alloc_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1135 u_long alignment, vm_paddr_t boundary)
1137 vm_paddr_t pa_end, pa_start;
1139 struct vm_phys_seg *seg;
1142 KASSERT(npages > 0, ("npages is 0"));
1143 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1144 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1145 vm_domain_free_assert_locked(VM_DOMAIN(domain));
1149 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
1150 seg = &vm_phys_segs[segind];
1151 if (seg->start >= high || seg->domain != domain)
1153 if (low >= seg->end)
1155 if (low <= seg->start)
1156 pa_start = seg->start;
1159 if (high < seg->end)
1163 if (pa_end - pa_start < ptoa(npages))
1165 m_run = vm_phys_alloc_seg_contig(seg, npages, low, high,
1166 alignment, boundary);
1174 * Allocate a run of contiguous physical pages from the free list for the
1175 * specified segment.
1178 vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, u_long npages,
1179 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1181 struct vm_freelist *fl;
1182 vm_paddr_t pa, pa_end, size;
1185 int oind, order, pind;
1187 KASSERT(npages > 0, ("npages is 0"));
1188 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1189 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1190 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1191 /* Compute the queue that is the best fit for npages. */
1192 for (order = 0; (1 << order) < npages; order++);
1193 /* Search for a run satisfying the specified conditions. */
1194 size = npages << PAGE_SHIFT;
1195 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER;
1197 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1198 fl = (*seg->free_queues)[pind];
1199 TAILQ_FOREACH(m_ret, &fl[oind].pl, listq) {
1201 * Is the size of this allocation request
1202 * larger than the largest block size?
1204 if (order >= VM_NFREEORDER) {
1206 * Determine if a sufficient number of
1207 * subsequent blocks to satisfy the
1208 * allocation request are free.
1210 pa = VM_PAGE_TO_PHYS(m_ret);
1215 pa += 1 << (PAGE_SHIFT +
1221 m = &seg->first_page[atop(pa -
1223 if (m->order != VM_NFREEORDER -
1227 /* If not, go to the next block. */
1233 * Determine if the blocks are within the
1234 * given range, satisfy the given alignment,
1235 * and do not cross the given boundary.
1237 pa = VM_PAGE_TO_PHYS(m_ret);
1239 if (pa >= low && pa_end <= high &&
1240 (pa & (alignment - 1)) == 0 &&
1241 rounddown2(pa ^ (pa_end - 1), boundary) == 0)
1248 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
1249 fl = (*seg->free_queues)[m->pool];
1250 vm_freelist_rem(fl, m, m->order);
1252 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
1253 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
1254 fl = (*seg->free_queues)[m_ret->pool];
1255 vm_phys_split_pages(m_ret, oind, fl, order);
1256 /* Return excess pages to the free lists. */
1257 npages_end = roundup2(npages, 1 << imin(oind, order));
1258 if (npages < npages_end)
1259 vm_phys_free_contig(&m_ret[npages], npages_end - npages);
1265 * Show the number of physical pages in each of the free lists.
1267 DB_SHOW_COMMAND(freepages, db_show_freepages)
1269 struct vm_freelist *fl;
1270 int flind, oind, pind, dom;
1272 for (dom = 0; dom < vm_ndomains; dom++) {
1273 db_printf("DOMAIN: %d\n", dom);
1274 for (flind = 0; flind < vm_nfreelists; flind++) {
1275 db_printf("FREE LIST %d:\n"
1276 "\n ORDER (SIZE) | NUMBER"
1278 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1279 db_printf(" | POOL %d", pind);
1281 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1282 db_printf("-- -- ");
1284 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
1285 db_printf(" %2.2d (%6.6dK)", oind,
1286 1 << (PAGE_SHIFT - 10 + oind));
1287 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1288 fl = vm_phys_free_queues[dom][flind][pind];
1289 db_printf(" | %6.6d", fl[oind].lcnt);