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