1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/t_lock.h> 31 #include <sys/param.h> 32 #include <sys/sysmacros.h> 33 #include <sys/tuneable.h> 34 #include <sys/systm.h> 35 #include <sys/vm.h> 36 #include <sys/kmem.h> 37 #include <sys/vmem.h> 38 #include <sys/mman.h> 39 #include <sys/cmn_err.h> 40 #include <sys/debug.h> 41 #include <sys/dumphdr.h> 42 #include <sys/bootconf.h> 43 #include <sys/lgrp.h> 44 #include <vm/seg_kmem.h> 45 #include <vm/hat.h> 46 #include <vm/page.h> 47 #include <vm/vm_dep.h> 48 #include <vm/faultcode.h> 49 #include <sys/promif.h> 50 #include <vm/seg_kp.h> 51 #include <sys/bitmap.h> 52 #include <sys/mem_cage.h> 53 54 /* 55 * seg_kmem is the primary kernel memory segment driver. It 56 * maps the kernel heap [kernelheap, ekernelheap), module text, 57 * and all memory which was allocated before the VM was initialized 58 * into kas. 59 * 60 * Pages which belong to seg_kmem are hashed into &kvp vnode at 61 * an offset equal to (u_offset_t)virt_addr, and have p_lckcnt >= 1. 62 * They must never be paged out since segkmem_fault() is a no-op to 63 * prevent recursive faults. 64 * 65 * Currently, seg_kmem pages are sharelocked (p_sharelock == 1) on 66 * __x86 and are unlocked (p_sharelock == 0) on __sparc. Once __x86 67 * supports relocation the #ifdef kludges can be removed. 68 * 69 * seg_kmem pages may be subject to relocation by page_relocate(), 70 * provided that the HAT supports it; if this is so, segkmem_reloc 71 * will be set to a nonzero value. All boot time allocated memory as 72 * well as static memory is considered off limits to relocation. 73 * Pages are "relocatable" if p_state does not have P_NORELOC set, so 74 * we request P_NORELOC pages for memory that isn't safe to relocate. 75 * 76 * The kernel heap is logically divided up into four pieces: 77 * 78 * heap32_arena is for allocations that require 32-bit absolute 79 * virtual addresses (e.g. code that uses 32-bit pointers/offsets). 80 * 81 * heap_core is for allocations that require 2GB *relative* 82 * offsets; in other words all memory from heap_core is within 83 * 2GB of all other memory from the same arena. This is a requirement 84 * of the addressing modes of some processors in supervisor code. 85 * 86 * heap_arena is the general heap arena. 87 * 88 * static_arena is the static memory arena. Allocations from it 89 * are not subject to relocation so it is safe to use the memory 90 * physical address as well as the virtual address (e.g. the VA to 91 * PA translations are static). Caches may import from static_arena; 92 * all other static memory allocations should use static_alloc_arena. 93 * 94 * On some platforms which have limited virtual address space, seg_kmem 95 * may share [kernelheap, ekernelheap) with seg_kp; if this is so, 96 * segkp_bitmap is non-NULL, and each bit represents a page of virtual 97 * address space which is actually seg_kp mapped. 98 */ 99 100 extern ulong_t *segkp_bitmap; /* Is set if segkp is from the kernel heap */ 101 102 char *kernelheap; /* start of primary kernel heap */ 103 char *ekernelheap; /* end of primary kernel heap */ 104 struct seg kvseg; /* primary kernel heap segment */ 105 struct seg kvseg_core; /* "core" kernel heap segment */ 106 vmem_t *heap_arena; /* primary kernel heap arena */ 107 vmem_t *heap_core_arena; /* core kernel heap arena */ 108 char *heap_core_base; /* start of core kernel heap arena */ 109 char *heap_lp_base; /* start of kernel large page heap arena */ 110 char *heap_lp_end; /* end of kernel large page heap arena */ 111 vmem_t *hat_memload_arena; /* HAT translation data */ 112 struct seg kvseg32; /* 32-bit kernel heap segment */ 113 vmem_t *heap32_arena; /* 32-bit kernel heap arena */ 114 vmem_t *heaptext_arena; /* heaptext arena */ 115 struct as kas; /* kernel address space */ 116 struct vnode kvp; /* vnode for all segkmem pages */ 117 int segkmem_reloc; /* enable/disable relocatable segkmem pages */ 118 vmem_t *static_arena; /* arena for caches to import static memory */ 119 vmem_t *static_alloc_arena; /* arena for allocating static memory */ 120 121 /* 122 * seg_kmem driver can map part of the kernel heap with large pages. 123 * Currently this functionality is implemented for sparc platforms only. 124 * 125 * The large page size "segkmem_lpsize" for kernel heap is selected in the 126 * platform specific code. It can also be modified via /etc/system file. 127 * Setting segkmem_lpsize to PAGESIZE in /etc/system disables usage of large 128 * pages for kernel heap. "segkmem_lpshift" is adjusted appropriately to 129 * match segkmem_lpsize. 130 * 131 * At boot time we carve from kernel heap arena a range of virtual addresses 132 * that will be used for large page mappings. This range [heap_lp_base, 133 * heap_lp_end) is set up as a separate vmem arena - "heap_lp_arena". We also 134 * create "kmem_lp_arena" that caches memory already backed up by large 135 * pages. kmem_lp_arena imports virtual segments from heap_lp_arena. 136 */ 137 138 size_t segkmem_lpsize; 139 static uint_t segkmem_lpshift = PAGESHIFT; 140 141 size_t segkmem_kmemlp_quantum = 0x400000; /* 4MB */ 142 size_t segkmem_heaplp_quantum; 143 vmem_t *heap_lp_arena; 144 static vmem_t *kmem_lp_arena; 145 static vmem_t *segkmem_ppa_arena; 146 static segkmem_lpcb_t segkmem_lpcb; 147 148 /* 149 * We use "segkmem_kmemlp_max" to limit the total amount of physical memory 150 * consumed by the large page heap. By default this parameter is set to 1/8 of 151 * physmem but can be adjusted through /etc/system either directly or 152 * indirectly by setting "segkmem_kmemlp_pcnt" to the percent of physmem 153 * we allow for large page heap. 154 */ 155 size_t segkmem_kmemlp_max; 156 static uint_t segkmem_kmemlp_pcnt; 157 158 /* 159 * Getting large pages for kernel heap could be problematic due to 160 * physical memory fragmentation. That's why we allow to preallocate 161 * "segkmem_kmemlp_min" bytes at boot time. 162 */ 163 static size_t segkmem_kmemlp_min; 164 165 /* 166 * Throttling is used to avoid expensive tries to allocate large pages 167 * for kernel heap when a lot of succesive attempts to do so fail. 168 */ 169 static ulong_t segkmem_lpthrottle_max = 0x400000; 170 static ulong_t segkmem_lpthrottle_start = 0x40; 171 static ulong_t segkmem_use_lpthrottle = 1; 172 173 /* 174 * Freed pages accumulate on a garbage list until segkmem is ready, 175 * at which point we call segkmem_gc() to free it all. 176 */ 177 typedef struct segkmem_gc_list { 178 struct segkmem_gc_list *gc_next; 179 vmem_t *gc_arena; 180 size_t gc_size; 181 } segkmem_gc_list_t; 182 183 static segkmem_gc_list_t *segkmem_gc_list; 184 185 /* 186 * Allocations from the hat_memload arena add VM_MEMLOAD to their 187 * vmflags so that segkmem_xalloc() can inform the hat layer that it needs 188 * to take steps to prevent infinite recursion. HAT allocations also 189 * must be non-relocatable to prevent recursive page faults. 190 */ 191 static void * 192 hat_memload_alloc(vmem_t *vmp, size_t size, int flags) 193 { 194 flags |= (VM_MEMLOAD | VM_NORELOC); 195 return (segkmem_alloc(vmp, size, flags)); 196 } 197 198 /* 199 * Allocations from static_arena arena (or any other arena that uses 200 * segkmem_alloc_permanent()) require non-relocatable (permanently 201 * wired) memory pages, since these pages are referenced by physical 202 * as well as virtual address. 203 */ 204 void * 205 segkmem_alloc_permanent(vmem_t *vmp, size_t size, int flags) 206 { 207 return (segkmem_alloc(vmp, size, flags | VM_NORELOC)); 208 } 209 210 /* 211 * Initialize kernel heap boundaries. 212 */ 213 void 214 kernelheap_init( 215 void *heap_start, 216 void *heap_end, 217 char *first_avail, 218 void *core_start, 219 void *core_end) 220 { 221 uintptr_t textbase; 222 size_t core_size; 223 size_t heap_size; 224 vmem_t *heaptext_parent; 225 size_t heap_lp_size = 0; 226 227 kernelheap = heap_start; 228 ekernelheap = heap_end; 229 230 #ifdef __sparc 231 heap_lp_size = (((uintptr_t)heap_end - (uintptr_t)heap_start) / 4); 232 heap_lp_base = ekernelheap - heap_lp_size; 233 heap_lp_end = heap_lp_base + heap_lp_size; 234 #endif /* __sparc */ 235 236 /* 237 * If this platform has a 'core' heap area, then the space for 238 * overflow module text should be carved out of the end of that 239 * heap. Otherwise, it gets carved out of the general purpose 240 * heap. 241 */ 242 core_size = (uintptr_t)core_end - (uintptr_t)core_start; 243 if (core_size > 0) { 244 ASSERT(core_size >= HEAPTEXT_SIZE); 245 textbase = (uintptr_t)core_end - HEAPTEXT_SIZE; 246 core_size -= HEAPTEXT_SIZE; 247 } 248 #ifndef __sparc 249 else { 250 ekernelheap -= HEAPTEXT_SIZE; 251 textbase = (uintptr_t)ekernelheap; 252 } 253 #endif 254 255 heap_size = (uintptr_t)ekernelheap - (uintptr_t)kernelheap; 256 heap_arena = vmem_init("heap", kernelheap, heap_size, PAGESIZE, 257 segkmem_alloc, segkmem_free); 258 259 if (core_size > 0) { 260 heap_core_arena = vmem_create("heap_core", core_start, 261 core_size, PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP); 262 heap_core_base = core_start; 263 } else { 264 heap_core_arena = heap_arena; 265 heap_core_base = kernelheap; 266 } 267 268 /* 269 * reserve space for the large page heap. If large pages for kernel 270 * heap is enabled large page heap arean will be created later in the 271 * boot sequence in segkmem_heap_lp_init(). Otherwise the allocated 272 * range will be returned back to the heap_arena. 273 */ 274 if (heap_lp_size) { 275 (void) vmem_xalloc(heap_arena, heap_lp_size, PAGESIZE, 0, 0, 276 heap_lp_base, heap_lp_end, 277 VM_NOSLEEP | VM_BESTFIT | VM_PANIC); 278 } 279 280 /* 281 * Remove the already-spoken-for memory range [kernelheap, first_avail). 282 */ 283 (void) vmem_xalloc(heap_arena, first_avail - kernelheap, PAGESIZE, 284 0, 0, kernelheap, first_avail, VM_NOSLEEP | VM_BESTFIT | VM_PANIC); 285 286 #ifdef __sparc 287 heap32_arena = vmem_create("heap32", (void *)SYSBASE32, 288 SYSLIMIT32 - SYSBASE32 - HEAPTEXT_SIZE, PAGESIZE, NULL, 289 NULL, NULL, 0, VM_SLEEP); 290 291 textbase = SYSLIMIT32 - HEAPTEXT_SIZE; 292 heaptext_parent = NULL; 293 #else /* __sparc */ 294 heap32_arena = heap_core_arena; 295 heaptext_parent = heap_core_arena; 296 #endif /* __sparc */ 297 298 heaptext_arena = vmem_create("heaptext", (void *)textbase, 299 HEAPTEXT_SIZE, PAGESIZE, NULL, NULL, heaptext_parent, 0, VM_SLEEP); 300 301 /* 302 * Create a set of arenas for memory with static translations 303 * (e.g. VA -> PA translations cannot change). Since using 304 * kernel pages by physical address implies it isn't safe to 305 * walk across page boundaries, the static_arena quantum must 306 * be PAGESIZE. Any kmem caches that require static memory 307 * should source from static_arena, while direct allocations 308 * should only use static_alloc_arena. 309 */ 310 static_arena = vmem_create("static", NULL, 0, PAGESIZE, 311 segkmem_alloc_permanent, segkmem_free, heap_arena, 0, VM_SLEEP); 312 static_alloc_arena = vmem_create("static_alloc", NULL, 0, 313 sizeof (uint64_t), vmem_alloc, vmem_free, static_arena, 314 0, VM_SLEEP); 315 316 /* 317 * Create an arena for translation data (ptes, hmes, or hblks). 318 * We need an arena for this because hat_memload() is essential 319 * to vmem_populate() (see comments in common/os/vmem.c). 320 * 321 * Note: any kmem cache that allocates from hat_memload_arena 322 * must be created as a KMC_NOHASH cache (i.e. no external slab 323 * and bufctl structures to allocate) so that slab creation doesn't 324 * require anything more than a single vmem_alloc(). 325 */ 326 hat_memload_arena = vmem_create("hat_memload", NULL, 0, PAGESIZE, 327 hat_memload_alloc, segkmem_free, heap_arena, 0, 328 VM_SLEEP | VMC_POPULATOR); 329 } 330 331 /* 332 * Grow kernel heap downward. 333 */ 334 void 335 kernelheap_extend(void *range_start, void *range_end) 336 { 337 size_t len = (uintptr_t)range_end - (uintptr_t)range_start; 338 339 ASSERT(range_start < range_end && range_end == kernelheap); 340 341 if (vmem_add(heap_arena, range_start, len, VM_NOSLEEP) == NULL) { 342 cmn_err(CE_WARN, "Could not grow kernel heap below 0x%p", 343 (void *)kernelheap); 344 } else { 345 kernelheap = range_start; 346 } 347 } 348 349 void 350 boot_mapin(caddr_t addr, size_t size) 351 { 352 caddr_t eaddr; 353 page_t *pp; 354 pfn_t pfnum; 355 356 if (page_resv(btop(size), KM_NOSLEEP) == 0) 357 panic("boot_mapin: page_resv failed"); 358 359 for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) { 360 pfnum = va_to_pfn(addr); 361 if ((pp = page_numtopp_nolock(pfnum)) == NULL) 362 panic("boot_mapin(): No pp for pfnum = %lx", pfnum); 363 364 /* 365 * must break up any large pages that may have constituent 366 * pages being utilized for BOP_ALLOC()'s before calling 367 * page_numtopp().The locking code (ie. page_reclaim()) 368 * can't handle them 369 */ 370 if (pp->p_szc != 0) 371 page_boot_demote(pp); 372 373 pp = page_numtopp(pfnum, SE_EXCL); 374 if (pp == NULL || PP_ISFREE(pp)) 375 panic("boot_alloc: pp is NULL or free"); 376 377 /* 378 * If the cage is on but doesn't yet contain this page, 379 * mark it as non-relocatable. 380 */ 381 if (kcage_on && !PP_ISNORELOC(pp)) 382 PP_SETNORELOC(pp); 383 384 (void) page_hashin(pp, &kvp, (u_offset_t)(uintptr_t)addr, NULL); 385 pp->p_lckcnt = 1; 386 #if defined(__x86) 387 page_downgrade(pp); 388 #else 389 page_unlock(pp); 390 #endif 391 } 392 } 393 394 /* 395 * Get pages from boot and hash them into the kernel's vp. 396 * Used after page structs have been allocated, but before segkmem is ready. 397 */ 398 void * 399 boot_alloc(void *inaddr, size_t size, uint_t align) 400 { 401 caddr_t addr = inaddr; 402 403 if (bootops == NULL) 404 prom_panic("boot_alloc: attempt to allocate memory after " 405 "BOP_GONE"); 406 407 size = ptob(btopr(size)); 408 if (BOP_ALLOC(bootops, addr, size, align) != addr) 409 panic("boot_alloc: BOP_ALLOC failed"); 410 boot_mapin((caddr_t)addr, size); 411 return (addr); 412 } 413 414 static void 415 segkmem_badop() 416 { 417 panic("segkmem_badop"); 418 } 419 420 #define SEGKMEM_BADOP(t) (t(*)())segkmem_badop 421 422 /*ARGSUSED*/ 423 static faultcode_t 424 segkmem_fault(struct hat *hat, struct seg *seg, caddr_t addr, size_t size, 425 enum fault_type type, enum seg_rw rw) 426 { 427 ASSERT(RW_READ_HELD(&seg->s_as->a_lock)); 428 429 if (seg->s_as != &kas || size > seg->s_size || 430 addr < seg->s_base || addr + size > seg->s_base + seg->s_size) 431 panic("segkmem_fault: bad args"); 432 433 if (segkp_bitmap && seg == &kvseg) { 434 435 /* 436 * If it is one of segkp pages, call segkp_fault. 437 */ 438 if (BT_TEST(segkp_bitmap, 439 btop((uintptr_t)(addr - seg->s_base)))) 440 return (SEGOP_FAULT(hat, segkp, addr, size, type, rw)); 441 } 442 443 switch (type) { 444 case F_SOFTLOCK: /* lock down already-loaded translations */ 445 if (rw == S_OTHER) { 446 hat_reserve(seg->s_as, addr, size); 447 return (0); 448 } 449 /*FALLTHROUGH*/ 450 case F_SOFTUNLOCK: 451 if (rw == S_READ || rw == S_WRITE) 452 return (0); 453 /*FALLTHROUGH*/ 454 default: 455 break; 456 } 457 return (FC_NOSUPPORT); 458 } 459 460 static int 461 segkmem_setprot(struct seg *seg, caddr_t addr, size_t size, uint_t prot) 462 { 463 ASSERT(RW_LOCK_HELD(&seg->s_as->a_lock)); 464 465 if (seg->s_as != &kas || size > seg->s_size || 466 addr < seg->s_base || addr + size > seg->s_base + seg->s_size) 467 panic("segkmem_setprot: bad args"); 468 469 if (segkp_bitmap && seg == &kvseg) { 470 471 /* 472 * If it is one of segkp pages, call segkp. 473 */ 474 if (BT_TEST(segkp_bitmap, 475 btop((uintptr_t)(addr - seg->s_base)))) 476 return (SEGOP_SETPROT(segkp, addr, size, prot)); 477 } 478 479 if (prot == 0) 480 hat_unload(kas.a_hat, addr, size, HAT_UNLOAD); 481 else 482 hat_chgprot(kas.a_hat, addr, size, prot); 483 return (0); 484 } 485 486 /* 487 * This is a dummy segkmem function overloaded to call segkp 488 * when segkp is under the heap. 489 */ 490 /* ARGSUSED */ 491 static int 492 segkmem_checkprot(struct seg *seg, caddr_t addr, size_t size, uint_t prot) 493 { 494 ASSERT(RW_LOCK_HELD(&seg->s_as->a_lock)); 495 496 if (seg->s_as != &kas) 497 segkmem_badop(); 498 499 if (segkp_bitmap && seg == &kvseg) { 500 501 /* 502 * If it is one of segkp pages, call into segkp. 503 */ 504 if (BT_TEST(segkp_bitmap, 505 btop((uintptr_t)(addr - seg->s_base)))) 506 return (SEGOP_CHECKPROT(segkp, addr, size, prot)); 507 } 508 segkmem_badop(); 509 return (0); 510 } 511 512 /* 513 * This is a dummy segkmem function overloaded to call segkp 514 * when segkp is under the heap. 515 */ 516 /* ARGSUSED */ 517 static int 518 segkmem_kluster(struct seg *seg, caddr_t addr, ssize_t delta) 519 { 520 ASSERT(RW_LOCK_HELD(&seg->s_as->a_lock)); 521 522 if (seg->s_as != &kas) 523 segkmem_badop(); 524 525 if (segkp_bitmap && seg == &kvseg) { 526 527 /* 528 * If it is one of segkp pages, call into segkp. 529 */ 530 if (BT_TEST(segkp_bitmap, 531 btop((uintptr_t)(addr - seg->s_base)))) 532 return (SEGOP_KLUSTER(segkp, addr, delta)); 533 } 534 segkmem_badop(); 535 return (0); 536 } 537 538 static void 539 segkmem_xdump_range(void *arg, void *start, size_t size) 540 { 541 struct as *as = arg; 542 caddr_t addr = start; 543 caddr_t addr_end = addr + size; 544 545 while (addr < addr_end) { 546 pfn_t pfn = hat_getpfnum(kas.a_hat, addr); 547 if (pfn != PFN_INVALID && pfn <= physmax && pf_is_memory(pfn)) 548 dump_addpage(as, addr, pfn); 549 addr += PAGESIZE; 550 dump_timeleft = dump_timeout; 551 } 552 } 553 554 static void 555 segkmem_dump_range(void *arg, void *start, size_t size) 556 { 557 caddr_t addr = start; 558 caddr_t addr_end = addr + size; 559 560 /* 561 * If we are about to start dumping the range of addresses we 562 * carved out of the kernel heap for the large page heap walk 563 * heap_lp_arena to find what segments are actually populated 564 */ 565 if (SEGKMEM_USE_LARGEPAGES && 566 addr == heap_lp_base && addr_end == heap_lp_end && 567 vmem_size(heap_lp_arena, VMEM_ALLOC) < size) { 568 vmem_walk(heap_lp_arena, VMEM_ALLOC | VMEM_REENTRANT, 569 segkmem_xdump_range, arg); 570 } else { 571 segkmem_xdump_range(arg, start, size); 572 } 573 } 574 575 static void 576 segkmem_dump(struct seg *seg) 577 { 578 /* 579 * The kernel's heap_arena (represented by kvseg) is a very large 580 * VA space, most of which is typically unused. To speed up dumping 581 * we use vmem_walk() to quickly find the pieces of heap_arena that 582 * are actually in use. We do the same for heap32_arena and 583 * heap_core. 584 * 585 * We specify VMEM_REENTRANT to vmem_walk() because dump_addpage() 586 * may ultimately need to allocate memory. Reentrant walks are 587 * necessarily imperfect snapshots. The kernel heap continues 588 * to change during a live crash dump, for example. For a normal 589 * crash dump, however, we know that there won't be any other threads 590 * messing with the heap. Therefore, at worst, we may fail to dump 591 * the pages that get allocated by the act of dumping; but we will 592 * always dump every page that was allocated when the walk began. 593 * 594 * The other segkmem segments are dense (fully populated), so there's 595 * no need to use this technique when dumping them. 596 * 597 * Note: when adding special dump handling for any new sparsely- 598 * populated segments, be sure to add similar handling to the ::kgrep 599 * code in mdb. 600 */ 601 if (seg == &kvseg) { 602 vmem_walk(heap_arena, VMEM_ALLOC | VMEM_REENTRANT, 603 segkmem_dump_range, seg->s_as); 604 #ifndef __sparc 605 vmem_walk(heaptext_arena, VMEM_ALLOC | VMEM_REENTRANT, 606 segkmem_dump_range, seg->s_as); 607 #endif 608 } else if (seg == &kvseg_core) { 609 vmem_walk(heap_core_arena, VMEM_ALLOC | VMEM_REENTRANT, 610 segkmem_dump_range, seg->s_as); 611 } else if (seg == &kvseg32) { 612 vmem_walk(heap32_arena, VMEM_ALLOC | VMEM_REENTRANT, 613 segkmem_dump_range, seg->s_as); 614 vmem_walk(heaptext_arena, VMEM_ALLOC | VMEM_REENTRANT, 615 segkmem_dump_range, seg->s_as); 616 } else { 617 segkmem_dump_range(seg->s_as, seg->s_base, seg->s_size); 618 } 619 } 620 621 /* 622 * lock/unlock kmem pages over a given range [addr, addr+len). 623 * Returns a shadow list of pages in ppp if *ppp is not NULL 624 * and memory can be allocated to hold the shadow list. 625 */ 626 /*ARGSUSED*/ 627 static int 628 segkmem_pagelock(struct seg *seg, caddr_t addr, size_t len, 629 page_t ***ppp, enum lock_type type, enum seg_rw rw) 630 { 631 page_t **pplist, *pp; 632 pgcnt_t npages; 633 size_t nb; 634 635 if (segkp_bitmap && seg == &kvseg) { 636 /* 637 * If it is one of segkp pages, call into segkp. 638 */ 639 if (BT_TEST(segkp_bitmap, 640 btop((uintptr_t)(addr - seg->s_base)))) 641 return (SEGOP_PAGELOCK(segkp, addr, len, ppp, 642 type, rw)); 643 } 644 645 if (type == L_PAGERECLAIM) 646 return (ENOTSUP); 647 648 npages = btopr(len); 649 nb = sizeof (page_t *) * npages; 650 651 if (type == L_PAGEUNLOCK) { 652 if ((pplist = *ppp) == NULL) { 653 /* 654 * No shadow list. Iterate over the range 655 * using page_find() and unlock the pages 656 * that we encounter. 657 */ 658 while (npages--) { 659 pp = page_find(&kvp, 660 (u_offset_t)(uintptr_t)addr); 661 if (pp) 662 page_unlock(pp); 663 addr += PAGESIZE; 664 } 665 return (0); 666 } 667 668 while (npages--) { 669 pp = *pplist++; 670 if (pp) 671 page_unlock(pp); 672 } 673 kmem_free(*ppp, nb); 674 return (0); 675 } 676 677 ASSERT(type == L_PAGELOCK); 678 679 pplist = NULL; 680 if (ppp != NULL) 681 *ppp = pplist = kmem_alloc(nb, KM_NOSLEEP); 682 683 while (npages--) { 684 pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, SE_SHARED); 685 /* 686 * We'd like to ASSERT(pp != NULL) here, but we can't 687 * because there are legitimate cases where the address 688 * isn't really mapped -- for instance, attaching a 689 * kernel debugger and poking at a non-existent address. 690 */ 691 if (pplist) 692 *pplist++ = pp; 693 addr += PAGESIZE; 694 } 695 return (0); 696 } 697 698 /* 699 * This is a dummy segkmem function overloaded to call segkp 700 * when segkp is under the heap. 701 */ 702 /* ARGSUSED */ 703 static int 704 segkmem_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp) 705 { 706 ASSERT(RW_LOCK_HELD(&seg->s_as->a_lock)); 707 708 if (seg->s_as != &kas) 709 segkmem_badop(); 710 711 if (segkp_bitmap && seg == &kvseg) { 712 713 /* 714 * If it is one of segkp pages, call into segkp. 715 */ 716 if (BT_TEST(segkp_bitmap, 717 btop((uintptr_t)(addr - seg->s_base)))) 718 return (SEGOP_GETMEMID(segkp, addr, memidp)); 719 } 720 segkmem_badop(); 721 return (0); 722 } 723 724 /*ARGSUSED*/ 725 static lgrp_mem_policy_info_t * 726 segkmem_getpolicy(struct seg *seg, caddr_t addr) 727 { 728 return (NULL); 729 } 730 731 /*ARGSUSED*/ 732 static int 733 segkmem_capable(struct seg *seg, segcapability_t capability) 734 { 735 if (capability == S_CAPABILITY_NOMINFLT) 736 return (1); 737 return (0); 738 } 739 740 static struct seg_ops segkmem_ops = { 741 SEGKMEM_BADOP(int), /* dup */ 742 SEGKMEM_BADOP(int), /* unmap */ 743 SEGKMEM_BADOP(void), /* free */ 744 segkmem_fault, 745 SEGKMEM_BADOP(faultcode_t), /* faulta */ 746 segkmem_setprot, 747 segkmem_checkprot, 748 segkmem_kluster, 749 SEGKMEM_BADOP(size_t), /* swapout */ 750 SEGKMEM_BADOP(int), /* sync */ 751 SEGKMEM_BADOP(size_t), /* incore */ 752 SEGKMEM_BADOP(int), /* lockop */ 753 SEGKMEM_BADOP(int), /* getprot */ 754 SEGKMEM_BADOP(u_offset_t), /* getoffset */ 755 SEGKMEM_BADOP(int), /* gettype */ 756 SEGKMEM_BADOP(int), /* getvp */ 757 SEGKMEM_BADOP(int), /* advise */ 758 segkmem_dump, 759 segkmem_pagelock, 760 SEGKMEM_BADOP(int), /* setpgsz */ 761 segkmem_getmemid, 762 segkmem_getpolicy, /* getpolicy */ 763 segkmem_capable, /* capable */ 764 }; 765 766 int 767 segkmem_create(struct seg *seg) 768 { 769 ASSERT(seg->s_as == &kas && RW_WRITE_HELD(&kas.a_lock)); 770 seg->s_ops = &segkmem_ops; 771 seg->s_data = NULL; 772 kas.a_size += seg->s_size; 773 return (0); 774 } 775 776 /*ARGSUSED*/ 777 page_t * 778 segkmem_page_create(void *addr, size_t size, int vmflag, void *arg) 779 { 780 struct seg kseg; 781 int pgflags; 782 783 kseg.s_as = &kas; 784 pgflags = PG_EXCL; 785 786 if (segkmem_reloc == 0 || (vmflag & VM_NORELOC)) 787 pgflags |= PG_NORELOC; 788 if ((vmflag & VM_NOSLEEP) == 0) 789 pgflags |= PG_WAIT; 790 if (vmflag & VM_PANIC) 791 pgflags |= PG_PANIC; 792 if (vmflag & VM_PUSHPAGE) 793 pgflags |= PG_PUSHPAGE; 794 795 return (page_create_va(&kvp, (u_offset_t)(uintptr_t)addr, size, 796 pgflags, &kseg, addr)); 797 } 798 799 /* 800 * Allocate pages to back the virtual address range [addr, addr + size). 801 * If addr is NULL, allocate the virtual address space as well. 802 */ 803 void * 804 segkmem_xalloc(vmem_t *vmp, void *inaddr, size_t size, int vmflag, uint_t attr, 805 page_t *(*page_create_func)(void *, size_t, int, void *), void *pcarg) 806 { 807 page_t *ppl; 808 caddr_t addr = inaddr; 809 pgcnt_t npages = btopr(size); 810 int allocflag; 811 812 if (inaddr == NULL && (addr = vmem_alloc(vmp, size, vmflag)) == NULL) 813 return (NULL); 814 815 ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0); 816 817 if (page_resv(npages, vmflag & VM_KMFLAGS) == 0) { 818 if (inaddr == NULL) 819 vmem_free(vmp, addr, size); 820 return (NULL); 821 } 822 823 ppl = page_create_func(addr, size, vmflag, pcarg); 824 if (ppl == NULL) { 825 if (inaddr == NULL) 826 vmem_free(vmp, addr, size); 827 page_unresv(npages); 828 return (NULL); 829 } 830 831 /* 832 * Under certain conditions, we need to let the HAT layer know 833 * that it cannot safely allocate memory. Allocations from 834 * the hat_memload vmem arena always need this, to prevent 835 * infinite recursion. 836 * 837 * In addition, the x86 hat cannot safely do memory 838 * allocations while in vmem_populate(), because there 839 * is no simple bound on its usage. 840 */ 841 if (vmflag & VM_MEMLOAD) 842 allocflag = HAT_NO_KALLOC; 843 #if defined(__x86) 844 else if (vmem_is_populator()) 845 allocflag = HAT_NO_KALLOC; 846 #endif 847 else 848 allocflag = 0; 849 850 while (ppl != NULL) { 851 page_t *pp = ppl; 852 page_sub(&ppl, pp); 853 ASSERT(page_iolock_assert(pp)); 854 ASSERT(PAGE_EXCL(pp)); 855 page_io_unlock(pp); 856 hat_memload(kas.a_hat, (caddr_t)(uintptr_t)pp->p_offset, pp, 857 (PROT_ALL & ~PROT_USER) | HAT_NOSYNC | attr, 858 HAT_LOAD_LOCK | allocflag); 859 pp->p_lckcnt = 1; 860 #if defined(__x86) 861 page_downgrade(pp); 862 #else 863 if (vmflag & SEGKMEM_SHARELOCKED) 864 page_downgrade(pp); 865 else 866 page_unlock(pp); 867 #endif 868 } 869 870 return (addr); 871 } 872 873 void * 874 segkmem_alloc(vmem_t *vmp, size_t size, int vmflag) 875 { 876 void *addr; 877 segkmem_gc_list_t *gcp, **prev_gcpp; 878 879 if (kvseg.s_base == NULL) { 880 #ifndef __sparc 881 if (bootops->bsys_alloc == NULL) 882 halt("Memory allocation between bop_alloc() and " 883 "kmem_alloc().\n"); 884 #endif 885 886 /* 887 * There's not a lot of memory to go around during boot, 888 * so recycle it if we can. 889 */ 890 for (prev_gcpp = &segkmem_gc_list; (gcp = *prev_gcpp) != NULL; 891 prev_gcpp = &gcp->gc_next) { 892 if (gcp->gc_arena == vmp && gcp->gc_size == size) { 893 *prev_gcpp = gcp->gc_next; 894 return (gcp); 895 } 896 } 897 898 addr = vmem_alloc(vmp, size, vmflag | VM_PANIC); 899 if (boot_alloc(addr, size, BO_NO_ALIGN) != addr) 900 panic("segkmem_alloc: boot_alloc failed"); 901 return (addr); 902 } 903 return (segkmem_xalloc(vmp, NULL, size, vmflag, 0, 904 segkmem_page_create, NULL)); 905 } 906 907 /* 908 * Any changes to this routine must also be carried over to 909 * devmap_free_pages() in the seg_dev driver. This is because 910 * we currently don't have a special kernel segment for non-paged 911 * kernel memory that is exported by drivers to user space. 912 */ 913 void 914 segkmem_free(vmem_t *vmp, void *inaddr, size_t size) 915 { 916 page_t *pp; 917 caddr_t addr = inaddr; 918 caddr_t eaddr; 919 pgcnt_t npages = btopr(size); 920 921 ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0); 922 923 if (kvseg.s_base == NULL) { 924 segkmem_gc_list_t *gc = inaddr; 925 gc->gc_arena = vmp; 926 gc->gc_size = size; 927 gc->gc_next = segkmem_gc_list; 928 segkmem_gc_list = gc; 929 return; 930 } 931 932 hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK); 933 934 for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) { 935 #if defined(__x86) 936 pp = page_find(&kvp, (u_offset_t)(uintptr_t)addr); 937 if (pp == NULL) 938 panic("segkmem_free: page not found"); 939 if (!page_tryupgrade(pp)) { 940 /* 941 * Some other thread has a sharelock. Wait for 942 * it to drop the lock so we can free this page. 943 */ 944 page_unlock(pp); 945 pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, 946 SE_EXCL); 947 } 948 #else 949 pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, SE_EXCL); 950 #endif 951 if (pp == NULL) 952 panic("segkmem_free: page not found"); 953 /* Clear p_lckcnt so page_destroy() doesn't update availrmem */ 954 pp->p_lckcnt = 0; 955 page_destroy(pp, 0); 956 } 957 page_unresv(npages); 958 959 if (vmp != NULL) 960 vmem_free(vmp, inaddr, size); 961 } 962 963 void 964 segkmem_gc(void) 965 { 966 ASSERT(kvseg.s_base != NULL); 967 while (segkmem_gc_list != NULL) { 968 segkmem_gc_list_t *gc = segkmem_gc_list; 969 segkmem_gc_list = gc->gc_next; 970 segkmem_free(gc->gc_arena, gc, gc->gc_size); 971 } 972 } 973 974 /* 975 * Legacy entry points from here to end of file. 976 */ 977 void 978 segkmem_mapin(struct seg *seg, void *addr, size_t size, uint_t vprot, 979 pfn_t pfn, uint_t flags) 980 { 981 hat_unload(seg->s_as->a_hat, addr, size, HAT_UNLOAD_UNLOCK); 982 hat_devload(seg->s_as->a_hat, addr, size, pfn, vprot, 983 flags | HAT_LOAD_LOCK); 984 } 985 986 void 987 segkmem_mapout(struct seg *seg, void *addr, size_t size) 988 { 989 hat_unload(seg->s_as->a_hat, addr, size, HAT_UNLOAD_UNLOCK); 990 } 991 992 void * 993 kmem_getpages(pgcnt_t npages, int kmflag) 994 { 995 return (kmem_alloc(ptob(npages), kmflag)); 996 } 997 998 void 999 kmem_freepages(void *addr, pgcnt_t npages) 1000 { 1001 kmem_free(addr, ptob(npages)); 1002 } 1003 1004 /* 1005 * segkmem_page_create_large() allocates a large page to be used for the kmem 1006 * caches. If kpr is enabled we ask for a relocatable page unless requested 1007 * otherwise. If kpr is disabled we have to ask for a non-reloc page 1008 */ 1009 static page_t * 1010 segkmem_page_create_large(void *addr, size_t size, int vmflag, void *arg) 1011 { 1012 int pgflags; 1013 1014 pgflags = PG_EXCL; 1015 1016 if (segkmem_reloc == 0 || (vmflag & VM_NORELOC)) 1017 pgflags |= PG_NORELOC; 1018 if (!(vmflag & VM_NOSLEEP)) 1019 pgflags |= PG_WAIT; 1020 if (vmflag & VM_PUSHPAGE) 1021 pgflags |= PG_PUSHPAGE; 1022 1023 return (page_create_va_large(&kvp, (u_offset_t)(uintptr_t)addr, size, 1024 pgflags, &kvseg, addr, arg)); 1025 } 1026 1027 /* 1028 * Allocate a large page to back the virtual address range 1029 * [addr, addr + size). If addr is NULL, allocate the virtual address 1030 * space as well. 1031 */ 1032 static void * 1033 segkmem_xalloc_lp(vmem_t *vmp, void *inaddr, size_t size, int vmflag, 1034 uint_t attr, page_t *(*page_create_func)(void *, size_t, int, void *), 1035 void *pcarg) 1036 { 1037 caddr_t addr = inaddr, pa; 1038 size_t lpsize = segkmem_lpsize; 1039 pgcnt_t npages = btopr(size); 1040 pgcnt_t nbpages = btop(lpsize); 1041 pgcnt_t nlpages = size >> segkmem_lpshift; 1042 size_t ppasize = nbpages * sizeof (page_t *); 1043 page_t *pp, *rootpp, **ppa, *pplist = NULL; 1044 int i; 1045 1046 if (page_resv(npages, vmflag & VM_KMFLAGS) == 0) { 1047 return (NULL); 1048 } 1049 1050 /* 1051 * allocate an array we need for hat_memload_array. 1052 * we use a separate arena to avoid recursion. 1053 * we will not need this array when hat_memload_array learns pp++ 1054 */ 1055 if ((ppa = vmem_alloc(segkmem_ppa_arena, ppasize, vmflag)) == NULL) { 1056 goto fail_array_alloc; 1057 } 1058 1059 if (inaddr == NULL && (addr = vmem_alloc(vmp, size, vmflag)) == NULL) 1060 goto fail_vmem_alloc; 1061 1062 ASSERT(((uintptr_t)addr & (lpsize - 1)) == 0); 1063 1064 /* create all the pages */ 1065 for (pa = addr, i = 0; i < nlpages; i++, pa += lpsize) { 1066 if ((pp = page_create_func(pa, lpsize, vmflag, pcarg)) == NULL) 1067 goto fail_page_create; 1068 page_list_concat(&pplist, &pp); 1069 } 1070 1071 /* at this point we have all the resource to complete the request */ 1072 while ((rootpp = pplist) != NULL) { 1073 for (i = 0; i < nbpages; i++) { 1074 ASSERT(pplist != NULL); 1075 pp = pplist; 1076 page_sub(&pplist, pp); 1077 ASSERT(page_iolock_assert(pp)); 1078 page_io_unlock(pp); 1079 ppa[i] = pp; 1080 } 1081 /* 1082 * Load the locked entry. It's OK to preload the entry into the 1083 * TSB since we now support large mappings in the kernel TSB. 1084 */ 1085 hat_memload_array(kas.a_hat, 1086 (caddr_t)(uintptr_t)rootpp->p_offset, lpsize, 1087 ppa, (PROT_ALL & ~PROT_USER) | HAT_NOSYNC | attr, 1088 HAT_LOAD_LOCK); 1089 1090 for (--i; i >= 0; --i) { 1091 ppa[i]->p_lckcnt = 1; 1092 page_unlock(ppa[i]); 1093 } 1094 } 1095 1096 vmem_free(segkmem_ppa_arena, ppa, ppasize); 1097 return (addr); 1098 1099 fail_page_create: 1100 while ((rootpp = pplist) != NULL) { 1101 for (i = 0, pp = pplist; i < nbpages; i++, pp = pplist) { 1102 ASSERT(pp != NULL); 1103 page_sub(&pplist, pp); 1104 ASSERT(page_iolock_assert(pp)); 1105 page_io_unlock(pp); 1106 } 1107 page_destroy_pages(rootpp); 1108 } 1109 1110 if (inaddr == NULL) 1111 vmem_free(vmp, addr, size); 1112 1113 fail_vmem_alloc: 1114 vmem_free(segkmem_ppa_arena, ppa, ppasize); 1115 1116 fail_array_alloc: 1117 page_unresv(npages); 1118 1119 return (NULL); 1120 } 1121 1122 static void 1123 segkmem_free_one_lp(caddr_t addr, size_t size) 1124 { 1125 page_t *pp, *rootpp = NULL; 1126 pgcnt_t pgs_left = btopr(size); 1127 1128 ASSERT(size == segkmem_lpsize); 1129 1130 hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK); 1131 1132 for (; pgs_left > 0; addr += PAGESIZE, pgs_left--) { 1133 pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, SE_EXCL); 1134 if (pp == NULL) 1135 panic("segkmem_free_one_lp: page not found"); 1136 ASSERT(PAGE_EXCL(pp)); 1137 pp->p_lckcnt = 0; 1138 if (rootpp == NULL) 1139 rootpp = pp; 1140 } 1141 ASSERT(rootpp != NULL); 1142 page_destroy_pages(rootpp); 1143 1144 /* page_unresv() is done by the caller */ 1145 } 1146 1147 /* 1148 * This function is called to import new spans into the vmem arenas like 1149 * kmem_default_arena and kmem_oversize_arena. It first tries to import 1150 * spans from large page arena - kmem_lp_arena. In order to do this it might 1151 * have to "upgrade the requested size" to kmem_lp_arena quantum. If 1152 * it was not able to satisfy the upgraded request it then calls regular 1153 * segkmem_alloc() that satisfies the request by importing from "*vmp" arena 1154 */ 1155 void * 1156 segkmem_alloc_lp(vmem_t *vmp, size_t *sizep, int vmflag) 1157 { 1158 size_t size; 1159 kthread_t *t = curthread; 1160 segkmem_lpcb_t *lpcb = &segkmem_lpcb; 1161 1162 ASSERT(sizep != NULL); 1163 1164 size = *sizep; 1165 1166 if (lpcb->lp_uselp && !(t->t_flag & T_PANIC) && 1167 !(vmflag & SEGKMEM_SHARELOCKED)) { 1168 1169 size_t kmemlp_qnt = segkmem_kmemlp_quantum; 1170 size_t asize = P2ROUNDUP(size, kmemlp_qnt); 1171 void *addr = NULL; 1172 ulong_t *lpthrtp = &lpcb->lp_throttle; 1173 ulong_t lpthrt = *lpthrtp; 1174 int dowakeup = 0; 1175 int doalloc = 1; 1176 1177 ASSERT(kmem_lp_arena != NULL); 1178 ASSERT(asize >= size); 1179 1180 if (lpthrt != 0) { 1181 /* try to update the throttle value */ 1182 lpthrt = atomic_add_long_nv(lpthrtp, 1); 1183 if (lpthrt >= segkmem_lpthrottle_max) { 1184 lpthrt = atomic_cas_ulong(lpthrtp, lpthrt, 1185 segkmem_lpthrottle_max / 4); 1186 } 1187 1188 /* 1189 * when we get above throttle start do an exponential 1190 * backoff at trying large pages and reaping 1191 */ 1192 if (lpthrt > segkmem_lpthrottle_start && 1193 (lpthrt & (lpthrt - 1))) { 1194 lpcb->allocs_throttled++; 1195 lpthrt--; 1196 if ((lpthrt & (lpthrt - 1)) == 0) 1197 kmem_reap(); 1198 return (segkmem_alloc(vmp, size, vmflag)); 1199 } 1200 } 1201 1202 if (!(vmflag & VM_NOSLEEP) && 1203 segkmem_heaplp_quantum >= (8 * kmemlp_qnt) && 1204 vmem_size(kmem_lp_arena, VMEM_FREE) <= kmemlp_qnt && 1205 asize < (segkmem_heaplp_quantum - kmemlp_qnt)) { 1206 1207 /* 1208 * we are low on free memory in kmem_lp_arena 1209 * we let only one guy to allocate heap_lp 1210 * quantum size chunk that everybody is going to 1211 * share 1212 */ 1213 mutex_enter(&lpcb->lp_lock); 1214 1215 if (lpcb->lp_wait) { 1216 1217 /* we are not the first one - wait */ 1218 cv_wait(&lpcb->lp_cv, &lpcb->lp_lock); 1219 if (vmem_size(kmem_lp_arena, VMEM_FREE) < 1220 kmemlp_qnt) { 1221 doalloc = 0; 1222 } 1223 } else if (vmem_size(kmem_lp_arena, VMEM_FREE) <= 1224 kmemlp_qnt) { 1225 1226 /* 1227 * we are the first one, make sure we import 1228 * a large page 1229 */ 1230 if (asize == kmemlp_qnt) 1231 asize += kmemlp_qnt; 1232 dowakeup = 1; 1233 lpcb->lp_wait = 1; 1234 } 1235 1236 mutex_exit(&lpcb->lp_lock); 1237 } 1238 1239 /* 1240 * VM_ABORT flag prevents sleeps in vmem_xalloc when 1241 * large pages are not available. In that case this allocation 1242 * attempt will fail and we will retry allocation with small 1243 * pages. We also do not want to panic if this allocation fails 1244 * because we are going to retry. 1245 */ 1246 if (doalloc) { 1247 addr = vmem_alloc(kmem_lp_arena, asize, 1248 (vmflag | VM_ABORT) & ~VM_PANIC); 1249 1250 if (dowakeup) { 1251 mutex_enter(&lpcb->lp_lock); 1252 ASSERT(lpcb->lp_wait != 0); 1253 lpcb->lp_wait = 0; 1254 cv_broadcast(&lpcb->lp_cv); 1255 mutex_exit(&lpcb->lp_lock); 1256 } 1257 } 1258 1259 if (addr != NULL) { 1260 *sizep = asize; 1261 *lpthrtp = 0; 1262 return (addr); 1263 } 1264 1265 if (vmflag & VM_NOSLEEP) 1266 lpcb->nosleep_allocs_failed++; 1267 else 1268 lpcb->sleep_allocs_failed++; 1269 lpcb->alloc_bytes_failed += size; 1270 1271 /* if large page throttling is not started yet do it */ 1272 if (segkmem_use_lpthrottle && lpthrt == 0) { 1273 lpthrt = atomic_cas_ulong(lpthrtp, lpthrt, 1); 1274 } 1275 } 1276 return (segkmem_alloc(vmp, size, vmflag)); 1277 } 1278 1279 void 1280 segkmem_free_lp(vmem_t *vmp, void *inaddr, size_t size) 1281 { 1282 if (kmem_lp_arena == NULL || !IS_KMEM_VA_LARGEPAGE((caddr_t)inaddr)) { 1283 segkmem_free(vmp, inaddr, size); 1284 } else { 1285 vmem_free(kmem_lp_arena, inaddr, size); 1286 } 1287 } 1288 1289 /* 1290 * segkmem_alloc_lpi() imports virtual memory from large page heap arena 1291 * into kmem_lp arena. In the process it maps the imported segment with 1292 * large pages 1293 */ 1294 static void * 1295 segkmem_alloc_lpi(vmem_t *vmp, size_t size, int vmflag) 1296 { 1297 segkmem_lpcb_t *lpcb = &segkmem_lpcb; 1298 void *addr; 1299 1300 ASSERT(size != 0); 1301 ASSERT(vmp == heap_lp_arena); 1302 1303 /* do not allow large page heap grow beyound limits */ 1304 if (vmem_size(vmp, VMEM_ALLOC) >= segkmem_kmemlp_max) { 1305 lpcb->allocs_limited++; 1306 return (NULL); 1307 } 1308 1309 addr = segkmem_xalloc_lp(vmp, NULL, size, vmflag, 0, 1310 segkmem_page_create_large, NULL); 1311 return (addr); 1312 } 1313 1314 /* 1315 * segkmem_free_lpi() returns virtual memory back into large page heap arena 1316 * from kmem_lp arena. Beore doing this it unmaps the segment and frees 1317 * large pages used to map it. 1318 */ 1319 static void 1320 segkmem_free_lpi(vmem_t *vmp, void *inaddr, size_t size) 1321 { 1322 pgcnt_t nlpages = size >> segkmem_lpshift; 1323 size_t lpsize = segkmem_lpsize; 1324 caddr_t addr = inaddr; 1325 pgcnt_t npages = btopr(size); 1326 int i; 1327 1328 ASSERT(vmp == heap_lp_arena); 1329 ASSERT(IS_KMEM_VA_LARGEPAGE(addr)); 1330 ASSERT(((uintptr_t)inaddr & (lpsize - 1)) == 0); 1331 1332 for (i = 0; i < nlpages; i++) { 1333 segkmem_free_one_lp(addr, lpsize); 1334 addr += lpsize; 1335 } 1336 1337 page_unresv(npages); 1338 1339 vmem_free(vmp, inaddr, size); 1340 } 1341 1342 /* 1343 * This function is called at system boot time by kmem_init right after 1344 * /etc/system file has been read. It checks based on hardware configuration 1345 * and /etc/system settings if system is going to use large pages. The 1346 * initialiazation necessary to actually start using large pages 1347 * happens later in the process after segkmem_heap_lp_init() is called. 1348 */ 1349 int 1350 segkmem_lpsetup() 1351 { 1352 int use_large_pages = 0; 1353 1354 #ifdef __sparc 1355 1356 size_t memtotal = physmem * PAGESIZE; 1357 1358 if (heap_lp_base == NULL) { 1359 segkmem_lpsize = PAGESIZE; 1360 return (0); 1361 } 1362 1363 /* get a platform dependent value of large page size for kernel heap */ 1364 segkmem_lpsize = get_segkmem_lpsize(segkmem_lpsize); 1365 1366 if (segkmem_lpsize <= PAGESIZE) { 1367 /* 1368 * put virtual space reserved for the large page kernel 1369 * back to the regular heap 1370 */ 1371 vmem_xfree(heap_arena, heap_lp_base, 1372 heap_lp_end - heap_lp_base); 1373 heap_lp_base = NULL; 1374 heap_lp_end = NULL; 1375 segkmem_lpsize = PAGESIZE; 1376 return (0); 1377 } 1378 1379 /* set heap_lp quantum if necessary */ 1380 if (segkmem_heaplp_quantum == 0 || 1381 (segkmem_heaplp_quantum & (segkmem_heaplp_quantum - 1)) || 1382 P2PHASE(segkmem_heaplp_quantum, segkmem_lpsize)) { 1383 segkmem_heaplp_quantum = segkmem_lpsize; 1384 } 1385 1386 /* set kmem_lp quantum if necessary */ 1387 if (segkmem_kmemlp_quantum == 0 || 1388 (segkmem_kmemlp_quantum & (segkmem_kmemlp_quantum - 1)) || 1389 segkmem_kmemlp_quantum > segkmem_heaplp_quantum) { 1390 segkmem_kmemlp_quantum = segkmem_heaplp_quantum; 1391 } 1392 1393 /* set total amount of memory allowed for large page kernel heap */ 1394 if (segkmem_kmemlp_max == 0) { 1395 if (segkmem_kmemlp_pcnt == 0 || segkmem_kmemlp_pcnt > 100) 1396 segkmem_kmemlp_pcnt = 12; 1397 segkmem_kmemlp_max = (memtotal * segkmem_kmemlp_pcnt) / 100; 1398 } 1399 segkmem_kmemlp_max = P2ROUNDUP(segkmem_kmemlp_max, 1400 segkmem_heaplp_quantum); 1401 1402 /* fix lp kmem preallocation request if necesssary */ 1403 if (segkmem_kmemlp_min) { 1404 segkmem_kmemlp_min = P2ROUNDUP(segkmem_kmemlp_min, 1405 segkmem_heaplp_quantum); 1406 if (segkmem_kmemlp_min > segkmem_kmemlp_max) 1407 segkmem_kmemlp_min = segkmem_kmemlp_max; 1408 } 1409 1410 use_large_pages = 1; 1411 segkmem_lpshift = page_get_shift(page_szc(segkmem_lpsize)); 1412 1413 #endif 1414 return (use_large_pages); 1415 } 1416 1417 #ifdef __sparc 1418 1419 1420 static void * 1421 segkmem_alloc_ppa(vmem_t *vmp, size_t size, int vmflag) 1422 { 1423 size_t ppaquantum = btopr(segkmem_lpsize) * sizeof (page_t *); 1424 void *addr; 1425 1426 if (ppaquantum <= PAGESIZE) 1427 return (segkmem_alloc(vmp, size, vmflag)); 1428 1429 ASSERT((size & (ppaquantum - 1)) == 0); 1430 1431 addr = vmem_xalloc(vmp, size, ppaquantum, 0, 0, NULL, NULL, vmflag); 1432 if (addr != NULL && segkmem_xalloc(vmp, addr, size, vmflag, 0, 1433 segkmem_page_create, NULL) == NULL) { 1434 vmem_xfree(vmp, addr, size); 1435 addr = NULL; 1436 } 1437 1438 return (addr); 1439 } 1440 1441 static void 1442 segkmem_free_ppa(vmem_t *vmp, void *addr, size_t size) 1443 { 1444 size_t ppaquantum = btopr(segkmem_lpsize) * sizeof (page_t *); 1445 1446 ASSERT(addr != NULL); 1447 1448 if (ppaquantum <= PAGESIZE) { 1449 segkmem_free(vmp, addr, size); 1450 } else { 1451 segkmem_free(NULL, addr, size); 1452 vmem_xfree(vmp, addr, size); 1453 } 1454 } 1455 1456 void 1457 segkmem_heap_lp_init() 1458 { 1459 segkmem_lpcb_t *lpcb = &segkmem_lpcb; 1460 size_t heap_lp_size = heap_lp_end - heap_lp_base; 1461 size_t lpsize = segkmem_lpsize; 1462 size_t ppaquantum; 1463 void *addr; 1464 1465 if (segkmem_lpsize <= PAGESIZE) { 1466 ASSERT(heap_lp_base == NULL); 1467 ASSERT(heap_lp_end == NULL); 1468 return; 1469 } 1470 1471 ASSERT(segkmem_heaplp_quantum >= lpsize); 1472 ASSERT((segkmem_heaplp_quantum & (lpsize - 1)) == 0); 1473 ASSERT(lpcb->lp_uselp == 0); 1474 ASSERT(heap_lp_base != NULL); 1475 ASSERT(heap_lp_end != NULL); 1476 ASSERT(heap_lp_base < heap_lp_end); 1477 ASSERT(heap_lp_arena == NULL); 1478 ASSERT(((uintptr_t)heap_lp_base & (lpsize - 1)) == 0); 1479 ASSERT(((uintptr_t)heap_lp_end & (lpsize - 1)) == 0); 1480 1481 /* create large page heap arena */ 1482 heap_lp_arena = vmem_create("heap_lp", heap_lp_base, heap_lp_size, 1483 segkmem_heaplp_quantum, NULL, NULL, NULL, 0, VM_SLEEP); 1484 1485 ASSERT(heap_lp_arena != NULL); 1486 1487 /* This arena caches memory already mapped by large pages */ 1488 kmem_lp_arena = vmem_create("kmem_lp", NULL, 0, segkmem_kmemlp_quantum, 1489 segkmem_alloc_lpi, segkmem_free_lpi, heap_lp_arena, 0, VM_SLEEP); 1490 1491 ASSERT(kmem_lp_arena != NULL); 1492 1493 mutex_init(&lpcb->lp_lock, NULL, MUTEX_DEFAULT, NULL); 1494 cv_init(&lpcb->lp_cv, NULL, CV_DEFAULT, NULL); 1495 1496 /* 1497 * this arena is used for the array of page_t pointers necessary 1498 * to call hat_mem_load_array 1499 */ 1500 ppaquantum = btopr(lpsize) * sizeof (page_t *); 1501 segkmem_ppa_arena = vmem_create("segkmem_ppa", NULL, 0, ppaquantum, 1502 segkmem_alloc_ppa, segkmem_free_ppa, heap_arena, ppaquantum, 1503 VM_SLEEP); 1504 1505 ASSERT(segkmem_ppa_arena != NULL); 1506 1507 /* prealloacate some memory for the lp kernel heap */ 1508 if (segkmem_kmemlp_min) { 1509 1510 ASSERT(P2PHASE(segkmem_kmemlp_min, 1511 segkmem_heaplp_quantum) == 0); 1512 1513 if ((addr = segkmem_alloc_lpi(heap_lp_arena, 1514 segkmem_kmemlp_min, VM_SLEEP)) != NULL) { 1515 1516 addr = vmem_add(kmem_lp_arena, addr, 1517 segkmem_kmemlp_min, VM_SLEEP); 1518 ASSERT(addr != NULL); 1519 } 1520 } 1521 1522 lpcb->lp_uselp = 1; 1523 } 1524 1525 #endif 1526