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