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