xref: /netbsd-src/sys/arch/amd64/include/segments.h (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: segments.h,v 1.4 2004/02/13 11:36:20 wiz Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * William Jolitz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)segments.h	7.1 (Berkeley) 5/9/91
35  */
36 
37 /*-
38  * Copyright (c) 1995, 1997
39  *	Charles M. Hannum.  All rights reserved.
40  * Copyright (c) 1989, 1990 William F. Jolitz
41  *
42  * This code is derived from software contributed to Berkeley by
43  * William Jolitz.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *	This product includes software developed by the University of
56  *	California, Berkeley and its contributors.
57  * 4. Neither the name of the University nor the names of its contributors
58  *    may be used to endorse or promote products derived from this software
59  *    without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  *
73  *	@(#)segments.h	7.1 (Berkeley) 5/9/91
74  */
75 
76 /*
77  * Adapted for NetBSD/amd64 by fvdl@wasabisystems.com.
78  */
79 
80 /*
81  * 386 Segmentation Data Structures and definitions
82  *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
83  */
84 
85 #ifndef _AMD64_SEGMENTS_H_
86 #define _AMD64_SEGMENTS_H_
87 
88 /*
89  * Selectors
90  */
91 
92 #define	ISPL(s)		((s) & SEL_RPL)	/* what is the priority level of a selector */
93 #define	SEL_KPL		0		/* kernel privilege level */
94 #define	SEL_UPL		3		/* user privilege level */
95 #define	SEL_RPL		3		/* requester's privilege level mask */
96 #define	ISLDT(s)	((s) & SEL_LDT)	/* is it local or global */
97 #define	SEL_LDT		4		/* local descriptor table */
98 
99 /* Dynamically allocated TSSs and LDTs start (byte offset) */
100 #define SYSSEL_START	(NGDT_MEM << 3)
101 #define DYNSEL_START	(SYSSEL_START + (NGDT_SYS << 4))
102 
103 /*
104  * These define the index not from the start of the GDT, but from
105  * the part of the GDT that they're allocated from.
106  * First NGDT_MEM entries are 8-byte descriptors for CS and DS.
107  * Next NGDT_SYS entries are 16-byte descriptors defining LDTs.
108  *
109  * The rest is 16-byte descriptors for TSS and LDT.
110  */
111 
112 #define	IDXSEL(s)	(((s) >> 3) & 0x1fff)
113 #define IDXDYNSEL(s)	((((s) & ~SEL_RPL) - DYNSEL_START) >> 4)
114 
115 #define	GSEL(s,r)	(((s) << 3) | r)
116 #define	GSYSSEL(s,r)	((((s) << 4) + SYSSEL_START) | r)
117 #define GDYNSEL(s,r)	((((s) << 4) + DYNSEL_START) | r | SEL_KPL)
118 
119 #define LSEL(s,r)	((s) | r | SEL_LDT)
120 
121 #define	USERMODE(c, f)		(ISPL(c) == SEL_UPL)
122 #define	KERNELMODE(c, f)	(ISPL(c) == SEL_KPL)
123 
124 #ifndef _LOCORE
125 
126 /*
127  * Memory and System segment descriptors
128  */
129 
130 /*
131  * Below is used for TSS and LDT.
132  */
133 struct sys_segment_descriptor {
134 /*BITFIELDTYPE*/ u_int64_t sd_lolimit:16;/* segment extent (lsb) */
135 /*BITFIELDTYPE*/ u_int64_t sd_lobase:24;/* segment base address (lsb) */
136 /*BITFIELDTYPE*/ u_int64_t sd_type:5;	/* segment type */
137 /*BITFIELDTYPE*/ u_int64_t sd_dpl:2;	/* segment descriptor priority level */
138 /*BITFIELDTYPE*/ u_int64_t sd_p:1;	/* segment descriptor present */
139 /*BITFIELDTYPE*/ u_int64_t sd_hilimit:4;/* segment extent (msb) */
140 /*BITFIELDTYPE*/ u_int64_t sd_xx1:3;	/* avl, long and def32 (not used) */
141 /*BITFIELDTYPE*/ u_int64_t sd_gran:1;	/* limit granularity (byte/page) */
142 /*BITFIELDTYPE*/ u_int64_t sd_hibase:40;/* segment base address (msb) */
143 /*BITFIELDTYPE*/ u_int64_t sd_xx2:8;	/* reserved */
144 /*BITFIELDTYPE*/ u_int64_t sd_zero:5;	/* must be zero */
145 /*BITFIELDTYPE*/ u_int64_t sd_xx3:19;	/* reserved */
146 } __attribute__((packed));
147 
148 /*
149  * Below is used for cs, ds, etc.
150  */
151 struct mem_segment_descriptor {
152 	unsigned sd_lolimit:16;         /* segment extent (lsb) */
153 	unsigned sd_lobase:24;          /* segment base address (lsb) */
154 	unsigned sd_type:5;             /* segment type */
155 	unsigned sd_dpl:2;              /* segment descriptor priority level */
156 	unsigned sd_p:1;                /* segment descriptor present */
157 	unsigned sd_hilimit:4;          /* segment extent (msb) */
158 	unsigned sd_avl:1;		/* available */
159 	unsigned sd_long:1;		/* long mode */
160 	unsigned sd_def32:1;            /* default 32 vs 16 bit size */
161 	unsigned sd_gran:1;             /* limit granularity (byte/page) */
162 	unsigned sd_hibase:8;           /* segment base address (msb) */
163 } __attribute__((packed));
164 
165 /*
166  * Gate descriptors (e.g. indirect descriptors)
167  */
168 struct gate_descriptor {
169 /*BITFIELDTYPE*/ u_int64_t gd_looffset:16;/* gate offset (lsb) */
170 /*BITFIELDTYPE*/ u_int64_t gd_selector:16;/* gate segment selector */
171 /*BITFIELDTYPE*/ u_int64_t gd_ist:3;	/* IST select */
172 /*BITFIELDTYPE*/ u_int64_t gd_xx1:5;	/* reserved */
173 /*BITFIELDTYPE*/ u_int64_t gd_type:5;	/* segment type */
174 /*BITFIELDTYPE*/ u_int64_t gd_dpl:2;	/* segment descriptor priority level */
175 /*BITFIELDTYPE*/ u_int64_t gd_p:1;	/* segment descriptor present */
176 /*BITFIELDTYPE*/ u_int64_t gd_hioffset:48;/* gate offset (msb) */
177 /*BITFIELDTYPE*/ u_int64_t gd_xx2:8;	/* reserved */
178 /*BITFIELDTYPE*/ u_int64_t gd_zero:5;	/* must be zero */
179 /*BITFIELDTYPE*/ u_int64_t gd_xx3:19;	/* reserved */
180 } __attribute__((packed));
181 
182 /*
183  * region descriptors, used to load gdt/idt tables before segments yet exist.
184  */
185 struct region_descriptor {
186 	u_int16_t rd_limit;		/* segment extent */
187 	u_int64_t rd_base;		/* base address  */
188 } __attribute__((packed));
189 
190 #ifdef _KERNEL
191 #if 0
192 extern struct sys_segment_descriptor *ldt;
193 #endif
194 extern struct gate_descriptor *idt;
195 extern char *gdtstore;
196 extern char *ldtstore;
197 
198 void setgate __P((struct gate_descriptor *, void *, int, int, int, int));
199 void unsetgate __P((struct gate_descriptor *));
200 void setregion __P((struct region_descriptor *, void *, u_int16_t));
201 void set_sys_segment __P((struct sys_segment_descriptor *, void *, size_t,
202 			  int, int, int));
203 void set_mem_segment __P((struct mem_segment_descriptor *, void *, size_t,
204 			  int, int, int, int, int));
205 int idt_vec_alloc __P((int, int));
206 void idt_vec_set __P((int, void (*)(void)));
207 void idt_vec_free __P((int));
208 void cpu_init_idt __P((void));
209 
210 #endif /* _KERNEL */
211 
212 #endif /* !_LOCORE */
213 
214 /* system segments and gate types */
215 #define	SDT_SYSNULL	 0	/* system null */
216 #define	SDT_SYS286TSS	 1	/* system 286 TSS available */
217 #define	SDT_SYSLDT	 2	/* system local descriptor table */
218 #define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
219 #define	SDT_SYS286CGT	 4	/* system 286 call gate */
220 #define	SDT_SYSTASKGT	 5	/* system task gate */
221 #define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
222 #define	SDT_SYS286TGT	 7	/* system 286 trap gate */
223 #define	SDT_SYSNULL2	 8	/* system null again */
224 #define	SDT_SYS386TSS	 9	/* system 386 TSS available */
225 #define	SDT_SYSNULL3	10	/* system null again */
226 #define	SDT_SYS386BSY	11	/* system 386 TSS busy */
227 #define	SDT_SYS386CGT	12	/* system 386 call gate */
228 #define	SDT_SYSNULL4	13	/* system null again */
229 #define	SDT_SYS386IGT	14	/* system 386 interrupt gate */
230 #define	SDT_SYS386TGT	15	/* system 386 trap gate */
231 
232 /* memory segment types */
233 #define	SDT_MEMRO	16	/* memory read only */
234 #define	SDT_MEMROA	17	/* memory read only accessed */
235 #define	SDT_MEMRW	18	/* memory read write */
236 #define	SDT_MEMRWA	19	/* memory read write accessed */
237 #define	SDT_MEMROD	20	/* memory read only expand dwn limit */
238 #define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
239 #define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
240 #define	SDT_MEMRWDA	23	/* memory read write expand dwn limit acessed */
241 #define	SDT_MEME	24	/* memory execute only */
242 #define	SDT_MEMEA	25	/* memory execute only accessed */
243 #define	SDT_MEMER	26	/* memory execute read */
244 #define	SDT_MEMERA	27	/* memory execute read accessed */
245 #define	SDT_MEMEC	28	/* memory execute only conforming */
246 #define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
247 #define	SDT_MEMERC	30	/* memory execute read conforming */
248 #define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
249 
250 /* is memory segment descriptor pointer ? */
251 #define ISMEMSDP(s)	((s->d_type) >= SDT_MEMRO && \
252 			 (s->d_type) <= SDT_MEMERAC)
253 
254 /* is 286 gate descriptor pointer ? */
255 #define IS286GDP(s)	((s->d_type) >= SDT_SYS286CGT && \
256 			 (s->d_type) < SDT_SYS286TGT)
257 
258 /* is 386 gate descriptor pointer ? */
259 #define IS386GDP(s)	((s->d_type) >= SDT_SYS386CGT && \
260 			 (s->d_type) < SDT_SYS386TGT)
261 
262 /* is gate descriptor pointer ? */
263 #define ISGDP(s)	(IS286GDP(s) || IS386GDP(s))
264 
265 /* is segment descriptor pointer ? */
266 #define ISSDP(s)	(ISMEMSDP(s) || !ISGDP(s))
267 
268 /* is system segment descriptor pointer ? */
269 #define ISSYSSDP(s)	(!ISMEMSDP(s) && !ISGDP(s))
270 
271 /*
272  * Segment Protection Exception code bits
273  */
274 #define	SEGEX_EXT	0x01	/* recursive or externally induced */
275 #define	SEGEX_IDT	0x02	/* interrupt descriptor table */
276 #define	SEGEX_TI	0x04	/* local descriptor table */
277 
278 /*
279  * Entries in the Interrupt Descriptor Table (IDT)
280  */
281 #define	NIDT	256
282 #define	NRSVIDT	32		/* reserved entries for CPU exceptions */
283 
284 /*
285  * Entries in the Global Descriptor Table (GDT)
286  * The code and data descriptors must come first. There
287  * are NGDT_MEM of them.
288  *
289  * Then come the predefined LDT (and possibly TSS) descriptors.
290  * There are NGDT_SYS of them.
291  */
292 #define	GNULL_SEL	0	/* Null descriptor */
293 #define	GCODE_SEL	1	/* Kernel code descriptor */
294 #define	GDATA_SEL	2	/* Kernel data descriptor */
295 #define	GUCODE_SEL	3	/* User code descriptor */
296 #define	GUDATA_SEL	4	/* User data descriptor */
297 #define	GAPM32CODE_SEL	5
298 #define	GAPM16CODE_SEL	6
299 #define	GAPMDATA_SEL	7
300 #define	GBIOSCODE_SEL	8
301 #define	GBIOSDATA_SEL	9
302 #define GPNPBIOSCODE_SEL 10
303 #define GPNPBIOSDATA_SEL 11
304 #define GPNPBIOSSCRATCH_SEL 12
305 #define GPNPBIOSTRAMP_SEL 13
306 #define GUCODE32_SEL	14
307 #define GUDATA32_SEL	15
308 #define NGDT_MEM 16
309 
310 #define	GLDT_SEL	0	/* Default LDT descriptor */
311 #define NGDT_SYS	1
312 
313 #define GDT_SYS_OFFSET	(NGDT_MEM << 3)
314 
315 #define GDT_ADDR_MEM(s,i)	\
316     ((struct mem_segment_descriptor *)((s) + ((i) << 3)))
317 #define GDT_ADDR_SYS(s,i)	\
318    ((struct sys_segment_descriptor *)((s) + (((i) << 4) + SYSSEL_START)))
319 
320 /*
321  * Byte offsets in the Local Descriptor Table (LDT)
322  * Strange order because of syscall/sysret insns
323  */
324 #define	LSYS5CALLS_SEL	0	/* iBCS system call gate */
325 #define LUCODE32_SEL	8	/* 32 bit user code descriptor */
326 #define	LUDATA_SEL	16	/* User data descriptor */
327 #define	LUCODE_SEL	24	/* User code descriptor */
328 #define	LSOL26CALLS_SEL	32	/* Solaris 2.6 system call gate */
329 #define LUDATA32_SEL	56	/* 32 bit user data descriptor (needed?)*/
330 #define	LBSDICALLS_SEL	128	/* BSDI system call gate */
331 
332 #define LDT_SIZE	144
333 
334 #define LSYSRETBASE_SEL	LUCODE32_SEL
335 
336 /*
337  * Checks for valid user selectors. If USER_LDT ever gets implemented
338  * for amd64, these must check the ldt length and SEL_UPL if a user
339  * ldt is active.
340  */
341 #define VALID_USER_DSEL32(s) \
342     ((s) == GSEL(GUDATA32_SEL, SEL_UPL) || (s) == LSEL(LUDATA32_SEL, SEL_UPL))
343 #define VALID_USER_CSEL32(s) \
344     ((s) == GSEL(GUCODE32_SEL, SEL_UPL) || (s) == LSEL(LUCODE32_SEL, SEL_UPL))
345 
346 #define VALID_USER_CSEL(s) \
347     ((s) == GSEL(GUCODE_SEL, SEL_UPL) || (s) == LSEL(LUCODE_SEL, SEL_UPL))
348 #define VALID_USER_DSEL(s) \
349     ((s) == GSEL(GUDATA_SEL, SEL_UPL) || (s) == LSEL(LUDATA_SEL, SEL_UPL))
350 
351 #endif /* _AMD64_SEGMENTS_H_ */
352