xref: /illumos-gate/usr/src/uts/common/vm/seg_kp.h (revision 7c478bd9)
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  */
27*7c478bd9Sstevel@tonic-gate #ifndef	_VM_SEG_KP_H
28*7c478bd9Sstevel@tonic-gate #define	_VM_SEG_KP_H
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * segkp (as in kernel pageable) is a segment driver that supports allocation
34*7c478bd9Sstevel@tonic-gate  * of page-aligned variable size of vm resources.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * Each vm resource represents a page-aligned range of virtual addresses.
37*7c478bd9Sstevel@tonic-gate  * The caller may specify whether the resource should include a redzone,
38*7c478bd9Sstevel@tonic-gate  * be locked down, or be zero initialized.
39*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate #include <vm/seg.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
44*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
45*7c478bd9Sstevel@tonic-gate extern "C" {
46*7c478bd9Sstevel@tonic-gate #endif
48*7c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * Private information per overall segkp segment (as opposed
52*7c478bd9Sstevel@tonic-gate  * to per resource within segment). There are as many anon slots
53*7c478bd9Sstevel@tonic-gate  * allocated as there there are pages in the segment.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate struct segkp_segdata {
56*7c478bd9Sstevel@tonic-gate 	struct anon_hdr	*kpsd_anon;	/* anon structs */
57*7c478bd9Sstevel@tonic-gate 	vmem_t		*kpsd_arena; 	/* virtual memory descriptor */
58*7c478bd9Sstevel@tonic-gate 	struct segkp_data **kpsd_hash;	/* Hash table for lookups */
59*7c478bd9Sstevel@tonic-gate };
61*7c478bd9Sstevel@tonic-gate #define	SEGKP_VMEM(seg)	(((struct segkp_segdata *)(seg)->s_data)->kpsd_arena)
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * A hash table is used to aid in the lookup of a kpd's based on vaddr.
65*7c478bd9Sstevel@tonic-gate  * Since the heaviest use of segkp occurs from segkp_*get and segkp_*release,
66*7c478bd9Sstevel@tonic-gate  * the hashing is based on the vaddr used by these routines.
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate #define	SEGKP_HASHSZ		256	/* power of two */
69*7c478bd9Sstevel@tonic-gate #define	SEGKP_HASHMASK		(SEGKP_HASHSZ - 1)
70*7c478bd9Sstevel@tonic-gate #define	SEGKP_HASH(vaddr)	\
71*7c478bd9Sstevel@tonic-gate 	((int)(((uintptr_t)vaddr >> PAGESHIFT) & SEGKP_HASHMASK))
73*7c478bd9Sstevel@tonic-gate struct segkp_data {
74*7c478bd9Sstevel@tonic-gate 	kmutex_t	kp_lock;	/* per resource lock */
75*7c478bd9Sstevel@tonic-gate 	caddr_t		kp_base;	/* starting addr of chunk */
76*7c478bd9Sstevel@tonic-gate 	size_t		kp_len;		/* # of bytes */
77*7c478bd9Sstevel@tonic-gate 	uint_t		kp_flags;	/* state info */
78*7c478bd9Sstevel@tonic-gate 	int		kp_cookie;	/* index into cache array */
79*7c478bd9Sstevel@tonic-gate 	ulong_t		kp_anon_idx;	/* index into main anon array */
80*7c478bd9Sstevel@tonic-gate 					/* in segkp_segdata */
81*7c478bd9Sstevel@tonic-gate 	struct anon_hdr	*kp_anon;	/* anon structs */
82*7c478bd9Sstevel@tonic-gate 	struct segkp_data *kp_next;	/* ptr to next in hash chain */
83*7c478bd9Sstevel@tonic-gate };
85*7c478bd9Sstevel@tonic-gate /*
86*7c478bd9Sstevel@tonic-gate  * Flag bits
87*7c478bd9Sstevel@tonic-gate  *
88*7c478bd9Sstevel@tonic-gate  */
89*7c478bd9Sstevel@tonic-gate #define	KPD_ZERO	0x01		/* initialize resource with 0 */
90*7c478bd9Sstevel@tonic-gate #define	KPD_LOCKED	0x02		/* resources locked */
91*7c478bd9Sstevel@tonic-gate #define	KPD_NO_ANON	0x04		/* no swap resources required */
92*7c478bd9Sstevel@tonic-gate #define	KPD_HASREDZONE	0x08		/* include a redzone */
93*7c478bd9Sstevel@tonic-gate #define	KPD_NOWAIT	0x10		/* do not wait for res. if unavail. */
94*7c478bd9Sstevel@tonic-gate #define	KPD_WRITEDIRTY	0x20		/* dirty pages should be flushed */
95*7c478bd9Sstevel@tonic-gate #define	KPD_HASAMP	0x40		/* anon_hdr managed by caller */
97*7c478bd9Sstevel@tonic-gate /*
98*7c478bd9Sstevel@tonic-gate  * A cache of segkp elements may be created via segkp_cache_init().
99*7c478bd9Sstevel@tonic-gate  * The elements on the freelist all have the same len and flags value.
100*7c478bd9Sstevel@tonic-gate  * The cookie passed to the client is an index into the freelist array.
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate struct segkp_cache  {
103*7c478bd9Sstevel@tonic-gate 	int		kpf_max;		/* max # of elements allowed */
104*7c478bd9Sstevel@tonic-gate 	int		kpf_count;		/* current no. of elments */
105*7c478bd9Sstevel@tonic-gate 	int		kpf_inuse;		/* list inuse */
106*7c478bd9Sstevel@tonic-gate 	uint_t		kpf_flags;		/* seg_kp flag value */
107*7c478bd9Sstevel@tonic-gate 	size_t		kpf_len;		/* len of resource */
108*7c478bd9Sstevel@tonic-gate 	struct seg	*kpf_seg;		/* segment */
109*7c478bd9Sstevel@tonic-gate 	struct segkp_data *kpf_list;		/* list of kpd's */
110*7c478bd9Sstevel@tonic-gate };
111*7c478bd9Sstevel@tonic-gate #define	SEGKP_MAX_CACHE		4	/* Number of caches maintained */
113*7c478bd9Sstevel@tonic-gate /*
114*7c478bd9Sstevel@tonic-gate  * Define redzone, and stack_to_memory macros.
115*7c478bd9Sstevel@tonic-gate  * The redzone is PAGESIZE bytes.
116*7c478bd9Sstevel@tonic-gate  */
117*7c478bd9Sstevel@tonic-gate #ifdef	STACK_GROWTH_DOWN
118*7c478bd9Sstevel@tonic-gate #define	KPD_REDZONE(kpd)	(0)
119*7c478bd9Sstevel@tonic-gate #define	stom(v, flags)	(((flags) & KPD_HASREDZONE) ? (v) + PAGESIZE : (v))
121*7c478bd9Sstevel@tonic-gate #else	/* STACK_GROWTH_DOWN */
123*7c478bd9Sstevel@tonic-gate #define	KPD_REDZONE(kpd) (btop(kpd->kp_len) - 1)
124*7c478bd9Sstevel@tonic-gate #define	stom(v)	(v)
125*7c478bd9Sstevel@tonic-gate #endif	/* STACK_GROWTH_DOWN */
127*7c478bd9Sstevel@tonic-gate #define	SEGKP_MAPLEN(len, flags) \
128*7c478bd9Sstevel@tonic-gate 	(((flags) & KPD_HASREDZONE) ? (len) - PAGESIZE : (len))
130*7c478bd9Sstevel@tonic-gate extern	struct seg *segkp;
131*7c478bd9Sstevel@tonic-gate /* If segkp becomes more than one seg this test will need changing. */
132*7c478bd9Sstevel@tonic-gate #define	SEG_IS_SEGKP(SEG)	((SEG) == segkp)
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate  * Public routine declarations not part of the segment ops vector go here.
136*7c478bd9Sstevel@tonic-gate  */
137*7c478bd9Sstevel@tonic-gate int	segkp_create(struct seg *seg);
138*7c478bd9Sstevel@tonic-gate caddr_t	segkp_get(struct seg *seg, size_t len, uint_t flags);
139*7c478bd9Sstevel@tonic-gate void	segkp_release(struct seg *seg, caddr_t vaddr);
140*7c478bd9Sstevel@tonic-gate void *  segkp_cache_init(struct seg *seg, int maxsize, size_t len,
141*7c478bd9Sstevel@tonic-gate 		uint_t flags);
142*7c478bd9Sstevel@tonic-gate void	segkp_cache_free();
143*7c478bd9Sstevel@tonic-gate caddr_t segkp_cache_get(void *cookie);
144*7c478bd9Sstevel@tonic-gate int	segkp_map_red(void);
145*7c478bd9Sstevel@tonic-gate void	segkp_unmap_red(void);
146*7c478bd9Sstevel@tonic-gate size_t	swapsize(caddr_t v);
148*7c478bd9Sstevel@tonic-gate /* Special currently only used by schedctl. */
149*7c478bd9Sstevel@tonic-gate struct anon_map;	/* Make the compiler happy about the next line. */
150*7c478bd9Sstevel@tonic-gate caddr_t segkp_get_withanonmap(struct seg *, size_t, uint_t, struct anon_map *);
152*7c478bd9Sstevel@tonic-gate /*
153*7c478bd9Sstevel@tonic-gate  * We allow explicit calls to segkp_fault, even though it's part
154*7c478bd9Sstevel@tonic-gate  * of the segkp ops vector.
155*7c478bd9Sstevel@tonic-gate  */
156*7c478bd9Sstevel@tonic-gate faultcode_t segkp_fault(struct hat *hat, struct seg *seg, caddr_t addr,
157*7c478bd9Sstevel@tonic-gate 	size_t len, enum fault_type type, enum seg_rw rw);
159*7c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
161*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate #endif
165*7c478bd9Sstevel@tonic-gate #endif	/* _VM_SEG_KP_H */