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