]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_phys.c
Add UPDATING entries and bump version.
[FreeBSD/FreeBSD.git] / sys / vm / vm_phys.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002-2006 Rice University
5  * Copyright (c) 2007 Alan L. Cox <alc@cs.rice.edu>
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Alan L. Cox,
9  * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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.
32  */
33
34 /*
35  *      Physical memory system implementation
36  *
37  * Any external functions defined by this module are only to be used by the
38  * virtual memory system.
39  */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include "opt_ddb.h"
45 #include "opt_vm.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/domainset.h>
50 #include <sys/lock.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/queue.h>
56 #include <sys/rwlock.h>
57 #include <sys/sbuf.h>
58 #include <sys/sysctl.h>
59 #include <sys/tree.h>
60 #include <sys/vmmeter.h>
61 #include <sys/seq.h>
62
63 #include <ddb/ddb.h>
64
65 #include <vm/vm.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_phys.h>
71 #include <vm/vm_pagequeue.h>
72
73 _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX,
74     "Too many physsegs.");
75
76 #ifdef NUMA
77 struct mem_affinity __read_mostly *mem_affinity;
78 int __read_mostly *mem_locality;
79 #endif
80
81 int __read_mostly vm_ndomains = 1;
82 domainset_t __read_mostly all_domains = DOMAINSET_T_INITIALIZER(0x1);
83
84 struct vm_phys_seg __read_mostly vm_phys_segs[VM_PHYSSEG_MAX];
85 int __read_mostly vm_phys_nsegs;
86
87 struct vm_phys_fictitious_seg;
88 static int vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *,
89     struct vm_phys_fictitious_seg *);
90
91 RB_HEAD(fict_tree, vm_phys_fictitious_seg) vm_phys_fictitious_tree =
92     RB_INITIALIZER(_vm_phys_fictitious_tree);
93
94 struct vm_phys_fictitious_seg {
95         RB_ENTRY(vm_phys_fictitious_seg) node;
96         /* Memory region data */
97         vm_paddr_t      start;
98         vm_paddr_t      end;
99         vm_page_t       first_page;
100 };
101
102 RB_GENERATE_STATIC(fict_tree, vm_phys_fictitious_seg, node,
103     vm_phys_fictitious_cmp);
104
105 static struct rwlock_padalign vm_phys_fictitious_reg_lock;
106 MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages");
107
108 static struct vm_freelist __aligned(CACHE_LINE_SIZE)
109     vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL]
110     [VM_NFREEORDER_MAX];
111
112 static int __read_mostly vm_nfreelists;
113
114 /*
115  * Provides the mapping from VM_FREELIST_* to free list indices (flind).
116  */
117 static int __read_mostly vm_freelist_to_flind[VM_NFREELIST];
118
119 CTASSERT(VM_FREELIST_DEFAULT == 0);
120
121 #ifdef VM_FREELIST_DMA32
122 #define VM_DMA32_BOUNDARY       ((vm_paddr_t)1 << 32)
123 #endif
124
125 /*
126  * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about
127  * the ordering of the free list boundaries.
128  */
129 #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY)
130 CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY);
131 #endif
132
133 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
134 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
135     NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
136
137 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
138 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
139     NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
140
141 #ifdef NUMA
142 static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS);
143 SYSCTL_OID(_vm, OID_AUTO, phys_locality, CTLTYPE_STRING | CTLFLAG_RD,
144     NULL, 0, sysctl_vm_phys_locality, "A", "Phys Locality Info");
145 #endif
146
147 SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
148     &vm_ndomains, 0, "Number of physical memory domains available.");
149
150 static vm_page_t vm_phys_alloc_seg_contig(struct vm_phys_seg *seg,
151     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
152     vm_paddr_t boundary);
153 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain);
154 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end);
155 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
156     int order, int tail);
157
158 /*
159  * Red-black tree helpers for vm fictitious range management.
160  */
161 static inline int
162 vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p,
163     struct vm_phys_fictitious_seg *range)
164 {
165
166         KASSERT(range->start != 0 && range->end != 0,
167             ("Invalid range passed on search for vm_fictitious page"));
168         if (p->start >= range->end)
169                 return (1);
170         if (p->start < range->start)
171                 return (-1);
172
173         return (0);
174 }
175
176 static int
177 vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1,
178     struct vm_phys_fictitious_seg *p2)
179 {
180
181         /* Check if this is a search for a page */
182         if (p1->end == 0)
183                 return (vm_phys_fictitious_in_range(p1, p2));
184
185         KASSERT(p2->end != 0,
186     ("Invalid range passed as second parameter to vm fictitious comparison"));
187
188         /* Searching to add a new range */
189         if (p1->end <= p2->start)
190                 return (-1);
191         if (p1->start >= p2->end)
192                 return (1);
193
194         panic("Trying to add overlapping vm fictitious ranges:\n"
195             "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start,
196             (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end);
197 }
198
199 int
200 vm_phys_domain_match(int prefer, vm_paddr_t low, vm_paddr_t high)
201 {
202 #ifdef NUMA
203         domainset_t mask;
204         int i;
205
206         if (vm_ndomains == 1 || mem_affinity == NULL)
207                 return (0);
208
209         DOMAINSET_ZERO(&mask);
210         /*
211          * Check for any memory that overlaps low, high.
212          */
213         for (i = 0; mem_affinity[i].end != 0; i++)
214                 if (mem_affinity[i].start <= high &&
215                     mem_affinity[i].end >= low)
216                         DOMAINSET_SET(mem_affinity[i].domain, &mask);
217         if (prefer != -1 && DOMAINSET_ISSET(prefer, &mask))
218                 return (prefer);
219         if (DOMAINSET_EMPTY(&mask))
220                 panic("vm_phys_domain_match:  Impossible constraint");
221         return (DOMAINSET_FFS(&mask) - 1);
222 #else
223         return (0);
224 #endif
225 }
226
227 /*
228  * Outputs the state of the physical memory allocator, specifically,
229  * the amount of physical memory in each free list.
230  */
231 static int
232 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
233 {
234         struct sbuf sbuf;
235         struct vm_freelist *fl;
236         int dom, error, flind, oind, pind;
237
238         error = sysctl_wire_old_buffer(req, 0);
239         if (error != 0)
240                 return (error);
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"
247                             "\n              ", flind);
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",
260                                             fl[oind].lcnt);
261                                 }
262                                 sbuf_printf(&sbuf, "\n");
263                         }
264                 }
265         }
266         error = sbuf_finish(&sbuf);
267         sbuf_delete(&sbuf);
268         return (error);
269 }
270
271 /*
272  * Outputs the set of physical memory segments.
273  */
274 static int
275 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
276 {
277         struct sbuf sbuf;
278         struct vm_phys_seg *seg;
279         int error, segind;
280
281         error = sysctl_wire_old_buffer(req, 0);
282         if (error != 0)
283                 return (error);
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);
294         }
295         error = sbuf_finish(&sbuf);
296         sbuf_delete(&sbuf);
297         return (error);
298 }
299
300 /*
301  * Return affinity, or -1 if there's no affinity information.
302  */
303 int
304 vm_phys_mem_affinity(int f, int t)
305 {
306
307 #ifdef NUMA
308         if (mem_locality == NULL)
309                 return (-1);
310         if (f >= vm_ndomains || t >= vm_ndomains)
311                 return (-1);
312         return (mem_locality[f * vm_ndomains + t]);
313 #else
314         return (-1);
315 #endif
316 }
317
318 #ifdef NUMA
319 /*
320  * Outputs the VM locality table.
321  */
322 static int
323 sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS)
324 {
325         struct sbuf sbuf;
326         int error, i, j;
327
328         error = sysctl_wire_old_buffer(req, 0);
329         if (error != 0)
330                 return (error);
331         sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
332
333         sbuf_printf(&sbuf, "\n");
334
335         for (i = 0; i < vm_ndomains; i++) {
336                 sbuf_printf(&sbuf, "%d: ", i);
337                 for (j = 0; j < vm_ndomains; j++) {
338                         sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j));
339                 }
340                 sbuf_printf(&sbuf, "\n");
341         }
342         error = sbuf_finish(&sbuf);
343         sbuf_delete(&sbuf);
344         return (error);
345 }
346 #endif
347
348 static void
349 vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
350 {
351
352         m->order = order;
353         if (tail)
354                 TAILQ_INSERT_TAIL(&fl[order].pl, m, listq);
355         else
356                 TAILQ_INSERT_HEAD(&fl[order].pl, m, listq);
357         fl[order].lcnt++;
358 }
359
360 static void
361 vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
362 {
363
364         TAILQ_REMOVE(&fl[order].pl, m, listq);
365         fl[order].lcnt--;
366         m->order = VM_NFREEORDER;
367 }
368
369 /*
370  * Create a physical memory segment.
371  */
372 static void
373 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain)
374 {
375         struct vm_phys_seg *seg;
376
377         KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
378             ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
379         KASSERT(domain >= 0 && domain < vm_ndomains,
380             ("vm_phys_create_seg: invalid domain provided"));
381         seg = &vm_phys_segs[vm_phys_nsegs++];
382         while (seg > vm_phys_segs && (seg - 1)->start >= end) {
383                 *seg = *(seg - 1);
384                 seg--;
385         }
386         seg->start = start;
387         seg->end = end;
388         seg->domain = domain;
389 }
390
391 static void
392 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end)
393 {
394 #ifdef NUMA
395         int i;
396
397         if (mem_affinity == NULL) {
398                 _vm_phys_create_seg(start, end, 0);
399                 return;
400         }
401
402         for (i = 0;; i++) {
403                 if (mem_affinity[i].end == 0)
404                         panic("Reached end of affinity info");
405                 if (mem_affinity[i].end <= start)
406                         continue;
407                 if (mem_affinity[i].start > start)
408                         panic("No affinity info for start %jx",
409                             (uintmax_t)start);
410                 if (mem_affinity[i].end >= end) {
411                         _vm_phys_create_seg(start, end,
412                             mem_affinity[i].domain);
413                         break;
414                 }
415                 _vm_phys_create_seg(start, mem_affinity[i].end,
416                     mem_affinity[i].domain);
417                 start = mem_affinity[i].end;
418         }
419 #else
420         _vm_phys_create_seg(start, end, 0);
421 #endif
422 }
423
424 /*
425  * Add a physical memory segment.
426  */
427 void
428 vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
429 {
430         vm_paddr_t paddr;
431
432         KASSERT((start & PAGE_MASK) == 0,
433             ("vm_phys_define_seg: start is not page aligned"));
434         KASSERT((end & PAGE_MASK) == 0,
435             ("vm_phys_define_seg: end is not page aligned"));
436
437         /*
438          * Split the physical memory segment if it spans two or more free
439          * list boundaries.
440          */
441         paddr = start;
442 #ifdef  VM_FREELIST_LOWMEM
443         if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) {
444                 vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY);
445                 paddr = VM_LOWMEM_BOUNDARY;
446         }
447 #endif
448 #ifdef  VM_FREELIST_DMA32
449         if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) {
450                 vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY);
451                 paddr = VM_DMA32_BOUNDARY;
452         }
453 #endif
454         vm_phys_create_seg(paddr, end);
455 }
456
457 /*
458  * Initialize the physical memory allocator.
459  *
460  * Requires that vm_page_array is initialized!
461  */
462 void
463 vm_phys_init(void)
464 {
465         struct vm_freelist *fl;
466         struct vm_phys_seg *end_seg, *prev_seg, *seg, *tmp_seg;
467         u_long npages;
468         int dom, flind, freelist, oind, pind, segind;
469
470         /*
471          * Compute the number of free lists, and generate the mapping from the
472          * manifest constants VM_FREELIST_* to the free list indices.
473          *
474          * Initially, the entries of vm_freelist_to_flind[] are set to either
475          * 0 or 1 to indicate which free lists should be created.
476          */
477         npages = 0;
478         for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
479                 seg = &vm_phys_segs[segind];
480 #ifdef  VM_FREELIST_LOWMEM
481                 if (seg->end <= VM_LOWMEM_BOUNDARY)
482                         vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1;
483                 else
484 #endif
485 #ifdef  VM_FREELIST_DMA32
486                 if (
487 #ifdef  VM_DMA32_NPAGES_THRESHOLD
488                     /*
489                      * Create the DMA32 free list only if the amount of
490                      * physical memory above physical address 4G exceeds the
491                      * given threshold.
492                      */
493                     npages > VM_DMA32_NPAGES_THRESHOLD &&
494 #endif
495                     seg->end <= VM_DMA32_BOUNDARY)
496                         vm_freelist_to_flind[VM_FREELIST_DMA32] = 1;
497                 else
498 #endif
499                 {
500                         npages += atop(seg->end - seg->start);
501                         vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1;
502                 }
503         }
504         /* Change each entry into a running total of the free lists. */
505         for (freelist = 1; freelist < VM_NFREELIST; freelist++) {
506                 vm_freelist_to_flind[freelist] +=
507                     vm_freelist_to_flind[freelist - 1];
508         }
509         vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1];
510         KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists"));
511         /* Change each entry into a free list index. */
512         for (freelist = 0; freelist < VM_NFREELIST; freelist++)
513                 vm_freelist_to_flind[freelist]--;
514
515         /*
516          * Initialize the first_page and free_queues fields of each physical
517          * memory segment.
518          */
519 #ifdef VM_PHYSSEG_SPARSE
520         npages = 0;
521 #endif
522         for (segind = 0; segind < vm_phys_nsegs; segind++) {
523                 seg = &vm_phys_segs[segind];
524 #ifdef VM_PHYSSEG_SPARSE
525                 seg->first_page = &vm_page_array[npages];
526                 npages += atop(seg->end - seg->start);
527 #else
528                 seg->first_page = PHYS_TO_VM_PAGE(seg->start);
529 #endif
530 #ifdef  VM_FREELIST_LOWMEM
531                 if (seg->end <= VM_LOWMEM_BOUNDARY) {
532                         flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM];
533                         KASSERT(flind >= 0,
534                             ("vm_phys_init: LOWMEM flind < 0"));
535                 } else
536 #endif
537 #ifdef  VM_FREELIST_DMA32
538                 if (seg->end <= VM_DMA32_BOUNDARY) {
539                         flind = vm_freelist_to_flind[VM_FREELIST_DMA32];
540                         KASSERT(flind >= 0,
541                             ("vm_phys_init: DMA32 flind < 0"));
542                 } else
543 #endif
544                 {
545                         flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT];
546                         KASSERT(flind >= 0,
547                             ("vm_phys_init: DEFAULT flind < 0"));
548                 }
549                 seg->free_queues = &vm_phys_free_queues[seg->domain][flind];
550         }
551
552         /*
553          * Coalesce physical memory segments that are contiguous and share the
554          * same per-domain free queues.
555          */
556         prev_seg = vm_phys_segs;
557         seg = &vm_phys_segs[1];
558         end_seg = &vm_phys_segs[vm_phys_nsegs];
559         while (seg < end_seg) {
560                 if (prev_seg->end == seg->start &&
561                     prev_seg->free_queues == seg->free_queues) {
562                         prev_seg->end = seg->end;
563                         KASSERT(prev_seg->domain == seg->domain,
564                             ("vm_phys_init: free queues cannot span domains"));
565                         vm_phys_nsegs--;
566                         end_seg--;
567                         for (tmp_seg = seg; tmp_seg < end_seg; tmp_seg++)
568                                 *tmp_seg = *(tmp_seg + 1);
569                 } else {
570                         prev_seg = seg;
571                         seg++;
572                 }
573         }
574
575         /*
576          * Initialize the free queues.
577          */
578         for (dom = 0; dom < vm_ndomains; dom++) {
579                 for (flind = 0; flind < vm_nfreelists; flind++) {
580                         for (pind = 0; pind < VM_NFREEPOOL; pind++) {
581                                 fl = vm_phys_free_queues[dom][flind][pind];
582                                 for (oind = 0; oind < VM_NFREEORDER; oind++)
583                                         TAILQ_INIT(&fl[oind].pl);
584                         }
585                 }
586         }
587
588         rw_init(&vm_phys_fictitious_reg_lock, "vmfctr");
589 }
590
591 /*
592  * Register info about the NUMA topology of the system.
593  *
594  * Invoked by platform-dependent code prior to vm_phys_init().
595  */
596 void
597 vm_phys_register_domains(int ndomains, struct mem_affinity *affinity,
598     int *locality)
599 {
600 #ifdef NUMA
601         int d, i;
602
603         /*
604          * For now the only override value that we support is 1, which
605          * effectively disables NUMA-awareness in the allocators.
606          */
607         d = 0;
608         TUNABLE_INT_FETCH("vm.numa.disabled", &d);
609         if (d)
610                 ndomains = 1;
611
612         if (ndomains > 1) {
613                 vm_ndomains = ndomains;
614                 mem_affinity = affinity;
615                 mem_locality = locality;
616         }
617
618         for (i = 0; i < vm_ndomains; i++)
619                 DOMAINSET_SET(i, &all_domains);
620 #else
621         (void)ndomains;
622         (void)affinity;
623         (void)locality;
624 #endif
625 }
626
627 int
628 _vm_phys_domain(vm_paddr_t pa)
629 {
630 #ifdef NUMA
631         int i;
632
633         if (vm_ndomains == 1 || mem_affinity == NULL)
634                 return (0);
635
636         /*
637          * Check for any memory that overlaps.
638          */
639         for (i = 0; mem_affinity[i].end != 0; i++)
640                 if (mem_affinity[i].start <= pa &&
641                     mem_affinity[i].end >= pa)
642                         return (mem_affinity[i].domain);
643 #endif
644         return (0);
645 }
646
647 /*
648  * Split a contiguous, power of two-sized set of physical pages.
649  *
650  * When this function is called by a page allocation function, the caller
651  * should request insertion at the head unless the order [order, oind) queues
652  * are known to be empty.  The objective being to reduce the likelihood of
653  * long-term fragmentation by promoting contemporaneous allocation and
654  * (hopefully) deallocation.
655  */
656 static __inline void
657 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order,
658     int tail)
659 {
660         vm_page_t m_buddy;
661
662         while (oind > order) {
663                 oind--;
664                 m_buddy = &m[1 << oind];
665                 KASSERT(m_buddy->order == VM_NFREEORDER,
666                     ("vm_phys_split_pages: page %p has unexpected order %d",
667                     m_buddy, m_buddy->order));
668                 vm_freelist_add(fl, m_buddy, oind, tail);
669         }
670 }
671
672 /*
673  * Add the physical pages [m, m + npages) at the end of a power-of-two aligned
674  * and sized set to the specified free list.
675  *
676  * When this function is called by a page allocation function, the caller
677  * should request insertion at the head unless the lower-order queues are
678  * known to be empty.  The objective being to reduce the likelihood of long-
679  * term fragmentation by promoting contemporaneous allocation and (hopefully)
680  * deallocation.
681  *
682  * The physical page m's buddy must not be free.
683  */
684 static void
685 vm_phys_enq_range(vm_page_t m, u_int npages, struct vm_freelist *fl, int tail)
686 {
687         u_int n;
688         int order;
689
690         KASSERT(npages > 0, ("vm_phys_enq_range: npages is 0"));
691         KASSERT(((VM_PAGE_TO_PHYS(m) + npages * PAGE_SIZE) &
692             ((PAGE_SIZE << (fls(npages) - 1)) - 1)) == 0,
693             ("vm_phys_enq_range: page %p and npages %u are misaligned",
694             m, npages));
695         do {
696                 KASSERT(m->order == VM_NFREEORDER,
697                     ("vm_phys_enq_range: page %p has unexpected order %d",
698                     m, m->order));
699                 order = ffs(npages) - 1;
700                 KASSERT(order < VM_NFREEORDER,
701                     ("vm_phys_enq_range: order %d is out of range", order));
702                 vm_freelist_add(fl, m, order, tail);
703                 n = 1 << order;
704                 m += n;
705                 npages -= n;
706         } while (npages > 0);
707 }
708
709 /*
710  * Tries to allocate the specified number of pages from the specified pool
711  * within the specified domain.  Returns the actual number of allocated pages
712  * and a pointer to each page through the array ma[].
713  *
714  * The returned pages may not be physically contiguous.  However, in contrast
715  * to performing multiple, back-to-back calls to vm_phys_alloc_pages(..., 0),
716  * calling this function once to allocate the desired number of pages will
717  * avoid wasted time in vm_phys_split_pages().
718  *
719  * The free page queues for the specified domain must be locked.
720  */
721 int
722 vm_phys_alloc_npages(int domain, int pool, int npages, vm_page_t ma[])
723 {
724         struct vm_freelist *alt, *fl;
725         vm_page_t m;
726         int avail, end, flind, freelist, i, need, oind, pind;
727
728         KASSERT(domain >= 0 && domain < vm_ndomains,
729             ("vm_phys_alloc_npages: domain %d is out of range", domain));
730         KASSERT(pool < VM_NFREEPOOL,
731             ("vm_phys_alloc_npages: pool %d is out of range", pool));
732         KASSERT(npages <= 1 << (VM_NFREEORDER - 1),
733             ("vm_phys_alloc_npages: npages %d is out of range", npages));
734         vm_domain_free_assert_locked(VM_DOMAIN(domain));
735         i = 0;
736         for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
737                 flind = vm_freelist_to_flind[freelist];
738                 if (flind < 0)
739                         continue;
740                 fl = vm_phys_free_queues[domain][flind][pool];
741                 for (oind = 0; oind < VM_NFREEORDER; oind++) {
742                         while ((m = TAILQ_FIRST(&fl[oind].pl)) != NULL) {
743                                 vm_freelist_rem(fl, m, oind);
744                                 avail = 1 << oind;
745                                 need = imin(npages - i, avail);
746                                 for (end = i + need; i < end;)
747                                         ma[i++] = m++;
748                                 if (need < avail) {
749                                         /*
750                                          * Return excess pages to fl.  Its
751                                          * order [0, oind) queues are empty.
752                                          */
753                                         vm_phys_enq_range(m, avail - need, fl,
754                                             1);
755                                         return (npages);
756                                 } else if (i == npages)
757                                         return (npages);
758                         }
759                 }
760                 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
761                         for (pind = 0; pind < VM_NFREEPOOL; pind++) {
762                                 alt = vm_phys_free_queues[domain][flind][pind];
763                                 while ((m = TAILQ_FIRST(&alt[oind].pl)) !=
764                                     NULL) {
765                                         vm_freelist_rem(alt, m, oind);
766                                         vm_phys_set_pool(pool, m, oind);
767                                         avail = 1 << oind;
768                                         need = imin(npages - i, avail);
769                                         for (end = i + need; i < end;)
770                                                 ma[i++] = m++;
771                                         if (need < avail) {
772                                                 /*
773                                                  * Return excess pages to fl.
774                                                  * Its order [0, oind) queues
775                                                  * are empty.
776                                                  */
777                                                 vm_phys_enq_range(m, avail -
778                                                     need, fl, 1);
779                                                 return (npages);
780                                         } else if (i == npages)
781                                                 return (npages);
782                                 }
783                         }
784                 }
785         }
786         return (i);
787 }
788
789 /*
790  * Allocate a contiguous, power of two-sized set of physical pages
791  * from the free lists.
792  *
793  * The free page queues must be locked.
794  */
795 vm_page_t
796 vm_phys_alloc_pages(int domain, int pool, int order)
797 {
798         vm_page_t m;
799         int freelist;
800
801         for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
802                 m = vm_phys_alloc_freelist_pages(domain, freelist, pool, order);
803                 if (m != NULL)
804                         return (m);
805         }
806         return (NULL);
807 }
808
809 /*
810  * Allocate a contiguous, power of two-sized set of physical pages from the
811  * specified free list.  The free list must be specified using one of the
812  * manifest constants VM_FREELIST_*.
813  *
814  * The free page queues must be locked.
815  */
816 vm_page_t
817 vm_phys_alloc_freelist_pages(int domain, int freelist, int pool, int order)
818 {
819         struct vm_freelist *alt, *fl;
820         vm_page_t m;
821         int oind, pind, flind;
822
823         KASSERT(domain >= 0 && domain < vm_ndomains,
824             ("vm_phys_alloc_freelist_pages: domain %d is out of range",
825             domain));
826         KASSERT(freelist < VM_NFREELIST,
827             ("vm_phys_alloc_freelist_pages: freelist %d is out of range",
828             freelist));
829         KASSERT(pool < VM_NFREEPOOL,
830             ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
831         KASSERT(order < VM_NFREEORDER,
832             ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
833
834         flind = vm_freelist_to_flind[freelist];
835         /* Check if freelist is present */
836         if (flind < 0)
837                 return (NULL);
838
839         vm_domain_free_assert_locked(VM_DOMAIN(domain));
840         fl = &vm_phys_free_queues[domain][flind][pool][0];
841         for (oind = order; oind < VM_NFREEORDER; oind++) {
842                 m = TAILQ_FIRST(&fl[oind].pl);
843                 if (m != NULL) {
844                         vm_freelist_rem(fl, m, oind);
845                         /* The order [order, oind) queues are empty. */
846                         vm_phys_split_pages(m, oind, fl, order, 1);
847                         return (m);
848                 }
849         }
850
851         /*
852          * The given pool was empty.  Find the largest
853          * contiguous, power-of-two-sized set of pages in any
854          * pool.  Transfer these pages to the given pool, and
855          * use them to satisfy the allocation.
856          */
857         for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
858                 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
859                         alt = &vm_phys_free_queues[domain][flind][pind][0];
860                         m = TAILQ_FIRST(&alt[oind].pl);
861                         if (m != NULL) {
862                                 vm_freelist_rem(alt, m, oind);
863                                 vm_phys_set_pool(pool, m, oind);
864                                 /* The order [order, oind) queues are empty. */
865                                 vm_phys_split_pages(m, oind, fl, order, 1);
866                                 return (m);
867                         }
868                 }
869         }
870         return (NULL);
871 }
872
873 /*
874  * Find the vm_page corresponding to the given physical address.
875  */
876 vm_page_t
877 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
878 {
879         struct vm_phys_seg *seg;
880         int segind;
881
882         for (segind = 0; segind < vm_phys_nsegs; segind++) {
883                 seg = &vm_phys_segs[segind];
884                 if (pa >= seg->start && pa < seg->end)
885                         return (&seg->first_page[atop(pa - seg->start)]);
886         }
887         return (NULL);
888 }
889
890 vm_page_t
891 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
892 {
893         struct vm_phys_fictitious_seg tmp, *seg;
894         vm_page_t m;
895
896         m = NULL;
897         tmp.start = pa;
898         tmp.end = 0;
899
900         rw_rlock(&vm_phys_fictitious_reg_lock);
901         seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
902         rw_runlock(&vm_phys_fictitious_reg_lock);
903         if (seg == NULL)
904                 return (NULL);
905
906         m = &seg->first_page[atop(pa - seg->start)];
907         KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m));
908
909         return (m);
910 }
911
912 static inline void
913 vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start,
914     long page_count, vm_memattr_t memattr)
915 {
916         long i;
917
918         bzero(range, page_count * sizeof(*range));
919         for (i = 0; i < page_count; i++) {
920                 vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr);
921                 range[i].oflags &= ~VPO_UNMANAGED;
922                 range[i].busy_lock = VPB_UNBUSIED;
923         }
924 }
925
926 int
927 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
928     vm_memattr_t memattr)
929 {
930         struct vm_phys_fictitious_seg *seg;
931         vm_page_t fp;
932         long page_count;
933 #ifdef VM_PHYSSEG_DENSE
934         long pi, pe;
935         long dpage_count;
936 #endif
937
938         KASSERT(start < end,
939             ("Start of segment isn't less than end (start: %jx end: %jx)",
940             (uintmax_t)start, (uintmax_t)end));
941
942         page_count = (end - start) / PAGE_SIZE;
943
944 #ifdef VM_PHYSSEG_DENSE
945         pi = atop(start);
946         pe = atop(end);
947         if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
948                 fp = &vm_page_array[pi - first_page];
949                 if ((pe - first_page) > vm_page_array_size) {
950                         /*
951                          * We have a segment that starts inside
952                          * of vm_page_array, but ends outside of it.
953                          *
954                          * Use vm_page_array pages for those that are
955                          * inside of the vm_page_array range, and
956                          * allocate the remaining ones.
957                          */
958                         dpage_count = vm_page_array_size - (pi - first_page);
959                         vm_phys_fictitious_init_range(fp, start, dpage_count,
960                             memattr);
961                         page_count -= dpage_count;
962                         start += ptoa(dpage_count);
963                         goto alloc;
964                 }
965                 /*
966                  * We can allocate the full range from vm_page_array,
967                  * so there's no need to register the range in the tree.
968                  */
969                 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
970                 return (0);
971         } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
972                 /*
973                  * We have a segment that ends inside of vm_page_array,
974                  * but starts outside of it.
975                  */
976                 fp = &vm_page_array[0];
977                 dpage_count = pe - first_page;
978                 vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count,
979                     memattr);
980                 end -= ptoa(dpage_count);
981                 page_count -= dpage_count;
982                 goto alloc;
983         } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
984                 /*
985                  * Trying to register a fictitious range that expands before
986                  * and after vm_page_array.
987                  */
988                 return (EINVAL);
989         } else {
990 alloc:
991 #endif
992                 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
993                     M_WAITOK);
994 #ifdef VM_PHYSSEG_DENSE
995         }
996 #endif
997         vm_phys_fictitious_init_range(fp, start, page_count, memattr);
998
999         seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO);
1000         seg->start = start;
1001         seg->end = end;
1002         seg->first_page = fp;
1003
1004         rw_wlock(&vm_phys_fictitious_reg_lock);
1005         RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg);
1006         rw_wunlock(&vm_phys_fictitious_reg_lock);
1007
1008         return (0);
1009 }
1010
1011 void
1012 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
1013 {
1014         struct vm_phys_fictitious_seg *seg, tmp;
1015 #ifdef VM_PHYSSEG_DENSE
1016         long pi, pe;
1017 #endif
1018
1019         KASSERT(start < end,
1020             ("Start of segment isn't less than end (start: %jx end: %jx)",
1021             (uintmax_t)start, (uintmax_t)end));
1022
1023 #ifdef VM_PHYSSEG_DENSE
1024         pi = atop(start);
1025         pe = atop(end);
1026         if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1027                 if ((pe - first_page) <= vm_page_array_size) {
1028                         /*
1029                          * This segment was allocated using vm_page_array
1030                          * only, there's nothing to do since those pages
1031                          * were never added to the tree.
1032                          */
1033                         return;
1034                 }
1035                 /*
1036                  * We have a segment that starts inside
1037                  * of vm_page_array, but ends outside of it.
1038                  *
1039                  * Calculate how many pages were added to the
1040                  * tree and free them.
1041                  */
1042                 start = ptoa(first_page + vm_page_array_size);
1043         } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
1044                 /*
1045                  * We have a segment that ends inside of vm_page_array,
1046                  * but starts outside of it.
1047                  */
1048                 end = ptoa(first_page);
1049         } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
1050                 /* Since it's not possible to register such a range, panic. */
1051                 panic(
1052                     "Unregistering not registered fictitious range [%#jx:%#jx]",
1053                     (uintmax_t)start, (uintmax_t)end);
1054         }
1055 #endif
1056         tmp.start = start;
1057         tmp.end = 0;
1058
1059         rw_wlock(&vm_phys_fictitious_reg_lock);
1060         seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
1061         if (seg->start != start || seg->end != end) {
1062                 rw_wunlock(&vm_phys_fictitious_reg_lock);
1063                 panic(
1064                     "Unregistering not registered fictitious range [%#jx:%#jx]",
1065                     (uintmax_t)start, (uintmax_t)end);
1066         }
1067         RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg);
1068         rw_wunlock(&vm_phys_fictitious_reg_lock);
1069         free(seg->first_page, M_FICT_PAGES);
1070         free(seg, M_FICT_PAGES);
1071 }
1072
1073 /*
1074  * Free a contiguous, power of two-sized set of physical pages.
1075  *
1076  * The free page queues must be locked.
1077  */
1078 void
1079 vm_phys_free_pages(vm_page_t m, int order)
1080 {
1081         struct vm_freelist *fl;
1082         struct vm_phys_seg *seg;
1083         vm_paddr_t pa;
1084         vm_page_t m_buddy;
1085
1086         KASSERT(m->order == VM_NFREEORDER,
1087             ("vm_phys_free_pages: page %p has unexpected order %d",
1088             m, m->order));
1089         KASSERT(m->pool < VM_NFREEPOOL,
1090             ("vm_phys_free_pages: page %p has unexpected pool %d",
1091             m, m->pool));
1092         KASSERT(order < VM_NFREEORDER,
1093             ("vm_phys_free_pages: order %d is out of range", order));
1094         seg = &vm_phys_segs[m->segind];
1095         vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1096         if (order < VM_NFREEORDER - 1) {
1097                 pa = VM_PAGE_TO_PHYS(m);
1098                 do {
1099                         pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
1100                         if (pa < seg->start || pa >= seg->end)
1101                                 break;
1102                         m_buddy = &seg->first_page[atop(pa - seg->start)];
1103                         if (m_buddy->order != order)
1104                                 break;
1105                         fl = (*seg->free_queues)[m_buddy->pool];
1106                         vm_freelist_rem(fl, m_buddy, order);
1107                         if (m_buddy->pool != m->pool)
1108                                 vm_phys_set_pool(m->pool, m_buddy, order);
1109                         order++;
1110                         pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
1111                         m = &seg->first_page[atop(pa - seg->start)];
1112                 } while (order < VM_NFREEORDER - 1);
1113         }
1114         fl = (*seg->free_queues)[m->pool];
1115         vm_freelist_add(fl, m, order, 1);
1116 }
1117
1118 /*
1119  * Free a contiguous, arbitrarily sized set of physical pages.
1120  *
1121  * The free page queues must be locked.
1122  */
1123 void
1124 vm_phys_free_contig(vm_page_t m, u_long npages)
1125 {
1126         u_int n;
1127         int order;
1128
1129         /*
1130          * Avoid unnecessary coalescing by freeing the pages in the largest
1131          * possible power-of-two-sized subsets.
1132          */
1133         vm_domain_free_assert_locked(vm_pagequeue_domain(m));
1134         for (;; npages -= n) {
1135                 /*
1136                  * Unsigned "min" is used here so that "order" is assigned
1137                  * "VM_NFREEORDER - 1" when "m"'s physical address is zero
1138                  * or the low-order bits of its physical address are zero
1139                  * because the size of a physical address exceeds the size of
1140                  * a long.
1141                  */
1142                 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
1143                     VM_NFREEORDER - 1);
1144                 n = 1 << order;
1145                 if (npages < n)
1146                         break;
1147                 vm_phys_free_pages(m, order);
1148                 m += n;
1149         }
1150         /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
1151         for (; npages > 0; npages -= n) {
1152                 order = flsl(npages) - 1;
1153                 n = 1 << order;
1154                 vm_phys_free_pages(m, order);
1155                 m += n;
1156         }
1157 }
1158
1159 /*
1160  * Scan physical memory between the specified addresses "low" and "high" for a
1161  * run of contiguous physical pages that satisfy the specified conditions, and
1162  * return the lowest page in the run.  The specified "alignment" determines
1163  * the alignment of the lowest physical page in the run.  If the specified
1164  * "boundary" is non-zero, then the run of physical pages cannot span a
1165  * physical address that is a multiple of "boundary".
1166  *
1167  * "npages" must be greater than zero.  Both "alignment" and "boundary" must
1168  * be a power of two.
1169  */
1170 vm_page_t
1171 vm_phys_scan_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1172     u_long alignment, vm_paddr_t boundary, int options)
1173 {
1174         vm_paddr_t pa_end;
1175         vm_page_t m_end, m_run, m_start;
1176         struct vm_phys_seg *seg;
1177         int segind;
1178
1179         KASSERT(npages > 0, ("npages is 0"));
1180         KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1181         KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1182         if (low >= high)
1183                 return (NULL);
1184         for (segind = 0; segind < vm_phys_nsegs; segind++) {
1185                 seg = &vm_phys_segs[segind];
1186                 if (seg->domain != domain)
1187                         continue;
1188                 if (seg->start >= high)
1189                         break;
1190                 if (low >= seg->end)
1191                         continue;
1192                 if (low <= seg->start)
1193                         m_start = seg->first_page;
1194                 else
1195                         m_start = &seg->first_page[atop(low - seg->start)];
1196                 if (high < seg->end)
1197                         pa_end = high;
1198                 else
1199                         pa_end = seg->end;
1200                 if (pa_end - VM_PAGE_TO_PHYS(m_start) < ptoa(npages))
1201                         continue;
1202                 m_end = &seg->first_page[atop(pa_end - seg->start)];
1203                 m_run = vm_page_scan_contig(npages, m_start, m_end,
1204                     alignment, boundary, options);
1205                 if (m_run != NULL)
1206                         return (m_run);
1207         }
1208         return (NULL);
1209 }
1210
1211 /*
1212  * Set the pool for a contiguous, power of two-sized set of physical pages. 
1213  */
1214 void
1215 vm_phys_set_pool(int pool, vm_page_t m, int order)
1216 {
1217         vm_page_t m_tmp;
1218
1219         for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
1220                 m_tmp->pool = pool;
1221 }
1222
1223 /*
1224  * Search for the given physical page "m" in the free lists.  If the search
1225  * succeeds, remove "m" from the free lists and return TRUE.  Otherwise, return
1226  * FALSE, indicating that "m" is not in the free lists.
1227  *
1228  * The free page queues must be locked.
1229  */
1230 boolean_t
1231 vm_phys_unfree_page(vm_page_t m)
1232 {
1233         struct vm_freelist *fl;
1234         struct vm_phys_seg *seg;
1235         vm_paddr_t pa, pa_half;
1236         vm_page_t m_set, m_tmp;
1237         int order;
1238
1239         /*
1240          * First, find the contiguous, power of two-sized set of free
1241          * physical pages containing the given physical page "m" and
1242          * assign it to "m_set".
1243          */
1244         seg = &vm_phys_segs[m->segind];
1245         vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1246         for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
1247             order < VM_NFREEORDER - 1; ) {
1248                 order++;
1249                 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
1250                 if (pa >= seg->start)
1251                         m_set = &seg->first_page[atop(pa - seg->start)];
1252                 else
1253                         return (FALSE);
1254         }
1255         if (m_set->order < order)
1256                 return (FALSE);
1257         if (m_set->order == VM_NFREEORDER)
1258                 return (FALSE);
1259         KASSERT(m_set->order < VM_NFREEORDER,
1260             ("vm_phys_unfree_page: page %p has unexpected order %d",
1261             m_set, m_set->order));
1262
1263         /*
1264          * Next, remove "m_set" from the free lists.  Finally, extract
1265          * "m" from "m_set" using an iterative algorithm: While "m_set"
1266          * is larger than a page, shrink "m_set" by returning the half
1267          * of "m_set" that does not contain "m" to the free lists.
1268          */
1269         fl = (*seg->free_queues)[m_set->pool];
1270         order = m_set->order;
1271         vm_freelist_rem(fl, m_set, order);
1272         while (order > 0) {
1273                 order--;
1274                 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
1275                 if (m->phys_addr < pa_half)
1276                         m_tmp = &seg->first_page[atop(pa_half - seg->start)];
1277                 else {
1278                         m_tmp = m_set;
1279                         m_set = &seg->first_page[atop(pa_half - seg->start)];
1280                 }
1281                 vm_freelist_add(fl, m_tmp, order, 0);
1282         }
1283         KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
1284         return (TRUE);
1285 }
1286
1287 /*
1288  * Allocate a contiguous set of physical pages of the given size
1289  * "npages" from the free lists.  All of the physical pages must be at
1290  * or above the given physical address "low" and below the given
1291  * physical address "high".  The given value "alignment" determines the
1292  * alignment of the first physical page in the set.  If the given value
1293  * "boundary" is non-zero, then the set of physical pages cannot cross
1294  * any physical address boundary that is a multiple of that value.  Both
1295  * "alignment" and "boundary" must be a power of two.
1296  */
1297 vm_page_t
1298 vm_phys_alloc_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1299     u_long alignment, vm_paddr_t boundary)
1300 {
1301         vm_paddr_t pa_end, pa_start;
1302         vm_page_t m_run;
1303         struct vm_phys_seg *seg;
1304         int segind;
1305
1306         KASSERT(npages > 0, ("npages is 0"));
1307         KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1308         KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1309         vm_domain_free_assert_locked(VM_DOMAIN(domain));
1310         if (low >= high)
1311                 return (NULL);
1312         m_run = NULL;
1313         for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
1314                 seg = &vm_phys_segs[segind];
1315                 if (seg->start >= high || seg->domain != domain)
1316                         continue;
1317                 if (low >= seg->end)
1318                         break;
1319                 if (low <= seg->start)
1320                         pa_start = seg->start;
1321                 else
1322                         pa_start = low;
1323                 if (high < seg->end)
1324                         pa_end = high;
1325                 else
1326                         pa_end = seg->end;
1327                 if (pa_end - pa_start < ptoa(npages))
1328                         continue;
1329                 m_run = vm_phys_alloc_seg_contig(seg, npages, low, high,
1330                     alignment, boundary);
1331                 if (m_run != NULL)
1332                         break;
1333         }
1334         return (m_run);
1335 }
1336
1337 /*
1338  * Allocate a run of contiguous physical pages from the free list for the
1339  * specified segment.
1340  */
1341 static vm_page_t
1342 vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, u_long npages,
1343     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1344 {
1345         struct vm_freelist *fl;
1346         vm_paddr_t pa, pa_end, size;
1347         vm_page_t m, m_ret;
1348         u_long npages_end;
1349         int oind, order, pind;
1350
1351         KASSERT(npages > 0, ("npages is 0"));
1352         KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1353         KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1354         vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1355         /* Compute the queue that is the best fit for npages. */
1356         order = flsl(npages - 1);
1357         /* Search for a run satisfying the specified conditions. */
1358         size = npages << PAGE_SHIFT;
1359         for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER;
1360             oind++) {
1361                 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1362                         fl = (*seg->free_queues)[pind];
1363                         TAILQ_FOREACH(m_ret, &fl[oind].pl, listq) {
1364                                 /*
1365                                  * Is the size of this allocation request
1366                                  * larger than the largest block size?
1367                                  */
1368                                 if (order >= VM_NFREEORDER) {
1369                                         /*
1370                                          * Determine if a sufficient number of
1371                                          * subsequent blocks to satisfy the
1372                                          * allocation request are free.
1373                                          */
1374                                         pa = VM_PAGE_TO_PHYS(m_ret);
1375                                         pa_end = pa + size;
1376                                         if (pa_end < pa)
1377                                                 continue;
1378                                         for (;;) {
1379                                                 pa += 1 << (PAGE_SHIFT +
1380                                                     VM_NFREEORDER - 1);
1381                                                 if (pa >= pa_end ||
1382                                                     pa < seg->start ||
1383                                                     pa >= seg->end)
1384                                                         break;
1385                                                 m = &seg->first_page[atop(pa -
1386                                                     seg->start)];
1387                                                 if (m->order != VM_NFREEORDER -
1388                                                     1)
1389                                                         break;
1390                                         }
1391                                         /* If not, go to the next block. */
1392                                         if (pa < pa_end)
1393                                                 continue;
1394                                 }
1395
1396                                 /*
1397                                  * Determine if the blocks are within the
1398                                  * given range, satisfy the given alignment,
1399                                  * and do not cross the given boundary.
1400                                  */
1401                                 pa = VM_PAGE_TO_PHYS(m_ret);
1402                                 pa_end = pa + size;
1403                                 if (pa >= low && pa_end <= high &&
1404                                     (pa & (alignment - 1)) == 0 &&
1405                                     rounddown2(pa ^ (pa_end - 1), boundary) == 0)
1406                                         goto done;
1407                         }
1408                 }
1409         }
1410         return (NULL);
1411 done:
1412         for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
1413                 fl = (*seg->free_queues)[m->pool];
1414                 vm_freelist_rem(fl, m, oind);
1415                 if (m->pool != VM_FREEPOOL_DEFAULT)
1416                         vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, oind);
1417         }
1418         /* Return excess pages to the free lists. */
1419         npages_end = roundup2(npages, 1 << oind);
1420         if (npages < npages_end) {
1421                 fl = (*seg->free_queues)[VM_FREEPOOL_DEFAULT];
1422                 vm_phys_enq_range(&m_ret[npages], npages_end - npages, fl, 0);
1423         }
1424         return (m_ret);
1425 }
1426
1427 #ifdef DDB
1428 /*
1429  * Show the number of physical pages in each of the free lists.
1430  */
1431 DB_SHOW_COMMAND(freepages, db_show_freepages)
1432 {
1433         struct vm_freelist *fl;
1434         int flind, oind, pind, dom;
1435
1436         for (dom = 0; dom < vm_ndomains; dom++) {
1437                 db_printf("DOMAIN: %d\n", dom);
1438                 for (flind = 0; flind < vm_nfreelists; flind++) {
1439                         db_printf("FREE LIST %d:\n"
1440                             "\n  ORDER (SIZE)  |  NUMBER"
1441                             "\n              ", flind);
1442                         for (pind = 0; pind < VM_NFREEPOOL; pind++)
1443                                 db_printf("  |  POOL %d", pind);
1444                         db_printf("\n--            ");
1445                         for (pind = 0; pind < VM_NFREEPOOL; pind++)
1446                                 db_printf("-- --      ");
1447                         db_printf("--\n");
1448                         for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
1449                                 db_printf("  %2.2d (%6.6dK)", oind,
1450                                     1 << (PAGE_SHIFT - 10 + oind));
1451                                 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1452                                 fl = vm_phys_free_queues[dom][flind][pind];
1453                                         db_printf("  |  %6.6d", fl[oind].lcnt);
1454                                 }
1455                                 db_printf("\n");
1456                         }
1457                         db_printf("\n");
1458                 }
1459                 db_printf("\n");
1460         }
1461 }
1462 #endif