xref: /netbsd-src/sys/arch/i386/include/segments.h (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: segments.h,v 1.44 2007/11/10 20:06:24 ad 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  * 386 Segmentation Data Structures and definitions
78  *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
79  */
80 
81 #ifndef _I386_SEGMENTS_H_
82 #define _I386_SEGMENTS_H_
83 
84 /*
85  * Selectors
86  */
87 
88 #define	ISPL(s)		((s) & SEL_RPL)	/* what is the priority level of a selector */
89 #define	SEL_KPL		0		/* kernel privilege level */
90 #define	SEL_UPL		3		/* user privilege level */
91 #define	SEL_RPL		3		/* requester's privilege level mask */
92 #define	ISLDT(s)	((s) & SEL_LDT)	/* is it local or global */
93 #define	SEL_LDT		4		/* local descriptor table */
94 #define	IDXSEL(s)	(((s) >> 3) & 0x1fff)		/* index of selector */
95 #define	GSEL(s,r)	(((s) << 3) | r)		/* a global selector */
96 #define	LSEL(s,r)	(((s) << 3) | r | SEL_LDT)	/* a local selector */
97 #define	GSYSSEL(s,r)	GSEL(s,r)	/* compat with amd64 */
98 
99 #if defined(_KERNEL_OPT)
100 #include "opt_vm86.h"
101 #endif
102 
103 #ifdef VM86
104 #define	USERMODE(c, f)		(ISPL(c) == SEL_UPL || ((f) & PSL_VM) != 0)
105 #define	KERNELMODE(c, f)	(ISPL(c) == SEL_KPL && ((f) & PSL_VM) == 0)
106 #else
107 #define	USERMODE(c, f)		(ISPL(c) == SEL_UPL)
108 #define	KERNELMODE(c, f)	(ISPL(c) == SEL_KPL)
109 #endif
110 
111 #ifndef _LOCORE
112 
113 #if __GNUC__ == 2 && __GNUC_MINOR__ < 7
114 #pragma pack(1)
115 #endif
116 
117 /*
118  * Memory and System segment descriptors
119  */
120 struct segment_descriptor {
121 	unsigned sd_lolimit:16;		/* segment extent (lsb) */
122 	unsigned sd_lobase:24;		/* segment base address (lsb) */
123 	unsigned sd_type:5;		/* segment type */
124 	unsigned sd_dpl:2;		/* segment descriptor priority level */
125 	unsigned sd_p:1;		/* segment descriptor present */
126 	unsigned sd_hilimit:4;		/* segment extent (msb) */
127 	unsigned sd_xx:2;		/* unused */
128 	unsigned sd_def32:1;		/* default 32 vs 16 bit size */
129 	unsigned sd_gran:1;		/* limit granularity (byte/page) */
130 	unsigned sd_hibase:8;		/* segment base address (msb) */
131 } __attribute__((packed));
132 
133 /*
134  * Gate descriptors (e.g. indirect descriptors)
135  */
136 struct gate_descriptor {
137 	unsigned gd_looffset:16;	/* gate offset (lsb) */
138 	unsigned gd_selector:16;	/* gate segment selector */
139 	unsigned gd_stkcpy:5;		/* number of stack wds to cpy */
140 	unsigned gd_xx:3;		/* unused */
141 	unsigned gd_type:5;		/* segment type */
142 	unsigned gd_dpl:2;		/* segment descriptor priority level */
143 	unsigned gd_p:1;		/* segment descriptor present */
144 	unsigned gd_hioffset:16;	/* gate offset (msb) */
145 } __attribute__((packed));
146 
147 /*
148  * Generic descriptor
149  */
150 union descriptor {
151 	struct segment_descriptor sd;
152 	struct gate_descriptor gd;
153 } __attribute__((packed));
154 
155 /*
156  * region descriptors, used to load gdt/idt tables before segments yet exist.
157  */
158 struct region_descriptor {
159 	unsigned rd_limit:16;		/* segment extent */
160 	unsigned rd_base:32;		/* base address  */
161 } __attribute__((packed));
162 
163 #if __GNUC__ == 2 && __GNUC_MINOR__ < 7
164 #pragma pack(4)
165 #endif
166 
167 #ifdef _KERNEL
168 extern union descriptor *gdt, *ldt;
169 extern struct gate_descriptor *idt;
170 
171 void setgate(struct gate_descriptor *, void *, int, int, int, int);
172 void setregion(struct region_descriptor *, void *, size_t);
173 void setsegment(struct segment_descriptor *, void *, size_t, int, int,
174     int, int);
175 void setgdt(int, void *, size_t, int, int, int, int);
176 void unsetgate(struct gate_descriptor *);
177 void cpu_init_idt(void);
178 
179 int idt_vec_alloc(int, int);
180 void idt_vec_set(int, void (*)(void));
181 void idt_vec_free(int);
182 
183 #endif /* _KERNEL */
184 
185 #endif /* !_LOCORE */
186 
187 /* system segments and gate types */
188 #define	SDT_SYSNULL	 0	/* system null */
189 #define	SDT_SYS286TSS	 1	/* system 286 TSS available */
190 #define	SDT_SYSLDT	 2	/* system local descriptor table */
191 #define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
192 #define	SDT_SYS286CGT	 4	/* system 286 call gate */
193 #define	SDT_SYSTASKGT	 5	/* system task gate */
194 #define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
195 #define	SDT_SYS286TGT	 7	/* system 286 trap gate */
196 #define	SDT_SYSNULL2	 8	/* system null again */
197 #define	SDT_SYS386TSS	 9	/* system 386 TSS available */
198 #define	SDT_SYSNULL3	10	/* system null again */
199 #define	SDT_SYS386BSY	11	/* system 386 TSS busy */
200 #define	SDT_SYS386CGT	12	/* system 386 call gate */
201 #define	SDT_SYSNULL4	13	/* system null again */
202 #define	SDT_SYS386IGT	14	/* system 386 interrupt gate */
203 #define	SDT_SYS386TGT	15	/* system 386 trap gate */
204 
205 /* memory segment types */
206 #define	SDT_MEMRO	16	/* memory read only */
207 #define	SDT_MEMROA	17	/* memory read only accessed */
208 #define	SDT_MEMRW	18	/* memory read write */
209 #define	SDT_MEMRWA	19	/* memory read write accessed */
210 #define	SDT_MEMROD	20	/* memory read only expand dwn limit */
211 #define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
212 #define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
213 #define	SDT_MEMRWDA	23	/* memory read write expand dwn limit acessed */
214 #define	SDT_MEME	24	/* memory execute only */
215 #define	SDT_MEMEA	25	/* memory execute only accessed */
216 #define	SDT_MEMER	26	/* memory execute read */
217 #define	SDT_MEMERA	27	/* memory execute read accessed */
218 #define	SDT_MEMEC	28	/* memory execute only conforming */
219 #define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
220 #define	SDT_MEMERC	30	/* memory execute read conforming */
221 #define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
222 
223 /* is memory segment descriptor pointer ? */
224 #define ISMEMSDP(s)	((s->d_type) >= SDT_MEMRO && \
225 			 (s->d_type) <= SDT_MEMERAC)
226 
227 /* is 286 gate descriptor pointer ? */
228 #define IS286GDP(s)	((s->d_type) >= SDT_SYS286CGT && \
229 			 (s->d_type) < SDT_SYS286TGT)
230 
231 /* is 386 gate descriptor pointer ? */
232 #define IS386GDP(s)	((s->d_type) >= SDT_SYS386CGT && \
233 			 (s->d_type) < SDT_SYS386TGT)
234 
235 /* is gate descriptor pointer ? */
236 #define ISGDP(s)	(IS286GDP(s) || IS386GDP(s))
237 
238 /* is segment descriptor pointer ? */
239 #define ISSDP(s)	(ISMEMSDP(s) || !ISGDP(s))
240 
241 /* is system segment descriptor pointer ? */
242 #define ISSYSSDP(s)	(!ISMEMSDP(s) && !ISGDP(s))
243 
244 /*
245  * Segment Protection Exception code bits
246  */
247 #define	SEGEX_EXT	0x01	/* recursive or externally induced */
248 #define	SEGEX_IDT	0x02	/* interrupt descriptor table */
249 #define	SEGEX_TI	0x04	/* local descriptor table */
250 
251 /*
252  * Entries in the Interrupt Descriptor Table (IDT)
253  */
254 #define	NIDT	256
255 #define	NRSVIDT	32		/* reserved entries for CPU exceptions */
256 
257 /*
258  * Entries in the Global Descriptor Table (GDT).
259  *
260  * NB: If you change GBIOSCODE/GBIOSDATA, you *must* rebuild arch/i386/
261  * bioscall/biostramp.inc, as that relies on GBIOSCODE/GBIOSDATA and a
262  * normal kernel build does not rebuild it (it's merely included whole-
263  * sale from i386/bioscall.s)
264  *
265  * Also, note that the GEXTBIOSDATA_SEL selector is special, as it maps
266  * to the value 0x0040 (when created as a KPL global selector).  Some
267  * BIOSes reference the extended BIOS data area at segment 0040 in a non
268  * relocatable fashion (even when in protected mode); mapping the zero page
269  * via the GEXTBIOSDATA_SEL allows these buggy BIOSes to continue to work
270  * under NetBSD.
271  *
272  * The order if the first 5 descriptors is special; the sysenter/sysexit
273  * instructions depend on them.
274  */
275 #define	GNULL_SEL	0	/* Null descriptor */
276 #define	GCODE_SEL	1	/* Kernel code descriptor */
277 #define	GDATA_SEL	2	/* Kernel data descriptor */
278 #define	GUCODE_SEL	3	/* User code descriptor */
279 #define	GUDATA_SEL	4	/* User data descriptor */
280 #define	GLDT_SEL	5	/* Default LDT descriptor */
281 #define GCPU_SEL	6	/* per-CPU segment */
282 #define	GMACHCALLS_SEL	7	/* Darwin (mach trap) system call gate */
283 #define	GEXTBIOSDATA_SEL 8	/* magic to catch BIOS refs to EBDA */
284 #define	GAPM32CODE_SEL	9	/* 3 APM segments must be consecutive */
285 #define	GAPM16CODE_SEL	10	/* and in the specified order: code32 */
286 #define	GAPMDATA_SEL	11	/* code16 and then data per APM spec */
287 #define	GBIOSCODE_SEL	12
288 #define	GBIOSDATA_SEL	13
289 #define	GPNPBIOSCODE_SEL 14
290 #define	GPNPBIOSDATA_SEL 15
291 #define	GPNPBIOSSCRATCH_SEL 16
292 #define	GPNPBIOSTRAMP_SEL 17
293 #define GTRAPTSS_SEL	18
294 #define GIPITSS_SEL	19
295 #define GUCODEBIG_SEL	20	/* User code with executable stack */
296 #define	GUFS_SEL	21
297 #define	GUGS_SEL	22
298 #define	NGDT		23
299 
300 /*
301  * Entries in the Local Descriptor Table (LDT)
302  */
303 #define	LSYS5CALLS_SEL	0	/* iBCS system call gate */
304 #define	LSYS5SIGR_SEL	1	/* iBCS sigreturn gate */
305 #define	LUCODE_SEL	2	/* User code descriptor */
306 #define	LUDATA_SEL	3	/* User data descriptor */
307 #define	LSOL26CALLS_SEL	4	/* Solaris 2.6 system call gate */
308 #define	LUCODEBIG_SEL	5	/* User code with executable stack */
309 #define	LBSDICALLS_SEL	16	/* BSDI system call gate */
310 #define	NLDT		17
311 
312 #endif /* _I386_SEGMENTS_H_ */
313