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