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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 /* 30 * Portions of this source code were derived from Berkeley 4.3 BSD 31 * under license from the Regents of the University of California. 32 */ 33 34 #pragma ident "%Z%%M% %I% %E% SMI" 35 36 /* 37 * UNIX machine dependent virtual memory support. 38 */ 39 40 #include <sys/vm.h> 41 #include <sys/exec.h> 42 #include <sys/cmn_err.h> 43 #include <sys/cpu_module.h> 44 #include <sys/cpu.h> 45 #include <sys/elf_SPARC.h> 46 #include <sys/archsystm.h> 47 #include <vm/hat_sfmmu.h> 48 #include <sys/memnode.h> 49 #include <sys/mem_cage.h> 50 #include <vm/vm_dep.h> 51 #include <sys/error.h> 52 #include <sys/machsystm.h> 53 #include <vm/seg_kmem.h> 54 55 uint_t page_colors = 0; 56 uint_t page_colors_mask = 0; 57 uint_t page_coloring_shift = 0; 58 int consistent_coloring; 59 60 uint_t mmu_page_sizes = MMU_PAGE_SIZES; 61 uint_t max_mmu_page_sizes = MMU_PAGE_SIZES; 62 uint_t mmu_hashcnt = MAX_HASHCNT; 63 uint_t max_mmu_hashcnt = MAX_HASHCNT; 64 size_t mmu_ism_pagesize = DEFAULT_ISM_PAGESIZE; 65 66 /* 67 * A bitmask of the page sizes supported by hardware based upon szc. 68 * The base pagesize (p_szc == 0) must always be supported by the hardware. 69 */ 70 int mmu_exported_pagesize_mask; 71 uint_t mmu_exported_page_sizes; 72 73 uint_t szc_2_userszc[MMU_PAGE_SIZES]; 74 uint_t userszc_2_szc[MMU_PAGE_SIZES]; 75 76 extern uint_t vac_colors_mask; 77 extern int vac_shift; 78 79 hw_pagesize_t hw_page_array[] = { 80 {MMU_PAGESIZE, MMU_PAGESHIFT, MMU_PAGESIZE >> MMU_PAGESHIFT}, 81 {MMU_PAGESIZE64K, MMU_PAGESHIFT64K, MMU_PAGESIZE64K >> MMU_PAGESHIFT}, 82 {MMU_PAGESIZE512K, MMU_PAGESHIFT512K, 83 MMU_PAGESIZE512K >> MMU_PAGESHIFT}, 84 {MMU_PAGESIZE4M, MMU_PAGESHIFT4M, MMU_PAGESIZE4M >> MMU_PAGESHIFT}, 85 {MMU_PAGESIZE32M, MMU_PAGESHIFT32M, MMU_PAGESIZE32M >> MMU_PAGESHIFT}, 86 {MMU_PAGESIZE256M, MMU_PAGESHIFT256M, 87 MMU_PAGESIZE256M >> MMU_PAGESHIFT}, 88 {0, 0, 0} 89 }; 90 91 /* 92 * Enable usage of 64k/4M pages for text and 64k pages for initdata for 93 * all sun4v platforms. These variables can be overwritten by the platmod 94 * or the CPU module. User can also change the setting via /etc/system. 95 */ 96 97 int use_text_pgsz64k = 1; 98 int use_text_pgsz4m = 1; 99 int use_initdata_pgsz64k = 1; 100 101 /* 102 * disable_text_largepages and disable_initdata_largepages bitmaks reflect 103 * both unconfigured and undesirable page sizes. Current implementation 104 * supports 64K and 4M page sizes for text and only 64K for data. Rest of 105 * the page sizes are not currently supported, hence disabled below. In 106 * future, when support is added for any other page size, it should be 107 * reflected below. 108 * 109 * Note that these bitmask can be set in platform or CPU specific code to 110 * disable page sizes that should not be used. These variables normally 111 * shouldn't be changed via /etc/system. 112 * 113 * These bitmasks are also updated within hat_init to reflect unsupported 114 * page sizes on a sun4v processor per mmu_exported_pagesize_mask global 115 * variable. 116 */ 117 118 int disable_text_largepages = 119 (1 << TTE512K) | (1 << TTE32M) | (1 << TTE256M) | (1 << TTE2G) | 120 (1 << TTE16G); 121 int disable_initdata_largepages = 122 (1 << TTE512K) | (1 << TTE4M) | (1 << TTE32M) | (1 << TTE256M) | 123 (1 << TTE2G) | (1 << TTE16G); 124 125 /* 126 * Minimum segment size tunables before 64K or 4M large pages 127 * should be used to map it. 128 */ 129 size_t text_pgsz64k_minsize = MMU_PAGESIZE64K; 130 size_t text_pgsz4m_minsize = MMU_PAGESIZE4M; 131 size_t initdata_pgsz64k_minsize = MMU_PAGESIZE64K; 132 133 size_t max_shm_lpsize = MMU_PAGESIZE4M; 134 135 /* 136 * map_addr_proc() is the routine called when the system is to 137 * choose an address for the user. We will pick an address 138 * range which is just below the current stack limit. The 139 * algorithm used for cache consistency on machines with virtual 140 * address caches is such that offset 0 in the vnode is always 141 * on a shm_alignment'ed aligned address. Unfortunately, this 142 * means that vnodes which are demand paged will not be mapped 143 * cache consistently with the executable images. When the 144 * cache alignment for a given object is inconsistent, the 145 * lower level code must manage the translations so that this 146 * is not seen here (at the cost of efficiency, of course). 147 * 148 * addrp is a value/result parameter. 149 * On input it is a hint from the user to be used in a completely 150 * machine dependent fashion. For MAP_ALIGN, addrp contains the 151 * minimal alignment. 152 * 153 * On output it is NULL if no address can be found in the current 154 * processes address space or else an address that is currently 155 * not mapped for len bytes with a page of red zone on either side. 156 * If vacalign is true, then the selected address will obey the alignment 157 * constraints of a vac machine based on the given off value. 158 */ 159 /*ARGSUSED3*/ 160 void 161 map_addr_proc(caddr_t *addrp, size_t len, offset_t off, int vacalign, 162 caddr_t userlimit, struct proc *p, uint_t flags) 163 { 164 struct as *as = p->p_as; 165 caddr_t addr; 166 caddr_t base; 167 size_t slen; 168 uintptr_t align_amount; 169 int allow_largepage_alignment = 1; 170 171 base = p->p_brkbase; 172 if (userlimit < as->a_userlimit) { 173 /* 174 * This happens when a program wants to map something in 175 * a range that's accessible to a program in a smaller 176 * address space. For example, a 64-bit program might 177 * be calling mmap32(2) to guarantee that the returned 178 * address is below 4Gbytes. 179 */ 180 ASSERT(userlimit > base); 181 slen = userlimit - base; 182 } else { 183 slen = p->p_usrstack - base - (((size_t)rctl_enforced_value( 184 rctlproc_legacy[RLIMIT_STACK], p->p_rctls, p) + PAGEOFFSET) 185 & PAGEMASK); 186 } 187 len = (len + PAGEOFFSET) & PAGEMASK; 188 189 /* 190 * Redzone for each side of the request. This is done to leave 191 * one page unmapped between segments. This is not required, but 192 * it's useful for the user because if their program strays across 193 * a segment boundary, it will catch a fault immediately making 194 * debugging a little easier. 195 */ 196 len += (2 * PAGESIZE); 197 198 /* 199 * If the request is larger than the size of a particular 200 * mmu level, then we use that level to map the request. 201 * But this requires that both the virtual and the physical 202 * addresses be aligned with respect to that level, so we 203 * do the virtual bit of nastiness here. 204 * 205 * For 32-bit processes, only those which have specified 206 * MAP_ALIGN or an addr will be aligned on a page size > 4MB. Otherwise 207 * we can potentially waste up to 256MB of the 4G process address 208 * space just for alignment. 209 * 210 * XXXQ Should iterate trough hw_page_array here to catch 211 * all supported pagesizes 212 */ 213 if (p->p_model == DATAMODEL_ILP32 && ((flags & MAP_ALIGN) == 0 || 214 ((uintptr_t)*addrp) != 0)) { 215 allow_largepage_alignment = 0; 216 } 217 if ((mmu_page_sizes == max_mmu_page_sizes) && 218 allow_largepage_alignment && 219 (len >= MMU_PAGESIZE256M)) { /* 256MB mappings */ 220 align_amount = MMU_PAGESIZE256M; 221 } else if ((mmu_page_sizes == max_mmu_page_sizes) && 222 allow_largepage_alignment && 223 (len >= MMU_PAGESIZE32M)) { /* 32MB mappings */ 224 align_amount = MMU_PAGESIZE32M; 225 } else if (len >= MMU_PAGESIZE4M) { /* 4MB mappings */ 226 align_amount = MMU_PAGESIZE4M; 227 } else if (len >= MMU_PAGESIZE512K) { /* 512KB mappings */ 228 align_amount = MMU_PAGESIZE512K; 229 } else if (len >= MMU_PAGESIZE64K) { /* 64KB mappings */ 230 align_amount = MMU_PAGESIZE64K; 231 } else { 232 /* 233 * Align virtual addresses on a 64K boundary to ensure 234 * that ELF shared libraries are mapped with the appropriate 235 * alignment constraints by the run-time linker. 236 */ 237 align_amount = ELF_SPARC_MAXPGSZ; 238 if ((flags & MAP_ALIGN) && ((uintptr_t)*addrp != 0) && 239 ((uintptr_t)*addrp < align_amount)) 240 align_amount = (uintptr_t)*addrp; 241 } 242 243 /* 244 * 64-bit processes require 1024K alignment of ELF shared libraries. 245 */ 246 if (p->p_model == DATAMODEL_LP64) 247 align_amount = MAX(align_amount, ELF_SPARCV9_MAXPGSZ); 248 #ifdef VAC 249 if (vac && vacalign && (align_amount < shm_alignment)) 250 align_amount = shm_alignment; 251 #endif 252 253 if ((flags & MAP_ALIGN) && ((uintptr_t)*addrp > align_amount)) { 254 align_amount = (uintptr_t)*addrp; 255 } 256 len += align_amount; 257 258 /* 259 * Look for a large enough hole starting below the stack limit. 260 * After finding it, use the upper part. Addition of PAGESIZE is 261 * for the redzone as described above. 262 */ 263 as_purge(as); 264 if (as_gap(as, len, &base, &slen, AH_HI, NULL) == 0) { 265 caddr_t as_addr; 266 267 addr = base + slen - len + PAGESIZE; 268 as_addr = addr; 269 /* 270 * Round address DOWN to the alignment amount, 271 * add the offset, and if this address is less 272 * than the original address, add alignment amount. 273 */ 274 addr = (caddr_t)((uintptr_t)addr & (~(align_amount - 1l))); 275 addr += (long)(off & (align_amount - 1l)); 276 if (addr < as_addr) { 277 addr += align_amount; 278 } 279 280 ASSERT(addr <= (as_addr + align_amount)); 281 ASSERT(((uintptr_t)addr & (align_amount - 1l)) == 282 ((uintptr_t)(off & (align_amount - 1l)))); 283 *addrp = addr; 284 285 } else { 286 *addrp = NULL; /* no more virtual space */ 287 } 288 } 289 290 /* Auto large page tunables. */ 291 int auto_lpg_tlb_threshold = 32; 292 int auto_lpg_minszc = TTE64K; 293 int auto_lpg_maxszc = TTE64K; 294 size_t auto_lpg_heap_default = MMU_PAGESIZE64K; 295 size_t auto_lpg_stack_default = MMU_PAGESIZE64K; 296 size_t auto_lpg_va_default = MMU_PAGESIZE64K; 297 size_t auto_lpg_remap_threshold = 0; /* always remap */ 298 /* 299 * Number of pages in 1 GB. Don't enable automatic large pages if we have 300 * fewer than this many pages. 301 */ 302 pgcnt_t auto_lpg_min_physmem = 1 << (30 - MMU_PAGESHIFT); 303 304 size_t 305 map_pgsz(int maptype, struct proc *p, caddr_t addr, size_t len, int *remap) 306 { 307 uint_t n; 308 size_t pgsz = 0; 309 310 if (remap) 311 *remap = (len > auto_lpg_remap_threshold); 312 313 switch (maptype) { 314 case MAPPGSZ_ISM: 315 n = hat_preferred_pgsz(p->p_as->a_hat, addr, len, maptype); 316 pgsz = hw_page_array[n].hp_size; 317 break; 318 319 case MAPPGSZ_VA: 320 pgsz = map_pgszva(p, addr, len); 321 break; 322 323 case MAPPGSZ_STK: 324 pgsz = map_pgszstk(p, addr, len); 325 break; 326 327 case MAPPGSZ_HEAP: 328 pgsz = map_pgszheap(p, addr, len); 329 break; 330 } 331 return (pgsz); 332 } 333 334 /* 335 * Platform-dependent page scrub call. 336 * We call hypervisor to scrub the page. 337 */ 338 void 339 pagescrub(page_t *pp, uint_t off, uint_t len) 340 { 341 uint64_t pa, length; 342 343 pa = (uint64_t)(pp->p_pagenum << MMU_PAGESHIFT + off); 344 length = (uint64_t)len; 345 346 (void) mem_scrub(pa, length); 347 } 348 349 void 350 sync_data_memory(caddr_t va, size_t len) 351 { 352 /* Call memory sync function */ 353 mem_sync(va, len); 354 } 355 356 size_t 357 mmu_get_kernel_lpsize(size_t lpsize) 358 { 359 extern int mmu_exported_pagesize_mask; 360 uint_t tte; 361 362 if (lpsize == 0) { 363 /* no setting for segkmem_lpsize in /etc/system: use default */ 364 if (mmu_exported_pagesize_mask & (1 << TTE256M)) { 365 lpsize = MMU_PAGESIZE256M; 366 } else if (mmu_exported_pagesize_mask & (1 << TTE4M)) { 367 lpsize = MMU_PAGESIZE4M; 368 } else if (mmu_exported_pagesize_mask & (1 << TTE64K)) { 369 lpsize = MMU_PAGESIZE64K; 370 } else { 371 lpsize = MMU_PAGESIZE; 372 } 373 374 return (lpsize); 375 } 376 377 for (tte = TTE8K; tte <= TTE256M; tte++) { 378 379 if ((mmu_exported_pagesize_mask & (1 << tte)) == 0) 380 continue; 381 382 if (lpsize == TTEBYTES(tte)) 383 return (lpsize); 384 } 385 386 lpsize = TTEBYTES(TTE8K); 387 return (lpsize); 388 } 389 390 void 391 mmu_init_kcontext() 392 { 393 } 394 395 /*ARGSUSED*/ 396 void 397 mmu_init_kernel_pgsz(struct hat *hat) 398 { 399 } 400 401 #define QUANTUM_SIZE 64 402 403 static vmem_t *contig_mem_slab_arena; 404 static vmem_t *contig_mem_arena; 405 406 uint_t contig_mem_slab_size = MMU_PAGESIZE4M; 407 408 static void * 409 contig_mem_span_alloc(vmem_t *vmp, size_t size, int vmflag) 410 { 411 page_t *ppl; 412 page_t *rootpp; 413 caddr_t addr = NULL; 414 pgcnt_t npages = btopr(size); 415 page_t **ppa; 416 int pgflags; 417 int i = 0; 418 419 420 /* 421 * The import request should be at least 422 * contig_mem_slab_size because that is the 423 * slab arena's quantum. The size can be 424 * further restricted since contiguous 425 * allocations larger than contig_mem_slab_size 426 * are not supported here. 427 */ 428 ASSERT(size == contig_mem_slab_size); 429 430 if ((addr = vmem_xalloc(vmp, size, size, 0, 0, 431 NULL, NULL, vmflag)) == NULL) { 432 return (NULL); 433 } 434 435 /* The address should be slab-size aligned. */ 436 ASSERT(((uintptr_t)addr & (contig_mem_slab_size - 1)) == 0); 437 438 if (page_resv(npages, vmflag & VM_KMFLAGS) == 0) { 439 vmem_xfree(vmp, addr, size); 440 return (NULL); 441 } 442 443 pgflags = PG_EXCL; 444 if ((vmflag & VM_NOSLEEP) == 0) 445 pgflags |= PG_WAIT; 446 if (vmflag & VM_PANIC) 447 pgflags |= PG_PANIC; 448 if (vmflag & VM_PUSHPAGE) 449 pgflags |= PG_PUSHPAGE; 450 451 ppl = page_create_va_large(&kvp, (u_offset_t)(uintptr_t)addr, size, 452 pgflags, &kvseg, addr, NULL); 453 454 if (ppl == NULL) { 455 vmem_xfree(vmp, addr, size); 456 page_unresv(npages); 457 return (NULL); 458 } 459 460 rootpp = ppl; 461 ppa = kmem_zalloc(npages * sizeof (page_t *), KM_SLEEP); 462 while (ppl != NULL) { 463 page_t *pp = ppl; 464 ppa[i++] = pp; 465 page_sub(&ppl, pp); 466 ASSERT(page_iolock_assert(pp)); 467 page_io_unlock(pp); 468 } 469 470 /* 471 * Load the locked entry. It's OK to preload the entry into 472 * the TSB since we now support large mappings in the kernel TSB. 473 */ 474 hat_memload_array(kas.a_hat, (caddr_t)rootpp->p_offset, size, 475 ppa, (PROT_ALL & ~PROT_USER) | HAT_NOSYNC, HAT_LOAD_LOCK); 476 477 for (--i; i >= 0; --i) { 478 (void) page_pp_lock(ppa[i], 0, 1); 479 page_unlock(ppa[i]); 480 } 481 482 kmem_free(ppa, npages * sizeof (page_t *)); 483 return (addr); 484 } 485 486 void 487 contig_mem_span_free(vmem_t *vmp, void *inaddr, size_t size) 488 { 489 page_t *pp; 490 caddr_t addr = inaddr; 491 caddr_t eaddr; 492 pgcnt_t npages = btopr(size); 493 pgcnt_t pgs_left = npages; 494 page_t *rootpp = NULL; 495 496 ASSERT(((uintptr_t)addr & (contig_mem_slab_size - 1)) == 0); 497 498 hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK); 499 500 for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) { 501 pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, SE_EXCL); 502 if (pp == NULL) 503 panic("contig_mem_span_free: page not found"); 504 505 ASSERT(PAGE_EXCL(pp)); 506 page_pp_unlock(pp, 0, 1); 507 508 if (rootpp == NULL) 509 rootpp = pp; 510 if (--pgs_left == 0) { 511 /* 512 * similar logic to segspt_free_pages, but we know we 513 * have one large page. 514 */ 515 page_destroy_pages(rootpp); 516 } 517 } 518 page_unresv(npages); 519 520 if (vmp != NULL) 521 vmem_xfree(vmp, inaddr, size); 522 } 523 524 static void * 525 contig_vmem_xalloc_aligned_wrapper(vmem_t *vmp, size_t size, int vmflag) 526 { 527 return (vmem_xalloc(vmp, size, size, 0, 0, NULL, NULL, vmflag)); 528 } 529 530 /* 531 * conting_mem_alloc_align allocates real contiguous memory with the specified 532 * alignment upto contig_mem_slab_size. The alignment must be a power of 2. 533 */ 534 void * 535 contig_mem_alloc_align(size_t size, size_t align) 536 { 537 ASSERT(align <= contig_mem_slab_size); 538 539 if ((align & (align - 1)) != 0) 540 return (NULL); 541 542 return (vmem_xalloc(contig_mem_arena, size, align, 0, 0, 543 NULL, NULL, VM_NOSLEEP)); 544 } 545 546 /* 547 * Allocates size aligned contiguous memory upto contig_mem_slab_size. 548 * Size must be a power of 2. 549 */ 550 void * 551 contig_mem_alloc(size_t size) 552 { 553 ASSERT((size & (size - 1)) == 0); 554 return (contig_mem_alloc_align(size, size)); 555 } 556 557 void 558 contig_mem_free(void *vaddr, size_t size) 559 { 560 vmem_xfree(contig_mem_arena, vaddr, size); 561 } 562 563 /* 564 * We create a set of stacked vmem arenas to enable us to 565 * allocate large >PAGESIZE chucks of contiguous Real Address space 566 * This is what the Dynamics TSB support does for TSBs. 567 * The contig_mem_arena import functions are exactly the same as the 568 * TSB kmem_default arena import functions. 569 */ 570 void 571 contig_mem_init(void) 572 { 573 574 contig_mem_slab_arena = vmem_create("contig_mem_slab_arena", NULL, 0, 575 contig_mem_slab_size, contig_vmem_xalloc_aligned_wrapper, 576 vmem_xfree, heap_arena, 0, VM_SLEEP); 577 578 contig_mem_arena = vmem_create("contig_mem_arena", NULL, 0, 579 QUANTUM_SIZE, contig_mem_span_alloc, contig_mem_span_free, 580 contig_mem_slab_arena, 0, VM_SLEEP | VM_BESTFIT); 581 582 } 583