1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #ifndef _VM_SEG_KP_H 28*0Sstevel@tonic-gate #define _VM_SEG_KP_H 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate /* 33*0Sstevel@tonic-gate * segkp (as in kernel pageable) is a segment driver that supports allocation 34*0Sstevel@tonic-gate * of page-aligned variable size of vm resources. 35*0Sstevel@tonic-gate * 36*0Sstevel@tonic-gate * Each vm resource represents a page-aligned range of virtual addresses. 37*0Sstevel@tonic-gate * The caller may specify whether the resource should include a redzone, 38*0Sstevel@tonic-gate * be locked down, or be zero initialized. 39*0Sstevel@tonic-gate */ 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #include <vm/seg.h> 42*0Sstevel@tonic-gate #include <sys/vmem.h> 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #ifdef __cplusplus 45*0Sstevel@tonic-gate extern "C" { 46*0Sstevel@tonic-gate #endif 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate #ifdef _KERNEL 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate /* 51*0Sstevel@tonic-gate * Private information per overall segkp segment (as opposed 52*0Sstevel@tonic-gate * to per resource within segment). There are as many anon slots 53*0Sstevel@tonic-gate * allocated as there there are pages in the segment. 54*0Sstevel@tonic-gate */ 55*0Sstevel@tonic-gate struct segkp_segdata { 56*0Sstevel@tonic-gate struct anon_hdr *kpsd_anon; /* anon structs */ 57*0Sstevel@tonic-gate vmem_t *kpsd_arena; /* virtual memory descriptor */ 58*0Sstevel@tonic-gate struct segkp_data **kpsd_hash; /* Hash table for lookups */ 59*0Sstevel@tonic-gate }; 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #define SEGKP_VMEM(seg) (((struct segkp_segdata *)(seg)->s_data)->kpsd_arena) 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate /* 64*0Sstevel@tonic-gate * A hash table is used to aid in the lookup of a kpd's based on vaddr. 65*0Sstevel@tonic-gate * Since the heaviest use of segkp occurs from segkp_*get and segkp_*release, 66*0Sstevel@tonic-gate * the hashing is based on the vaddr used by these routines. 67*0Sstevel@tonic-gate */ 68*0Sstevel@tonic-gate #define SEGKP_HASHSZ 256 /* power of two */ 69*0Sstevel@tonic-gate #define SEGKP_HASHMASK (SEGKP_HASHSZ - 1) 70*0Sstevel@tonic-gate #define SEGKP_HASH(vaddr) \ 71*0Sstevel@tonic-gate ((int)(((uintptr_t)vaddr >> PAGESHIFT) & SEGKP_HASHMASK)) 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate struct segkp_data { 74*0Sstevel@tonic-gate kmutex_t kp_lock; /* per resource lock */ 75*0Sstevel@tonic-gate caddr_t kp_base; /* starting addr of chunk */ 76*0Sstevel@tonic-gate size_t kp_len; /* # of bytes */ 77*0Sstevel@tonic-gate uint_t kp_flags; /* state info */ 78*0Sstevel@tonic-gate int kp_cookie; /* index into cache array */ 79*0Sstevel@tonic-gate ulong_t kp_anon_idx; /* index into main anon array */ 80*0Sstevel@tonic-gate /* in segkp_segdata */ 81*0Sstevel@tonic-gate struct anon_hdr *kp_anon; /* anon structs */ 82*0Sstevel@tonic-gate struct segkp_data *kp_next; /* ptr to next in hash chain */ 83*0Sstevel@tonic-gate }; 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate /* 86*0Sstevel@tonic-gate * Flag bits 87*0Sstevel@tonic-gate * 88*0Sstevel@tonic-gate */ 89*0Sstevel@tonic-gate #define KPD_ZERO 0x01 /* initialize resource with 0 */ 90*0Sstevel@tonic-gate #define KPD_LOCKED 0x02 /* resources locked */ 91*0Sstevel@tonic-gate #define KPD_NO_ANON 0x04 /* no swap resources required */ 92*0Sstevel@tonic-gate #define KPD_HASREDZONE 0x08 /* include a redzone */ 93*0Sstevel@tonic-gate #define KPD_NOWAIT 0x10 /* do not wait for res. if unavail. */ 94*0Sstevel@tonic-gate #define KPD_WRITEDIRTY 0x20 /* dirty pages should be flushed */ 95*0Sstevel@tonic-gate #define KPD_HASAMP 0x40 /* anon_hdr managed by caller */ 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate /* 98*0Sstevel@tonic-gate * A cache of segkp elements may be created via segkp_cache_init(). 99*0Sstevel@tonic-gate * The elements on the freelist all have the same len and flags value. 100*0Sstevel@tonic-gate * The cookie passed to the client is an index into the freelist array. 101*0Sstevel@tonic-gate */ 102*0Sstevel@tonic-gate struct segkp_cache { 103*0Sstevel@tonic-gate int kpf_max; /* max # of elements allowed */ 104*0Sstevel@tonic-gate int kpf_count; /* current no. of elments */ 105*0Sstevel@tonic-gate int kpf_inuse; /* list inuse */ 106*0Sstevel@tonic-gate uint_t kpf_flags; /* seg_kp flag value */ 107*0Sstevel@tonic-gate size_t kpf_len; /* len of resource */ 108*0Sstevel@tonic-gate struct seg *kpf_seg; /* segment */ 109*0Sstevel@tonic-gate struct segkp_data *kpf_list; /* list of kpd's */ 110*0Sstevel@tonic-gate }; 111*0Sstevel@tonic-gate #define SEGKP_MAX_CACHE 4 /* Number of caches maintained */ 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate /* 114*0Sstevel@tonic-gate * Define redzone, and stack_to_memory macros. 115*0Sstevel@tonic-gate * The redzone is PAGESIZE bytes. 116*0Sstevel@tonic-gate */ 117*0Sstevel@tonic-gate #ifdef STACK_GROWTH_DOWN 118*0Sstevel@tonic-gate #define KPD_REDZONE(kpd) (0) 119*0Sstevel@tonic-gate #define stom(v, flags) (((flags) & KPD_HASREDZONE) ? (v) + PAGESIZE : (v)) 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate #else /* STACK_GROWTH_DOWN */ 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate #define KPD_REDZONE(kpd) (btop(kpd->kp_len) - 1) 124*0Sstevel@tonic-gate #define stom(v) (v) 125*0Sstevel@tonic-gate #endif /* STACK_GROWTH_DOWN */ 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate #define SEGKP_MAPLEN(len, flags) \ 128*0Sstevel@tonic-gate (((flags) & KPD_HASREDZONE) ? (len) - PAGESIZE : (len)) 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate extern struct seg *segkp; 131*0Sstevel@tonic-gate /* If segkp becomes more than one seg this test will need changing. */ 132*0Sstevel@tonic-gate #define SEG_IS_SEGKP(SEG) ((SEG) == segkp) 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate /* 135*0Sstevel@tonic-gate * Public routine declarations not part of the segment ops vector go here. 136*0Sstevel@tonic-gate */ 137*0Sstevel@tonic-gate int segkp_create(struct seg *seg); 138*0Sstevel@tonic-gate caddr_t segkp_get(struct seg *seg, size_t len, uint_t flags); 139*0Sstevel@tonic-gate void segkp_release(struct seg *seg, caddr_t vaddr); 140*0Sstevel@tonic-gate void * segkp_cache_init(struct seg *seg, int maxsize, size_t len, 141*0Sstevel@tonic-gate uint_t flags); 142*0Sstevel@tonic-gate void segkp_cache_free(); 143*0Sstevel@tonic-gate caddr_t segkp_cache_get(void *cookie); 144*0Sstevel@tonic-gate int segkp_map_red(void); 145*0Sstevel@tonic-gate void segkp_unmap_red(void); 146*0Sstevel@tonic-gate size_t swapsize(caddr_t v); 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate /* Special currently only used by schedctl. */ 149*0Sstevel@tonic-gate struct anon_map; /* Make the compiler happy about the next line. */ 150*0Sstevel@tonic-gate caddr_t segkp_get_withanonmap(struct seg *, size_t, uint_t, struct anon_map *); 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate /* 153*0Sstevel@tonic-gate * We allow explicit calls to segkp_fault, even though it's part 154*0Sstevel@tonic-gate * of the segkp ops vector. 155*0Sstevel@tonic-gate */ 156*0Sstevel@tonic-gate faultcode_t segkp_fault(struct hat *hat, struct seg *seg, caddr_t addr, 157*0Sstevel@tonic-gate size_t len, enum fault_type type, enum seg_rw rw); 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate #endif /* _KERNEL */ 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate #ifdef __cplusplus 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate #endif 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate #endif /* _VM_SEG_KP_H */ 166