1233203Stijl/*-
2233203Stijl * Copyright (c) 1989, 1990 William F. Jolitz
3233203Stijl * Copyright (c) 1990 The Regents of the University of California.
4233203Stijl * All rights reserved.
5233203Stijl *
6233203Stijl * This code is derived from software contributed to Berkeley by
7233203Stijl * William Jolitz.
8233203Stijl *
9233203Stijl * Redistribution and use in source and binary forms, with or without
10233203Stijl * modification, are permitted provided that the following conditions
11233203Stijl * are met:
12233203Stijl * 1. Redistributions of source code must retain the above copyright
13233203Stijl *    notice, this list of conditions and the following disclaimer.
14233203Stijl * 2. Redistributions in binary form must reproduce the above copyright
15233203Stijl *    notice, this list of conditions and the following disclaimer in the
16233203Stijl *    documentation and/or other materials provided with the distribution.
17233203Stijl * 4. Neither the name of the University nor the names of its contributors
18233203Stijl *    may be used to endorse or promote products derived from this software
19233203Stijl *    without specific prior written permission.
20233203Stijl *
21233203Stijl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22233203Stijl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233203Stijl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233203Stijl * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25233203Stijl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233203Stijl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233203Stijl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233203Stijl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233203Stijl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233203Stijl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233203Stijl * SUCH DAMAGE.
32233203Stijl *
33233203Stijl *	from: @(#)segments.h	7.1 (Berkeley) 5/9/91
34233203Stijl * $FreeBSD: releng/10.3/sys/x86/include/segments.h 255040 2013-08-29 19:52:18Z gibbs $
35233203Stijl */
36233203Stijl
37233203Stijl#ifndef _X86_SEGMENTS_H_
38233203Stijl#define	_X86_SEGMENTS_H_
39233203Stijl
40233203Stijl/*
41233203Stijl * X86 Segmentation Data Structures and definitions
42233203Stijl */
43233203Stijl
44233203Stijl/*
45233203Stijl * Selectors
46233203Stijl */
47233203Stijl#define	SEL_RPL_MASK	3		/* requester priv level */
48233203Stijl#define	ISPL(s)		((s)&3)		/* priority level of a selector */
49233203Stijl#ifdef XEN
50233203Stijl#define	SEL_KPL		1		/* kernel priority level */
51233203Stijl#else
52233203Stijl#define	SEL_KPL		0		/* kernel priority level */
53233203Stijl#endif
54233203Stijl#define	SEL_UPL		3		/* user priority level */
55233203Stijl#define	ISLDT(s)	((s)&SEL_LDT)	/* is it local or global */
56233203Stijl#define	SEL_LDT		4		/* local descriptor table */
57233203Stijl#define	IDXSEL(s)	(((s)>>3) & 0x1fff) /* index of selector */
58233203Stijl#define	LSEL(s,r)	(((s)<<3) | SEL_LDT | r) /* a local selector */
59233203Stijl#define	GSEL(s,r)	(((s)<<3) | r)	/* a global selector */
60233203Stijl
61233203Stijl/*
62233203Stijl * User segment descriptors (%cs, %ds etc for i386 apps. 64 bit wide)
63233203Stijl * For long-mode apps, %cs only has the conforming bit in sd_type, the sd_dpl,
64233203Stijl * sd_p, sd_l and sd_def32 which must be zero).  %ds only has sd_p.
65233203Stijl */
66233203Stijlstruct segment_descriptor {
67233203Stijl	unsigned sd_lolimit:16;		/* segment extent (lsb) */
68233203Stijl	unsigned sd_lobase:24;		/* segment base address (lsb) */
69233203Stijl	unsigned sd_type:5;		/* segment type */
70233203Stijl	unsigned sd_dpl:2;		/* segment descriptor priority level */
71233203Stijl	unsigned sd_p:1;		/* segment descriptor present */
72233203Stijl	unsigned sd_hilimit:4;		/* segment extent (msb) */
73233203Stijl	unsigned sd_xx:2;		/* unused */
74233203Stijl	unsigned sd_def32:1;		/* default 32 vs 16 bit size */
75233203Stijl	unsigned sd_gran:1;		/* limit granularity (byte/page units)*/
76233203Stijl	unsigned sd_hibase:8;		/* segment base address  (msb) */
77233203Stijl} __packed;
78233203Stijl
79233203Stijlstruct user_segment_descriptor {
80233203Stijl	unsigned sd_lolimit:16;		/* segment extent (lsb) */
81233203Stijl	unsigned sd_lobase:24;		/* segment base address (lsb) */
82233203Stijl	unsigned sd_type:5;		/* segment type */
83233203Stijl	unsigned sd_dpl:2;		/* segment descriptor priority level */
84233203Stijl	unsigned sd_p:1;		/* segment descriptor present */
85233203Stijl	unsigned sd_hilimit:4;		/* segment extent (msb) */
86233203Stijl	unsigned sd_xx:1;		/* unused */
87233203Stijl	unsigned sd_long:1;		/* long mode (cs only) */
88233203Stijl	unsigned sd_def32:1;		/* default 32 vs 16 bit size */
89233203Stijl	unsigned sd_gran:1;		/* limit granularity (byte/page units)*/
90233203Stijl	unsigned sd_hibase:8;		/* segment base address  (msb) */
91233203Stijl} __packed;
92233203Stijl
93233203Stijl#define	USD_GETBASE(sd)		(((sd)->sd_lobase) | (sd)->sd_hibase << 24)
94233203Stijl#define	USD_SETBASE(sd, b)	(sd)->sd_lobase = (b);	\
95233203Stijl				(sd)->sd_hibase = ((b) >> 24);
96233203Stijl#define	USD_GETLIMIT(sd)	(((sd)->sd_lolimit) | (sd)->sd_hilimit << 16)
97233203Stijl#define	USD_SETLIMIT(sd, l)	(sd)->sd_lolimit = (l);	\
98233203Stijl				(sd)->sd_hilimit = ((l) >> 16);
99233203Stijl
100233203Stijl#ifdef __i386__
101233203Stijl/*
102233203Stijl * Gate descriptors (e.g. indirect descriptors)
103233203Stijl */
104233203Stijlstruct gate_descriptor {
105233203Stijl	unsigned gd_looffset:16;	/* gate offset (lsb) */
106233203Stijl	unsigned gd_selector:16;	/* gate segment selector */
107233203Stijl	unsigned gd_stkcpy:5;		/* number of stack wds to cpy */
108233203Stijl	unsigned gd_xx:3;		/* unused */
109233203Stijl	unsigned gd_type:5;		/* segment type */
110233203Stijl	unsigned gd_dpl:2;		/* segment descriptor priority level */
111233203Stijl	unsigned gd_p:1;		/* segment descriptor present */
112233203Stijl	unsigned gd_hioffset:16;	/* gate offset (msb) */
113233203Stijl} __packed;
114233203Stijl
115233203Stijl/*
116233203Stijl * Generic descriptor
117233203Stijl */
118233203Stijlunion descriptor {
119233203Stijl	struct segment_descriptor sd;
120233203Stijl	struct gate_descriptor gd;
121233203Stijl};
122233203Stijl#else
123233203Stijl/*
124233203Stijl * Gate descriptors (e.g. indirect descriptors, trap, interrupt etc. 128 bit)
125233203Stijl * Only interrupt and trap gates have gd_ist.
126233203Stijl */
127233203Stijlstruct gate_descriptor {
128233203Stijl	uint64_t gd_looffset:16;	/* gate offset (lsb) */
129233203Stijl	uint64_t gd_selector:16;	/* gate segment selector */
130233203Stijl	uint64_t gd_ist:3;		/* IST table index */
131233203Stijl	uint64_t gd_xx:5;		/* unused */
132233203Stijl	uint64_t gd_type:5;		/* segment type */
133233203Stijl	uint64_t gd_dpl:2;		/* segment descriptor priority level */
134233203Stijl	uint64_t gd_p:1;		/* segment descriptor present */
135233203Stijl	uint64_t gd_hioffset:48;	/* gate offset (msb) */
136233203Stijl	uint64_t sd_xx1:32;
137233203Stijl} __packed;
138233203Stijl
139233203Stijl/*
140233203Stijl * Generic descriptor
141233203Stijl */
142233203Stijlunion descriptor {
143233203Stijl	struct user_segment_descriptor sd;
144233203Stijl	struct gate_descriptor gd;
145233203Stijl};
146233203Stijl#endif
147233203Stijl
148233203Stijl	/* system segments and gate types */
149233203Stijl#define	SDT_SYSNULL	 0	/* system null */
150233203Stijl#define	SDT_SYS286TSS	 1	/* system 286 TSS available */
151233203Stijl#define	SDT_SYSLDT	 2	/* system local descriptor table */
152233203Stijl#define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
153233203Stijl#define	SDT_SYS286CGT	 4	/* system 286 call gate */
154233203Stijl#define	SDT_SYSTASKGT	 5	/* system task gate */
155233203Stijl#define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
156233203Stijl#define	SDT_SYS286TGT	 7	/* system 286 trap gate */
157233203Stijl#define	SDT_SYSNULL2	 8	/* system null again */
158233203Stijl#define	SDT_SYS386TSS	 9	/* system 386 TSS available */
159233203Stijl#define	SDT_SYSTSS	 9	/* system available 64 bit TSS */
160233203Stijl#define	SDT_SYSNULL3	10	/* system null again */
161233203Stijl#define	SDT_SYS386BSY	11	/* system 386 TSS busy */
162233203Stijl#define	SDT_SYSBSY	11	/* system busy 64 bit TSS */
163233203Stijl#define	SDT_SYS386CGT	12	/* system 386 call gate */
164233203Stijl#define	SDT_SYSCGT	12	/* system 64 bit call gate */
165233203Stijl#define	SDT_SYSNULL4	13	/* system null again */
166233203Stijl#define	SDT_SYS386IGT	14	/* system 386 interrupt gate */
167233203Stijl#define	SDT_SYSIGT	14	/* system 64 bit interrupt gate */
168233203Stijl#define	SDT_SYS386TGT	15	/* system 386 trap gate */
169233203Stijl#define	SDT_SYSTGT	15	/* system 64 bit trap gate */
170233203Stijl
171233203Stijl	/* memory segment types */
172233203Stijl#define	SDT_MEMRO	16	/* memory read only */
173233203Stijl#define	SDT_MEMROA	17	/* memory read only accessed */
174233203Stijl#define	SDT_MEMRW	18	/* memory read write */
175233203Stijl#define	SDT_MEMRWA	19	/* memory read write accessed */
176233203Stijl#define	SDT_MEMROD	20	/* memory read only expand dwn limit */
177233203Stijl#define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
178233203Stijl#define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
179233203Stijl#define	SDT_MEMRWDA	23	/* memory read write expand dwn limit accessed*/
180233203Stijl#define	SDT_MEME	24	/* memory execute only */
181233203Stijl#define	SDT_MEMEA	25	/* memory execute only accessed */
182233203Stijl#define	SDT_MEMER	26	/* memory execute read */
183233203Stijl#define	SDT_MEMERA	27	/* memory execute read accessed */
184233203Stijl#define	SDT_MEMEC	28	/* memory execute only conforming */
185233203Stijl#define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
186233203Stijl#define	SDT_MEMERC	30	/* memory execute read conforming */
187233203Stijl#define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
188233203Stijl
189233203Stijl/*
190233203Stijl * Size of IDT table
191233203Stijl */
192233203Stijl#define	NIDT		256	/* 32 reserved, 0x80 syscall, most are h/w */
193233203Stijl#define	NRSVIDT		32	/* reserved entries for cpu exceptions */
194233203Stijl
195233203Stijl/*
196233203Stijl * Entries in the Interrupt Descriptor Table (IDT)
197233203Stijl */
198233203Stijl#define	IDT_DE		0	/* #DE: Divide Error */
199233203Stijl#define	IDT_DB		1	/* #DB: Debug */
200233203Stijl#define	IDT_NMI		2	/* Nonmaskable External Interrupt */
201233203Stijl#define	IDT_BP		3	/* #BP: Breakpoint */
202233203Stijl#define	IDT_OF		4	/* #OF: Overflow */
203233203Stijl#define	IDT_BR		5	/* #BR: Bound Range Exceeded */
204233203Stijl#define	IDT_UD		6	/* #UD: Undefined/Invalid Opcode */
205233203Stijl#define	IDT_NM		7	/* #NM: No Math Coprocessor */
206233203Stijl#define	IDT_DF		8	/* #DF: Double Fault */
207233203Stijl#define	IDT_FPUGP	9	/* Coprocessor Segment Overrun */
208233203Stijl#define	IDT_TS		10	/* #TS: Invalid TSS */
209233203Stijl#define	IDT_NP		11	/* #NP: Segment Not Present */
210233203Stijl#define	IDT_SS		12	/* #SS: Stack Segment Fault */
211233203Stijl#define	IDT_GP		13	/* #GP: General Protection Fault */
212233203Stijl#define	IDT_PF		14	/* #PF: Page Fault */
213233203Stijl#define	IDT_MF		16	/* #MF: FPU Floating-Point Error */
214233203Stijl#define	IDT_AC		17	/* #AC: Alignment Check */
215233203Stijl#define	IDT_MC		18	/* #MC: Machine Check */
216233203Stijl#define	IDT_XF		19	/* #XF: SIMD Floating-Point Exception */
217233203Stijl#define	IDT_IO_INTS	NRSVIDT	/* Base of IDT entries for I/O interrupts. */
218233203Stijl#define	IDT_SYSCALL	0x80	/* System Call Interrupt Vector */
219233613Sjhb#define	IDT_DTRACE_RET	0x92	/* DTrace pid provider Interrupt Vector */
220255040Sgibbs#define	IDT_EVTCHN	0x93	/* Xen HVM Event Channel Interrupt Vector */
221233203Stijl
222233203Stijl#if defined(__i386__) || defined(__ia64__)
223233203Stijl/*
224233203Stijl * Entries in the Global Descriptor Table (GDT)
225233203Stijl * Note that each 4 entries share a single 32 byte L1 cache line.
226233203Stijl * Some of the fast syscall instructions require a specific order here.
227233203Stijl */
228233203Stijl#define	GNULL_SEL	0	/* Null Descriptor */
229233203Stijl#define	GPRIV_SEL	1	/* SMP Per-Processor Private Data */
230233203Stijl#define	GUFS_SEL	2	/* User %fs Descriptor (order critical: 1) */
231233203Stijl#define	GUGS_SEL	3	/* User %gs Descriptor (order critical: 2) */
232233203Stijl#define	GCODE_SEL	4	/* Kernel Code Descriptor (order critical: 1) */
233233203Stijl#define	GDATA_SEL	5	/* Kernel Data Descriptor (order critical: 2) */
234233203Stijl#define	GUCODE_SEL	6	/* User Code Descriptor (order critical: 3) */
235233203Stijl#define	GUDATA_SEL	7	/* User Data Descriptor (order critical: 4) */
236233203Stijl#define	GBIOSLOWMEM_SEL	8	/* BIOS low memory access (must be entry 8) */
237233203Stijl#define	GPROC0_SEL	9	/* Task state process slot zero and up */
238233203Stijl#define	GLDT_SEL	10	/* Default User LDT */
239233203Stijl#define	GUSERLDT_SEL	11	/* User LDT */
240233203Stijl#define	GPANIC_SEL	12	/* Task state to consider panic from */
241233203Stijl#define	GBIOSCODE32_SEL	13	/* BIOS interface (32bit Code) */
242233203Stijl#define	GBIOSCODE16_SEL	14	/* BIOS interface (16bit Code) */
243233203Stijl#define	GBIOSDATA_SEL	15	/* BIOS interface (Data) */
244233203Stijl#define	GBIOSUTIL_SEL	16	/* BIOS interface (Utility) */
245233203Stijl#define	GBIOSARGS_SEL	17	/* BIOS interface (Arguments) */
246233203Stijl#define	GNDIS_SEL	18	/* For the NDIS layer */
247233203Stijl#ifdef XEN
248233203Stijl#define	NGDT		9
249233203Stijl#else
250233203Stijl#define	NGDT		19
251233203Stijl#endif
252233203Stijl
253233203Stijl/*
254233203Stijl * Entries in the Local Descriptor Table (LDT)
255233203Stijl */
256233203Stijl#define	LSYS5CALLS_SEL	0	/* forced by intel BCS */
257233203Stijl#define	LSYS5SIGR_SEL	1
258233203Stijl#define	L43BSDCALLS_SEL	2	/* notyet */
259233203Stijl#define	LUCODE_SEL	3
260233203Stijl#define	LSOL26CALLS_SEL	4	/* Solaris >= 2.6 system call gate */
261233203Stijl#define	LUDATA_SEL	5
262233203Stijl/* separate stack, es,fs,gs sels ? */
263233203Stijl/* #define	LPOSIXCALLS_SEL	5*/	/* notyet */
264233203Stijl#define	LBSDICALLS_SEL	16	/* BSDI system call gate */
265233203Stijl#define	NLDT		(LBSDICALLS_SEL + 1)
266233203Stijl
267233203Stijl#else /* !__i386__ && !__ia64__ */
268233203Stijl/*
269233203Stijl * Entries in the Global Descriptor Table (GDT)
270233203Stijl */
271233203Stijl#define	GNULL_SEL	0	/* Null Descriptor */
272233203Stijl#define	GNULL2_SEL	1	/* Null Descriptor */
273233203Stijl#define	GUFS32_SEL	2	/* User 32 bit %fs Descriptor */
274233203Stijl#define	GUGS32_SEL	3	/* User 32 bit %gs Descriptor */
275233203Stijl#define	GCODE_SEL	4	/* Kernel Code Descriptor */
276233203Stijl#define	GDATA_SEL	5	/* Kernel Data Descriptor */
277233203Stijl#define	GUCODE32_SEL	6	/* User 32 bit code Descriptor */
278233203Stijl#define	GUDATA_SEL	7	/* User 32/64 bit Data Descriptor */
279233203Stijl#define	GUCODE_SEL	8	/* User 64 bit Code Descriptor */
280233203Stijl#define	GPROC0_SEL	9	/* TSS for entering kernel etc */
281233203Stijl/* slot 10 is second half of GPROC0_SEL */
282233203Stijl#define	GUSERLDT_SEL	11	/* LDT */
283233203Stijl/* slot 12 is second half of GUSERLDT_SEL */
284233203Stijl#define	NGDT 		13
285233203Stijl#endif /* __i386__ || __ia64__ */
286233203Stijl
287233203Stijl#endif /* !_X86_SEGMENTS_H_ */
288