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>
71 #include <vm/vm_domain.h>
73 _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX,
74 "Too many physsegs.");
77 struct mem_affinity *mem_affinity;
83 struct vm_phys_seg vm_phys_segs[VM_PHYSSEG_MAX];
86 struct vm_phys_fictitious_seg;
87 static int vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *,
88 struct vm_phys_fictitious_seg *);
90 RB_HEAD(fict_tree, vm_phys_fictitious_seg) vm_phys_fictitious_tree =
91 RB_INITIALIZER(_vm_phys_fictitious_tree);
93 struct vm_phys_fictitious_seg {
94 RB_ENTRY(vm_phys_fictitious_seg) node;
95 /* Memory region data */
101 RB_GENERATE_STATIC(fict_tree, vm_phys_fictitious_seg, node,
102 vm_phys_fictitious_cmp);
104 static struct rwlock vm_phys_fictitious_reg_lock;
105 MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages");
107 static struct vm_freelist
108 vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL][VM_NFREEORDER];
110 static int vm_nfreelists;
113 * Provides the mapping from VM_FREELIST_* to free list indices (flind).
115 static int vm_freelist_to_flind[VM_NFREELIST];
117 CTASSERT(VM_FREELIST_DEFAULT == 0);
119 #ifdef VM_FREELIST_ISADMA
120 #define VM_ISADMA_BOUNDARY 16777216
122 #ifdef VM_FREELIST_DMA32
123 #define VM_DMA32_BOUNDARY ((vm_paddr_t)1 << 32)
127 * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about
128 * the ordering of the free list boundaries.
130 #if defined(VM_ISADMA_BOUNDARY) && defined(VM_LOWMEM_BOUNDARY)
131 CTASSERT(VM_ISADMA_BOUNDARY < VM_LOWMEM_BOUNDARY);
133 #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY)
134 CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY);
137 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
138 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
139 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
141 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
142 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
143 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
146 static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS);
147 SYSCTL_OID(_vm, OID_AUTO, phys_locality, CTLTYPE_STRING | CTLFLAG_RD,
148 NULL, 0, sysctl_vm_phys_locality, "A", "Phys Locality Info");
151 SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
152 &vm_ndomains, 0, "Number of physical memory domains available.");
154 static vm_page_t vm_phys_alloc_seg_contig(struct vm_phys_seg *seg,
155 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
156 vm_paddr_t boundary);
157 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain);
158 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end);
159 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
163 * Red-black tree helpers for vm fictitious range management.
166 vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p,
167 struct vm_phys_fictitious_seg *range)
170 KASSERT(range->start != 0 && range->end != 0,
171 ("Invalid range passed on search for vm_fictitious page"));
172 if (p->start >= range->end)
174 if (p->start < range->start)
181 vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1,
182 struct vm_phys_fictitious_seg *p2)
185 /* Check if this is a search for a page */
187 return (vm_phys_fictitious_in_range(p1, p2));
189 KASSERT(p2->end != 0,
190 ("Invalid range passed as second parameter to vm fictitious comparison"));
192 /* Searching to add a new range */
193 if (p1->end <= p2->start)
195 if (p1->start >= p2->end)
198 panic("Trying to add overlapping vm fictitious ranges:\n"
199 "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start,
200 (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end);
204 vm_phys_domain_intersects(long mask, vm_paddr_t low, vm_paddr_t high)
206 struct vm_phys_seg *s;
209 while ((idx = ffsl(mask)) != 0) {
210 idx--; /* ffsl counts from 1 */
211 mask &= ~(1UL << idx);
212 s = &vm_phys_segs[idx];
213 if (low < s->end && high > s->start)
220 * Outputs the state of the physical memory allocator, specifically,
221 * the amount of physical memory in each free list.
224 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
227 struct vm_freelist *fl;
228 int dom, error, flind, oind, pind;
230 error = sysctl_wire_old_buffer(req, 0);
233 sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req);
234 for (dom = 0; dom < vm_ndomains; dom++) {
235 sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom);
236 for (flind = 0; flind < vm_nfreelists; flind++) {
237 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
238 "\n ORDER (SIZE) | NUMBER"
240 for (pind = 0; pind < VM_NFREEPOOL; pind++)
241 sbuf_printf(&sbuf, " | POOL %d", pind);
242 sbuf_printf(&sbuf, "\n-- ");
243 for (pind = 0; pind < VM_NFREEPOOL; pind++)
244 sbuf_printf(&sbuf, "-- -- ");
245 sbuf_printf(&sbuf, "--\n");
246 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
247 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
248 1 << (PAGE_SHIFT - 10 + oind));
249 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
250 fl = vm_phys_free_queues[dom][flind][pind];
251 sbuf_printf(&sbuf, " | %6d",
254 sbuf_printf(&sbuf, "\n");
258 error = sbuf_finish(&sbuf);
264 * Outputs the set of physical memory segments.
267 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
270 struct vm_phys_seg *seg;
273 error = sysctl_wire_old_buffer(req, 0);
276 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
277 for (segind = 0; segind < vm_phys_nsegs; segind++) {
278 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
279 seg = &vm_phys_segs[segind];
280 sbuf_printf(&sbuf, "start: %#jx\n",
281 (uintmax_t)seg->start);
282 sbuf_printf(&sbuf, "end: %#jx\n",
283 (uintmax_t)seg->end);
284 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
285 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
287 error = sbuf_finish(&sbuf);
293 * Return affinity, or -1 if there's no affinity information.
296 vm_phys_mem_affinity(int f, int t)
300 if (mem_locality == NULL)
302 if (f >= vm_ndomains || t >= vm_ndomains)
304 return (mem_locality[f * vm_ndomains + t]);
312 * Outputs the VM locality table.
315 sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS)
320 error = sysctl_wire_old_buffer(req, 0);
323 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
325 sbuf_printf(&sbuf, "\n");
327 for (i = 0; i < vm_ndomains; i++) {
328 sbuf_printf(&sbuf, "%d: ", i);
329 for (j = 0; j < vm_ndomains; j++) {
330 sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j));
332 sbuf_printf(&sbuf, "\n");
334 error = sbuf_finish(&sbuf);
341 vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
346 TAILQ_INSERT_TAIL(&fl[order].pl, m, plinks.q);
348 TAILQ_INSERT_HEAD(&fl[order].pl, m, plinks.q);
353 vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
356 TAILQ_REMOVE(&fl[order].pl, m, plinks.q);
358 m->order = VM_NFREEORDER;
362 * Create a physical memory segment.
365 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain)
367 struct vm_phys_seg *seg;
369 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
370 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
371 KASSERT(domain >= 0 && domain < vm_ndomains,
372 ("vm_phys_create_seg: invalid domain provided"));
373 seg = &vm_phys_segs[vm_phys_nsegs++];
374 while (seg > vm_phys_segs && (seg - 1)->start >= end) {
380 seg->domain = domain;
384 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end)
389 if (mem_affinity == NULL) {
390 _vm_phys_create_seg(start, end, 0);
395 if (mem_affinity[i].end == 0)
396 panic("Reached end of affinity info");
397 if (mem_affinity[i].end <= start)
399 if (mem_affinity[i].start > start)
400 panic("No affinity info for start %jx",
402 if (mem_affinity[i].end >= end) {
403 _vm_phys_create_seg(start, end,
404 mem_affinity[i].domain);
407 _vm_phys_create_seg(start, mem_affinity[i].end,
408 mem_affinity[i].domain);
409 start = mem_affinity[i].end;
412 _vm_phys_create_seg(start, end, 0);
417 * Add a physical memory segment.
420 vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
424 KASSERT((start & PAGE_MASK) == 0,
425 ("vm_phys_define_seg: start is not page aligned"));
426 KASSERT((end & PAGE_MASK) == 0,
427 ("vm_phys_define_seg: end is not page aligned"));
430 * Split the physical memory segment if it spans two or more free
434 #ifdef VM_FREELIST_ISADMA
435 if (paddr < VM_ISADMA_BOUNDARY && end > VM_ISADMA_BOUNDARY) {
436 vm_phys_create_seg(paddr, VM_ISADMA_BOUNDARY);
437 paddr = VM_ISADMA_BOUNDARY;
440 #ifdef VM_FREELIST_LOWMEM
441 if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) {
442 vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY);
443 paddr = VM_LOWMEM_BOUNDARY;
446 #ifdef VM_FREELIST_DMA32
447 if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) {
448 vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY);
449 paddr = VM_DMA32_BOUNDARY;
452 vm_phys_create_seg(paddr, end);
456 * Initialize the physical memory allocator.
458 * Requires that vm_page_array is initialized!
463 struct vm_freelist *fl;
464 struct vm_phys_seg *seg;
466 int dom, flind, freelist, oind, pind, segind;
469 * Compute the number of free lists, and generate the mapping from the
470 * manifest constants VM_FREELIST_* to the free list indices.
472 * Initially, the entries of vm_freelist_to_flind[] are set to either
473 * 0 or 1 to indicate which free lists should be created.
476 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
477 seg = &vm_phys_segs[segind];
478 #ifdef VM_FREELIST_ISADMA
479 if (seg->end <= VM_ISADMA_BOUNDARY)
480 vm_freelist_to_flind[VM_FREELIST_ISADMA] = 1;
483 #ifdef VM_FREELIST_LOWMEM
484 if (seg->end <= VM_LOWMEM_BOUNDARY)
485 vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1;
488 #ifdef VM_FREELIST_DMA32
490 #ifdef VM_DMA32_NPAGES_THRESHOLD
492 * Create the DMA32 free list only if the amount of
493 * physical memory above physical address 4G exceeds the
496 npages > VM_DMA32_NPAGES_THRESHOLD &&
498 seg->end <= VM_DMA32_BOUNDARY)
499 vm_freelist_to_flind[VM_FREELIST_DMA32] = 1;
503 npages += atop(seg->end - seg->start);
504 vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1;
507 /* Change each entry into a running total of the free lists. */
508 for (freelist = 1; freelist < VM_NFREELIST; freelist++) {
509 vm_freelist_to_flind[freelist] +=
510 vm_freelist_to_flind[freelist - 1];
512 vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1];
513 KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists"));
514 /* Change each entry into a free list index. */
515 for (freelist = 0; freelist < VM_NFREELIST; freelist++)
516 vm_freelist_to_flind[freelist]--;
519 * Initialize the first_page and free_queues fields of each physical
522 #ifdef VM_PHYSSEG_SPARSE
525 for (segind = 0; segind < vm_phys_nsegs; segind++) {
526 seg = &vm_phys_segs[segind];
527 #ifdef VM_PHYSSEG_SPARSE
528 seg->first_page = &vm_page_array[npages];
529 npages += atop(seg->end - seg->start);
531 seg->first_page = PHYS_TO_VM_PAGE(seg->start);
533 #ifdef VM_FREELIST_ISADMA
534 if (seg->end <= VM_ISADMA_BOUNDARY) {
535 flind = vm_freelist_to_flind[VM_FREELIST_ISADMA];
537 ("vm_phys_init: ISADMA flind < 0"));
540 #ifdef VM_FREELIST_LOWMEM
541 if (seg->end <= VM_LOWMEM_BOUNDARY) {
542 flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM];
544 ("vm_phys_init: LOWMEM flind < 0"));
547 #ifdef VM_FREELIST_DMA32
548 if (seg->end <= VM_DMA32_BOUNDARY) {
549 flind = vm_freelist_to_flind[VM_FREELIST_DMA32];
551 ("vm_phys_init: DMA32 flind < 0"));
555 flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT];
557 ("vm_phys_init: DEFAULT flind < 0"));
559 seg->free_queues = &vm_phys_free_queues[seg->domain][flind];
563 * Initialize the free queues.
565 for (dom = 0; dom < vm_ndomains; dom++) {
566 for (flind = 0; flind < vm_nfreelists; flind++) {
567 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
568 fl = vm_phys_free_queues[dom][flind][pind];
569 for (oind = 0; oind < VM_NFREEORDER; oind++)
570 TAILQ_INIT(&fl[oind].pl);
575 rw_init(&vm_phys_fictitious_reg_lock, "vmfctr");
579 * Split a contiguous, power of two-sized set of physical pages.
582 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
586 while (oind > order) {
588 m_buddy = &m[1 << oind];
589 KASSERT(m_buddy->order == VM_NFREEORDER,
590 ("vm_phys_split_pages: page %p has unexpected order %d",
591 m_buddy, m_buddy->order));
592 vm_freelist_add(fl, m_buddy, oind, 0);
597 * Allocate a contiguous, power of two-sized set of physical pages
598 * from the free lists.
600 * The free page queues must be locked.
603 vm_phys_alloc_pages(int domain, int pool, int order)
608 for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
609 m = vm_phys_alloc_freelist_pages(domain, freelist, pool, order);
617 * Allocate a contiguous, power of two-sized set of physical pages from the
618 * specified free list. The free list must be specified using one of the
619 * manifest constants VM_FREELIST_*.
621 * The free page queues must be locked.
624 vm_phys_alloc_freelist_pages(int domain, int freelist, int pool, int order)
626 struct vm_freelist *alt, *fl;
628 int oind, pind, flind;
630 KASSERT(domain >= 0 && domain < vm_ndomains,
631 ("vm_phys_alloc_freelist_pages: domain %d is out of range",
633 KASSERT(freelist < VM_NFREELIST,
634 ("vm_phys_alloc_freelist_pages: freelist %d is out of range",
636 KASSERT(pool < VM_NFREEPOOL,
637 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
638 KASSERT(order < VM_NFREEORDER,
639 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
641 flind = vm_freelist_to_flind[freelist];
642 /* Check if freelist is present */
646 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
647 fl = &vm_phys_free_queues[domain][flind][pool][0];
648 for (oind = order; oind < VM_NFREEORDER; oind++) {
649 m = TAILQ_FIRST(&fl[oind].pl);
651 vm_freelist_rem(fl, m, oind);
652 vm_phys_split_pages(m, oind, fl, order);
658 * The given pool was empty. Find the largest
659 * contiguous, power-of-two-sized set of pages in any
660 * pool. Transfer these pages to the given pool, and
661 * use them to satisfy the allocation.
663 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
664 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
665 alt = &vm_phys_free_queues[domain][flind][pind][0];
666 m = TAILQ_FIRST(&alt[oind].pl);
668 vm_freelist_rem(alt, m, oind);
669 vm_phys_set_pool(pool, m, oind);
670 vm_phys_split_pages(m, oind, fl, order);
679 * Find the vm_page corresponding to the given physical address.
682 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
684 struct vm_phys_seg *seg;
687 for (segind = 0; segind < vm_phys_nsegs; segind++) {
688 seg = &vm_phys_segs[segind];
689 if (pa >= seg->start && pa < seg->end)
690 return (&seg->first_page[atop(pa - seg->start)]);
696 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
698 struct vm_phys_fictitious_seg tmp, *seg;
705 rw_rlock(&vm_phys_fictitious_reg_lock);
706 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
707 rw_runlock(&vm_phys_fictitious_reg_lock);
711 m = &seg->first_page[atop(pa - seg->start)];
712 KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m));
718 vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start,
719 long page_count, vm_memattr_t memattr)
723 bzero(range, page_count * sizeof(*range));
724 for (i = 0; i < page_count; i++) {
725 vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr);
726 range[i].oflags &= ~VPO_UNMANAGED;
727 range[i].busy_lock = VPB_UNBUSIED;
732 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
733 vm_memattr_t memattr)
735 struct vm_phys_fictitious_seg *seg;
738 #ifdef VM_PHYSSEG_DENSE
744 ("Start of segment isn't less than end (start: %jx end: %jx)",
745 (uintmax_t)start, (uintmax_t)end));
747 page_count = (end - start) / PAGE_SIZE;
749 #ifdef VM_PHYSSEG_DENSE
752 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
753 fp = &vm_page_array[pi - first_page];
754 if ((pe - first_page) > vm_page_array_size) {
756 * We have a segment that starts inside
757 * of vm_page_array, but ends outside of it.
759 * Use vm_page_array pages for those that are
760 * inside of the vm_page_array range, and
761 * allocate the remaining ones.
763 dpage_count = vm_page_array_size - (pi - first_page);
764 vm_phys_fictitious_init_range(fp, start, dpage_count,
766 page_count -= dpage_count;
767 start += ptoa(dpage_count);
771 * We can allocate the full range from vm_page_array,
772 * so there's no need to register the range in the tree.
774 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
776 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
778 * We have a segment that ends inside of vm_page_array,
779 * but starts outside of it.
781 fp = &vm_page_array[0];
782 dpage_count = pe - first_page;
783 vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count,
785 end -= ptoa(dpage_count);
786 page_count -= dpage_count;
788 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
790 * Trying to register a fictitious range that expands before
791 * and after vm_page_array.
797 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
799 #ifdef VM_PHYSSEG_DENSE
802 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
804 seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO);
807 seg->first_page = fp;
809 rw_wlock(&vm_phys_fictitious_reg_lock);
810 RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg);
811 rw_wunlock(&vm_phys_fictitious_reg_lock);
817 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
819 struct vm_phys_fictitious_seg *seg, tmp;
820 #ifdef VM_PHYSSEG_DENSE
825 ("Start of segment isn't less than end (start: %jx end: %jx)",
826 (uintmax_t)start, (uintmax_t)end));
828 #ifdef VM_PHYSSEG_DENSE
831 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
832 if ((pe - first_page) <= vm_page_array_size) {
834 * This segment was allocated using vm_page_array
835 * only, there's nothing to do since those pages
836 * were never added to the tree.
841 * We have a segment that starts inside
842 * of vm_page_array, but ends outside of it.
844 * Calculate how many pages were added to the
845 * tree and free them.
847 start = ptoa(first_page + vm_page_array_size);
848 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
850 * We have a segment that ends inside of vm_page_array,
851 * but starts outside of it.
853 end = ptoa(first_page);
854 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
855 /* Since it's not possible to register such a range, panic. */
857 "Unregistering not registered fictitious range [%#jx:%#jx]",
858 (uintmax_t)start, (uintmax_t)end);
864 rw_wlock(&vm_phys_fictitious_reg_lock);
865 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
866 if (seg->start != start || seg->end != end) {
867 rw_wunlock(&vm_phys_fictitious_reg_lock);
869 "Unregistering not registered fictitious range [%#jx:%#jx]",
870 (uintmax_t)start, (uintmax_t)end);
872 RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg);
873 rw_wunlock(&vm_phys_fictitious_reg_lock);
874 free(seg->first_page, M_FICT_PAGES);
875 free(seg, M_FICT_PAGES);
879 * Free a contiguous, power of two-sized set of physical pages.
881 * The free page queues must be locked.
884 vm_phys_free_pages(vm_page_t m, int order)
886 struct vm_freelist *fl;
887 struct vm_phys_seg *seg;
891 KASSERT(m->order == VM_NFREEORDER,
892 ("vm_phys_free_pages: page %p has unexpected order %d",
894 KASSERT(m->pool < VM_NFREEPOOL,
895 ("vm_phys_free_pages: page %p has unexpected pool %d",
897 KASSERT(order < VM_NFREEORDER,
898 ("vm_phys_free_pages: order %d is out of range", order));
899 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
900 seg = &vm_phys_segs[m->segind];
901 if (order < VM_NFREEORDER - 1) {
902 pa = VM_PAGE_TO_PHYS(m);
904 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
905 if (pa < seg->start || pa >= seg->end)
907 m_buddy = &seg->first_page[atop(pa - seg->start)];
908 if (m_buddy->order != order)
910 fl = (*seg->free_queues)[m_buddy->pool];
911 vm_freelist_rem(fl, m_buddy, order);
912 if (m_buddy->pool != m->pool)
913 vm_phys_set_pool(m->pool, m_buddy, order);
915 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
916 m = &seg->first_page[atop(pa - seg->start)];
917 } while (order < VM_NFREEORDER - 1);
919 fl = (*seg->free_queues)[m->pool];
920 vm_freelist_add(fl, m, order, 1);
924 * Free a contiguous, arbitrarily sized set of physical pages.
926 * The free page queues must be locked.
929 vm_phys_free_contig(vm_page_t m, u_long npages)
935 * Avoid unnecessary coalescing by freeing the pages in the largest
936 * possible power-of-two-sized subsets.
938 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
939 for (;; npages -= n) {
941 * Unsigned "min" is used here so that "order" is assigned
942 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
943 * or the low-order bits of its physical address are zero
944 * because the size of a physical address exceeds the size of
947 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
952 vm_phys_free_pages(m, order);
955 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
956 for (; npages > 0; npages -= n) {
957 order = flsl(npages) - 1;
959 vm_phys_free_pages(m, order);
965 * Scan physical memory between the specified addresses "low" and "high" for a
966 * run of contiguous physical pages that satisfy the specified conditions, and
967 * return the lowest page in the run. The specified "alignment" determines
968 * the alignment of the lowest physical page in the run. If the specified
969 * "boundary" is non-zero, then the run of physical pages cannot span a
970 * physical address that is a multiple of "boundary".
972 * "npages" must be greater than zero. Both "alignment" and "boundary" must
976 vm_phys_scan_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
977 u_long alignment, vm_paddr_t boundary, int options)
980 vm_page_t m_end, m_run, m_start;
981 struct vm_phys_seg *seg;
984 KASSERT(npages > 0, ("npages is 0"));
985 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
986 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
989 for (segind = 0; segind < vm_phys_nsegs; segind++) {
990 seg = &vm_phys_segs[segind];
991 if (seg->start >= high)
995 if (low <= seg->start)
996 m_start = seg->first_page;
998 m_start = &seg->first_page[atop(low - seg->start)];
1003 if (pa_end - VM_PAGE_TO_PHYS(m_start) < ptoa(npages))
1005 m_end = &seg->first_page[atop(pa_end - seg->start)];
1006 m_run = vm_page_scan_contig(npages, m_start, m_end,
1007 alignment, boundary, options);
1015 * Set the pool for a contiguous, power of two-sized set of physical pages.
1018 vm_phys_set_pool(int pool, vm_page_t m, int order)
1022 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
1027 * Search for the given physical page "m" in the free lists. If the search
1028 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
1029 * FALSE, indicating that "m" is not in the free lists.
1031 * The free page queues must be locked.
1034 vm_phys_unfree_page(vm_page_t m)
1036 struct vm_freelist *fl;
1037 struct vm_phys_seg *seg;
1038 vm_paddr_t pa, pa_half;
1039 vm_page_t m_set, m_tmp;
1042 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1045 * First, find the contiguous, power of two-sized set of free
1046 * physical pages containing the given physical page "m" and
1047 * assign it to "m_set".
1049 seg = &vm_phys_segs[m->segind];
1050 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
1051 order < VM_NFREEORDER - 1; ) {
1053 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
1054 if (pa >= seg->start)
1055 m_set = &seg->first_page[atop(pa - seg->start)];
1059 if (m_set->order < order)
1061 if (m_set->order == VM_NFREEORDER)
1063 KASSERT(m_set->order < VM_NFREEORDER,
1064 ("vm_phys_unfree_page: page %p has unexpected order %d",
1065 m_set, m_set->order));
1068 * Next, remove "m_set" from the free lists. Finally, extract
1069 * "m" from "m_set" using an iterative algorithm: While "m_set"
1070 * is larger than a page, shrink "m_set" by returning the half
1071 * of "m_set" that does not contain "m" to the free lists.
1073 fl = (*seg->free_queues)[m_set->pool];
1074 order = m_set->order;
1075 vm_freelist_rem(fl, m_set, order);
1078 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
1079 if (m->phys_addr < pa_half)
1080 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
1083 m_set = &seg->first_page[atop(pa_half - seg->start)];
1085 vm_freelist_add(fl, m_tmp, order, 0);
1087 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
1092 * Allocate a contiguous set of physical pages of the given size
1093 * "npages" from the free lists. All of the physical pages must be at
1094 * or above the given physical address "low" and below the given
1095 * physical address "high". The given value "alignment" determines the
1096 * alignment of the first physical page in the set. If the given value
1097 * "boundary" is non-zero, then the set of physical pages cannot cross
1098 * any physical address boundary that is a multiple of that value. Both
1099 * "alignment" and "boundary" must be a power of two.
1102 vm_phys_alloc_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1103 u_long alignment, vm_paddr_t boundary)
1105 vm_paddr_t pa_end, pa_start;
1107 struct vm_phys_seg *seg;
1110 KASSERT(npages > 0, ("npages is 0"));
1111 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1112 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1113 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1117 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
1118 seg = &vm_phys_segs[segind];
1119 if (seg->start >= high || seg->domain != domain)
1121 if (low >= seg->end)
1123 if (low <= seg->start)
1124 pa_start = seg->start;
1127 if (high < seg->end)
1131 if (pa_end - pa_start < ptoa(npages))
1133 m_run = vm_phys_alloc_seg_contig(seg, npages, low, high,
1134 alignment, boundary);
1142 * Allocate a run of contiguous physical pages from the free list for the
1143 * specified segment.
1146 vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, u_long npages,
1147 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1149 struct vm_freelist *fl;
1150 vm_paddr_t pa, pa_end, size;
1153 int oind, order, pind;
1155 KASSERT(npages > 0, ("npages is 0"));
1156 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1157 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1158 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1159 /* Compute the queue that is the best fit for npages. */
1160 for (order = 0; (1 << order) < npages; order++);
1161 /* Search for a run satisfying the specified conditions. */
1162 size = npages << PAGE_SHIFT;
1163 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER;
1165 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1166 fl = (*seg->free_queues)[pind];
1167 TAILQ_FOREACH(m_ret, &fl[oind].pl, plinks.q) {
1169 * Is the size of this allocation request
1170 * larger than the largest block size?
1172 if (order >= VM_NFREEORDER) {
1174 * Determine if a sufficient number of
1175 * subsequent blocks to satisfy the
1176 * allocation request are free.
1178 pa = VM_PAGE_TO_PHYS(m_ret);
1181 pa += 1 << (PAGE_SHIFT +
1187 m = &seg->first_page[atop(pa -
1189 if (m->order != VM_NFREEORDER -
1193 /* If not, go to the next block. */
1199 * Determine if the blocks are within the
1200 * given range, satisfy the given alignment,
1201 * and do not cross the given boundary.
1203 pa = VM_PAGE_TO_PHYS(m_ret);
1205 if (pa >= low && pa_end <= high &&
1206 (pa & (alignment - 1)) == 0 &&
1207 rounddown2(pa ^ (pa_end - 1), boundary) == 0)
1214 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
1215 fl = (*seg->free_queues)[m->pool];
1216 vm_freelist_rem(fl, m, m->order);
1218 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
1219 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
1220 fl = (*seg->free_queues)[m_ret->pool];
1221 vm_phys_split_pages(m_ret, oind, fl, order);
1222 /* Return excess pages to the free lists. */
1223 npages_end = roundup2(npages, 1 << imin(oind, order));
1224 if (npages < npages_end)
1225 vm_phys_free_contig(&m_ret[npages], npages_end - npages);
1231 * Show the number of physical pages in each of the free lists.
1233 DB_SHOW_COMMAND(freepages, db_show_freepages)
1235 struct vm_freelist *fl;
1236 int flind, oind, pind, dom;
1238 for (dom = 0; dom < vm_ndomains; dom++) {
1239 db_printf("DOMAIN: %d\n", dom);
1240 for (flind = 0; flind < vm_nfreelists; flind++) {
1241 db_printf("FREE LIST %d:\n"
1242 "\n ORDER (SIZE) | NUMBER"
1244 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1245 db_printf(" | POOL %d", pind);
1247 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1248 db_printf("-- -- ");
1250 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
1251 db_printf(" %2.2d (%6.6dK)", oind,
1252 1 << (PAGE_SHIFT - 10 + oind));
1253 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1254 fl = vm_phys_free_queues[dom][flind][pind];
1255 db_printf(" | %6.6d", fl[oind].lcnt);