segments.h revision 4
14Srgrimes/*-
24Srgrimes * Copyright (c) 1989, 1990 William F. Jolitz
34Srgrimes * Copyright (c) 1990 The Regents of the University of California.
44Srgrimes * All rights reserved.
54Srgrimes *
64Srgrimes * This code is derived from software contributed to Berkeley by
74Srgrimes * William Jolitz.
84Srgrimes *
94Srgrimes * Redistribution and use in source and binary forms, with or without
104Srgrimes * modification, are permitted provided that the following conditions
114Srgrimes * are met:
124Srgrimes * 1. Redistributions of source code must retain the above copyright
134Srgrimes *    notice, this list of conditions and the following disclaimer.
144Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
154Srgrimes *    notice, this list of conditions and the following disclaimer in the
164Srgrimes *    documentation and/or other materials provided with the distribution.
174Srgrimes * 3. All advertising materials mentioning features or use of this software
184Srgrimes *    must display the following acknowledgement:
194Srgrimes *	This product includes software developed by the University of
204Srgrimes *	California, Berkeley and its contributors.
214Srgrimes * 4. Neither the name of the University nor the names of its contributors
224Srgrimes *    may be used to endorse or promote products derived from this software
234Srgrimes *    without specific prior written permission.
244Srgrimes *
254Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
264Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
274Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
284Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
294Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
304Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
314Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
324Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
334Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
344Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
354Srgrimes * SUCH DAMAGE.
364Srgrimes *
374Srgrimes *	@(#)segments.h	7.1 (Berkeley) 5/9/91
384Srgrimes */
394Srgrimes
404Srgrimes/*
414Srgrimes * 386 Segmentation Data Structures and definitions
424Srgrimes *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
434Srgrimes */
444Srgrimes
454Srgrimes/*
464Srgrimes * Selectors
474Srgrimes */
484Srgrimes
494Srgrimes#define	ISPL(s)	((s)&3)		/* what is the priority level of a selector */
504Srgrimes#define	SEL_KPL	0		/* kernel priority level */
514Srgrimes#define	SEL_UPL	3		/* user priority level */
524Srgrimes#define	ISLDT(s)	((s)&SEL_LDT)	/* is it local or global */
534Srgrimes#define	SEL_LDT	4		/* local descriptor table */
544Srgrimes#define	IDXSEL(s)	(((s)>>3) & 0x1fff)		/* index of selector */
554Srgrimes#define	LSEL(s,r)	(((s)<<3) | SEL_LDT | r)	/* a local selector */
564Srgrimes#define	GSEL(s,r)	(((s)<<3) | r)			/* a global selector */
574Srgrimes
584Srgrimes/*
594Srgrimes * Memory and System segment descriptors
604Srgrimes */
614Srgrimesstruct	segment_descriptor	{
624Srgrimes	unsigned sd_lolimit:16 ;	/* segment extent (lsb) */
634Srgrimes	unsigned sd_lobase:24 ;		/* segment base address (lsb) */
644Srgrimes	unsigned sd_type:5 ;		/* segment type */
654Srgrimes	unsigned sd_dpl:2 ;		/* segment descriptor priority level */
664Srgrimes	unsigned sd_p:1 ;		/* segment descriptor present */
674Srgrimes	unsigned sd_hilimit:4 ;		/* segment extent (msb) */
684Srgrimes	unsigned sd_xx:2 ;		/* unused */
694Srgrimes	unsigned sd_def32:1 ;		/* default 32 vs 16 bit size */
704Srgrimes	unsigned sd_gran:1 ;		/* limit granularity (byte/page units)*/
714Srgrimes	unsigned sd_hibase:8 ;		/* segment base address  (msb) */
724Srgrimes} ;
734Srgrimes
744Srgrimes/*
754Srgrimes * Gate descriptors (e.g. indirect descriptors)
764Srgrimes */
774Srgrimesstruct	gate_descriptor	{
784Srgrimes	unsigned gd_looffset:16 ;	/* gate offset (lsb) */
794Srgrimes	unsigned gd_selector:16 ;	/* gate segment selector */
804Srgrimes	unsigned gd_stkcpy:5 ;		/* number of stack wds to cpy */
814Srgrimes	unsigned gd_xx:3 ;		/* unused */
824Srgrimes	unsigned gd_type:5 ;		/* segment type */
834Srgrimes	unsigned gd_dpl:2 ;		/* segment descriptor priority level */
844Srgrimes	unsigned gd_p:1 ;		/* segment descriptor present */
854Srgrimes	unsigned gd_hioffset:16 ;	/* gate offset (msb) */
864Srgrimes} ;
874Srgrimes
884Srgrimes/*
894Srgrimes * Generic descriptor
904Srgrimes */
914Srgrimesunion	descriptor	{
924Srgrimes	struct	segment_descriptor sd;
934Srgrimes	struct	gate_descriptor gd;
944Srgrimes};
954Srgrimes
964Srgrimes	/* system segments and gate types */
974Srgrimes#define	SDT_SYSNULL	 0	/* system null */
984Srgrimes#define	SDT_SYS286TSS	 1	/* system 286 TSS available */
994Srgrimes#define	SDT_SYSLDT	 2	/* system local descriptor table */
1004Srgrimes#define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
1014Srgrimes#define	SDT_SYS286CGT	 4	/* system 286 call gate */
1024Srgrimes#define	SDT_SYSTASKGT	 5	/* system task gate */
1034Srgrimes#define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
1044Srgrimes#define	SDT_SYS286TGT	 7	/* system 286 trap gate */
1054Srgrimes#define	SDT_SYSNULL2	 8	/* system null again */
1064Srgrimes#define	SDT_SYS386TSS	 9	/* system 386 TSS available */
1074Srgrimes#define	SDT_SYSNULL3	10	/* system null again */
1084Srgrimes#define	SDT_SYS386BSY	11	/* system 386 TSS busy */
1094Srgrimes#define	SDT_SYS386CGT	12	/* system 386 call gate */
1104Srgrimes#define	SDT_SYSNULL4	13	/* system null again */
1114Srgrimes#define	SDT_SYS386IGT	14	/* system 386 interrupt gate */
1124Srgrimes#define	SDT_SYS386TGT	15	/* system 386 trap gate */
1134Srgrimes
1144Srgrimes	/* memory segment types */
1154Srgrimes#define	SDT_MEMRO	16	/* memory read only */
1164Srgrimes#define	SDT_MEMROA	17	/* memory read only accessed */
1174Srgrimes#define	SDT_MEMRW	18	/* memory read write */
1184Srgrimes#define	SDT_MEMRWA	19	/* memory read write accessed */
1194Srgrimes#define	SDT_MEMROD	20	/* memory read only expand dwn limit */
1204Srgrimes#define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
1214Srgrimes#define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
1224Srgrimes#define	SDT_MEMRWDA	23	/* memory read write expand dwn limit acessed */
1234Srgrimes#define	SDT_MEME	24	/* memory execute only */
1244Srgrimes#define	SDT_MEMEA	25	/* memory execute only accessed */
1254Srgrimes#define	SDT_MEMER	26	/* memory execute read */
1264Srgrimes#define	SDT_MEMERA	27	/* memory execute read accessed */
1274Srgrimes#define	SDT_MEMEC	28	/* memory execute only conforming */
1284Srgrimes#define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
1294Srgrimes#define	SDT_MEMERC	30	/* memory execute read conforming */
1304Srgrimes#define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
1314Srgrimes
1324Srgrimes/* is memory segment descriptor pointer ? */
1334Srgrimes#define ISMEMSDP(s)	((s->d_type) >= SDT_MEMRO && (s->d_type) <= SDT_MEMERAC)
1344Srgrimes
1354Srgrimes/* is 286 gate descriptor pointer ? */
1364Srgrimes#define IS286GDP(s)	(((s->d_type) >= SDT_SYS286CGT \
1374Srgrimes				 && (s->d_type) < SDT_SYS286TGT))
1384Srgrimes
1394Srgrimes/* is 386 gate descriptor pointer ? */
1404Srgrimes#define IS386GDP(s)	(((s->d_type) >= SDT_SYS386CGT \
1414Srgrimes				&& (s->d_type) < SDT_SYS386TGT))
1424Srgrimes
1434Srgrimes/* is gate descriptor pointer ? */
1444Srgrimes#define ISGDP(s)	(IS286GDP(s) || IS386GDP(s))
1454Srgrimes
1464Srgrimes/* is segment descriptor pointer ? */
1474Srgrimes#define ISSDP(s)	(ISMEMSDP(s) || !ISGDP(s))
1484Srgrimes
1494Srgrimes/* is system segment descriptor pointer ? */
1504Srgrimes#define ISSYSSDP(s)	(!ISMEMSDP(s) && !ISGDP(s))
1514Srgrimes
1524Srgrimes/*
1534Srgrimes * Software definitions are in this convenient format,
1544Srgrimes * which are translated into inconvenient segment descriptors
1554Srgrimes * when needed to be used by the 386 hardware
1564Srgrimes */
1574Srgrimes
1584Srgrimesstruct	soft_segment_descriptor	{
1594Srgrimes	unsigned ssd_base ;		/* segment base address  */
1604Srgrimes	unsigned ssd_limit ;		/* segment extent */
1614Srgrimes	unsigned ssd_type:5 ;		/* segment type */
1624Srgrimes	unsigned ssd_dpl:2 ;		/* segment descriptor priority level */
1634Srgrimes	unsigned ssd_p:1 ;		/* segment descriptor present */
1644Srgrimes	unsigned ssd_xx:4 ;		/* unused */
1654Srgrimes	unsigned ssd_xx1:2 ;		/* unused */
1664Srgrimes	unsigned ssd_def32:1 ;		/* default 32 vs 16 bit size */
1674Srgrimes	unsigned ssd_gran:1 ;		/* limit granularity (byte/page units)*/
1684Srgrimes};
1694Srgrimes
1704Srgrimesextern ssdtosd() ;	/* to decode a ssd */
1714Srgrimesextern sdtossd() ;	/* to encode a sd */
1724Srgrimes
1734Srgrimes/*
1744Srgrimes * region descriptors, used to load gdt/idt tables before segments yet exist.
1754Srgrimes */
1764Srgrimesstruct region_descriptor {
1774Srgrimes	unsigned rd_limit:16;		/* segment extent */
1784Srgrimes	unsigned rd_base:32;		/* base address  */
1794Srgrimes};
1804Srgrimes
1814Srgrimes/*
1824Srgrimes * Segment Protection Exception code bits
1834Srgrimes */
1844Srgrimes
1854Srgrimes#define	SEGEX_EXT	0x01	/* recursive or externally induced */
1864Srgrimes#define	SEGEX_IDT	0x02	/* interrupt descriptor table */
1874Srgrimes#define	SEGEX_TI	0x04	/* local descriptor table */
1884Srgrimes				/* other bits are affected descriptor index */
1894Srgrimes#define SEGEX_IDX(s)	((s)>>3)&0x1fff)
1904Srgrimes
1914Srgrimes/*
1924Srgrimes * Size of IDT table
1934Srgrimes */
1944Srgrimes
1954Srgrimes#define	NIDT	256
1964Srgrimes#define	NRSVIDT	32		/* reserved entries for cpu exceptions */
197