xref: /titanic_44/usr/src/uts/common/vm/seg_kmem.c (revision ace1a5f11236a072fca1b5e0ea1416a083a9f2aa)
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 
732 static struct seg_ops segkmem_ops = {
733 	SEGKMEM_BADOP(int),		/* dup */
734 	SEGKMEM_BADOP(int),		/* unmap */
735 	SEGKMEM_BADOP(void),		/* free */
736 	segkmem_fault,
737 	SEGKMEM_BADOP(faultcode_t),	/* faulta */
738 	segkmem_setprot,
739 	segkmem_checkprot,
740 	segkmem_kluster,
741 	SEGKMEM_BADOP(size_t),		/* swapout */
742 	SEGKMEM_BADOP(int),		/* sync */
743 	SEGKMEM_BADOP(size_t),		/* incore */
744 	SEGKMEM_BADOP(int),		/* lockop */
745 	SEGKMEM_BADOP(int),		/* getprot */
746 	SEGKMEM_BADOP(u_offset_t),	/* getoffset */
747 	SEGKMEM_BADOP(int),		/* gettype */
748 	SEGKMEM_BADOP(int),		/* getvp */
749 	SEGKMEM_BADOP(int),		/* advise */
750 	segkmem_dump,
751 	segkmem_pagelock,
752 	SEGKMEM_BADOP(int),		/* setpgsz */
753 	segkmem_getmemid,
754 	segkmem_getpolicy,		/* getpolicy */
755 };
756 
757 int
758 segkmem_create(struct seg *seg)
759 {
760 	ASSERT(seg->s_as == &kas && RW_WRITE_HELD(&kas.a_lock));
761 	seg->s_ops = &segkmem_ops;
762 	seg->s_data = NULL;
763 	kas.a_size += seg->s_size;
764 	return (0);
765 }
766 
767 /*ARGSUSED*/
768 page_t *
769 segkmem_page_create(void *addr, size_t size, int vmflag, void *arg)
770 {
771 	struct seg kseg;
772 	int pgflags;
773 
774 	kseg.s_as = &kas;
775 	pgflags = PG_EXCL;
776 
777 	if (segkmem_reloc == 0 || (vmflag & VM_NORELOC))
778 		pgflags |= PG_NORELOC;
779 	if ((vmflag & VM_NOSLEEP) == 0)
780 		pgflags |= PG_WAIT;
781 	if (vmflag & VM_PANIC)
782 		pgflags |= PG_PANIC;
783 	if (vmflag & VM_PUSHPAGE)
784 		pgflags |= PG_PUSHPAGE;
785 
786 	return (page_create_va(&kvp, (u_offset_t)(uintptr_t)addr, size,
787 	    pgflags, &kseg, addr));
788 }
789 
790 /*
791  * Allocate pages to back the virtual address range [addr, addr + size).
792  * If addr is NULL, allocate the virtual address space as well.
793  */
794 void *
795 segkmem_xalloc(vmem_t *vmp, void *inaddr, size_t size, int vmflag, uint_t attr,
796 	page_t *(*page_create_func)(void *, size_t, int, void *), void *pcarg)
797 {
798 	page_t *ppl;
799 	caddr_t addr = inaddr;
800 	pgcnt_t npages = btopr(size);
801 	int allocflag;
802 
803 	if (inaddr == NULL && (addr = vmem_alloc(vmp, size, vmflag)) == NULL)
804 		return (NULL);
805 
806 	ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0);
807 
808 	if (page_resv(npages, vmflag & VM_KMFLAGS) == 0) {
809 		if (inaddr == NULL)
810 			vmem_free(vmp, addr, size);
811 		return (NULL);
812 	}
813 
814 	ppl = page_create_func(addr, size, vmflag, pcarg);
815 	if (ppl == NULL) {
816 		if (inaddr == NULL)
817 			vmem_free(vmp, addr, size);
818 		page_unresv(npages);
819 		return (NULL);
820 	}
821 
822 	/*
823 	 * Under certain conditions, we need to let the HAT layer know
824 	 * that it cannot safely allocate memory.  Allocations from
825 	 * the hat_memload vmem arena always need this, to prevent
826 	 * infinite recursion.
827 	 *
828 	 * In addition, the x86 hat cannot safely do memory
829 	 * allocations while in vmem_populate(), because there
830 	 * is no simple bound on its usage.
831 	 */
832 	if (vmflag & VM_MEMLOAD)
833 		allocflag = HAT_NO_KALLOC;
834 #if defined(__x86)
835 	else if (vmem_is_populator())
836 		allocflag = HAT_NO_KALLOC;
837 #endif
838 	else
839 		allocflag = 0;
840 
841 	while (ppl != NULL) {
842 		page_t *pp = ppl;
843 		page_sub(&ppl, pp);
844 		ASSERT(page_iolock_assert(pp));
845 		ASSERT(PAGE_EXCL(pp));
846 		page_io_unlock(pp);
847 		hat_memload(kas.a_hat, (caddr_t)(uintptr_t)pp->p_offset, pp,
848 		    (PROT_ALL & ~PROT_USER) | HAT_NOSYNC | attr,
849 		    HAT_LOAD_LOCK | allocflag);
850 		pp->p_lckcnt = 1;
851 #if defined(__x86)
852 		page_downgrade(pp);
853 #else
854 		if (vmflag & SEGKMEM_SHARELOCKED)
855 			page_downgrade(pp);
856 		else
857 			page_unlock(pp);
858 #endif
859 	}
860 
861 	return (addr);
862 }
863 
864 void *
865 segkmem_alloc(vmem_t *vmp, size_t size, int vmflag)
866 {
867 	void *addr;
868 	segkmem_gc_list_t *gcp, **prev_gcpp;
869 
870 	if (kvseg.s_base == NULL) {
871 #ifndef __sparc
872 		if (bootops->bsys_alloc == NULL)
873 			halt("Memory allocation between bop_alloc() and "
874 			    "kmem_alloc().\n");
875 #endif
876 
877 		/*
878 		 * There's not a lot of memory to go around during boot,
879 		 * so recycle it if we can.
880 		 */
881 		for (prev_gcpp = &segkmem_gc_list; (gcp = *prev_gcpp) != NULL;
882 		    prev_gcpp = &gcp->gc_next) {
883 			if (gcp->gc_arena == vmp && gcp->gc_size == size) {
884 				*prev_gcpp = gcp->gc_next;
885 				return (gcp);
886 			}
887 		}
888 
889 		addr = vmem_alloc(vmp, size, vmflag | VM_PANIC);
890 		if (boot_alloc(addr, size, BO_NO_ALIGN) != addr)
891 			panic("segkmem_alloc: boot_alloc failed");
892 		return (addr);
893 	}
894 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
895 	    segkmem_page_create, NULL));
896 }
897 
898 /*
899  * Any changes to this routine must also be carried over to
900  * devmap_free_pages() in the seg_dev driver. This is because
901  * we currently don't have a special kernel segment for non-paged
902  * kernel memory that is exported by drivers to user space.
903  */
904 void
905 segkmem_free(vmem_t *vmp, void *inaddr, size_t size)
906 {
907 	page_t *pp;
908 	caddr_t addr = inaddr;
909 	caddr_t eaddr;
910 	pgcnt_t npages = btopr(size);
911 
912 	ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0);
913 
914 	if (kvseg.s_base == NULL) {
915 		segkmem_gc_list_t *gc = inaddr;
916 		gc->gc_arena = vmp;
917 		gc->gc_size = size;
918 		gc->gc_next = segkmem_gc_list;
919 		segkmem_gc_list = gc;
920 		return;
921 	}
922 
923 	hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK);
924 
925 	for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) {
926 #if defined(__x86)
927 		pp = page_find(&kvp, (u_offset_t)(uintptr_t)addr);
928 		if (pp == NULL)
929 			panic("segkmem_free: page not found");
930 		if (!page_tryupgrade(pp)) {
931 			/*
932 			 * Some other thread has a sharelock. Wait for
933 			 * it to drop the lock so we can free this page.
934 			 */
935 			page_unlock(pp);
936 			pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr,
937 			    SE_EXCL);
938 		}
939 #else
940 		pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, SE_EXCL);
941 #endif
942 		if (pp == NULL)
943 			panic("segkmem_free: page not found");
944 		/* Clear p_lckcnt so page_destroy() doesn't update availrmem */
945 		pp->p_lckcnt = 0;
946 		page_destroy(pp, 0);
947 	}
948 	page_unresv(npages);
949 
950 	if (vmp != NULL)
951 		vmem_free(vmp, inaddr, size);
952 }
953 
954 void
955 segkmem_gc(void)
956 {
957 	ASSERT(kvseg.s_base != NULL);
958 	while (segkmem_gc_list != NULL) {
959 		segkmem_gc_list_t *gc = segkmem_gc_list;
960 		segkmem_gc_list = gc->gc_next;
961 		segkmem_free(gc->gc_arena, gc, gc->gc_size);
962 	}
963 }
964 
965 /*
966  * Legacy entry points from here to end of file.
967  */
968 void
969 segkmem_mapin(struct seg *seg, void *addr, size_t size, uint_t vprot,
970     pfn_t pfn, uint_t flags)
971 {
972 	hat_unload(seg->s_as->a_hat, addr, size, HAT_UNLOAD_UNLOCK);
973 	hat_devload(seg->s_as->a_hat, addr, size, pfn, vprot,
974 	    flags | HAT_LOAD_LOCK);
975 }
976 
977 void
978 segkmem_mapout(struct seg *seg, void *addr, size_t size)
979 {
980 	hat_unload(seg->s_as->a_hat, addr, size, HAT_UNLOAD_UNLOCK);
981 }
982 
983 void *
984 kmem_getpages(pgcnt_t npages, int kmflag)
985 {
986 	return (kmem_alloc(ptob(npages), kmflag));
987 }
988 
989 void
990 kmem_freepages(void *addr, pgcnt_t npages)
991 {
992 	kmem_free(addr, ptob(npages));
993 }
994 
995 /*
996  * segkmem_page_create_large() allocates a large page to be used for the kmem
997  * caches. If kpr is enabled we ask for a relocatable page unless requested
998  * otherwise. If kpr is disabled we have to ask for a non-reloc page
999  */
1000 static page_t *
1001 segkmem_page_create_large(void *addr, size_t size, int vmflag, void *arg)
1002 {
1003 	int pgflags;
1004 
1005 	pgflags = PG_EXCL;
1006 
1007 	if (segkmem_reloc == 0 || (vmflag & VM_NORELOC))
1008 		pgflags |= PG_NORELOC;
1009 	if (!(vmflag & VM_NOSLEEP))
1010 		pgflags |= PG_WAIT;
1011 	if (vmflag & VM_PUSHPAGE)
1012 		pgflags |= PG_PUSHPAGE;
1013 
1014 	return (page_create_va_large(&kvp, (u_offset_t)(uintptr_t)addr, size,
1015 	    pgflags, &kvseg, addr, arg));
1016 }
1017 
1018 /*
1019  * Allocate a large page to back the virtual address range
1020  * [addr, addr + size).  If addr is NULL, allocate the virtual address
1021  * space as well.
1022  */
1023 static void *
1024 segkmem_xalloc_lp(vmem_t *vmp, void *inaddr, size_t size, int vmflag,
1025     uint_t attr, page_t *(*page_create_func)(void *, size_t, int, void *),
1026     void *pcarg)
1027 {
1028 	caddr_t addr = inaddr, pa;
1029 	size_t  lpsize = segkmem_lpsize;
1030 	pgcnt_t npages = btopr(size);
1031 	pgcnt_t nbpages = btop(lpsize);
1032 	pgcnt_t nlpages = size >> segkmem_lpshift;
1033 	size_t  ppasize = nbpages * sizeof (page_t *);
1034 	page_t *pp, *rootpp, **ppa, *pplist = NULL;
1035 	int i;
1036 
1037 	if (page_resv(npages, vmflag & VM_KMFLAGS) == 0) {
1038 		return (NULL);
1039 	}
1040 
1041 	/*
1042 	 * allocate an array we need for hat_memload_array.
1043 	 * we use a separate arena to avoid recursion.
1044 	 * we will not need this array when hat_memload_array learns pp++
1045 	 */
1046 	if ((ppa = vmem_alloc(segkmem_ppa_arena, ppasize, vmflag)) == NULL) {
1047 		goto fail_array_alloc;
1048 	}
1049 
1050 	if (inaddr == NULL && (addr = vmem_alloc(vmp, size, vmflag)) == NULL)
1051 		goto fail_vmem_alloc;
1052 
1053 	ASSERT(((uintptr_t)addr & (lpsize - 1)) == 0);
1054 
1055 	/* create all the pages */
1056 	for (pa = addr, i = 0; i < nlpages; i++, pa += lpsize) {
1057 		if ((pp = page_create_func(pa, lpsize, vmflag, pcarg)) == NULL)
1058 			goto fail_page_create;
1059 		page_list_concat(&pplist, &pp);
1060 	}
1061 
1062 	/* at this point we have all the resource to complete the request */
1063 	while ((rootpp = pplist) != NULL) {
1064 		for (i = 0; i < nbpages; i++) {
1065 			ASSERT(pplist != NULL);
1066 			pp = pplist;
1067 			page_sub(&pplist, pp);
1068 			ASSERT(page_iolock_assert(pp));
1069 			page_io_unlock(pp);
1070 			ppa[i] = pp;
1071 		}
1072 		/*
1073 		 * Load the locked entry. It's OK to preload the entry into the
1074 		 * TSB since we now support large mappings in the kernel TSB.
1075 		 */
1076 		hat_memload_array(kas.a_hat,
1077 		    (caddr_t)(uintptr_t)rootpp->p_offset, lpsize,
1078 		    ppa, (PROT_ALL & ~PROT_USER) | HAT_NOSYNC | attr,
1079 		    HAT_LOAD_LOCK);
1080 
1081 		for (--i; i >= 0; --i) {
1082 			ppa[i]->p_lckcnt = 1;
1083 			page_unlock(ppa[i]);
1084 		}
1085 	}
1086 
1087 	vmem_free(segkmem_ppa_arena, ppa, ppasize);
1088 	return (addr);
1089 
1090 fail_page_create:
1091 	while ((rootpp = pplist) != NULL) {
1092 		for (i = 0, pp = pplist; i < nbpages; i++, pp = pplist) {
1093 			ASSERT(pp != NULL);
1094 			page_sub(&pplist, pp);
1095 			ASSERT(page_iolock_assert(pp));
1096 			page_io_unlock(pp);
1097 		}
1098 		page_destroy_pages(rootpp);
1099 	}
1100 
1101 	if (inaddr == NULL)
1102 		vmem_free(vmp, addr, size);
1103 
1104 fail_vmem_alloc:
1105 	vmem_free(segkmem_ppa_arena, ppa, ppasize);
1106 
1107 fail_array_alloc:
1108 	page_unresv(npages);
1109 
1110 	return (NULL);
1111 }
1112 
1113 static void
1114 segkmem_free_one_lp(caddr_t addr, size_t size)
1115 {
1116 	page_t		*pp, *rootpp = NULL;
1117 	pgcnt_t 	pgs_left = btopr(size);
1118 
1119 	ASSERT(size == segkmem_lpsize);
1120 
1121 	hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK);
1122 
1123 	for (; pgs_left > 0; addr += PAGESIZE, pgs_left--) {
1124 		pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, SE_EXCL);
1125 		if (pp == NULL)
1126 			panic("segkmem_free_one_lp: page not found");
1127 		ASSERT(PAGE_EXCL(pp));
1128 		pp->p_lckcnt = 0;
1129 		if (rootpp == NULL)
1130 			rootpp = pp;
1131 	}
1132 	ASSERT(rootpp != NULL);
1133 	page_destroy_pages(rootpp);
1134 
1135 	/* page_unresv() is done by the caller */
1136 }
1137 
1138 /*
1139  * This function is called to import new spans into the vmem arenas like
1140  * kmem_default_arena and kmem_oversize_arena. It first tries to import
1141  * spans from large page arena - kmem_lp_arena. In order to do this it might
1142  * have to "upgrade the requested size" to kmem_lp_arena quantum. If
1143  * it was not able to satisfy the upgraded request it then calls regular
1144  * segkmem_alloc() that satisfies the request by importing from "*vmp" arena
1145  */
1146 void *
1147 segkmem_alloc_lp(vmem_t *vmp, size_t *sizep, int vmflag)
1148 {
1149 	size_t size;
1150 	kthread_t *t = curthread;
1151 	segkmem_lpcb_t *lpcb = &segkmem_lpcb;
1152 
1153 	ASSERT(sizep != NULL);
1154 
1155 	size = *sizep;
1156 
1157 	if (lpcb->lp_uselp && !(t->t_flag & T_PANIC) &&
1158 	    !(vmflag & SEGKMEM_SHARELOCKED)) {
1159 
1160 		size_t kmemlp_qnt = segkmem_kmemlp_quantum;
1161 		size_t asize = P2ROUNDUP(size, kmemlp_qnt);
1162 		void  *addr = NULL;
1163 		ulong_t *lpthrtp = &lpcb->lp_throttle;
1164 		ulong_t lpthrt = *lpthrtp;
1165 		int	dowakeup = 0;
1166 		int	doalloc = 1;
1167 
1168 		ASSERT(kmem_lp_arena != NULL);
1169 		ASSERT(asize >= size);
1170 
1171 		if (lpthrt != 0) {
1172 			/* try to update the throttle value */
1173 			lpthrt = atomic_add_long_nv(lpthrtp, 1);
1174 			if (lpthrt >= segkmem_lpthrottle_max) {
1175 				lpthrt = atomic_cas_ulong(lpthrtp, lpthrt,
1176 				    segkmem_lpthrottle_max / 4);
1177 			}
1178 
1179 			/*
1180 			 * when we get above throttle start do an exponential
1181 			 * backoff at trying large pages and reaping
1182 			 */
1183 			if (lpthrt > segkmem_lpthrottle_start &&
1184 			    (lpthrt & (lpthrt - 1))) {
1185 				lpcb->allocs_throttled++;
1186 				lpthrt--;
1187 				if ((lpthrt & (lpthrt - 1)) == 0)
1188 					kmem_reap();
1189 				return (segkmem_alloc(vmp, size, vmflag));
1190 			}
1191 		}
1192 
1193 		if (!(vmflag & VM_NOSLEEP) &&
1194 		    segkmem_heaplp_quantum >= (8 * kmemlp_qnt) &&
1195 		    vmem_size(kmem_lp_arena, VMEM_FREE) <= kmemlp_qnt &&
1196 		    asize < (segkmem_heaplp_quantum - kmemlp_qnt)) {
1197 
1198 			/*
1199 			 * we are low on free memory in kmem_lp_arena
1200 			 * we let only one guy to allocate heap_lp
1201 			 * quantum size chunk that everybody is going to
1202 			 * share
1203 			 */
1204 			mutex_enter(&lpcb->lp_lock);
1205 
1206 			if (lpcb->lp_wait) {
1207 
1208 				/* we are not the first one - wait */
1209 				cv_wait(&lpcb->lp_cv, &lpcb->lp_lock);
1210 				if (vmem_size(kmem_lp_arena, VMEM_FREE) <
1211 				    kmemlp_qnt)  {
1212 					doalloc = 0;
1213 				}
1214 			} else if (vmem_size(kmem_lp_arena, VMEM_FREE) <=
1215 			    kmemlp_qnt) {
1216 
1217 				/*
1218 				 * we are the first one, make sure we import
1219 				 * a large page
1220 				 */
1221 				if (asize == kmemlp_qnt)
1222 					asize += kmemlp_qnt;
1223 				dowakeup = 1;
1224 				lpcb->lp_wait = 1;
1225 			}
1226 
1227 			mutex_exit(&lpcb->lp_lock);
1228 		}
1229 
1230 		/*
1231 		 * VM_ABORT flag prevents sleeps in vmem_xalloc when
1232 		 * large pages are not available. In that case this allocation
1233 		 * attempt will fail and we will retry allocation with small
1234 		 * pages. We also do not want to panic if this allocation fails
1235 		 * because we are going to retry.
1236 		 */
1237 		if (doalloc) {
1238 			addr = vmem_alloc(kmem_lp_arena, asize,
1239 			    (vmflag | VM_ABORT) & ~VM_PANIC);
1240 
1241 			if (dowakeup) {
1242 				mutex_enter(&lpcb->lp_lock);
1243 				ASSERT(lpcb->lp_wait != 0);
1244 				lpcb->lp_wait = 0;
1245 				cv_broadcast(&lpcb->lp_cv);
1246 				mutex_exit(&lpcb->lp_lock);
1247 			}
1248 		}
1249 
1250 		if (addr != NULL) {
1251 			*sizep = asize;
1252 			*lpthrtp = 0;
1253 			return (addr);
1254 		}
1255 
1256 		if (vmflag & VM_NOSLEEP)
1257 			lpcb->nosleep_allocs_failed++;
1258 		else
1259 			lpcb->sleep_allocs_failed++;
1260 		lpcb->alloc_bytes_failed += size;
1261 
1262 		/* if large page throttling is not started yet do it */
1263 		if (segkmem_use_lpthrottle && lpthrt == 0) {
1264 			lpthrt = atomic_cas_ulong(lpthrtp, lpthrt, 1);
1265 		}
1266 	}
1267 	return (segkmem_alloc(vmp, size, vmflag));
1268 }
1269 
1270 void
1271 segkmem_free_lp(vmem_t *vmp, void *inaddr, size_t size)
1272 {
1273 	if (kmem_lp_arena == NULL || !IS_KMEM_VA_LARGEPAGE((caddr_t)inaddr)) {
1274 		segkmem_free(vmp, inaddr, size);
1275 	} else {
1276 		vmem_free(kmem_lp_arena, inaddr, size);
1277 	}
1278 }
1279 
1280 /*
1281  * segkmem_alloc_lpi() imports virtual memory from large page heap arena
1282  * into kmem_lp arena. In the process it maps the imported segment with
1283  * large pages
1284  */
1285 static void *
1286 segkmem_alloc_lpi(vmem_t *vmp, size_t size, int vmflag)
1287 {
1288 	segkmem_lpcb_t *lpcb = &segkmem_lpcb;
1289 	void  *addr;
1290 
1291 	ASSERT(size != 0);
1292 	ASSERT(vmp == heap_lp_arena);
1293 
1294 	/* do not allow large page heap grow beyound limits */
1295 	if (vmem_size(vmp, VMEM_ALLOC) >= segkmem_kmemlp_max) {
1296 		lpcb->allocs_limited++;
1297 		return (NULL);
1298 	}
1299 
1300 	addr = segkmem_xalloc_lp(vmp, NULL, size, vmflag, 0,
1301 	    segkmem_page_create_large, NULL);
1302 	return (addr);
1303 }
1304 
1305 /*
1306  * segkmem_free_lpi() returns virtual memory back into large page heap arena
1307  * from kmem_lp arena. Beore doing this it unmaps the segment and frees
1308  * large pages used to map it.
1309  */
1310 static void
1311 segkmem_free_lpi(vmem_t *vmp, void *inaddr, size_t size)
1312 {
1313 	pgcnt_t		nlpages = size >> segkmem_lpshift;
1314 	size_t		lpsize = segkmem_lpsize;
1315 	caddr_t		addr = inaddr;
1316 	pgcnt_t 	npages = btopr(size);
1317 	int		i;
1318 
1319 	ASSERT(vmp == heap_lp_arena);
1320 	ASSERT(IS_KMEM_VA_LARGEPAGE(addr));
1321 	ASSERT(((uintptr_t)inaddr & (lpsize - 1)) == 0);
1322 
1323 	for (i = 0; i < nlpages; i++) {
1324 		segkmem_free_one_lp(addr, lpsize);
1325 		addr += lpsize;
1326 	}
1327 
1328 	page_unresv(npages);
1329 
1330 	vmem_free(vmp, inaddr, size);
1331 }
1332 
1333 /*
1334  * This function is called at system boot time by kmem_init right after
1335  * /etc/system file has been read. It checks based on hardware configuration
1336  * and /etc/system settings if system is going to use large pages. The
1337  * initialiazation necessary to actually start using large pages
1338  * happens later in the process after segkmem_heap_lp_init() is called.
1339  */
1340 int
1341 segkmem_lpsetup()
1342 {
1343 	int use_large_pages = 0;
1344 
1345 #ifdef __sparc
1346 
1347 	size_t memtotal = physmem * PAGESIZE;
1348 
1349 	if (heap_lp_base == NULL) {
1350 		segkmem_lpsize = PAGESIZE;
1351 		return (0);
1352 	}
1353 
1354 	/* get a platform dependent value of large page size for kernel heap */
1355 	segkmem_lpsize = get_segkmem_lpsize(segkmem_lpsize);
1356 
1357 	if (segkmem_lpsize <= PAGESIZE) {
1358 		/*
1359 		 * put virtual space reserved for the large page kernel
1360 		 * back to the regular heap
1361 		 */
1362 		vmem_xfree(heap_arena, heap_lp_base,
1363 		    heap_lp_end - heap_lp_base);
1364 		heap_lp_base = NULL;
1365 		heap_lp_end = NULL;
1366 		segkmem_lpsize = PAGESIZE;
1367 		return (0);
1368 	}
1369 
1370 	/* set heap_lp quantum if necessary */
1371 	if (segkmem_heaplp_quantum == 0 ||
1372 	    (segkmem_heaplp_quantum & (segkmem_heaplp_quantum - 1)) ||
1373 	    P2PHASE(segkmem_heaplp_quantum, segkmem_lpsize)) {
1374 		segkmem_heaplp_quantum = segkmem_lpsize;
1375 	}
1376 
1377 	/* set kmem_lp quantum if necessary */
1378 	if (segkmem_kmemlp_quantum == 0 ||
1379 	    (segkmem_kmemlp_quantum & (segkmem_kmemlp_quantum - 1)) ||
1380 	    segkmem_kmemlp_quantum > segkmem_heaplp_quantum) {
1381 		segkmem_kmemlp_quantum = segkmem_heaplp_quantum;
1382 	}
1383 
1384 	/* set total amount of memory allowed for large page kernel heap */
1385 	if (segkmem_kmemlp_max == 0) {
1386 		if (segkmem_kmemlp_pcnt == 0 || segkmem_kmemlp_pcnt > 100)
1387 			segkmem_kmemlp_pcnt = 12;
1388 		segkmem_kmemlp_max = (memtotal * segkmem_kmemlp_pcnt) / 100;
1389 	}
1390 	segkmem_kmemlp_max = P2ROUNDUP(segkmem_kmemlp_max,
1391 	    segkmem_heaplp_quantum);
1392 
1393 	/* fix lp kmem preallocation request if necesssary */
1394 	if (segkmem_kmemlp_min) {
1395 		segkmem_kmemlp_min = P2ROUNDUP(segkmem_kmemlp_min,
1396 		    segkmem_heaplp_quantum);
1397 		if (segkmem_kmemlp_min > segkmem_kmemlp_max)
1398 			segkmem_kmemlp_min = segkmem_kmemlp_max;
1399 	}
1400 
1401 	use_large_pages = 1;
1402 	segkmem_lpshift = page_get_shift(page_szc(segkmem_lpsize));
1403 
1404 #endif
1405 	return (use_large_pages);
1406 }
1407 
1408 #ifdef __sparc
1409 
1410 
1411 static void *
1412 segkmem_alloc_ppa(vmem_t *vmp, size_t size, int vmflag)
1413 {
1414 	size_t ppaquantum = btopr(segkmem_lpsize) * sizeof (page_t *);
1415 	void   *addr;
1416 
1417 	if (ppaquantum <= PAGESIZE)
1418 		return (segkmem_alloc(vmp, size, vmflag));
1419 
1420 	ASSERT((size & (ppaquantum - 1)) == 0);
1421 
1422 	addr = vmem_xalloc(vmp, size, ppaquantum, 0, 0, NULL, NULL, vmflag);
1423 	if (addr != NULL && segkmem_xalloc(vmp, addr, size, vmflag, 0,
1424 		segkmem_page_create, NULL) == NULL) {
1425 		vmem_xfree(vmp, addr, size);
1426 		addr = NULL;
1427 	}
1428 
1429 	return (addr);
1430 }
1431 
1432 static void
1433 segkmem_free_ppa(vmem_t *vmp, void *addr, size_t size)
1434 {
1435 	size_t ppaquantum = btopr(segkmem_lpsize) * sizeof (page_t *);
1436 
1437 	ASSERT(addr != NULL);
1438 
1439 	if (ppaquantum <= PAGESIZE) {
1440 		segkmem_free(vmp, addr, size);
1441 	} else {
1442 		segkmem_free(NULL, addr, size);
1443 		vmem_xfree(vmp, addr, size);
1444 	}
1445 }
1446 
1447 void
1448 segkmem_heap_lp_init()
1449 {
1450 	segkmem_lpcb_t *lpcb = &segkmem_lpcb;
1451 	size_t heap_lp_size = heap_lp_end - heap_lp_base;
1452 	size_t lpsize = segkmem_lpsize;
1453 	size_t ppaquantum;
1454 	void   *addr;
1455 
1456 	if (segkmem_lpsize <= PAGESIZE) {
1457 		ASSERT(heap_lp_base == NULL);
1458 		ASSERT(heap_lp_end == NULL);
1459 		return;
1460 	}
1461 
1462 	ASSERT(segkmem_heaplp_quantum >= lpsize);
1463 	ASSERT((segkmem_heaplp_quantum & (lpsize - 1)) == 0);
1464 	ASSERT(lpcb->lp_uselp == 0);
1465 	ASSERT(heap_lp_base != NULL);
1466 	ASSERT(heap_lp_end != NULL);
1467 	ASSERT(heap_lp_base < heap_lp_end);
1468 	ASSERT(heap_lp_arena == NULL);
1469 	ASSERT(((uintptr_t)heap_lp_base & (lpsize - 1)) == 0);
1470 	ASSERT(((uintptr_t)heap_lp_end & (lpsize - 1)) == 0);
1471 
1472 	/* create large page heap arena */
1473 	heap_lp_arena = vmem_create("heap_lp", heap_lp_base, heap_lp_size,
1474 	    segkmem_heaplp_quantum, NULL, NULL, NULL, 0, VM_SLEEP);
1475 
1476 	ASSERT(heap_lp_arena != NULL);
1477 
1478 	/* This arena caches memory already mapped by large pages */
1479 	kmem_lp_arena = vmem_create("kmem_lp", NULL, 0, segkmem_kmemlp_quantum,
1480 	    segkmem_alloc_lpi, segkmem_free_lpi, heap_lp_arena, 0, VM_SLEEP);
1481 
1482 	ASSERT(kmem_lp_arena != NULL);
1483 
1484 	mutex_init(&lpcb->lp_lock, NULL, MUTEX_DEFAULT, NULL);
1485 	cv_init(&lpcb->lp_cv, NULL, CV_DEFAULT, NULL);
1486 
1487 	/*
1488 	 * this arena is used for the array of page_t pointers necessary
1489 	 * to call hat_mem_load_array
1490 	 */
1491 	ppaquantum = btopr(lpsize) * sizeof (page_t *);
1492 	segkmem_ppa_arena = vmem_create("segkmem_ppa", NULL, 0, ppaquantum,
1493 	    segkmem_alloc_ppa, segkmem_free_ppa, heap_arena, ppaquantum,
1494 	    VM_SLEEP);
1495 
1496 	ASSERT(segkmem_ppa_arena != NULL);
1497 
1498 	/* prealloacate some memory for the lp kernel heap */
1499 	if (segkmem_kmemlp_min) {
1500 
1501 		ASSERT(P2PHASE(segkmem_kmemlp_min,
1502 		    segkmem_heaplp_quantum) == 0);
1503 
1504 		if ((addr = segkmem_alloc_lpi(heap_lp_arena,
1505 		    segkmem_kmemlp_min, VM_SLEEP)) != NULL) {
1506 
1507 			addr = vmem_add(kmem_lp_arena, addr,
1508 			    segkmem_kmemlp_min, VM_SLEEP);
1509 			ASSERT(addr != NULL);
1510 		}
1511 	}
1512 
1513 	lpcb->lp_uselp = 1;
1514 }
1515 
1516 #endif
1517