xref: /netbsd/sys/arch/i386/include/segments.h (revision c4a72b64)
1 /*	$NetBSD: segments.h,v 1.35 2002/10/05 21:26:24 fvdl Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1997
5  *	Charles M. Hannum.  All rights reserved.
6  * Copyright (c) 1989, 1990 William F. Jolitz
7  * Copyright (c) 1990 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * William Jolitz.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)segments.h	7.1 (Berkeley) 5/9/91
42  */
43 
44 /*
45  * 386 Segmentation Data Structures and definitions
46  *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
47  */
48 
49 #ifndef _I386_SEGMENTS_H_
50 #define _I386_SEGMENTS_H_
51 
52 /*
53  * Selectors
54  */
55 
56 #define	ISPL(s)		((s) & SEL_RPL)	/* what is the priority level of a selector */
57 #define	SEL_KPL		0		/* kernel privilege level */
58 #define	SEL_UPL		3		/* user privilege level */
59 #define	SEL_RPL		3		/* requester's privilege level mask */
60 #define	ISLDT(s)	((s) & SEL_LDT)	/* is it local or global */
61 #define	SEL_LDT		4		/* local descriptor table */
62 #define	IDXSEL(s)	(((s) >> 3) & 0x1fff)		/* index of selector */
63 #define	GSEL(s,r)	(((s) << 3) | r)		/* a global selector */
64 #define	LSEL(s,r)	(((s) << 3) | r | SEL_LDT)	/* a local selector */
65 
66 #if defined(_KERNEL_OPT)
67 #include "opt_vm86.h"
68 #endif
69 
70 #ifdef VM86
71 #define	USERMODE(c, f)		(ISPL(c) == SEL_UPL || ((f) & PSL_VM) != 0)
72 #define	KERNELMODE(c, f)	(ISPL(c) == SEL_KPL && ((f) & PSL_VM) == 0)
73 #else
74 #define	USERMODE(c, f)		(ISPL(c) == SEL_UPL)
75 #define	KERNELMODE(c, f)	(ISPL(c) == SEL_KPL)
76 #endif
77 
78 #ifndef _LOCORE
79 
80 #if __GNUC__ == 2 && __GNUC_MINOR__ < 7
81 #pragma pack(1)
82 #endif
83 
84 /*
85  * Memory and System segment descriptors
86  */
87 struct segment_descriptor {
88 	unsigned sd_lolimit:16;		/* segment extent (lsb) */
89 	unsigned sd_lobase:24;		/* segment base address (lsb) */
90 	unsigned sd_type:5;		/* segment type */
91 	unsigned sd_dpl:2;		/* segment descriptor priority level */
92 	unsigned sd_p:1;		/* segment descriptor present */
93 	unsigned sd_hilimit:4;		/* segment extent (msb) */
94 	unsigned sd_xx:2;		/* unused */
95 	unsigned sd_def32:1;		/* default 32 vs 16 bit size */
96 	unsigned sd_gran:1;		/* limit granularity (byte/page) */
97 	unsigned sd_hibase:8;		/* segment base address (msb) */
98 } __attribute__((packed));
99 
100 /*
101  * Gate descriptors (e.g. indirect descriptors)
102  */
103 struct gate_descriptor {
104 	unsigned gd_looffset:16;	/* gate offset (lsb) */
105 	unsigned gd_selector:16;	/* gate segment selector */
106 	unsigned gd_stkcpy:5;		/* number of stack wds to cpy */
107 	unsigned gd_xx:3;		/* unused */
108 	unsigned gd_type:5;		/* segment type */
109 	unsigned gd_dpl:2;		/* segment descriptor priority level */
110 	unsigned gd_p:1;		/* segment descriptor present */
111 	unsigned gd_hioffset:16;	/* gate offset (msb) */
112 } __attribute__((packed));
113 
114 /*
115  * Generic descriptor
116  */
117 union descriptor {
118 	struct segment_descriptor sd;
119 	struct gate_descriptor gd;
120 } __attribute__((packed));
121 
122 /*
123  * region descriptors, used to load gdt/idt tables before segments yet exist.
124  */
125 struct region_descriptor {
126 	unsigned rd_limit:16;		/* segment extent */
127 	unsigned rd_base:32;		/* base address  */
128 } __attribute__((packed));
129 
130 #if __GNUC__ == 2 && __GNUC_MINOR__ < 7
131 #pragma pack(4)
132 #endif
133 
134 #ifdef _KERNEL
135 extern union descriptor *idt, *gdt, *ldt;
136 
137 void setgate __P((struct gate_descriptor *, void *, int, int, int, int));
138 void setregion __P((struct region_descriptor *, void *, size_t));
139 void setsegment __P((struct segment_descriptor *, void *, size_t, int, int,
140     int, int));
141 void setgdt __P((int, void *, size_t, int, int, int, int));
142 void unsetgate __P((struct gate_descriptor *));
143 void cpu_init_idt __P((void));
144 
145 int idt_vec_alloc __P((int, int));
146 void idt_vec_set __P((int, void (*)(void)));
147 void idt_vec_free __P((int));
148 
149 #endif /* _KERNEL */
150 
151 #endif /* !_LOCORE */
152 
153 /* system segments and gate types */
154 #define	SDT_SYSNULL	 0	/* system null */
155 #define	SDT_SYS286TSS	 1	/* system 286 TSS available */
156 #define	SDT_SYSLDT	 2	/* system local descriptor table */
157 #define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
158 #define	SDT_SYS286CGT	 4	/* system 286 call gate */
159 #define	SDT_SYSTASKGT	 5	/* system task gate */
160 #define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
161 #define	SDT_SYS286TGT	 7	/* system 286 trap gate */
162 #define	SDT_SYSNULL2	 8	/* system null again */
163 #define	SDT_SYS386TSS	 9	/* system 386 TSS available */
164 #define	SDT_SYSNULL3	10	/* system null again */
165 #define	SDT_SYS386BSY	11	/* system 386 TSS busy */
166 #define	SDT_SYS386CGT	12	/* system 386 call gate */
167 #define	SDT_SYSNULL4	13	/* system null again */
168 #define	SDT_SYS386IGT	14	/* system 386 interrupt gate */
169 #define	SDT_SYS386TGT	15	/* system 386 trap gate */
170 
171 /* memory segment types */
172 #define	SDT_MEMRO	16	/* memory read only */
173 #define	SDT_MEMROA	17	/* memory read only accessed */
174 #define	SDT_MEMRW	18	/* memory read write */
175 #define	SDT_MEMRWA	19	/* memory read write accessed */
176 #define	SDT_MEMROD	20	/* memory read only expand dwn limit */
177 #define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
178 #define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
179 #define	SDT_MEMRWDA	23	/* memory read write expand dwn limit acessed */
180 #define	SDT_MEME	24	/* memory execute only */
181 #define	SDT_MEMEA	25	/* memory execute only accessed */
182 #define	SDT_MEMER	26	/* memory execute read */
183 #define	SDT_MEMERA	27	/* memory execute read accessed */
184 #define	SDT_MEMEC	28	/* memory execute only conforming */
185 #define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
186 #define	SDT_MEMERC	30	/* memory execute read conforming */
187 #define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
188 
189 /* is memory segment descriptor pointer ? */
190 #define ISMEMSDP(s)	((s->d_type) >= SDT_MEMRO && \
191 			 (s->d_type) <= SDT_MEMERAC)
192 
193 /* is 286 gate descriptor pointer ? */
194 #define IS286GDP(s)	((s->d_type) >= SDT_SYS286CGT && \
195 			 (s->d_type) < SDT_SYS286TGT)
196 
197 /* is 386 gate descriptor pointer ? */
198 #define IS386GDP(s)	((s->d_type) >= SDT_SYS386CGT && \
199 			 (s->d_type) < SDT_SYS386TGT)
200 
201 /* is gate descriptor pointer ? */
202 #define ISGDP(s)	(IS286GDP(s) || IS386GDP(s))
203 
204 /* is segment descriptor pointer ? */
205 #define ISSDP(s)	(ISMEMSDP(s) || !ISGDP(s))
206 
207 /* is system segment descriptor pointer ? */
208 #define ISSYSSDP(s)	(!ISMEMSDP(s) && !ISGDP(s))
209 
210 /*
211  * Segment Protection Exception code bits
212  */
213 #define	SEGEX_EXT	0x01	/* recursive or externally induced */
214 #define	SEGEX_IDT	0x02	/* interrupt descriptor table */
215 #define	SEGEX_TI	0x04	/* local descriptor table */
216 
217 /*
218  * Entries in the Interrupt Descriptor Table (IDT)
219  */
220 #define	NIDT	256
221 #define	NRSVIDT	32		/* reserved entries for cpu exceptions */
222 
223 /*
224  * Entries in the Global Descriptor Table (GDT).
225  *
226  * NB: If you change GBIOSCODE/GBIOSDATA, you *must* rebuild arch/i386/
227  * bioscall/biostramp.inc, as that relies on GBIOSCODE/GBIOSDATA and a
228  * normal kernel build does not rebuild it (it's merely included whole-
229  * sale from i386/bioscall.s)
230  *
231  * Also, note that the GEXTBIOSDATA_SEL selector is special, as it maps
232  * to the value 0x0040 (when created as a KPL global selector).  Some
233  * BIOSes reference the extended BIOS data area at segment 0040 in a non
234  * relocatable fashion (even when in protected mode); mapping the zero page
235  * via the GEXTBIOSDATA_SEL allows these buggy BIOSes to continue to work
236  * under NetBSD.
237  */
238 #define	GNULL_SEL	0	/* Null descriptor */
239 #define	GCODE_SEL	1	/* Kernel code descriptor */
240 #define	GDATA_SEL	2	/* Kernel data descriptor */
241 #define	GLDT_SEL	3	/* Default LDT descriptor */
242 #define	GUCODE_SEL	4	/* User code descriptor */
243 #define	GUDATA_SEL	5	/* User data descriptor */
244 #define GCPU_SEL	6	/* per-CPU segment */
245 #define	GMACHCALLS_SEL	7	/* Darwin (mach trap) system call gate */
246 #define	GEXTBIOSDATA_SEL 8	/* magic to catch BIOS refs to EBDA */
247 #define	GAPM32CODE_SEL	9	/* 3 APM segments must be consecutive */
248 #define	GAPM16CODE_SEL	10	/* and in the specified order: code32 */
249 #define	GAPMDATA_SEL	11	/* code16 and then data per APM spec */
250 #define	GBIOSCODE_SEL	12
251 #define	GBIOSDATA_SEL	13
252 #define	GPNPBIOSCODE_SEL 14
253 #define	GPNPBIOSDATA_SEL 15
254 #define	GPNPBIOSSCRATCH_SEL 16
255 #define	GPNPBIOSTRAMP_SEL 17
256 #define GTRAPTSS_SEL	18
257 #define GIPITSS_SEL	19
258 #define	NGDT		20
259 
260 /*
261  * Entries in the Local Descriptor Table (LDT)
262  */
263 #define	LSYS5CALLS_SEL	0	/* iBCS system call gate */
264 #define	LSYS5SIGR_SEL	1	/* iBCS sigreturn gate */
265 #define	LUCODE_SEL	2	/* User code descriptor */
266 #define	LUDATA_SEL	3	/* User data descriptor */
267 #define	LSOL26CALLS_SEL	4	/* Solaris 2.6 system call gate */
268 #define	LBSDICALLS_SEL	16	/* BSDI system call gate */
269 #define	NLDT		17
270 
271 #endif /* _I386_SEGMENTS_H_ */
272