2 * Copyright (c) 2002-2006 Rice University
3 * Copyright (c) 2007 Alan L. Cox <alc@cs.rice.edu>
6 * This software was developed for the FreeBSD Project by Alan L. Cox,
7 * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
28 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Physical memory system implementation
35 * Any external functions defined by this module are only to be used by the
36 * virtual memory system.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
54 #include <sys/queue.h>
55 #include <sys/rwlock.h>
57 #include <sys/sysctl.h>
59 #include <sys/vmmeter.h>
64 #include <vm/vm_param.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_phys.h>
70 _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX,
71 "Too many physsegs.");
73 struct mem_affinity *mem_affinity;
77 struct vm_phys_seg vm_phys_segs[VM_PHYSSEG_MAX];
80 struct vm_phys_fictitious_seg;
81 static int vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *,
82 struct vm_phys_fictitious_seg *);
84 RB_HEAD(fict_tree, vm_phys_fictitious_seg) vm_phys_fictitious_tree =
85 RB_INITIALIZER(_vm_phys_fictitious_tree);
87 struct vm_phys_fictitious_seg {
88 RB_ENTRY(vm_phys_fictitious_seg) node;
89 /* Memory region data */
95 RB_GENERATE_STATIC(fict_tree, vm_phys_fictitious_seg, node,
96 vm_phys_fictitious_cmp);
98 static struct rwlock vm_phys_fictitious_reg_lock;
99 MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages");
101 static struct vm_freelist
102 vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL][VM_NFREEORDER];
104 static int vm_nfreelists;
107 * Provides the mapping from VM_FREELIST_* to free list indices (flind).
109 static int vm_freelist_to_flind[VM_NFREELIST];
111 CTASSERT(VM_FREELIST_DEFAULT == 0);
113 #ifdef VM_FREELIST_ISADMA
114 #define VM_ISADMA_BOUNDARY 16777216
116 #ifdef VM_FREELIST_DMA32
117 #define VM_DMA32_BOUNDARY ((vm_paddr_t)1 << 32)
121 * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about
122 * the ordering of the free list boundaries.
124 #if defined(VM_ISADMA_BOUNDARY) && defined(VM_LOWMEM_BOUNDARY)
125 CTASSERT(VM_ISADMA_BOUNDARY < VM_LOWMEM_BOUNDARY);
127 #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY)
128 CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY);
131 static int cnt_prezero;
132 SYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
133 &cnt_prezero, 0, "The number of physical pages prezeroed at idle time");
135 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
136 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
137 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
139 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
140 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
141 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
143 SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
144 &vm_ndomains, 0, "Number of physical memory domains available.");
146 static vm_page_t vm_phys_alloc_domain_pages(int domain, int flind, int pool,
148 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain);
149 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end);
150 static int vm_phys_paddr_to_segind(vm_paddr_t pa);
151 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
155 * Red-black tree helpers for vm fictitious range management.
158 vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p,
159 struct vm_phys_fictitious_seg *range)
162 KASSERT(range->start != 0 && range->end != 0,
163 ("Invalid range passed on search for vm_fictitious page"));
164 if (p->start >= range->end)
166 if (p->start < range->start)
173 vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1,
174 struct vm_phys_fictitious_seg *p2)
177 /* Check if this is a search for a page */
179 return (vm_phys_fictitious_in_range(p1, p2));
181 KASSERT(p2->end != 0,
182 ("Invalid range passed as second parameter to vm fictitious comparison"));
184 /* Searching to add a new range */
185 if (p1->end <= p2->start)
187 if (p1->start >= p2->end)
190 panic("Trying to add overlapping vm fictitious ranges:\n"
191 "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start,
192 (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end);
196 vm_rr_selectdomain(void)
204 td->td_dom_rr_idx %= vm_ndomains;
205 return (td->td_dom_rr_idx);
212 vm_phys_domain_intersects(long mask, vm_paddr_t low, vm_paddr_t high)
214 struct vm_phys_seg *s;
217 while ((idx = ffsl(mask)) != 0) {
218 idx--; /* ffsl counts from 1 */
219 mask &= ~(1UL << idx);
220 s = &vm_phys_segs[idx];
221 if (low < s->end && high > s->start)
228 * Outputs the state of the physical memory allocator, specifically,
229 * the amount of physical memory in each free list.
232 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
235 struct vm_freelist *fl;
236 int dom, error, flind, oind, pind;
238 error = sysctl_wire_old_buffer(req, 0);
241 sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req);
242 for (dom = 0; dom < vm_ndomains; dom++) {
243 sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom);
244 for (flind = 0; flind < vm_nfreelists; flind++) {
245 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
246 "\n ORDER (SIZE) | NUMBER"
248 for (pind = 0; pind < VM_NFREEPOOL; pind++)
249 sbuf_printf(&sbuf, " | POOL %d", pind);
250 sbuf_printf(&sbuf, "\n-- ");
251 for (pind = 0; pind < VM_NFREEPOOL; pind++)
252 sbuf_printf(&sbuf, "-- -- ");
253 sbuf_printf(&sbuf, "--\n");
254 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
255 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
256 1 << (PAGE_SHIFT - 10 + oind));
257 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
258 fl = vm_phys_free_queues[dom][flind][pind];
259 sbuf_printf(&sbuf, " | %6d",
262 sbuf_printf(&sbuf, "\n");
266 error = sbuf_finish(&sbuf);
272 * Outputs the set of physical memory segments.
275 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
278 struct vm_phys_seg *seg;
281 error = sysctl_wire_old_buffer(req, 0);
284 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
285 for (segind = 0; segind < vm_phys_nsegs; segind++) {
286 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
287 seg = &vm_phys_segs[segind];
288 sbuf_printf(&sbuf, "start: %#jx\n",
289 (uintmax_t)seg->start);
290 sbuf_printf(&sbuf, "end: %#jx\n",
291 (uintmax_t)seg->end);
292 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
293 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
295 error = sbuf_finish(&sbuf);
301 vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
306 TAILQ_INSERT_TAIL(&fl[order].pl, m, plinks.q);
308 TAILQ_INSERT_HEAD(&fl[order].pl, m, plinks.q);
313 vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
316 TAILQ_REMOVE(&fl[order].pl, m, plinks.q);
318 m->order = VM_NFREEORDER;
322 * Create a physical memory segment.
325 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain)
327 struct vm_phys_seg *seg;
329 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
330 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
331 KASSERT(domain < vm_ndomains,
332 ("vm_phys_create_seg: invalid domain provided"));
333 seg = &vm_phys_segs[vm_phys_nsegs++];
334 while (seg > vm_phys_segs && (seg - 1)->start >= end) {
340 seg->domain = domain;
344 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end)
348 if (mem_affinity == NULL) {
349 _vm_phys_create_seg(start, end, 0);
354 if (mem_affinity[i].end == 0)
355 panic("Reached end of affinity info");
356 if (mem_affinity[i].end <= start)
358 if (mem_affinity[i].start > start)
359 panic("No affinity info for start %jx",
361 if (mem_affinity[i].end >= end) {
362 _vm_phys_create_seg(start, end,
363 mem_affinity[i].domain);
366 _vm_phys_create_seg(start, mem_affinity[i].end,
367 mem_affinity[i].domain);
368 start = mem_affinity[i].end;
373 * Add a physical memory segment.
376 vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
380 KASSERT((start & PAGE_MASK) == 0,
381 ("vm_phys_define_seg: start is not page aligned"));
382 KASSERT((end & PAGE_MASK) == 0,
383 ("vm_phys_define_seg: end is not page aligned"));
386 * Split the physical memory segment if it spans two or more free
390 #ifdef VM_FREELIST_ISADMA
391 if (paddr < VM_ISADMA_BOUNDARY && end > VM_ISADMA_BOUNDARY) {
392 vm_phys_create_seg(paddr, VM_ISADMA_BOUNDARY);
393 paddr = VM_ISADMA_BOUNDARY;
396 #ifdef VM_FREELIST_LOWMEM
397 if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) {
398 vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY);
399 paddr = VM_LOWMEM_BOUNDARY;
402 #ifdef VM_FREELIST_DMA32
403 if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) {
404 vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY);
405 paddr = VM_DMA32_BOUNDARY;
408 vm_phys_create_seg(paddr, end);
412 * Initialize the physical memory allocator.
414 * Requires that vm_page_array is initialized!
419 struct vm_freelist *fl;
420 struct vm_phys_seg *seg;
422 int dom, flind, freelist, oind, pind, segind;
425 * Compute the number of free lists, and generate the mapping from the
426 * manifest constants VM_FREELIST_* to the free list indices.
428 * Initially, the entries of vm_freelist_to_flind[] are set to either
429 * 0 or 1 to indicate which free lists should be created.
432 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
433 seg = &vm_phys_segs[segind];
434 #ifdef VM_FREELIST_ISADMA
435 if (seg->end <= VM_ISADMA_BOUNDARY)
436 vm_freelist_to_flind[VM_FREELIST_ISADMA] = 1;
439 #ifdef VM_FREELIST_LOWMEM
440 if (seg->end <= VM_LOWMEM_BOUNDARY)
441 vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1;
444 #ifdef VM_FREELIST_DMA32
446 #ifdef VM_DMA32_NPAGES_THRESHOLD
448 * Create the DMA32 free list only if the amount of
449 * physical memory above physical address 4G exceeds the
452 npages > VM_DMA32_NPAGES_THRESHOLD &&
454 seg->end <= VM_DMA32_BOUNDARY)
455 vm_freelist_to_flind[VM_FREELIST_DMA32] = 1;
459 npages += atop(seg->end - seg->start);
460 vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1;
463 /* Change each entry into a running total of the free lists. */
464 for (freelist = 1; freelist < VM_NFREELIST; freelist++) {
465 vm_freelist_to_flind[freelist] +=
466 vm_freelist_to_flind[freelist - 1];
468 vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1];
469 KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists"));
470 /* Change each entry into a free list index. */
471 for (freelist = 0; freelist < VM_NFREELIST; freelist++)
472 vm_freelist_to_flind[freelist]--;
475 * Initialize the first_page and free_queues fields of each physical
478 #ifdef VM_PHYSSEG_SPARSE
481 for (segind = 0; segind < vm_phys_nsegs; segind++) {
482 seg = &vm_phys_segs[segind];
483 #ifdef VM_PHYSSEG_SPARSE
484 seg->first_page = &vm_page_array[npages];
485 npages += atop(seg->end - seg->start);
487 seg->first_page = PHYS_TO_VM_PAGE(seg->start);
489 #ifdef VM_FREELIST_ISADMA
490 if (seg->end <= VM_ISADMA_BOUNDARY) {
491 flind = vm_freelist_to_flind[VM_FREELIST_ISADMA];
493 ("vm_phys_init: ISADMA flind < 0"));
496 #ifdef VM_FREELIST_LOWMEM
497 if (seg->end <= VM_LOWMEM_BOUNDARY) {
498 flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM];
500 ("vm_phys_init: LOWMEM flind < 0"));
503 #ifdef VM_FREELIST_DMA32
504 if (seg->end <= VM_DMA32_BOUNDARY) {
505 flind = vm_freelist_to_flind[VM_FREELIST_DMA32];
507 ("vm_phys_init: DMA32 flind < 0"));
511 flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT];
513 ("vm_phys_init: DEFAULT flind < 0"));
515 seg->free_queues = &vm_phys_free_queues[seg->domain][flind];
519 * Initialize the free queues.
521 for (dom = 0; dom < vm_ndomains; dom++) {
522 for (flind = 0; flind < vm_nfreelists; flind++) {
523 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
524 fl = vm_phys_free_queues[dom][flind][pind];
525 for (oind = 0; oind < VM_NFREEORDER; oind++)
526 TAILQ_INIT(&fl[oind].pl);
531 rw_init(&vm_phys_fictitious_reg_lock, "vmfctr");
535 * Split a contiguous, power of two-sized set of physical pages.
538 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
542 while (oind > order) {
544 m_buddy = &m[1 << oind];
545 KASSERT(m_buddy->order == VM_NFREEORDER,
546 ("vm_phys_split_pages: page %p has unexpected order %d",
547 m_buddy, m_buddy->order));
548 vm_freelist_add(fl, m_buddy, oind, 0);
553 * Initialize a physical page and add it to the free lists.
556 vm_phys_add_page(vm_paddr_t pa)
559 struct vm_domain *vmd;
561 vm_cnt.v_page_count++;
562 m = vm_phys_paddr_to_vm_page(pa);
565 m->segind = vm_phys_paddr_to_segind(pa);
566 vmd = vm_phys_domain(m);
567 vmd->vmd_page_count++;
568 vmd->vmd_segs |= 1UL << m->segind;
569 KASSERT(m->order == VM_NFREEORDER,
570 ("vm_phys_add_page: page %p has unexpected order %d",
572 m->pool = VM_FREEPOOL_DEFAULT;
574 mtx_lock(&vm_page_queue_free_mtx);
575 vm_phys_freecnt_adj(m, 1);
576 vm_phys_free_pages(m, 0);
577 mtx_unlock(&vm_page_queue_free_mtx);
581 * Allocate a contiguous, power of two-sized set of physical pages
582 * from the free lists.
584 * The free page queues must be locked.
587 vm_phys_alloc_pages(int pool, int order)
590 int dom, domain, flind;
592 KASSERT(pool < VM_NFREEPOOL,
593 ("vm_phys_alloc_pages: pool %d is out of range", pool));
594 KASSERT(order < VM_NFREEORDER,
595 ("vm_phys_alloc_pages: order %d is out of range", order));
597 for (dom = 0; dom < vm_ndomains; dom++) {
598 domain = vm_rr_selectdomain();
599 for (flind = 0; flind < vm_nfreelists; flind++) {
600 m = vm_phys_alloc_domain_pages(domain, flind, pool,
610 * Allocate a contiguous, power of two-sized set of physical pages from the
611 * specified free list. The free list must be specified using one of the
612 * manifest constants VM_FREELIST_*.
614 * The free page queues must be locked.
617 vm_phys_alloc_freelist_pages(int freelist, int pool, int order)
622 KASSERT(freelist < VM_NFREELIST,
623 ("vm_phys_alloc_freelist_pages: freelist %d is out of range",
625 KASSERT(pool < VM_NFREEPOOL,
626 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
627 KASSERT(order < VM_NFREEORDER,
628 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
629 for (dom = 0; dom < vm_ndomains; dom++) {
630 domain = vm_rr_selectdomain();
631 m = vm_phys_alloc_domain_pages(domain,
632 vm_freelist_to_flind[freelist], pool, order);
640 vm_phys_alloc_domain_pages(int domain, int flind, int pool, int order)
642 struct vm_freelist *fl;
643 struct vm_freelist *alt;
647 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
648 fl = &vm_phys_free_queues[domain][flind][pool][0];
649 for (oind = order; oind < VM_NFREEORDER; oind++) {
650 m = TAILQ_FIRST(&fl[oind].pl);
652 vm_freelist_rem(fl, m, oind);
653 vm_phys_split_pages(m, oind, fl, order);
659 * The given pool was empty. Find the largest
660 * contiguous, power-of-two-sized set of pages in any
661 * pool. Transfer these pages to the given pool, and
662 * use them to satisfy the allocation.
664 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
665 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
666 alt = &vm_phys_free_queues[domain][flind][pind][0];
667 m = TAILQ_FIRST(&alt[oind].pl);
669 vm_freelist_rem(alt, m, oind);
670 vm_phys_set_pool(pool, m, oind);
671 vm_phys_split_pages(m, oind, fl, order);
680 * Find the vm_page corresponding to the given physical address.
683 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
685 struct vm_phys_seg *seg;
688 for (segind = 0; segind < vm_phys_nsegs; segind++) {
689 seg = &vm_phys_segs[segind];
690 if (pa >= seg->start && pa < seg->end)
691 return (&seg->first_page[atop(pa - seg->start)]);
697 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
699 struct vm_phys_fictitious_seg tmp, *seg;
706 rw_rlock(&vm_phys_fictitious_reg_lock);
707 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
708 rw_runlock(&vm_phys_fictitious_reg_lock);
712 m = &seg->first_page[atop(pa - seg->start)];
713 KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m));
719 vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start,
720 long page_count, vm_memattr_t memattr)
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 * Find the segment containing the given physical address.
882 vm_phys_paddr_to_segind(vm_paddr_t pa)
884 struct vm_phys_seg *seg;
887 for (segind = 0; segind < vm_phys_nsegs; segind++) {
888 seg = &vm_phys_segs[segind];
889 if (pa >= seg->start && pa < seg->end)
892 panic("vm_phys_paddr_to_segind: paddr %#jx is not in any segment" ,
897 * Free a contiguous, power of two-sized set of physical pages.
899 * The free page queues must be locked.
902 vm_phys_free_pages(vm_page_t m, int order)
904 struct vm_freelist *fl;
905 struct vm_phys_seg *seg;
909 KASSERT(m->order == VM_NFREEORDER,
910 ("vm_phys_free_pages: page %p has unexpected order %d",
912 KASSERT(m->pool < VM_NFREEPOOL,
913 ("vm_phys_free_pages: page %p has unexpected pool %d",
915 KASSERT(order < VM_NFREEORDER,
916 ("vm_phys_free_pages: order %d is out of range", order));
917 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
918 seg = &vm_phys_segs[m->segind];
919 if (order < VM_NFREEORDER - 1) {
920 pa = VM_PAGE_TO_PHYS(m);
922 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
923 if (pa < seg->start || pa >= seg->end)
925 m_buddy = &seg->first_page[atop(pa - seg->start)];
926 if (m_buddy->order != order)
928 fl = (*seg->free_queues)[m_buddy->pool];
929 vm_freelist_rem(fl, m_buddy, order);
930 if (m_buddy->pool != m->pool)
931 vm_phys_set_pool(m->pool, m_buddy, order);
933 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
934 m = &seg->first_page[atop(pa - seg->start)];
935 } while (order < VM_NFREEORDER - 1);
937 fl = (*seg->free_queues)[m->pool];
938 vm_freelist_add(fl, m, order, 1);
942 * Free a contiguous, arbitrarily sized set of physical pages.
944 * The free page queues must be locked.
947 vm_phys_free_contig(vm_page_t m, u_long npages)
953 * Avoid unnecessary coalescing by freeing the pages in the largest
954 * possible power-of-two-sized subsets.
956 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
957 for (;; npages -= n) {
959 * Unsigned "min" is used here so that "order" is assigned
960 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
961 * or the low-order bits of its physical address are zero
962 * because the size of a physical address exceeds the size of
965 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
970 vm_phys_free_pages(m, order);
973 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
974 for (; npages > 0; npages -= n) {
975 order = flsl(npages) - 1;
977 vm_phys_free_pages(m, order);
983 * Set the pool for a contiguous, power of two-sized set of physical pages.
986 vm_phys_set_pool(int pool, vm_page_t m, int order)
990 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
995 * Search for the given physical page "m" in the free lists. If the search
996 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
997 * FALSE, indicating that "m" is not in the free lists.
999 * The free page queues must be locked.
1002 vm_phys_unfree_page(vm_page_t m)
1004 struct vm_freelist *fl;
1005 struct vm_phys_seg *seg;
1006 vm_paddr_t pa, pa_half;
1007 vm_page_t m_set, m_tmp;
1010 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1013 * First, find the contiguous, power of two-sized set of free
1014 * physical pages containing the given physical page "m" and
1015 * assign it to "m_set".
1017 seg = &vm_phys_segs[m->segind];
1018 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
1019 order < VM_NFREEORDER - 1; ) {
1021 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
1022 if (pa >= seg->start)
1023 m_set = &seg->first_page[atop(pa - seg->start)];
1027 if (m_set->order < order)
1029 if (m_set->order == VM_NFREEORDER)
1031 KASSERT(m_set->order < VM_NFREEORDER,
1032 ("vm_phys_unfree_page: page %p has unexpected order %d",
1033 m_set, m_set->order));
1036 * Next, remove "m_set" from the free lists. Finally, extract
1037 * "m" from "m_set" using an iterative algorithm: While "m_set"
1038 * is larger than a page, shrink "m_set" by returning the half
1039 * of "m_set" that does not contain "m" to the free lists.
1041 fl = (*seg->free_queues)[m_set->pool];
1042 order = m_set->order;
1043 vm_freelist_rem(fl, m_set, order);
1046 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
1047 if (m->phys_addr < pa_half)
1048 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
1051 m_set = &seg->first_page[atop(pa_half - seg->start)];
1053 vm_freelist_add(fl, m_tmp, order, 0);
1055 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
1060 * Try to zero one physical page. Used by an idle priority thread.
1063 vm_phys_zero_pages_idle(void)
1065 static struct vm_freelist *fl;
1066 static int flind, oind, pind;
1070 domain = vm_rr_selectdomain();
1071 fl = vm_phys_free_queues[domain][0][0];
1072 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1074 TAILQ_FOREACH_REVERSE(m, &fl[oind].pl, pglist, plinks.q) {
1075 for (m_tmp = m; m_tmp < &m[1 << oind]; m_tmp++) {
1076 if ((m_tmp->flags & (PG_CACHED | PG_ZERO)) == 0) {
1077 vm_phys_unfree_page(m_tmp);
1078 vm_phys_freecnt_adj(m, -1);
1079 mtx_unlock(&vm_page_queue_free_mtx);
1080 pmap_zero_page_idle(m_tmp);
1081 m_tmp->flags |= PG_ZERO;
1082 mtx_lock(&vm_page_queue_free_mtx);
1083 vm_phys_freecnt_adj(m, 1);
1084 vm_phys_free_pages(m_tmp, 0);
1085 vm_page_zero_count++;
1092 if (oind == VM_NFREEORDER) {
1095 if (pind == VM_NFREEPOOL) {
1098 if (flind == vm_nfreelists)
1101 fl = vm_phys_free_queues[domain][flind][pind];
1107 * Allocate a contiguous set of physical pages of the given size
1108 * "npages" from the free lists. All of the physical pages must be at
1109 * or above the given physical address "low" and below the given
1110 * physical address "high". The given value "alignment" determines the
1111 * alignment of the first physical page in the set. If the given value
1112 * "boundary" is non-zero, then the set of physical pages cannot cross
1113 * any physical address boundary that is a multiple of that value. Both
1114 * "alignment" and "boundary" must be a power of two.
1117 vm_phys_alloc_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
1118 u_long alignment, vm_paddr_t boundary)
1120 struct vm_freelist *fl;
1121 struct vm_phys_seg *seg;
1122 vm_paddr_t pa, pa_last, size;
1125 int dom, domain, flind, oind, order, pind;
1127 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1128 size = npages << PAGE_SHIFT;
1130 ("vm_phys_alloc_contig: size must not be 0"));
1131 KASSERT((alignment & (alignment - 1)) == 0,
1132 ("vm_phys_alloc_contig: alignment must be a power of 2"));
1133 KASSERT((boundary & (boundary - 1)) == 0,
1134 ("vm_phys_alloc_contig: boundary must be a power of 2"));
1135 /* Compute the queue that is the best fit for npages. */
1136 for (order = 0; (1 << order) < npages; order++);
1139 domain = vm_rr_selectdomain();
1140 for (flind = 0; flind < vm_nfreelists; flind++) {
1141 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER; oind++) {
1142 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1143 fl = &vm_phys_free_queues[domain][flind][pind][0];
1144 TAILQ_FOREACH(m_ret, &fl[oind].pl, plinks.q) {
1146 * A free list may contain physical pages
1147 * from one or more segments.
1149 seg = &vm_phys_segs[m_ret->segind];
1150 if (seg->start > high ||
1155 * Is the size of this allocation request
1156 * larger than the largest block size?
1158 if (order >= VM_NFREEORDER) {
1160 * Determine if a sufficient number
1161 * of subsequent blocks to satisfy
1162 * the allocation request are free.
1164 pa = VM_PAGE_TO_PHYS(m_ret);
1165 pa_last = pa + size;
1167 pa += 1 << (PAGE_SHIFT + VM_NFREEORDER - 1);
1170 if (pa < seg->start ||
1173 m = &seg->first_page[atop(pa - seg->start)];
1174 if (m->order != VM_NFREEORDER - 1)
1177 /* If not, continue to the next block. */
1183 * Determine if the blocks are within the given range,
1184 * satisfy the given alignment, and do not cross the
1187 pa = VM_PAGE_TO_PHYS(m_ret);
1189 pa + size <= high &&
1190 (pa & (alignment - 1)) == 0 &&
1191 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) == 0)
1197 if (++dom < vm_ndomains)
1201 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
1202 fl = (*seg->free_queues)[m->pool];
1203 vm_freelist_rem(fl, m, m->order);
1205 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
1206 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
1207 fl = (*seg->free_queues)[m_ret->pool];
1208 vm_phys_split_pages(m_ret, oind, fl, order);
1209 /* Return excess pages to the free lists. */
1210 npages_end = roundup2(npages, 1 << imin(oind, order));
1211 if (npages < npages_end)
1212 vm_phys_free_contig(&m_ret[npages], npages_end - npages);
1218 * Show the number of physical pages in each of the free lists.
1220 DB_SHOW_COMMAND(freepages, db_show_freepages)
1222 struct vm_freelist *fl;
1223 int flind, oind, pind, dom;
1225 for (dom = 0; dom < vm_ndomains; dom++) {
1226 db_printf("DOMAIN: %d\n", dom);
1227 for (flind = 0; flind < vm_nfreelists; flind++) {
1228 db_printf("FREE LIST %d:\n"
1229 "\n ORDER (SIZE) | NUMBER"
1231 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1232 db_printf(" | POOL %d", pind);
1234 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1235 db_printf("-- -- ");
1237 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
1238 db_printf(" %2.2d (%6.6dK)", oind,
1239 1 << (PAGE_SHIFT - 10 + oind));
1240 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1241 fl = vm_phys_free_queues[dom][flind][pind];
1242 db_printf(" | %6.6d", fl[oind].lcnt);