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