uts: make emu10k non-verbose
[unleashed.git] / kernel / vm / seg_kp.c
blob77c5504df286f14984e2f4ca39325892323f381c
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
26 /* All Rights Reserved */
29 * Portions of this source code were derived from Berkeley 4.3 BSD
30 * under license from the Regents of the University of California.
34 * segkp is a segment driver that administers the allocation and deallocation
35 * of pageable variable size chunks of kernel virtual address space. Each
36 * allocated resource is page-aligned.
38 * The user may specify whether the resource should be initialized to 0,
39 * include a redzone, or locked in memory.
42 #include <sys/types.h>
43 #include <sys/t_lock.h>
44 #include <sys/thread.h>
45 #include <sys/param.h>
46 #include <sys/errno.h>
47 #include <sys/sysmacros.h>
48 #include <sys/systm.h>
49 #include <sys/buf.h>
50 #include <sys/mman.h>
51 #include <sys/vnode.h>
52 #include <sys/cmn_err.h>
53 #include <sys/swap.h>
54 #include <sys/tuneable.h>
55 #include <sys/kmem.h>
56 #include <sys/vmem.h>
57 #include <sys/cred.h>
58 #include <sys/dumphdr.h>
59 #include <sys/debug.h>
60 #include <sys/vtrace.h>
61 #include <sys/stack.h>
62 #include <sys/atomic.h>
63 #include <sys/archsystm.h>
64 #include <sys/lgrp.h>
66 #include <vm/as.h>
67 #include <vm/seg.h>
68 #include <vm/seg_kp.h>
69 #include <vm/seg_kmem.h>
70 #include <vm/anon.h>
71 #include <vm/page.h>
72 #include <vm/hat.h>
73 #include <sys/bitmap.h>
76 * Private seg op routines
78 static void segkp_badop(void);
79 static void segkp_dump(struct seg *seg);
80 static int segkp_checkprot(struct seg *seg, caddr_t addr, size_t len,
81 uint_t prot);
82 static int segkp_kluster(struct seg *seg, caddr_t addr, ssize_t delta);
83 static int segkp_pagelock(struct seg *seg, caddr_t addr, size_t len,
84 struct page ***page, enum lock_type type,
85 enum seg_rw rw);
86 static void segkp_insert(struct seg *seg, struct segkp_data *kpd);
87 static void segkp_delete(struct seg *seg, struct segkp_data *kpd);
88 static caddr_t segkp_get_internal(struct seg *seg, size_t len, uint_t flags,
89 struct segkp_data **tkpd, struct anon_map *amp);
90 static void segkp_release_internal(struct seg *seg,
91 struct segkp_data *kpd, size_t len);
92 static int segkp_unlock(struct hat *hat, struct seg *seg, caddr_t vaddr,
93 size_t len, struct segkp_data *kpd, uint_t flags);
94 static int segkp_load(struct hat *hat, struct seg *seg, caddr_t vaddr,
95 size_t len, struct segkp_data *kpd, uint_t flags);
96 static struct segkp_data *segkp_find(struct seg *seg, caddr_t vaddr);
99 * Lock used to protect the hash table(s) and caches.
101 static kmutex_t segkp_lock;
104 * The segkp caches
106 static struct segkp_cache segkp_cache[SEGKP_MAX_CACHE];
108 #define SEGKP_BADOP(t) (t(*)())segkp_badop
111 * When there are fewer than red_minavail bytes left on the stack,
112 * segkp_map_red() will map in the redzone (if called). 5000 seems
113 * to work reasonably well...
115 long red_minavail = 5000;
118 * will be set to 1 for 32 bit x86 systems only, in startup.c
120 int segkp_fromheap = 0;
121 ulong_t *segkp_bitmap;
124 * If segkp_map_red() is called with the redzone already mapped and
125 * with less than RED_DEEP_THRESHOLD bytes available on the stack,
126 * then the stack situation has become quite serious; if much more stack
127 * is consumed, we have the potential of scrogging the next thread/LWP
128 * structure. To help debug the "can't happen" panics which may
129 * result from this condition, we record hrestime and the calling thread
130 * in red_deep_hires and red_deep_thread respectively.
132 #define RED_DEEP_THRESHOLD 2000
134 hrtime_t red_deep_hires;
135 kthread_t *red_deep_thread;
137 uint32_t red_nmapped;
138 uint32_t red_closest = UINT_MAX;
139 uint32_t red_ndoubles;
141 pgcnt_t anon_segkp_pages_locked; /* See vm/anon.h */
142 pgcnt_t anon_segkp_pages_resv; /* anon reserved by seg_kp */
144 static const struct seg_ops segkp_ops = {
145 .dup = SEGKP_BADOP(int),
146 .unmap = SEGKP_BADOP(int),
147 .free = SEGKP_BADOP(void),
148 .fault = segkp_fault,
149 .faulta = SEGKP_BADOP(faultcode_t),
150 .setprot = SEGKP_BADOP(int),
151 .checkprot = segkp_checkprot,
152 .kluster = segkp_kluster,
153 .sync = SEGKP_BADOP(int),
154 .incore = SEGKP_BADOP(size_t),
155 .lockop = SEGKP_BADOP(int),
156 .getprot = SEGKP_BADOP(int),
157 .getoffset = SEGKP_BADOP(uoff_t),
158 .gettype = SEGKP_BADOP(int),
159 .getvp = SEGKP_BADOP(int),
160 .advise = SEGKP_BADOP(int),
161 .dump = segkp_dump,
162 .pagelock = segkp_pagelock,
163 .setpagesize = SEGKP_BADOP(int),
167 static void
168 segkp_badop(void)
170 panic("segkp_badop");
171 /*NOTREACHED*/
174 static void segkpinit_mem_config(struct seg *);
176 static uint32_t segkp_indel;
179 * Allocate the segment specific private data struct and fill it in
180 * with the per kp segment mutex, anon ptr. array and hash table.
183 segkp_create(struct seg *seg)
185 struct segkp_segdata *kpsd;
186 size_t np;
188 ASSERT(seg != NULL && seg->s_as == &kas);
189 ASSERT(RW_WRITE_HELD(&seg->s_as->a_lock));
191 if (seg->s_size & PAGEOFFSET) {
192 panic("Bad segkp size");
193 /*NOTREACHED*/
196 kpsd = kmem_zalloc(sizeof (struct segkp_segdata), KM_SLEEP);
199 * Allocate the virtual memory for segkp and initialize it
201 if (segkp_fromheap) {
202 np = btop(kvseg.s_size);
203 segkp_bitmap = kmem_zalloc(BT_SIZEOFMAP(np), KM_SLEEP);
204 kpsd->kpsd_arena = vmem_create("segkp", NULL, 0, PAGESIZE,
205 vmem_alloc, vmem_free, heap_arena, 5 * PAGESIZE, VM_SLEEP);
206 } else {
207 segkp_bitmap = NULL;
208 np = btop(seg->s_size);
209 kpsd->kpsd_arena = vmem_create("segkp", seg->s_base,
210 seg->s_size, PAGESIZE, NULL, NULL, NULL, 5 * PAGESIZE,
211 VM_SLEEP);
214 kpsd->kpsd_anon = anon_create(np, ANON_SLEEP | ANON_ALLOC_FORCE);
216 kpsd->kpsd_hash = kmem_zalloc(SEGKP_HASHSZ * sizeof (struct segkp *),
217 KM_SLEEP);
218 seg->s_data = (void *)kpsd;
219 seg->s_ops = &segkp_ops;
220 segkpinit_mem_config(seg);
221 return (0);
226 * Find a free 'freelist' and initialize it with the appropriate attributes
228 void *
229 segkp_cache_init(struct seg *seg, int maxsize, size_t len, uint_t flags)
231 int i;
233 if ((flags & KPD_NO_ANON) && !(flags & KPD_LOCKED))
234 return ((void *)-1);
236 mutex_enter(&segkp_lock);
237 for (i = 0; i < SEGKP_MAX_CACHE; i++) {
238 if (segkp_cache[i].kpf_inuse)
239 continue;
240 segkp_cache[i].kpf_inuse = 1;
241 segkp_cache[i].kpf_max = maxsize;
242 segkp_cache[i].kpf_flags = flags;
243 segkp_cache[i].kpf_seg = seg;
244 segkp_cache[i].kpf_len = len;
245 mutex_exit(&segkp_lock);
246 return ((void *)(uintptr_t)i);
248 mutex_exit(&segkp_lock);
249 return ((void *)-1);
253 * Free all the cache resources.
255 void
256 segkp_cache_free(void)
258 struct segkp_data *kpd;
259 struct seg *seg;
260 int i;
262 mutex_enter(&segkp_lock);
263 for (i = 0; i < SEGKP_MAX_CACHE; i++) {
264 if (!segkp_cache[i].kpf_inuse)
265 continue;
267 * Disconnect the freelist and process each element
269 kpd = segkp_cache[i].kpf_list;
270 seg = segkp_cache[i].kpf_seg;
271 segkp_cache[i].kpf_list = NULL;
272 segkp_cache[i].kpf_count = 0;
273 mutex_exit(&segkp_lock);
275 while (kpd != NULL) {
276 struct segkp_data *next;
278 next = kpd->kp_next;
279 segkp_release_internal(seg, kpd, kpd->kp_len);
280 kpd = next;
282 mutex_enter(&segkp_lock);
284 mutex_exit(&segkp_lock);
288 * There are 2 entries into segkp_get_internal. The first includes a cookie
289 * used to access a pool of cached segkp resources. The second does not
290 * use the cache.
292 caddr_t
293 segkp_get(struct seg *seg, size_t len, uint_t flags)
295 struct segkp_data *kpd = NULL;
297 if (segkp_get_internal(seg, len, flags, &kpd, NULL) != NULL) {
298 kpd->kp_cookie = -1;
299 return (stom(kpd->kp_base, flags));
301 return (NULL);
305 * Return a 'cached' segkp address
307 caddr_t
308 segkp_cache_get(void *cookie)
310 struct segkp_cache *freelist = NULL;
311 struct segkp_data *kpd = NULL;
312 int index = (int)(uintptr_t)cookie;
313 struct seg *seg;
314 size_t len;
315 uint_t flags;
317 if (index < 0 || index >= SEGKP_MAX_CACHE)
318 return (NULL);
319 freelist = &segkp_cache[index];
321 mutex_enter(&segkp_lock);
322 seg = freelist->kpf_seg;
323 flags = freelist->kpf_flags;
324 if (freelist->kpf_list != NULL) {
325 kpd = freelist->kpf_list;
326 freelist->kpf_list = kpd->kp_next;
327 freelist->kpf_count--;
328 mutex_exit(&segkp_lock);
329 kpd->kp_next = NULL;
330 segkp_insert(seg, kpd);
331 return (stom(kpd->kp_base, flags));
333 len = freelist->kpf_len;
334 mutex_exit(&segkp_lock);
335 if (segkp_get_internal(seg, len, flags, &kpd, NULL) != NULL) {
336 kpd->kp_cookie = index;
337 return (stom(kpd->kp_base, flags));
339 return (NULL);
342 caddr_t
343 segkp_get_withanonmap(
344 struct seg *seg,
345 size_t len,
346 uint_t flags,
347 struct anon_map *amp)
349 struct segkp_data *kpd = NULL;
351 ASSERT(amp != NULL);
352 flags |= KPD_HASAMP;
353 if (segkp_get_internal(seg, len, flags, &kpd, amp) != NULL) {
354 kpd->kp_cookie = -1;
355 return (stom(kpd->kp_base, flags));
357 return (NULL);
361 * This does the real work of segkp allocation.
362 * Return to client base addr. len must be page-aligned. A null value is
363 * returned if there are no more vm resources (e.g. pages, swap). The len
364 * and base recorded in the private data structure include the redzone
365 * and the redzone length (if applicable). If the user requests a redzone
366 * either the first or last page is left unmapped depending whether stacks
367 * grow to low or high memory.
369 * The client may also specify a no-wait flag. If that is set then the
370 * request will choose a non-blocking path when requesting resources.
371 * The default is make the client wait.
373 static caddr_t
374 segkp_get_internal(
375 struct seg *seg,
376 size_t len,
377 uint_t flags,
378 struct segkp_data **tkpd,
379 struct anon_map *amp)
381 struct segkp_segdata *kpsd = (struct segkp_segdata *)seg->s_data;
382 struct segkp_data *kpd;
383 caddr_t vbase = NULL; /* always first virtual, may not be mapped */
384 pgcnt_t np = 0; /* number of pages in the resource */
385 pgcnt_t segkpindex;
386 long i;
387 caddr_t va;
388 pgcnt_t pages = 0;
389 ulong_t anon_idx = 0;
390 int kmflag = (flags & KPD_NOWAIT) ? KM_NOSLEEP : KM_SLEEP;
391 caddr_t s_base = (segkp_fromheap) ? kvseg.s_base : seg->s_base;
393 if (len & PAGEOFFSET) {
394 panic("segkp_get: len is not page-aligned");
395 /*NOTREACHED*/
398 ASSERT(((flags & KPD_HASAMP) == 0) == (amp == NULL));
400 /* Only allow KPD_NO_ANON if we are going to lock it down */
401 if ((flags & (KPD_LOCKED|KPD_NO_ANON)) == KPD_NO_ANON)
402 return (NULL);
404 if ((kpd = kmem_zalloc(sizeof (struct segkp_data), kmflag)) == NULL)
405 return (NULL);
407 * Fix up the len to reflect the REDZONE if applicable
409 if (flags & KPD_HASREDZONE)
410 len += PAGESIZE;
411 np = btop(len);
413 vbase = vmem_alloc(SEGKP_VMEM(seg), len, kmflag | VM_BESTFIT);
414 if (vbase == NULL) {
415 kmem_free(kpd, sizeof (struct segkp_data));
416 return (NULL);
419 /* If locking, reserve physical memory */
420 if (flags & KPD_LOCKED) {
421 pages = btop(SEGKP_MAPLEN(len, flags));
422 if (page_resv(pages, kmflag) == 0) {
423 vmem_free(SEGKP_VMEM(seg), vbase, len);
424 kmem_free(kpd, sizeof (struct segkp_data));
425 return (NULL);
427 if ((flags & KPD_NO_ANON) == 0)
428 atomic_add_long(&anon_segkp_pages_locked, pages);
432 * Reserve sufficient swap space for this vm resource. We'll
433 * actually allocate it in the loop below, but reserving it
434 * here allows us to back out more gracefully than if we
435 * had an allocation failure in the body of the loop.
437 * Note that we don't need swap space for the red zone page.
439 if (amp != NULL) {
441 * The swap reservation has been done, if required, and the
442 * anon_hdr is separate.
444 anon_idx = 0;
445 kpd->kp_anon_idx = anon_idx;
446 kpd->kp_anon = amp->ahp;
447 } else if ((flags & KPD_NO_ANON) == 0) {
448 if (anon_resv_zone(SEGKP_MAPLEN(len, flags), NULL) == 0) {
449 if (flags & KPD_LOCKED) {
450 atomic_add_long(&anon_segkp_pages_locked,
451 -pages);
452 page_unresv(pages);
454 vmem_free(SEGKP_VMEM(seg), vbase, len);
455 kmem_free(kpd, sizeof (struct segkp_data));
456 return (NULL);
458 atomic_add_long(&anon_segkp_pages_resv,
459 btop(SEGKP_MAPLEN(len, flags)));
460 anon_idx = ((uintptr_t)(vbase - s_base)) >> PAGESHIFT;
461 kpd->kp_anon_idx = anon_idx;
462 kpd->kp_anon = kpsd->kpsd_anon;
463 } else {
464 kpd->kp_anon = NULL;
465 kpd->kp_anon_idx = 0;
469 * Allocate page and anon resources for the virtual address range
470 * except the redzone
472 if (segkp_fromheap)
473 segkpindex = btop((uintptr_t)(vbase - kvseg.s_base));
474 for (i = 0, va = vbase; i < np; i++, va += PAGESIZE) {
475 page_t *pl[2];
476 struct vnode *vp;
477 anoff_t off;
478 int err;
479 page_t *pp = NULL;
482 * Mark this page to be a segkp page in the bitmap.
484 if (segkp_fromheap) {
485 BT_ATOMIC_SET(segkp_bitmap, segkpindex);
486 segkpindex++;
490 * If this page is the red zone page, we don't need swap
491 * space for it. Note that we skip over the code that
492 * establishes MMU mappings, so that the page remains
493 * invalid.
495 if ((flags & KPD_HASREDZONE) && KPD_REDZONE(kpd) == i)
496 continue;
498 if (kpd->kp_anon != NULL) {
499 struct anon *ap;
501 ASSERT(anon_get_ptr(kpd->kp_anon, anon_idx + i)
502 == NULL);
504 * Determine the "vp" and "off" of the anon slot.
506 ap = anon_alloc(NULL, 0);
507 if (amp != NULL)
508 ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
509 (void) anon_set_ptr(kpd->kp_anon, anon_idx + i,
510 ap, ANON_SLEEP);
511 if (amp != NULL)
512 ANON_LOCK_EXIT(&amp->a_rwlock);
513 swap_xlate(ap, &vp, &off);
516 * Create a page with the specified identity. The
517 * page is returned with the "shared" lock held.
519 err = fop_getpage(vp, (offset_t)off, PAGESIZE,
520 NULL, pl, PAGESIZE, seg, va, S_CREATE,
521 kcred, NULL);
522 if (err) {
524 * XXX - This should not fail.
526 panic("segkp_get: no pages");
527 /*NOTREACHED*/
529 pp = pl[0];
530 } else {
531 ASSERT(page_exists(&kvp,
532 (uoff_t)(uintptr_t)va) == NULL);
534 if ((pp = page_create_va(&kvp,
535 (uoff_t)(uintptr_t)va, PAGESIZE,
536 (flags & KPD_NOWAIT ? 0 : PG_WAIT) | PG_EXCL |
537 PG_NORELOC, seg, va)) == NULL) {
539 * Legitimize resource; then destroy it.
540 * Easier than trying to unwind here.
542 kpd->kp_flags = flags;
543 kpd->kp_base = vbase;
544 kpd->kp_len = len;
545 segkp_release_internal(seg, kpd, va - vbase);
546 return (NULL);
548 page_io_unlock(pp);
551 if (flags & KPD_ZERO)
552 pagezero(pp, 0, PAGESIZE);
555 * Load and lock an MMU translation for the page.
557 hat_memload(seg->s_as->a_hat, va, pp, (PROT_READ|PROT_WRITE),
558 ((flags & KPD_LOCKED) ? HAT_LOAD_LOCK : HAT_LOAD));
561 * Now, release lock on the page.
563 if (flags & KPD_LOCKED) {
565 * Indicate to page_retire framework that this
566 * page can only be retired when it is freed.
568 PP_SETRAF(pp);
569 page_downgrade(pp);
570 } else
571 page_unlock(pp);
574 kpd->kp_flags = flags;
575 kpd->kp_base = vbase;
576 kpd->kp_len = len;
577 segkp_insert(seg, kpd);
578 *tkpd = kpd;
579 return (stom(kpd->kp_base, flags));
583 * Release the resource to cache if the pool(designate by the cookie)
584 * has less than the maximum allowable. If inserted in cache,
585 * segkp_delete insures element is taken off of active list.
587 void
588 segkp_release(struct seg *seg, caddr_t vaddr)
590 struct segkp_cache *freelist;
591 struct segkp_data *kpd = NULL;
593 if ((kpd = segkp_find(seg, vaddr)) == NULL) {
594 panic("segkp_release: null kpd");
595 /*NOTREACHED*/
598 if (kpd->kp_cookie != -1) {
599 freelist = &segkp_cache[kpd->kp_cookie];
600 mutex_enter(&segkp_lock);
601 if (!segkp_indel && freelist->kpf_count < freelist->kpf_max) {
602 segkp_delete(seg, kpd);
603 kpd->kp_next = freelist->kpf_list;
604 freelist->kpf_list = kpd;
605 freelist->kpf_count++;
606 mutex_exit(&segkp_lock);
607 return;
608 } else {
609 mutex_exit(&segkp_lock);
610 kpd->kp_cookie = -1;
613 segkp_release_internal(seg, kpd, kpd->kp_len);
617 * Free the entire resource. segkp_unlock gets called with the start of the
618 * mapped portion of the resource. The length is the size of the mapped
619 * portion
621 static void
622 segkp_release_internal(struct seg *seg, struct segkp_data *kpd, size_t len)
624 caddr_t va;
625 long i;
626 long redzone;
627 size_t np;
628 page_t *pp;
629 struct vnode *vp;
630 anoff_t off;
631 struct anon *ap;
632 pgcnt_t segkpindex;
634 ASSERT(kpd != NULL);
635 ASSERT((kpd->kp_flags & KPD_HASAMP) == 0 || kpd->kp_cookie == -1);
636 np = btop(len);
638 /* Remove from active hash list */
639 if (kpd->kp_cookie == -1) {
640 mutex_enter(&segkp_lock);
641 segkp_delete(seg, kpd);
642 mutex_exit(&segkp_lock);
646 * Precompute redzone page index.
648 redzone = -1;
649 if (kpd->kp_flags & KPD_HASREDZONE)
650 redzone = KPD_REDZONE(kpd);
653 va = kpd->kp_base;
655 hat_unload(seg->s_as->a_hat, va, (np << PAGESHIFT),
656 ((kpd->kp_flags & KPD_LOCKED) ? HAT_UNLOAD_UNLOCK : HAT_UNLOAD));
658 * Free up those anon resources that are quiescent.
660 if (segkp_fromheap)
661 segkpindex = btop((uintptr_t)(va - kvseg.s_base));
662 for (i = 0; i < np; i++, va += PAGESIZE) {
665 * Clear the bit for this page from the bitmap.
667 if (segkp_fromheap) {
668 BT_ATOMIC_CLEAR(segkp_bitmap, segkpindex);
669 segkpindex++;
672 if (i == redzone)
673 continue;
674 if (kpd->kp_anon) {
676 * Free up anon resources and destroy the
677 * associated pages.
679 * Release the lock if there is one. Have to get the
680 * page to do this, unfortunately.
682 if (kpd->kp_flags & KPD_LOCKED) {
683 ap = anon_get_ptr(kpd->kp_anon,
684 kpd->kp_anon_idx + i);
685 swap_xlate(ap, &vp, &off);
686 /* Find the shared-locked page. */
687 pp = page_find(vp, (uoff_t)off);
688 if (pp == NULL) {
689 panic("segkp_release: "
690 "kp_anon: no page to unlock ");
691 /*NOTREACHED*/
693 if (PP_ISRAF(pp))
694 PP_CLRRAF(pp);
696 page_unlock(pp);
698 if ((kpd->kp_flags & KPD_HASAMP) == 0) {
699 anon_free(kpd->kp_anon, kpd->kp_anon_idx + i,
700 PAGESIZE);
701 anon_unresv_zone(PAGESIZE, NULL);
702 atomic_dec_ulong(&anon_segkp_pages_resv);
704 } else {
705 if (kpd->kp_flags & KPD_LOCKED) {
706 pp = page_find(&kvp, (uoff_t)(uintptr_t)va);
707 if (pp == NULL) {
708 panic("segkp_release: "
709 "no page to unlock");
710 /*NOTREACHED*/
712 if (PP_ISRAF(pp))
713 PP_CLRRAF(pp);
715 * We should just upgrade the lock here
716 * but there is no upgrade that waits.
718 page_unlock(pp);
720 pp = page_lookup(&kvp, (uoff_t)(uintptr_t)va,
721 SE_EXCL);
722 if (pp != NULL)
723 page_destroy(pp, 0);
727 /* If locked, release physical memory reservation */
728 if (kpd->kp_flags & KPD_LOCKED) {
729 pgcnt_t pages = btop(SEGKP_MAPLEN(kpd->kp_len, kpd->kp_flags));
730 if ((kpd->kp_flags & KPD_NO_ANON) == 0)
731 atomic_add_long(&anon_segkp_pages_locked, -pages);
732 page_unresv(pages);
735 vmem_free(SEGKP_VMEM(seg), kpd->kp_base, kpd->kp_len);
736 kmem_free(kpd, sizeof (struct segkp_data));
740 * segkp_map_red() will check the current frame pointer against the
741 * stack base. If the amount of stack remaining is questionable
742 * (less than red_minavail), then segkp_map_red() will map in the redzone
743 * and return 1. Otherwise, it will return 0. segkp_map_red() can
744 * _only_ be called when it is safe to sleep on page_create_va().
746 * It is up to the caller to remember whether segkp_map_red() successfully
747 * mapped the redzone, and, if so, to call segkp_unmap_red() at a later
748 * time.
750 * Currently, this routine is only called from pagefault() (which necessarily
751 * satisfies the above conditions).
753 #if defined(STACK_GROWTH_DOWN)
755 segkp_map_red(void)
757 uintptr_t fp = STACK_BIAS + (uintptr_t)getfp();
758 #ifndef _LP64
759 caddr_t stkbase;
760 #endif
763 * Optimize for the common case where we simply return.
765 if ((curthread->t_red_pp == NULL) &&
766 (fp - (uintptr_t)curthread->t_stkbase >= red_minavail))
767 return (0);
769 #if defined(_LP64)
771 * XXX We probably need something better than this.
773 panic("kernel stack overflow");
774 /*NOTREACHED*/
775 #else /* _LP64 */
776 if (curthread->t_red_pp == NULL) {
777 page_t *red_pp;
778 struct seg kseg;
780 caddr_t red_va = (caddr_t)
781 (((uintptr_t)curthread->t_stkbase & (uintptr_t)PAGEMASK) -
782 PAGESIZE);
784 ASSERT(page_exists(&kvp, (uoff_t)(uintptr_t)red_va) ==
785 NULL);
788 * Allocate the physical for the red page.
791 * No PG_NORELOC here to avoid waits. Unlikely to get
792 * a relocate happening in the short time the page exists
793 * and it will be OK anyway.
796 kseg.s_as = &kas;
797 red_pp = page_create_va(&kvp, (uoff_t)(uintptr_t)red_va,
798 PAGESIZE, PG_WAIT | PG_EXCL, &kseg, red_va);
799 ASSERT(red_pp != NULL);
802 * So we now have a page to jam into the redzone...
804 page_io_unlock(red_pp);
806 hat_memload(kas.a_hat, red_va, red_pp,
807 (PROT_READ|PROT_WRITE), HAT_LOAD_LOCK);
808 page_downgrade(red_pp);
811 * The page is left SE_SHARED locked so we can hold on to
812 * the page_t pointer.
814 curthread->t_red_pp = red_pp;
816 atomic_inc_32(&red_nmapped);
817 while (fp - (uintptr_t)curthread->t_stkbase < red_closest) {
818 (void) atomic_cas_32(&red_closest, red_closest,
819 (uint32_t)(fp - (uintptr_t)curthread->t_stkbase));
821 return (1);
824 stkbase = (caddr_t)(((uintptr_t)curthread->t_stkbase &
825 (uintptr_t)PAGEMASK) - PAGESIZE);
827 atomic_inc_32(&red_ndoubles);
829 if (fp - (uintptr_t)stkbase < RED_DEEP_THRESHOLD) {
831 * Oh boy. We're already deep within the mapped-in
832 * redzone page, and the caller is trying to prepare
833 * for a deep stack run. We're running without a
834 * redzone right now: if the caller plows off the
835 * end of the stack, it'll plow another thread or
836 * LWP structure. That situation could result in
837 * a very hard-to-debug panic, so, in the spirit of
838 * recording the name of one's killer in one's own
839 * blood, we're going to record hrestime and the calling
840 * thread.
842 red_deep_hires = hrestime.tv_nsec;
843 red_deep_thread = curthread;
847 * If this is a DEBUG kernel, and we've run too deep for comfort, toss.
849 ASSERT(fp - (uintptr_t)stkbase >= RED_DEEP_THRESHOLD);
850 return (0);
851 #endif /* _LP64 */
854 void
855 segkp_unmap_red(void)
857 page_t *pp;
858 caddr_t red_va = (caddr_t)(((uintptr_t)curthread->t_stkbase &
859 (uintptr_t)PAGEMASK) - PAGESIZE);
861 ASSERT(curthread->t_red_pp != NULL);
864 * Because we locked the mapping down, we can't simply rely
865 * on page_destroy() to clean everything up; we need to call
866 * hat_unload() to explicitly unlock the mapping resources.
868 hat_unload(kas.a_hat, red_va, PAGESIZE, HAT_UNLOAD_UNLOCK);
870 pp = curthread->t_red_pp;
872 ASSERT(pp == page_find(&kvp, (uoff_t)(uintptr_t)red_va));
875 * Need to upgrade the SE_SHARED lock to SE_EXCL.
877 if (!page_tryupgrade(pp)) {
879 * As there is now wait for upgrade, release the
880 * SE_SHARED lock and wait for SE_EXCL.
882 page_unlock(pp);
883 pp = page_lookup(&kvp, (uoff_t)(uintptr_t)red_va, SE_EXCL);
884 /* pp may be NULL here, hence the test below */
888 * Destroy the page, with dontfree set to zero (i.e. free it).
890 if (pp != NULL)
891 page_destroy(pp, 0);
892 curthread->t_red_pp = NULL;
894 #else
895 #error Red stacks only supported with downwards stack growth.
896 #endif
899 * Handle a fault on an address corresponding to one of the
900 * resources in the segkp segment.
902 faultcode_t
903 segkp_fault(
904 struct hat *hat,
905 struct seg *seg,
906 caddr_t vaddr,
907 size_t len,
908 enum fault_type type,
909 enum seg_rw rw)
911 struct segkp_data *kpd = NULL;
912 int err;
914 ASSERT(seg->s_as == &kas && RW_READ_HELD(&seg->s_as->a_lock));
917 * Sanity checks.
919 if (type == F_PROT) {
920 panic("segkp_fault: unexpected F_PROT fault");
921 /*NOTREACHED*/
924 if ((kpd = segkp_find(seg, vaddr)) == NULL)
925 return (FC_NOMAP);
927 mutex_enter(&kpd->kp_lock);
929 if (type == F_SOFTLOCK) {
930 ASSERT(!(kpd->kp_flags & KPD_LOCKED));
932 * The F_SOFTLOCK case has more stringent
933 * range requirements: the given range must exactly coincide
934 * with the resource's mapped portion. Note reference to
935 * redzone is handled since vaddr would not equal base
937 if (vaddr != stom(kpd->kp_base, kpd->kp_flags) ||
938 len != SEGKP_MAPLEN(kpd->kp_len, kpd->kp_flags)) {
939 mutex_exit(&kpd->kp_lock);
940 return (FC_MAKE_ERR(EFAULT));
943 if ((err = segkp_load(hat, seg, vaddr, len, kpd, KPD_LOCKED))) {
944 mutex_exit(&kpd->kp_lock);
945 return (FC_MAKE_ERR(err));
947 kpd->kp_flags |= KPD_LOCKED;
948 mutex_exit(&kpd->kp_lock);
949 return (0);
952 if (type == F_INVAL) {
953 ASSERT(!(kpd->kp_flags & KPD_NO_ANON));
956 * Check if we touched the redzone. Somewhat optimistic
957 * here if we are touching the redzone of our own stack
958 * since we wouldn't have a stack to get this far...
960 if ((kpd->kp_flags & KPD_HASREDZONE) &&
961 btop((uintptr_t)(vaddr - kpd->kp_base)) == KPD_REDZONE(kpd))
962 panic("segkp_fault: accessing redzone");
965 * This fault may occur while the page is being F_SOFTLOCK'ed.
966 * Return since a 2nd segkp_load is unnecessary and also would
967 * result in the page being locked twice and eventually
968 * hang the thread_reaper thread.
970 if (kpd->kp_flags & KPD_LOCKED) {
971 mutex_exit(&kpd->kp_lock);
972 return (0);
975 err = segkp_load(hat, seg, vaddr, len, kpd, kpd->kp_flags);
976 mutex_exit(&kpd->kp_lock);
977 return (err ? FC_MAKE_ERR(err) : 0);
980 if (type == F_SOFTUNLOCK) {
981 uint_t flags;
984 * Make sure the addr is LOCKED and it has anon backing
985 * before unlocking
987 if ((kpd->kp_flags & (KPD_LOCKED|KPD_NO_ANON)) != KPD_LOCKED) {
988 panic("segkp_fault: bad unlock");
989 /*NOTREACHED*/
992 if (vaddr != stom(kpd->kp_base, kpd->kp_flags) ||
993 len != SEGKP_MAPLEN(kpd->kp_len, kpd->kp_flags)) {
994 panic("segkp_fault: bad range");
995 /*NOTREACHED*/
998 if (rw == S_WRITE)
999 flags = kpd->kp_flags | KPD_WRITEDIRTY;
1000 else
1001 flags = kpd->kp_flags;
1002 err = segkp_unlock(hat, seg, vaddr, len, kpd, flags);
1003 kpd->kp_flags &= ~KPD_LOCKED;
1004 mutex_exit(&kpd->kp_lock);
1005 return (err ? FC_MAKE_ERR(err) : 0);
1007 mutex_exit(&kpd->kp_lock);
1008 panic("segkp_fault: bogus fault type: %d\n", type);
1009 /*NOTREACHED*/
1013 * Check that the given protections suffice over the range specified by
1014 * vaddr and len. For this segment type, the only issue is whether or
1015 * not the range lies completely within the mapped part of an allocated
1016 * resource.
1018 /* ARGSUSED */
1019 static int
1020 segkp_checkprot(struct seg *seg, caddr_t vaddr, size_t len, uint_t prot)
1022 struct segkp_data *kpd = NULL;
1023 caddr_t mbase;
1024 size_t mlen;
1026 if ((kpd = segkp_find(seg, vaddr)) == NULL)
1027 return (EACCES);
1029 mutex_enter(&kpd->kp_lock);
1030 mbase = stom(kpd->kp_base, kpd->kp_flags);
1031 mlen = SEGKP_MAPLEN(kpd->kp_len, kpd->kp_flags);
1032 if (len > mlen || vaddr < mbase ||
1033 ((vaddr + len) > (mbase + mlen))) {
1034 mutex_exit(&kpd->kp_lock);
1035 return (EACCES);
1037 mutex_exit(&kpd->kp_lock);
1038 return (0);
1043 * Check to see if it makes sense to do kluster/read ahead to
1044 * addr + delta relative to the mapping at addr. We assume here
1045 * that delta is a signed PAGESIZE'd multiple (which can be negative).
1047 * For seg_u we always "approve" of this action from our standpoint.
1049 /*ARGSUSED*/
1050 static int
1051 segkp_kluster(struct seg *seg, caddr_t addr, ssize_t delta)
1053 return (0);
1057 * Load and possibly lock intra-slot resources in the range given by
1058 * vaddr and len.
1060 static int
1061 segkp_load(
1062 struct hat *hat,
1063 struct seg *seg,
1064 caddr_t vaddr,
1065 size_t len,
1066 struct segkp_data *kpd,
1067 uint_t flags)
1069 caddr_t va;
1070 caddr_t vlim;
1071 ulong_t i;
1072 uint_t lock;
1074 ASSERT(MUTEX_HELD(&kpd->kp_lock));
1076 len = P2ROUNDUP(len, PAGESIZE);
1078 /* If locking, reserve physical memory */
1079 if (flags & KPD_LOCKED) {
1080 pgcnt_t pages = btop(len);
1081 if ((kpd->kp_flags & KPD_NO_ANON) == 0)
1082 atomic_add_long(&anon_segkp_pages_locked, pages);
1083 (void) page_resv(pages, KM_SLEEP);
1087 * Loop through the pages in the given range.
1089 va = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK);
1090 vaddr = va;
1091 vlim = va + len;
1092 lock = flags & KPD_LOCKED;
1093 i = ((uintptr_t)(va - kpd->kp_base)) >> PAGESHIFT;
1094 for (; va < vlim; va += PAGESIZE, i++) {
1095 page_t *pl[2]; /* second element NULL terminator */
1096 struct vnode *vp;
1097 anoff_t off;
1098 int err;
1099 struct anon *ap;
1102 * Summon the page. If it's not resident, arrange
1103 * for synchronous i/o to pull it in.
1105 ap = anon_get_ptr(kpd->kp_anon, kpd->kp_anon_idx + i);
1106 swap_xlate(ap, &vp, &off);
1109 * The returned page list will have exactly one entry,
1110 * which is returned to us already kept.
1112 err = fop_getpage(vp, (offset_t)off, PAGESIZE, NULL,
1113 pl, PAGESIZE, seg, va, S_READ, kcred, NULL);
1115 if (err) {
1117 * Back out of what we've done so far.
1119 (void) segkp_unlock(hat, seg, vaddr,
1120 (va - vaddr), kpd, flags);
1121 return (err);
1125 * Load an MMU translation for the page.
1127 hat_memload(hat, va, pl[0], (PROT_READ|PROT_WRITE),
1128 lock ? HAT_LOAD_LOCK : HAT_LOAD);
1130 if (!lock) {
1132 * Now, release "shared" lock on the page.
1134 page_unlock(pl[0]);
1137 return (0);
1141 * At the very least unload the mmu-translations and unlock the range if locked
1142 * Can be called with the following flag value KPD_WRITEDIRTY which specifies
1143 * any dirty pages should be written to disk.
1145 static int
1146 segkp_unlock(
1147 struct hat *hat,
1148 struct seg *seg,
1149 caddr_t vaddr,
1150 size_t len,
1151 struct segkp_data *kpd,
1152 uint_t flags)
1154 caddr_t va;
1155 caddr_t vlim;
1156 ulong_t i;
1157 struct page *pp;
1158 struct vnode *vp;
1159 anoff_t off;
1160 struct anon *ap;
1163 ASSERT(MUTEX_HELD(&kpd->kp_lock));
1166 * Loop through the pages in the given range. It is assumed
1167 * segkp_unlock is called with page aligned base
1169 va = vaddr;
1170 vlim = va + len;
1171 i = ((uintptr_t)(va - kpd->kp_base)) >> PAGESHIFT;
1172 hat_unload(hat, va, len,
1173 ((flags & KPD_LOCKED) ? HAT_UNLOAD_UNLOCK : HAT_UNLOAD));
1174 for (; va < vlim; va += PAGESIZE, i++) {
1176 * Find the page associated with this part of the
1177 * slot, tracking it down through its associated swap
1178 * space.
1180 ap = anon_get_ptr(kpd->kp_anon, kpd->kp_anon_idx + i);
1181 swap_xlate(ap, &vp, &off);
1183 if (flags & KPD_LOCKED) {
1184 if ((pp = page_find(vp, off)) == NULL) {
1185 if (flags & KPD_LOCKED) {
1186 panic("segkp_softunlock: missing page");
1187 /*NOTREACHED*/
1190 } else {
1192 * Nothing to do if the slot is not locked and the
1193 * page doesn't exist.
1195 if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL)
1196 continue;
1200 * If the page doesn't have any translations, is
1201 * dirty and not being shared, then push it out
1202 * asynchronously and avoid waiting for the
1203 * pageout daemon to do it for us.
1205 * XXX - Do we really need to get the "exclusive"
1206 * lock via an upgrade?
1208 if ((flags & KPD_WRITEDIRTY) && !hat_page_is_mapped(pp) &&
1209 hat_ismod(pp) && page_tryupgrade(pp)) {
1211 * Hold the vnode before releasing the page lock to
1212 * prevent it from being freed and re-used by some
1213 * other thread.
1215 VN_HOLD(vp);
1216 page_unlock(pp);
1219 * Want most powerful credentials we can get so
1220 * use kcred.
1222 (void) fop_putpage(vp, (offset_t)off, PAGESIZE,
1223 B_ASYNC | B_FREE, kcred, NULL);
1224 VN_RELE(vp);
1225 } else {
1226 page_unlock(pp);
1230 /* If unlocking, release physical memory */
1231 if (flags & KPD_LOCKED) {
1232 pgcnt_t pages = btopr(len);
1233 if ((kpd->kp_flags & KPD_NO_ANON) == 0)
1234 atomic_add_long(&anon_segkp_pages_locked, -pages);
1235 page_unresv(pages);
1237 return (0);
1241 * Insert the kpd in the hash table.
1243 static void
1244 segkp_insert(struct seg *seg, struct segkp_data *kpd)
1246 struct segkp_segdata *kpsd = (struct segkp_segdata *)seg->s_data;
1247 int index;
1250 * Insert the kpd based on the address that will be returned
1251 * via segkp_release.
1253 index = SEGKP_HASH(stom(kpd->kp_base, kpd->kp_flags));
1254 mutex_enter(&segkp_lock);
1255 kpd->kp_next = kpsd->kpsd_hash[index];
1256 kpsd->kpsd_hash[index] = kpd;
1257 mutex_exit(&segkp_lock);
1261 * Remove kpd from the hash table.
1263 static void
1264 segkp_delete(struct seg *seg, struct segkp_data *kpd)
1266 struct segkp_segdata *kpsd = (struct segkp_segdata *)seg->s_data;
1267 struct segkp_data **kpp;
1268 int index;
1270 ASSERT(MUTEX_HELD(&segkp_lock));
1272 index = SEGKP_HASH(stom(kpd->kp_base, kpd->kp_flags));
1273 for (kpp = &kpsd->kpsd_hash[index];
1274 *kpp != NULL; kpp = &((*kpp)->kp_next)) {
1275 if (*kpp == kpd) {
1276 *kpp = kpd->kp_next;
1277 return;
1280 panic("segkp_delete: unable to find element to delete");
1281 /*NOTREACHED*/
1285 * Find the kpd associated with a vaddr.
1287 * Most of the callers of segkp_find will pass the vaddr that
1288 * hashes to the desired index, but there are cases where
1289 * this is not true in which case we have to (potentially) scan
1290 * the whole table looking for it. This should be very rare
1291 * (e.g. a segkp_fault(F_INVAL) on an address somewhere in the
1292 * middle of the segkp_data region).
1294 static struct segkp_data *
1295 segkp_find(struct seg *seg, caddr_t vaddr)
1297 struct segkp_segdata *kpsd = (struct segkp_segdata *)seg->s_data;
1298 struct segkp_data *kpd;
1299 int i;
1300 int stop;
1302 i = stop = SEGKP_HASH(vaddr);
1303 mutex_enter(&segkp_lock);
1304 do {
1305 for (kpd = kpsd->kpsd_hash[i]; kpd != NULL;
1306 kpd = kpd->kp_next) {
1307 if (vaddr >= kpd->kp_base &&
1308 vaddr < kpd->kp_base + kpd->kp_len) {
1309 mutex_exit(&segkp_lock);
1310 return (kpd);
1313 if (--i < 0)
1314 i = SEGKP_HASHSZ - 1; /* Wrap */
1315 } while (i != stop);
1316 mutex_exit(&segkp_lock);
1317 return (NULL); /* Not found */
1321 * Dump out all the active segkp pages
1323 static void
1324 segkp_dump(struct seg *seg)
1326 int i;
1327 struct segkp_data *kpd;
1328 struct segkp_segdata *kpsd = (struct segkp_segdata *)seg->s_data;
1330 for (i = 0; i < SEGKP_HASHSZ; i++) {
1331 for (kpd = kpsd->kpsd_hash[i];
1332 kpd != NULL; kpd = kpd->kp_next) {
1333 pfn_t pfn;
1334 caddr_t addr;
1335 caddr_t eaddr;
1337 addr = kpd->kp_base;
1338 eaddr = addr + kpd->kp_len;
1339 while (addr < eaddr) {
1340 ASSERT(seg->s_as == &kas);
1341 pfn = hat_getpfnum(seg->s_as->a_hat, addr);
1342 if (pfn != PFN_INVALID)
1343 dump_addpage(seg->s_as, addr, pfn);
1344 addr += PAGESIZE;
1345 dump_timeleft = dump_timeout;
1351 /*ARGSUSED*/
1352 static int
1353 segkp_pagelock(struct seg *seg, caddr_t addr, size_t len,
1354 struct page ***ppp, enum lock_type type, enum seg_rw rw)
1356 return (ENOTSUP);
1359 #include <sys/mem_config.h>
1361 /*ARGSUSED*/
1362 static void
1363 segkp_mem_config_post_add(void *arg, pgcnt_t delta_pages)
1367 * During memory delete, turn off caches so that pages are not held.
1368 * A better solution may be to unlock the pages while they are
1369 * in the cache so that they may be collected naturally.
1372 /*ARGSUSED*/
1373 static int
1374 segkp_mem_config_pre_del(void *arg, pgcnt_t delta_pages)
1376 atomic_inc_32(&segkp_indel);
1377 segkp_cache_free();
1378 return (0);
1381 /*ARGSUSED*/
1382 static void
1383 segkp_mem_config_post_del(void *arg, pgcnt_t delta_pages, int cancelled)
1385 atomic_dec_32(&segkp_indel);
1388 static kphysm_setup_vector_t segkp_mem_config_vec = {
1389 KPHYSM_SETUP_VECTOR_VERSION,
1390 segkp_mem_config_post_add,
1391 segkp_mem_config_pre_del,
1392 segkp_mem_config_post_del,
1395 static void
1396 segkpinit_mem_config(struct seg *seg)
1398 int ret;
1400 ret = kphysm_setup_func_register(&segkp_mem_config_vec, (void *)seg);
1401 ASSERT(ret == 0);