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