Print this page
8956 Implement KPTI
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/i86pc/vm/vm_machdep.c
+++ new/usr/src/uts/i86pc/vm/vm_machdep.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 */
24 24 /*
25 25 * Copyright (c) 2010, Intel Corporation.
26 26 * All rights reserved.
27 - * Copyright 2016 Joyent, Inc.
27 + * Copyright 2018 Joyent, Inc.
28 28 */
29 29
30 30 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
31 31 /* All Rights Reserved */
32 32
33 33 /*
34 34 * Portions of this source code were derived from Berkeley 4.3 BSD
35 35 * under license from the Regents of the University of California.
36 36 */
37 37
38 38 /*
39 39 * UNIX machine dependent virtual memory support.
40 40 */
41 41
42 42 #include <sys/types.h>
43 43 #include <sys/param.h>
44 44 #include <sys/systm.h>
45 45 #include <sys/user.h>
46 46 #include <sys/proc.h>
47 47 #include <sys/kmem.h>
48 48 #include <sys/vmem.h>
49 49 #include <sys/buf.h>
50 50 #include <sys/cpuvar.h>
51 51 #include <sys/lgrp.h>
52 52 #include <sys/disp.h>
53 53 #include <sys/vm.h>
54 54 #include <sys/mman.h>
55 55 #include <sys/vnode.h>
56 56 #include <sys/cred.h>
57 57 #include <sys/exec.h>
58 58 #include <sys/exechdr.h>
59 59 #include <sys/debug.h>
60 60 #include <sys/vmsystm.h>
61 61 #include <sys/swap.h>
62 62 #include <sys/dumphdr.h>
63 63 #include <sys/random.h>
64 64
65 65 #include <vm/hat.h>
66 66 #include <vm/as.h>
67 67 #include <vm/seg.h>
68 68 #include <vm/seg_kp.h>
69 69 #include <vm/seg_vn.h>
70 70 #include <vm/page.h>
71 71 #include <vm/seg_kmem.h>
72 72 #include <vm/seg_kpm.h>
73 73 #include <vm/vm_dep.h>
74 74
75 75 #include <sys/cpu.h>
76 76 #include <sys/vm_machparam.h>
77 77 #include <sys/memlist.h>
78 78 #include <sys/bootconf.h> /* XXX the memlist stuff belongs in memlist_plat.h */
79 79 #include <vm/hat_i86.h>
80 80 #include <sys/x86_archext.h>
81 81 #include <sys/elf_386.h>
82 82 #include <sys/cmn_err.h>
83 83 #include <sys/archsystm.h>
84 84 #include <sys/machsystm.h>
85 85 #include <sys/secflags.h>
86 86
87 87 #include <sys/vtrace.h>
88 88 #include <sys/ddidmareq.h>
89 89 #include <sys/promif.h>
90 90 #include <sys/memnode.h>
91 91 #include <sys/stack.h>
92 92 #include <util/qsort.h>
93 93 #include <sys/taskq.h>
94 94
95 95 #ifdef __xpv
96 96
97 97 #include <sys/hypervisor.h>
98 98 #include <sys/xen_mmu.h>
99 99 #include <sys/balloon_impl.h>
100 100
101 101 /*
102 102 * domain 0 pages usable for DMA are kept pre-allocated and kept in
103 103 * distinct lists, ordered by increasing mfn.
104 104 */
105 105 static kmutex_t io_pool_lock;
106 106 static kmutex_t contig_list_lock;
107 107 static page_t *io_pool_4g; /* pool for 32 bit dma limited devices */
108 108 static page_t *io_pool_16m; /* pool for 24 bit dma limited legacy devices */
109 109 static long io_pool_cnt;
110 110 static long io_pool_cnt_max = 0;
111 111 #define DEFAULT_IO_POOL_MIN 128
112 112 static long io_pool_cnt_min = DEFAULT_IO_POOL_MIN;
113 113 static long io_pool_cnt_lowater = 0;
114 114 static long io_pool_shrink_attempts; /* how many times did we try to shrink */
115 115 static long io_pool_shrinks; /* how many times did we really shrink */
116 116 static long io_pool_grows; /* how many times did we grow */
117 117 static mfn_t start_mfn = 1;
118 118 static caddr_t io_pool_kva; /* use to alloc pages when needed */
119 119
120 120 static int create_contig_pfnlist(uint_t);
121 121
122 122 /*
123 123 * percentage of phys mem to hold in the i/o pool
124 124 */
125 125 #define DEFAULT_IO_POOL_PCT 2
126 126 static long io_pool_physmem_pct = DEFAULT_IO_POOL_PCT;
127 127 static void page_io_pool_sub(page_t **, page_t *, page_t *);
128 128 int ioalloc_dbg = 0;
129 129
130 130 #endif /* __xpv */
131 131
132 132 uint_t vac_colors = 1;
133 133
134 134 int largepagesupport = 0;
135 135 extern uint_t page_create_new;
136 136 extern uint_t page_create_exists;
137 137 extern uint_t page_create_putbacks;
138 138 /*
139 139 * Allow users to disable the kernel's use of SSE.
140 140 */
141 141 extern int use_sse_pagecopy, use_sse_pagezero;
142 142
143 143 /*
144 144 * combined memory ranges from mnode and memranges[] to manage single
145 145 * mnode/mtype dimension in the page lists.
146 146 */
147 147 typedef struct {
148 148 pfn_t mnr_pfnlo;
149 149 pfn_t mnr_pfnhi;
150 150 int mnr_mnode;
151 151 int mnr_memrange; /* index into memranges[] */
152 152 int mnr_next; /* next lower PA mnoderange */
153 153 int mnr_exists;
154 154 /* maintain page list stats */
155 155 pgcnt_t mnr_mt_clpgcnt; /* cache list cnt */
156 156 pgcnt_t mnr_mt_flpgcnt[MMU_PAGE_SIZES]; /* free list cnt per szc */
157 157 pgcnt_t mnr_mt_totcnt; /* sum of cache and free lists */
158 158 #ifdef DEBUG
159 159 struct mnr_mts { /* mnode/mtype szc stats */
160 160 pgcnt_t mnr_mts_pgcnt;
161 161 int mnr_mts_colors;
162 162 pgcnt_t *mnr_mtsc_pgcnt;
163 163 } *mnr_mts;
164 164 #endif
165 165 } mnoderange_t;
166 166
167 167 #define MEMRANGEHI(mtype) \
168 168 ((mtype > 0) ? memranges[mtype - 1] - 1: physmax)
169 169 #define MEMRANGELO(mtype) (memranges[mtype])
170 170
171 171 #define MTYPE_FREEMEM(mt) (mnoderanges[mt].mnr_mt_totcnt)
172 172
173 173 /*
174 174 * As the PC architecture evolved memory up was clumped into several
175 175 * ranges for various historical I/O devices to do DMA.
176 176 * < 16Meg - ISA bus
177 177 * < 2Gig - ???
178 178 * < 4Gig - PCI bus or drivers that don't understand PAE mode
179 179 *
180 180 * These are listed in reverse order, so that we can skip over unused
181 181 * ranges on machines with small memories.
182 182 *
183 183 * For now under the Hypervisor, we'll only ever have one memrange.
184 184 */
185 185 #define PFN_4GIG 0x100000
186 186 #define PFN_16MEG 0x1000
187 187 /* Indices into the memory range (arch_memranges) array. */
188 188 #define MRI_4G 0
189 189 #define MRI_2G 1
190 190 #define MRI_16M 2
191 191 #define MRI_0 3
192 192 static pfn_t arch_memranges[NUM_MEM_RANGES] = {
193 193 PFN_4GIG, /* pfn range for 4G and above */
194 194 0x80000, /* pfn range for 2G-4G */
195 195 PFN_16MEG, /* pfn range for 16M-2G */
196 196 0x00000, /* pfn range for 0-16M */
197 197 };
198 198 pfn_t *memranges = &arch_memranges[0];
199 199 int nranges = NUM_MEM_RANGES;
200 200
201 201 /*
202 202 * This combines mem_node_config and memranges into one data
203 203 * structure to be used for page list management.
204 204 */
205 205 mnoderange_t *mnoderanges;
206 206 int mnoderangecnt;
207 207 int mtype4g;
208 208 int mtype16m;
209 209 int mtypetop; /* index of highest pfn'ed mnoderange */
210 210
211 211 /*
212 212 * 4g memory management variables for systems with more than 4g of memory:
213 213 *
214 214 * physical memory below 4g is required for 32bit dma devices and, currently,
215 215 * for kmem memory. On systems with more than 4g of memory, the pool of memory
216 216 * below 4g can be depleted without any paging activity given that there is
217 217 * likely to be sufficient memory above 4g.
218 218 *
219 219 * physmax4g is set true if the largest pfn is over 4g. The rest of the
220 220 * 4g memory management code is enabled only when physmax4g is true.
221 221 *
222 222 * maxmem4g is the count of the maximum number of pages on the page lists
223 223 * with physical addresses below 4g. It can be a lot less then 4g given that
224 224 * BIOS may reserve large chunks of space below 4g for hot plug pci devices,
225 225 * agp aperture etc.
226 226 *
227 227 * freemem4g maintains the count of the number of available pages on the
228 228 * page lists with physical addresses below 4g.
229 229 *
230 230 * DESFREE4G specifies the desired amount of below 4g memory. It defaults to
231 231 * 6% (desfree4gshift = 4) of maxmem4g.
232 232 *
233 233 * RESTRICT4G_ALLOC returns true if freemem4g falls below DESFREE4G
234 234 * and the amount of physical memory above 4g is greater than freemem4g.
235 235 * In this case, page_get_* routines will restrict below 4g allocations
236 236 * for requests that don't specifically require it.
237 237 */
238 238
239 239 #define DESFREE4G (maxmem4g >> desfree4gshift)
240 240
241 241 #define RESTRICT4G_ALLOC \
242 242 (physmax4g && (freemem4g < DESFREE4G) && ((freemem4g << 1) < freemem))
243 243
244 244 static pgcnt_t maxmem4g;
245 245 static pgcnt_t freemem4g;
246 246 static int physmax4g;
247 247 static int desfree4gshift = 4; /* maxmem4g shift to derive DESFREE4G */
248 248
249 249 /*
250 250 * 16m memory management:
251 251 *
252 252 * reserve some amount of physical memory below 16m for legacy devices.
253 253 *
254 254 * RESTRICT16M_ALLOC returns true if an there are sufficient free pages above
255 255 * 16m or if the 16m pool drops below DESFREE16M.
256 256 *
257 257 * In this case, general page allocations via page_get_{free,cache}list
258 258 * routines will be restricted from allocating from the 16m pool. Allocations
259 259 * that require specific pfn ranges (page_get_anylist) and PG_PANIC allocations
260 260 * are not restricted.
261 261 */
262 262
263 263 #define FREEMEM16M MTYPE_FREEMEM(mtype16m)
264 264 #define DESFREE16M desfree16m
265 265 #define RESTRICT16M_ALLOC(freemem, pgcnt, flags) \
266 266 ((freemem != 0) && ((flags & PG_PANIC) == 0) && \
267 267 ((freemem >= (FREEMEM16M)) || \
268 268 (FREEMEM16M < (DESFREE16M + pgcnt))))
269 269
270 270 static pgcnt_t desfree16m = 0x380;
271 271
272 272 /*
273 273 * This can be patched via /etc/system to allow old non-PAE aware device
274 274 * drivers to use kmem_alloc'd memory on 32 bit systems with > 4Gig RAM.
275 275 */
276 276 int restricted_kmemalloc = 0;
277 277
278 278 #ifdef VM_STATS
279 279 struct {
280 280 ulong_t pga_alloc;
281 281 ulong_t pga_notfullrange;
282 282 ulong_t pga_nulldmaattr;
283 283 ulong_t pga_allocok;
284 284 ulong_t pga_allocfailed;
285 285 ulong_t pgma_alloc;
286 286 ulong_t pgma_allocok;
287 287 ulong_t pgma_allocfailed;
288 288 ulong_t pgma_allocempty;
289 289 } pga_vmstats;
290 290 #endif
291 291
292 292 uint_t mmu_page_sizes;
293 293
294 294 /* How many page sizes the users can see */
295 295 uint_t mmu_exported_page_sizes;
296 296
297 297 /* page sizes that legacy applications can see */
298 298 uint_t mmu_legacy_page_sizes;
299 299
300 300 /*
301 301 * Number of pages in 1 GB. Don't enable automatic large pages if we have
302 302 * fewer than this many pages.
303 303 */
304 304 pgcnt_t shm_lpg_min_physmem = 1 << (30 - MMU_PAGESHIFT);
305 305 pgcnt_t privm_lpg_min_physmem = 1 << (30 - MMU_PAGESHIFT);
306 306
307 307 /*
308 308 * Maximum and default segment size tunables for user private
309 309 * and shared anon memory, and user text and initialized data.
310 310 * These can be patched via /etc/system to allow large pages
311 311 * to be used for mapping application private and shared anon memory.
312 312 */
313 313 size_t mcntl0_lpsize = MMU_PAGESIZE;
314 314 size_t max_uheap_lpsize = MMU_PAGESIZE;
315 315 size_t default_uheap_lpsize = MMU_PAGESIZE;
316 316 size_t max_ustack_lpsize = MMU_PAGESIZE;
317 317 size_t default_ustack_lpsize = MMU_PAGESIZE;
318 318 size_t max_privmap_lpsize = MMU_PAGESIZE;
319 319 size_t max_uidata_lpsize = MMU_PAGESIZE;
320 320 size_t max_utext_lpsize = MMU_PAGESIZE;
321 321 size_t max_shm_lpsize = MMU_PAGESIZE;
322 322
323 323
324 324 /*
325 325 * initialized by page_coloring_init().
326 326 */
327 327 uint_t page_colors;
328 328 uint_t page_colors_mask;
329 329 uint_t page_coloring_shift;
330 330 int cpu_page_colors;
331 331 static uint_t l2_colors;
332 332
333 333 /*
334 334 * Page freelists and cachelists are dynamically allocated once mnoderangecnt
335 335 * and page_colors are calculated from the l2 cache n-way set size. Within a
336 336 * mnode range, the page freelist and cachelist are hashed into bins based on
337 337 * color. This makes it easier to search for a page within a specific memory
338 338 * range.
339 339 */
340 340 #define PAGE_COLORS_MIN 16
341 341
342 342 page_t ****page_freelists;
343 343 page_t ***page_cachelists;
344 344
345 345
346 346 /*
347 347 * Used by page layer to know about page sizes
348 348 */
349 349 hw_pagesize_t hw_page_array[MAX_NUM_LEVEL + 1];
350 350
351 351 kmutex_t *fpc_mutex[NPC_MUTEX];
352 352 kmutex_t *cpc_mutex[NPC_MUTEX];
353 353
354 354 /* Lock to protect mnoderanges array for memory DR operations. */
355 355 static kmutex_t mnoderange_lock;
356 356
↓ open down ↓ |
319 lines elided |
↑ open up ↑ |
357 357 /*
358 358 * Only let one thread at a time try to coalesce large pages, to
359 359 * prevent them from working against each other.
360 360 */
361 361 static kmutex_t contig_lock;
362 362 #define CONTIG_LOCK() mutex_enter(&contig_lock);
363 363 #define CONTIG_UNLOCK() mutex_exit(&contig_lock);
364 364
365 365 #define PFN_16M (mmu_btop((uint64_t)0x1000000))
366 366
367 +caddr_t
368 +i86devmap(pfn_t pf, pgcnt_t pgcnt, uint_t prot)
369 +{
370 + caddr_t addr;
371 + caddr_t addr1;
372 + page_t *pp;
373 +
374 + addr1 = addr = vmem_alloc(heap_arena, mmu_ptob(pgcnt), VM_SLEEP);
375 +
376 + for (; pgcnt != 0; addr += MMU_PAGESIZE, ++pf, --pgcnt) {
377 + pp = page_numtopp_nolock(pf);
378 + if (pp == NULL) {
379 + hat_devload(kas.a_hat, addr, MMU_PAGESIZE, pf,
380 + prot | HAT_NOSYNC, HAT_LOAD_LOCK);
381 + } else {
382 + hat_memload(kas.a_hat, addr, pp,
383 + prot | HAT_NOSYNC, HAT_LOAD_LOCK);
384 + }
385 + }
386 +
387 + return (addr1);
388 +}
389 +
367 390 /*
391 + * This routine is like page_numtopp, but accepts only free pages, which
392 + * it allocates (unfrees) and returns with the exclusive lock held.
393 + * It is used by machdep.c/dma_init() to find contiguous free pages.
394 + */
395 +page_t *
396 +page_numtopp_alloc(pfn_t pfnum)
397 +{
398 + page_t *pp;
399 +
400 +retry:
401 + pp = page_numtopp_nolock(pfnum);
402 + if (pp == NULL) {
403 + return (NULL);
404 + }
405 +
406 + if (!page_trylock(pp, SE_EXCL)) {
407 + return (NULL);
408 + }
409 +
410 + if (page_pptonum(pp) != pfnum) {
411 + page_unlock(pp);
412 + goto retry;
413 + }
414 +
415 + if (!PP_ISFREE(pp)) {
416 + page_unlock(pp);
417 + return (NULL);
418 + }
419 + if (pp->p_szc) {
420 + page_demote_free_pages(pp);
421 + page_unlock(pp);
422 + goto retry;
423 + }
424 +
425 + /* If associated with a vnode, destroy mappings */
426 +
427 + if (pp->p_vnode) {
428 +
429 + page_destroy_free(pp);
430 +
431 + if (!page_lock(pp, SE_EXCL, (kmutex_t *)NULL, P_NO_RECLAIM)) {
432 + return (NULL);
433 + }
434 +
435 + if (page_pptonum(pp) != pfnum) {
436 + page_unlock(pp);
437 + goto retry;
438 + }
439 + }
440 +
441 + if (!PP_ISFREE(pp)) {
442 + page_unlock(pp);
443 + return (NULL);
444 + }
445 +
446 + if (!page_reclaim(pp, (kmutex_t *)NULL))
447 + return (NULL);
448 +
449 + return (pp);
450 +}
451 +
452 +/*
368 453 * Return the optimum page size for a given mapping
369 454 */
370 455 /*ARGSUSED*/
371 456 size_t
372 457 map_pgsz(int maptype, struct proc *p, caddr_t addr, size_t len, int memcntl)
373 458 {
374 459 level_t l = 0;
375 460 size_t pgsz = MMU_PAGESIZE;
376 461 size_t max_lpsize;
377 462 uint_t mszc;
378 463
379 464 ASSERT(maptype != MAPPGSZ_VA);
380 465
381 466 if (maptype != MAPPGSZ_ISM && physmem < privm_lpg_min_physmem) {
382 467 return (MMU_PAGESIZE);
383 468 }
384 469
385 470 switch (maptype) {
386 471 case MAPPGSZ_HEAP:
387 472 case MAPPGSZ_STK:
388 473 max_lpsize = memcntl ? mcntl0_lpsize : (maptype ==
389 474 MAPPGSZ_HEAP ? max_uheap_lpsize : max_ustack_lpsize);
390 475 if (max_lpsize == MMU_PAGESIZE) {
391 476 return (MMU_PAGESIZE);
392 477 }
393 478 if (len == 0) {
394 479 len = (maptype == MAPPGSZ_HEAP) ? p->p_brkbase +
395 480 p->p_brksize - p->p_bssbase : p->p_stksize;
396 481 }
397 482 len = (maptype == MAPPGSZ_HEAP) ? MAX(len,
398 483 default_uheap_lpsize) : MAX(len, default_ustack_lpsize);
399 484
400 485 /*
401 486 * use the pages size that best fits len
402 487 */
403 488 for (l = mmu.umax_page_level; l > 0; --l) {
404 489 if (LEVEL_SIZE(l) > max_lpsize || len < LEVEL_SIZE(l)) {
405 490 continue;
406 491 } else {
407 492 pgsz = LEVEL_SIZE(l);
408 493 }
409 494 break;
410 495 }
411 496
412 497 mszc = (maptype == MAPPGSZ_HEAP ? p->p_brkpageszc :
413 498 p->p_stkpageszc);
414 499 if (addr == 0 && (pgsz < hw_page_array[mszc].hp_size)) {
415 500 pgsz = hw_page_array[mszc].hp_size;
416 501 }
417 502 return (pgsz);
418 503
419 504 case MAPPGSZ_ISM:
420 505 for (l = mmu.umax_page_level; l > 0; --l) {
421 506 if (len >= LEVEL_SIZE(l))
422 507 return (LEVEL_SIZE(l));
423 508 }
424 509 return (LEVEL_SIZE(0));
425 510 }
426 511 return (pgsz);
427 512 }
428 513
429 514 static uint_t
430 515 map_szcvec(caddr_t addr, size_t size, uintptr_t off, size_t max_lpsize,
431 516 size_t min_physmem)
432 517 {
433 518 caddr_t eaddr = addr + size;
434 519 uint_t szcvec = 0;
435 520 caddr_t raddr;
436 521 caddr_t readdr;
437 522 size_t pgsz;
438 523 int i;
439 524
440 525 if (physmem < min_physmem || max_lpsize <= MMU_PAGESIZE) {
441 526 return (0);
442 527 }
443 528
444 529 for (i = mmu_exported_page_sizes - 1; i > 0; i--) {
445 530 pgsz = page_get_pagesize(i);
446 531 if (pgsz > max_lpsize) {
447 532 continue;
448 533 }
449 534 raddr = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
450 535 readdr = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
451 536 if (raddr < addr || raddr >= readdr) {
452 537 continue;
453 538 }
454 539 if (P2PHASE((uintptr_t)addr ^ off, pgsz)) {
455 540 continue;
456 541 }
457 542 /*
458 543 * Set szcvec to the remaining page sizes.
459 544 */
460 545 szcvec = ((1 << (i + 1)) - 1) & ~1;
461 546 break;
462 547 }
463 548 return (szcvec);
464 549 }
465 550
466 551 /*
467 552 * Return a bit vector of large page size codes that
468 553 * can be used to map [addr, addr + len) region.
469 554 */
470 555 /*ARGSUSED*/
471 556 uint_t
472 557 map_pgszcvec(caddr_t addr, size_t size, uintptr_t off, int flags, int type,
473 558 int memcntl)
474 559 {
475 560 size_t max_lpsize = mcntl0_lpsize;
476 561
477 562 if (mmu.max_page_level == 0)
478 563 return (0);
479 564
480 565 if (flags & MAP_TEXT) {
481 566 if (!memcntl)
482 567 max_lpsize = max_utext_lpsize;
483 568 return (map_szcvec(addr, size, off, max_lpsize,
484 569 shm_lpg_min_physmem));
485 570
486 571 } else if (flags & MAP_INITDATA) {
487 572 if (!memcntl)
488 573 max_lpsize = max_uidata_lpsize;
489 574 return (map_szcvec(addr, size, off, max_lpsize,
490 575 privm_lpg_min_physmem));
491 576
492 577 } else if (type == MAPPGSZC_SHM) {
493 578 if (!memcntl)
494 579 max_lpsize = max_shm_lpsize;
495 580 return (map_szcvec(addr, size, off, max_lpsize,
496 581 shm_lpg_min_physmem));
497 582
498 583 } else if (type == MAPPGSZC_HEAP) {
499 584 if (!memcntl)
500 585 max_lpsize = max_uheap_lpsize;
501 586 return (map_szcvec(addr, size, off, max_lpsize,
502 587 privm_lpg_min_physmem));
503 588
504 589 } else if (type == MAPPGSZC_STACK) {
505 590 if (!memcntl)
506 591 max_lpsize = max_ustack_lpsize;
507 592 return (map_szcvec(addr, size, off, max_lpsize,
508 593 privm_lpg_min_physmem));
509 594
510 595 } else {
511 596 if (!memcntl)
512 597 max_lpsize = max_privmap_lpsize;
513 598 return (map_szcvec(addr, size, off, max_lpsize,
514 599 privm_lpg_min_physmem));
515 600 }
516 601 }
517 602
518 603 /*
519 604 * Handle a pagefault.
520 605 */
521 606 faultcode_t
522 607 pagefault(
523 608 caddr_t addr,
524 609 enum fault_type type,
525 610 enum seg_rw rw,
526 611 int iskernel)
527 612 {
528 613 struct as *as;
529 614 struct hat *hat;
530 615 struct proc *p;
531 616 kthread_t *t;
532 617 faultcode_t res;
533 618 caddr_t base;
534 619 size_t len;
535 620 int err;
536 621 int mapped_red;
537 622 uintptr_t ea;
538 623
539 624 ASSERT_STACK_ALIGNED();
540 625
541 626 if (INVALID_VADDR(addr))
542 627 return (FC_NOMAP);
543 628
544 629 mapped_red = segkp_map_red();
545 630
546 631 if (iskernel) {
547 632 as = &kas;
548 633 hat = as->a_hat;
549 634 } else {
550 635 t = curthread;
551 636 p = ttoproc(t);
552 637 as = p->p_as;
553 638 hat = as->a_hat;
554 639 }
555 640
556 641 /*
557 642 * Dispatch pagefault.
558 643 */
559 644 res = as_fault(hat, as, addr, 1, type, rw);
560 645
561 646 /*
562 647 * If this isn't a potential unmapped hole in the user's
563 648 * UNIX data or stack segments, just return status info.
564 649 */
565 650 if (res != FC_NOMAP || iskernel)
566 651 goto out;
567 652
568 653 /*
569 654 * Check to see if we happened to faulted on a currently unmapped
570 655 * part of the UNIX data or stack segments. If so, create a zfod
571 656 * mapping there and then try calling the fault routine again.
572 657 */
573 658 base = p->p_brkbase;
574 659 len = p->p_brksize;
575 660
576 661 if (addr < base || addr >= base + len) { /* data seg? */
577 662 base = (caddr_t)p->p_usrstack - p->p_stksize;
578 663 len = p->p_stksize;
579 664 if (addr < base || addr >= p->p_usrstack) { /* stack seg? */
580 665 /* not in either UNIX data or stack segments */
581 666 res = FC_NOMAP;
582 667 goto out;
583 668 }
584 669 }
585 670
586 671 /*
587 672 * the rest of this function implements a 3.X 4.X 5.X compatibility
588 673 * This code is probably not needed anymore
589 674 */
590 675 if (p->p_model == DATAMODEL_ILP32) {
591 676
592 677 /* expand the gap to the page boundaries on each side */
593 678 ea = P2ROUNDUP((uintptr_t)base + len, MMU_PAGESIZE);
594 679 base = (caddr_t)P2ALIGN((uintptr_t)base, MMU_PAGESIZE);
595 680 len = ea - (uintptr_t)base;
596 681
597 682 as_rangelock(as);
598 683 if (as_gap(as, MMU_PAGESIZE, &base, &len, AH_CONTAIN, addr) ==
599 684 0) {
600 685 err = as_map(as, base, len, segvn_create, zfod_argsp);
601 686 as_rangeunlock(as);
602 687 if (err) {
603 688 res = FC_MAKE_ERR(err);
604 689 goto out;
605 690 }
606 691 } else {
607 692 /*
608 693 * This page is already mapped by another thread after
609 694 * we returned from as_fault() above. We just fall
610 695 * through as_fault() below.
611 696 */
612 697 as_rangeunlock(as);
613 698 }
614 699
615 700 res = as_fault(hat, as, addr, 1, F_INVAL, rw);
616 701 }
617 702
618 703 out:
619 704 if (mapped_red)
620 705 segkp_unmap_red();
621 706
622 707 return (res);
623 708 }
624 709
625 710 void
626 711 map_addr(caddr_t *addrp, size_t len, offset_t off, int vacalign, uint_t flags)
627 712 {
628 713 struct proc *p = curproc;
629 714 caddr_t userlimit = (flags & _MAP_LOW32) ?
630 715 (caddr_t)_userlimit32 : p->p_as->a_userlimit;
631 716
632 717 map_addr_proc(addrp, len, off, vacalign, userlimit, curproc, flags);
633 718 }
634 719
635 720 /*ARGSUSED*/
636 721 int
637 722 map_addr_vacalign_check(caddr_t addr, u_offset_t off)
638 723 {
639 724 return (0);
640 725 }
641 726
642 727 /*
643 728 * The maximum amount a randomized mapping will be slewed. We should perhaps
644 729 * arrange things so these tunables can be separate for mmap, mmapobj, and
645 730 * ld.so
646 731 */
647 732 size_t aslr_max_map_skew = 256 * 1024 * 1024; /* 256MB */
648 733
649 734 /*
650 735 * map_addr_proc() is the routine called when the system is to
651 736 * choose an address for the user. We will pick an address
652 737 * range which is the highest available below userlimit.
653 738 *
654 739 * Every mapping will have a redzone of a single page on either side of
655 740 * the request. This is done to leave one page unmapped between segments.
656 741 * This is not required, but it's useful for the user because if their
657 742 * program strays across a segment boundary, it will catch a fault
658 743 * immediately making debugging a little easier. Currently the redzone
659 744 * is mandatory.
660 745 *
661 746 * addrp is a value/result parameter.
662 747 * On input it is a hint from the user to be used in a completely
663 748 * machine dependent fashion. We decide to completely ignore this hint.
664 749 * If MAP_ALIGN was specified, addrp contains the minimal alignment, which
665 750 * must be some "power of two" multiple of pagesize.
666 751 *
667 752 * On output it is NULL if no address can be found in the current
668 753 * processes address space or else an address that is currently
669 754 * not mapped for len bytes with a page of red zone on either side.
670 755 *
671 756 * vacalign is not needed on x86 (it's for viturally addressed caches)
672 757 */
673 758 /*ARGSUSED*/
674 759 void
675 760 map_addr_proc(
676 761 caddr_t *addrp,
677 762 size_t len,
678 763 offset_t off,
679 764 int vacalign,
680 765 caddr_t userlimit,
681 766 struct proc *p,
682 767 uint_t flags)
683 768 {
684 769 struct as *as = p->p_as;
685 770 caddr_t addr;
686 771 caddr_t base;
687 772 size_t slen;
688 773 size_t align_amount;
689 774
690 775 ASSERT32(userlimit == as->a_userlimit);
691 776
692 777 base = p->p_brkbase;
693 778 #if defined(__amd64)
694 779 /*
695 780 * XX64 Yes, this needs more work.
696 781 */
697 782 if (p->p_model == DATAMODEL_NATIVE) {
698 783 if (userlimit < as->a_userlimit) {
699 784 /*
700 785 * This happens when a program wants to map
701 786 * something in a range that's accessible to a
702 787 * program in a smaller address space. For example,
703 788 * a 64-bit program calling mmap32(2) to guarantee
704 789 * that the returned address is below 4Gbytes.
705 790 */
706 791 ASSERT((uintptr_t)userlimit < ADDRESS_C(0xffffffff));
707 792
708 793 if (userlimit > base)
709 794 slen = userlimit - base;
710 795 else {
711 796 *addrp = NULL;
712 797 return;
713 798 }
714 799 } else {
715 800 /*
716 801 * XX64 This layout is probably wrong .. but in
717 802 * the event we make the amd64 address space look
718 803 * like sparcv9 i.e. with the stack -above- the
719 804 * heap, this bit of code might even be correct.
720 805 */
721 806 slen = p->p_usrstack - base -
722 807 ((p->p_stk_ctl + PAGEOFFSET) & PAGEMASK);
723 808 }
724 809 } else
725 810 #endif
726 811 slen = userlimit - base;
727 812
728 813 /* Make len be a multiple of PAGESIZE */
729 814 len = (len + PAGEOFFSET) & PAGEMASK;
730 815
731 816 /*
732 817 * figure out what the alignment should be
733 818 *
734 819 * XX64 -- is there an ELF_AMD64_MAXPGSZ or is it the same????
735 820 */
736 821 if (len <= ELF_386_MAXPGSZ) {
737 822 /*
738 823 * Align virtual addresses to ensure that ELF shared libraries
739 824 * are mapped with the appropriate alignment constraints by
740 825 * the run-time linker.
741 826 */
742 827 align_amount = ELF_386_MAXPGSZ;
743 828 } else {
744 829 /*
745 830 * For 32-bit processes, only those which have specified
746 831 * MAP_ALIGN and an addr will be aligned on a larger page size.
747 832 * Not doing so can potentially waste up to 1G of process
748 833 * address space.
749 834 */
750 835 int lvl = (p->p_model == DATAMODEL_ILP32) ? 1 :
751 836 mmu.umax_page_level;
752 837
753 838 while (lvl && len < LEVEL_SIZE(lvl))
754 839 --lvl;
755 840
756 841 align_amount = LEVEL_SIZE(lvl);
757 842 }
758 843 if ((flags & MAP_ALIGN) && ((uintptr_t)*addrp > align_amount))
759 844 align_amount = (uintptr_t)*addrp;
760 845
761 846 ASSERT(ISP2(align_amount));
762 847 ASSERT(align_amount == 0 || align_amount >= PAGESIZE);
763 848
764 849 off = off & (align_amount - 1);
765 850
766 851 /*
767 852 * Look for a large enough hole starting below userlimit.
768 853 * After finding it, use the upper part.
769 854 */
770 855 if (as_gap_aligned(as, len, &base, &slen, AH_HI, NULL, align_amount,
771 856 PAGESIZE, off) == 0) {
772 857 caddr_t as_addr;
773 858
774 859 /*
775 860 * addr is the highest possible address to use since we have
776 861 * a PAGESIZE redzone at the beginning and end.
777 862 */
778 863 addr = base + slen - (PAGESIZE + len);
779 864 as_addr = addr;
780 865 /*
781 866 * Round address DOWN to the alignment amount and
782 867 * add the offset in.
783 868 * If addr is greater than as_addr, len would not be large
784 869 * enough to include the redzone, so we must adjust down
785 870 * by the alignment amount.
786 871 */
787 872 addr = (caddr_t)((uintptr_t)addr & (~(align_amount - 1)));
788 873 addr += (uintptr_t)off;
789 874 if (addr > as_addr) {
790 875 addr -= align_amount;
791 876 }
792 877
793 878 /*
794 879 * If randomization is requested, slew the allocation
795 880 * backwards, within the same gap, by a random amount.
796 881 */
797 882 if (flags & _MAP_RANDOMIZE) {
798 883 uint32_t slew;
799 884
800 885 (void) random_get_pseudo_bytes((uint8_t *)&slew,
801 886 sizeof (slew));
802 887
803 888 slew = slew % MIN(aslr_max_map_skew, (addr - base));
804 889 addr -= P2ALIGN(slew, align_amount);
805 890 }
806 891
807 892 ASSERT(addr > base);
808 893 ASSERT(addr + len < base + slen);
809 894 ASSERT(((uintptr_t)addr & (align_amount - 1)) ==
810 895 ((uintptr_t)(off)));
811 896 *addrp = addr;
812 897 } else {
813 898 *addrp = NULL; /* no more virtual space */
814 899 }
815 900 }
816 901
817 902 int valid_va_range_aligned_wraparound;
818 903
819 904 /*
820 905 * Determine whether [*basep, *basep + *lenp) contains a mappable range of
821 906 * addresses at least "minlen" long, where the base of the range is at "off"
822 907 * phase from an "align" boundary and there is space for a "redzone"-sized
823 908 * redzone on either side of the range. On success, 1 is returned and *basep
824 909 * and *lenp are adjusted to describe the acceptable range (including
825 910 * the redzone). On failure, 0 is returned.
826 911 */
827 912 /*ARGSUSED3*/
828 913 int
829 914 valid_va_range_aligned(caddr_t *basep, size_t *lenp, size_t minlen, int dir,
830 915 size_t align, size_t redzone, size_t off)
831 916 {
832 917 uintptr_t hi, lo;
833 918 size_t tot_len;
834 919
835 920 ASSERT(align == 0 ? off == 0 : off < align);
836 921 ASSERT(ISP2(align));
837 922 ASSERT(align == 0 || align >= PAGESIZE);
838 923
839 924 lo = (uintptr_t)*basep;
840 925 hi = lo + *lenp;
841 926 tot_len = minlen + 2 * redzone; /* need at least this much space */
842 927
843 928 /*
844 929 * If hi rolled over the top, try cutting back.
845 930 */
846 931 if (hi < lo) {
847 932 *lenp = 0UL - lo - 1UL;
848 933 /* See if this really happens. If so, then we figure out why */
849 934 valid_va_range_aligned_wraparound++;
850 935 hi = lo + *lenp;
851 936 }
852 937 if (*lenp < tot_len) {
853 938 return (0);
854 939 }
855 940
856 941 #if defined(__amd64)
857 942 /*
858 943 * Deal with a possible hole in the address range between
859 944 * hole_start and hole_end that should never be mapped.
860 945 */
861 946 if (lo < hole_start) {
862 947 if (hi > hole_start) {
863 948 if (hi < hole_end) {
864 949 hi = hole_start;
865 950 } else {
866 951 /* lo < hole_start && hi >= hole_end */
867 952 if (dir == AH_LO) {
868 953 /*
869 954 * prefer lowest range
870 955 */
871 956 if (hole_start - lo >= tot_len)
872 957 hi = hole_start;
873 958 else if (hi - hole_end >= tot_len)
874 959 lo = hole_end;
875 960 else
876 961 return (0);
877 962 } else {
878 963 /*
879 964 * prefer highest range
880 965 */
881 966 if (hi - hole_end >= tot_len)
882 967 lo = hole_end;
883 968 else if (hole_start - lo >= tot_len)
884 969 hi = hole_start;
885 970 else
886 971 return (0);
887 972 }
888 973 }
889 974 }
890 975 } else {
891 976 /* lo >= hole_start */
892 977 if (hi < hole_end)
893 978 return (0);
894 979 if (lo < hole_end)
895 980 lo = hole_end;
896 981 }
897 982 #endif
898 983
899 984 if (hi - lo < tot_len)
900 985 return (0);
901 986
902 987 if (align > 1) {
903 988 uintptr_t tlo = lo + redzone;
904 989 uintptr_t thi = hi - redzone;
905 990 tlo = (uintptr_t)P2PHASEUP(tlo, align, off);
906 991 if (tlo < lo + redzone) {
907 992 return (0);
908 993 }
909 994 if (thi < tlo || thi - tlo < minlen) {
910 995 return (0);
911 996 }
912 997 }
913 998
914 999 *basep = (caddr_t)lo;
915 1000 *lenp = hi - lo;
916 1001 return (1);
917 1002 }
918 1003
919 1004 /*
920 1005 * Determine whether [*basep, *basep + *lenp) contains a mappable range of
921 1006 * addresses at least "minlen" long. On success, 1 is returned and *basep
922 1007 * and *lenp are adjusted to describe the acceptable range. On failure, 0
923 1008 * is returned.
924 1009 */
925 1010 int
926 1011 valid_va_range(caddr_t *basep, size_t *lenp, size_t minlen, int dir)
927 1012 {
928 1013 return (valid_va_range_aligned(basep, lenp, minlen, dir, 0, 0, 0));
929 1014 }
930 1015
931 1016 /*
932 1017 * Default to forbidding the first 64k of address space. This protects most
933 1018 * reasonably sized structures from dereferences through NULL:
934 1019 * ((foo_t *)0)->bar
935 1020 */
936 1021 uintptr_t forbidden_null_mapping_sz = 0x10000;
937 1022
938 1023 /*
939 1024 * Determine whether [addr, addr+len] are valid user addresses.
940 1025 */
941 1026 /*ARGSUSED*/
942 1027 int
943 1028 valid_usr_range(caddr_t addr, size_t len, uint_t prot, struct as *as,
944 1029 caddr_t userlimit)
945 1030 {
946 1031 caddr_t eaddr = addr + len;
947 1032
948 1033 if (eaddr <= addr || addr >= userlimit || eaddr > userlimit)
949 1034 return (RANGE_BADADDR);
950 1035
951 1036 if ((addr <= (caddr_t)forbidden_null_mapping_sz) &&
952 1037 as->a_proc != NULL &&
953 1038 secflag_enabled(as->a_proc, PROC_SEC_FORBIDNULLMAP))
954 1039 return (RANGE_BADADDR);
955 1040
956 1041 #if defined(__amd64)
957 1042 /*
958 1043 * Check for the VA hole
959 1044 */
960 1045 if (eaddr > (caddr_t)hole_start && addr < (caddr_t)hole_end)
961 1046 return (RANGE_BADADDR);
962 1047 #endif
963 1048
964 1049 return (RANGE_OKAY);
965 1050 }
966 1051
967 1052 /*
968 1053 * Return 1 if the page frame is onboard memory, else 0.
969 1054 */
970 1055 int
971 1056 pf_is_memory(pfn_t pf)
972 1057 {
973 1058 if (pfn_is_foreign(pf))
974 1059 return (0);
975 1060 return (address_in_memlist(phys_install, pfn_to_pa(pf), 1));
976 1061 }
977 1062
978 1063 /*
979 1064 * return the memrange containing pfn
980 1065 */
981 1066 int
982 1067 memrange_num(pfn_t pfn)
983 1068 {
984 1069 int n;
985 1070
986 1071 for (n = 0; n < nranges - 1; ++n) {
987 1072 if (pfn >= memranges[n])
988 1073 break;
989 1074 }
990 1075 return (n);
991 1076 }
992 1077
993 1078 /*
994 1079 * return the mnoderange containing pfn
995 1080 */
996 1081 /*ARGSUSED*/
997 1082 int
998 1083 pfn_2_mtype(pfn_t pfn)
999 1084 {
1000 1085 #if defined(__xpv)
1001 1086 return (0);
1002 1087 #else
1003 1088 int n;
1004 1089
1005 1090 /* Always start from highest pfn and work our way down */
1006 1091 for (n = mtypetop; n != -1; n = mnoderanges[n].mnr_next) {
1007 1092 if (pfn >= mnoderanges[n].mnr_pfnlo) {
1008 1093 break;
1009 1094 }
1010 1095 }
1011 1096 return (n);
1012 1097 #endif
1013 1098 }
1014 1099
1015 1100 #if !defined(__xpv)
1016 1101 /*
1017 1102 * is_contigpage_free:
1018 1103 * returns a page list of contiguous pages. It minimally has to return
1019 1104 * minctg pages. Caller determines minctg based on the scatter-gather
1020 1105 * list length.
1021 1106 *
1022 1107 * pfnp is set to the next page frame to search on return.
1023 1108 */
1024 1109 static page_t *
1025 1110 is_contigpage_free(
1026 1111 pfn_t *pfnp,
1027 1112 pgcnt_t *pgcnt,
1028 1113 pgcnt_t minctg,
1029 1114 uint64_t pfnseg,
1030 1115 int iolock)
1031 1116 {
1032 1117 int i = 0;
1033 1118 pfn_t pfn = *pfnp;
1034 1119 page_t *pp;
1035 1120 page_t *plist = NULL;
1036 1121
1037 1122 /*
1038 1123 * fail if pfn + minctg crosses a segment boundary.
1039 1124 * Adjust for next starting pfn to begin at segment boundary.
1040 1125 */
1041 1126
1042 1127 if (((*pfnp + minctg - 1) & pfnseg) < (*pfnp & pfnseg)) {
1043 1128 *pfnp = roundup(*pfnp, pfnseg + 1);
1044 1129 return (NULL);
1045 1130 }
1046 1131
1047 1132 do {
1048 1133 retry:
1049 1134 pp = page_numtopp_nolock(pfn + i);
1050 1135 if ((pp == NULL) || IS_DUMP_PAGE(pp) ||
1051 1136 (page_trylock(pp, SE_EXCL) == 0)) {
1052 1137 (*pfnp)++;
1053 1138 break;
1054 1139 }
1055 1140 if (page_pptonum(pp) != pfn + i) {
1056 1141 page_unlock(pp);
1057 1142 goto retry;
1058 1143 }
1059 1144
1060 1145 if (!(PP_ISFREE(pp))) {
1061 1146 page_unlock(pp);
1062 1147 (*pfnp)++;
1063 1148 break;
1064 1149 }
1065 1150
1066 1151 if (!PP_ISAGED(pp)) {
1067 1152 page_list_sub(pp, PG_CACHE_LIST);
1068 1153 page_hashout(pp, (kmutex_t *)NULL);
1069 1154 } else {
1070 1155 page_list_sub(pp, PG_FREE_LIST);
1071 1156 }
1072 1157
1073 1158 if (iolock)
1074 1159 page_io_lock(pp);
1075 1160 page_list_concat(&plist, &pp);
1076 1161
1077 1162 /*
1078 1163 * exit loop when pgcnt satisfied or segment boundary reached.
1079 1164 */
1080 1165
1081 1166 } while ((++i < *pgcnt) && ((pfn + i) & pfnseg));
1082 1167
1083 1168 *pfnp += i; /* set to next pfn to search */
1084 1169
1085 1170 if (i >= minctg) {
1086 1171 *pgcnt -= i;
1087 1172 return (plist);
1088 1173 }
1089 1174
1090 1175 /*
1091 1176 * failure: minctg not satisfied.
1092 1177 *
1093 1178 * if next request crosses segment boundary, set next pfn
1094 1179 * to search from the segment boundary.
1095 1180 */
1096 1181 if (((*pfnp + minctg - 1) & pfnseg) < (*pfnp & pfnseg))
1097 1182 *pfnp = roundup(*pfnp, pfnseg + 1);
1098 1183
1099 1184 /* clean up any pages already allocated */
1100 1185
1101 1186 while (plist) {
1102 1187 pp = plist;
1103 1188 page_sub(&plist, pp);
1104 1189 page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL);
1105 1190 if (iolock)
1106 1191 page_io_unlock(pp);
1107 1192 page_unlock(pp);
1108 1193 }
1109 1194
1110 1195 return (NULL);
1111 1196 }
1112 1197 #endif /* !__xpv */
1113 1198
1114 1199 /*
1115 1200 * verify that pages being returned from allocator have correct DMA attribute
1116 1201 */
1117 1202 #ifndef DEBUG
1118 1203 #define check_dma(a, b, c) (void)(0)
1119 1204 #else
1120 1205 static void
1121 1206 check_dma(ddi_dma_attr_t *dma_attr, page_t *pp, int cnt)
1122 1207 {
1123 1208 if (dma_attr == NULL)
1124 1209 return;
1125 1210
1126 1211 while (cnt-- > 0) {
1127 1212 if (pa_to_ma(pfn_to_pa(pp->p_pagenum)) <
1128 1213 dma_attr->dma_attr_addr_lo)
1129 1214 panic("PFN (pp=%p) below dma_attr_addr_lo", (void *)pp);
1130 1215 if (pa_to_ma(pfn_to_pa(pp->p_pagenum)) >=
1131 1216 dma_attr->dma_attr_addr_hi)
1132 1217 panic("PFN (pp=%p) above dma_attr_addr_hi", (void *)pp);
1133 1218 pp = pp->p_next;
1134 1219 }
1135 1220 }
1136 1221 #endif
1137 1222
1138 1223 #if !defined(__xpv)
1139 1224 static page_t *
1140 1225 page_get_contigpage(pgcnt_t *pgcnt, ddi_dma_attr_t *mattr, int iolock)
1141 1226 {
1142 1227 pfn_t pfn;
1143 1228 int sgllen;
1144 1229 uint64_t pfnseg;
1145 1230 pgcnt_t minctg;
1146 1231 page_t *pplist = NULL, *plist;
1147 1232 uint64_t lo, hi;
1148 1233 pgcnt_t pfnalign = 0;
1149 1234 static pfn_t startpfn;
1150 1235 static pgcnt_t lastctgcnt;
1151 1236 uintptr_t align;
1152 1237
1153 1238 CONTIG_LOCK();
1154 1239
1155 1240 if (mattr) {
1156 1241 lo = mmu_btop((mattr->dma_attr_addr_lo + MMU_PAGEOFFSET));
1157 1242 hi = mmu_btop(mattr->dma_attr_addr_hi);
1158 1243 if (hi >= physmax)
1159 1244 hi = physmax - 1;
1160 1245 sgllen = mattr->dma_attr_sgllen;
1161 1246 pfnseg = mmu_btop(mattr->dma_attr_seg);
1162 1247
1163 1248 align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
1164 1249 if (align > MMU_PAGESIZE)
1165 1250 pfnalign = mmu_btop(align);
1166 1251
1167 1252 /*
1168 1253 * in order to satisfy the request, must minimally
1169 1254 * acquire minctg contiguous pages
1170 1255 */
1171 1256 minctg = howmany(*pgcnt, sgllen);
1172 1257
1173 1258 ASSERT(hi >= lo);
1174 1259
1175 1260 /*
1176 1261 * start from where last searched if the minctg >= lastctgcnt
1177 1262 */
1178 1263 if (minctg < lastctgcnt || startpfn < lo || startpfn > hi)
1179 1264 startpfn = lo;
1180 1265 } else {
1181 1266 hi = physmax - 1;
1182 1267 lo = 0;
1183 1268 sgllen = 1;
1184 1269 pfnseg = mmu.highest_pfn;
1185 1270 minctg = *pgcnt;
1186 1271
1187 1272 if (minctg < lastctgcnt)
1188 1273 startpfn = lo;
1189 1274 }
1190 1275 lastctgcnt = minctg;
1191 1276
1192 1277 ASSERT(pfnseg + 1 >= (uint64_t)minctg);
1193 1278
1194 1279 /* conserve 16m memory - start search above 16m when possible */
1195 1280 if (hi > PFN_16M && startpfn < PFN_16M)
1196 1281 startpfn = PFN_16M;
1197 1282
1198 1283 pfn = startpfn;
1199 1284 if (pfnalign)
1200 1285 pfn = P2ROUNDUP(pfn, pfnalign);
1201 1286
1202 1287 while (pfn + minctg - 1 <= hi) {
1203 1288
1204 1289 plist = is_contigpage_free(&pfn, pgcnt, minctg, pfnseg, iolock);
1205 1290 if (plist) {
1206 1291 page_list_concat(&pplist, &plist);
1207 1292 sgllen--;
1208 1293 /*
1209 1294 * return when contig pages no longer needed
1210 1295 */
1211 1296 if (!*pgcnt || ((*pgcnt <= sgllen) && !pfnalign)) {
1212 1297 startpfn = pfn;
1213 1298 CONTIG_UNLOCK();
1214 1299 check_dma(mattr, pplist, *pgcnt);
1215 1300 return (pplist);
1216 1301 }
1217 1302 minctg = howmany(*pgcnt, sgllen);
1218 1303 }
1219 1304 if (pfnalign)
1220 1305 pfn = P2ROUNDUP(pfn, pfnalign);
1221 1306 }
1222 1307
1223 1308 /* cannot find contig pages in specified range */
1224 1309 if (startpfn == lo) {
1225 1310 CONTIG_UNLOCK();
1226 1311 return (NULL);
1227 1312 }
1228 1313
1229 1314 /* did not start with lo previously */
1230 1315 pfn = lo;
1231 1316 if (pfnalign)
1232 1317 pfn = P2ROUNDUP(pfn, pfnalign);
1233 1318
1234 1319 /* allow search to go above startpfn */
1235 1320 while (pfn < startpfn) {
1236 1321
1237 1322 plist = is_contigpage_free(&pfn, pgcnt, minctg, pfnseg, iolock);
1238 1323 if (plist != NULL) {
1239 1324
1240 1325 page_list_concat(&pplist, &plist);
1241 1326 sgllen--;
1242 1327
1243 1328 /*
1244 1329 * return when contig pages no longer needed
1245 1330 */
1246 1331 if (!*pgcnt || ((*pgcnt <= sgllen) && !pfnalign)) {
1247 1332 startpfn = pfn;
1248 1333 CONTIG_UNLOCK();
1249 1334 check_dma(mattr, pplist, *pgcnt);
1250 1335 return (pplist);
1251 1336 }
1252 1337 minctg = howmany(*pgcnt, sgllen);
1253 1338 }
1254 1339 if (pfnalign)
1255 1340 pfn = P2ROUNDUP(pfn, pfnalign);
1256 1341 }
1257 1342 CONTIG_UNLOCK();
1258 1343 return (NULL);
1259 1344 }
1260 1345 #endif /* !__xpv */
1261 1346
1262 1347 /*
1263 1348 * mnode_range_cnt() calculates the number of memory ranges for mnode and
1264 1349 * memranges[]. Used to determine the size of page lists and mnoderanges.
1265 1350 */
1266 1351 int
1267 1352 mnode_range_cnt(int mnode)
1268 1353 {
1269 1354 #if defined(__xpv)
1270 1355 ASSERT(mnode == 0);
1271 1356 return (1);
1272 1357 #else /* __xpv */
1273 1358 int mri;
1274 1359 int mnrcnt = 0;
1275 1360
1276 1361 if (mem_node_config[mnode].exists != 0) {
1277 1362 mri = nranges - 1;
1278 1363
1279 1364 /* find the memranges index below contained in mnode range */
1280 1365
1281 1366 while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
1282 1367 mri--;
1283 1368
1284 1369 /*
1285 1370 * increment mnode range counter when memranges or mnode
1286 1371 * boundary is reached.
1287 1372 */
1288 1373 while (mri >= 0 &&
1289 1374 mem_node_config[mnode].physmax >= MEMRANGELO(mri)) {
1290 1375 mnrcnt++;
1291 1376 if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
1292 1377 mri--;
1293 1378 else
1294 1379 break;
1295 1380 }
1296 1381 }
1297 1382 ASSERT(mnrcnt <= MAX_MNODE_MRANGES);
1298 1383 return (mnrcnt);
1299 1384 #endif /* __xpv */
1300 1385 }
1301 1386
1302 1387 /*
1303 1388 * mnode_range_setup() initializes mnoderanges.
1304 1389 */
1305 1390 void
1306 1391 mnode_range_setup(mnoderange_t *mnoderanges)
1307 1392 {
1308 1393 mnoderange_t *mp = mnoderanges;
1309 1394 int mnode, mri;
1310 1395 int mindex = 0; /* current index into mnoderanges array */
1311 1396 int i, j;
1312 1397 pfn_t hipfn;
1313 1398 int last, hi;
1314 1399
1315 1400 for (mnode = 0; mnode < max_mem_nodes; mnode++) {
1316 1401 if (mem_node_config[mnode].exists == 0)
1317 1402 continue;
1318 1403
1319 1404 mri = nranges - 1;
1320 1405
1321 1406 while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
1322 1407 mri--;
1323 1408
1324 1409 while (mri >= 0 && mem_node_config[mnode].physmax >=
1325 1410 MEMRANGELO(mri)) {
1326 1411 mnoderanges->mnr_pfnlo = MAX(MEMRANGELO(mri),
1327 1412 mem_node_config[mnode].physbase);
1328 1413 mnoderanges->mnr_pfnhi = MIN(MEMRANGEHI(mri),
1329 1414 mem_node_config[mnode].physmax);
1330 1415 mnoderanges->mnr_mnode = mnode;
1331 1416 mnoderanges->mnr_memrange = mri;
1332 1417 mnoderanges->mnr_exists = 1;
1333 1418 mnoderanges++;
1334 1419 mindex++;
1335 1420 if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
1336 1421 mri--;
1337 1422 else
1338 1423 break;
1339 1424 }
1340 1425 }
1341 1426
1342 1427 /*
1343 1428 * For now do a simple sort of the mnoderanges array to fill in
1344 1429 * the mnr_next fields. Since mindex is expected to be relatively
1345 1430 * small, using a simple O(N^2) algorithm.
1346 1431 */
1347 1432 for (i = 0; i < mindex; i++) {
1348 1433 if (mp[i].mnr_pfnlo == 0) /* find lowest */
1349 1434 break;
1350 1435 }
1351 1436 ASSERT(i < mindex);
1352 1437 last = i;
1353 1438 mtype16m = last;
1354 1439 mp[last].mnr_next = -1;
1355 1440 for (i = 0; i < mindex - 1; i++) {
1356 1441 hipfn = (pfn_t)(-1);
1357 1442 hi = -1;
1358 1443 /* find next highest mnode range */
1359 1444 for (j = 0; j < mindex; j++) {
1360 1445 if (mp[j].mnr_pfnlo > mp[last].mnr_pfnlo &&
1361 1446 mp[j].mnr_pfnlo < hipfn) {
1362 1447 hipfn = mp[j].mnr_pfnlo;
1363 1448 hi = j;
1364 1449 }
1365 1450 }
1366 1451 mp[hi].mnr_next = last;
1367 1452 last = hi;
1368 1453 }
1369 1454 mtypetop = last;
1370 1455 }
1371 1456
1372 1457 #ifndef __xpv
1373 1458 /*
1374 1459 * Update mnoderanges for memory hot-add DR operations.
1375 1460 */
1376 1461 static void
1377 1462 mnode_range_add(int mnode)
1378 1463 {
1379 1464 int *prev;
1380 1465 int n, mri;
1381 1466 pfn_t start, end;
1382 1467 extern void membar_sync(void);
1383 1468
1384 1469 ASSERT(0 <= mnode && mnode < max_mem_nodes);
1385 1470 ASSERT(mem_node_config[mnode].exists);
1386 1471 start = mem_node_config[mnode].physbase;
1387 1472 end = mem_node_config[mnode].physmax;
1388 1473 ASSERT(start <= end);
1389 1474 mutex_enter(&mnoderange_lock);
1390 1475
1391 1476 #ifdef DEBUG
1392 1477 /* Check whether it interleaves with other memory nodes. */
1393 1478 for (n = mtypetop; n != -1; n = mnoderanges[n].mnr_next) {
1394 1479 ASSERT(mnoderanges[n].mnr_exists);
1395 1480 if (mnoderanges[n].mnr_mnode == mnode)
1396 1481 continue;
1397 1482 ASSERT(start > mnoderanges[n].mnr_pfnhi ||
1398 1483 end < mnoderanges[n].mnr_pfnlo);
1399 1484 }
1400 1485 #endif /* DEBUG */
1401 1486
1402 1487 mri = nranges - 1;
1403 1488 while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
1404 1489 mri--;
1405 1490 while (mri >= 0 && mem_node_config[mnode].physmax >= MEMRANGELO(mri)) {
1406 1491 /* Check whether mtype already exists. */
1407 1492 for (n = mtypetop; n != -1; n = mnoderanges[n].mnr_next) {
1408 1493 if (mnoderanges[n].mnr_mnode == mnode &&
1409 1494 mnoderanges[n].mnr_memrange == mri) {
1410 1495 mnoderanges[n].mnr_pfnlo = MAX(MEMRANGELO(mri),
1411 1496 start);
1412 1497 mnoderanges[n].mnr_pfnhi = MIN(MEMRANGEHI(mri),
1413 1498 end);
1414 1499 break;
1415 1500 }
1416 1501 }
1417 1502
1418 1503 /* Add a new entry if it doesn't exist yet. */
1419 1504 if (n == -1) {
1420 1505 /* Try to find an unused entry in mnoderanges array. */
1421 1506 for (n = 0; n < mnoderangecnt; n++) {
1422 1507 if (mnoderanges[n].mnr_exists == 0)
1423 1508 break;
1424 1509 }
1425 1510 ASSERT(n < mnoderangecnt);
1426 1511 mnoderanges[n].mnr_pfnlo = MAX(MEMRANGELO(mri), start);
1427 1512 mnoderanges[n].mnr_pfnhi = MIN(MEMRANGEHI(mri), end);
1428 1513 mnoderanges[n].mnr_mnode = mnode;
1429 1514 mnoderanges[n].mnr_memrange = mri;
1430 1515 mnoderanges[n].mnr_exists = 1;
1431 1516 /* Page 0 should always be present. */
1432 1517 for (prev = &mtypetop;
1433 1518 mnoderanges[*prev].mnr_pfnlo > start;
1434 1519 prev = &mnoderanges[*prev].mnr_next) {
1435 1520 ASSERT(mnoderanges[*prev].mnr_next >= 0);
1436 1521 ASSERT(mnoderanges[*prev].mnr_pfnlo > end);
1437 1522 }
1438 1523 mnoderanges[n].mnr_next = *prev;
1439 1524 membar_sync();
1440 1525 *prev = n;
1441 1526 }
1442 1527
1443 1528 if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
1444 1529 mri--;
1445 1530 else
1446 1531 break;
1447 1532 }
1448 1533
1449 1534 mutex_exit(&mnoderange_lock);
1450 1535 }
1451 1536
1452 1537 /*
1453 1538 * Update mnoderanges for memory hot-removal DR operations.
1454 1539 */
1455 1540 static void
1456 1541 mnode_range_del(int mnode)
1457 1542 {
1458 1543 _NOTE(ARGUNUSED(mnode));
1459 1544 ASSERT(0 <= mnode && mnode < max_mem_nodes);
1460 1545 /* TODO: support deletion operation. */
1461 1546 ASSERT(0);
1462 1547 }
1463 1548
1464 1549 void
1465 1550 plat_slice_add(pfn_t start, pfn_t end)
1466 1551 {
1467 1552 mem_node_add_slice(start, end);
1468 1553 if (plat_dr_enabled()) {
1469 1554 mnode_range_add(PFN_2_MEM_NODE(start));
1470 1555 }
1471 1556 }
1472 1557
1473 1558 void
1474 1559 plat_slice_del(pfn_t start, pfn_t end)
1475 1560 {
1476 1561 ASSERT(PFN_2_MEM_NODE(start) == PFN_2_MEM_NODE(end));
1477 1562 ASSERT(plat_dr_enabled());
1478 1563 mnode_range_del(PFN_2_MEM_NODE(start));
1479 1564 mem_node_del_slice(start, end);
1480 1565 }
1481 1566 #endif /* __xpv */
1482 1567
1483 1568 /*ARGSUSED*/
1484 1569 int
1485 1570 mtype_init(vnode_t *vp, caddr_t vaddr, uint_t *flags, size_t pgsz)
1486 1571 {
1487 1572 int mtype = mtypetop;
1488 1573
1489 1574 #if !defined(__xpv)
1490 1575 #if defined(__i386)
1491 1576 /*
1492 1577 * set the mtype range
1493 1578 * - kmem requests need to be below 4g if restricted_kmemalloc is set.
1494 1579 * - for non kmem requests, set range to above 4g if memory below 4g
1495 1580 * runs low.
1496 1581 */
1497 1582 if (restricted_kmemalloc && VN_ISKAS(vp) &&
1498 1583 (caddr_t)(vaddr) >= kernelheap &&
1499 1584 (caddr_t)(vaddr) < ekernelheap) {
1500 1585 ASSERT(physmax4g);
1501 1586 mtype = mtype4g;
1502 1587 if (RESTRICT16M_ALLOC(freemem4g - btop(pgsz),
1503 1588 btop(pgsz), *flags)) {
1504 1589 *flags |= PGI_MT_RANGE16M;
1505 1590 } else {
1506 1591 VM_STAT_ADD(vmm_vmstats.unrestrict16mcnt);
1507 1592 VM_STAT_COND_ADD((*flags & PG_PANIC),
1508 1593 vmm_vmstats.pgpanicalloc);
1509 1594 *flags |= PGI_MT_RANGE0;
1510 1595 }
1511 1596 return (mtype);
1512 1597 }
1513 1598 #endif /* __i386 */
1514 1599
1515 1600 if (RESTRICT4G_ALLOC) {
1516 1601 VM_STAT_ADD(vmm_vmstats.restrict4gcnt);
1517 1602 /* here only for > 4g systems */
1518 1603 *flags |= PGI_MT_RANGE4G;
1519 1604 } else if (RESTRICT16M_ALLOC(freemem, btop(pgsz), *flags)) {
1520 1605 *flags |= PGI_MT_RANGE16M;
1521 1606 } else {
1522 1607 VM_STAT_ADD(vmm_vmstats.unrestrict16mcnt);
1523 1608 VM_STAT_COND_ADD((*flags & PG_PANIC), vmm_vmstats.pgpanicalloc);
1524 1609 *flags |= PGI_MT_RANGE0;
1525 1610 }
1526 1611 #endif /* !__xpv */
1527 1612 return (mtype);
1528 1613 }
1529 1614
1530 1615
1531 1616 /* mtype init for page_get_replacement_page */
1532 1617 /*ARGSUSED*/
1533 1618 int
1534 1619 mtype_pgr_init(int *flags, page_t *pp, int mnode, pgcnt_t pgcnt)
1535 1620 {
1536 1621 int mtype = mtypetop;
1537 1622 #if !defined(__xpv)
1538 1623 if (RESTRICT16M_ALLOC(freemem, pgcnt, *flags)) {
1539 1624 *flags |= PGI_MT_RANGE16M;
1540 1625 } else {
1541 1626 VM_STAT_ADD(vmm_vmstats.unrestrict16mcnt);
1542 1627 *flags |= PGI_MT_RANGE0;
1543 1628 }
1544 1629 #endif
1545 1630 return (mtype);
1546 1631 }
1547 1632
1548 1633 /*
1549 1634 * Determine if the mnode range specified in mtype contains memory belonging
1550 1635 * to memory node mnode. If flags & PGI_MT_RANGE is set then mtype contains
1551 1636 * the range from high pfn to 0, 16m or 4g.
1552 1637 *
1553 1638 * Return first mnode range type index found otherwise return -1 if none found.
1554 1639 */
1555 1640 int
1556 1641 mtype_func(int mnode, int mtype, uint_t flags)
1557 1642 {
1558 1643 if (flags & PGI_MT_RANGE) {
1559 1644 int mnr_lim = MRI_0;
1560 1645
1561 1646 if (flags & PGI_MT_NEXT) {
1562 1647 mtype = mnoderanges[mtype].mnr_next;
1563 1648 }
1564 1649 if (flags & PGI_MT_RANGE4G)
1565 1650 mnr_lim = MRI_4G; /* exclude 0-4g range */
1566 1651 else if (flags & PGI_MT_RANGE16M)
1567 1652 mnr_lim = MRI_16M; /* exclude 0-16m range */
1568 1653 while (mtype != -1 &&
1569 1654 mnoderanges[mtype].mnr_memrange <= mnr_lim) {
1570 1655 if (mnoderanges[mtype].mnr_mnode == mnode)
1571 1656 return (mtype);
1572 1657 mtype = mnoderanges[mtype].mnr_next;
1573 1658 }
1574 1659 } else if (mnoderanges[mtype].mnr_mnode == mnode) {
1575 1660 return (mtype);
1576 1661 }
1577 1662 return (-1);
1578 1663 }
1579 1664
1580 1665 /*
1581 1666 * Update the page list max counts with the pfn range specified by the
1582 1667 * input parameters.
1583 1668 */
1584 1669 void
1585 1670 mtype_modify_max(pfn_t startpfn, long cnt)
1586 1671 {
1587 1672 int mtype;
1588 1673 pgcnt_t inc;
1589 1674 spgcnt_t scnt = (spgcnt_t)(cnt);
1590 1675 pgcnt_t acnt = ABS(scnt);
1591 1676 pfn_t endpfn = startpfn + acnt;
1592 1677 pfn_t pfn, lo;
1593 1678
1594 1679 if (!physmax4g)
1595 1680 return;
1596 1681
1597 1682 mtype = mtypetop;
1598 1683 for (pfn = endpfn; pfn > startpfn; ) {
1599 1684 ASSERT(mtype != -1);
1600 1685 lo = mnoderanges[mtype].mnr_pfnlo;
1601 1686 if (pfn > lo) {
1602 1687 if (startpfn >= lo) {
1603 1688 inc = pfn - startpfn;
1604 1689 } else {
1605 1690 inc = pfn - lo;
1606 1691 }
1607 1692 if (mnoderanges[mtype].mnr_memrange != MRI_4G) {
1608 1693 if (scnt > 0)
1609 1694 maxmem4g += inc;
1610 1695 else
1611 1696 maxmem4g -= inc;
1612 1697 }
1613 1698 pfn -= inc;
1614 1699 }
1615 1700 mtype = mnoderanges[mtype].mnr_next;
1616 1701 }
1617 1702 }
1618 1703
1619 1704 int
1620 1705 mtype_2_mrange(int mtype)
1621 1706 {
1622 1707 return (mnoderanges[mtype].mnr_memrange);
1623 1708 }
1624 1709
1625 1710 void
1626 1711 mnodetype_2_pfn(int mnode, int mtype, pfn_t *pfnlo, pfn_t *pfnhi)
1627 1712 {
1628 1713 _NOTE(ARGUNUSED(mnode));
1629 1714 ASSERT(mnoderanges[mtype].mnr_mnode == mnode);
1630 1715 *pfnlo = mnoderanges[mtype].mnr_pfnlo;
1631 1716 *pfnhi = mnoderanges[mtype].mnr_pfnhi;
1632 1717 }
1633 1718
1634 1719 size_t
1635 1720 plcnt_sz(size_t ctrs_sz)
1636 1721 {
1637 1722 #ifdef DEBUG
1638 1723 int szc, colors;
1639 1724
1640 1725 ctrs_sz += mnoderangecnt * sizeof (struct mnr_mts) * mmu_page_sizes;
1641 1726 for (szc = 0; szc < mmu_page_sizes; szc++) {
1642 1727 colors = page_get_pagecolors(szc);
1643 1728 ctrs_sz += mnoderangecnt * sizeof (pgcnt_t) * colors;
1644 1729 }
1645 1730 #endif
1646 1731 return (ctrs_sz);
1647 1732 }
1648 1733
1649 1734 caddr_t
1650 1735 plcnt_init(caddr_t addr)
1651 1736 {
1652 1737 #ifdef DEBUG
1653 1738 int mt, szc, colors;
1654 1739
1655 1740 for (mt = 0; mt < mnoderangecnt; mt++) {
1656 1741 mnoderanges[mt].mnr_mts = (struct mnr_mts *)addr;
1657 1742 addr += (sizeof (struct mnr_mts) * mmu_page_sizes);
1658 1743 for (szc = 0; szc < mmu_page_sizes; szc++) {
1659 1744 colors = page_get_pagecolors(szc);
1660 1745 mnoderanges[mt].mnr_mts[szc].mnr_mts_colors = colors;
1661 1746 mnoderanges[mt].mnr_mts[szc].mnr_mtsc_pgcnt =
1662 1747 (pgcnt_t *)addr;
1663 1748 addr += (sizeof (pgcnt_t) * colors);
1664 1749 }
1665 1750 }
1666 1751 #endif
1667 1752 return (addr);
1668 1753 }
1669 1754
1670 1755 void
1671 1756 plcnt_inc_dec(page_t *pp, int mtype, int szc, long cnt, int flags)
1672 1757 {
1673 1758 _NOTE(ARGUNUSED(pp));
1674 1759 #ifdef DEBUG
1675 1760 int bin = PP_2_BIN(pp);
1676 1761
1677 1762 atomic_add_long(&mnoderanges[mtype].mnr_mts[szc].mnr_mts_pgcnt, cnt);
1678 1763 atomic_add_long(&mnoderanges[mtype].mnr_mts[szc].mnr_mtsc_pgcnt[bin],
1679 1764 cnt);
1680 1765 #endif
1681 1766 ASSERT(mtype == PP_2_MTYPE(pp));
1682 1767 if (physmax4g && mnoderanges[mtype].mnr_memrange != MRI_4G)
1683 1768 atomic_add_long(&freemem4g, cnt);
1684 1769 if (flags & PG_CACHE_LIST)
1685 1770 atomic_add_long(&mnoderanges[mtype].mnr_mt_clpgcnt, cnt);
1686 1771 else
1687 1772 atomic_add_long(&mnoderanges[mtype].mnr_mt_flpgcnt[szc], cnt);
1688 1773 atomic_add_long(&mnoderanges[mtype].mnr_mt_totcnt, cnt);
1689 1774 }
1690 1775
1691 1776 /*
1692 1777 * Returns the free page count for mnode
1693 1778 */
1694 1779 int
1695 1780 mnode_pgcnt(int mnode)
1696 1781 {
1697 1782 int mtype = mtypetop;
1698 1783 int flags = PGI_MT_RANGE0;
1699 1784 pgcnt_t pgcnt = 0;
1700 1785
1701 1786 mtype = mtype_func(mnode, mtype, flags);
1702 1787
1703 1788 while (mtype != -1) {
1704 1789 pgcnt += MTYPE_FREEMEM(mtype);
1705 1790 mtype = mtype_func(mnode, mtype, flags | PGI_MT_NEXT);
1706 1791 }
1707 1792 return (pgcnt);
1708 1793 }
1709 1794
1710 1795 /*
1711 1796 * Initialize page coloring variables based on the l2 cache parameters.
1712 1797 * Calculate and return memory needed for page coloring data structures.
1713 1798 */
1714 1799 size_t
1715 1800 page_coloring_init(uint_t l2_sz, int l2_linesz, int l2_assoc)
1716 1801 {
1717 1802 _NOTE(ARGUNUSED(l2_linesz));
1718 1803 size_t colorsz = 0;
1719 1804 int i;
1720 1805 int colors;
1721 1806
1722 1807 #if defined(__xpv)
1723 1808 /*
1724 1809 * Hypervisor domains currently don't have any concept of NUMA.
1725 1810 * Hence we'll act like there is only 1 memrange.
1726 1811 */
1727 1812 i = memrange_num(1);
1728 1813 #else /* !__xpv */
1729 1814 /*
1730 1815 * Reduce the memory ranges lists if we don't have large amounts
1731 1816 * of memory. This avoids searching known empty free lists.
1732 1817 * To support memory DR operations, we need to keep memory ranges
1733 1818 * for possible memory hot-add operations.
1734 1819 */
1735 1820 if (plat_dr_physmax > physmax)
1736 1821 i = memrange_num(plat_dr_physmax);
1737 1822 else
1738 1823 i = memrange_num(physmax);
1739 1824 #if defined(__i386)
1740 1825 if (i > MRI_4G)
1741 1826 restricted_kmemalloc = 0;
1742 1827 #endif
1743 1828 /* physmax greater than 4g */
1744 1829 if (i == MRI_4G)
1745 1830 physmax4g = 1;
1746 1831 #endif /* !__xpv */
1747 1832 memranges += i;
1748 1833 nranges -= i;
1749 1834
1750 1835 ASSERT(mmu_page_sizes <= MMU_PAGE_SIZES);
1751 1836
1752 1837 ASSERT(ISP2(l2_linesz));
1753 1838 ASSERT(l2_sz > MMU_PAGESIZE);
1754 1839
1755 1840 /* l2_assoc is 0 for fully associative l2 cache */
1756 1841 if (l2_assoc)
1757 1842 l2_colors = MAX(1, l2_sz / (l2_assoc * MMU_PAGESIZE));
1758 1843 else
1759 1844 l2_colors = 1;
1760 1845
1761 1846 ASSERT(ISP2(l2_colors));
1762 1847
1763 1848 /* for scalability, configure at least PAGE_COLORS_MIN color bins */
1764 1849 page_colors = MAX(l2_colors, PAGE_COLORS_MIN);
1765 1850
1766 1851 /*
1767 1852 * cpu_page_colors is non-zero when a page color may be spread across
1768 1853 * multiple bins.
1769 1854 */
1770 1855 if (l2_colors < page_colors)
1771 1856 cpu_page_colors = l2_colors;
1772 1857
1773 1858 ASSERT(ISP2(page_colors));
1774 1859
1775 1860 page_colors_mask = page_colors - 1;
1776 1861
1777 1862 ASSERT(ISP2(CPUSETSIZE()));
1778 1863 page_coloring_shift = lowbit(CPUSETSIZE());
1779 1864
1780 1865 /* initialize number of colors per page size */
1781 1866 for (i = 0; i <= mmu.max_page_level; i++) {
1782 1867 hw_page_array[i].hp_size = LEVEL_SIZE(i);
1783 1868 hw_page_array[i].hp_shift = LEVEL_SHIFT(i);
1784 1869 hw_page_array[i].hp_pgcnt = LEVEL_SIZE(i) >> LEVEL_SHIFT(0);
1785 1870 hw_page_array[i].hp_colors = (page_colors_mask >>
1786 1871 (hw_page_array[i].hp_shift - hw_page_array[0].hp_shift))
1787 1872 + 1;
1788 1873 colorequivszc[i] = 0;
1789 1874 }
1790 1875
1791 1876 /*
1792 1877 * The value of cpu_page_colors determines if additional color bins
1793 1878 * need to be checked for a particular color in the page_get routines.
1794 1879 */
1795 1880 if (cpu_page_colors != 0) {
1796 1881
1797 1882 int a = lowbit(page_colors) - lowbit(cpu_page_colors);
1798 1883 ASSERT(a > 0);
1799 1884 ASSERT(a < 16);
1800 1885
1801 1886 for (i = 0; i <= mmu.max_page_level; i++) {
1802 1887 if ((colors = hw_page_array[i].hp_colors) <= 1) {
1803 1888 colorequivszc[i] = 0;
1804 1889 continue;
1805 1890 }
1806 1891 while ((colors >> a) == 0)
1807 1892 a--;
1808 1893 ASSERT(a >= 0);
1809 1894
1810 1895 /* higher 4 bits encodes color equiv mask */
1811 1896 colorequivszc[i] = (a << 4);
1812 1897 }
1813 1898 }
1814 1899
1815 1900 /* factor in colorequiv to check additional 'equivalent' bins. */
1816 1901 if (colorequiv > 1) {
1817 1902
1818 1903 int a = lowbit(colorequiv) - 1;
1819 1904 if (a > 15)
1820 1905 a = 15;
1821 1906
1822 1907 for (i = 0; i <= mmu.max_page_level; i++) {
1823 1908 if ((colors = hw_page_array[i].hp_colors) <= 1) {
1824 1909 continue;
1825 1910 }
1826 1911 while ((colors >> a) == 0)
1827 1912 a--;
1828 1913 if ((a << 4) > colorequivszc[i]) {
1829 1914 colorequivszc[i] = (a << 4);
1830 1915 }
1831 1916 }
1832 1917 }
1833 1918
1834 1919 /* size for mnoderanges */
1835 1920 for (mnoderangecnt = 0, i = 0; i < max_mem_nodes; i++)
1836 1921 mnoderangecnt += mnode_range_cnt(i);
1837 1922 if (plat_dr_support_memory()) {
1838 1923 /*
1839 1924 * Reserve enough space for memory DR operations.
1840 1925 * Two extra mnoderanges for possbile fragmentations,
1841 1926 * one for the 2G boundary and the other for the 4G boundary.
1842 1927 * We don't expect a memory board crossing the 16M boundary
1843 1928 * for memory hot-add operations on x86 platforms.
1844 1929 */
1845 1930 mnoderangecnt += 2 + max_mem_nodes - lgrp_plat_node_cnt;
1846 1931 }
1847 1932 colorsz = mnoderangecnt * sizeof (mnoderange_t);
1848 1933
1849 1934 /* size for fpc_mutex and cpc_mutex */
1850 1935 colorsz += (2 * max_mem_nodes * sizeof (kmutex_t) * NPC_MUTEX);
1851 1936
1852 1937 /* size of page_freelists */
1853 1938 colorsz += mnoderangecnt * sizeof (page_t ***);
1854 1939 colorsz += mnoderangecnt * mmu_page_sizes * sizeof (page_t **);
1855 1940
1856 1941 for (i = 0; i < mmu_page_sizes; i++) {
1857 1942 colors = page_get_pagecolors(i);
1858 1943 colorsz += mnoderangecnt * colors * sizeof (page_t *);
1859 1944 }
1860 1945
1861 1946 /* size of page_cachelists */
1862 1947 colorsz += mnoderangecnt * sizeof (page_t **);
1863 1948 colorsz += mnoderangecnt * page_colors * sizeof (page_t *);
1864 1949
1865 1950 return (colorsz);
1866 1951 }
1867 1952
1868 1953 /*
1869 1954 * Called once at startup to configure page_coloring data structures and
1870 1955 * does the 1st page_free()/page_freelist_add().
1871 1956 */
1872 1957 void
1873 1958 page_coloring_setup(caddr_t pcmemaddr)
1874 1959 {
1875 1960 int i;
1876 1961 int j;
1877 1962 int k;
1878 1963 caddr_t addr;
1879 1964 int colors;
1880 1965
1881 1966 /*
1882 1967 * do page coloring setup
1883 1968 */
1884 1969 addr = pcmemaddr;
1885 1970
1886 1971 mnoderanges = (mnoderange_t *)addr;
1887 1972 addr += (mnoderangecnt * sizeof (mnoderange_t));
1888 1973
1889 1974 mnode_range_setup(mnoderanges);
1890 1975
1891 1976 if (physmax4g)
1892 1977 mtype4g = pfn_2_mtype(0xfffff);
1893 1978
1894 1979 for (k = 0; k < NPC_MUTEX; k++) {
1895 1980 fpc_mutex[k] = (kmutex_t *)addr;
1896 1981 addr += (max_mem_nodes * sizeof (kmutex_t));
1897 1982 }
1898 1983 for (k = 0; k < NPC_MUTEX; k++) {
1899 1984 cpc_mutex[k] = (kmutex_t *)addr;
1900 1985 addr += (max_mem_nodes * sizeof (kmutex_t));
1901 1986 }
1902 1987 page_freelists = (page_t ****)addr;
1903 1988 addr += (mnoderangecnt * sizeof (page_t ***));
1904 1989
1905 1990 page_cachelists = (page_t ***)addr;
1906 1991 addr += (mnoderangecnt * sizeof (page_t **));
1907 1992
1908 1993 for (i = 0; i < mnoderangecnt; i++) {
1909 1994 page_freelists[i] = (page_t ***)addr;
1910 1995 addr += (mmu_page_sizes * sizeof (page_t **));
1911 1996
1912 1997 for (j = 0; j < mmu_page_sizes; j++) {
1913 1998 colors = page_get_pagecolors(j);
1914 1999 page_freelists[i][j] = (page_t **)addr;
1915 2000 addr += (colors * sizeof (page_t *));
1916 2001 }
1917 2002 page_cachelists[i] = (page_t **)addr;
1918 2003 addr += (page_colors * sizeof (page_t *));
1919 2004 }
1920 2005 }
1921 2006
1922 2007 #if defined(__xpv)
1923 2008 /*
1924 2009 * Give back 10% of the io_pool pages to the free list.
1925 2010 * Don't shrink the pool below some absolute minimum.
1926 2011 */
1927 2012 static void
1928 2013 page_io_pool_shrink()
1929 2014 {
1930 2015 int retcnt;
1931 2016 page_t *pp, *pp_first, *pp_last, **curpool;
1932 2017 mfn_t mfn;
1933 2018 int bothpools = 0;
1934 2019
1935 2020 mutex_enter(&io_pool_lock);
1936 2021 io_pool_shrink_attempts++; /* should be a kstat? */
1937 2022 retcnt = io_pool_cnt / 10;
1938 2023 if (io_pool_cnt - retcnt < io_pool_cnt_min)
1939 2024 retcnt = io_pool_cnt - io_pool_cnt_min;
1940 2025 if (retcnt <= 0)
1941 2026 goto done;
1942 2027 io_pool_shrinks++; /* should be a kstat? */
1943 2028 curpool = &io_pool_4g;
1944 2029 domore:
1945 2030 /*
1946 2031 * Loop through taking pages from the end of the list
1947 2032 * (highest mfns) till amount to return reached.
1948 2033 */
1949 2034 for (pp = *curpool; pp && retcnt > 0; ) {
1950 2035 pp_first = pp_last = pp->p_prev;
1951 2036 if (pp_first == *curpool)
1952 2037 break;
1953 2038 retcnt--;
1954 2039 io_pool_cnt--;
1955 2040 page_io_pool_sub(curpool, pp_first, pp_last);
1956 2041 if ((mfn = pfn_to_mfn(pp->p_pagenum)) < start_mfn)
1957 2042 start_mfn = mfn;
1958 2043 page_free(pp_first, 1);
1959 2044 pp = *curpool;
1960 2045 }
1961 2046 if (retcnt != 0 && !bothpools) {
1962 2047 /*
1963 2048 * If not enough found in less constrained pool try the
1964 2049 * more constrained one.
1965 2050 */
1966 2051 curpool = &io_pool_16m;
1967 2052 bothpools = 1;
1968 2053 goto domore;
1969 2054 }
1970 2055 done:
1971 2056 mutex_exit(&io_pool_lock);
1972 2057 }
1973 2058
1974 2059 #endif /* __xpv */
1975 2060
1976 2061 uint_t
1977 2062 page_create_update_flags_x86(uint_t flags)
1978 2063 {
1979 2064 #if defined(__xpv)
1980 2065 /*
1981 2066 * Check this is an urgent allocation and free pages are depleted.
1982 2067 */
1983 2068 if (!(flags & PG_WAIT) && freemem < desfree)
1984 2069 page_io_pool_shrink();
1985 2070 #else /* !__xpv */
1986 2071 /*
1987 2072 * page_create_get_something may call this because 4g memory may be
1988 2073 * depleted. Set flags to allow for relocation of base page below
1989 2074 * 4g if necessary.
1990 2075 */
1991 2076 if (physmax4g)
1992 2077 flags |= (PGI_PGCPSZC0 | PGI_PGCPHIPRI);
1993 2078 #endif /* __xpv */
1994 2079 return (flags);
1995 2080 }
1996 2081
1997 2082 /*ARGSUSED*/
1998 2083 int
1999 2084 bp_color(struct buf *bp)
2000 2085 {
2001 2086 return (0);
2002 2087 }
2003 2088
2004 2089 #if defined(__xpv)
2005 2090
2006 2091 /*
2007 2092 * Take pages out of an io_pool
2008 2093 */
2009 2094 static void
2010 2095 page_io_pool_sub(page_t **poolp, page_t *pp_first, page_t *pp_last)
2011 2096 {
2012 2097 if (*poolp == pp_first) {
2013 2098 *poolp = pp_last->p_next;
2014 2099 if (*poolp == pp_first)
2015 2100 *poolp = NULL;
2016 2101 }
2017 2102 pp_first->p_prev->p_next = pp_last->p_next;
2018 2103 pp_last->p_next->p_prev = pp_first->p_prev;
2019 2104 pp_first->p_prev = pp_last;
2020 2105 pp_last->p_next = pp_first;
2021 2106 }
2022 2107
2023 2108 /*
2024 2109 * Put a page on the io_pool list. The list is ordered by increasing MFN.
2025 2110 */
2026 2111 static void
2027 2112 page_io_pool_add(page_t **poolp, page_t *pp)
2028 2113 {
2029 2114 page_t *look;
2030 2115 mfn_t mfn = mfn_list[pp->p_pagenum];
2031 2116
2032 2117 if (*poolp == NULL) {
2033 2118 *poolp = pp;
2034 2119 pp->p_next = pp;
2035 2120 pp->p_prev = pp;
2036 2121 return;
2037 2122 }
2038 2123
2039 2124 /*
2040 2125 * Since we try to take pages from the high end of the pool
2041 2126 * chances are good that the pages to be put on the list will
2042 2127 * go at or near the end of the list. so start at the end and
2043 2128 * work backwards.
2044 2129 */
2045 2130 look = (*poolp)->p_prev;
2046 2131 while (mfn < mfn_list[look->p_pagenum]) {
2047 2132 look = look->p_prev;
2048 2133 if (look == (*poolp)->p_prev)
2049 2134 break; /* backed all the way to front of list */
2050 2135 }
2051 2136
2052 2137 /* insert after look */
2053 2138 pp->p_prev = look;
2054 2139 pp->p_next = look->p_next;
2055 2140 pp->p_next->p_prev = pp;
2056 2141 look->p_next = pp;
2057 2142 if (mfn < mfn_list[(*poolp)->p_pagenum]) {
2058 2143 /*
2059 2144 * we inserted a new first list element
2060 2145 * adjust pool pointer to newly inserted element
2061 2146 */
2062 2147 *poolp = pp;
2063 2148 }
2064 2149 }
2065 2150
2066 2151 /*
2067 2152 * Add a page to the io_pool. Setting the force flag will force the page
2068 2153 * into the io_pool no matter what.
2069 2154 */
2070 2155 static void
2071 2156 add_page_to_pool(page_t *pp, int force)
2072 2157 {
2073 2158 page_t *highest;
2074 2159 page_t *freep = NULL;
2075 2160
2076 2161 mutex_enter(&io_pool_lock);
2077 2162 /*
2078 2163 * Always keep the scarce low memory pages
2079 2164 */
2080 2165 if (mfn_list[pp->p_pagenum] < PFN_16MEG) {
2081 2166 ++io_pool_cnt;
2082 2167 page_io_pool_add(&io_pool_16m, pp);
2083 2168 goto done;
2084 2169 }
2085 2170 if (io_pool_cnt < io_pool_cnt_max || force || io_pool_4g == NULL) {
2086 2171 ++io_pool_cnt;
2087 2172 page_io_pool_add(&io_pool_4g, pp);
2088 2173 } else {
2089 2174 highest = io_pool_4g->p_prev;
2090 2175 if (mfn_list[pp->p_pagenum] < mfn_list[highest->p_pagenum]) {
2091 2176 page_io_pool_sub(&io_pool_4g, highest, highest);
2092 2177 page_io_pool_add(&io_pool_4g, pp);
2093 2178 freep = highest;
2094 2179 } else {
2095 2180 freep = pp;
2096 2181 }
2097 2182 }
2098 2183 done:
2099 2184 mutex_exit(&io_pool_lock);
2100 2185 if (freep)
2101 2186 page_free(freep, 1);
2102 2187 }
2103 2188
2104 2189
2105 2190 int contig_pfn_cnt; /* no of pfns in the contig pfn list */
2106 2191 int contig_pfn_max; /* capacity of the contig pfn list */
2107 2192 int next_alloc_pfn; /* next position in list to start a contig search */
2108 2193 int contig_pfnlist_updates; /* pfn list update count */
2109 2194 int contig_pfnlist_builds; /* how many times have we (re)built list */
2110 2195 int contig_pfnlist_buildfailed; /* how many times has list build failed */
2111 2196 int create_contig_pending; /* nonzero means taskq creating contig list */
2112 2197 pfn_t *contig_pfn_list = NULL; /* list of contig pfns in ascending mfn order */
2113 2198
2114 2199 /*
2115 2200 * Function to use in sorting a list of pfns by their underlying mfns.
2116 2201 */
2117 2202 static int
2118 2203 mfn_compare(const void *pfnp1, const void *pfnp2)
2119 2204 {
2120 2205 mfn_t mfn1 = mfn_list[*(pfn_t *)pfnp1];
2121 2206 mfn_t mfn2 = mfn_list[*(pfn_t *)pfnp2];
2122 2207
2123 2208 if (mfn1 > mfn2)
2124 2209 return (1);
2125 2210 if (mfn1 < mfn2)
2126 2211 return (-1);
2127 2212 return (0);
2128 2213 }
2129 2214
2130 2215 /*
2131 2216 * Compact the contig_pfn_list by tossing all the non-contiguous
2132 2217 * elements from the list.
2133 2218 */
2134 2219 static void
2135 2220 compact_contig_pfn_list(void)
2136 2221 {
2137 2222 pfn_t pfn, lapfn, prev_lapfn;
2138 2223 mfn_t mfn;
2139 2224 int i, newcnt = 0;
2140 2225
2141 2226 prev_lapfn = 0;
2142 2227 for (i = 0; i < contig_pfn_cnt - 1; i++) {
2143 2228 pfn = contig_pfn_list[i];
2144 2229 lapfn = contig_pfn_list[i + 1];
2145 2230 mfn = mfn_list[pfn];
2146 2231 /*
2147 2232 * See if next pfn is for a contig mfn
2148 2233 */
2149 2234 if (mfn_list[lapfn] != mfn + 1)
2150 2235 continue;
2151 2236 /*
2152 2237 * pfn and lookahead are both put in list
2153 2238 * unless pfn is the previous lookahead.
2154 2239 */
2155 2240 if (pfn != prev_lapfn)
2156 2241 contig_pfn_list[newcnt++] = pfn;
2157 2242 contig_pfn_list[newcnt++] = lapfn;
2158 2243 prev_lapfn = lapfn;
2159 2244 }
2160 2245 for (i = newcnt; i < contig_pfn_cnt; i++)
2161 2246 contig_pfn_list[i] = 0;
2162 2247 contig_pfn_cnt = newcnt;
2163 2248 }
2164 2249
2165 2250 /*ARGSUSED*/
2166 2251 static void
2167 2252 call_create_contiglist(void *arg)
2168 2253 {
2169 2254 (void) create_contig_pfnlist(PG_WAIT);
2170 2255 }
2171 2256
2172 2257 /*
2173 2258 * Create list of freelist pfns that have underlying
2174 2259 * contiguous mfns. The list is kept in ascending mfn order.
2175 2260 * returns 1 if list created else 0.
2176 2261 */
2177 2262 static int
2178 2263 create_contig_pfnlist(uint_t flags)
2179 2264 {
2180 2265 pfn_t pfn;
2181 2266 page_t *pp;
2182 2267 int ret = 1;
2183 2268
2184 2269 mutex_enter(&contig_list_lock);
2185 2270 if (contig_pfn_list != NULL)
2186 2271 goto out;
2187 2272 contig_pfn_max = freemem + (freemem / 10);
2188 2273 contig_pfn_list = kmem_zalloc(contig_pfn_max * sizeof (pfn_t),
2189 2274 (flags & PG_WAIT) ? KM_SLEEP : KM_NOSLEEP);
2190 2275 if (contig_pfn_list == NULL) {
2191 2276 /*
2192 2277 * If we could not create the contig list (because
2193 2278 * we could not sleep for memory). Dispatch a taskq that can
2194 2279 * sleep to get the memory.
2195 2280 */
2196 2281 if (!create_contig_pending) {
2197 2282 if (taskq_dispatch(system_taskq, call_create_contiglist,
2198 2283 NULL, TQ_NOSLEEP) != NULL)
2199 2284 create_contig_pending = 1;
2200 2285 }
2201 2286 contig_pfnlist_buildfailed++; /* count list build failures */
2202 2287 ret = 0;
2203 2288 goto out;
2204 2289 }
2205 2290 create_contig_pending = 0;
2206 2291 ASSERT(contig_pfn_cnt == 0);
2207 2292 for (pfn = 0; pfn < mfn_count; pfn++) {
2208 2293 pp = page_numtopp_nolock(pfn);
2209 2294 if (pp == NULL || !PP_ISFREE(pp))
2210 2295 continue;
2211 2296 contig_pfn_list[contig_pfn_cnt] = pfn;
2212 2297 if (++contig_pfn_cnt == contig_pfn_max)
2213 2298 break;
2214 2299 }
2215 2300 /*
2216 2301 * Sanity check the new list.
2217 2302 */
2218 2303 if (contig_pfn_cnt < 2) { /* no contig pfns */
2219 2304 contig_pfn_cnt = 0;
2220 2305 contig_pfnlist_buildfailed++;
2221 2306 kmem_free(contig_pfn_list, contig_pfn_max * sizeof (pfn_t));
2222 2307 contig_pfn_list = NULL;
2223 2308 contig_pfn_max = 0;
2224 2309 ret = 0;
2225 2310 goto out;
2226 2311 }
2227 2312 qsort(contig_pfn_list, contig_pfn_cnt, sizeof (pfn_t), mfn_compare);
2228 2313 compact_contig_pfn_list();
2229 2314 /*
2230 2315 * Make sure next search of the newly created contiguous pfn
2231 2316 * list starts at the beginning of the list.
2232 2317 */
2233 2318 next_alloc_pfn = 0;
2234 2319 contig_pfnlist_builds++; /* count list builds */
2235 2320 out:
2236 2321 mutex_exit(&contig_list_lock);
2237 2322 return (ret);
2238 2323 }
2239 2324
2240 2325
2241 2326 /*
2242 2327 * Toss the current contig pfnlist. Someone is about to do a massive
2243 2328 * update to pfn<->mfn mappings. So we have them destroy the list and lock
2244 2329 * it till they are done with their update.
2245 2330 */
2246 2331 void
2247 2332 clear_and_lock_contig_pfnlist()
2248 2333 {
2249 2334 pfn_t *listp = NULL;
2250 2335 size_t listsize;
2251 2336
2252 2337 mutex_enter(&contig_list_lock);
2253 2338 if (contig_pfn_list != NULL) {
2254 2339 listp = contig_pfn_list;
2255 2340 listsize = contig_pfn_max * sizeof (pfn_t);
2256 2341 contig_pfn_list = NULL;
2257 2342 contig_pfn_max = contig_pfn_cnt = 0;
2258 2343 }
2259 2344 if (listp != NULL)
2260 2345 kmem_free(listp, listsize);
2261 2346 }
2262 2347
2263 2348 /*
2264 2349 * Unlock the contig_pfn_list. The next attempted use of it will cause
2265 2350 * it to be re-created.
2266 2351 */
2267 2352 void
2268 2353 unlock_contig_pfnlist()
2269 2354 {
2270 2355 mutex_exit(&contig_list_lock);
2271 2356 }
2272 2357
2273 2358 /*
2274 2359 * Update the contiguous pfn list in response to a pfn <-> mfn reassignment
2275 2360 */
2276 2361 void
2277 2362 update_contig_pfnlist(pfn_t pfn, mfn_t oldmfn, mfn_t newmfn)
2278 2363 {
2279 2364 int probe_hi, probe_lo, probe_pos, insert_after, insert_point;
2280 2365 pfn_t probe_pfn;
2281 2366 mfn_t probe_mfn;
2282 2367 int drop_lock = 0;
2283 2368
2284 2369 if (mutex_owner(&contig_list_lock) != curthread) {
2285 2370 drop_lock = 1;
2286 2371 mutex_enter(&contig_list_lock);
2287 2372 }
2288 2373 if (contig_pfn_list == NULL)
2289 2374 goto done;
2290 2375 contig_pfnlist_updates++;
2291 2376 /*
2292 2377 * Find the pfn in the current list. Use a binary chop to locate it.
2293 2378 */
2294 2379 probe_hi = contig_pfn_cnt - 1;
2295 2380 probe_lo = 0;
2296 2381 probe_pos = (probe_hi + probe_lo) / 2;
2297 2382 while ((probe_pfn = contig_pfn_list[probe_pos]) != pfn) {
2298 2383 if (probe_pos == probe_lo) { /* pfn not in list */
2299 2384 probe_pos = -1;
2300 2385 break;
2301 2386 }
2302 2387 if (pfn_to_mfn(probe_pfn) <= oldmfn)
2303 2388 probe_lo = probe_pos;
2304 2389 else
2305 2390 probe_hi = probe_pos;
2306 2391 probe_pos = (probe_hi + probe_lo) / 2;
2307 2392 }
2308 2393 if (probe_pos >= 0) {
2309 2394 /*
2310 2395 * Remove pfn from list and ensure next alloc
2311 2396 * position stays in bounds.
2312 2397 */
2313 2398 if (--contig_pfn_cnt <= next_alloc_pfn)
2314 2399 next_alloc_pfn = 0;
2315 2400 if (contig_pfn_cnt < 2) { /* no contig pfns */
2316 2401 contig_pfn_cnt = 0;
2317 2402 kmem_free(contig_pfn_list,
2318 2403 contig_pfn_max * sizeof (pfn_t));
2319 2404 contig_pfn_list = NULL;
2320 2405 contig_pfn_max = 0;
2321 2406 goto done;
2322 2407 }
2323 2408 ovbcopy(&contig_pfn_list[probe_pos + 1],
2324 2409 &contig_pfn_list[probe_pos],
2325 2410 (contig_pfn_cnt - probe_pos) * sizeof (pfn_t));
2326 2411 }
2327 2412 if (newmfn == MFN_INVALID)
2328 2413 goto done;
2329 2414 /*
2330 2415 * Check if new mfn has adjacent mfns in the list
2331 2416 */
2332 2417 probe_hi = contig_pfn_cnt - 1;
2333 2418 probe_lo = 0;
2334 2419 insert_after = -2;
2335 2420 do {
2336 2421 probe_pos = (probe_hi + probe_lo) / 2;
2337 2422 probe_mfn = pfn_to_mfn(contig_pfn_list[probe_pos]);
2338 2423 if (newmfn == probe_mfn + 1)
2339 2424 insert_after = probe_pos;
2340 2425 else if (newmfn == probe_mfn - 1)
2341 2426 insert_after = probe_pos - 1;
2342 2427 if (probe_pos == probe_lo)
2343 2428 break;
2344 2429 if (probe_mfn <= newmfn)
2345 2430 probe_lo = probe_pos;
2346 2431 else
2347 2432 probe_hi = probe_pos;
2348 2433 } while (insert_after == -2);
2349 2434 /*
2350 2435 * If there is space in the list and there are adjacent mfns
2351 2436 * insert the pfn in to its proper place in the list.
2352 2437 */
2353 2438 if (insert_after != -2 && contig_pfn_cnt + 1 <= contig_pfn_max) {
2354 2439 insert_point = insert_after + 1;
2355 2440 ovbcopy(&contig_pfn_list[insert_point],
2356 2441 &contig_pfn_list[insert_point + 1],
2357 2442 (contig_pfn_cnt - insert_point) * sizeof (pfn_t));
2358 2443 contig_pfn_list[insert_point] = pfn;
2359 2444 contig_pfn_cnt++;
2360 2445 }
2361 2446 done:
2362 2447 if (drop_lock)
2363 2448 mutex_exit(&contig_list_lock);
2364 2449 }
2365 2450
2366 2451 /*
2367 2452 * Called to (re-)populate the io_pool from the free page lists.
2368 2453 */
2369 2454 long
2370 2455 populate_io_pool(void)
2371 2456 {
2372 2457 pfn_t pfn;
2373 2458 mfn_t mfn, max_mfn;
2374 2459 page_t *pp;
2375 2460
2376 2461 /*
2377 2462 * Figure out the bounds of the pool on first invocation.
2378 2463 * We use a percentage of memory for the io pool size.
2379 2464 * we allow that to shrink, but not to less than a fixed minimum
2380 2465 */
2381 2466 if (io_pool_cnt_max == 0) {
2382 2467 io_pool_cnt_max = physmem / (100 / io_pool_physmem_pct);
2383 2468 io_pool_cnt_lowater = io_pool_cnt_max;
2384 2469 /*
2385 2470 * This is the first time in populate_io_pool, grab a va to use
2386 2471 * when we need to allocate pages.
2387 2472 */
2388 2473 io_pool_kva = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
2389 2474 }
2390 2475 /*
2391 2476 * If we are out of pages in the pool, then grow the size of the pool
2392 2477 */
2393 2478 if (io_pool_cnt == 0) {
2394 2479 /*
2395 2480 * Grow the max size of the io pool by 5%, but never more than
2396 2481 * 25% of physical memory.
2397 2482 */
2398 2483 if (io_pool_cnt_max < physmem / 4)
2399 2484 io_pool_cnt_max += io_pool_cnt_max / 20;
2400 2485 }
2401 2486 io_pool_grows++; /* should be a kstat? */
2402 2487
2403 2488 /*
2404 2489 * Get highest mfn on this platform, but limit to the 32 bit DMA max.
2405 2490 */
2406 2491 (void) mfn_to_pfn(start_mfn);
2407 2492 max_mfn = MIN(cached_max_mfn, PFN_4GIG);
2408 2493 for (mfn = start_mfn; mfn < max_mfn; start_mfn = ++mfn) {
2409 2494 pfn = mfn_to_pfn(mfn);
2410 2495 if (pfn & PFN_IS_FOREIGN_MFN)
2411 2496 continue;
2412 2497 /*
2413 2498 * try to allocate it from free pages
2414 2499 */
2415 2500 pp = page_numtopp_alloc(pfn);
2416 2501 if (pp == NULL)
2417 2502 continue;
2418 2503 PP_CLRFREE(pp);
2419 2504 add_page_to_pool(pp, 1);
2420 2505 if (io_pool_cnt >= io_pool_cnt_max)
2421 2506 break;
2422 2507 }
2423 2508
2424 2509 return (io_pool_cnt);
2425 2510 }
2426 2511
2427 2512 /*
2428 2513 * Destroy a page that was being used for DMA I/O. It may or
2429 2514 * may not actually go back to the io_pool.
2430 2515 */
2431 2516 void
2432 2517 page_destroy_io(page_t *pp)
2433 2518 {
2434 2519 mfn_t mfn = mfn_list[pp->p_pagenum];
2435 2520
2436 2521 /*
2437 2522 * When the page was alloc'd a reservation was made, release it now
2438 2523 */
2439 2524 page_unresv(1);
2440 2525 /*
2441 2526 * Unload translations, if any, then hash out the
2442 2527 * page to erase its identity.
2443 2528 */
2444 2529 (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
2445 2530 page_hashout(pp, NULL);
2446 2531
2447 2532 /*
2448 2533 * If the page came from the free lists, just put it back to them.
2449 2534 * DomU pages always go on the free lists as well.
2450 2535 */
2451 2536 if (!DOMAIN_IS_INITDOMAIN(xen_info) || mfn >= PFN_4GIG) {
2452 2537 page_free(pp, 1);
2453 2538 return;
2454 2539 }
2455 2540
2456 2541 add_page_to_pool(pp, 0);
2457 2542 }
2458 2543
2459 2544
2460 2545 long contig_searches; /* count of times contig pages requested */
2461 2546 long contig_search_restarts; /* count of contig ranges tried */
2462 2547 long contig_search_failed; /* count of contig alloc failures */
2463 2548
2464 2549 /*
2465 2550 * Free partial page list
2466 2551 */
2467 2552 static void
2468 2553 free_partial_list(page_t **pplist)
2469 2554 {
2470 2555 page_t *pp;
2471 2556
2472 2557 while (*pplist != NULL) {
2473 2558 pp = *pplist;
2474 2559 page_io_pool_sub(pplist, pp, pp);
2475 2560 page_free(pp, 1);
2476 2561 }
2477 2562 }
2478 2563
2479 2564 /*
2480 2565 * Look thru the contiguous pfns that are not part of the io_pool for
2481 2566 * contiguous free pages. Return a list of the found pages or NULL.
2482 2567 */
2483 2568 page_t *
2484 2569 find_contig_free(uint_t npages, uint_t flags, uint64_t pfnseg,
2485 2570 pgcnt_t pfnalign)
2486 2571 {
2487 2572 page_t *pp, *plist = NULL;
2488 2573 mfn_t mfn, prev_mfn, start_mfn;
2489 2574 pfn_t pfn;
2490 2575 int pages_needed, pages_requested;
2491 2576 int search_start;
2492 2577
2493 2578 /*
2494 2579 * create the contig pfn list if not already done
2495 2580 */
2496 2581 retry:
2497 2582 mutex_enter(&contig_list_lock);
2498 2583 if (contig_pfn_list == NULL) {
2499 2584 mutex_exit(&contig_list_lock);
2500 2585 if (!create_contig_pfnlist(flags)) {
2501 2586 return (NULL);
2502 2587 }
2503 2588 goto retry;
2504 2589 }
2505 2590 contig_searches++;
2506 2591 /*
2507 2592 * Search contiguous pfn list for physically contiguous pages not in
2508 2593 * the io_pool. Start the search where the last search left off.
2509 2594 */
2510 2595 pages_requested = pages_needed = npages;
2511 2596 search_start = next_alloc_pfn;
2512 2597 start_mfn = prev_mfn = 0;
2513 2598 while (pages_needed) {
2514 2599 pfn = contig_pfn_list[next_alloc_pfn];
2515 2600 mfn = pfn_to_mfn(pfn);
2516 2601 /*
2517 2602 * Check if mfn is first one or contig to previous one and
2518 2603 * if page corresponding to mfn is free and that mfn
2519 2604 * range is not crossing a segment boundary.
2520 2605 */
2521 2606 if ((prev_mfn == 0 || mfn == prev_mfn + 1) &&
2522 2607 (pp = page_numtopp_alloc(pfn)) != NULL &&
2523 2608 !((mfn & pfnseg) < (start_mfn & pfnseg))) {
2524 2609 PP_CLRFREE(pp);
2525 2610 page_io_pool_add(&plist, pp);
2526 2611 pages_needed--;
2527 2612 if (prev_mfn == 0) {
2528 2613 if (pfnalign &&
2529 2614 mfn != P2ROUNDUP(mfn, pfnalign)) {
2530 2615 /*
2531 2616 * not properly aligned
2532 2617 */
2533 2618 contig_search_restarts++;
2534 2619 free_partial_list(&plist);
2535 2620 pages_needed = pages_requested;
2536 2621 start_mfn = prev_mfn = 0;
2537 2622 goto skip;
2538 2623 }
2539 2624 start_mfn = mfn;
2540 2625 }
2541 2626 prev_mfn = mfn;
2542 2627 } else {
2543 2628 contig_search_restarts++;
2544 2629 free_partial_list(&plist);
2545 2630 pages_needed = pages_requested;
2546 2631 start_mfn = prev_mfn = 0;
2547 2632 }
2548 2633 skip:
2549 2634 if (++next_alloc_pfn == contig_pfn_cnt)
2550 2635 next_alloc_pfn = 0;
2551 2636 if (next_alloc_pfn == search_start)
2552 2637 break; /* all pfns searched */
2553 2638 }
2554 2639 mutex_exit(&contig_list_lock);
2555 2640 if (pages_needed) {
2556 2641 contig_search_failed++;
2557 2642 /*
2558 2643 * Failed to find enough contig pages.
2559 2644 * free partial page list
2560 2645 */
2561 2646 free_partial_list(&plist);
2562 2647 }
2563 2648 return (plist);
2564 2649 }
2565 2650
2566 2651 /*
2567 2652 * Search the reserved io pool pages for a page range with the
2568 2653 * desired characteristics.
2569 2654 */
2570 2655 page_t *
2571 2656 page_io_pool_alloc(ddi_dma_attr_t *mattr, int contig, pgcnt_t minctg)
2572 2657 {
2573 2658 page_t *pp_first, *pp_last;
2574 2659 page_t *pp, **poolp;
2575 2660 pgcnt_t nwanted, pfnalign;
2576 2661 uint64_t pfnseg;
2577 2662 mfn_t mfn, tmfn, hi_mfn, lo_mfn;
2578 2663 int align, attempt = 0;
2579 2664
2580 2665 if (minctg == 1)
2581 2666 contig = 0;
2582 2667 lo_mfn = mmu_btop(mattr->dma_attr_addr_lo);
2583 2668 hi_mfn = mmu_btop(mattr->dma_attr_addr_hi);
2584 2669 pfnseg = mmu_btop(mattr->dma_attr_seg);
2585 2670 align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
2586 2671 if (align > MMU_PAGESIZE)
2587 2672 pfnalign = mmu_btop(align);
2588 2673 else
2589 2674 pfnalign = 0;
2590 2675
2591 2676 try_again:
2592 2677 /*
2593 2678 * See if we want pages for a legacy device
2594 2679 */
2595 2680 if (hi_mfn < PFN_16MEG)
2596 2681 poolp = &io_pool_16m;
2597 2682 else
2598 2683 poolp = &io_pool_4g;
2599 2684 try_smaller:
2600 2685 /*
2601 2686 * Take pages from I/O pool. We'll use pages from the highest
2602 2687 * MFN range possible.
2603 2688 */
2604 2689 pp_first = pp_last = NULL;
2605 2690 mutex_enter(&io_pool_lock);
2606 2691 nwanted = minctg;
2607 2692 for (pp = *poolp; pp && nwanted > 0; ) {
2608 2693 pp = pp->p_prev;
2609 2694
2610 2695 /*
2611 2696 * skip pages above allowable range
2612 2697 */
2613 2698 mfn = mfn_list[pp->p_pagenum];
2614 2699 if (hi_mfn < mfn)
2615 2700 goto skip;
2616 2701
2617 2702 /*
2618 2703 * stop at pages below allowable range
2619 2704 */
2620 2705 if (lo_mfn > mfn)
2621 2706 break;
2622 2707 restart:
2623 2708 if (pp_last == NULL) {
2624 2709 /*
2625 2710 * Check alignment
2626 2711 */
2627 2712 tmfn = mfn - (minctg - 1);
2628 2713 if (pfnalign && tmfn != P2ROUNDUP(tmfn, pfnalign))
2629 2714 goto skip; /* not properly aligned */
2630 2715 /*
2631 2716 * Check segment
2632 2717 */
2633 2718 if ((mfn & pfnseg) < (tmfn & pfnseg))
2634 2719 goto skip; /* crosses seg boundary */
2635 2720 /*
2636 2721 * Start building page list
2637 2722 */
2638 2723 pp_first = pp_last = pp;
2639 2724 nwanted--;
2640 2725 } else {
2641 2726 /*
2642 2727 * check physical contiguity if required
2643 2728 */
2644 2729 if (contig &&
2645 2730 mfn_list[pp_first->p_pagenum] != mfn + 1) {
2646 2731 /*
2647 2732 * not a contiguous page, restart list.
2648 2733 */
2649 2734 pp_last = NULL;
2650 2735 nwanted = minctg;
2651 2736 goto restart;
2652 2737 } else { /* add page to list */
2653 2738 pp_first = pp;
2654 2739 nwanted--;
2655 2740 }
2656 2741 }
2657 2742 skip:
2658 2743 if (pp == *poolp)
2659 2744 break;
2660 2745 }
2661 2746
2662 2747 /*
2663 2748 * If we didn't find memory. Try the more constrained pool, then
2664 2749 * sweep free pages into the DMA pool and try again.
2665 2750 */
2666 2751 if (nwanted != 0) {
2667 2752 mutex_exit(&io_pool_lock);
2668 2753 /*
2669 2754 * If we were looking in the less constrained pool and
2670 2755 * didn't find pages, try the more constrained pool.
2671 2756 */
2672 2757 if (poolp == &io_pool_4g) {
2673 2758 poolp = &io_pool_16m;
2674 2759 goto try_smaller;
2675 2760 }
2676 2761 kmem_reap();
2677 2762 if (++attempt < 4) {
2678 2763 /*
2679 2764 * Grab some more io_pool pages
2680 2765 */
2681 2766 (void) populate_io_pool();
2682 2767 goto try_again; /* go around and retry */
2683 2768 }
2684 2769 return (NULL);
2685 2770 }
2686 2771 /*
2687 2772 * Found the pages, now snip them from the list
2688 2773 */
2689 2774 page_io_pool_sub(poolp, pp_first, pp_last);
2690 2775 io_pool_cnt -= minctg;
2691 2776 /*
2692 2777 * reset low water mark
2693 2778 */
2694 2779 if (io_pool_cnt < io_pool_cnt_lowater)
2695 2780 io_pool_cnt_lowater = io_pool_cnt;
2696 2781 mutex_exit(&io_pool_lock);
2697 2782 return (pp_first);
2698 2783 }
2699 2784
2700 2785 page_t *
2701 2786 page_swap_with_hypervisor(struct vnode *vp, u_offset_t off, caddr_t vaddr,
2702 2787 ddi_dma_attr_t *mattr, uint_t flags, pgcnt_t minctg)
2703 2788 {
2704 2789 uint_t kflags;
2705 2790 int order, extra, extpages, i, contig, nbits, extents;
2706 2791 page_t *pp, *expp, *pp_first, **pplist = NULL;
2707 2792 mfn_t *mfnlist = NULL;
2708 2793
2709 2794 contig = flags & PG_PHYSCONTIG;
2710 2795 if (minctg == 1)
2711 2796 contig = 0;
2712 2797 flags &= ~PG_PHYSCONTIG;
2713 2798 kflags = flags & PG_WAIT ? KM_SLEEP : KM_NOSLEEP;
2714 2799 /*
2715 2800 * Hypervisor will allocate extents, if we want contig
2716 2801 * pages extent must be >= minctg
2717 2802 */
2718 2803 if (contig) {
2719 2804 order = highbit(minctg) - 1;
2720 2805 if (minctg & ((1 << order) - 1))
2721 2806 order++;
2722 2807 extpages = 1 << order;
2723 2808 } else {
2724 2809 order = 0;
2725 2810 extpages = minctg;
2726 2811 }
2727 2812 if (extpages > minctg) {
2728 2813 extra = extpages - minctg;
2729 2814 if (!page_resv(extra, kflags))
2730 2815 return (NULL);
2731 2816 }
2732 2817 pp_first = NULL;
2733 2818 pplist = kmem_alloc(extpages * sizeof (page_t *), kflags);
2734 2819 if (pplist == NULL)
2735 2820 goto balloon_fail;
2736 2821 mfnlist = kmem_alloc(extpages * sizeof (mfn_t), kflags);
2737 2822 if (mfnlist == NULL)
2738 2823 goto balloon_fail;
2739 2824 pp = page_create_va(vp, off, minctg * PAGESIZE, flags, &kvseg, vaddr);
2740 2825 if (pp == NULL)
2741 2826 goto balloon_fail;
2742 2827 pp_first = pp;
2743 2828 if (extpages > minctg) {
2744 2829 /*
2745 2830 * fill out the rest of extent pages to swap
2746 2831 * with the hypervisor
2747 2832 */
2748 2833 for (i = 0; i < extra; i++) {
2749 2834 expp = page_create_va(vp,
2750 2835 (u_offset_t)(uintptr_t)io_pool_kva,
2751 2836 PAGESIZE, flags, &kvseg, io_pool_kva);
2752 2837 if (expp == NULL)
2753 2838 goto balloon_fail;
2754 2839 (void) hat_pageunload(expp, HAT_FORCE_PGUNLOAD);
2755 2840 page_io_unlock(expp);
2756 2841 page_hashout(expp, NULL);
2757 2842 page_io_lock(expp);
2758 2843 /*
2759 2844 * add page to end of list
2760 2845 */
2761 2846 expp->p_prev = pp_first->p_prev;
2762 2847 expp->p_next = pp_first;
2763 2848 expp->p_prev->p_next = expp;
2764 2849 pp_first->p_prev = expp;
2765 2850 }
2766 2851
2767 2852 }
2768 2853 for (i = 0; i < extpages; i++) {
2769 2854 pplist[i] = pp;
2770 2855 pp = pp->p_next;
2771 2856 }
2772 2857 nbits = highbit(mattr->dma_attr_addr_hi);
2773 2858 extents = contig ? 1 : minctg;
2774 2859 if (balloon_replace_pages(extents, pplist, nbits, order,
2775 2860 mfnlist) != extents) {
2776 2861 if (ioalloc_dbg)
2777 2862 cmn_err(CE_NOTE, "request to hypervisor"
2778 2863 " for %d pages, maxaddr %" PRIx64 " failed",
2779 2864 extpages, mattr->dma_attr_addr_hi);
2780 2865 goto balloon_fail;
2781 2866 }
2782 2867
2783 2868 kmem_free(pplist, extpages * sizeof (page_t *));
2784 2869 kmem_free(mfnlist, extpages * sizeof (mfn_t));
2785 2870 /*
2786 2871 * Return any excess pages to free list
2787 2872 */
2788 2873 if (extpages > minctg) {
2789 2874 for (i = 0; i < extra; i++) {
2790 2875 pp = pp_first->p_prev;
2791 2876 page_sub(&pp_first, pp);
2792 2877 page_io_unlock(pp);
2793 2878 page_unresv(1);
2794 2879 page_free(pp, 1);
2795 2880 }
2796 2881 }
2797 2882 return (pp_first);
2798 2883 balloon_fail:
2799 2884 /*
2800 2885 * Return pages to free list and return failure
2801 2886 */
2802 2887 while (pp_first != NULL) {
2803 2888 pp = pp_first;
2804 2889 page_sub(&pp_first, pp);
2805 2890 page_io_unlock(pp);
2806 2891 if (pp->p_vnode != NULL)
2807 2892 page_hashout(pp, NULL);
2808 2893 page_free(pp, 1);
2809 2894 }
2810 2895 if (pplist)
2811 2896 kmem_free(pplist, extpages * sizeof (page_t *));
2812 2897 if (mfnlist)
2813 2898 kmem_free(mfnlist, extpages * sizeof (mfn_t));
2814 2899 page_unresv(extpages - minctg);
2815 2900 return (NULL);
2816 2901 }
2817 2902
2818 2903 static void
2819 2904 return_partial_alloc(page_t *plist)
2820 2905 {
2821 2906 page_t *pp;
2822 2907
2823 2908 while (plist != NULL) {
2824 2909 pp = plist;
2825 2910 page_sub(&plist, pp);
2826 2911 page_io_unlock(pp);
2827 2912 page_destroy_io(pp);
2828 2913 }
2829 2914 }
2830 2915
2831 2916 static page_t *
2832 2917 page_get_contigpages(
2833 2918 struct vnode *vp,
2834 2919 u_offset_t off,
2835 2920 int *npagesp,
2836 2921 uint_t flags,
2837 2922 caddr_t vaddr,
2838 2923 ddi_dma_attr_t *mattr)
2839 2924 {
2840 2925 mfn_t max_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
2841 2926 page_t *plist; /* list to return */
2842 2927 page_t *pp, *mcpl;
2843 2928 int contig, anyaddr, npages, getone = 0;
2844 2929 mfn_t lo_mfn;
2845 2930 mfn_t hi_mfn;
2846 2931 pgcnt_t pfnalign = 0;
2847 2932 int align, sgllen;
2848 2933 uint64_t pfnseg;
2849 2934 pgcnt_t minctg;
2850 2935
2851 2936 npages = *npagesp;
2852 2937 ASSERT(mattr != NULL);
2853 2938 lo_mfn = mmu_btop(mattr->dma_attr_addr_lo);
2854 2939 hi_mfn = mmu_btop(mattr->dma_attr_addr_hi);
2855 2940 sgllen = mattr->dma_attr_sgllen;
2856 2941 pfnseg = mmu_btop(mattr->dma_attr_seg);
2857 2942 align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
2858 2943 if (align > MMU_PAGESIZE)
2859 2944 pfnalign = mmu_btop(align);
2860 2945
2861 2946 contig = flags & PG_PHYSCONTIG;
2862 2947 if (npages == -1) {
2863 2948 npages = 1;
2864 2949 pfnalign = 0;
2865 2950 }
2866 2951 /*
2867 2952 * Clear the contig flag if only one page is needed.
2868 2953 */
2869 2954 if (npages == 1) {
2870 2955 getone = 1;
2871 2956 contig = 0;
2872 2957 }
2873 2958
2874 2959 /*
2875 2960 * Check if any page in the system is fine.
2876 2961 */
2877 2962 anyaddr = lo_mfn == 0 && hi_mfn >= max_mfn;
2878 2963 if (!contig && anyaddr && !pfnalign) {
2879 2964 flags &= ~PG_PHYSCONTIG;
2880 2965 plist = page_create_va(vp, off, npages * MMU_PAGESIZE,
2881 2966 flags, &kvseg, vaddr);
2882 2967 if (plist != NULL) {
2883 2968 *npagesp = 0;
2884 2969 return (plist);
2885 2970 }
2886 2971 }
2887 2972 plist = NULL;
2888 2973 minctg = howmany(npages, sgllen);
2889 2974 while (npages > sgllen || getone) {
2890 2975 if (minctg > npages)
2891 2976 minctg = npages;
2892 2977 mcpl = NULL;
2893 2978 /*
2894 2979 * We could want contig pages with no address range limits.
2895 2980 */
2896 2981 if (anyaddr && contig) {
2897 2982 /*
2898 2983 * Look for free contig pages to satisfy the request.
2899 2984 */
2900 2985 mcpl = find_contig_free(minctg, flags, pfnseg,
2901 2986 pfnalign);
2902 2987 }
2903 2988 /*
2904 2989 * Try the reserved io pools next
2905 2990 */
2906 2991 if (mcpl == NULL)
2907 2992 mcpl = page_io_pool_alloc(mattr, contig, minctg);
2908 2993 if (mcpl != NULL) {
2909 2994 pp = mcpl;
2910 2995 do {
2911 2996 if (!page_hashin(pp, vp, off, NULL)) {
2912 2997 panic("page_get_contigpages:"
2913 2998 " hashin failed"
2914 2999 " pp %p, vp %p, off %llx",
2915 3000 (void *)pp, (void *)vp, off);
2916 3001 }
2917 3002 off += MMU_PAGESIZE;
2918 3003 PP_CLRFREE(pp);
2919 3004 PP_CLRAGED(pp);
2920 3005 page_set_props(pp, P_REF);
2921 3006 page_io_lock(pp);
2922 3007 pp = pp->p_next;
2923 3008 } while (pp != mcpl);
2924 3009 } else {
2925 3010 /*
2926 3011 * Hypervisor exchange doesn't handle segment or
2927 3012 * alignment constraints
2928 3013 */
2929 3014 if (mattr->dma_attr_seg < mattr->dma_attr_addr_hi ||
2930 3015 pfnalign)
2931 3016 goto fail;
2932 3017 /*
2933 3018 * Try exchanging pages with the hypervisor
2934 3019 */
2935 3020 mcpl = page_swap_with_hypervisor(vp, off, vaddr, mattr,
2936 3021 flags, minctg);
2937 3022 if (mcpl == NULL)
2938 3023 goto fail;
2939 3024 off += minctg * MMU_PAGESIZE;
2940 3025 }
2941 3026 check_dma(mattr, mcpl, minctg);
2942 3027 /*
2943 3028 * Here with a minctg run of contiguous pages, add them to the
2944 3029 * list we will return for this request.
2945 3030 */
2946 3031 page_list_concat(&plist, &mcpl);
2947 3032 npages -= minctg;
2948 3033 *npagesp = npages;
2949 3034 sgllen--;
2950 3035 if (getone)
2951 3036 break;
2952 3037 }
2953 3038 return (plist);
2954 3039 fail:
2955 3040 return_partial_alloc(plist);
2956 3041 return (NULL);
2957 3042 }
2958 3043
2959 3044 /*
2960 3045 * Allocator for domain 0 I/O pages. We match the required
2961 3046 * DMA attributes and contiguity constraints.
2962 3047 */
2963 3048 /*ARGSUSED*/
2964 3049 page_t *
2965 3050 page_create_io(
2966 3051 struct vnode *vp,
2967 3052 u_offset_t off,
2968 3053 uint_t bytes,
2969 3054 uint_t flags,
2970 3055 struct as *as,
2971 3056 caddr_t vaddr,
2972 3057 ddi_dma_attr_t *mattr)
2973 3058 {
2974 3059 page_t *plist = NULL, *pp;
2975 3060 int npages = 0, contig, anyaddr, pages_req;
2976 3061 mfn_t lo_mfn;
2977 3062 mfn_t hi_mfn;
2978 3063 pgcnt_t pfnalign = 0;
2979 3064 int align;
2980 3065 int is_domu = 0;
2981 3066 int dummy, bytes_got;
2982 3067 mfn_t max_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
2983 3068
2984 3069 ASSERT(mattr != NULL);
2985 3070 lo_mfn = mmu_btop(mattr->dma_attr_addr_lo);
2986 3071 hi_mfn = mmu_btop(mattr->dma_attr_addr_hi);
2987 3072 align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
2988 3073 if (align > MMU_PAGESIZE)
2989 3074 pfnalign = mmu_btop(align);
2990 3075
2991 3076 /*
2992 3077 * Clear the contig flag if only one page is needed or the scatter
2993 3078 * gather list length is >= npages.
2994 3079 */
2995 3080 pages_req = npages = mmu_btopr(bytes);
2996 3081 contig = (flags & PG_PHYSCONTIG);
2997 3082 bytes = P2ROUNDUP(bytes, MMU_PAGESIZE);
2998 3083 if (bytes == MMU_PAGESIZE || mattr->dma_attr_sgllen >= npages)
2999 3084 contig = 0;
3000 3085
3001 3086 /*
3002 3087 * Check if any old page in the system is fine.
3003 3088 * DomU should always go down this path.
3004 3089 */
3005 3090 is_domu = !DOMAIN_IS_INITDOMAIN(xen_info);
3006 3091 anyaddr = lo_mfn == 0 && hi_mfn >= max_mfn && !pfnalign;
3007 3092 if ((!contig && anyaddr) || is_domu) {
3008 3093 flags &= ~PG_PHYSCONTIG;
3009 3094 plist = page_create_va(vp, off, bytes, flags, &kvseg, vaddr);
3010 3095 if (plist != NULL)
3011 3096 return (plist);
3012 3097 else if (is_domu)
3013 3098 return (NULL); /* no memory available */
3014 3099 }
3015 3100 /*
3016 3101 * DomU should never reach here
3017 3102 */
3018 3103 if (contig) {
3019 3104 plist = page_get_contigpages(vp, off, &npages, flags, vaddr,
3020 3105 mattr);
3021 3106 if (plist == NULL)
3022 3107 goto fail;
3023 3108 bytes_got = (pages_req - npages) << MMU_PAGESHIFT;
3024 3109 vaddr += bytes_got;
3025 3110 off += bytes_got;
3026 3111 /*
3027 3112 * We now have all the contiguous pages we need, but
3028 3113 * we may still need additional non-contiguous pages.
3029 3114 */
3030 3115 }
3031 3116 /*
3032 3117 * now loop collecting the requested number of pages, these do
3033 3118 * not have to be contiguous pages but we will use the contig
3034 3119 * page alloc code to get the pages since it will honor any
3035 3120 * other constraints the pages may have.
3036 3121 */
3037 3122 while (npages--) {
3038 3123 dummy = -1;
3039 3124 pp = page_get_contigpages(vp, off, &dummy, flags, vaddr, mattr);
3040 3125 if (pp == NULL)
3041 3126 goto fail;
3042 3127 page_add(&plist, pp);
3043 3128 vaddr += MMU_PAGESIZE;
3044 3129 off += MMU_PAGESIZE;
3045 3130 }
3046 3131 return (plist);
3047 3132 fail:
3048 3133 /*
3049 3134 * Failed to get enough pages, return ones we did get
3050 3135 */
3051 3136 return_partial_alloc(plist);
3052 3137 return (NULL);
3053 3138 }
3054 3139
3055 3140 /*
3056 3141 * Lock and return the page with the highest mfn that we can find. last_mfn
3057 3142 * holds the last one found, so the next search can start from there. We
3058 3143 * also keep a counter so that we don't loop forever if the machine has no
3059 3144 * free pages.
3060 3145 *
3061 3146 * This is called from the balloon thread to find pages to give away. new_high
3062 3147 * is used when new mfn's have been added to the system - we will reset our
3063 3148 * search if the new mfn's are higher than our current search position.
3064 3149 */
3065 3150 page_t *
3066 3151 page_get_high_mfn(mfn_t new_high)
3067 3152 {
3068 3153 static mfn_t last_mfn = 0;
3069 3154 pfn_t pfn;
3070 3155 page_t *pp;
3071 3156 ulong_t loop_count = 0;
3072 3157
3073 3158 if (new_high > last_mfn)
3074 3159 last_mfn = new_high;
3075 3160
3076 3161 for (; loop_count < mfn_count; loop_count++, last_mfn--) {
3077 3162 if (last_mfn == 0) {
3078 3163 last_mfn = cached_max_mfn;
3079 3164 }
3080 3165
3081 3166 pfn = mfn_to_pfn(last_mfn);
3082 3167 if (pfn & PFN_IS_FOREIGN_MFN)
3083 3168 continue;
3084 3169
3085 3170 /* See if the page is free. If so, lock it. */
3086 3171 pp = page_numtopp_alloc(pfn);
3087 3172 if (pp == NULL)
3088 3173 continue;
3089 3174 PP_CLRFREE(pp);
3090 3175
3091 3176 ASSERT(PAGE_EXCL(pp));
3092 3177 ASSERT(pp->p_vnode == NULL);
3093 3178 ASSERT(!hat_page_is_mapped(pp));
3094 3179 last_mfn--;
3095 3180 return (pp);
3096 3181 }
3097 3182 return (NULL);
3098 3183 }
3099 3184
3100 3185 #else /* !__xpv */
3101 3186
3102 3187 /*
3103 3188 * get a page from any list with the given mnode
3104 3189 */
3105 3190 static page_t *
3106 3191 page_get_mnode_anylist(ulong_t origbin, uchar_t szc, uint_t flags,
3107 3192 int mnode, int mtype, ddi_dma_attr_t *dma_attr)
3108 3193 {
3109 3194 kmutex_t *pcm;
3110 3195 int i;
3111 3196 page_t *pp;
3112 3197 page_t *first_pp;
3113 3198 uint64_t pgaddr;
3114 3199 ulong_t bin;
3115 3200 int mtypestart;
3116 3201 int plw_initialized;
3117 3202 page_list_walker_t plw;
3118 3203
3119 3204 VM_STAT_ADD(pga_vmstats.pgma_alloc);
3120 3205
3121 3206 ASSERT((flags & PG_MATCH_COLOR) == 0);
3122 3207 ASSERT(szc == 0);
3123 3208 ASSERT(dma_attr != NULL);
3124 3209
3125 3210 MTYPE_START(mnode, mtype, flags);
3126 3211 if (mtype < 0) {
3127 3212 VM_STAT_ADD(pga_vmstats.pgma_allocempty);
3128 3213 return (NULL);
3129 3214 }
3130 3215
3131 3216 mtypestart = mtype;
3132 3217
3133 3218 bin = origbin;
3134 3219
3135 3220 /*
3136 3221 * check up to page_colors + 1 bins - origbin may be checked twice
3137 3222 * because of BIN_STEP skip
3138 3223 */
3139 3224 do {
3140 3225 plw_initialized = 0;
3141 3226
3142 3227 for (plw.plw_count = 0;
3143 3228 plw.plw_count < page_colors; plw.plw_count++) {
3144 3229
3145 3230 if (PAGE_FREELISTS(mnode, szc, bin, mtype) == NULL)
3146 3231 goto nextfreebin;
3147 3232
3148 3233 pcm = PC_BIN_MUTEX(mnode, bin, PG_FREE_LIST);
3149 3234 mutex_enter(pcm);
3150 3235 pp = PAGE_FREELISTS(mnode, szc, bin, mtype);
3151 3236 first_pp = pp;
3152 3237 while (pp != NULL) {
3153 3238 if (IS_DUMP_PAGE(pp) || page_trylock(pp,
3154 3239 SE_EXCL) == 0) {
3155 3240 pp = pp->p_next;
3156 3241 if (pp == first_pp) {
3157 3242 pp = NULL;
3158 3243 }
3159 3244 continue;
3160 3245 }
3161 3246
3162 3247 ASSERT(PP_ISFREE(pp));
3163 3248 ASSERT(PP_ISAGED(pp));
3164 3249 ASSERT(pp->p_vnode == NULL);
3165 3250 ASSERT(pp->p_hash == NULL);
3166 3251 ASSERT(pp->p_offset == (u_offset_t)-1);
3167 3252 ASSERT(pp->p_szc == szc);
3168 3253 ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
3169 3254 /* check if page within DMA attributes */
3170 3255 pgaddr = pa_to_ma(pfn_to_pa(pp->p_pagenum));
3171 3256 if ((pgaddr >= dma_attr->dma_attr_addr_lo) &&
3172 3257 (pgaddr + MMU_PAGESIZE - 1 <=
3173 3258 dma_attr->dma_attr_addr_hi)) {
3174 3259 break;
3175 3260 }
3176 3261
3177 3262 /* continue looking */
3178 3263 page_unlock(pp);
3179 3264 pp = pp->p_next;
3180 3265 if (pp == first_pp)
3181 3266 pp = NULL;
3182 3267
3183 3268 }
3184 3269 if (pp != NULL) {
3185 3270 ASSERT(mtype == PP_2_MTYPE(pp));
3186 3271 ASSERT(pp->p_szc == 0);
3187 3272
3188 3273 /* found a page with specified DMA attributes */
3189 3274 page_sub(&PAGE_FREELISTS(mnode, szc, bin,
3190 3275 mtype), pp);
3191 3276 page_ctr_sub(mnode, mtype, pp, PG_FREE_LIST);
3192 3277
3193 3278 if ((PP_ISFREE(pp) == 0) ||
3194 3279 (PP_ISAGED(pp) == 0)) {
3195 3280 cmn_err(CE_PANIC, "page %p is not free",
3196 3281 (void *)pp);
3197 3282 }
3198 3283
3199 3284 mutex_exit(pcm);
3200 3285 check_dma(dma_attr, pp, 1);
3201 3286 VM_STAT_ADD(pga_vmstats.pgma_allocok);
3202 3287 return (pp);
3203 3288 }
3204 3289 mutex_exit(pcm);
3205 3290 nextfreebin:
3206 3291 if (plw_initialized == 0) {
3207 3292 page_list_walk_init(szc, 0, bin, 1, 0, &plw);
3208 3293 ASSERT(plw.plw_ceq_dif == page_colors);
3209 3294 plw_initialized = 1;
3210 3295 }
3211 3296
3212 3297 if (plw.plw_do_split) {
3213 3298 pp = page_freelist_split(szc, bin, mnode,
3214 3299 mtype,
3215 3300 mmu_btop(dma_attr->dma_attr_addr_lo),
3216 3301 mmu_btop(dma_attr->dma_attr_addr_hi + 1),
3217 3302 &plw);
3218 3303 if (pp != NULL) {
3219 3304 check_dma(dma_attr, pp, 1);
3220 3305 return (pp);
3221 3306 }
3222 3307 }
3223 3308
3224 3309 bin = page_list_walk_next_bin(szc, bin, &plw);
3225 3310 }
3226 3311
3227 3312 MTYPE_NEXT(mnode, mtype, flags);
3228 3313 } while (mtype >= 0);
3229 3314
3230 3315 /* failed to find a page in the freelist; try it in the cachelist */
3231 3316
3232 3317 /* reset mtype start for cachelist search */
3233 3318 mtype = mtypestart;
3234 3319 ASSERT(mtype >= 0);
3235 3320
3236 3321 /* start with the bin of matching color */
3237 3322 bin = origbin;
3238 3323
3239 3324 do {
3240 3325 for (i = 0; i <= page_colors; i++) {
3241 3326 if (PAGE_CACHELISTS(mnode, bin, mtype) == NULL)
3242 3327 goto nextcachebin;
3243 3328 pcm = PC_BIN_MUTEX(mnode, bin, PG_CACHE_LIST);
3244 3329 mutex_enter(pcm);
3245 3330 pp = PAGE_CACHELISTS(mnode, bin, mtype);
3246 3331 first_pp = pp;
3247 3332 while (pp != NULL) {
3248 3333 if (IS_DUMP_PAGE(pp) || page_trylock(pp,
3249 3334 SE_EXCL) == 0) {
3250 3335 pp = pp->p_next;
3251 3336 if (pp == first_pp)
3252 3337 pp = NULL;
3253 3338 continue;
3254 3339 }
3255 3340 ASSERT(pp->p_vnode);
3256 3341 ASSERT(PP_ISAGED(pp) == 0);
3257 3342 ASSERT(pp->p_szc == 0);
3258 3343 ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
3259 3344
3260 3345 /* check if page within DMA attributes */
3261 3346
3262 3347 pgaddr = pa_to_ma(pfn_to_pa(pp->p_pagenum));
3263 3348 if ((pgaddr >= dma_attr->dma_attr_addr_lo) &&
3264 3349 (pgaddr + MMU_PAGESIZE - 1 <=
3265 3350 dma_attr->dma_attr_addr_hi)) {
3266 3351 break;
3267 3352 }
3268 3353
3269 3354 /* continue looking */
3270 3355 page_unlock(pp);
3271 3356 pp = pp->p_next;
3272 3357 if (pp == first_pp)
3273 3358 pp = NULL;
3274 3359 }
3275 3360
3276 3361 if (pp != NULL) {
3277 3362 ASSERT(mtype == PP_2_MTYPE(pp));
3278 3363 ASSERT(pp->p_szc == 0);
3279 3364
3280 3365 /* found a page with specified DMA attributes */
3281 3366 page_sub(&PAGE_CACHELISTS(mnode, bin,
3282 3367 mtype), pp);
3283 3368 page_ctr_sub(mnode, mtype, pp, PG_CACHE_LIST);
3284 3369
3285 3370 mutex_exit(pcm);
3286 3371 ASSERT(pp->p_vnode);
3287 3372 ASSERT(PP_ISAGED(pp) == 0);
3288 3373 check_dma(dma_attr, pp, 1);
3289 3374 VM_STAT_ADD(pga_vmstats.pgma_allocok);
3290 3375 return (pp);
3291 3376 }
3292 3377 mutex_exit(pcm);
3293 3378 nextcachebin:
3294 3379 bin += (i == 0) ? BIN_STEP : 1;
3295 3380 bin &= page_colors_mask;
3296 3381 }
3297 3382 MTYPE_NEXT(mnode, mtype, flags);
3298 3383 } while (mtype >= 0);
3299 3384
3300 3385 VM_STAT_ADD(pga_vmstats.pgma_allocfailed);
3301 3386 return (NULL);
3302 3387 }
3303 3388
3304 3389 /*
3305 3390 * This function is similar to page_get_freelist()/page_get_cachelist()
3306 3391 * but it searches both the lists to find a page with the specified
3307 3392 * color (or no color) and DMA attributes. The search is done in the
3308 3393 * freelist first and then in the cache list within the highest memory
3309 3394 * range (based on DMA attributes) before searching in the lower
3310 3395 * memory ranges.
3311 3396 *
3312 3397 * Note: This function is called only by page_create_io().
3313 3398 */
3314 3399 /*ARGSUSED*/
3315 3400 static page_t *
3316 3401 page_get_anylist(struct vnode *vp, u_offset_t off, struct as *as, caddr_t vaddr,
3317 3402 size_t size, uint_t flags, ddi_dma_attr_t *dma_attr, lgrp_t *lgrp)
3318 3403 {
3319 3404 uint_t bin;
3320 3405 int mtype;
3321 3406 page_t *pp;
3322 3407 int n;
3323 3408 int m;
3324 3409 int szc;
3325 3410 int fullrange;
3326 3411 int mnode;
3327 3412 int local_failed_stat = 0;
3328 3413 lgrp_mnode_cookie_t lgrp_cookie;
3329 3414
3330 3415 VM_STAT_ADD(pga_vmstats.pga_alloc);
3331 3416
3332 3417 /* only base pagesize currently supported */
3333 3418 if (size != MMU_PAGESIZE)
3334 3419 return (NULL);
3335 3420
3336 3421 /*
3337 3422 * If we're passed a specific lgroup, we use it. Otherwise,
3338 3423 * assume first-touch placement is desired.
3339 3424 */
3340 3425 if (!LGRP_EXISTS(lgrp))
3341 3426 lgrp = lgrp_home_lgrp();
3342 3427
3343 3428 /* LINTED */
3344 3429 AS_2_BIN(as, seg, vp, vaddr, bin, 0);
3345 3430
3346 3431 /*
3347 3432 * Only hold one freelist or cachelist lock at a time, that way we
3348 3433 * can start anywhere and not have to worry about lock
3349 3434 * ordering.
3350 3435 */
3351 3436 if (dma_attr == NULL) {
3352 3437 n = mtype16m;
3353 3438 m = mtypetop;
3354 3439 fullrange = 1;
3355 3440 VM_STAT_ADD(pga_vmstats.pga_nulldmaattr);
3356 3441 } else {
3357 3442 pfn_t pfnlo = mmu_btop(dma_attr->dma_attr_addr_lo);
3358 3443 pfn_t pfnhi = mmu_btop(dma_attr->dma_attr_addr_hi);
3359 3444
3360 3445 /*
3361 3446 * We can guarantee alignment only for page boundary.
3362 3447 */
3363 3448 if (dma_attr->dma_attr_align > MMU_PAGESIZE)
3364 3449 return (NULL);
3365 3450
3366 3451 /* Sanity check the dma_attr */
3367 3452 if (pfnlo > pfnhi)
3368 3453 return (NULL);
3369 3454
3370 3455 n = pfn_2_mtype(pfnlo);
3371 3456 m = pfn_2_mtype(pfnhi);
3372 3457
3373 3458 fullrange = ((pfnlo == mnoderanges[n].mnr_pfnlo) &&
3374 3459 (pfnhi >= mnoderanges[m].mnr_pfnhi));
3375 3460 }
3376 3461 VM_STAT_COND_ADD(fullrange == 0, pga_vmstats.pga_notfullrange);
3377 3462
3378 3463 szc = 0;
3379 3464
3380 3465 /* cylcing thru mtype handled by RANGE0 if n == mtype16m */
3381 3466 if (n == mtype16m) {
3382 3467 flags |= PGI_MT_RANGE0;
3383 3468 n = m;
3384 3469 }
3385 3470
3386 3471 /*
3387 3472 * Try local memory node first, but try remote if we can't
3388 3473 * get a page of the right color.
3389 3474 */
3390 3475 LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp, LGRP_SRCH_HIER);
3391 3476 while ((mnode = lgrp_memnode_choose(&lgrp_cookie)) >= 0) {
3392 3477 /*
3393 3478 * allocate pages from high pfn to low.
3394 3479 */
3395 3480 mtype = m;
3396 3481 do {
3397 3482 if (fullrange != 0) {
3398 3483 pp = page_get_mnode_freelist(mnode,
3399 3484 bin, mtype, szc, flags);
3400 3485 if (pp == NULL) {
3401 3486 pp = page_get_mnode_cachelist(
3402 3487 bin, flags, mnode, mtype);
3403 3488 }
3404 3489 } else {
3405 3490 pp = page_get_mnode_anylist(bin, szc,
3406 3491 flags, mnode, mtype, dma_attr);
3407 3492 }
3408 3493 if (pp != NULL) {
3409 3494 VM_STAT_ADD(pga_vmstats.pga_allocok);
3410 3495 check_dma(dma_attr, pp, 1);
3411 3496 return (pp);
3412 3497 }
3413 3498 } while (mtype != n &&
3414 3499 (mtype = mnoderanges[mtype].mnr_next) != -1);
3415 3500 if (!local_failed_stat) {
3416 3501 lgrp_stat_add(lgrp->lgrp_id, LGRP_NUM_ALLOC_FAIL, 1);
3417 3502 local_failed_stat = 1;
3418 3503 }
3419 3504 }
3420 3505 VM_STAT_ADD(pga_vmstats.pga_allocfailed);
3421 3506
3422 3507 return (NULL);
3423 3508 }
3424 3509
3425 3510 /*
3426 3511 * page_create_io()
3427 3512 *
3428 3513 * This function is a copy of page_create_va() with an additional
3429 3514 * argument 'mattr' that specifies DMA memory requirements to
3430 3515 * the page list functions. This function is used by the segkmem
3431 3516 * allocator so it is only to create new pages (i.e PG_EXCL is
3432 3517 * set).
3433 3518 *
3434 3519 * Note: This interface is currently used by x86 PSM only and is
3435 3520 * not fully specified so the commitment level is only for
3436 3521 * private interface specific to x86. This interface uses PSM
3437 3522 * specific page_get_anylist() interface.
3438 3523 */
3439 3524
3440 3525 #define PAGE_HASH_SEARCH(index, pp, vp, off) { \
3441 3526 for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash) { \
3442 3527 if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \
3443 3528 break; \
3444 3529 } \
3445 3530 }
3446 3531
3447 3532
3448 3533 page_t *
3449 3534 page_create_io(
3450 3535 struct vnode *vp,
3451 3536 u_offset_t off,
3452 3537 uint_t bytes,
3453 3538 uint_t flags,
3454 3539 struct as *as,
3455 3540 caddr_t vaddr,
3456 3541 ddi_dma_attr_t *mattr) /* DMA memory attributes if any */
3457 3542 {
3458 3543 page_t *plist = NULL;
3459 3544 uint_t plist_len = 0;
3460 3545 pgcnt_t npages;
3461 3546 page_t *npp = NULL;
3462 3547 uint_t pages_req;
3463 3548 page_t *pp;
3464 3549 kmutex_t *phm = NULL;
3465 3550 uint_t index;
3466 3551
3467 3552 TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_START,
3468 3553 "page_create_start:vp %p off %llx bytes %u flags %x",
3469 3554 vp, off, bytes, flags);
3470 3555
3471 3556 ASSERT((flags & ~(PG_EXCL | PG_WAIT | PG_PHYSCONTIG)) == 0);
3472 3557
3473 3558 pages_req = npages = mmu_btopr(bytes);
3474 3559
3475 3560 /*
3476 3561 * Do the freemem and pcf accounting.
3477 3562 */
3478 3563 if (!page_create_wait(npages, flags)) {
3479 3564 return (NULL);
3480 3565 }
3481 3566
3482 3567 TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SUCCESS,
3483 3568 "page_create_success:vp %p off %llx", vp, off);
3484 3569
3485 3570 /*
3486 3571 * If satisfying this request has left us with too little
3487 3572 * memory, start the wheels turning to get some back. The
3488 3573 * first clause of the test prevents waking up the pageout
3489 3574 * daemon in situations where it would decide that there's
3490 3575 * nothing to do.
3491 3576 */
3492 3577 if (nscan < desscan && freemem < minfree) {
3493 3578 TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL,
3494 3579 "pageout_cv_signal:freemem %ld", freemem);
3495 3580 cv_signal(&proc_pageout->p_cv);
3496 3581 }
3497 3582
3498 3583 if (flags & PG_PHYSCONTIG) {
3499 3584
3500 3585 plist = page_get_contigpage(&npages, mattr, 1);
3501 3586 if (plist == NULL) {
3502 3587 page_create_putback(npages);
3503 3588 return (NULL);
3504 3589 }
3505 3590
3506 3591 pp = plist;
3507 3592
3508 3593 do {
3509 3594 if (!page_hashin(pp, vp, off, NULL)) {
3510 3595 panic("pg_creat_io: hashin failed %p %p %llx",
3511 3596 (void *)pp, (void *)vp, off);
3512 3597 }
3513 3598 VM_STAT_ADD(page_create_new);
3514 3599 off += MMU_PAGESIZE;
3515 3600 PP_CLRFREE(pp);
3516 3601 PP_CLRAGED(pp);
3517 3602 page_set_props(pp, P_REF);
3518 3603 pp = pp->p_next;
3519 3604 } while (pp != plist);
3520 3605
3521 3606 if (!npages) {
3522 3607 check_dma(mattr, plist, pages_req);
3523 3608 return (plist);
3524 3609 } else {
3525 3610 vaddr += (pages_req - npages) << MMU_PAGESHIFT;
3526 3611 }
3527 3612
3528 3613 /*
3529 3614 * fall-thru:
3530 3615 *
3531 3616 * page_get_contigpage returns when npages <= sgllen.
3532 3617 * Grab the rest of the non-contig pages below from anylist.
3533 3618 */
3534 3619 }
3535 3620
3536 3621 /*
3537 3622 * Loop around collecting the requested number of pages.
3538 3623 * Most of the time, we have to `create' a new page. With
3539 3624 * this in mind, pull the page off the free list before
3540 3625 * getting the hash lock. This will minimize the hash
3541 3626 * lock hold time, nesting, and the like. If it turns
3542 3627 * out we don't need the page, we put it back at the end.
3543 3628 */
3544 3629 while (npages--) {
3545 3630 phm = NULL;
3546 3631
3547 3632 index = PAGE_HASH_FUNC(vp, off);
3548 3633 top:
3549 3634 ASSERT(phm == NULL);
3550 3635 ASSERT(index == PAGE_HASH_FUNC(vp, off));
3551 3636 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp)));
3552 3637
3553 3638 if (npp == NULL) {
3554 3639 /*
3555 3640 * Try to get the page of any color either from
3556 3641 * the freelist or from the cache list.
3557 3642 */
3558 3643 npp = page_get_anylist(vp, off, as, vaddr, MMU_PAGESIZE,
3559 3644 flags & ~PG_MATCH_COLOR, mattr, NULL);
3560 3645 if (npp == NULL) {
3561 3646 if (mattr == NULL) {
3562 3647 /*
3563 3648 * Not looking for a special page;
3564 3649 * panic!
3565 3650 */
3566 3651 panic("no page found %d", (int)npages);
3567 3652 }
3568 3653 /*
3569 3654 * No page found! This can happen
3570 3655 * if we are looking for a page
3571 3656 * within a specific memory range
3572 3657 * for DMA purposes. If PG_WAIT is
3573 3658 * specified then we wait for a
3574 3659 * while and then try again. The
3575 3660 * wait could be forever if we
3576 3661 * don't get the page(s) we need.
3577 3662 *
3578 3663 * Note: XXX We really need a mechanism
3579 3664 * to wait for pages in the desired
3580 3665 * range. For now, we wait for any
3581 3666 * pages and see if we can use it.
3582 3667 */
3583 3668
3584 3669 if ((mattr != NULL) && (flags & PG_WAIT)) {
3585 3670 delay(10);
3586 3671 goto top;
3587 3672 }
3588 3673 goto fail; /* undo accounting stuff */
3589 3674 }
3590 3675
3591 3676 if (PP_ISAGED(npp) == 0) {
3592 3677 /*
3593 3678 * Since this page came from the
3594 3679 * cachelist, we must destroy the
3595 3680 * old vnode association.
3596 3681 */
3597 3682 page_hashout(npp, (kmutex_t *)NULL);
3598 3683 }
3599 3684 }
3600 3685
3601 3686 /*
3602 3687 * We own this page!
3603 3688 */
3604 3689 ASSERT(PAGE_EXCL(npp));
3605 3690 ASSERT(npp->p_vnode == NULL);
3606 3691 ASSERT(!hat_page_is_mapped(npp));
3607 3692 PP_CLRFREE(npp);
3608 3693 PP_CLRAGED(npp);
3609 3694
3610 3695 /*
3611 3696 * Here we have a page in our hot little mits and are
3612 3697 * just waiting to stuff it on the appropriate lists.
3613 3698 * Get the mutex and check to see if it really does
3614 3699 * not exist.
3615 3700 */
3616 3701 phm = PAGE_HASH_MUTEX(index);
3617 3702 mutex_enter(phm);
3618 3703 PAGE_HASH_SEARCH(index, pp, vp, off);
3619 3704 if (pp == NULL) {
3620 3705 VM_STAT_ADD(page_create_new);
3621 3706 pp = npp;
3622 3707 npp = NULL;
3623 3708 if (!page_hashin(pp, vp, off, phm)) {
3624 3709 /*
3625 3710 * Since we hold the page hash mutex and
3626 3711 * just searched for this page, page_hashin
3627 3712 * had better not fail. If it does, that
3628 3713 * means somethread did not follow the
3629 3714 * page hash mutex rules. Panic now and
3630 3715 * get it over with. As usual, go down
3631 3716 * holding all the locks.
3632 3717 */
3633 3718 ASSERT(MUTEX_HELD(phm));
3634 3719 panic("page_create: hashin fail %p %p %llx %p",
3635 3720 (void *)pp, (void *)vp, off, (void *)phm);
3636 3721
3637 3722 }
3638 3723 ASSERT(MUTEX_HELD(phm));
3639 3724 mutex_exit(phm);
3640 3725 phm = NULL;
3641 3726
3642 3727 /*
3643 3728 * Hat layer locking need not be done to set
3644 3729 * the following bits since the page is not hashed
3645 3730 * and was on the free list (i.e., had no mappings).
3646 3731 *
3647 3732 * Set the reference bit to protect
3648 3733 * against immediate pageout
3649 3734 *
3650 3735 * XXXmh modify freelist code to set reference
3651 3736 * bit so we don't have to do it here.
3652 3737 */
3653 3738 page_set_props(pp, P_REF);
3654 3739 } else {
3655 3740 ASSERT(MUTEX_HELD(phm));
3656 3741 mutex_exit(phm);
3657 3742 phm = NULL;
3658 3743 /*
3659 3744 * NOTE: This should not happen for pages associated
3660 3745 * with kernel vnode 'kvp'.
3661 3746 */
3662 3747 /* XX64 - to debug why this happens! */
3663 3748 ASSERT(!VN_ISKAS(vp));
3664 3749 if (VN_ISKAS(vp))
3665 3750 cmn_err(CE_NOTE,
3666 3751 "page_create: page not expected "
3667 3752 "in hash list for kernel vnode - pp 0x%p",
3668 3753 (void *)pp);
3669 3754 VM_STAT_ADD(page_create_exists);
3670 3755 goto fail;
3671 3756 }
3672 3757
3673 3758 /*
3674 3759 * Got a page! It is locked. Acquire the i/o
3675 3760 * lock since we are going to use the p_next and
3676 3761 * p_prev fields to link the requested pages together.
3677 3762 */
3678 3763 page_io_lock(pp);
3679 3764 page_add(&plist, pp);
3680 3765 plist = plist->p_next;
3681 3766 off += MMU_PAGESIZE;
3682 3767 vaddr += MMU_PAGESIZE;
3683 3768 }
3684 3769
3685 3770 check_dma(mattr, plist, pages_req);
3686 3771 return (plist);
3687 3772
3688 3773 fail:
3689 3774 if (npp != NULL) {
3690 3775 /*
3691 3776 * Did not need this page after all.
3692 3777 * Put it back on the free list.
3693 3778 */
3694 3779 VM_STAT_ADD(page_create_putbacks);
3695 3780 PP_SETFREE(npp);
3696 3781 PP_SETAGED(npp);
3697 3782 npp->p_offset = (u_offset_t)-1;
3698 3783 page_list_add(npp, PG_FREE_LIST | PG_LIST_TAIL);
3699 3784 page_unlock(npp);
3700 3785 }
3701 3786
3702 3787 /*
3703 3788 * Give up the pages we already got.
3704 3789 */
3705 3790 while (plist != NULL) {
3706 3791 pp = plist;
3707 3792 page_sub(&plist, pp);
3708 3793 page_io_unlock(pp);
3709 3794 plist_len++;
3710 3795 /*LINTED: constant in conditional ctx*/
3711 3796 VN_DISPOSE(pp, B_INVAL, 0, kcred);
3712 3797 }
3713 3798
3714 3799 /*
3715 3800 * VN_DISPOSE does freemem accounting for the pages in plist
3716 3801 * by calling page_free. So, we need to undo the pcf accounting
3717 3802 * for only the remaining pages.
3718 3803 */
3719 3804 VM_STAT_ADD(page_create_putbacks);
3720 3805 page_create_putback(pages_req - plist_len);
3721 3806
3722 3807 return (NULL);
3723 3808 }
3724 3809 #endif /* !__xpv */
3725 3810
3726 3811
3727 3812 /*
3728 3813 * Copy the data from the physical page represented by "frompp" to
3729 3814 * that represented by "topp". ppcopy uses CPU->cpu_caddr1 and
3730 3815 * CPU->cpu_caddr2. It assumes that no one uses either map at interrupt
3731 3816 * level and no one sleeps with an active mapping there.
3732 3817 *
3733 3818 * Note that the ref/mod bits in the page_t's are not affected by
3734 3819 * this operation, hence it is up to the caller to update them appropriately.
3735 3820 */
3736 3821 int
3737 3822 ppcopy(page_t *frompp, page_t *topp)
3738 3823 {
3739 3824 caddr_t pp_addr1;
3740 3825 caddr_t pp_addr2;
3741 3826 hat_mempte_t pte1;
3742 3827 hat_mempte_t pte2;
3743 3828 kmutex_t *ppaddr_mutex;
3744 3829 label_t ljb;
3745 3830 int ret = 1;
3746 3831
3747 3832 ASSERT_STACK_ALIGNED();
3748 3833 ASSERT(PAGE_LOCKED(frompp));
3749 3834 ASSERT(PAGE_LOCKED(topp));
3750 3835
3751 3836 if (kpm_enable) {
3752 3837 pp_addr1 = hat_kpm_page2va(frompp, 0);
3753 3838 pp_addr2 = hat_kpm_page2va(topp, 0);
3754 3839 kpreempt_disable();
3755 3840 } else {
3756 3841 /*
3757 3842 * disable pre-emption so that CPU can't change
3758 3843 */
3759 3844 kpreempt_disable();
3760 3845
3761 3846 pp_addr1 = CPU->cpu_caddr1;
3762 3847 pp_addr2 = CPU->cpu_caddr2;
3763 3848 pte1 = CPU->cpu_caddr1pte;
3764 3849 pte2 = CPU->cpu_caddr2pte;
3765 3850
3766 3851 ppaddr_mutex = &CPU->cpu_ppaddr_mutex;
3767 3852 mutex_enter(ppaddr_mutex);
3768 3853
3769 3854 hat_mempte_remap(page_pptonum(frompp), pp_addr1, pte1,
3770 3855 PROT_READ | HAT_STORECACHING_OK, HAT_LOAD_NOCONSIST);
3771 3856 hat_mempte_remap(page_pptonum(topp), pp_addr2, pte2,
3772 3857 PROT_READ | PROT_WRITE | HAT_STORECACHING_OK,
3773 3858 HAT_LOAD_NOCONSIST);
3774 3859 }
3775 3860
3776 3861 if (on_fault(&ljb)) {
3777 3862 ret = 0;
3778 3863 goto faulted;
3779 3864 }
3780 3865 if (use_sse_pagecopy)
3781 3866 #ifdef __xpv
3782 3867 page_copy_no_xmm(pp_addr2, pp_addr1);
3783 3868 #else
3784 3869 hwblkpagecopy(pp_addr1, pp_addr2);
3785 3870 #endif
3786 3871 else
3787 3872 bcopy(pp_addr1, pp_addr2, PAGESIZE);
3788 3873
3789 3874 no_fault();
3790 3875 faulted:
3791 3876 if (!kpm_enable) {
3792 3877 #ifdef __xpv
3793 3878 /*
3794 3879 * We can't leave unused mappings laying about under the
3795 3880 * hypervisor, so blow them away.
3796 3881 */
3797 3882 if (HYPERVISOR_update_va_mapping((uintptr_t)pp_addr1, 0,
3798 3883 UVMF_INVLPG | UVMF_LOCAL) < 0)
3799 3884 panic("HYPERVISOR_update_va_mapping() failed");
3800 3885 if (HYPERVISOR_update_va_mapping((uintptr_t)pp_addr2, 0,
3801 3886 UVMF_INVLPG | UVMF_LOCAL) < 0)
3802 3887 panic("HYPERVISOR_update_va_mapping() failed");
3803 3888 #endif
3804 3889 mutex_exit(ppaddr_mutex);
3805 3890 }
3806 3891 kpreempt_enable();
3807 3892 return (ret);
3808 3893 }
3809 3894
3810 3895 void
3811 3896 pagezero(page_t *pp, uint_t off, uint_t len)
3812 3897 {
3813 3898 ASSERT(PAGE_LOCKED(pp));
3814 3899 pfnzero(page_pptonum(pp), off, len);
3815 3900 }
3816 3901
3817 3902 /*
3818 3903 * Zero the physical page from off to off + len given by pfn
3819 3904 * without changing the reference and modified bits of page.
3820 3905 *
3821 3906 * We use this using CPU private page address #2, see ppcopy() for more info.
3822 3907 * pfnzero() must not be called at interrupt level.
3823 3908 */
3824 3909 void
3825 3910 pfnzero(pfn_t pfn, uint_t off, uint_t len)
3826 3911 {
3827 3912 caddr_t pp_addr2;
3828 3913 hat_mempte_t pte2;
3829 3914 kmutex_t *ppaddr_mutex = NULL;
3830 3915
3831 3916 ASSERT_STACK_ALIGNED();
3832 3917 ASSERT(len <= MMU_PAGESIZE);
3833 3918 ASSERT(off <= MMU_PAGESIZE);
3834 3919 ASSERT(off + len <= MMU_PAGESIZE);
3835 3920
3836 3921 if (kpm_enable && !pfn_is_foreign(pfn)) {
3837 3922 pp_addr2 = hat_kpm_pfn2va(pfn);
3838 3923 kpreempt_disable();
3839 3924 } else {
3840 3925 kpreempt_disable();
3841 3926
3842 3927 pp_addr2 = CPU->cpu_caddr2;
3843 3928 pte2 = CPU->cpu_caddr2pte;
3844 3929
3845 3930 ppaddr_mutex = &CPU->cpu_ppaddr_mutex;
3846 3931 mutex_enter(ppaddr_mutex);
3847 3932
3848 3933 hat_mempte_remap(pfn, pp_addr2, pte2,
3849 3934 PROT_READ | PROT_WRITE | HAT_STORECACHING_OK,
3850 3935 HAT_LOAD_NOCONSIST);
3851 3936 }
3852 3937
3853 3938 if (use_sse_pagezero) {
3854 3939 #ifdef __xpv
3855 3940 uint_t rem;
3856 3941
3857 3942 /*
3858 3943 * zero a byte at a time until properly aligned for
3859 3944 * block_zero_no_xmm().
3860 3945 */
3861 3946 while (!P2NPHASE(off, ((uint_t)BLOCKZEROALIGN)) && len-- > 0)
3862 3947 pp_addr2[off++] = 0;
3863 3948
3864 3949 /*
3865 3950 * Now use faster block_zero_no_xmm() for any range
3866 3951 * that is properly aligned and sized.
3867 3952 */
3868 3953 rem = P2PHASE(len, ((uint_t)BLOCKZEROALIGN));
3869 3954 len -= rem;
3870 3955 if (len != 0) {
3871 3956 block_zero_no_xmm(pp_addr2 + off, len);
3872 3957 off += len;
3873 3958 }
3874 3959
3875 3960 /*
3876 3961 * zero remainder with byte stores.
3877 3962 */
3878 3963 while (rem-- > 0)
3879 3964 pp_addr2[off++] = 0;
3880 3965 #else
3881 3966 hwblkclr(pp_addr2 + off, len);
3882 3967 #endif
3883 3968 } else {
3884 3969 bzero(pp_addr2 + off, len);
3885 3970 }
3886 3971
3887 3972 if (!kpm_enable || pfn_is_foreign(pfn)) {
3888 3973 #ifdef __xpv
3889 3974 /*
3890 3975 * On the hypervisor this page might get used for a page
3891 3976 * table before any intervening change to this mapping,
3892 3977 * so blow it away.
3893 3978 */
3894 3979 if (HYPERVISOR_update_va_mapping((uintptr_t)pp_addr2, 0,
3895 3980 UVMF_INVLPG) < 0)
3896 3981 panic("HYPERVISOR_update_va_mapping() failed");
3897 3982 #endif
3898 3983 mutex_exit(ppaddr_mutex);
3899 3984 }
3900 3985
3901 3986 kpreempt_enable();
3902 3987 }
3903 3988
3904 3989 /*
3905 3990 * Platform-dependent page scrub call.
3906 3991 */
3907 3992 void
3908 3993 pagescrub(page_t *pp, uint_t off, uint_t len)
3909 3994 {
3910 3995 /*
3911 3996 * For now, we rely on the fact that pagezero() will
3912 3997 * always clear UEs.
3913 3998 */
3914 3999 pagezero(pp, off, len);
3915 4000 }
3916 4001
3917 4002 /*
3918 4003 * set up two private addresses for use on a given CPU for use in ppcopy()
3919 4004 */
3920 4005 void
3921 4006 setup_vaddr_for_ppcopy(struct cpu *cpup)
3922 4007 {
3923 4008 void *addr;
3924 4009 hat_mempte_t pte_pa;
3925 4010
3926 4011 addr = vmem_alloc(heap_arena, mmu_ptob(1), VM_SLEEP);
3927 4012 pte_pa = hat_mempte_setup(addr);
3928 4013 cpup->cpu_caddr1 = addr;
3929 4014 cpup->cpu_caddr1pte = pte_pa;
3930 4015
3931 4016 addr = vmem_alloc(heap_arena, mmu_ptob(1), VM_SLEEP);
3932 4017 pte_pa = hat_mempte_setup(addr);
3933 4018 cpup->cpu_caddr2 = addr;
3934 4019 cpup->cpu_caddr2pte = pte_pa;
3935 4020
3936 4021 mutex_init(&cpup->cpu_ppaddr_mutex, NULL, MUTEX_DEFAULT, NULL);
3937 4022 }
3938 4023
3939 4024 /*
3940 4025 * Undo setup_vaddr_for_ppcopy
3941 4026 */
3942 4027 void
3943 4028 teardown_vaddr_for_ppcopy(struct cpu *cpup)
3944 4029 {
3945 4030 mutex_destroy(&cpup->cpu_ppaddr_mutex);
3946 4031
3947 4032 hat_mempte_release(cpup->cpu_caddr2, cpup->cpu_caddr2pte);
3948 4033 cpup->cpu_caddr2pte = 0;
3949 4034 vmem_free(heap_arena, cpup->cpu_caddr2, mmu_ptob(1));
3950 4035 cpup->cpu_caddr2 = 0;
3951 4036
3952 4037 hat_mempte_release(cpup->cpu_caddr1, cpup->cpu_caddr1pte);
3953 4038 cpup->cpu_caddr1pte = 0;
3954 4039 vmem_free(heap_arena, cpup->cpu_caddr1, mmu_ptob(1));
3955 4040 cpup->cpu_caddr1 = 0;
3956 4041 }
3957 4042
3958 4043 /*
3959 4044 * Function for flushing D-cache when performing module relocations
3960 4045 * to an alternate mapping. Unnecessary on Intel / AMD platforms.
3961 4046 */
3962 4047 void
3963 4048 dcache_flushall()
3964 4049 {}
3965 4050
3966 4051 /*
3967 4052 * Allocate a memory page. The argument 'seed' can be any pseudo-random
3968 4053 * number to vary where the pages come from. This is quite a hacked up
3969 4054 * method -- it works for now, but really needs to be fixed up a bit.
3970 4055 *
3971 4056 * We currently use page_create_va() on the kvp with fake offsets,
3972 4057 * segments and virt address. This is pretty bogus, but was copied from the
3973 4058 * old hat_i86.c code. A better approach would be to specify either mnode
3974 4059 * random or mnode local and takes a page from whatever color has the MOST
3975 4060 * available - this would have a minimal impact on page coloring.
3976 4061 */
3977 4062 page_t *
3978 4063 page_get_physical(uintptr_t seed)
3979 4064 {
3980 4065 page_t *pp;
3981 4066 u_offset_t offset;
3982 4067 static struct seg tmpseg;
3983 4068 static uintptr_t ctr = 0;
3984 4069
3985 4070 /*
3986 4071 * This code is gross, we really need a simpler page allocator.
3987 4072 *
3988 4073 * We need to assign an offset for the page to call page_create_va()
3989 4074 * To avoid conflicts with other pages, we get creative with the offset.
3990 4075 * For 32 bits, we need an offset > 4Gig
3991 4076 * For 64 bits, need an offset somewhere in the VA hole.
3992 4077 */
3993 4078 offset = seed;
3994 4079 if (offset > kernelbase)
3995 4080 offset -= kernelbase;
3996 4081 offset <<= MMU_PAGESHIFT;
3997 4082 #if defined(__amd64)
3998 4083 offset += mmu.hole_start; /* something in VA hole */
3999 4084 #else
4000 4085 offset += 1ULL << 40; /* something > 4 Gig */
4001 4086 #endif
4002 4087
4003 4088 if (page_resv(1, KM_NOSLEEP) == 0)
4004 4089 return (NULL);
4005 4090
4006 4091 #ifdef DEBUG
4007 4092 pp = page_exists(&kvp, offset);
4008 4093 if (pp != NULL)
4009 4094 panic("page already exists %p", (void *)pp);
4010 4095 #endif
4011 4096
4012 4097 pp = page_create_va(&kvp, offset, MMU_PAGESIZE, PG_EXCL,
4013 4098 &tmpseg, (caddr_t)(ctr += MMU_PAGESIZE)); /* changing VA usage */
4014 4099 if (pp != NULL) {
4015 4100 page_io_unlock(pp);
4016 4101 page_downgrade(pp);
4017 4102 }
4018 4103 return (pp);
4019 4104 }
↓ open down ↓ |
3642 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX