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>
49 #include <sys/domainset.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
55 #include <sys/queue.h>
56 #include <sys/rwlock.h>
58 #include <sys/sysctl.h>
60 #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;
81 domainset_t __read_mostly all_domains = DOMAINSET_T_INITIALIZER(0x1);
83 struct vm_phys_seg __read_mostly vm_phys_segs[VM_PHYSSEG_MAX];
84 int __read_mostly vm_phys_nsegs;
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_padalign vm_phys_fictitious_reg_lock;
105 MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages");
107 static struct vm_freelist __aligned(CACHE_LINE_SIZE)
108 vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL]
111 static int __read_mostly vm_nfreelists;
114 * These "avail lists" are globals used to communicate boot-time physical
115 * memory layout to other parts of the kernel. Each physically contiguous
116 * region of memory is defined by a start address at an even index and an
117 * end address at the following odd index. Each list is terminated by a
118 * pair of zero entries.
120 * dump_avail tells the dump code what regions to include in a crash dump, and
121 * phys_avail is all of the remaining physical memory that is available for
124 * Initially dump_avail and phys_avail are identical. Boot time memory
125 * allocations remove extents from phys_avail that may still be included
128 vm_paddr_t phys_avail[PHYS_AVAIL_COUNT];
129 vm_paddr_t dump_avail[PHYS_AVAIL_COUNT];
132 * Provides the mapping from VM_FREELIST_* to free list indices (flind).
134 static int __read_mostly vm_freelist_to_flind[VM_NFREELIST];
136 CTASSERT(VM_FREELIST_DEFAULT == 0);
138 #ifdef VM_FREELIST_DMA32
139 #define VM_DMA32_BOUNDARY ((vm_paddr_t)1 << 32)
143 * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about
144 * the ordering of the free list boundaries.
146 #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY)
147 CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY);
150 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
151 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
152 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
154 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
155 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
156 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
159 static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS);
160 SYSCTL_OID(_vm, OID_AUTO, phys_locality, CTLTYPE_STRING | CTLFLAG_RD,
161 NULL, 0, sysctl_vm_phys_locality, "A", "Phys Locality Info");
164 SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
165 &vm_ndomains, 0, "Number of physical memory domains available.");
167 static vm_page_t vm_phys_alloc_seg_contig(struct vm_phys_seg *seg,
168 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
169 vm_paddr_t boundary);
170 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain);
171 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end);
172 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
173 int order, int tail);
176 * Red-black tree helpers for vm fictitious range management.
179 vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p,
180 struct vm_phys_fictitious_seg *range)
183 KASSERT(range->start != 0 && range->end != 0,
184 ("Invalid range passed on search for vm_fictitious page"));
185 if (p->start >= range->end)
187 if (p->start < range->start)
194 vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1,
195 struct vm_phys_fictitious_seg *p2)
198 /* Check if this is a search for a page */
200 return (vm_phys_fictitious_in_range(p1, p2));
202 KASSERT(p2->end != 0,
203 ("Invalid range passed as second parameter to vm fictitious comparison"));
205 /* Searching to add a new range */
206 if (p1->end <= p2->start)
208 if (p1->start >= p2->end)
211 panic("Trying to add overlapping vm fictitious ranges:\n"
212 "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start,
213 (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end);
217 vm_phys_domain_match(int prefer, vm_paddr_t low, vm_paddr_t high)
223 if (vm_ndomains == 1 || mem_affinity == NULL)
226 DOMAINSET_ZERO(&mask);
228 * Check for any memory that overlaps low, high.
230 for (i = 0; mem_affinity[i].end != 0; i++)
231 if (mem_affinity[i].start <= high &&
232 mem_affinity[i].end >= low)
233 DOMAINSET_SET(mem_affinity[i].domain, &mask);
234 if (prefer != -1 && DOMAINSET_ISSET(prefer, &mask))
236 if (DOMAINSET_EMPTY(&mask))
237 panic("vm_phys_domain_match: Impossible constraint");
238 return (DOMAINSET_FFS(&mask) - 1);
245 * Outputs the state of the physical memory allocator, specifically,
246 * the amount of physical memory in each free list.
249 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
252 struct vm_freelist *fl;
253 int dom, error, flind, oind, pind;
255 error = sysctl_wire_old_buffer(req, 0);
258 sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req);
259 for (dom = 0; dom < vm_ndomains; dom++) {
260 sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom);
261 for (flind = 0; flind < vm_nfreelists; flind++) {
262 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
263 "\n ORDER (SIZE) | NUMBER"
265 for (pind = 0; pind < VM_NFREEPOOL; pind++)
266 sbuf_printf(&sbuf, " | POOL %d", pind);
267 sbuf_printf(&sbuf, "\n-- ");
268 for (pind = 0; pind < VM_NFREEPOOL; pind++)
269 sbuf_printf(&sbuf, "-- -- ");
270 sbuf_printf(&sbuf, "--\n");
271 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
272 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
273 1 << (PAGE_SHIFT - 10 + oind));
274 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
275 fl = vm_phys_free_queues[dom][flind][pind];
276 sbuf_printf(&sbuf, " | %6d",
279 sbuf_printf(&sbuf, "\n");
283 error = sbuf_finish(&sbuf);
289 * Outputs the set of physical memory segments.
292 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
295 struct vm_phys_seg *seg;
298 error = sysctl_wire_old_buffer(req, 0);
301 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
302 for (segind = 0; segind < vm_phys_nsegs; segind++) {
303 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
304 seg = &vm_phys_segs[segind];
305 sbuf_printf(&sbuf, "start: %#jx\n",
306 (uintmax_t)seg->start);
307 sbuf_printf(&sbuf, "end: %#jx\n",
308 (uintmax_t)seg->end);
309 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
310 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
312 error = sbuf_finish(&sbuf);
318 * Return affinity, or -1 if there's no affinity information.
321 vm_phys_mem_affinity(int f, int t)
325 if (mem_locality == NULL)
327 if (f >= vm_ndomains || t >= vm_ndomains)
329 return (mem_locality[f * vm_ndomains + t]);
337 * Outputs the VM locality table.
340 sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS)
345 error = sysctl_wire_old_buffer(req, 0);
348 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
350 sbuf_printf(&sbuf, "\n");
352 for (i = 0; i < vm_ndomains; i++) {
353 sbuf_printf(&sbuf, "%d: ", i);
354 for (j = 0; j < vm_ndomains; j++) {
355 sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j));
357 sbuf_printf(&sbuf, "\n");
359 error = sbuf_finish(&sbuf);
366 vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
371 TAILQ_INSERT_TAIL(&fl[order].pl, m, listq);
373 TAILQ_INSERT_HEAD(&fl[order].pl, m, listq);
378 vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
381 TAILQ_REMOVE(&fl[order].pl, m, listq);
383 m->order = VM_NFREEORDER;
387 * Create a physical memory segment.
390 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain)
392 struct vm_phys_seg *seg;
394 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
395 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
396 KASSERT(domain >= 0 && domain < vm_ndomains,
397 ("vm_phys_create_seg: invalid domain provided"));
398 seg = &vm_phys_segs[vm_phys_nsegs++];
399 while (seg > vm_phys_segs && (seg - 1)->start >= end) {
405 seg->domain = domain;
409 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end)
414 if (mem_affinity == NULL) {
415 _vm_phys_create_seg(start, end, 0);
420 if (mem_affinity[i].end == 0)
421 panic("Reached end of affinity info");
422 if (mem_affinity[i].end <= start)
424 if (mem_affinity[i].start > start)
425 panic("No affinity info for start %jx",
427 if (mem_affinity[i].end >= end) {
428 _vm_phys_create_seg(start, end,
429 mem_affinity[i].domain);
432 _vm_phys_create_seg(start, mem_affinity[i].end,
433 mem_affinity[i].domain);
434 start = mem_affinity[i].end;
437 _vm_phys_create_seg(start, end, 0);
442 * Add a physical memory segment.
445 vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
449 KASSERT((start & PAGE_MASK) == 0,
450 ("vm_phys_define_seg: start is not page aligned"));
451 KASSERT((end & PAGE_MASK) == 0,
452 ("vm_phys_define_seg: end is not page aligned"));
455 * Split the physical memory segment if it spans two or more free
459 #ifdef VM_FREELIST_LOWMEM
460 if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) {
461 vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY);
462 paddr = VM_LOWMEM_BOUNDARY;
465 #ifdef VM_FREELIST_DMA32
466 if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) {
467 vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY);
468 paddr = VM_DMA32_BOUNDARY;
471 vm_phys_create_seg(paddr, end);
475 * Initialize the physical memory allocator.
477 * Requires that vm_page_array is initialized!
482 struct vm_freelist *fl;
483 struct vm_phys_seg *end_seg, *prev_seg, *seg, *tmp_seg;
485 int dom, flind, freelist, oind, pind, segind;
488 * Compute the number of free lists, and generate the mapping from the
489 * manifest constants VM_FREELIST_* to the free list indices.
491 * Initially, the entries of vm_freelist_to_flind[] are set to either
492 * 0 or 1 to indicate which free lists should be created.
495 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
496 seg = &vm_phys_segs[segind];
497 #ifdef VM_FREELIST_LOWMEM
498 if (seg->end <= VM_LOWMEM_BOUNDARY)
499 vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1;
502 #ifdef VM_FREELIST_DMA32
504 #ifdef VM_DMA32_NPAGES_THRESHOLD
506 * Create the DMA32 free list only if the amount of
507 * physical memory above physical address 4G exceeds the
510 npages > VM_DMA32_NPAGES_THRESHOLD &&
512 seg->end <= VM_DMA32_BOUNDARY)
513 vm_freelist_to_flind[VM_FREELIST_DMA32] = 1;
517 npages += atop(seg->end - seg->start);
518 vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1;
521 /* Change each entry into a running total of the free lists. */
522 for (freelist = 1; freelist < VM_NFREELIST; freelist++) {
523 vm_freelist_to_flind[freelist] +=
524 vm_freelist_to_flind[freelist - 1];
526 vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1];
527 KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists"));
528 /* Change each entry into a free list index. */
529 for (freelist = 0; freelist < VM_NFREELIST; freelist++)
530 vm_freelist_to_flind[freelist]--;
533 * Initialize the first_page and free_queues fields of each physical
536 #ifdef VM_PHYSSEG_SPARSE
539 for (segind = 0; segind < vm_phys_nsegs; segind++) {
540 seg = &vm_phys_segs[segind];
541 #ifdef VM_PHYSSEG_SPARSE
542 seg->first_page = &vm_page_array[npages];
543 npages += atop(seg->end - seg->start);
545 seg->first_page = PHYS_TO_VM_PAGE(seg->start);
547 #ifdef VM_FREELIST_LOWMEM
548 if (seg->end <= VM_LOWMEM_BOUNDARY) {
549 flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM];
551 ("vm_phys_init: LOWMEM flind < 0"));
554 #ifdef VM_FREELIST_DMA32
555 if (seg->end <= VM_DMA32_BOUNDARY) {
556 flind = vm_freelist_to_flind[VM_FREELIST_DMA32];
558 ("vm_phys_init: DMA32 flind < 0"));
562 flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT];
564 ("vm_phys_init: DEFAULT flind < 0"));
566 seg->free_queues = &vm_phys_free_queues[seg->domain][flind];
570 * Coalesce physical memory segments that are contiguous and share the
571 * same per-domain free queues.
573 prev_seg = vm_phys_segs;
574 seg = &vm_phys_segs[1];
575 end_seg = &vm_phys_segs[vm_phys_nsegs];
576 while (seg < end_seg) {
577 if (prev_seg->end == seg->start &&
578 prev_seg->free_queues == seg->free_queues) {
579 prev_seg->end = seg->end;
580 KASSERT(prev_seg->domain == seg->domain,
581 ("vm_phys_init: free queues cannot span domains"));
584 for (tmp_seg = seg; tmp_seg < end_seg; tmp_seg++)
585 *tmp_seg = *(tmp_seg + 1);
593 * Initialize the free queues.
595 for (dom = 0; dom < vm_ndomains; dom++) {
596 for (flind = 0; flind < vm_nfreelists; flind++) {
597 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
598 fl = vm_phys_free_queues[dom][flind][pind];
599 for (oind = 0; oind < VM_NFREEORDER; oind++)
600 TAILQ_INIT(&fl[oind].pl);
605 rw_init(&vm_phys_fictitious_reg_lock, "vmfctr");
609 * Register info about the NUMA topology of the system.
611 * Invoked by platform-dependent code prior to vm_phys_init().
614 vm_phys_register_domains(int ndomains, struct mem_affinity *affinity,
621 * For now the only override value that we support is 1, which
622 * effectively disables NUMA-awareness in the allocators.
625 TUNABLE_INT_FETCH("vm.numa.disabled", &d);
630 vm_ndomains = ndomains;
631 mem_affinity = affinity;
632 mem_locality = locality;
635 for (i = 0; i < vm_ndomains; i++)
636 DOMAINSET_SET(i, &all_domains);
645 _vm_phys_domain(vm_paddr_t pa)
650 if (vm_ndomains == 1 || mem_affinity == NULL)
654 * Check for any memory that overlaps.
656 for (i = 0; mem_affinity[i].end != 0; i++)
657 if (mem_affinity[i].start <= pa &&
658 mem_affinity[i].end >= pa)
659 return (mem_affinity[i].domain);
665 * Split a contiguous, power of two-sized set of physical pages.
667 * When this function is called by a page allocation function, the caller
668 * should request insertion at the head unless the order [order, oind) queues
669 * are known to be empty. The objective being to reduce the likelihood of
670 * long-term fragmentation by promoting contemporaneous allocation and
671 * (hopefully) deallocation.
674 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order,
679 while (oind > order) {
681 m_buddy = &m[1 << oind];
682 KASSERT(m_buddy->order == VM_NFREEORDER,
683 ("vm_phys_split_pages: page %p has unexpected order %d",
684 m_buddy, m_buddy->order));
685 vm_freelist_add(fl, m_buddy, oind, tail);
690 * Add the physical pages [m, m + npages) at the end of a power-of-two aligned
691 * and sized set to the specified free list.
693 * When this function is called by a page allocation function, the caller
694 * should request insertion at the head unless the lower-order queues are
695 * known to be empty. The objective being to reduce the likelihood of long-
696 * term fragmentation by promoting contemporaneous allocation and (hopefully)
699 * The physical page m's buddy must not be free.
702 vm_phys_enq_range(vm_page_t m, u_int npages, struct vm_freelist *fl, int tail)
707 KASSERT(npages > 0, ("vm_phys_enq_range: npages is 0"));
708 KASSERT(((VM_PAGE_TO_PHYS(m) + npages * PAGE_SIZE) &
709 ((PAGE_SIZE << (fls(npages) - 1)) - 1)) == 0,
710 ("vm_phys_enq_range: page %p and npages %u are misaligned",
713 KASSERT(m->order == VM_NFREEORDER,
714 ("vm_phys_enq_range: page %p has unexpected order %d",
716 order = ffs(npages) - 1;
717 KASSERT(order < VM_NFREEORDER,
718 ("vm_phys_enq_range: order %d is out of range", order));
719 vm_freelist_add(fl, m, order, tail);
723 } while (npages > 0);
727 * Tries to allocate the specified number of pages from the specified pool
728 * within the specified domain. Returns the actual number of allocated pages
729 * and a pointer to each page through the array ma[].
731 * The returned pages may not be physically contiguous. However, in contrast
732 * to performing multiple, back-to-back calls to vm_phys_alloc_pages(..., 0),
733 * calling this function once to allocate the desired number of pages will
734 * avoid wasted time in vm_phys_split_pages().
736 * The free page queues for the specified domain must be locked.
739 vm_phys_alloc_npages(int domain, int pool, int npages, vm_page_t ma[])
741 struct vm_freelist *alt, *fl;
743 int avail, end, flind, freelist, i, need, oind, pind;
745 KASSERT(domain >= 0 && domain < vm_ndomains,
746 ("vm_phys_alloc_npages: domain %d is out of range", domain));
747 KASSERT(pool < VM_NFREEPOOL,
748 ("vm_phys_alloc_npages: pool %d is out of range", pool));
749 KASSERT(npages <= 1 << (VM_NFREEORDER - 1),
750 ("vm_phys_alloc_npages: npages %d is out of range", npages));
751 vm_domain_free_assert_locked(VM_DOMAIN(domain));
753 for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
754 flind = vm_freelist_to_flind[freelist];
757 fl = vm_phys_free_queues[domain][flind][pool];
758 for (oind = 0; oind < VM_NFREEORDER; oind++) {
759 while ((m = TAILQ_FIRST(&fl[oind].pl)) != NULL) {
760 vm_freelist_rem(fl, m, oind);
762 need = imin(npages - i, avail);
763 for (end = i + need; i < end;)
767 * Return excess pages to fl. Its
768 * order [0, oind) queues are empty.
770 vm_phys_enq_range(m, avail - need, fl,
773 } else if (i == npages)
777 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
778 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
779 alt = vm_phys_free_queues[domain][flind][pind];
780 while ((m = TAILQ_FIRST(&alt[oind].pl)) !=
782 vm_freelist_rem(alt, m, oind);
783 vm_phys_set_pool(pool, m, oind);
785 need = imin(npages - i, avail);
786 for (end = i + need; i < end;)
790 * Return excess pages to fl.
791 * Its order [0, oind) queues
794 vm_phys_enq_range(m, avail -
797 } else if (i == npages)
807 * Allocate a contiguous, power of two-sized set of physical pages
808 * from the free lists.
810 * The free page queues must be locked.
813 vm_phys_alloc_pages(int domain, int pool, int order)
818 for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
819 m = vm_phys_alloc_freelist_pages(domain, freelist, pool, order);
827 * Allocate a contiguous, power of two-sized set of physical pages from the
828 * specified free list. The free list must be specified using one of the
829 * manifest constants VM_FREELIST_*.
831 * The free page queues must be locked.
834 vm_phys_alloc_freelist_pages(int domain, int freelist, int pool, int order)
836 struct vm_freelist *alt, *fl;
838 int oind, pind, flind;
840 KASSERT(domain >= 0 && domain < vm_ndomains,
841 ("vm_phys_alloc_freelist_pages: domain %d is out of range",
843 KASSERT(freelist < VM_NFREELIST,
844 ("vm_phys_alloc_freelist_pages: freelist %d is out of range",
846 KASSERT(pool < VM_NFREEPOOL,
847 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
848 KASSERT(order < VM_NFREEORDER,
849 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
851 flind = vm_freelist_to_flind[freelist];
852 /* Check if freelist is present */
856 vm_domain_free_assert_locked(VM_DOMAIN(domain));
857 fl = &vm_phys_free_queues[domain][flind][pool][0];
858 for (oind = order; oind < VM_NFREEORDER; oind++) {
859 m = TAILQ_FIRST(&fl[oind].pl);
861 vm_freelist_rem(fl, m, oind);
862 /* The order [order, oind) queues are empty. */
863 vm_phys_split_pages(m, oind, fl, order, 1);
869 * The given pool was empty. Find the largest
870 * contiguous, power-of-two-sized set of pages in any
871 * pool. Transfer these pages to the given pool, and
872 * use them to satisfy the allocation.
874 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
875 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
876 alt = &vm_phys_free_queues[domain][flind][pind][0];
877 m = TAILQ_FIRST(&alt[oind].pl);
879 vm_freelist_rem(alt, m, oind);
880 vm_phys_set_pool(pool, m, oind);
881 /* The order [order, oind) queues are empty. */
882 vm_phys_split_pages(m, oind, fl, order, 1);
891 * Find the vm_page corresponding to the given physical address.
894 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
896 struct vm_phys_seg *seg;
899 for (segind = 0; segind < vm_phys_nsegs; segind++) {
900 seg = &vm_phys_segs[segind];
901 if (pa >= seg->start && pa < seg->end)
902 return (&seg->first_page[atop(pa - seg->start)]);
908 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
910 struct vm_phys_fictitious_seg tmp, *seg;
917 rw_rlock(&vm_phys_fictitious_reg_lock);
918 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
919 rw_runlock(&vm_phys_fictitious_reg_lock);
923 m = &seg->first_page[atop(pa - seg->start)];
924 KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m));
930 vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start,
931 long page_count, vm_memattr_t memattr)
935 bzero(range, page_count * sizeof(*range));
936 for (i = 0; i < page_count; i++) {
937 vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr);
938 range[i].oflags &= ~VPO_UNMANAGED;
939 range[i].busy_lock = VPB_UNBUSIED;
944 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
945 vm_memattr_t memattr)
947 struct vm_phys_fictitious_seg *seg;
950 #ifdef VM_PHYSSEG_DENSE
956 ("Start of segment isn't less than end (start: %jx end: %jx)",
957 (uintmax_t)start, (uintmax_t)end));
959 page_count = (end - start) / PAGE_SIZE;
961 #ifdef VM_PHYSSEG_DENSE
964 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
965 fp = &vm_page_array[pi - first_page];
966 if ((pe - first_page) > vm_page_array_size) {
968 * We have a segment that starts inside
969 * of vm_page_array, but ends outside of it.
971 * Use vm_page_array pages for those that are
972 * inside of the vm_page_array range, and
973 * allocate the remaining ones.
975 dpage_count = vm_page_array_size - (pi - first_page);
976 vm_phys_fictitious_init_range(fp, start, dpage_count,
978 page_count -= dpage_count;
979 start += ptoa(dpage_count);
983 * We can allocate the full range from vm_page_array,
984 * so there's no need to register the range in the tree.
986 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
988 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
990 * We have a segment that ends inside of vm_page_array,
991 * but starts outside of it.
993 fp = &vm_page_array[0];
994 dpage_count = pe - first_page;
995 vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count,
997 end -= ptoa(dpage_count);
998 page_count -= dpage_count;
1000 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
1002 * Trying to register a fictitious range that expands before
1003 * and after vm_page_array.
1009 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
1011 #ifdef VM_PHYSSEG_DENSE
1014 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
1016 seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO);
1019 seg->first_page = fp;
1021 rw_wlock(&vm_phys_fictitious_reg_lock);
1022 RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg);
1023 rw_wunlock(&vm_phys_fictitious_reg_lock);
1029 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
1031 struct vm_phys_fictitious_seg *seg, tmp;
1032 #ifdef VM_PHYSSEG_DENSE
1036 KASSERT(start < end,
1037 ("Start of segment isn't less than end (start: %jx end: %jx)",
1038 (uintmax_t)start, (uintmax_t)end));
1040 #ifdef VM_PHYSSEG_DENSE
1043 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1044 if ((pe - first_page) <= vm_page_array_size) {
1046 * This segment was allocated using vm_page_array
1047 * only, there's nothing to do since those pages
1048 * were never added to the tree.
1053 * We have a segment that starts inside
1054 * of vm_page_array, but ends outside of it.
1056 * Calculate how many pages were added to the
1057 * tree and free them.
1059 start = ptoa(first_page + vm_page_array_size);
1060 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
1062 * We have a segment that ends inside of vm_page_array,
1063 * but starts outside of it.
1065 end = ptoa(first_page);
1066 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
1067 /* Since it's not possible to register such a range, panic. */
1069 "Unregistering not registered fictitious range [%#jx:%#jx]",
1070 (uintmax_t)start, (uintmax_t)end);
1076 rw_wlock(&vm_phys_fictitious_reg_lock);
1077 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
1078 if (seg->start != start || seg->end != end) {
1079 rw_wunlock(&vm_phys_fictitious_reg_lock);
1081 "Unregistering not registered fictitious range [%#jx:%#jx]",
1082 (uintmax_t)start, (uintmax_t)end);
1084 RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg);
1085 rw_wunlock(&vm_phys_fictitious_reg_lock);
1086 free(seg->first_page, M_FICT_PAGES);
1087 free(seg, M_FICT_PAGES);
1091 * Free a contiguous, power of two-sized set of physical pages.
1093 * The free page queues must be locked.
1096 vm_phys_free_pages(vm_page_t m, int order)
1098 struct vm_freelist *fl;
1099 struct vm_phys_seg *seg;
1103 KASSERT(m->order == VM_NFREEORDER,
1104 ("vm_phys_free_pages: page %p has unexpected order %d",
1106 KASSERT(m->pool < VM_NFREEPOOL,
1107 ("vm_phys_free_pages: page %p has unexpected pool %d",
1109 KASSERT(order < VM_NFREEORDER,
1110 ("vm_phys_free_pages: order %d is out of range", order));
1111 seg = &vm_phys_segs[m->segind];
1112 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1113 if (order < VM_NFREEORDER - 1) {
1114 pa = VM_PAGE_TO_PHYS(m);
1116 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
1117 if (pa < seg->start || pa >= seg->end)
1119 m_buddy = &seg->first_page[atop(pa - seg->start)];
1120 if (m_buddy->order != order)
1122 fl = (*seg->free_queues)[m_buddy->pool];
1123 vm_freelist_rem(fl, m_buddy, order);
1124 if (m_buddy->pool != m->pool)
1125 vm_phys_set_pool(m->pool, m_buddy, order);
1127 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
1128 m = &seg->first_page[atop(pa - seg->start)];
1129 } while (order < VM_NFREEORDER - 1);
1131 fl = (*seg->free_queues)[m->pool];
1132 vm_freelist_add(fl, m, order, 1);
1136 * Return the largest possible order of a set of pages starting at m.
1139 max_order(vm_page_t m)
1143 * Unsigned "min" is used here so that "order" is assigned
1144 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
1145 * or the low-order bits of its physical address are zero
1146 * because the size of a physical address exceeds the size of
1149 return (min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
1150 VM_NFREEORDER - 1));
1154 * Free a contiguous, arbitrarily sized set of physical pages, without
1155 * merging across set boundaries.
1157 * The free page queues must be locked.
1160 vm_phys_enqueue_contig(vm_page_t m, u_long npages)
1162 struct vm_freelist *fl;
1163 struct vm_phys_seg *seg;
1168 * Avoid unnecessary coalescing by freeing the pages in the largest
1169 * possible power-of-two-sized subsets.
1171 vm_domain_free_assert_locked(vm_pagequeue_domain(m));
1172 seg = &vm_phys_segs[m->segind];
1173 fl = (*seg->free_queues)[m->pool];
1175 /* Free blocks of increasing size. */
1176 while ((order = max_order(m)) < VM_NFREEORDER - 1 &&
1177 m + (1 << order) <= m_end) {
1178 KASSERT(seg == &vm_phys_segs[m->segind],
1179 ("%s: page range [%p,%p) spans multiple segments",
1180 __func__, m_end - npages, m));
1181 vm_freelist_add(fl, m, order, 1);
1184 /* Free blocks of maximum size. */
1185 while (m + (1 << order) <= m_end) {
1186 KASSERT(seg == &vm_phys_segs[m->segind],
1187 ("%s: page range [%p,%p) spans multiple segments",
1188 __func__, m_end - npages, m));
1189 vm_freelist_add(fl, m, order, 1);
1192 /* Free blocks of diminishing size. */
1194 KASSERT(seg == &vm_phys_segs[m->segind],
1195 ("%s: page range [%p,%p) spans multiple segments",
1196 __func__, m_end - npages, m));
1197 order = flsl(m_end - m) - 1;
1198 vm_freelist_add(fl, m, order, 1);
1204 * Free a contiguous, arbitrarily sized set of physical pages.
1206 * The free page queues must be locked.
1209 vm_phys_free_contig(vm_page_t m, u_long npages)
1211 int order_start, order_end;
1212 vm_page_t m_start, m_end;
1214 vm_domain_free_assert_locked(vm_pagequeue_domain(m));
1217 order_start = max_order(m_start);
1218 if (order_start < VM_NFREEORDER - 1)
1219 m_start += 1 << order_start;
1221 order_end = max_order(m_end);
1222 if (order_end < VM_NFREEORDER - 1)
1223 m_end -= 1 << order_end;
1225 * Avoid unnecessary coalescing by freeing the pages at the start and
1226 * end of the range last.
1228 if (m_start < m_end)
1229 vm_phys_enqueue_contig(m_start, m_end - m_start);
1230 if (order_start < VM_NFREEORDER - 1)
1231 vm_phys_free_pages(m, order_start);
1232 if (order_end < VM_NFREEORDER - 1)
1233 vm_phys_free_pages(m_end, order_end);
1237 * Scan physical memory between the specified addresses "low" and "high" for a
1238 * run of contiguous physical pages that satisfy the specified conditions, and
1239 * return the lowest page in the run. The specified "alignment" determines
1240 * the alignment of the lowest physical page in the run. If the specified
1241 * "boundary" is non-zero, then the run of physical pages cannot span a
1242 * physical address that is a multiple of "boundary".
1244 * "npages" must be greater than zero. Both "alignment" and "boundary" must
1245 * be a power of two.
1248 vm_phys_scan_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1249 u_long alignment, vm_paddr_t boundary, int options)
1252 vm_page_t m_end, m_run, m_start;
1253 struct vm_phys_seg *seg;
1256 KASSERT(npages > 0, ("npages is 0"));
1257 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1258 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1261 for (segind = 0; segind < vm_phys_nsegs; segind++) {
1262 seg = &vm_phys_segs[segind];
1263 if (seg->domain != domain)
1265 if (seg->start >= high)
1267 if (low >= seg->end)
1269 if (low <= seg->start)
1270 m_start = seg->first_page;
1272 m_start = &seg->first_page[atop(low - seg->start)];
1273 if (high < seg->end)
1277 if (pa_end - VM_PAGE_TO_PHYS(m_start) < ptoa(npages))
1279 m_end = &seg->first_page[atop(pa_end - seg->start)];
1280 m_run = vm_page_scan_contig(npages, m_start, m_end,
1281 alignment, boundary, options);
1289 * Set the pool for a contiguous, power of two-sized set of physical pages.
1292 vm_phys_set_pool(int pool, vm_page_t m, int order)
1296 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
1301 * Search for the given physical page "m" in the free lists. If the search
1302 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
1303 * FALSE, indicating that "m" is not in the free lists.
1305 * The free page queues must be locked.
1308 vm_phys_unfree_page(vm_page_t m)
1310 struct vm_freelist *fl;
1311 struct vm_phys_seg *seg;
1312 vm_paddr_t pa, pa_half;
1313 vm_page_t m_set, m_tmp;
1317 * First, find the contiguous, power of two-sized set of free
1318 * physical pages containing the given physical page "m" and
1319 * assign it to "m_set".
1321 seg = &vm_phys_segs[m->segind];
1322 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1323 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
1324 order < VM_NFREEORDER - 1; ) {
1326 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
1327 if (pa >= seg->start)
1328 m_set = &seg->first_page[atop(pa - seg->start)];
1332 if (m_set->order < order)
1334 if (m_set->order == VM_NFREEORDER)
1336 KASSERT(m_set->order < VM_NFREEORDER,
1337 ("vm_phys_unfree_page: page %p has unexpected order %d",
1338 m_set, m_set->order));
1341 * Next, remove "m_set" from the free lists. Finally, extract
1342 * "m" from "m_set" using an iterative algorithm: While "m_set"
1343 * is larger than a page, shrink "m_set" by returning the half
1344 * of "m_set" that does not contain "m" to the free lists.
1346 fl = (*seg->free_queues)[m_set->pool];
1347 order = m_set->order;
1348 vm_freelist_rem(fl, m_set, order);
1351 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
1352 if (m->phys_addr < pa_half)
1353 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
1356 m_set = &seg->first_page[atop(pa_half - seg->start)];
1358 vm_freelist_add(fl, m_tmp, order, 0);
1360 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
1365 * Allocate a contiguous set of physical pages of the given size
1366 * "npages" from the free lists. All of the physical pages must be at
1367 * or above the given physical address "low" and below the given
1368 * physical address "high". The given value "alignment" determines the
1369 * alignment of the first physical page in the set. If the given value
1370 * "boundary" is non-zero, then the set of physical pages cannot cross
1371 * any physical address boundary that is a multiple of that value. Both
1372 * "alignment" and "boundary" must be a power of two.
1375 vm_phys_alloc_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1376 u_long alignment, vm_paddr_t boundary)
1378 vm_paddr_t pa_end, pa_start;
1380 struct vm_phys_seg *seg;
1383 KASSERT(npages > 0, ("npages is 0"));
1384 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1385 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1386 vm_domain_free_assert_locked(VM_DOMAIN(domain));
1390 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
1391 seg = &vm_phys_segs[segind];
1392 if (seg->start >= high || seg->domain != domain)
1394 if (low >= seg->end)
1396 if (low <= seg->start)
1397 pa_start = seg->start;
1400 if (high < seg->end)
1404 if (pa_end - pa_start < ptoa(npages))
1406 m_run = vm_phys_alloc_seg_contig(seg, npages, low, high,
1407 alignment, boundary);
1415 * Allocate a run of contiguous physical pages from the free list for the
1416 * specified segment.
1419 vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, u_long npages,
1420 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1422 struct vm_freelist *fl;
1423 vm_paddr_t pa, pa_end, size;
1426 int oind, order, pind;
1428 KASSERT(npages > 0, ("npages is 0"));
1429 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1430 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1431 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1432 /* Compute the queue that is the best fit for npages. */
1433 order = flsl(npages - 1);
1434 /* Search for a run satisfying the specified conditions. */
1435 size = npages << PAGE_SHIFT;
1436 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER;
1438 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1439 fl = (*seg->free_queues)[pind];
1440 TAILQ_FOREACH(m_ret, &fl[oind].pl, listq) {
1442 * Is the size of this allocation request
1443 * larger than the largest block size?
1445 if (order >= VM_NFREEORDER) {
1447 * Determine if a sufficient number of
1448 * subsequent blocks to satisfy the
1449 * allocation request are free.
1451 pa = VM_PAGE_TO_PHYS(m_ret);
1456 pa += 1 << (PAGE_SHIFT +
1462 m = &seg->first_page[atop(pa -
1464 if (m->order != VM_NFREEORDER -
1468 /* If not, go to the next block. */
1474 * Determine if the blocks are within the
1475 * given range, satisfy the given alignment,
1476 * and do not cross the given boundary.
1478 pa = VM_PAGE_TO_PHYS(m_ret);
1480 if (pa >= low && pa_end <= high &&
1481 (pa & (alignment - 1)) == 0 &&
1482 rounddown2(pa ^ (pa_end - 1), boundary) == 0)
1489 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
1490 fl = (*seg->free_queues)[m->pool];
1491 vm_freelist_rem(fl, m, oind);
1492 if (m->pool != VM_FREEPOOL_DEFAULT)
1493 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, oind);
1495 /* Return excess pages to the free lists. */
1496 npages_end = roundup2(npages, 1 << oind);
1497 if (npages < npages_end) {
1498 fl = (*seg->free_queues)[VM_FREEPOOL_DEFAULT];
1499 vm_phys_enq_range(&m_ret[npages], npages_end - npages, fl, 0);
1505 * Return the index of the first unused slot which may be the terminating
1509 vm_phys_avail_count(void)
1513 for (i = 0; phys_avail[i + 1]; i += 2)
1515 if (i > PHYS_AVAIL_ENTRIES)
1516 panic("Improperly terminated phys_avail %d entries", i);
1522 * Assert that a phys_avail entry is valid.
1525 vm_phys_avail_check(int i)
1527 if (phys_avail[i] & PAGE_MASK)
1528 panic("Unaligned phys_avail[%d]: %#jx", i,
1529 (intmax_t)phys_avail[i]);
1530 if (phys_avail[i+1] & PAGE_MASK)
1531 panic("Unaligned phys_avail[%d + 1]: %#jx", i,
1532 (intmax_t)phys_avail[i]);
1533 if (phys_avail[i + 1] < phys_avail[i])
1534 panic("phys_avail[%d] start %#jx < end %#jx", i,
1535 (intmax_t)phys_avail[i], (intmax_t)phys_avail[i+1]);
1539 * Return the index of an overlapping phys_avail entry or -1.
1543 vm_phys_avail_find(vm_paddr_t pa)
1547 for (i = 0; phys_avail[i + 1]; i += 2)
1548 if (phys_avail[i] <= pa && phys_avail[i + 1] > pa)
1555 * Return the index of the largest entry.
1558 vm_phys_avail_largest(void)
1560 vm_paddr_t sz, largesz;
1566 for (i = 0; phys_avail[i + 1]; i += 2) {
1567 sz = vm_phys_avail_size(i);
1578 vm_phys_avail_size(int i)
1581 return (phys_avail[i + 1] - phys_avail[i]);
1585 * Split an entry at the address 'pa'. Return zero on success or errno.
1588 vm_phys_avail_split(vm_paddr_t pa, int i)
1592 vm_phys_avail_check(i);
1593 if (pa <= phys_avail[i] || pa >= phys_avail[i + 1])
1594 panic("vm_phys_avail_split: invalid address");
1595 cnt = vm_phys_avail_count();
1596 if (cnt >= PHYS_AVAIL_ENTRIES)
1598 memmove(&phys_avail[i + 2], &phys_avail[i],
1599 (cnt - i) * sizeof(phys_avail[0]));
1600 phys_avail[i + 1] = pa;
1601 phys_avail[i + 2] = pa;
1602 vm_phys_avail_check(i);
1603 vm_phys_avail_check(i+2);
1609 * This routine allocates NUMA node specific memory before the page
1610 * allocator is bootstrapped.
1613 vm_phys_early_alloc(int domain, size_t alloc_size)
1615 int i, mem_index, biggestone;
1616 vm_paddr_t pa, mem_start, mem_end, size, biggestsize, align;
1620 * Search the mem_affinity array for the biggest address
1621 * range in the desired domain. This is used to constrain
1622 * the phys_avail selection below.
1629 if (mem_affinity != NULL) {
1630 for (i = 0; ; i++) {
1631 size = mem_affinity[i].end - mem_affinity[i].start;
1634 if (mem_affinity[i].domain != domain)
1636 if (size > biggestsize) {
1641 mem_start = mem_affinity[mem_index].start;
1642 mem_end = mem_affinity[mem_index].end;
1647 * Now find biggest physical segment in within the desired
1652 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
1653 /* skip regions that are out of range */
1654 if (phys_avail[i+1] - alloc_size < mem_start ||
1655 phys_avail[i+1] > mem_end)
1657 size = vm_phys_avail_size(i);
1658 if (size > biggestsize) {
1663 alloc_size = round_page(alloc_size);
1666 * Grab single pages from the front to reduce fragmentation.
1668 if (alloc_size == PAGE_SIZE) {
1669 pa = phys_avail[biggestone];
1670 phys_avail[biggestone] += PAGE_SIZE;
1671 vm_phys_avail_check(biggestone);
1676 * Naturally align large allocations.
1678 align = phys_avail[biggestone + 1] & (alloc_size - 1);
1679 if (alloc_size + align > biggestsize)
1680 panic("cannot find a large enough size\n");
1682 vm_phys_avail_split(phys_avail[biggestone + 1] - align,
1684 /* Wasting memory. */
1685 phys_avail[biggestone + 1] -= align;
1687 phys_avail[biggestone + 1] -= alloc_size;
1688 vm_phys_avail_check(biggestone);
1689 pa = phys_avail[biggestone + 1];
1694 vm_phys_early_startup(void)
1698 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
1699 phys_avail[i] = round_page(phys_avail[i]);
1700 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
1704 /* Force phys_avail to be split by domain. */
1705 if (mem_affinity != NULL) {
1708 for (i = 0; mem_affinity[i].end != 0; i++) {
1709 idx = vm_phys_avail_find(mem_affinity[i].start);
1711 phys_avail[idx] != mem_affinity[i].start)
1712 vm_phys_avail_split(mem_affinity[i].start, idx);
1713 idx = vm_phys_avail_find(mem_affinity[i].end);
1715 phys_avail[idx] != mem_affinity[i].end)
1716 vm_phys_avail_split(mem_affinity[i].end, idx);
1724 * Show the number of physical pages in each of the free lists.
1726 DB_SHOW_COMMAND(freepages, db_show_freepages)
1728 struct vm_freelist *fl;
1729 int flind, oind, pind, dom;
1731 for (dom = 0; dom < vm_ndomains; dom++) {
1732 db_printf("DOMAIN: %d\n", dom);
1733 for (flind = 0; flind < vm_nfreelists; flind++) {
1734 db_printf("FREE LIST %d:\n"
1735 "\n ORDER (SIZE) | NUMBER"
1737 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1738 db_printf(" | POOL %d", pind);
1740 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1741 db_printf("-- -- ");
1743 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
1744 db_printf(" %2.2d (%6.6dK)", oind,
1745 1 << (PAGE_SHIFT - 10 + oind));
1746 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1747 fl = vm_phys_free_queues[dom][flind][pind];
1748 db_printf(" | %6.6d", fl[oind].lcnt);