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