segments.h revision 66715
180709Sjake/*-
280709Sjake * Copyright (c) 1989, 1990 William F. Jolitz
380709Sjake * Copyright (c) 1990 The Regents of the University of California.
480709Sjake * All rights reserved.
580709Sjake *
680709Sjake * This code is derived from software contributed to Berkeley by
780709Sjake * William Jolitz.
880709Sjake *
980709Sjake * Redistribution and use in source and binary forms, with or without
1080709Sjake * modification, are permitted provided that the following conditions
1180709Sjake * are met:
1280709Sjake * 1. Redistributions of source code must retain the above copyright
1380709Sjake *    notice, this list of conditions and the following disclaimer.
1481337Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1580709Sjake *    notice, this list of conditions and the following disclaimer in the
1680709Sjake *    documentation and/or other materials provided with the distribution.
1781337Sobrien * 3. All advertising materials mentioning features or use of this software
1880709Sjake *    must display the following acknowledgement:
1980709Sjake *	This product includes software developed by the University of
2080709Sjake *	California, Berkeley and its contributors.
2180709Sjake * 4. Neither the name of the University nor the names of its contributors
2280709Sjake *    may be used to endorse or promote products derived from this software
2380709Sjake *    without specific prior written permission.
2480709Sjake *
2580709Sjake * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2680709Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2780709Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2880709Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29145153Smarius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30145153Smarius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31145153Smarius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32118239Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33118239Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3480709Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3580709Sjake * SUCH DAMAGE.
3682910Sjake *
3780709Sjake *	from: @(#)segments.h	7.1 (Berkeley) 5/9/91
3891783Sjake * $FreeBSD: head/sys/i386/include/segments.h 66715 2000-10-06 01:59:07Z jhb $
3984186Sjake */
4080709Sjake
4180709Sjake#ifndef _MACHINE_SEGMENTS_H_
4281381Sjake#define	_MACHINE_SEGMENTS_H_
4381381Sjake
4480709Sjake#include <machine/globals.h>
45166105Smarius
4697001Sjake/*
47166105Smarius * 386 Segmentation Data Structures and definitions
48166105Smarius *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
49166105Smarius */
50166105Smarius
5180709Sjake/*
5280709Sjake * Selectors
5389052Sjake */
54166105Smarius
5580709Sjake#define	ISPL(s)	((s)&3)		/* what is the priority level of a selector */
56163146Skmacy#define	SEL_KPL	0		/* kernel priority level */
57163965Skmacy#define	SEL_UPL	3		/* user priority level */
58163146Skmacy#define	ISLDT(s)	((s)&SEL_LDT)	/* is it local or global */
59163146Skmacy#define	SEL_LDT	4		/* local descriptor table */
60166105Smarius#define	IDXSEL(s)	(((s)>>3) & 0x1fff)		/* index of selector */
6180709Sjake#define	LSEL(s,r)	(((s)<<3) | SEL_LDT | r)	/* a local selector */
6283756Sjake#define	GSEL(s,r)	(((s)<<3) | r)			/* a global selector */
6383756Sjake
6483366Sjulian/*
6591360Sjake * Memory and System segment descriptors
6680709Sjake */
6791224Sjakestruct	segment_descriptor	{
6891224Sjake	unsigned sd_lolimit:16 ;	/* segment extent (lsb) */
69182768Smarius	unsigned sd_lobase:24 __attribute__ ((packed));
70182768Smarius					/* segment base address (lsb) */
71182768Smarius	unsigned sd_type:5 ;		/* segment type */
72182768Smarius	unsigned sd_dpl:2 ;		/* segment descriptor priority level */
7381381Sjake	unsigned sd_p:1 ;		/* segment descriptor present */
74182768Smarius	unsigned sd_hilimit:4 ;		/* segment extent (msb) */
7591783Sjake	unsigned sd_xx:2 ;		/* unused */
7681381Sjake	unsigned sd_def32:1 ;		/* default 32 vs 16 bit size */
7791336Sjake	unsigned sd_gran:1 ;		/* limit granularity (byte/page units)*/
7891336Sjake	unsigned sd_hibase:8 ;		/* segment base address  (msb) */
7991336Sjake} ;
8080709Sjake
81102040Sjake/*
82102040Sjake * Gate descriptors (e.g. indirect descriptors)
83102040Sjake */
84163146Skmacystruct	gate_descriptor	{
8580709Sjake	unsigned gd_looffset:16 ;	/* gate offset (lsb) */
86183142Smarius	unsigned gd_selector:16 ;	/* gate segment selector */
8791783Sjake	unsigned gd_stkcpy:5 ;		/* number of stack wds to cpy */
8889052Sjake	unsigned gd_xx:3 ;		/* unused */
89166105Smarius	unsigned gd_type:5 ;		/* segment type */
90182730Smarius	unsigned gd_dpl:2 ;		/* segment descriptor priority level */
91182730Smarius	unsigned gd_p:1 ;		/* segment descriptor present */
9291617Sjake	unsigned gd_hioffset:16 ;	/* gate offset (msb) */
93182730Smarius} ;
9491617Sjake
95166105Smarius/*
96166105Smarius * Generic descriptor
97163152Skmacy */
98163146Skmacyunion	descriptor	{
99183142Smarius	struct	segment_descriptor sd;
10097001Sjake	struct	gate_descriptor gd;
101166105Smarius};
10297001Sjake
10397001Sjake	/* system segments and gate types */
10497001Sjake#define	SDT_SYSNULL	 0	/* system null */
10597001Sjake#define	SDT_SYS286TSS	 1	/* system 286 TSS available */
106166105Smarius#define	SDT_SYSLDT	 2	/* system local descriptor table */
10797001Sjake#define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
108183142Smarius#define	SDT_SYS286CGT	 4	/* system 286 call gate */
10997001Sjake#define	SDT_SYSTASKGT	 5	/* system task gate */
110183142Smarius#define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
11197001Sjake#define	SDT_SYS286TGT	 7	/* system 286 trap gate */
11282910Sjake#define	SDT_SYSNULL2	 8	/* system null again */
11393949Sjake#define	SDT_SYS386TSS	 9	/* system 386 TSS available */
11493949Sjake#define	SDT_SYSNULL3	10	/* system null again */
11582910Sjake#define	SDT_SYS386BSY	11	/* system 386 TSS busy */
11692199Sjake#define	SDT_SYS386CGT	12	/* system 386 call gate */
11793503Sjake#define	SDT_SYSNULL4	13	/* system null again */
11893503Sjake#define	SDT_SYS386IGT	14	/* system 386 interrupt gate */
11993503Sjake#define	SDT_SYS386TGT	15	/* system 386 trap gate */
12093503Sjake
12193503Sjake	/* memory segment types */
12293503Sjake#define	SDT_MEMRO	16	/* memory read only */
12393503Sjake#define	SDT_MEMROA	17	/* memory read only accessed */
12482910Sjake#define	SDT_MEMRW	18	/* memory read write */
125163146Skmacy#define	SDT_MEMRWA	19	/* memory read write accessed */
126166105Smarius#define	SDT_MEMROD	20	/* memory read only expand dwn limit */
12791224Sjake#define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
12880709Sjake#define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
12980709Sjake#define	SDT_MEMRWDA	23	/* memory read write expand dwn limit accessed */
130182916Smarius#define	SDT_MEME	24	/* memory execute only */
13181614Sjake#define	SDT_MEMEA	25	/* memory execute only accessed */
13280709Sjake#define	SDT_MEMER	26	/* memory execute read */
13388657Sjake#define	SDT_MEMERA	27	/* memory execute read accessed */
134182916Smarius#define	SDT_MEMEC	28	/* memory execute only conforming */
135100771Sjake#define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
136100771Sjake#define	SDT_MEMERC	30	/* memory execute read conforming */
13780709Sjake#define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
13880709Sjake
139102040Sjake/* is memory segment descriptor pointer ? */
140102040Sjake#define ISMEMSDP(s)	((s->d_type) >= SDT_MEMRO && (s->d_type) <= SDT_MEMERAC)
141182916Smarius
142205258Smarius/* is 286 gate descriptor pointer ? */
143163146Skmacy#define IS286GDP(s)	(((s->d_type) >= SDT_SYS286CGT \
144102040Sjake				 && (s->d_type) < SDT_SYS286TGT))
145166105Smarius
146166105Smarius/* is 386 gate descriptor pointer ? */
147166105Smarius#define IS386GDP(s)	(((s->d_type) >= SDT_SYS386CGT \
148163146Skmacy				&& (s->d_type) < SDT_SYS386TGT))
149163146Skmacy
150163146Skmacy/* is gate descriptor pointer ? */
151163146Skmacy#define ISGDP(s)	(IS286GDP(s) || IS386GDP(s))
152102040Sjake
153163146Skmacy/* is segment descriptor pointer ? */
154163146Skmacy#define ISSDP(s)	(ISMEMSDP(s) || !ISGDP(s))
155163146Skmacy
156163146Skmacy/* is system segment descriptor pointer ? */
157163146Skmacy#define ISSYSSDP(s)	(!ISMEMSDP(s) && !ISGDP(s))
158163146Skmacy
159163146Skmacy/*
160163146Skmacy * Software definitions are in this convenient format,
16184186Sjake * which are translated into inconvenient segment descriptors
16284186Sjake * when needed to be used by the 386 hardware
163210176Smav */
16487702Sjhb
16587702Sjhbstruct	soft_segment_descriptor	{
16687702Sjhb	unsigned ssd_base ;		/* segment base address  */
16791337Sjake	unsigned ssd_limit ;		/* segment extent */
16897265Sjake	unsigned ssd_type:5 ;		/* segment type */
16997265Sjake	unsigned ssd_dpl:2 ;		/* segment descriptor priority level */
17097265Sjake	unsigned ssd_p:1 ;		/* segment descriptor present */
171163146Skmacy	unsigned ssd_xx:4 ;		/* unused */
172163146Skmacy	unsigned ssd_xx1:2 ;		/* unused */
173163146Skmacy	unsigned ssd_def32:1 ;		/* default 32 vs 16 bit size */
174163146Skmacy	unsigned ssd_gran:1 ;		/* limit granularity (byte/page units)*/
175163146Skmacy};
176163146Skmacy
177163146Skmacy/*
178163146Skmacy * region descriptors, used to load gdt/idt tables before segments yet exist.
179163146Skmacy */
180163146Skmacystruct region_descriptor {
181163146Skmacy	unsigned rd_limit:16;				/* segment extent */
182163146Skmacy	unsigned rd_base:32 __attribute__ ((packed));	/* base address  */
183163146Skmacy};
184163146Skmacy
185163146Skmacy/*
186163146Skmacy * Segment Protection Exception code bits
187163146Skmacy */
188163146Skmacy
189163146Skmacy#define	SEGEX_EXT	0x01	/* recursive or externally induced */
190163146Skmacy#define	SEGEX_IDT	0x02	/* interrupt descriptor table */
191163146Skmacy#define	SEGEX_TI	0x04	/* local descriptor table */
192163146Skmacy				/* other bits are affected descriptor index */
193163146Skmacy#define SEGEX_IDX(s)	((s)>>3)&0x1fff)
194163146Skmacy
195164737Skmacy/*
196164737Skmacy * Size of IDT table
197164737Skmacy */
198164737Skmacy
199164737Skmacy#if defined(SMP) || defined(APIC_IO)
200164485Skmacy#define	NIDT	256		/* we use them all */
201164485Skmacy#else
202166105Smarius#define	NIDT	129		/* 32 reserved, 16 h/w, 0 s/w, linux's 0x80 */
203166105Smarius#endif /* SMP || APIC_IO */
204182689Smarius#define	NRSVIDT	32		/* reserved entries for cpu exceptions */
20589052Sjake
206182689Smarius/*
20791613Sjake * Entries in the Global Descriptor Table (GDT)
20891613Sjake */
20991613Sjake#define	GNULL_SEL	0	/* Null Descriptor */
210166105Smarius#define	GCODE_SEL	1	/* Kernel Code Descriptor */
21180709Sjake#define	GDATA_SEL	2	/* Kernel Data Descriptor */
21297265Sjake#define	GPRIV_SEL	3	/* SMP Per-Processor Private Data */
21397265Sjake#define	GPROC0_SEL	4	/* Task state process slot zero and up */
21497265Sjake#define	GLDT_SEL	5	/* LDT - eventually one per process */
21597265Sjake#define	GUSERLDT_SEL	6	/* User LDT */
21697265Sjake#define	GTGATE_SEL	7	/* Process task switch gate */
21781614Sjake#define	GBIOSLOWMEM_SEL	8	/* BIOS low memory access (must be entry 8) */
218183142Smarius#define	GPANIC_SEL	9	/* Task state to consider panic from */
219211071Smarius#define GBIOSCODE32_SEL	10	/* BIOS interface (32bit Code) */
220211071Smarius#define GBIOSCODE16_SEL	11	/* BIOS interface (16bit Code) */
221211071Smarius#define GBIOSDATA_SEL	12	/* BIOS interface (Data) */
22292199Sjake#define GBIOSUTIL_SEL	13	/* BIOS interface (Utility) */
22391783Sjake#define GBIOSARGS_SEL	14	/* BIOS interface (Arguments) */
22489052Sjake
22589052Sjake#ifdef BDE_DEBUGGER
22689052Sjake#define	NGDT		18	/* some of 11-17 are reserved for debugger */
227183142Smarius#else
22889052Sjake#define NGDT 		15
22981614Sjake#endif
23081614Sjake
23181614Sjake/*
23281614Sjake * Entries in the Local Descriptor Table (LDT)
233111032Sjulian */
234111032Sjulian#define	LSYS5CALLS_SEL	0	/* forced by intel BCS */
23582910Sjake#define	LSYS5SIGR_SEL	1
23688788Sjake#define	L43BSDCALLS_SEL	2	/* notyet */
23788788Sjake#define	LUCODE_SEL	3
23882910Sjake#define LSOL26CALLS_SEL	4	/* Solaris >= 2.6 system call gate */
23988657Sjake#define	LUDATA_SEL	5
24088657Sjake/* separate stack, es,fs,gs sels ? */
24180709Sjake/* #define	LPOSIXCALLS_SEL	5*/	/* notyet */
24280709Sjake#define LBSDICALLS_SEL	16	/* BSDI system call gate */
243111032Sjulian#define NLDT		(LBSDICALLS_SEL + 1)
24483366Sjulian
24583366Sjulian#ifdef _KERNEL
246203185Smariusextern int	_default_ldt;
24783366Sjulianextern union descriptor gdt[];
24883366Sjulianextern struct soft_segment_descriptor gdt_segs[];
249163146Skmacyextern struct gate_descriptor *idt;
250163146Skmacyextern union descriptor ldt[NLDT];
25183366Sjulian
25284186Sjakevoid	lgdt		__P((struct region_descriptor *rdp));
253113023Sjakevoid	lidt		__P((struct region_descriptor *rdp));
254113023Sjakevoid	lldt		__P((u_short sel));
255112920Sjakevoid	sdtossd		__P((struct segment_descriptor *sdp,
256113023Sjake			     struct soft_segment_descriptor *ssdp));
257112924Sjakevoid	ssdtosd		__P((struct soft_segment_descriptor *ssdp,
25882910Sjake			     struct segment_descriptor *sdp));
259113023Sjake#endif /* _KERNEL */
260113023Sjake
261163965Skmacy#endif /* !_MACHINE_SEGMENTS_H_ */
262163146Skmacy