segments.h revision 25164
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 *
37621Srgrimes *	from: @(#)segments.h	7.1 (Berkeley) 5/9/91
3825164Speter *	$Id: segments.h,v 1.13 1997/02/22 09:35:09 peter Exp $
394Srgrimes */
404Srgrimes
41719Swollman#ifndef _MACHINE_SEGMENTS_H_
424501Sbde#define	_MACHINE_SEGMENTS_H_
43719Swollman
444Srgrimes/*
454Srgrimes * 386 Segmentation Data Structures and definitions
464Srgrimes *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
474Srgrimes */
484Srgrimes
494Srgrimes/*
504Srgrimes * Selectors
514Srgrimes */
524Srgrimes
534Srgrimes#define	ISPL(s)	((s)&3)		/* what is the priority level of a selector */
548876Srgrimes#define	SEL_KPL	0		/* kernel priority level */
558876Srgrimes#define	SEL_UPL	3		/* user priority level */
564Srgrimes#define	ISLDT(s)	((s)&SEL_LDT)	/* is it local or global */
578876Srgrimes#define	SEL_LDT	4		/* local descriptor table */
584Srgrimes#define	IDXSEL(s)	(((s)>>3) & 0x1fff)		/* index of selector */
594Srgrimes#define	LSEL(s,r)	(((s)<<3) | SEL_LDT | r)	/* a local selector */
604Srgrimes#define	GSEL(s,r)	(((s)<<3) | r)			/* a global selector */
614Srgrimes
624Srgrimes/*
634Srgrimes * Memory and System segment descriptors
644Srgrimes */
654Srgrimesstruct	segment_descriptor	{
664Srgrimes	unsigned sd_lolimit:16 ;	/* segment extent (lsb) */
671051Sdg	unsigned sd_lobase:24 __attribute__ ((packed));
681051Sdg					/* segment base address (lsb) */
694Srgrimes	unsigned sd_type:5 ;		/* segment type */
704Srgrimes	unsigned sd_dpl:2 ;		/* segment descriptor priority level */
714Srgrimes	unsigned sd_p:1 ;		/* segment descriptor present */
724Srgrimes	unsigned sd_hilimit:4 ;		/* segment extent (msb) */
734Srgrimes	unsigned sd_xx:2 ;		/* unused */
744Srgrimes	unsigned sd_def32:1 ;		/* default 32 vs 16 bit size */
754Srgrimes	unsigned sd_gran:1 ;		/* limit granularity (byte/page units)*/
764Srgrimes	unsigned sd_hibase:8 ;		/* segment base address  (msb) */
774Srgrimes} ;
784Srgrimes
794Srgrimes/*
804Srgrimes * Gate descriptors (e.g. indirect descriptors)
814Srgrimes */
824Srgrimesstruct	gate_descriptor	{
834Srgrimes	unsigned gd_looffset:16 ;	/* gate offset (lsb) */
844Srgrimes	unsigned gd_selector:16 ;	/* gate segment selector */
854Srgrimes	unsigned gd_stkcpy:5 ;		/* number of stack wds to cpy */
864Srgrimes	unsigned gd_xx:3 ;		/* unused */
874Srgrimes	unsigned gd_type:5 ;		/* segment type */
884Srgrimes	unsigned gd_dpl:2 ;		/* segment descriptor priority level */
894Srgrimes	unsigned gd_p:1 ;		/* segment descriptor present */
904Srgrimes	unsigned gd_hioffset:16 ;	/* gate offset (msb) */
914Srgrimes} ;
924Srgrimes
934Srgrimes/*
944Srgrimes * Generic descriptor
954Srgrimes */
964Srgrimesunion	descriptor	{
974Srgrimes	struct	segment_descriptor sd;
984Srgrimes	struct	gate_descriptor gd;
994Srgrimes};
1004Srgrimes
1014Srgrimes	/* system segments and gate types */
1024Srgrimes#define	SDT_SYSNULL	 0	/* system null */
1034Srgrimes#define	SDT_SYS286TSS	 1	/* system 286 TSS available */
1044Srgrimes#define	SDT_SYSLDT	 2	/* system local descriptor table */
1054Srgrimes#define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
1064Srgrimes#define	SDT_SYS286CGT	 4	/* system 286 call gate */
1074Srgrimes#define	SDT_SYSTASKGT	 5	/* system task gate */
1084Srgrimes#define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
1094Srgrimes#define	SDT_SYS286TGT	 7	/* system 286 trap gate */
1104Srgrimes#define	SDT_SYSNULL2	 8	/* system null again */
1114Srgrimes#define	SDT_SYS386TSS	 9	/* system 386 TSS available */
1124Srgrimes#define	SDT_SYSNULL3	10	/* system null again */
1134Srgrimes#define	SDT_SYS386BSY	11	/* system 386 TSS busy */
1144Srgrimes#define	SDT_SYS386CGT	12	/* system 386 call gate */
1154Srgrimes#define	SDT_SYSNULL4	13	/* system null again */
1164Srgrimes#define	SDT_SYS386IGT	14	/* system 386 interrupt gate */
1174Srgrimes#define	SDT_SYS386TGT	15	/* system 386 trap gate */
1184Srgrimes
1194Srgrimes	/* memory segment types */
1204Srgrimes#define	SDT_MEMRO	16	/* memory read only */
1214Srgrimes#define	SDT_MEMROA	17	/* memory read only accessed */
1224Srgrimes#define	SDT_MEMRW	18	/* memory read write */
1234Srgrimes#define	SDT_MEMRWA	19	/* memory read write accessed */
1244Srgrimes#define	SDT_MEMROD	20	/* memory read only expand dwn limit */
1254Srgrimes#define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
1264Srgrimes#define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
12713765Smpp#define	SDT_MEMRWDA	23	/* memory read write expand dwn limit accessed */
1284Srgrimes#define	SDT_MEME	24	/* memory execute only */
1294Srgrimes#define	SDT_MEMEA	25	/* memory execute only accessed */
1304Srgrimes#define	SDT_MEMER	26	/* memory execute read */
1314Srgrimes#define	SDT_MEMERA	27	/* memory execute read accessed */
1324Srgrimes#define	SDT_MEMEC	28	/* memory execute only conforming */
1334Srgrimes#define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
1344Srgrimes#define	SDT_MEMERC	30	/* memory execute read conforming */
1354Srgrimes#define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
1364Srgrimes
1374Srgrimes/* is memory segment descriptor pointer ? */
1384Srgrimes#define ISMEMSDP(s)	((s->d_type) >= SDT_MEMRO && (s->d_type) <= SDT_MEMERAC)
1394Srgrimes
1404Srgrimes/* is 286 gate descriptor pointer ? */
1414Srgrimes#define IS286GDP(s)	(((s->d_type) >= SDT_SYS286CGT \
1424Srgrimes				 && (s->d_type) < SDT_SYS286TGT))
1434Srgrimes
1444Srgrimes/* is 386 gate descriptor pointer ? */
1454Srgrimes#define IS386GDP(s)	(((s->d_type) >= SDT_SYS386CGT \
1464Srgrimes				&& (s->d_type) < SDT_SYS386TGT))
1474Srgrimes
1484Srgrimes/* is gate descriptor pointer ? */
1494Srgrimes#define ISGDP(s)	(IS286GDP(s) || IS386GDP(s))
1504Srgrimes
1514Srgrimes/* is segment descriptor pointer ? */
1524Srgrimes#define ISSDP(s)	(ISMEMSDP(s) || !ISGDP(s))
1534Srgrimes
1544Srgrimes/* is system segment descriptor pointer ? */
1554Srgrimes#define ISSYSSDP(s)	(!ISMEMSDP(s) && !ISGDP(s))
1564Srgrimes
1574Srgrimes/*
1584Srgrimes * Software definitions are in this convenient format,
1594Srgrimes * which are translated into inconvenient segment descriptors
1604Srgrimes * when needed to be used by the 386 hardware
1614Srgrimes */
1624Srgrimes
1634Srgrimesstruct	soft_segment_descriptor	{
1644Srgrimes	unsigned ssd_base ;		/* segment base address  */
1654Srgrimes	unsigned ssd_limit ;		/* segment extent */
1664Srgrimes	unsigned ssd_type:5 ;		/* segment type */
1674Srgrimes	unsigned ssd_dpl:2 ;		/* segment descriptor priority level */
1684Srgrimes	unsigned ssd_p:1 ;		/* segment descriptor present */
1694Srgrimes	unsigned ssd_xx:4 ;		/* unused */
1704Srgrimes	unsigned ssd_xx1:2 ;		/* unused */
1714Srgrimes	unsigned ssd_def32:1 ;		/* default 32 vs 16 bit size */
1724Srgrimes	unsigned ssd_gran:1 ;		/* limit granularity (byte/page units)*/
1734Srgrimes};
1744Srgrimes
1754Srgrimes/*
1764Srgrimes * region descriptors, used to load gdt/idt tables before segments yet exist.
1774Srgrimes */
1784Srgrimesstruct region_descriptor {
1791051Sdg	unsigned rd_limit:16;				/* segment extent */
1801051Sdg	unsigned rd_base:32 __attribute__ ((packed));	/* base address  */
1814Srgrimes};
1824Srgrimes
1834Srgrimes/*
1844Srgrimes * Segment Protection Exception code bits
1854Srgrimes */
1864Srgrimes
1874Srgrimes#define	SEGEX_EXT	0x01	/* recursive or externally induced */
1884Srgrimes#define	SEGEX_IDT	0x02	/* interrupt descriptor table */
1894Srgrimes#define	SEGEX_TI	0x04	/* local descriptor table */
1904Srgrimes				/* other bits are affected descriptor index */
1914Srgrimes#define SEGEX_IDX(s)	((s)>>3)&0x1fff)
1924Srgrimes
1934Srgrimes/*
1944Srgrimes * Size of IDT table
1954Srgrimes */
1964Srgrimes
1976380Ssos#define	NIDT	129		/* 32 reserved, 16 h/w, 0 s/w, linux's 0x80 */
1984Srgrimes#define	NRSVIDT	32		/* reserved entries for cpu exceptions */
1991051Sdg
2001051Sdg/*
2011051Sdg * Entries in the Global Descriptor Table (GDT)
2021051Sdg */
2031051Sdg#define	GNULL_SEL	0	/* Null Descriptor */
2041051Sdg#define	GCODE_SEL	1	/* Kernel Code Descriptor */
2051051Sdg#define	GDATA_SEL	2	/* Kernel Data Descriptor */
2061051Sdg#define	GLDT_SEL	3	/* LDT - eventually one per process */
2071051Sdg#define	GTGATE_SEL	4	/* Process task switch gate */
2081051Sdg#define	GPANIC_SEL	5	/* Task state to consider panic from */
2091051Sdg#define	GPROC0_SEL	6	/* Task state process slot zero and up */
2101051Sdg#define	GUSERLDT_SEL	7	/* User LDT */
2113258Sdg#define GAPMCODE32_SEL	8	/* APM BIOS 32-bit interface (32bit Code) */
2123258Sdg#define GAPMCODE16_SEL	9	/* APM BIOS 32-bit interface (16bit Code) */
2133258Sdg#define GAPMDATA_SEL	10	/* APM BIOS 32-bit interface (Data) */
2141051Sdg
2154473Sbde#ifdef BDE_DEBUGGER
2164473Sbde#define	NGDT		18	/* some of 11-17 are reserved for debugger */
2174473Sbde#else
2184473Sbde#define NGDT 		(GAPMDATA_SEL + 1)
2194473Sbde#endif
2203258Sdg
2211051Sdg/*
2221051Sdg * Entries in the Local Descriptor Table (LDT)
2231051Sdg */
2241051Sdg#define	LSYS5CALLS_SEL	0	/* forced by intel BCS */
2251051Sdg#define	LSYS5SIGR_SEL	1
2261051Sdg#define	L43BSDCALLS_SEL	2	/* notyet */
2271051Sdg#define	LUCODE_SEL	3
2281051Sdg#define	LUDATA_SEL	4
22913765Smpp/* separate stack, es,fs,gs sels ? */
2301051Sdg/* #define	LPOSIXCALLS_SEL	5*/	/* notyet */
23118513Speter#define LBSDICALLS_SEL	16	/* BSDI system call gate */
23218513Speter#define NLDT		(LBSDICALLS_SEL + 1)
2331051Sdg
2341051Sdg#ifdef KERNEL
2354501Sbdeextern int	currentldt;
2364501Sbdeextern int	_default_ldt;
23725164Speterextern union descriptor gdt[];
2384501Sbdeextern struct soft_segment_descriptor gdt_segs[];
2394473Sbdeextern struct gate_descriptor idt[NIDT];
2404501Sbdeextern union descriptor ldt[NLDT];
2411051Sdg
2424473Sbdevoid	lgdt		__P((struct region_descriptor *rdp));
2434473Sbdevoid	lidt		__P((struct region_descriptor *rdp));
2444473Sbdevoid	lldt		__P((u_short sel));
2454473Sbdevoid	sdtossd		__P((struct segment_descriptor *sdp,
2464473Sbde			     struct soft_segment_descriptor *ssdp));
2474473Sbdevoid	ssdtosd		__P((struct soft_segment_descriptor *ssdp,
2484473Sbde			     struct segment_descriptor *sdp));
2494473Sbde#endif /* KERNEL */
2504473Sbde
2514473Sbde#endif /* !_MACHINE_SEGMENTS_H_ */
252