xref: /illumos-gate/usr/src/uts/common/vm/seg_kmem.c (revision f48205be61a214698b763ff550ab9e657525104c)
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 /*ARGSUSED*/
1248 void *
1249 segkmem_alloc_lp(vmem_t *vmp, size_t *sizep, size_t align, int vmflag)
1250 {
1251 	size_t size;
1252 	kthread_t *t = curthread;
1253 	segkmem_lpcb_t *lpcb = &segkmem_lpcb;
1254 
1255 	ASSERT(sizep != NULL);
1256 
1257 	size = *sizep;
1258 
1259 	if (lpcb->lp_uselp && !(t->t_flag & T_PANIC) &&
1260 	    !(vmflag & SEGKMEM_SHARELOCKED)) {
1261 
1262 		size_t kmemlp_qnt = segkmem_kmemlp_quantum;
1263 		size_t asize = P2ROUNDUP(size, kmemlp_qnt);
1264 		void  *addr = NULL;
1265 		ulong_t *lpthrtp = &lpcb->lp_throttle;
1266 		ulong_t lpthrt = *lpthrtp;
1267 		int	dowakeup = 0;
1268 		int	doalloc = 1;
1269 
1270 		ASSERT(kmem_lp_arena != NULL);
1271 		ASSERT(asize >= size);
1272 
1273 		if (lpthrt != 0) {
1274 			/* try to update the throttle value */
1275 			lpthrt = atomic_add_long_nv(lpthrtp, 1);
1276 			if (lpthrt >= segkmem_lpthrottle_max) {
1277 				lpthrt = atomic_cas_ulong(lpthrtp, lpthrt,
1278 				    segkmem_lpthrottle_max / 4);
1279 			}
1280 
1281 			/*
1282 			 * when we get above throttle start do an exponential
1283 			 * backoff at trying large pages and reaping
1284 			 */
1285 			if (lpthrt > segkmem_lpthrottle_start &&
1286 			    (lpthrt & (lpthrt - 1))) {
1287 				lpcb->allocs_throttled++;
1288 				lpthrt--;
1289 				if ((lpthrt & (lpthrt - 1)) == 0)
1290 					kmem_reap();
1291 				return (segkmem_alloc(vmp, size, vmflag));
1292 			}
1293 		}
1294 
1295 		if (!(vmflag & VM_NOSLEEP) &&
1296 		    segkmem_heaplp_quantum >= (8 * kmemlp_qnt) &&
1297 		    vmem_size(kmem_lp_arena, VMEM_FREE) <= kmemlp_qnt &&
1298 		    asize < (segkmem_heaplp_quantum - kmemlp_qnt)) {
1299 
1300 			/*
1301 			 * we are low on free memory in kmem_lp_arena
1302 			 * we let only one guy to allocate heap_lp
1303 			 * quantum size chunk that everybody is going to
1304 			 * share
1305 			 */
1306 			mutex_enter(&lpcb->lp_lock);
1307 
1308 			if (lpcb->lp_wait) {
1309 
1310 				/* we are not the first one - wait */
1311 				cv_wait(&lpcb->lp_cv, &lpcb->lp_lock);
1312 				if (vmem_size(kmem_lp_arena, VMEM_FREE) <
1313 				    kmemlp_qnt)  {
1314 					doalloc = 0;
1315 				}
1316 			} else if (vmem_size(kmem_lp_arena, VMEM_FREE) <=
1317 			    kmemlp_qnt) {
1318 
1319 				/*
1320 				 * we are the first one, make sure we import
1321 				 * a large page
1322 				 */
1323 				if (asize == kmemlp_qnt)
1324 					asize += kmemlp_qnt;
1325 				dowakeup = 1;
1326 				lpcb->lp_wait = 1;
1327 			}
1328 
1329 			mutex_exit(&lpcb->lp_lock);
1330 		}
1331 
1332 		/*
1333 		 * VM_ABORT flag prevents sleeps in vmem_xalloc when
1334 		 * large pages are not available. In that case this allocation
1335 		 * attempt will fail and we will retry allocation with small
1336 		 * pages. We also do not want to panic if this allocation fails
1337 		 * because we are going to retry.
1338 		 */
1339 		if (doalloc) {
1340 			addr = vmem_alloc(kmem_lp_arena, asize,
1341 			    (vmflag | VM_ABORT) & ~VM_PANIC);
1342 
1343 			if (dowakeup) {
1344 				mutex_enter(&lpcb->lp_lock);
1345 				ASSERT(lpcb->lp_wait != 0);
1346 				lpcb->lp_wait = 0;
1347 				cv_broadcast(&lpcb->lp_cv);
1348 				mutex_exit(&lpcb->lp_lock);
1349 			}
1350 		}
1351 
1352 		if (addr != NULL) {
1353 			*sizep = asize;
1354 			*lpthrtp = 0;
1355 			return (addr);
1356 		}
1357 
1358 		if (vmflag & VM_NOSLEEP)
1359 			lpcb->nosleep_allocs_failed++;
1360 		else
1361 			lpcb->sleep_allocs_failed++;
1362 		lpcb->alloc_bytes_failed += size;
1363 
1364 		/* if large page throttling is not started yet do it */
1365 		if (segkmem_use_lpthrottle && lpthrt == 0) {
1366 			lpthrt = atomic_cas_ulong(lpthrtp, lpthrt, 1);
1367 		}
1368 	}
1369 	return (segkmem_alloc(vmp, size, vmflag));
1370 }
1371 
1372 void
1373 segkmem_free_lp(vmem_t *vmp, void *inaddr, size_t size)
1374 {
1375 	if (kmem_lp_arena == NULL || !IS_KMEM_VA_LARGEPAGE((caddr_t)inaddr)) {
1376 		segkmem_free(vmp, inaddr, size);
1377 	} else {
1378 		vmem_free(kmem_lp_arena, inaddr, size);
1379 	}
1380 }
1381 
1382 /*
1383  * segkmem_alloc_lpi() imports virtual memory from large page heap arena
1384  * into kmem_lp arena. In the process it maps the imported segment with
1385  * large pages
1386  */
1387 static void *
1388 segkmem_alloc_lpi(vmem_t *vmp, size_t size, int vmflag)
1389 {
1390 	segkmem_lpcb_t *lpcb = &segkmem_lpcb;
1391 	void  *addr;
1392 
1393 	ASSERT(size != 0);
1394 	ASSERT(vmp == heap_lp_arena);
1395 
1396 	/* do not allow large page heap grow beyound limits */
1397 	if (vmem_size(vmp, VMEM_ALLOC) >= segkmem_kmemlp_max) {
1398 		lpcb->allocs_limited++;
1399 		return (NULL);
1400 	}
1401 
1402 	addr = segkmem_xalloc_lp(vmp, NULL, size, vmflag, 0,
1403 	    segkmem_page_create_large, NULL);
1404 	return (addr);
1405 }
1406 
1407 /*
1408  * segkmem_free_lpi() returns virtual memory back into large page heap arena
1409  * from kmem_lp arena. Beore doing this it unmaps the segment and frees
1410  * large pages used to map it.
1411  */
1412 static void
1413 segkmem_free_lpi(vmem_t *vmp, void *inaddr, size_t size)
1414 {
1415 	pgcnt_t		nlpages = size >> segkmem_lpshift;
1416 	size_t		lpsize = segkmem_lpsize;
1417 	caddr_t		addr = inaddr;
1418 	pgcnt_t 	npages = btopr(size);
1419 	int		i;
1420 
1421 	ASSERT(vmp == heap_lp_arena);
1422 	ASSERT(IS_KMEM_VA_LARGEPAGE(addr));
1423 	ASSERT(((uintptr_t)inaddr & (lpsize - 1)) == 0);
1424 
1425 	for (i = 0; i < nlpages; i++) {
1426 		segkmem_free_one_lp(addr, lpsize);
1427 		addr += lpsize;
1428 	}
1429 
1430 	page_unresv(npages);
1431 
1432 	vmem_free(vmp, inaddr, size);
1433 }
1434 
1435 /*
1436  * This function is called at system boot time by kmem_init right after
1437  * /etc/system file has been read. It checks based on hardware configuration
1438  * and /etc/system settings if system is going to use large pages. The
1439  * initialiazation necessary to actually start using large pages
1440  * happens later in the process after segkmem_heap_lp_init() is called.
1441  */
1442 int
1443 segkmem_lpsetup()
1444 {
1445 	int use_large_pages = 0;
1446 
1447 #ifdef __sparc
1448 
1449 	size_t memtotal = physmem * PAGESIZE;
1450 
1451 	if (heap_lp_base == NULL) {
1452 		segkmem_lpsize = PAGESIZE;
1453 		return (0);
1454 	}
1455 
1456 	/* get a platform dependent value of large page size for kernel heap */
1457 	segkmem_lpsize = get_segkmem_lpsize(segkmem_lpsize);
1458 
1459 	if (segkmem_lpsize <= PAGESIZE) {
1460 		/*
1461 		 * put virtual space reserved for the large page kernel
1462 		 * back to the regular heap
1463 		 */
1464 		vmem_xfree(heap_arena, heap_lp_base,
1465 		    heap_lp_end - heap_lp_base);
1466 		heap_lp_base = NULL;
1467 		heap_lp_end = NULL;
1468 		segkmem_lpsize = PAGESIZE;
1469 		return (0);
1470 	}
1471 
1472 	/* set heap_lp quantum if necessary */
1473 	if (segkmem_heaplp_quantum == 0 ||
1474 	    (segkmem_heaplp_quantum & (segkmem_heaplp_quantum - 1)) ||
1475 	    P2PHASE(segkmem_heaplp_quantum, segkmem_lpsize)) {
1476 		segkmem_heaplp_quantum = segkmem_lpsize;
1477 	}
1478 
1479 	/* set kmem_lp quantum if necessary */
1480 	if (segkmem_kmemlp_quantum == 0 ||
1481 	    (segkmem_kmemlp_quantum & (segkmem_kmemlp_quantum - 1)) ||
1482 	    segkmem_kmemlp_quantum > segkmem_heaplp_quantum) {
1483 		segkmem_kmemlp_quantum = segkmem_heaplp_quantum;
1484 	}
1485 
1486 	/* set total amount of memory allowed for large page kernel heap */
1487 	if (segkmem_kmemlp_max == 0) {
1488 		if (segkmem_kmemlp_pcnt == 0 || segkmem_kmemlp_pcnt > 100)
1489 			segkmem_kmemlp_pcnt = 12;
1490 		segkmem_kmemlp_max = (memtotal * segkmem_kmemlp_pcnt) / 100;
1491 	}
1492 	segkmem_kmemlp_max = P2ROUNDUP(segkmem_kmemlp_max,
1493 	    segkmem_heaplp_quantum);
1494 
1495 	/* fix lp kmem preallocation request if necesssary */
1496 	if (segkmem_kmemlp_min) {
1497 		segkmem_kmemlp_min = P2ROUNDUP(segkmem_kmemlp_min,
1498 		    segkmem_heaplp_quantum);
1499 		if (segkmem_kmemlp_min > segkmem_kmemlp_max)
1500 			segkmem_kmemlp_min = segkmem_kmemlp_max;
1501 	}
1502 
1503 	use_large_pages = 1;
1504 	segkmem_lpszc = page_szc(segkmem_lpsize);
1505 	segkmem_lpshift = page_get_shift(segkmem_lpszc);
1506 
1507 #endif
1508 	return (use_large_pages);
1509 }
1510 
1511 void
1512 segkmem_zio_init(void *zio_mem_base, size_t zio_mem_size)
1513 {
1514 	ASSERT(zio_mem_base != NULL);
1515 	ASSERT(zio_mem_size != 0);
1516 
1517 	zio_arena = vmem_create("zio", zio_mem_base, zio_mem_size, PAGESIZE,
1518 	    NULL, NULL, NULL, 0, VM_SLEEP);
1519 
1520 	zio_alloc_arena = vmem_create("zio_buf", NULL, 0, PAGESIZE,
1521 	    segkmem_zio_alloc, segkmem_zio_free, zio_arena, 0, VM_SLEEP);
1522 
1523 	ASSERT(zio_arena != NULL);
1524 	ASSERT(zio_alloc_arena != NULL);
1525 }
1526 
1527 #ifdef __sparc
1528 
1529 
1530 static void *
1531 segkmem_alloc_ppa(vmem_t *vmp, size_t size, int vmflag)
1532 {
1533 	size_t ppaquantum = btopr(segkmem_lpsize) * sizeof (page_t *);
1534 	void   *addr;
1535 
1536 	if (ppaquantum <= PAGESIZE)
1537 		return (segkmem_alloc(vmp, size, vmflag));
1538 
1539 	ASSERT((size & (ppaquantum - 1)) == 0);
1540 
1541 	addr = vmem_xalloc(vmp, size, ppaquantum, 0, 0, NULL, NULL, vmflag);
1542 	if (addr != NULL && segkmem_xalloc(vmp, addr, size, vmflag, 0,
1543 		segkmem_page_create, NULL) == NULL) {
1544 		vmem_xfree(vmp, addr, size);
1545 		addr = NULL;
1546 	}
1547 
1548 	return (addr);
1549 }
1550 
1551 static void
1552 segkmem_free_ppa(vmem_t *vmp, void *addr, size_t size)
1553 {
1554 	size_t ppaquantum = btopr(segkmem_lpsize) * sizeof (page_t *);
1555 
1556 	ASSERT(addr != NULL);
1557 
1558 	if (ppaquantum <= PAGESIZE) {
1559 		segkmem_free(vmp, addr, size);
1560 	} else {
1561 		segkmem_free(NULL, addr, size);
1562 		vmem_xfree(vmp, addr, size);
1563 	}
1564 }
1565 
1566 void
1567 segkmem_heap_lp_init()
1568 {
1569 	segkmem_lpcb_t *lpcb = &segkmem_lpcb;
1570 	size_t heap_lp_size = heap_lp_end - heap_lp_base;
1571 	size_t lpsize = segkmem_lpsize;
1572 	size_t ppaquantum;
1573 	void   *addr;
1574 
1575 	if (segkmem_lpsize <= PAGESIZE) {
1576 		ASSERT(heap_lp_base == NULL);
1577 		ASSERT(heap_lp_end == NULL);
1578 		return;
1579 	}
1580 
1581 	ASSERT(segkmem_heaplp_quantum >= lpsize);
1582 	ASSERT((segkmem_heaplp_quantum & (lpsize - 1)) == 0);
1583 	ASSERT(lpcb->lp_uselp == 0);
1584 	ASSERT(heap_lp_base != NULL);
1585 	ASSERT(heap_lp_end != NULL);
1586 	ASSERT(heap_lp_base < heap_lp_end);
1587 	ASSERT(heap_lp_arena == NULL);
1588 	ASSERT(((uintptr_t)heap_lp_base & (lpsize - 1)) == 0);
1589 	ASSERT(((uintptr_t)heap_lp_end & (lpsize - 1)) == 0);
1590 
1591 	/* create large page heap arena */
1592 	heap_lp_arena = vmem_create("heap_lp", heap_lp_base, heap_lp_size,
1593 	    segkmem_heaplp_quantum, NULL, NULL, NULL, 0, VM_SLEEP);
1594 
1595 	ASSERT(heap_lp_arena != NULL);
1596 
1597 	/* This arena caches memory already mapped by large pages */
1598 	kmem_lp_arena = vmem_create("kmem_lp", NULL, 0, segkmem_kmemlp_quantum,
1599 	    segkmem_alloc_lpi, segkmem_free_lpi, heap_lp_arena, 0, VM_SLEEP);
1600 
1601 	ASSERT(kmem_lp_arena != NULL);
1602 
1603 	mutex_init(&lpcb->lp_lock, NULL, MUTEX_DEFAULT, NULL);
1604 	cv_init(&lpcb->lp_cv, NULL, CV_DEFAULT, NULL);
1605 
1606 	/*
1607 	 * this arena is used for the array of page_t pointers necessary
1608 	 * to call hat_mem_load_array
1609 	 */
1610 	ppaquantum = btopr(lpsize) * sizeof (page_t *);
1611 	segkmem_ppa_arena = vmem_create("segkmem_ppa", NULL, 0, ppaquantum,
1612 	    segkmem_alloc_ppa, segkmem_free_ppa, heap_arena, ppaquantum,
1613 	    VM_SLEEP);
1614 
1615 	ASSERT(segkmem_ppa_arena != NULL);
1616 
1617 	/* prealloacate some memory for the lp kernel heap */
1618 	if (segkmem_kmemlp_min) {
1619 
1620 		ASSERT(P2PHASE(segkmem_kmemlp_min,
1621 		    segkmem_heaplp_quantum) == 0);
1622 
1623 		if ((addr = segkmem_alloc_lpi(heap_lp_arena,
1624 		    segkmem_kmemlp_min, VM_SLEEP)) != NULL) {
1625 
1626 			addr = vmem_add(kmem_lp_arena, addr,
1627 			    segkmem_kmemlp_min, VM_SLEEP);
1628 			ASSERT(addr != NULL);
1629 		}
1630 	}
1631 
1632 	lpcb->lp_uselp = 1;
1633 }
1634 
1635 #endif
1636