xref: /original-bsd/sys/i386/include/segments.h (revision 8a8e718f)
1 /*-
2  * Copyright (c) 1989, 1990 William F. Jolitz
3  * Copyright (c) 1990, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)segments.h	8.1 (Berkeley) 06/11/93
12  */
13 
14 /*
15  * 386 Segmentation Data Structures and definitions
16  *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
17  */
18 
19 /*
20  * Selectors
21  */
22 
23 #define	ISPL(s)	((s)&3)		/* what is the priority level of a selector */
24 #define	SEL_KPL	0		/* kernel priority level */
25 #define	SEL_UPL	3		/* user priority level */
26 #define	ISLDT(s)	((s)&SEL_LDT)	/* is it local or global */
27 #define	SEL_LDT	4		/* local descriptor table */
28 #define	IDXSEL(s)	(((s)>>3) & 0x1fff)		/* index of selector */
29 #define	LSEL(s,r)	(((s)<<3) | SEL_LDT | r)	/* a local selector */
30 #define	GSEL(s,r)	(((s)<<3) | r)			/* a global selector */
31 
32 /*
33  * Memory and System segment descriptors
34  */
35 struct	segment_descriptor	{
36 	unsigned sd_lolimit:16 ;	/* segment extent (lsb) */
37 	unsigned sd_lobase:24 ;		/* segment base address (lsb) */
38 	unsigned sd_type:5 ;		/* segment type */
39 	unsigned sd_dpl:2 ;		/* segment descriptor priority level */
40 	unsigned sd_p:1 ;		/* segment descriptor present */
41 	unsigned sd_hilimit:4 ;		/* segment extent (msb) */
42 	unsigned sd_xx:2 ;		/* unused */
43 	unsigned sd_def32:1 ;		/* default 32 vs 16 bit size */
44 	unsigned sd_gran:1 ;		/* limit granularity (byte/page units)*/
45 	unsigned sd_hibase:8 ;		/* segment base address  (msb) */
46 } ;
47 
48 /*
49  * Gate descriptors (e.g. indirect descriptors)
50  */
51 struct	gate_descriptor	{
52 	unsigned gd_looffset:16 ;	/* gate offset (lsb) */
53 	unsigned gd_selector:16 ;	/* gate segment selector */
54 	unsigned gd_stkcpy:5 ;		/* number of stack wds to cpy */
55 	unsigned gd_xx:3 ;		/* unused */
56 	unsigned gd_type:5 ;		/* segment type */
57 	unsigned gd_dpl:2 ;		/* segment descriptor priority level */
58 	unsigned gd_p:1 ;		/* segment descriptor present */
59 	unsigned gd_hioffset:16 ;	/* gate offset (msb) */
60 } ;
61 
62 /*
63  * Generic descriptor
64  */
65 union	descriptor	{
66 	struct	segment_descriptor sd;
67 	struct	gate_descriptor gd;
68 };
69 #define	d_type	gd.gd_type
70 
71 	/* system segments and gate types */
72 #define	SDT_SYSNULL	 0	/* system null */
73 #define	SDT_SYS286TSS	 1	/* system 286 TSS available */
74 #define	SDT_SYSLDT	 2	/* system local descriptor table */
75 #define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
76 #define	SDT_SYS286CGT	 4	/* system 286 call gate */
77 #define	SDT_SYSTASKGT	 5	/* system task gate */
78 #define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
79 #define	SDT_SYS286TGT	 7	/* system 286 trap gate */
80 #define	SDT_SYSNULL2	 8	/* system null again */
81 #define	SDT_SYS386TSS	 9	/* system 386 TSS available */
82 #define	SDT_SYSNULL3	10	/* system null again */
83 #define	SDT_SYS386BSY	11	/* system 386 TSS busy */
84 #define	SDT_SYS386CGT	12	/* system 386 call gate */
85 #define	SDT_SYSNULL4	13	/* system null again */
86 #define	SDT_SYS386IGT	14	/* system 386 interrupt gate */
87 #define	SDT_SYS386TGT	15	/* system 386 trap gate */
88 
89 	/* memory segment types */
90 #define	SDT_MEMRO	16	/* memory read only */
91 #define	SDT_MEMROA	17	/* memory read only accessed */
92 #define	SDT_MEMRW	18	/* memory read write */
93 #define	SDT_MEMRWA	19	/* memory read write accessed */
94 #define	SDT_MEMROD	20	/* memory read only expand dwn limit */
95 #define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
96 #define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
97 #define	SDT_MEMRWDA	23	/* memory read write expand dwn limit acessed */
98 #define	SDT_MEME	24	/* memory execute only */
99 #define	SDT_MEMEA	25	/* memory execute only accessed */
100 #define	SDT_MEMER	26	/* memory execute read */
101 #define	SDT_MEMERA	27	/* memory execute read accessed */
102 #define	SDT_MEMEC	28	/* memory execute only conforming */
103 #define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
104 #define	SDT_MEMERC	30	/* memory execute read conforming */
105 #define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
106 
107 /* is memory segment descriptor pointer ? */
108 #define ISMEMSDP(s)	((s->d_type) >= SDT_MEMRO && (s->d_type) <= SDT_MEMERAC)
109 
110 /* is 286 gate descriptor pointer ? */
111 #define IS286GDP(s)	(((s->d_type) >= SDT_SYS286CGT \
112 				 && (s->d_type) < SDT_SYS286TGT))
113 
114 /* is 386 gate descriptor pointer ? */
115 #define IS386GDP(s)	(((s->d_type) >= SDT_SYS386CGT \
116 				&& (s->d_type) < SDT_SYS386TGT))
117 
118 /* is gate descriptor pointer ? */
119 #define ISGDP(s)	(IS286GDP(s) || IS386GDP(s))
120 
121 /* is segment descriptor pointer ? */
122 #define ISSDP(s)	(ISMEMSDP(s) || !ISGDP(s))
123 
124 /* is system segment descriptor pointer ? */
125 #define ISSYSSDP(s)	(!ISMEMSDP(s) && !ISGDP(s))
126 
127 /*
128  * Software definitions are in this convenient format,
129  * which are translated into inconvenient segment descriptors
130  * when needed to be used by the 386 hardware
131  */
132 
133 struct	soft_segment_descriptor	{
134 	unsigned ssd_base ;		/* segment base address  */
135 	unsigned ssd_limit ;		/* segment extent */
136 	unsigned ssd_type:5 ;		/* segment type */
137 	unsigned ssd_dpl:2 ;		/* segment descriptor priority level */
138 	unsigned ssd_p:1 ;		/* segment descriptor present */
139 	unsigned ssd_xx:4 ;		/* unused */
140 	unsigned ssd_xx1:2 ;		/* unused */
141 	unsigned ssd_def32:1 ;		/* default 32 vs 16 bit size */
142 	unsigned ssd_gran:1 ;		/* limit granularity (byte/page units)*/
143 };
144 
145 extern ssdtosd() ;	/* to decode a ssd */
146 extern sdtossd() ;	/* to encode a sd */
147 
148 /*
149  * region descriptors, used to load gdt/idt tables before segments yet exist
150  */
151 struct region_descriptor {
152 	unsigned rd_limit:16 ;		/* segment extent */
153 	char *rd_base;			/* base address  */
154 };
155 
156 /*
157  * Segment Protection Exception code bits
158  */
159 
160 #define	SEGEX_EXT	0x01	/* recursive or externally induced */
161 #define	SEGEX_IDT	0x02	/* interrupt descriptor table */
162 #define	SEGEX_TI	0x04	/* local descriptor table */
163 				/* other bits are affected descriptor index */
164 #define SEGEX_IDX(s)	((s)>>3)&0x1fff)
165