segments.h revision 209763
1226031Sstas/*-
2234027Sstas * Copyright (c) 1989, 1990 William F. Jolitz
3226031Sstas * Copyright (c) 1990 The Regents of the University of California.
4226031Sstas * All rights reserved.
5226031Sstas *
6226031Sstas * This code is derived from software contributed to Berkeley by
7226031Sstas * William Jolitz.
8226031Sstas *
9226031Sstas * Redistribution and use in source and binary forms, with or without
10226031Sstas * modification, are permitted provided that the following conditions
11226031Sstas * are met:
12226031Sstas * 1. Redistributions of source code must retain the above copyright
13226031Sstas *    notice, this list of conditions and the following disclaimer.
14226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
15226031Sstas *    notice, this list of conditions and the following disclaimer in the
16226031Sstas *    documentation and/or other materials provided with the distribution.
17226031Sstas * 4. Neither the name of the University nor the names of its contributors
18226031Sstas *    may be used to endorse or promote products derived from this software
19226031Sstas *    without specific prior written permission.
20226031Sstas *
21226031Sstas * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31226031Sstas * SUCH DAMAGE.
32226031Sstas *
33226031Sstas *	from: @(#)segments.h	7.1 (Berkeley) 5/9/91
34226031Sstas * $FreeBSD: head/sys/amd64/include/segments.h 209763 2010-07-07 12:08:58Z rpaulo $
35226031Sstas */
36226031Sstas
37226031Sstas#ifndef _MACHINE_SEGMENTS_H_
38226031Sstas#define	_MACHINE_SEGMENTS_H_
39226031Sstas
40226031Sstas/*
41226031Sstas * AMD64 Segmentation Data Structures and definitions
42226031Sstas */
43226031Sstas
44226031Sstas/*
45226031Sstas * Selectors
46226031Sstas */
47226031Sstas
48226031Sstas#define	SEL_RPL_MASK	3	/* requester priv level */
49226031Sstas#define	ISPL(s)	((s)&3)		/* what is the priority level of a selector */
50226031Sstas#define	SEL_KPL	0		/* kernel priority level */
51226031Sstas#define	SEL_UPL	3		/* user priority level */
52226031Sstas#define	ISLDT(s)	((s)&SEL_LDT)	/* is it local or global */
53226031Sstas#define	SEL_LDT	4		/* local descriptor table */
54226031Sstas#define	IDXSEL(s)	(((s)>>3) & 0x1fff)		/* index of selector */
55226031Sstas#define	LSEL(s,r)	(((s)<<3) | SEL_LDT | r)	/* a local selector */
56226031Sstas#define	GSEL(s,r)	(((s)<<3) | r)			/* a global selector */
57226031Sstas
58226031Sstas/*
59226031Sstas * User segment descriptors (%cs, %ds etc for compatability apps. 64 bit wide)
60226031Sstas * For long-mode apps, %cs only has the conforming bit in sd_type, the sd_dpl,
61226031Sstas * sd_p, sd_l and sd_def32 which must be zero).  %ds only has sd_p.
62226031Sstas */
63226031Sstasstruct	user_segment_descriptor {
64226031Sstas	u_int64_t sd_lolimit:16;	/* segment extent (lsb) */
65226031Sstas	u_int64_t sd_lobase:24;		/* segment base address (lsb) */
66226031Sstas	u_int64_t sd_type:5;		/* segment type */
67226031Sstas	u_int64_t sd_dpl:2;		/* segment descriptor priority level */
68226031Sstas	u_int64_t sd_p:1;		/* segment descriptor present */
69226031Sstas	u_int64_t sd_hilimit:4;		/* segment extent (msb) */
70226031Sstas	u_int64_t sd_xx:1;		/* unused */
71226031Sstas	u_int64_t sd_long:1;		/* long mode (cs only) */
72226031Sstas	u_int64_t sd_def32:1;		/* default 32 vs 16 bit size */
73226031Sstas	u_int64_t sd_gran:1;		/* limit granularity (byte/page units)*/
74226031Sstas	u_int64_t sd_hibase:8;		/* segment base address  (msb) */
75226031Sstas} __packed;
76226031Sstas
77226031Sstas#define	USD_GETBASE(sd)		(((sd)->sd_lobase) | (sd)->sd_hibase << 24)
78226031Sstas#define	USD_SETBASE(sd, b)	(sd)->sd_lobase = (b); 	\
79226031Sstas				(sd)->sd_hibase = ((b) >> 24);
80226031Sstas#define	USD_GETLIMIT(sd)	(((sd)->sd_lolimit) | (sd)->sd_hilimit << 16)
81226031Sstas#define	USD_SETLIMIT(sd, l)	(sd)->sd_lolimit = (l);	\
82226031Sstas				(sd)->sd_hilimit = ((l) >> 16);
83226031Sstas
84226031Sstas/*
85226031Sstas * System segment descriptors (128 bit wide)
86226031Sstas */
87226031Sstasstruct	system_segment_descriptor {
88226031Sstas	u_int64_t sd_lolimit:16;	/* segment extent (lsb) */
89226031Sstas	u_int64_t sd_lobase:24;		/* segment base address (lsb) */
90226031Sstas	u_int64_t sd_type:5;		/* segment type */
91226031Sstas	u_int64_t sd_dpl:2;		/* segment descriptor priority level */
92226031Sstas	u_int64_t sd_p:1;		/* segment descriptor present */
93226031Sstas	u_int64_t sd_hilimit:4;		/* segment extent (msb) */
94226031Sstas	u_int64_t sd_xx0:3;		/* unused */
95226031Sstas	u_int64_t sd_gran:1;		/* limit granularity (byte/page units)*/
96226031Sstas	u_int64_t sd_hibase:40 __packed;/* segment base address  (msb) */
97226031Sstas	u_int64_t sd_xx1:8;
98226031Sstas	u_int64_t sd_mbz:5;		/* MUST be zero */
99226031Sstas	u_int64_t sd_xx2:19;
100226031Sstas} __packed;
101226031Sstas
102226031Sstas/*
103226031Sstas * Gate descriptors (e.g. indirect descriptors, trap, interrupt etc. 128 bit)
104226031Sstas * Only interrupt and trap gates have gd_ist.
105226031Sstas */
106226031Sstasstruct	gate_descriptor {
107226031Sstas	u_int64_t gd_looffset:16;	/* gate offset (lsb) */
108226031Sstas	u_int64_t gd_selector:16;	/* gate segment selector */
109226031Sstas	u_int64_t gd_ist:3;		/* IST table index */
110226031Sstas	u_int64_t gd_xx:5;		/* unused */
111226031Sstas	u_int64_t gd_type:5;		/* segment type */
112226031Sstas	u_int64_t gd_dpl:2;		/* segment descriptor priority level */
113226031Sstas	u_int64_t gd_p:1;		/* segment descriptor present */
114226031Sstas	u_int64_t gd_hioffset:48 __packed;	/* gate offset (msb) */
115226031Sstas	u_int64_t sd_xx1:32;
116226031Sstas} __packed;
117226031Sstas
118226031Sstas/*
119226031Sstas * Generic descriptor
120226031Sstas */
121226031Sstasunion	descriptor	{
122226031Sstas	struct	user_segment_descriptor sd;
123226031Sstas	struct	gate_descriptor gd;
124226031Sstas};
125226031Sstas
126226031Sstas	/* system segments and gate types */
127226031Sstas#define	SDT_SYSNULL	 0	/* system null */
128226031Sstas#define	SDT_SYS286TSS	 1	/* system 286 TSS available */
129226031Sstas#define	SDT_SYSLDT	 2	/* system 64 bit local descriptor table */
130226031Sstas#define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
131226031Sstas#define	SDT_SYS286CGT	 4	/* system 286 call gate */
132226031Sstas#define	SDT_SYSTASKGT	 5	/* system task gate */
133226031Sstas#define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
134226031Sstas#define	SDT_SYS286TGT	 7	/* system 286 trap gate */
135226031Sstas#define	SDT_SYSNULL2	 8	/* system null again */
136226031Sstas#define	SDT_SYSTSS	 9	/* system available 64 bit TSS */
137226031Sstas#define	SDT_SYSNULL3	10	/* system null again */
138226031Sstas#define	SDT_SYSBSY	11	/* system busy 64 bit TSS */
139226031Sstas#define	SDT_SYSCGT	12	/* system 64 bit call gate */
140226031Sstas#define	SDT_SYSNULL4	13	/* system null again */
141226031Sstas#define	SDT_SYSIGT	14	/* system 64 bit interrupt gate */
142226031Sstas#define	SDT_SYSTGT	15	/* system 64 bit trap gate */
143226031Sstas
144226031Sstas	/* memory segment types */
145226031Sstas#define	SDT_MEMRO	16	/* memory read only */
146226031Sstas#define	SDT_MEMROA	17	/* memory read only accessed */
147226031Sstas#define	SDT_MEMRW	18	/* memory read write */
148226031Sstas#define	SDT_MEMRWA	19	/* memory read write accessed */
149226031Sstas#define	SDT_MEMROD	20	/* memory read only expand dwn limit */
150226031Sstas#define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
151226031Sstas#define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
152226031Sstas#define	SDT_MEMRWDA	23	/* memory read write expand dwn limit accessed */
153226031Sstas#define	SDT_MEME	24	/* memory execute only */
154226031Sstas#define	SDT_MEMEA	25	/* memory execute only accessed */
155226031Sstas#define	SDT_MEMER	26	/* memory execute read */
156226031Sstas#define	SDT_MEMERA	27	/* memory execute read accessed */
157226031Sstas#define	SDT_MEMEC	28	/* memory execute only conforming */
158226031Sstas#define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
159226031Sstas#define	SDT_MEMERC	30	/* memory execute read conforming */
160226031Sstas#define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
161226031Sstas
162226031Sstas/*
163226031Sstas * Software definitions are in this convenient format,
164226031Sstas * which are translated into inconvenient segment descriptors
165226031Sstas * when needed to be used by the 386 hardware
166226031Sstas */
167226031Sstas
168226031Sstasstruct	soft_segment_descriptor {
169226031Sstas	unsigned long ssd_base;		/* segment base address  */
170226031Sstas	unsigned long ssd_limit;	/* segment extent */
171226031Sstas	unsigned long ssd_type:5;	/* segment type */
172226031Sstas	unsigned long ssd_dpl:2;	/* segment descriptor priority level */
173226031Sstas	unsigned long ssd_p:1;		/* segment descriptor present */
174226031Sstas	unsigned long ssd_long:1;	/* long mode (for %cs) */
175226031Sstas	unsigned long ssd_def32:1;	/* default 32 vs 16 bit size */
176226031Sstas	unsigned long ssd_gran:1;	/* limit granularity (byte/page units)*/
177226031Sstas} __packed;
178226031Sstas
179226031Sstas/*
180226031Sstas * region descriptors, used to load gdt/idt tables before segments yet exist.
181226031Sstas */
182226031Sstasstruct region_descriptor {
183226031Sstas	unsigned long rd_limit:16;		/* segment extent */
184226031Sstas	unsigned long rd_base:64 __packed;	/* base address  */
185226031Sstas} __packed;
186226031Sstas
187226031Sstas/*
188226031Sstas * Size of IDT table
189226031Sstas */
190226031Sstas#define	NIDT	256		/* 32 reserved, 16 h/w, 0 s/w, linux's 0x80 */
191226031Sstas#define	NRSVIDT	32		/* reserved entries for cpu exceptions */
192226031Sstas
193226031Sstas/*
194226031Sstas * Entries in the Interrupt Descriptor Table (IDT)
195226031Sstas */
196226031Sstas#define	IDT_DE		0	/* #DE: Divide Error */
197226031Sstas#define	IDT_DB		1	/* #DB: Debug */
198226031Sstas#define	IDT_NMI		2	/* Nonmaskable External Interrupt */
199226031Sstas#define	IDT_BP		3	/* #BP: Breakpoint */
200226031Sstas#define	IDT_OF		4	/* #OF: Overflow */
201226031Sstas#define	IDT_BR		5	/* #BR: Bound Range Exceeded */
202226031Sstas#define	IDT_UD		6	/* #UD: Undefined/Invalid Opcode */
203226031Sstas#define	IDT_NM		7	/* #NM: No Math Coprocessor */
204226031Sstas#define	IDT_DF		8	/* #DF: Double Fault */
205226031Sstas#define	IDT_FPUGP	9	/* Coprocessor Segment Overrun */
206226031Sstas#define	IDT_TS		10	/* #TS: Invalid TSS */
207226031Sstas#define	IDT_NP		11	/* #NP: Segment Not Present */
208226031Sstas#define	IDT_SS		12	/* #SS: Stack Segment Fault */
209226031Sstas#define	IDT_GP		13	/* #GP: General Protection Fault */
210226031Sstas#define	IDT_PF		14	/* #PF: Page Fault */
211226031Sstas#define	IDT_MF		16	/* #MF: FPU Floating-Point Error */
212226031Sstas#define	IDT_AC		17	/* #AC: Alignment Check */
213226031Sstas#define	IDT_MC		18	/* #MC: Machine Check */
214226031Sstas#define	IDT_XF		19	/* #XF: SIMD Floating-Point Exception */
215226031Sstas#define	IDT_IO_INTS	NRSVIDT	/* Base of IDT entries for I/O interrupts. */
216226031Sstas#define	IDT_SYSCALL	0x80	/* System Call Interrupt Vector */
217226031Sstas
218226031Sstas/*
219226031Sstas * Entries in the Global Descriptor Table (GDT)
220226031Sstas */
221226031Sstas#define	GNULL_SEL	0	/* Null Descriptor */
222226031Sstas#define	GNULL2_SEL	1	/* Null Descriptor */
223226031Sstas#define	GUFS32_SEL	2	/* User 32 bit %fs Descriptor */
224226031Sstas#define	GUGS32_SEL	3	/* User 32 bit %gs Descriptor */
225226031Sstas#define	GCODE_SEL	4	/* Kernel Code Descriptor */
226226031Sstas#define	GDATA_SEL	5	/* Kernel Data Descriptor */
227226031Sstas#define	GUCODE32_SEL	6	/* User 32 bit code Descriptor */
228226031Sstas#define	GUDATA_SEL	7	/* User 32/64 bit Data Descriptor */
229226031Sstas#define	GUCODE_SEL	8	/* User 64 bit Code Descriptor */
230226031Sstas#define	GPROC0_SEL	9	/* TSS for entering kernel etc */
231226031Sstas/* slot 10 is second half of GPROC0_SEL */
232226031Sstas#define	GUSERLDT_SEL	11	/* LDT */
233226031Sstas/* slot 11 is second half of GUSERLDT_SEL */
234226031Sstas#define	NGDT 		13
235226031Sstas
236226031Sstas#ifdef _KERNEL
237226031Sstasextern struct user_segment_descriptor gdt[];
238226031Sstasextern struct soft_segment_descriptor gdt_segs[];
239226031Sstasextern struct gate_descriptor *idt;
240226031Sstasextern struct region_descriptor r_gdt, r_idt;
241226031Sstas
242226031Sstasvoid	lgdt(struct region_descriptor *rdp);
243226031Sstasvoid	sdtossd(struct user_segment_descriptor *sdp,
244226031Sstas	    struct soft_segment_descriptor *ssdp);
245226031Sstasvoid	ssdtosd(struct soft_segment_descriptor *ssdp,
246226031Sstas	    struct user_segment_descriptor *sdp);
247226031Sstasvoid	ssdtosyssd(struct soft_segment_descriptor *ssdp,
248226031Sstas	    struct system_segment_descriptor *sdp);
249226031Sstasvoid	update_gdt_gsbase(struct thread *td, uint32_t base);
250226031Sstasvoid	update_gdt_fsbase(struct thread *td, uint32_t base);
251226031Sstas
252226031Sstas#endif /* _KERNEL */
253226031Sstas
254226031Sstas#endif /* !_MACHINE_SEGMENTS_H_ */
255226031Sstas