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