10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53247Sgjelinek * Common Development and Distribution License (the "License"). 63247Sgjelinek * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*5331Samw * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 310Sstevel@tonic-gate * under license from the Regents of the University of California. 320Sstevel@tonic-gate */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 350Sstevel@tonic-gate 360Sstevel@tonic-gate /* 370Sstevel@tonic-gate * segkp is a segment driver that administers the allocation and deallocation 380Sstevel@tonic-gate * of pageable variable size chunks of kernel virtual address space. Each 390Sstevel@tonic-gate * allocated resource is page-aligned. 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * The user may specify whether the resource should be initialized to 0, 420Sstevel@tonic-gate * include a redzone, or locked in memory. 430Sstevel@tonic-gate */ 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include <sys/types.h> 460Sstevel@tonic-gate #include <sys/t_lock.h> 470Sstevel@tonic-gate #include <sys/thread.h> 480Sstevel@tonic-gate #include <sys/param.h> 490Sstevel@tonic-gate #include <sys/errno.h> 500Sstevel@tonic-gate #include <sys/sysmacros.h> 510Sstevel@tonic-gate #include <sys/systm.h> 520Sstevel@tonic-gate #include <sys/buf.h> 530Sstevel@tonic-gate #include <sys/mman.h> 540Sstevel@tonic-gate #include <sys/vnode.h> 550Sstevel@tonic-gate #include <sys/cmn_err.h> 560Sstevel@tonic-gate #include <sys/swap.h> 570Sstevel@tonic-gate #include <sys/tuneable.h> 580Sstevel@tonic-gate #include <sys/kmem.h> 590Sstevel@tonic-gate #include <sys/vmem.h> 600Sstevel@tonic-gate #include <sys/cred.h> 610Sstevel@tonic-gate #include <sys/dumphdr.h> 620Sstevel@tonic-gate #include <sys/debug.h> 630Sstevel@tonic-gate #include <sys/vtrace.h> 640Sstevel@tonic-gate #include <sys/stack.h> 650Sstevel@tonic-gate #include <sys/atomic.h> 660Sstevel@tonic-gate #include <sys/archsystm.h> 670Sstevel@tonic-gate #include <sys/lgrp.h> 680Sstevel@tonic-gate 690Sstevel@tonic-gate #include <vm/as.h> 700Sstevel@tonic-gate #include <vm/seg.h> 710Sstevel@tonic-gate #include <vm/seg_kp.h> 720Sstevel@tonic-gate #include <vm/seg_kmem.h> 730Sstevel@tonic-gate #include <vm/anon.h> 740Sstevel@tonic-gate #include <vm/page.h> 750Sstevel@tonic-gate #include <vm/hat.h> 760Sstevel@tonic-gate #include <sys/bitmap.h> 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* 790Sstevel@tonic-gate * Private seg op routines 800Sstevel@tonic-gate */ 810Sstevel@tonic-gate static void segkp_badop(void); 820Sstevel@tonic-gate static void segkp_dump(struct seg *seg); 830Sstevel@tonic-gate static int segkp_checkprot(struct seg *seg, caddr_t addr, size_t len, 840Sstevel@tonic-gate uint_t prot); 850Sstevel@tonic-gate static int segkp_kluster(struct seg *seg, caddr_t addr, ssize_t delta); 860Sstevel@tonic-gate static int segkp_pagelock(struct seg *seg, caddr_t addr, size_t len, 870Sstevel@tonic-gate struct page ***page, enum lock_type type, 880Sstevel@tonic-gate enum seg_rw rw); 890Sstevel@tonic-gate static void segkp_insert(struct seg *seg, struct segkp_data *kpd); 900Sstevel@tonic-gate static void segkp_delete(struct seg *seg, struct segkp_data *kpd); 910Sstevel@tonic-gate static caddr_t segkp_get_internal(struct seg *seg, size_t len, uint_t flags, 920Sstevel@tonic-gate struct segkp_data **tkpd, struct anon_map *amp); 930Sstevel@tonic-gate static void segkp_release_internal(struct seg *seg, 940Sstevel@tonic-gate struct segkp_data *kpd, size_t len); 950Sstevel@tonic-gate static int segkp_unlock(struct hat *hat, struct seg *seg, caddr_t vaddr, 960Sstevel@tonic-gate size_t len, struct segkp_data *kpd, uint_t flags); 970Sstevel@tonic-gate static int segkp_load(struct hat *hat, struct seg *seg, caddr_t vaddr, 980Sstevel@tonic-gate size_t len, struct segkp_data *kpd, uint_t flags); 990Sstevel@tonic-gate static struct segkp_data *segkp_find(struct seg *seg, caddr_t vaddr); 1000Sstevel@tonic-gate static int segkp_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp); 1010Sstevel@tonic-gate static lgrp_mem_policy_info_t *segkp_getpolicy(struct seg *seg, 1020Sstevel@tonic-gate caddr_t addr); 103670Selowe static int segkp_capable(struct seg *seg, segcapability_t capability); 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate /* 1060Sstevel@tonic-gate * Lock used to protect the hash table(s) and caches. 1070Sstevel@tonic-gate */ 1080Sstevel@tonic-gate static kmutex_t segkp_lock; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* 1110Sstevel@tonic-gate * The segkp caches 1120Sstevel@tonic-gate */ 1130Sstevel@tonic-gate static struct segkp_cache segkp_cache[SEGKP_MAX_CACHE]; 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate #define SEGKP_BADOP(t) (t(*)())segkp_badop 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * When there are fewer than red_minavail bytes left on the stack, 1190Sstevel@tonic-gate * segkp_map_red() will map in the redzone (if called). 5000 seems 1200Sstevel@tonic-gate * to work reasonably well... 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate long red_minavail = 5000; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate /* 1250Sstevel@tonic-gate * will be set to 1 for 32 bit x86 systems only, in startup.c 1260Sstevel@tonic-gate */ 1270Sstevel@tonic-gate int segkp_fromheap = 0; 1280Sstevel@tonic-gate ulong_t *segkp_bitmap; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* 1310Sstevel@tonic-gate * If segkp_map_red() is called with the redzone already mapped and 1320Sstevel@tonic-gate * with less than RED_DEEP_THRESHOLD bytes available on the stack, 1330Sstevel@tonic-gate * then the stack situation has become quite serious; if much more stack 1340Sstevel@tonic-gate * is consumed, we have the potential of scrogging the next thread/LWP 1350Sstevel@tonic-gate * structure. To help debug the "can't happen" panics which may 1360Sstevel@tonic-gate * result from this condition, we record lbolt and the calling thread 1370Sstevel@tonic-gate * in red_deep_lbolt and red_deep_thread respectively. 1380Sstevel@tonic-gate */ 1390Sstevel@tonic-gate #define RED_DEEP_THRESHOLD 2000 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate clock_t red_deep_lbolt; 1420Sstevel@tonic-gate kthread_t *red_deep_thread; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate uint32_t red_nmapped; 1450Sstevel@tonic-gate uint32_t red_closest = UINT_MAX; 1460Sstevel@tonic-gate uint32_t red_ndoubles; 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate pgcnt_t anon_segkp_pages_locked; /* See vm/anon.h */ 1493247Sgjelinek pgcnt_t anon_segkp_pages_resv; /* anon reserved by seg_kp */ 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate static struct seg_ops segkp_ops = { 1520Sstevel@tonic-gate SEGKP_BADOP(int), /* dup */ 1530Sstevel@tonic-gate SEGKP_BADOP(int), /* unmap */ 1540Sstevel@tonic-gate SEGKP_BADOP(void), /* free */ 1550Sstevel@tonic-gate segkp_fault, 1560Sstevel@tonic-gate SEGKP_BADOP(faultcode_t), /* faulta */ 1570Sstevel@tonic-gate SEGKP_BADOP(int), /* setprot */ 1580Sstevel@tonic-gate segkp_checkprot, 1590Sstevel@tonic-gate segkp_kluster, 1600Sstevel@tonic-gate SEGKP_BADOP(size_t), /* swapout */ 1610Sstevel@tonic-gate SEGKP_BADOP(int), /* sync */ 1620Sstevel@tonic-gate SEGKP_BADOP(size_t), /* incore */ 1630Sstevel@tonic-gate SEGKP_BADOP(int), /* lockop */ 1640Sstevel@tonic-gate SEGKP_BADOP(int), /* getprot */ 1650Sstevel@tonic-gate SEGKP_BADOP(u_offset_t), /* getoffset */ 1660Sstevel@tonic-gate SEGKP_BADOP(int), /* gettype */ 1670Sstevel@tonic-gate SEGKP_BADOP(int), /* getvp */ 1680Sstevel@tonic-gate SEGKP_BADOP(int), /* advise */ 1690Sstevel@tonic-gate segkp_dump, /* dump */ 1700Sstevel@tonic-gate segkp_pagelock, /* pagelock */ 1710Sstevel@tonic-gate SEGKP_BADOP(int), /* setpgsz */ 1720Sstevel@tonic-gate segkp_getmemid, /* getmemid */ 1730Sstevel@tonic-gate segkp_getpolicy, /* getpolicy */ 174670Selowe segkp_capable, /* capable */ 1750Sstevel@tonic-gate }; 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate static void 1790Sstevel@tonic-gate segkp_badop(void) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate panic("segkp_badop"); 1820Sstevel@tonic-gate /*NOTREACHED*/ 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate static void segkpinit_mem_config(struct seg *); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate static uint32_t segkp_indel; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * Allocate the segment specific private data struct and fill it in 1910Sstevel@tonic-gate * with the per kp segment mutex, anon ptr. array and hash table. 1920Sstevel@tonic-gate */ 1930Sstevel@tonic-gate int 1940Sstevel@tonic-gate segkp_create(struct seg *seg) 1950Sstevel@tonic-gate { 1960Sstevel@tonic-gate struct segkp_segdata *kpsd; 1970Sstevel@tonic-gate size_t np; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate ASSERT(seg != NULL && seg->s_as == &kas); 2000Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&seg->s_as->a_lock)); 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate if (seg->s_size & PAGEOFFSET) { 2030Sstevel@tonic-gate panic("Bad segkp size"); 2040Sstevel@tonic-gate /*NOTREACHED*/ 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate kpsd = kmem_zalloc(sizeof (struct segkp_segdata), KM_SLEEP); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate /* 2100Sstevel@tonic-gate * Allocate the virtual memory for segkp and initialize it 2110Sstevel@tonic-gate */ 2120Sstevel@tonic-gate if (segkp_fromheap) { 2130Sstevel@tonic-gate np = btop(kvseg.s_size); 2140Sstevel@tonic-gate segkp_bitmap = kmem_zalloc(BT_SIZEOFMAP(np), KM_SLEEP); 2150Sstevel@tonic-gate kpsd->kpsd_arena = vmem_create("segkp", NULL, 0, PAGESIZE, 2160Sstevel@tonic-gate vmem_alloc, vmem_free, heap_arena, 5 * PAGESIZE, VM_SLEEP); 2170Sstevel@tonic-gate } else { 2180Sstevel@tonic-gate segkp_bitmap = NULL; 2190Sstevel@tonic-gate np = btop(seg->s_size); 2200Sstevel@tonic-gate kpsd->kpsd_arena = vmem_create("segkp", seg->s_base, 2210Sstevel@tonic-gate seg->s_size, PAGESIZE, NULL, NULL, NULL, 5 * PAGESIZE, 2220Sstevel@tonic-gate VM_SLEEP); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate kpsd->kpsd_anon = anon_create(np, ANON_SLEEP | ANON_ALLOC_FORCE); 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate kpsd->kpsd_hash = kmem_zalloc(SEGKP_HASHSZ * sizeof (struct segkp *), 2280Sstevel@tonic-gate KM_SLEEP); 2290Sstevel@tonic-gate seg->s_data = (void *)kpsd; 2300Sstevel@tonic-gate seg->s_ops = &segkp_ops; 2310Sstevel@tonic-gate segkpinit_mem_config(seg); 2320Sstevel@tonic-gate return (0); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate /* 2370Sstevel@tonic-gate * Find a free 'freelist' and initialize it with the appropriate attributes 2380Sstevel@tonic-gate */ 2390Sstevel@tonic-gate void * 2400Sstevel@tonic-gate segkp_cache_init(struct seg *seg, int maxsize, size_t len, uint_t flags) 2410Sstevel@tonic-gate { 2420Sstevel@tonic-gate int i; 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate if ((flags & KPD_NO_ANON) && !(flags & KPD_LOCKED)) 2450Sstevel@tonic-gate return ((void *)-1); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate mutex_enter(&segkp_lock); 2480Sstevel@tonic-gate for (i = 0; i < SEGKP_MAX_CACHE; i++) { 2490Sstevel@tonic-gate if (segkp_cache[i].kpf_inuse) 2500Sstevel@tonic-gate continue; 2510Sstevel@tonic-gate segkp_cache[i].kpf_inuse = 1; 2520Sstevel@tonic-gate segkp_cache[i].kpf_max = maxsize; 2530Sstevel@tonic-gate segkp_cache[i].kpf_flags = flags; 2540Sstevel@tonic-gate segkp_cache[i].kpf_seg = seg; 2550Sstevel@tonic-gate segkp_cache[i].kpf_len = len; 2560Sstevel@tonic-gate mutex_exit(&segkp_lock); 2570Sstevel@tonic-gate return ((void *)(uintptr_t)i); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate mutex_exit(&segkp_lock); 2600Sstevel@tonic-gate return ((void *)-1); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * Free all the cache resources. 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate void 2670Sstevel@tonic-gate segkp_cache_free(void) 2680Sstevel@tonic-gate { 2690Sstevel@tonic-gate struct segkp_data *kpd; 2700Sstevel@tonic-gate struct seg *seg; 2710Sstevel@tonic-gate int i; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate mutex_enter(&segkp_lock); 2740Sstevel@tonic-gate for (i = 0; i < SEGKP_MAX_CACHE; i++) { 2750Sstevel@tonic-gate if (!segkp_cache[i].kpf_inuse) 2760Sstevel@tonic-gate continue; 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * Disconnect the freelist and process each element 2790Sstevel@tonic-gate */ 2800Sstevel@tonic-gate kpd = segkp_cache[i].kpf_list; 2810Sstevel@tonic-gate seg = segkp_cache[i].kpf_seg; 2820Sstevel@tonic-gate segkp_cache[i].kpf_list = NULL; 2830Sstevel@tonic-gate segkp_cache[i].kpf_count = 0; 2840Sstevel@tonic-gate mutex_exit(&segkp_lock); 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate while (kpd != NULL) { 2870Sstevel@tonic-gate struct segkp_data *next; 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate next = kpd->kp_next; 2900Sstevel@tonic-gate segkp_release_internal(seg, kpd, kpd->kp_len); 2910Sstevel@tonic-gate kpd = next; 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate mutex_enter(&segkp_lock); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate mutex_exit(&segkp_lock); 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* 2990Sstevel@tonic-gate * There are 2 entries into segkp_get_internal. The first includes a cookie 3000Sstevel@tonic-gate * used to access a pool of cached segkp resources. The second does not 3010Sstevel@tonic-gate * use the cache. 3020Sstevel@tonic-gate */ 3030Sstevel@tonic-gate caddr_t 3040Sstevel@tonic-gate segkp_get(struct seg *seg, size_t len, uint_t flags) 3050Sstevel@tonic-gate { 3060Sstevel@tonic-gate struct segkp_data *kpd = NULL; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate if (segkp_get_internal(seg, len, flags, &kpd, NULL) != NULL) { 3090Sstevel@tonic-gate kpd->kp_cookie = -1; 3100Sstevel@tonic-gate return (stom(kpd->kp_base, flags)); 3110Sstevel@tonic-gate } 3120Sstevel@tonic-gate return (NULL); 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* 3160Sstevel@tonic-gate * Return a 'cached' segkp address 3170Sstevel@tonic-gate */ 3180Sstevel@tonic-gate caddr_t 3190Sstevel@tonic-gate segkp_cache_get(void *cookie) 3200Sstevel@tonic-gate { 3210Sstevel@tonic-gate struct segkp_cache *freelist = NULL; 3220Sstevel@tonic-gate struct segkp_data *kpd = NULL; 3230Sstevel@tonic-gate int index = (int)(uintptr_t)cookie; 3240Sstevel@tonic-gate struct seg *seg; 3250Sstevel@tonic-gate size_t len; 3260Sstevel@tonic-gate uint_t flags; 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate if (index < 0 || index >= SEGKP_MAX_CACHE) 3290Sstevel@tonic-gate return (NULL); 3300Sstevel@tonic-gate freelist = &segkp_cache[index]; 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate mutex_enter(&segkp_lock); 3330Sstevel@tonic-gate seg = freelist->kpf_seg; 3340Sstevel@tonic-gate flags = freelist->kpf_flags; 3350Sstevel@tonic-gate if (freelist->kpf_list != NULL) { 3360Sstevel@tonic-gate kpd = freelist->kpf_list; 3370Sstevel@tonic-gate freelist->kpf_list = kpd->kp_next; 3380Sstevel@tonic-gate freelist->kpf_count--; 3390Sstevel@tonic-gate mutex_exit(&segkp_lock); 3400Sstevel@tonic-gate kpd->kp_next = NULL; 3410Sstevel@tonic-gate segkp_insert(seg, kpd); 3420Sstevel@tonic-gate return (stom(kpd->kp_base, flags)); 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate len = freelist->kpf_len; 3450Sstevel@tonic-gate mutex_exit(&segkp_lock); 3460Sstevel@tonic-gate if (segkp_get_internal(seg, len, flags, &kpd, NULL) != NULL) { 3470Sstevel@tonic-gate kpd->kp_cookie = index; 3480Sstevel@tonic-gate return (stom(kpd->kp_base, flags)); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate return (NULL); 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate caddr_t 3540Sstevel@tonic-gate segkp_get_withanonmap( 3550Sstevel@tonic-gate struct seg *seg, 3560Sstevel@tonic-gate size_t len, 3570Sstevel@tonic-gate uint_t flags, 3580Sstevel@tonic-gate struct anon_map *amp) 3590Sstevel@tonic-gate { 3600Sstevel@tonic-gate struct segkp_data *kpd = NULL; 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate ASSERT(amp != NULL); 3630Sstevel@tonic-gate flags |= KPD_HASAMP; 3640Sstevel@tonic-gate if (segkp_get_internal(seg, len, flags, &kpd, amp) != NULL) { 3650Sstevel@tonic-gate kpd->kp_cookie = -1; 3660Sstevel@tonic-gate return (stom(kpd->kp_base, flags)); 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate return (NULL); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * This does the real work of segkp allocation. 3730Sstevel@tonic-gate * Return to client base addr. len must be page-aligned. A null value is 3740Sstevel@tonic-gate * returned if there are no more vm resources (e.g. pages, swap). The len 3750Sstevel@tonic-gate * and base recorded in the private data structure include the redzone 3760Sstevel@tonic-gate * and the redzone length (if applicable). If the user requests a redzone 3770Sstevel@tonic-gate * either the first or last page is left unmapped depending whether stacks 3780Sstevel@tonic-gate * grow to low or high memory. 3790Sstevel@tonic-gate * 3800Sstevel@tonic-gate * The client may also specify a no-wait flag. If that is set then the 3810Sstevel@tonic-gate * request will choose a non-blocking path when requesting resources. 3820Sstevel@tonic-gate * The default is make the client wait. 3830Sstevel@tonic-gate */ 3840Sstevel@tonic-gate static caddr_t 3850Sstevel@tonic-gate segkp_get_internal( 3860Sstevel@tonic-gate struct seg *seg, 3870Sstevel@tonic-gate size_t len, 3880Sstevel@tonic-gate uint_t flags, 3890Sstevel@tonic-gate struct segkp_data **tkpd, 3900Sstevel@tonic-gate struct anon_map *amp) 3910Sstevel@tonic-gate { 3920Sstevel@tonic-gate struct segkp_segdata *kpsd = (struct segkp_segdata *)seg->s_data; 3930Sstevel@tonic-gate struct segkp_data *kpd; 3940Sstevel@tonic-gate caddr_t vbase = NULL; /* always first virtual, may not be mapped */ 3950Sstevel@tonic-gate pgcnt_t np = 0; /* number of pages in the resource */ 3960Sstevel@tonic-gate pgcnt_t segkpindex; 3970Sstevel@tonic-gate long i; 3980Sstevel@tonic-gate caddr_t va; 3990Sstevel@tonic-gate pgcnt_t pages = 0; 4000Sstevel@tonic-gate ulong_t anon_idx = 0; 4010Sstevel@tonic-gate int kmflag = (flags & KPD_NOWAIT) ? KM_NOSLEEP : KM_SLEEP; 4020Sstevel@tonic-gate caddr_t s_base = (segkp_fromheap) ? kvseg.s_base : seg->s_base; 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate if (len & PAGEOFFSET) { 4050Sstevel@tonic-gate panic("segkp_get: len is not page-aligned"); 4060Sstevel@tonic-gate /*NOTREACHED*/ 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate ASSERT(((flags & KPD_HASAMP) == 0) == (amp == NULL)); 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate /* Only allow KPD_NO_ANON if we are going to lock it down */ 4120Sstevel@tonic-gate if ((flags & (KPD_LOCKED|KPD_NO_ANON)) == KPD_NO_ANON) 4130Sstevel@tonic-gate return (NULL); 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate if ((kpd = kmem_zalloc(sizeof (struct segkp_data), kmflag)) == NULL) 4160Sstevel@tonic-gate return (NULL); 4170Sstevel@tonic-gate /* 4180Sstevel@tonic-gate * Fix up the len to reflect the REDZONE if applicable 4190Sstevel@tonic-gate */ 4200Sstevel@tonic-gate if (flags & KPD_HASREDZONE) 4210Sstevel@tonic-gate len += PAGESIZE; 4220Sstevel@tonic-gate np = btop(len); 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate vbase = vmem_alloc(SEGKP_VMEM(seg), len, kmflag | VM_BESTFIT); 4250Sstevel@tonic-gate if (vbase == NULL) { 4260Sstevel@tonic-gate kmem_free(kpd, sizeof (struct segkp_data)); 4270Sstevel@tonic-gate return (NULL); 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate /* If locking, reserve physical memory */ 4310Sstevel@tonic-gate if (flags & KPD_LOCKED) { 4320Sstevel@tonic-gate pages = btop(SEGKP_MAPLEN(len, flags)); 4330Sstevel@tonic-gate if (page_resv(pages, kmflag) == 0) { 4340Sstevel@tonic-gate vmem_free(SEGKP_VMEM(seg), vbase, len); 4350Sstevel@tonic-gate kmem_free(kpd, sizeof (struct segkp_data)); 4360Sstevel@tonic-gate return (NULL); 4370Sstevel@tonic-gate } 4380Sstevel@tonic-gate if ((flags & KPD_NO_ANON) == 0) 4390Sstevel@tonic-gate atomic_add_long(&anon_segkp_pages_locked, pages); 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate /* 4430Sstevel@tonic-gate * Reserve sufficient swap space for this vm resource. We'll 4440Sstevel@tonic-gate * actually allocate it in the loop below, but reserving it 4450Sstevel@tonic-gate * here allows us to back out more gracefully than if we 4460Sstevel@tonic-gate * had an allocation failure in the body of the loop. 4470Sstevel@tonic-gate * 4480Sstevel@tonic-gate * Note that we don't need swap space for the red zone page. 4490Sstevel@tonic-gate */ 4500Sstevel@tonic-gate if (amp != NULL) { 4513247Sgjelinek /* 4523247Sgjelinek * The swap reservation has been done, if required, and the 4533247Sgjelinek * anon_hdr is separate. 4543247Sgjelinek */ 4550Sstevel@tonic-gate anon_idx = 0; 4560Sstevel@tonic-gate kpd->kp_anon_idx = anon_idx; 4570Sstevel@tonic-gate kpd->kp_anon = amp->ahp; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate TRACE_5(TR_FAC_VM, TR_ANON_SEGKP, "anon segkp:%p %p %lu %u %u", 4600Sstevel@tonic-gate kpd, vbase, len, flags, 1); 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate } else if ((flags & KPD_NO_ANON) == 0) { 4633247Sgjelinek if (anon_resv_zone(SEGKP_MAPLEN(len, flags), NULL) == 0) { 4640Sstevel@tonic-gate if (flags & KPD_LOCKED) { 4650Sstevel@tonic-gate atomic_add_long(&anon_segkp_pages_locked, 4660Sstevel@tonic-gate -pages); 4670Sstevel@tonic-gate page_unresv(pages); 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate vmem_free(SEGKP_VMEM(seg), vbase, len); 4700Sstevel@tonic-gate kmem_free(kpd, sizeof (struct segkp_data)); 4710Sstevel@tonic-gate return (NULL); 4720Sstevel@tonic-gate } 4733247Sgjelinek atomic_add_long(&anon_segkp_pages_resv, 4743247Sgjelinek btop(SEGKP_MAPLEN(len, flags))); 4750Sstevel@tonic-gate anon_idx = ((uintptr_t)(vbase - s_base)) >> PAGESHIFT; 4760Sstevel@tonic-gate kpd->kp_anon_idx = anon_idx; 4770Sstevel@tonic-gate kpd->kp_anon = kpsd->kpsd_anon; 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate TRACE_5(TR_FAC_VM, TR_ANON_SEGKP, "anon segkp:%p %p %lu %u %u", 4800Sstevel@tonic-gate kpd, vbase, len, flags, 1); 4810Sstevel@tonic-gate } else { 4820Sstevel@tonic-gate kpd->kp_anon = NULL; 4830Sstevel@tonic-gate kpd->kp_anon_idx = 0; 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* 4870Sstevel@tonic-gate * Allocate page and anon resources for the virtual address range 4880Sstevel@tonic-gate * except the redzone 4890Sstevel@tonic-gate */ 4900Sstevel@tonic-gate if (segkp_fromheap) 4910Sstevel@tonic-gate segkpindex = btop((uintptr_t)(vbase - kvseg.s_base)); 4920Sstevel@tonic-gate for (i = 0, va = vbase; i < np; i++, va += PAGESIZE) { 4930Sstevel@tonic-gate page_t *pl[2]; 4940Sstevel@tonic-gate struct vnode *vp; 4950Sstevel@tonic-gate anoff_t off; 4960Sstevel@tonic-gate int err; 4970Sstevel@tonic-gate page_t *pp = NULL; 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate /* 5000Sstevel@tonic-gate * Mark this page to be a segkp page in the bitmap. 5010Sstevel@tonic-gate */ 5020Sstevel@tonic-gate if (segkp_fromheap) { 5030Sstevel@tonic-gate BT_ATOMIC_SET(segkp_bitmap, segkpindex); 5040Sstevel@tonic-gate segkpindex++; 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* 5080Sstevel@tonic-gate * If this page is the red zone page, we don't need swap 5090Sstevel@tonic-gate * space for it. Note that we skip over the code that 5100Sstevel@tonic-gate * establishes MMU mappings, so that the page remains 5110Sstevel@tonic-gate * invalid. 5120Sstevel@tonic-gate */ 5130Sstevel@tonic-gate if ((flags & KPD_HASREDZONE) && KPD_REDZONE(kpd) == i) 5140Sstevel@tonic-gate continue; 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate if (kpd->kp_anon != NULL) { 5170Sstevel@tonic-gate struct anon *ap; 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate ASSERT(anon_get_ptr(kpd->kp_anon, anon_idx + i) 5200Sstevel@tonic-gate == NULL); 5210Sstevel@tonic-gate /* 5220Sstevel@tonic-gate * Determine the "vp" and "off" of the anon slot. 5230Sstevel@tonic-gate */ 5240Sstevel@tonic-gate ap = anon_alloc(NULL, 0); 5250Sstevel@tonic-gate if (amp != NULL) 5260Sstevel@tonic-gate ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER); 5270Sstevel@tonic-gate (void) anon_set_ptr(kpd->kp_anon, anon_idx + i, 5280Sstevel@tonic-gate ap, ANON_SLEEP); 5290Sstevel@tonic-gate if (amp != NULL) 5300Sstevel@tonic-gate ANON_LOCK_EXIT(&->a_rwlock); 5310Sstevel@tonic-gate swap_xlate(ap, &vp, &off); 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate /* 5340Sstevel@tonic-gate * Create a page with the specified identity. The 5350Sstevel@tonic-gate * page is returned with the "shared" lock held. 5360Sstevel@tonic-gate */ 5370Sstevel@tonic-gate err = VOP_GETPAGE(vp, (offset_t)off, PAGESIZE, 5380Sstevel@tonic-gate NULL, pl, PAGESIZE, seg, va, S_CREATE, 539*5331Samw kcred, NULL); 5400Sstevel@tonic-gate if (err) { 5410Sstevel@tonic-gate /* 5420Sstevel@tonic-gate * XXX - This should not fail. 5430Sstevel@tonic-gate */ 5440Sstevel@tonic-gate panic("segkp_get: no pages"); 5450Sstevel@tonic-gate /*NOTREACHED*/ 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate pp = pl[0]; 5480Sstevel@tonic-gate } else { 5490Sstevel@tonic-gate ASSERT(page_exists(&kvp, 5500Sstevel@tonic-gate (u_offset_t)(uintptr_t)va) == NULL); 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate if ((pp = page_create_va(&kvp, 5530Sstevel@tonic-gate (u_offset_t)(uintptr_t)va, PAGESIZE, 5540Sstevel@tonic-gate (flags & KPD_NOWAIT ? 0 : PG_WAIT) | PG_EXCL | 5550Sstevel@tonic-gate PG_NORELOC, seg, va)) == NULL) { 5560Sstevel@tonic-gate /* 5570Sstevel@tonic-gate * Legitimize resource; then destroy it. 5580Sstevel@tonic-gate * Easier than trying to unwind here. 5590Sstevel@tonic-gate */ 5600Sstevel@tonic-gate kpd->kp_flags = flags; 5610Sstevel@tonic-gate kpd->kp_base = vbase; 5620Sstevel@tonic-gate kpd->kp_len = len; 5630Sstevel@tonic-gate segkp_release_internal(seg, kpd, va - vbase); 5640Sstevel@tonic-gate return (NULL); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate page_io_unlock(pp); 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate if (flags & KPD_ZERO) 5700Sstevel@tonic-gate pagezero(pp, 0, PAGESIZE); 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate /* 5730Sstevel@tonic-gate * Load and lock an MMU translation for the page. 5740Sstevel@tonic-gate */ 5750Sstevel@tonic-gate hat_memload(seg->s_as->a_hat, va, pp, (PROT_READ|PROT_WRITE), 5760Sstevel@tonic-gate ((flags & KPD_LOCKED) ? HAT_LOAD_LOCK : HAT_LOAD)); 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate /* 5790Sstevel@tonic-gate * Now, release lock on the page. 5800Sstevel@tonic-gate */ 5810Sstevel@tonic-gate if (flags & KPD_LOCKED) 5820Sstevel@tonic-gate page_downgrade(pp); 5830Sstevel@tonic-gate else 5840Sstevel@tonic-gate page_unlock(pp); 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate kpd->kp_flags = flags; 5880Sstevel@tonic-gate kpd->kp_base = vbase; 5890Sstevel@tonic-gate kpd->kp_len = len; 5900Sstevel@tonic-gate segkp_insert(seg, kpd); 5910Sstevel@tonic-gate *tkpd = kpd; 5920Sstevel@tonic-gate return (stom(kpd->kp_base, flags)); 5930Sstevel@tonic-gate } 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate /* 5960Sstevel@tonic-gate * Release the resource to cache if the pool(designate by the cookie) 5970Sstevel@tonic-gate * has less than the maximum allowable. If inserted in cache, 5980Sstevel@tonic-gate * segkp_delete insures element is taken off of active list. 5990Sstevel@tonic-gate */ 6000Sstevel@tonic-gate void 6010Sstevel@tonic-gate segkp_release(struct seg *seg, caddr_t vaddr) 6020Sstevel@tonic-gate { 6030Sstevel@tonic-gate struct segkp_cache *freelist; 6040Sstevel@tonic-gate struct segkp_data *kpd = NULL; 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate if ((kpd = segkp_find(seg, vaddr)) == NULL) { 6070Sstevel@tonic-gate panic("segkp_release: null kpd"); 6080Sstevel@tonic-gate /*NOTREACHED*/ 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate if (kpd->kp_cookie != -1) { 6120Sstevel@tonic-gate freelist = &segkp_cache[kpd->kp_cookie]; 6130Sstevel@tonic-gate mutex_enter(&segkp_lock); 6140Sstevel@tonic-gate if (!segkp_indel && freelist->kpf_count < freelist->kpf_max) { 6150Sstevel@tonic-gate segkp_delete(seg, kpd); 6160Sstevel@tonic-gate kpd->kp_next = freelist->kpf_list; 6170Sstevel@tonic-gate freelist->kpf_list = kpd; 6180Sstevel@tonic-gate freelist->kpf_count++; 6190Sstevel@tonic-gate mutex_exit(&segkp_lock); 6200Sstevel@tonic-gate return; 6210Sstevel@tonic-gate } else { 6220Sstevel@tonic-gate mutex_exit(&segkp_lock); 6230Sstevel@tonic-gate kpd->kp_cookie = -1; 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate segkp_release_internal(seg, kpd, kpd->kp_len); 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate /* 6300Sstevel@tonic-gate * Free the entire resource. segkp_unlock gets called with the start of the 6310Sstevel@tonic-gate * mapped portion of the resource. The length is the size of the mapped 6320Sstevel@tonic-gate * portion 6330Sstevel@tonic-gate */ 6340Sstevel@tonic-gate static void 6350Sstevel@tonic-gate segkp_release_internal(struct seg *seg, struct segkp_data *kpd, size_t len) 6360Sstevel@tonic-gate { 6370Sstevel@tonic-gate caddr_t va; 6380Sstevel@tonic-gate long i; 6390Sstevel@tonic-gate long redzone; 6400Sstevel@tonic-gate size_t np; 6410Sstevel@tonic-gate page_t *pp; 6420Sstevel@tonic-gate struct vnode *vp; 6430Sstevel@tonic-gate anoff_t off; 6440Sstevel@tonic-gate struct anon *ap; 6450Sstevel@tonic-gate pgcnt_t segkpindex; 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate ASSERT(kpd != NULL); 6480Sstevel@tonic-gate ASSERT((kpd->kp_flags & KPD_HASAMP) == 0 || kpd->kp_cookie == -1); 6490Sstevel@tonic-gate np = btop(len); 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate /* Remove from active hash list */ 6520Sstevel@tonic-gate if (kpd->kp_cookie == -1) { 6530Sstevel@tonic-gate mutex_enter(&segkp_lock); 6540Sstevel@tonic-gate segkp_delete(seg, kpd); 6550Sstevel@tonic-gate mutex_exit(&segkp_lock); 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate /* 6590Sstevel@tonic-gate * Precompute redzone page index. 6600Sstevel@tonic-gate */ 6610Sstevel@tonic-gate redzone = -1; 6620Sstevel@tonic-gate if (kpd->kp_flags & KPD_HASREDZONE) 6630Sstevel@tonic-gate redzone = KPD_REDZONE(kpd); 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate va = kpd->kp_base; 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate hat_unload(seg->s_as->a_hat, va, (np << PAGESHIFT), 6690Sstevel@tonic-gate ((kpd->kp_flags & KPD_LOCKED) ? HAT_UNLOAD_UNLOCK : HAT_UNLOAD)); 6700Sstevel@tonic-gate /* 6710Sstevel@tonic-gate * Free up those anon resources that are quiescent. 6720Sstevel@tonic-gate */ 6730Sstevel@tonic-gate if (segkp_fromheap) 6740Sstevel@tonic-gate segkpindex = btop((uintptr_t)(va - kvseg.s_base)); 6750Sstevel@tonic-gate for (i = 0; i < np; i++, va += PAGESIZE) { 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate /* 6780Sstevel@tonic-gate * Clear the bit for this page from the bitmap. 6790Sstevel@tonic-gate */ 6800Sstevel@tonic-gate if (segkp_fromheap) { 6810Sstevel@tonic-gate BT_ATOMIC_CLEAR(segkp_bitmap, segkpindex); 6820Sstevel@tonic-gate segkpindex++; 6830Sstevel@tonic-gate } 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate if (i == redzone) 6860Sstevel@tonic-gate continue; 6870Sstevel@tonic-gate if (kpd->kp_anon) { 6880Sstevel@tonic-gate /* 6890Sstevel@tonic-gate * Free up anon resources and destroy the 6900Sstevel@tonic-gate * associated pages. 6910Sstevel@tonic-gate * 6920Sstevel@tonic-gate * Release the lock if there is one. Have to get the 6930Sstevel@tonic-gate * page to do this, unfortunately. 6940Sstevel@tonic-gate */ 6950Sstevel@tonic-gate if (kpd->kp_flags & KPD_LOCKED) { 6960Sstevel@tonic-gate ap = anon_get_ptr(kpd->kp_anon, 6970Sstevel@tonic-gate kpd->kp_anon_idx + i); 6980Sstevel@tonic-gate swap_xlate(ap, &vp, &off); 6990Sstevel@tonic-gate /* Find the shared-locked page. */ 7000Sstevel@tonic-gate pp = page_find(vp, (u_offset_t)off); 7010Sstevel@tonic-gate if (pp == NULL) { 7020Sstevel@tonic-gate panic("segkp_release: " 7030Sstevel@tonic-gate "kp_anon: no page to unlock "); 7040Sstevel@tonic-gate /*NOTREACHED*/ 7050Sstevel@tonic-gate } 7060Sstevel@tonic-gate page_unlock(pp); 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate if ((kpd->kp_flags & KPD_HASAMP) == 0) { 7090Sstevel@tonic-gate anon_free(kpd->kp_anon, kpd->kp_anon_idx + i, 7100Sstevel@tonic-gate PAGESIZE); 7113247Sgjelinek anon_unresv_zone(PAGESIZE, NULL); 7123247Sgjelinek atomic_add_long(&anon_segkp_pages_resv, 7133247Sgjelinek -1); 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate TRACE_5(TR_FAC_VM, 7160Sstevel@tonic-gate TR_ANON_SEGKP, "anon segkp:%p %p %lu %u %u", 7170Sstevel@tonic-gate kpd, va, PAGESIZE, 0, 0); 7180Sstevel@tonic-gate } else { 7190Sstevel@tonic-gate if (kpd->kp_flags & KPD_LOCKED) { 7200Sstevel@tonic-gate pp = page_find(&kvp, (u_offset_t)(uintptr_t)va); 7210Sstevel@tonic-gate if (pp == NULL) { 7220Sstevel@tonic-gate panic("segkp_release: " 7230Sstevel@tonic-gate "no page to unlock"); 7240Sstevel@tonic-gate /*NOTREACHED*/ 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate /* 7270Sstevel@tonic-gate * We should just upgrade the lock here 7280Sstevel@tonic-gate * but there is no upgrade that waits. 7290Sstevel@tonic-gate */ 7300Sstevel@tonic-gate page_unlock(pp); 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)va, 7330Sstevel@tonic-gate SE_EXCL); 7340Sstevel@tonic-gate if (pp != NULL) 7350Sstevel@tonic-gate page_destroy(pp, 0); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate /* If locked, release physical memory reservation */ 7400Sstevel@tonic-gate if (kpd->kp_flags & KPD_LOCKED) { 7410Sstevel@tonic-gate pgcnt_t pages = btop(SEGKP_MAPLEN(kpd->kp_len, kpd->kp_flags)); 7420Sstevel@tonic-gate if ((kpd->kp_flags & KPD_NO_ANON) == 0) 7430Sstevel@tonic-gate atomic_add_long(&anon_segkp_pages_locked, -pages); 7440Sstevel@tonic-gate page_unresv(pages); 7450Sstevel@tonic-gate } 7460Sstevel@tonic-gate 7470Sstevel@tonic-gate vmem_free(SEGKP_VMEM(seg), kpd->kp_base, kpd->kp_len); 7480Sstevel@tonic-gate kmem_free(kpd, sizeof (struct segkp_data)); 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate /* 7520Sstevel@tonic-gate * segkp_map_red() will check the current frame pointer against the 7530Sstevel@tonic-gate * stack base. If the amount of stack remaining is questionable 7540Sstevel@tonic-gate * (less than red_minavail), then segkp_map_red() will map in the redzone 7550Sstevel@tonic-gate * and return 1. Otherwise, it will return 0. segkp_map_red() can 7560Sstevel@tonic-gate * _only_ be called when: 7570Sstevel@tonic-gate * 7580Sstevel@tonic-gate * - it is safe to sleep on page_create_va(). 7590Sstevel@tonic-gate * - the caller is non-swappable. 7600Sstevel@tonic-gate * 7610Sstevel@tonic-gate * It is up to the caller to remember whether segkp_map_red() successfully 7620Sstevel@tonic-gate * mapped the redzone, and, if so, to call segkp_unmap_red() at a later 7630Sstevel@tonic-gate * time. Note that the caller must _remain_ non-swappable until after 7640Sstevel@tonic-gate * calling segkp_unmap_red(). 7650Sstevel@tonic-gate * 7660Sstevel@tonic-gate * Currently, this routine is only called from pagefault() (which necessarily 7670Sstevel@tonic-gate * satisfies the above conditions). 7680Sstevel@tonic-gate */ 7690Sstevel@tonic-gate #if defined(STACK_GROWTH_DOWN) 7700Sstevel@tonic-gate int 7710Sstevel@tonic-gate segkp_map_red(void) 7720Sstevel@tonic-gate { 7730Sstevel@tonic-gate uintptr_t fp = STACK_BIAS + (uintptr_t)getfp(); 7740Sstevel@tonic-gate #ifndef _LP64 7750Sstevel@tonic-gate caddr_t stkbase; 7760Sstevel@tonic-gate #endif 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate ASSERT(curthread->t_schedflag & TS_DONT_SWAP); 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate /* 7810Sstevel@tonic-gate * Optimize for the common case where we simply return. 7820Sstevel@tonic-gate */ 7830Sstevel@tonic-gate if ((curthread->t_red_pp == NULL) && 7840Sstevel@tonic-gate (fp - (uintptr_t)curthread->t_stkbase >= red_minavail)) 7850Sstevel@tonic-gate return (0); 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate #if defined(_LP64) 7880Sstevel@tonic-gate /* 7890Sstevel@tonic-gate * XXX We probably need something better than this. 7900Sstevel@tonic-gate */ 7910Sstevel@tonic-gate panic("kernel stack overflow"); 7920Sstevel@tonic-gate /*NOTREACHED*/ 7930Sstevel@tonic-gate #else /* _LP64 */ 7940Sstevel@tonic-gate if (curthread->t_red_pp == NULL) { 7950Sstevel@tonic-gate page_t *red_pp; 7960Sstevel@tonic-gate struct seg kseg; 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate caddr_t red_va = (caddr_t) 7990Sstevel@tonic-gate (((uintptr_t)curthread->t_stkbase & (uintptr_t)PAGEMASK) - 8000Sstevel@tonic-gate PAGESIZE); 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate ASSERT(page_exists(&kvp, (u_offset_t)(uintptr_t)red_va) == 8030Sstevel@tonic-gate NULL); 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate /* 8060Sstevel@tonic-gate * Allocate the physical for the red page. 8070Sstevel@tonic-gate */ 8080Sstevel@tonic-gate /* 8090Sstevel@tonic-gate * No PG_NORELOC here to avoid waits. Unlikely to get 8100Sstevel@tonic-gate * a relocate happening in the short time the page exists 8110Sstevel@tonic-gate * and it will be OK anyway. 8120Sstevel@tonic-gate */ 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate kseg.s_as = &kas; 8150Sstevel@tonic-gate red_pp = page_create_va(&kvp, (u_offset_t)(uintptr_t)red_va, 8160Sstevel@tonic-gate PAGESIZE, PG_WAIT | PG_EXCL, &kseg, red_va); 8170Sstevel@tonic-gate ASSERT(red_pp != NULL); 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate /* 8200Sstevel@tonic-gate * So we now have a page to jam into the redzone... 8210Sstevel@tonic-gate */ 8220Sstevel@tonic-gate page_io_unlock(red_pp); 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate hat_memload(kas.a_hat, red_va, red_pp, 8250Sstevel@tonic-gate (PROT_READ|PROT_WRITE), HAT_LOAD_LOCK); 8260Sstevel@tonic-gate page_downgrade(red_pp); 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate /* 8290Sstevel@tonic-gate * The page is left SE_SHARED locked so we can hold on to 8300Sstevel@tonic-gate * the page_t pointer. 8310Sstevel@tonic-gate */ 8320Sstevel@tonic-gate curthread->t_red_pp = red_pp; 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate atomic_add_32(&red_nmapped, 1); 8350Sstevel@tonic-gate while (fp - (uintptr_t)curthread->t_stkbase < red_closest) { 8360Sstevel@tonic-gate (void) cas32(&red_closest, red_closest, 8370Sstevel@tonic-gate (uint32_t)(fp - (uintptr_t)curthread->t_stkbase)); 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate return (1); 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate stkbase = (caddr_t)(((uintptr_t)curthread->t_stkbase & 8430Sstevel@tonic-gate (uintptr_t)PAGEMASK) - PAGESIZE); 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate atomic_add_32(&red_ndoubles, 1); 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate if (fp - (uintptr_t)stkbase < RED_DEEP_THRESHOLD) { 8480Sstevel@tonic-gate /* 8490Sstevel@tonic-gate * Oh boy. We're already deep within the mapped-in 8500Sstevel@tonic-gate * redzone page, and the caller is trying to prepare 8510Sstevel@tonic-gate * for a deep stack run. We're running without a 8520Sstevel@tonic-gate * redzone right now: if the caller plows off the 8530Sstevel@tonic-gate * end of the stack, it'll plow another thread or 8540Sstevel@tonic-gate * LWP structure. That situation could result in 8550Sstevel@tonic-gate * a very hard-to-debug panic, so, in the spirit of 8560Sstevel@tonic-gate * recording the name of one's killer in one's own 8570Sstevel@tonic-gate * blood, we're going to record lbolt and the calling 8580Sstevel@tonic-gate * thread. 8590Sstevel@tonic-gate */ 8600Sstevel@tonic-gate red_deep_lbolt = lbolt; 8610Sstevel@tonic-gate red_deep_thread = curthread; 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate /* 8650Sstevel@tonic-gate * If this is a DEBUG kernel, and we've run too deep for comfort, toss. 8660Sstevel@tonic-gate */ 8670Sstevel@tonic-gate ASSERT(fp - (uintptr_t)stkbase >= RED_DEEP_THRESHOLD); 8680Sstevel@tonic-gate return (0); 8690Sstevel@tonic-gate #endif /* _LP64 */ 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate void 8730Sstevel@tonic-gate segkp_unmap_red(void) 8740Sstevel@tonic-gate { 8750Sstevel@tonic-gate page_t *pp; 8760Sstevel@tonic-gate caddr_t red_va = (caddr_t)(((uintptr_t)curthread->t_stkbase & 8770Sstevel@tonic-gate (uintptr_t)PAGEMASK) - PAGESIZE); 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate ASSERT(curthread->t_red_pp != NULL); 8800Sstevel@tonic-gate ASSERT(curthread->t_schedflag & TS_DONT_SWAP); 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate /* 8830Sstevel@tonic-gate * Because we locked the mapping down, we can't simply rely 8840Sstevel@tonic-gate * on page_destroy() to clean everything up; we need to call 8850Sstevel@tonic-gate * hat_unload() to explicitly unlock the mapping resources. 8860Sstevel@tonic-gate */ 8870Sstevel@tonic-gate hat_unload(kas.a_hat, red_va, PAGESIZE, HAT_UNLOAD_UNLOCK); 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate pp = curthread->t_red_pp; 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate ASSERT(pp == page_find(&kvp, (u_offset_t)(uintptr_t)red_va)); 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate /* 8940Sstevel@tonic-gate * Need to upgrade the SE_SHARED lock to SE_EXCL. 8950Sstevel@tonic-gate */ 8960Sstevel@tonic-gate if (!page_tryupgrade(pp)) { 8970Sstevel@tonic-gate /* 8980Sstevel@tonic-gate * As there is now wait for upgrade, release the 8990Sstevel@tonic-gate * SE_SHARED lock and wait for SE_EXCL. 9000Sstevel@tonic-gate */ 9010Sstevel@tonic-gate page_unlock(pp); 9020Sstevel@tonic-gate pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)red_va, SE_EXCL); 9030Sstevel@tonic-gate /* pp may be NULL here, hence the test below */ 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate /* 9070Sstevel@tonic-gate * Destroy the page, with dontfree set to zero (i.e. free it). 9080Sstevel@tonic-gate */ 9090Sstevel@tonic-gate if (pp != NULL) 9100Sstevel@tonic-gate page_destroy(pp, 0); 9110Sstevel@tonic-gate curthread->t_red_pp = NULL; 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate #else 9140Sstevel@tonic-gate #error Red stacks only supported with downwards stack growth. 9150Sstevel@tonic-gate #endif 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate /* 9180Sstevel@tonic-gate * Handle a fault on an address corresponding to one of the 9190Sstevel@tonic-gate * resources in the segkp segment. 9200Sstevel@tonic-gate */ 9210Sstevel@tonic-gate faultcode_t 9220Sstevel@tonic-gate segkp_fault( 9230Sstevel@tonic-gate struct hat *hat, 9240Sstevel@tonic-gate struct seg *seg, 9250Sstevel@tonic-gate caddr_t vaddr, 9260Sstevel@tonic-gate size_t len, 9270Sstevel@tonic-gate enum fault_type type, 9280Sstevel@tonic-gate enum seg_rw rw) 9290Sstevel@tonic-gate { 9300Sstevel@tonic-gate struct segkp_data *kpd = NULL; 9310Sstevel@tonic-gate int err; 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate ASSERT(seg->s_as == &kas && RW_READ_HELD(&seg->s_as->a_lock)); 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate /* 9360Sstevel@tonic-gate * Sanity checks. 9370Sstevel@tonic-gate */ 9380Sstevel@tonic-gate if (type == F_PROT) { 9390Sstevel@tonic-gate panic("segkp_fault: unexpected F_PROT fault"); 9400Sstevel@tonic-gate /*NOTREACHED*/ 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate if ((kpd = segkp_find(seg, vaddr)) == NULL) 9440Sstevel@tonic-gate return (FC_NOMAP); 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate mutex_enter(&kpd->kp_lock); 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate if (type == F_SOFTLOCK) { 9490Sstevel@tonic-gate ASSERT(!(kpd->kp_flags & KPD_LOCKED)); 9500Sstevel@tonic-gate /* 9510Sstevel@tonic-gate * The F_SOFTLOCK case has more stringent 9520Sstevel@tonic-gate * range requirements: the given range must exactly coincide 9530Sstevel@tonic-gate * with the resource's mapped portion. Note reference to 9540Sstevel@tonic-gate * redzone is handled since vaddr would not equal base 9550Sstevel@tonic-gate */ 9560Sstevel@tonic-gate if (vaddr != stom(kpd->kp_base, kpd->kp_flags) || 9570Sstevel@tonic-gate len != SEGKP_MAPLEN(kpd->kp_len, kpd->kp_flags)) { 9580Sstevel@tonic-gate mutex_exit(&kpd->kp_lock); 9590Sstevel@tonic-gate return (FC_MAKE_ERR(EFAULT)); 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate if ((err = segkp_load(hat, seg, vaddr, len, kpd, KPD_LOCKED))) { 9630Sstevel@tonic-gate mutex_exit(&kpd->kp_lock); 9640Sstevel@tonic-gate return (FC_MAKE_ERR(err)); 9650Sstevel@tonic-gate } 9660Sstevel@tonic-gate kpd->kp_flags |= KPD_LOCKED; 9670Sstevel@tonic-gate mutex_exit(&kpd->kp_lock); 9680Sstevel@tonic-gate return (0); 9690Sstevel@tonic-gate } 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate if (type == F_INVAL) { 9720Sstevel@tonic-gate ASSERT(!(kpd->kp_flags & KPD_NO_ANON)); 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate /* 9750Sstevel@tonic-gate * Check if we touched the redzone. Somewhat optimistic 9760Sstevel@tonic-gate * here if we are touching the redzone of our own stack 9770Sstevel@tonic-gate * since we wouldn't have a stack to get this far... 9780Sstevel@tonic-gate */ 9790Sstevel@tonic-gate if ((kpd->kp_flags & KPD_HASREDZONE) && 9800Sstevel@tonic-gate btop((uintptr_t)(vaddr - kpd->kp_base)) == KPD_REDZONE(kpd)) 9810Sstevel@tonic-gate panic("segkp_fault: accessing redzone"); 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate /* 9840Sstevel@tonic-gate * This fault may occur while the page is being F_SOFTLOCK'ed. 9850Sstevel@tonic-gate * Return since a 2nd segkp_load is unnecessary and also would 9860Sstevel@tonic-gate * result in the page being locked twice and eventually 9870Sstevel@tonic-gate * hang the thread_reaper thread. 9880Sstevel@tonic-gate */ 9890Sstevel@tonic-gate if (kpd->kp_flags & KPD_LOCKED) { 9900Sstevel@tonic-gate mutex_exit(&kpd->kp_lock); 9910Sstevel@tonic-gate return (0); 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate err = segkp_load(hat, seg, vaddr, len, kpd, kpd->kp_flags); 9950Sstevel@tonic-gate mutex_exit(&kpd->kp_lock); 9960Sstevel@tonic-gate return (err ? FC_MAKE_ERR(err) : 0); 9970Sstevel@tonic-gate } 9980Sstevel@tonic-gate 9990Sstevel@tonic-gate if (type == F_SOFTUNLOCK) { 10000Sstevel@tonic-gate uint_t flags; 10010Sstevel@tonic-gate 10020Sstevel@tonic-gate /* 10030Sstevel@tonic-gate * Make sure the addr is LOCKED and it has anon backing 10040Sstevel@tonic-gate * before unlocking 10050Sstevel@tonic-gate */ 10060Sstevel@tonic-gate if ((kpd->kp_flags & (KPD_LOCKED|KPD_NO_ANON)) == KPD_NO_ANON) { 10070Sstevel@tonic-gate panic("segkp_fault: bad unlock"); 10080Sstevel@tonic-gate /*NOTREACHED*/ 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate if (vaddr != stom(kpd->kp_base, kpd->kp_flags) || 10120Sstevel@tonic-gate len != SEGKP_MAPLEN(kpd->kp_len, kpd->kp_flags)) { 10130Sstevel@tonic-gate panic("segkp_fault: bad range"); 10140Sstevel@tonic-gate /*NOTREACHED*/ 10150Sstevel@tonic-gate } 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate if (rw == S_WRITE) 10180Sstevel@tonic-gate flags = kpd->kp_flags | KPD_WRITEDIRTY; 10190Sstevel@tonic-gate else 10200Sstevel@tonic-gate flags = kpd->kp_flags; 10210Sstevel@tonic-gate err = segkp_unlock(hat, seg, vaddr, len, kpd, flags); 10220Sstevel@tonic-gate kpd->kp_flags &= ~KPD_LOCKED; 10230Sstevel@tonic-gate mutex_exit(&kpd->kp_lock); 10240Sstevel@tonic-gate return (err ? FC_MAKE_ERR(err) : 0); 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate mutex_exit(&kpd->kp_lock); 10270Sstevel@tonic-gate panic("segkp_fault: bogus fault type: %d\n", type); 10280Sstevel@tonic-gate /*NOTREACHED*/ 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate /* 10320Sstevel@tonic-gate * Check that the given protections suffice over the range specified by 10330Sstevel@tonic-gate * vaddr and len. For this segment type, the only issue is whether or 10340Sstevel@tonic-gate * not the range lies completely within the mapped part of an allocated 10350Sstevel@tonic-gate * resource. 10360Sstevel@tonic-gate */ 10370Sstevel@tonic-gate /* ARGSUSED */ 10380Sstevel@tonic-gate static int 10390Sstevel@tonic-gate segkp_checkprot(struct seg *seg, caddr_t vaddr, size_t len, uint_t prot) 10400Sstevel@tonic-gate { 10410Sstevel@tonic-gate struct segkp_data *kpd = NULL; 10420Sstevel@tonic-gate caddr_t mbase; 10430Sstevel@tonic-gate size_t mlen; 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate if ((kpd = segkp_find(seg, vaddr)) == NULL) 10460Sstevel@tonic-gate return (EACCES); 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate mutex_enter(&kpd->kp_lock); 10490Sstevel@tonic-gate mbase = stom(kpd->kp_base, kpd->kp_flags); 10500Sstevel@tonic-gate mlen = SEGKP_MAPLEN(kpd->kp_len, kpd->kp_flags); 10510Sstevel@tonic-gate if (len > mlen || vaddr < mbase || 10520Sstevel@tonic-gate ((vaddr + len) > (mbase + mlen))) { 10530Sstevel@tonic-gate mutex_exit(&kpd->kp_lock); 10540Sstevel@tonic-gate return (EACCES); 10550Sstevel@tonic-gate } 10560Sstevel@tonic-gate mutex_exit(&kpd->kp_lock); 10570Sstevel@tonic-gate return (0); 10580Sstevel@tonic-gate } 10590Sstevel@tonic-gate 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate /* 10620Sstevel@tonic-gate * Check to see if it makes sense to do kluster/read ahead to 10630Sstevel@tonic-gate * addr + delta relative to the mapping at addr. We assume here 10640Sstevel@tonic-gate * that delta is a signed PAGESIZE'd multiple (which can be negative). 10650Sstevel@tonic-gate * 10660Sstevel@tonic-gate * For seg_u we always "approve" of this action from our standpoint. 10670Sstevel@tonic-gate */ 10680Sstevel@tonic-gate /*ARGSUSED*/ 10690Sstevel@tonic-gate static int 10700Sstevel@tonic-gate segkp_kluster(struct seg *seg, caddr_t addr, ssize_t delta) 10710Sstevel@tonic-gate { 10720Sstevel@tonic-gate return (0); 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate /* 10760Sstevel@tonic-gate * Load and possibly lock intra-slot resources in the range given by 10770Sstevel@tonic-gate * vaddr and len. 10780Sstevel@tonic-gate */ 10790Sstevel@tonic-gate static int 10800Sstevel@tonic-gate segkp_load( 10810Sstevel@tonic-gate struct hat *hat, 10820Sstevel@tonic-gate struct seg *seg, 10830Sstevel@tonic-gate caddr_t vaddr, 10840Sstevel@tonic-gate size_t len, 10850Sstevel@tonic-gate struct segkp_data *kpd, 10860Sstevel@tonic-gate uint_t flags) 10870Sstevel@tonic-gate { 10880Sstevel@tonic-gate caddr_t va; 10890Sstevel@tonic-gate caddr_t vlim; 10900Sstevel@tonic-gate ulong_t i; 10910Sstevel@tonic-gate uint_t lock; 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate ASSERT(MUTEX_HELD(&kpd->kp_lock)); 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate len = P2ROUNDUP(len, PAGESIZE); 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate /* If locking, reserve physical memory */ 10980Sstevel@tonic-gate if (flags & KPD_LOCKED) { 10990Sstevel@tonic-gate pgcnt_t pages = btop(len); 11000Sstevel@tonic-gate if ((kpd->kp_flags & KPD_NO_ANON) == 0) 11010Sstevel@tonic-gate atomic_add_long(&anon_segkp_pages_locked, pages); 11020Sstevel@tonic-gate (void) page_resv(pages, KM_SLEEP); 11030Sstevel@tonic-gate } 11040Sstevel@tonic-gate 11050Sstevel@tonic-gate /* 11060Sstevel@tonic-gate * Loop through the pages in the given range. 11070Sstevel@tonic-gate */ 11080Sstevel@tonic-gate va = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK); 11090Sstevel@tonic-gate vaddr = va; 11100Sstevel@tonic-gate vlim = va + len; 11110Sstevel@tonic-gate lock = flags & KPD_LOCKED; 11120Sstevel@tonic-gate i = ((uintptr_t)(va - kpd->kp_base)) >> PAGESHIFT; 11130Sstevel@tonic-gate for (; va < vlim; va += PAGESIZE, i++) { 11140Sstevel@tonic-gate page_t *pl[2]; /* second element NULL terminator */ 11150Sstevel@tonic-gate struct vnode *vp; 11160Sstevel@tonic-gate anoff_t off; 11170Sstevel@tonic-gate int err; 11180Sstevel@tonic-gate struct anon *ap; 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate /* 11210Sstevel@tonic-gate * Summon the page. If it's not resident, arrange 11220Sstevel@tonic-gate * for synchronous i/o to pull it in. 11230Sstevel@tonic-gate */ 11240Sstevel@tonic-gate ap = anon_get_ptr(kpd->kp_anon, kpd->kp_anon_idx + i); 11250Sstevel@tonic-gate swap_xlate(ap, &vp, &off); 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate /* 11280Sstevel@tonic-gate * The returned page list will have exactly one entry, 11290Sstevel@tonic-gate * which is returned to us already kept. 11300Sstevel@tonic-gate */ 11310Sstevel@tonic-gate err = VOP_GETPAGE(vp, (offset_t)off, PAGESIZE, NULL, 1132*5331Samw pl, PAGESIZE, seg, va, S_READ, kcred, NULL); 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate if (err) { 11350Sstevel@tonic-gate /* 11360Sstevel@tonic-gate * Back out of what we've done so far. 11370Sstevel@tonic-gate */ 11380Sstevel@tonic-gate (void) segkp_unlock(hat, seg, vaddr, 11390Sstevel@tonic-gate (va - vaddr), kpd, flags); 11400Sstevel@tonic-gate return (err); 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate /* 11440Sstevel@tonic-gate * Load an MMU translation for the page. 11450Sstevel@tonic-gate */ 11460Sstevel@tonic-gate hat_memload(hat, va, pl[0], (PROT_READ|PROT_WRITE), 11470Sstevel@tonic-gate lock ? HAT_LOAD_LOCK : HAT_LOAD); 11480Sstevel@tonic-gate 11490Sstevel@tonic-gate if (!lock) { 11500Sstevel@tonic-gate /* 11510Sstevel@tonic-gate * Now, release "shared" lock on the page. 11520Sstevel@tonic-gate */ 11530Sstevel@tonic-gate page_unlock(pl[0]); 11540Sstevel@tonic-gate } 11550Sstevel@tonic-gate } 11560Sstevel@tonic-gate return (0); 11570Sstevel@tonic-gate } 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate /* 11600Sstevel@tonic-gate * At the very least unload the mmu-translations and unlock the range if locked 11610Sstevel@tonic-gate * Can be called with the following flag value KPD_WRITEDIRTY which specifies 11620Sstevel@tonic-gate * any dirty pages should be written to disk. 11630Sstevel@tonic-gate */ 11640Sstevel@tonic-gate static int 11650Sstevel@tonic-gate segkp_unlock( 11660Sstevel@tonic-gate struct hat *hat, 11670Sstevel@tonic-gate struct seg *seg, 11680Sstevel@tonic-gate caddr_t vaddr, 11690Sstevel@tonic-gate size_t len, 11700Sstevel@tonic-gate struct segkp_data *kpd, 11710Sstevel@tonic-gate uint_t flags) 11720Sstevel@tonic-gate { 11730Sstevel@tonic-gate caddr_t va; 11740Sstevel@tonic-gate caddr_t vlim; 11750Sstevel@tonic-gate ulong_t i; 11760Sstevel@tonic-gate struct page *pp; 11770Sstevel@tonic-gate struct vnode *vp; 11780Sstevel@tonic-gate anoff_t off; 11790Sstevel@tonic-gate struct anon *ap; 11800Sstevel@tonic-gate 11810Sstevel@tonic-gate #ifdef lint 11820Sstevel@tonic-gate seg = seg; 11830Sstevel@tonic-gate #endif /* lint */ 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate ASSERT(MUTEX_HELD(&kpd->kp_lock)); 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate /* 11880Sstevel@tonic-gate * Loop through the pages in the given range. It is assumed 11890Sstevel@tonic-gate * segkp_unlock is called with page aligned base 11900Sstevel@tonic-gate */ 11910Sstevel@tonic-gate va = vaddr; 11920Sstevel@tonic-gate vlim = va + len; 11930Sstevel@tonic-gate i = ((uintptr_t)(va - kpd->kp_base)) >> PAGESHIFT; 11940Sstevel@tonic-gate hat_unload(hat, va, len, 11950Sstevel@tonic-gate ((flags & KPD_LOCKED) ? HAT_UNLOAD_UNLOCK : HAT_UNLOAD)); 11960Sstevel@tonic-gate for (; va < vlim; va += PAGESIZE, i++) { 11970Sstevel@tonic-gate /* 11980Sstevel@tonic-gate * Find the page associated with this part of the 11990Sstevel@tonic-gate * slot, tracking it down through its associated swap 12000Sstevel@tonic-gate * space. 12010Sstevel@tonic-gate */ 12020Sstevel@tonic-gate ap = anon_get_ptr(kpd->kp_anon, kpd->kp_anon_idx + i); 12030Sstevel@tonic-gate swap_xlate(ap, &vp, &off); 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate if (flags & KPD_LOCKED) { 12060Sstevel@tonic-gate if ((pp = page_find(vp, off)) == NULL) { 12070Sstevel@tonic-gate if (flags & KPD_LOCKED) { 12080Sstevel@tonic-gate panic("segkp_softunlock: missing page"); 12090Sstevel@tonic-gate /*NOTREACHED*/ 12100Sstevel@tonic-gate } 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate } else { 12130Sstevel@tonic-gate /* 12140Sstevel@tonic-gate * Nothing to do if the slot is not locked and the 12150Sstevel@tonic-gate * page doesn't exist. 12160Sstevel@tonic-gate */ 12170Sstevel@tonic-gate if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) 12180Sstevel@tonic-gate continue; 12190Sstevel@tonic-gate } 12200Sstevel@tonic-gate 12210Sstevel@tonic-gate /* 12220Sstevel@tonic-gate * If the page doesn't have any translations, is 12230Sstevel@tonic-gate * dirty and not being shared, then push it out 12240Sstevel@tonic-gate * asynchronously and avoid waiting for the 12250Sstevel@tonic-gate * pageout daemon to do it for us. 12260Sstevel@tonic-gate * 12270Sstevel@tonic-gate * XXX - Do we really need to get the "exclusive" 12280Sstevel@tonic-gate * lock via an upgrade? 12290Sstevel@tonic-gate */ 12300Sstevel@tonic-gate if ((flags & KPD_WRITEDIRTY) && !hat_page_is_mapped(pp) && 12310Sstevel@tonic-gate hat_ismod(pp) && page_tryupgrade(pp)) { 12320Sstevel@tonic-gate /* 12330Sstevel@tonic-gate * Hold the vnode before releasing the page lock to 12340Sstevel@tonic-gate * prevent it from being freed and re-used by some 12350Sstevel@tonic-gate * other thread. 12360Sstevel@tonic-gate */ 12370Sstevel@tonic-gate VN_HOLD(vp); 12380Sstevel@tonic-gate page_unlock(pp); 12390Sstevel@tonic-gate 12400Sstevel@tonic-gate /* 12410Sstevel@tonic-gate * Want most powerful credentials we can get so 12420Sstevel@tonic-gate * use kcred. 12430Sstevel@tonic-gate */ 12440Sstevel@tonic-gate (void) VOP_PUTPAGE(vp, (offset_t)off, PAGESIZE, 1245*5331Samw B_ASYNC | B_FREE, kcred, NULL); 12460Sstevel@tonic-gate VN_RELE(vp); 12470Sstevel@tonic-gate } else { 12480Sstevel@tonic-gate page_unlock(pp); 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate 12520Sstevel@tonic-gate /* If unlocking, release physical memory */ 12530Sstevel@tonic-gate if (flags & KPD_LOCKED) { 12540Sstevel@tonic-gate pgcnt_t pages = btopr(len); 12550Sstevel@tonic-gate if ((kpd->kp_flags & KPD_NO_ANON) == 0) 12560Sstevel@tonic-gate atomic_add_long(&anon_segkp_pages_locked, -pages); 12570Sstevel@tonic-gate page_unresv(pages); 12580Sstevel@tonic-gate } 12590Sstevel@tonic-gate return (0); 12600Sstevel@tonic-gate } 12610Sstevel@tonic-gate 12620Sstevel@tonic-gate /* 12630Sstevel@tonic-gate * Insert the kpd in the hash table. 12640Sstevel@tonic-gate */ 12650Sstevel@tonic-gate static void 12660Sstevel@tonic-gate segkp_insert(struct seg *seg, struct segkp_data *kpd) 12670Sstevel@tonic-gate { 12680Sstevel@tonic-gate struct segkp_segdata *kpsd = (struct segkp_segdata *)seg->s_data; 12690Sstevel@tonic-gate int index; 12700Sstevel@tonic-gate 12710Sstevel@tonic-gate /* 12720Sstevel@tonic-gate * Insert the kpd based on the address that will be returned 12730Sstevel@tonic-gate * via segkp_release. 12740Sstevel@tonic-gate */ 12750Sstevel@tonic-gate index = SEGKP_HASH(stom(kpd->kp_base, kpd->kp_flags)); 12760Sstevel@tonic-gate mutex_enter(&segkp_lock); 12770Sstevel@tonic-gate kpd->kp_next = kpsd->kpsd_hash[index]; 12780Sstevel@tonic-gate kpsd->kpsd_hash[index] = kpd; 12790Sstevel@tonic-gate mutex_exit(&segkp_lock); 12800Sstevel@tonic-gate } 12810Sstevel@tonic-gate 12820Sstevel@tonic-gate /* 12830Sstevel@tonic-gate * Remove kpd from the hash table. 12840Sstevel@tonic-gate */ 12850Sstevel@tonic-gate static void 12860Sstevel@tonic-gate segkp_delete(struct seg *seg, struct segkp_data *kpd) 12870Sstevel@tonic-gate { 12880Sstevel@tonic-gate struct segkp_segdata *kpsd = (struct segkp_segdata *)seg->s_data; 12890Sstevel@tonic-gate struct segkp_data **kpp; 12900Sstevel@tonic-gate int index; 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate ASSERT(MUTEX_HELD(&segkp_lock)); 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate index = SEGKP_HASH(stom(kpd->kp_base, kpd->kp_flags)); 12950Sstevel@tonic-gate for (kpp = &kpsd->kpsd_hash[index]; 12960Sstevel@tonic-gate *kpp != NULL; kpp = &((*kpp)->kp_next)) { 12970Sstevel@tonic-gate if (*kpp == kpd) { 12980Sstevel@tonic-gate *kpp = kpd->kp_next; 12990Sstevel@tonic-gate return; 13000Sstevel@tonic-gate } 13010Sstevel@tonic-gate } 13020Sstevel@tonic-gate panic("segkp_delete: unable to find element to delete"); 13030Sstevel@tonic-gate /*NOTREACHED*/ 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate /* 13070Sstevel@tonic-gate * Find the kpd associated with a vaddr. 13080Sstevel@tonic-gate * 13090Sstevel@tonic-gate * Most of the callers of segkp_find will pass the vaddr that 13100Sstevel@tonic-gate * hashes to the desired index, but there are cases where 13110Sstevel@tonic-gate * this is not true in which case we have to (potentially) scan 13120Sstevel@tonic-gate * the whole table looking for it. This should be very rare 13130Sstevel@tonic-gate * (e.g. a segkp_fault(F_INVAL) on an address somewhere in the 13140Sstevel@tonic-gate * middle of the segkp_data region). 13150Sstevel@tonic-gate */ 13160Sstevel@tonic-gate static struct segkp_data * 13170Sstevel@tonic-gate segkp_find(struct seg *seg, caddr_t vaddr) 13180Sstevel@tonic-gate { 13190Sstevel@tonic-gate struct segkp_segdata *kpsd = (struct segkp_segdata *)seg->s_data; 13200Sstevel@tonic-gate struct segkp_data *kpd; 13210Sstevel@tonic-gate int i; 13220Sstevel@tonic-gate int stop; 13230Sstevel@tonic-gate 13240Sstevel@tonic-gate i = stop = SEGKP_HASH(vaddr); 13250Sstevel@tonic-gate mutex_enter(&segkp_lock); 13260Sstevel@tonic-gate do { 13270Sstevel@tonic-gate for (kpd = kpsd->kpsd_hash[i]; kpd != NULL; 13280Sstevel@tonic-gate kpd = kpd->kp_next) { 13290Sstevel@tonic-gate if (vaddr >= kpd->kp_base && 13300Sstevel@tonic-gate vaddr < kpd->kp_base + kpd->kp_len) { 13310Sstevel@tonic-gate mutex_exit(&segkp_lock); 13320Sstevel@tonic-gate return (kpd); 13330Sstevel@tonic-gate } 13340Sstevel@tonic-gate } 13350Sstevel@tonic-gate if (--i < 0) 13360Sstevel@tonic-gate i = SEGKP_HASHSZ - 1; /* Wrap */ 13370Sstevel@tonic-gate } while (i != stop); 13380Sstevel@tonic-gate mutex_exit(&segkp_lock); 13390Sstevel@tonic-gate return (NULL); /* Not found */ 13400Sstevel@tonic-gate } 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate /* 13430Sstevel@tonic-gate * returns size of swappable area. 13440Sstevel@tonic-gate */ 13450Sstevel@tonic-gate size_t 13460Sstevel@tonic-gate swapsize(caddr_t v) 13470Sstevel@tonic-gate { 13480Sstevel@tonic-gate struct segkp_data *kpd; 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate if ((kpd = segkp_find(segkp, v)) != NULL) 13510Sstevel@tonic-gate return (SEGKP_MAPLEN(kpd->kp_len, kpd->kp_flags)); 13520Sstevel@tonic-gate else 13530Sstevel@tonic-gate return (NULL); 13540Sstevel@tonic-gate } 13550Sstevel@tonic-gate 13560Sstevel@tonic-gate /* 13570Sstevel@tonic-gate * Dump out all the active segkp pages 13580Sstevel@tonic-gate */ 13590Sstevel@tonic-gate static void 13600Sstevel@tonic-gate segkp_dump(struct seg *seg) 13610Sstevel@tonic-gate { 13620Sstevel@tonic-gate int i; 13630Sstevel@tonic-gate struct segkp_data *kpd; 13640Sstevel@tonic-gate struct segkp_segdata *kpsd = (struct segkp_segdata *)seg->s_data; 13650Sstevel@tonic-gate 13660Sstevel@tonic-gate for (i = 0; i < SEGKP_HASHSZ; i++) { 13670Sstevel@tonic-gate for (kpd = kpsd->kpsd_hash[i]; 13680Sstevel@tonic-gate kpd != NULL; kpd = kpd->kp_next) { 13690Sstevel@tonic-gate pfn_t pfn; 13700Sstevel@tonic-gate caddr_t addr; 13710Sstevel@tonic-gate caddr_t eaddr; 13720Sstevel@tonic-gate 13730Sstevel@tonic-gate addr = kpd->kp_base; 13740Sstevel@tonic-gate eaddr = addr + kpd->kp_len; 13750Sstevel@tonic-gate while (addr < eaddr) { 13760Sstevel@tonic-gate ASSERT(seg->s_as == &kas); 13770Sstevel@tonic-gate pfn = hat_getpfnum(seg->s_as->a_hat, addr); 13780Sstevel@tonic-gate if (pfn != PFN_INVALID) 13790Sstevel@tonic-gate dump_addpage(seg->s_as, addr, pfn); 13800Sstevel@tonic-gate addr += PAGESIZE; 13810Sstevel@tonic-gate dump_timeleft = dump_timeout; 13820Sstevel@tonic-gate } 13830Sstevel@tonic-gate } 13840Sstevel@tonic-gate } 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate /*ARGSUSED*/ 13880Sstevel@tonic-gate static int 13890Sstevel@tonic-gate segkp_pagelock(struct seg *seg, caddr_t addr, size_t len, 13900Sstevel@tonic-gate struct page ***ppp, enum lock_type type, enum seg_rw rw) 13910Sstevel@tonic-gate { 13920Sstevel@tonic-gate return (ENOTSUP); 13930Sstevel@tonic-gate } 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate /*ARGSUSED*/ 13960Sstevel@tonic-gate static int 13970Sstevel@tonic-gate segkp_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp) 13980Sstevel@tonic-gate { 13990Sstevel@tonic-gate return (ENODEV); 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate 14020Sstevel@tonic-gate /*ARGSUSED*/ 14030Sstevel@tonic-gate static lgrp_mem_policy_info_t * 14040Sstevel@tonic-gate segkp_getpolicy(struct seg *seg, caddr_t addr) 14050Sstevel@tonic-gate { 14060Sstevel@tonic-gate return (NULL); 14070Sstevel@tonic-gate } 14080Sstevel@tonic-gate 1409670Selowe /*ARGSUSED*/ 1410670Selowe static int 1411670Selowe segkp_capable(struct seg *seg, segcapability_t capability) 1412670Selowe { 1413670Selowe return (0); 1414670Selowe } 1415670Selowe 14160Sstevel@tonic-gate #include <sys/mem_config.h> 14170Sstevel@tonic-gate 14180Sstevel@tonic-gate /*ARGSUSED*/ 14190Sstevel@tonic-gate static void 14200Sstevel@tonic-gate segkp_mem_config_post_add(void *arg, pgcnt_t delta_pages) 14210Sstevel@tonic-gate {} 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate /* 14240Sstevel@tonic-gate * During memory delete, turn off caches so that pages are not held. 14250Sstevel@tonic-gate * A better solution may be to unlock the pages while they are 14260Sstevel@tonic-gate * in the cache so that they may be collected naturally. 14270Sstevel@tonic-gate */ 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate /*ARGSUSED*/ 14300Sstevel@tonic-gate static int 14310Sstevel@tonic-gate segkp_mem_config_pre_del(void *arg, pgcnt_t delta_pages) 14320Sstevel@tonic-gate { 14330Sstevel@tonic-gate atomic_add_32(&segkp_indel, 1); 14340Sstevel@tonic-gate segkp_cache_free(); 14350Sstevel@tonic-gate return (0); 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate /*ARGSUSED*/ 14390Sstevel@tonic-gate static void 14400Sstevel@tonic-gate segkp_mem_config_post_del(void *arg, pgcnt_t delta_pages, int cancelled) 14410Sstevel@tonic-gate { 14420Sstevel@tonic-gate atomic_add_32(&segkp_indel, -1); 14430Sstevel@tonic-gate } 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate static kphysm_setup_vector_t segkp_mem_config_vec = { 14460Sstevel@tonic-gate KPHYSM_SETUP_VECTOR_VERSION, 14470Sstevel@tonic-gate segkp_mem_config_post_add, 14480Sstevel@tonic-gate segkp_mem_config_pre_del, 14490Sstevel@tonic-gate segkp_mem_config_post_del, 14500Sstevel@tonic-gate }; 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate static void 14530Sstevel@tonic-gate segkpinit_mem_config(struct seg *seg) 14540Sstevel@tonic-gate { 14550Sstevel@tonic-gate int ret; 14560Sstevel@tonic-gate 14570Sstevel@tonic-gate ret = kphysm_setup_func_register(&segkp_mem_config_vec, (void *)seg); 14580Sstevel@tonic-gate ASSERT(ret == 0); 14590Sstevel@tonic-gate } 1460