segments.h revision 227290
122347Spst/*-
222347Spst * Copyright (c) 1989, 1990 William F. Jolitz
329964Sache * Copyright (c) 1990 The Regents of the University of California.
429964Sache * All rights reserved.
522347Spst *
622347Spst * This code is derived from software contributed to Berkeley by
722347Spst * William Jolitz.
822347Spst *
922347Spst * Redistribution and use in source and binary forms, with or without
1022347Spst * modification, are permitted provided that the following conditions
1122347Spst * are met:
1222347Spst * 1. Redistributions of source code must retain the above copyright
1322347Spst *    notice, this list of conditions and the following disclaimer.
1422347Spst * 2. Redistributions in binary form must reproduce the above copyright
1522347Spst *    notice, this list of conditions and the following disclaimer in the
1622347Spst *    documentation and/or other materials provided with the distribution.
1722347Spst * 4. Neither the name of the University nor the names of its contributors
1822347Spst *    may be used to endorse or promote products derived from this software
1922347Spst *    without specific prior written permission.
2022347Spst *
2122347Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2222347Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2322347Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2422347Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2522347Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2622347Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2722347Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2822347Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2922347Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3022347Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3122347Spst * SUCH DAMAGE.
3222347Spst *
3322347Spst *	from: @(#)segments.h	7.1 (Berkeley) 5/9/91
3422347Spst * $FreeBSD: head/sys/i386/include/segments.h 227290 2011-11-07 01:53:25Z rstone $
3522347Spst */
3622347Spst
3722347Spst#ifndef _MACHINE_SEGMENTS_H_
3822347Spst#define	_MACHINE_SEGMENTS_H_
3922347Spst
4022347Spst/*
4122347Spst * 386 Segmentation Data Structures and definitions
4222347Spst *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
4322347Spst */
4422347Spst
4522347Spst/*
4622347Spst * Selectors
4722347Spst */
4822347Spst
4922347Spst#define	ISPL(s)	((s)&3)		/* what is the priority level of a selector */
5022347Spst#ifdef XEN
5122347Spst#define	SEL_KPL	1		/* kernel priority level */
5222347Spst#else
5322347Spst#define	SEL_KPL	0		/* kernel priority level */
5422347Spst#endif
5522347Spst#define	SEL_UPL	3		/* user priority level */
5622347Spst#define	ISLDT(s)	((s)&SEL_LDT)	/* is it local or global */
5722347Spst#define	SEL_LDT	4		/* local descriptor table */
5822347Spst#define	IDXSEL(s)	(((s)>>3) & 0x1fff)		/* index of selector */
5922347Spst#define	LSEL(s,r)	(((s)<<3) | SEL_LDT | r)	/* a local selector */
6022347Spst#define	GSEL(s,r)	(((s)<<3) | r)			/* a global selector */
6122347Spst
6222347Spst/*
6322347Spst * Memory and System segment descriptors
6422347Spst */
6522347Spststruct	segment_descriptor	{
6622347Spst	unsigned sd_lolimit:16 ;	/* segment extent (lsb) */
6722347Spst	unsigned sd_lobase:24 __packed;	/* segment base address (lsb) */
6822347Spst	unsigned sd_type:5 ;		/* segment type */
6922347Spst	unsigned sd_dpl:2 ;		/* segment descriptor priority level */
7022347Spst	unsigned sd_p:1 ;		/* segment descriptor present */
7122347Spst	unsigned sd_hilimit:4 ;		/* segment extent (msb) */
7222347Spst	unsigned sd_xx:2 ;		/* unused */
7322347Spst	unsigned sd_def32:1 ;		/* default 32 vs 16 bit size */
7422347Spst	unsigned sd_gran:1 ;		/* limit granularity (byte/page units)*/
7522347Spst	unsigned sd_hibase:8 ;		/* segment base address  (msb) */
7622347Spst} ;
7722347Spst
7822347Spst#define	USD_GETBASE(sd)		(((sd)->sd_lobase) | (sd)->sd_hibase << 24)
7922347Spst#define	USD_SETBASE(sd, b)	(sd)->sd_lobase = (b);  \
8022347Spst				(sd)->sd_hibase = ((b) >> 24);
8122347Spst#define	USD_GETLIMIT(sd)	(((sd)->sd_lolimit) | (sd)->sd_hilimit << 16)
8222347Spst#define	USD_SETLIMIT(sd, l)	(sd)->sd_lolimit = (l); \
8322347Spst				(sd)->sd_hilimit = ((l) >> 16);
8422347Spst
8522347Spst/*
8622347Spst * Gate descriptors (e.g. indirect descriptors)
8722347Spst */
8822347Spststruct	gate_descriptor	{
8922347Spst	unsigned gd_looffset:16 ;	/* gate offset (lsb) */
9022347Spst	unsigned gd_selector:16 ;	/* gate segment selector */
9122347Spst	unsigned gd_stkcpy:5 ;		/* number of stack wds to cpy */
9222347Spst	unsigned gd_xx:3 ;		/* unused */
9322347Spst	unsigned gd_type:5 ;		/* segment type */
9422347Spst	unsigned gd_dpl:2 ;		/* segment descriptor priority level */
9522347Spst	unsigned gd_p:1 ;		/* segment descriptor present */
9622347Spst	unsigned gd_hioffset:16 ;	/* gate offset (msb) */
9722347Spst} ;
9822347Spst
9922347Spst/*
10022347Spst * Generic descriptor
10122347Spst */
10222347Spstunion	descriptor	{
10322347Spst	struct	segment_descriptor sd;
10422347Spst	struct	gate_descriptor gd;
10522347Spst};
10622347Spst
10722347Spst	/* system segments and gate types */
10822347Spst#define	SDT_SYSNULL	 0	/* system null */
10922347Spst#define	SDT_SYS286TSS	 1	/* system 286 TSS available */
11022347Spst#define	SDT_SYSLDT	 2	/* system local descriptor table */
11122347Spst#define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
11222347Spst#define	SDT_SYS286CGT	 4	/* system 286 call gate */
11322347Spst#define	SDT_SYSTASKGT	 5	/* system task gate */
11422347Spst#define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
11522347Spst#define	SDT_SYS286TGT	 7	/* system 286 trap gate */
11622347Spst#define	SDT_SYSNULL2	 8	/* system null again */
11722347Spst#define	SDT_SYS386TSS	 9	/* system 386 TSS available */
11822347Spst#define	SDT_SYSNULL3	10	/* system null again */
11922347Spst#define	SDT_SYS386BSY	11	/* system 386 TSS busy */
12022347Spst#define	SDT_SYS386CGT	12	/* system 386 call gate */
12122347Spst#define	SDT_SYSNULL4	13	/* system null again */
12222347Spst#define	SDT_SYS386IGT	14	/* system 386 interrupt gate */
12322347Spst#define	SDT_SYS386TGT	15	/* system 386 trap gate */
12422347Spst
12522347Spst	/* memory segment types */
12622347Spst#define	SDT_MEMRO	16	/* memory read only */
12722347Spst#define	SDT_MEMROA	17	/* memory read only accessed */
12822347Spst#define	SDT_MEMRW	18	/* memory read write */
12922347Spst#define	SDT_MEMRWA	19	/* memory read write accessed */
13022347Spst#define	SDT_MEMROD	20	/* memory read only expand dwn limit */
13122347Spst#define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
13222347Spst#define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
13322347Spst#define	SDT_MEMRWDA	23	/* memory read write expand dwn limit accessed */
13422347Spst#define	SDT_MEME	24	/* memory execute only */
13522347Spst#define	SDT_MEMEA	25	/* memory execute only accessed */
13622347Spst#define	SDT_MEMER	26	/* memory execute read */
13722347Spst#define	SDT_MEMERA	27	/* memory execute read accessed */
13822347Spst#define	SDT_MEMEC	28	/* memory execute only conforming */
13922347Spst#define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
14022347Spst#define	SDT_MEMERC	30	/* memory execute read conforming */
14122347Spst#define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
14222347Spst
14322347Spst/*
14422347Spst * Software definitions are in this convenient format,
14522347Spst * which are translated into inconvenient segment descriptors
14622347Spst * when needed to be used by the 386 hardware
14722347Spst */
14822347Spst
14922347Spststruct	soft_segment_descriptor	{
15022347Spst	unsigned ssd_base ;		/* segment base address  */
15122347Spst	unsigned ssd_limit ;		/* segment extent */
15222347Spst	unsigned ssd_type:5 ;		/* segment type */
15322347Spst	unsigned ssd_dpl:2 ;		/* segment descriptor priority level */
15422347Spst	unsigned ssd_p:1 ;		/* segment descriptor present */
15522347Spst	unsigned ssd_xx:4 ;		/* unused */
15622347Spst	unsigned ssd_xx1:2 ;		/* unused */
15722347Spst	unsigned ssd_def32:1 ;		/* default 32 vs 16 bit size */
15822347Spst	unsigned ssd_gran:1 ;		/* limit granularity (byte/page units)*/
15922347Spst};
16022347Spst
16122347Spst/*
16222347Spst * region descriptors, used to load gdt/idt tables before segments yet exist.
16322347Spst */
16422347Spststruct region_descriptor {
16522347Spst	unsigned rd_limit:16;		/* segment extent */
16622347Spst	unsigned rd_base:32 __packed;	/* base address  */
16722347Spst};
16822347Spst
16922347Spst/*
17022347Spst * Segment Protection Exception code bits
17122347Spst */
17222347Spst
17322347Spst#define	SEGEX_EXT	0x01	/* recursive or externally induced */
17422347Spst#define	SEGEX_IDT	0x02	/* interrupt descriptor table */
17522347Spst#define	SEGEX_TI	0x04	/* local descriptor table */
17622347Spst				/* other bits are affected descriptor index */
17722347Spst#define SEGEX_IDX(s)	(((s)>>3)&0x1fff)
17822347Spst
17922347Spst/*
18022347Spst * Size of IDT table
18122347Spst */
18222347Spst
18322347Spst#define	NIDT	256		/* 32 reserved, 0x80 syscall, most are h/w */
18422347Spst#define	NRSVIDT	32		/* reserved entries for cpu exceptions */
18522347Spst
18622347Spst/*
18722347Spst * Entries in the Interrupt Descriptor Table (IDT)
18822347Spst */
18922347Spst#define	IDT_DE		0	/* #DE: Divide Error */
19022347Spst#define	IDT_DB		1	/* #DB: Debug */
19122347Spst#define	IDT_NMI		2	/* Nonmaskable External Interrupt */
19222347Spst#define	IDT_BP		3	/* #BP: Breakpoint */
19322347Spst#define	IDT_OF		4	/* #OF: Overflow */
19422347Spst#define	IDT_BR		5	/* #BR: Bound Range Exceeded */
19522347Spst#define	IDT_UD		6	/* #UD: Undefined/Invalid Opcode */
19622347Spst#define	IDT_NM		7	/* #NM: No Math Coprocessor */
19722347Spst#define	IDT_DF		8	/* #DF: Double Fault */
19822347Spst#define	IDT_FPUGP	9	/* Coprocessor Segment Overrun */
19922347Spst#define	IDT_TS		10	/* #TS: Invalid TSS */
20022347Spst#define	IDT_NP		11	/* #NP: Segment Not Present */
20122347Spst#define	IDT_SS		12	/* #SS: Stack Segment Fault */
20222347Spst#define	IDT_GP		13	/* #GP: General Protection Fault */
20322347Spst#define	IDT_PF		14	/* #PF: Page Fault */
20422347Spst#define	IDT_MF		16	/* #MF: FPU Floating-Point Error */
20522347Spst#define	IDT_AC		17	/* #AC: Alignment Check */
20622347Spst#define	IDT_MC		18	/* #MC: Machine Check */
20722347Spst#define	IDT_XF		19	/* #XF: SIMD Floating-Point Exception */
20822347Spst#define	IDT_IO_INTS	NRSVIDT	/* Base of IDT entries for I/O interrupts. */
20922347Spst#define	IDT_SYSCALL	0x80	/* System Call Interrupt Vector */
21022347Spst#define	IDT_DTRACE_RET	0x20	/* DTrace pid provider Interrupt Vector */
21122347Spst
21222347Spst/*
21322347Spst * Entries in the Global Descriptor Table (GDT)
21422347Spst * Note that each 4 entries share a single 32 byte L1 cache line.
21522347Spst * Some of the fast syscall instructions require a specific order here.
21622347Spst */
21722347Spst#define	GNULL_SEL	0	/* Null Descriptor */
21822347Spst#define	GPRIV_SEL	1	/* SMP Per-Processor Private Data */
21922347Spst#define	GUFS_SEL	2	/* User %fs Descriptor (order critical: 1) */
22022347Spst#define	GUGS_SEL	3	/* User %gs Descriptor (order critical: 2) */
22122347Spst#define	GCODE_SEL	4	/* Kernel Code Descriptor (order critical: 1) */
22222347Spst#define	GDATA_SEL	5	/* Kernel Data Descriptor (order critical: 2) */
22322347Spst#define	GUCODE_SEL	6	/* User Code Descriptor (order critical: 3) */
22422347Spst#define	GUDATA_SEL	7	/* User Data Descriptor (order critical: 4) */
22522347Spst#define	GBIOSLOWMEM_SEL	8	/* BIOS low memory access (must be entry 8) */
22622347Spst#define	GPROC0_SEL	9	/* Task state process slot zero and up */
22722347Spst#define	GLDT_SEL	10	/* Default User LDT */
22822347Spst#define	GUSERLDT_SEL	11	/* User LDT */
22922347Spst#define	GPANIC_SEL	12	/* Task state to consider panic from */
23022347Spst#define	GBIOSCODE32_SEL	13	/* BIOS interface (32bit Code) */
23122347Spst#define	GBIOSCODE16_SEL	14	/* BIOS interface (16bit Code) */
23222347Spst#define	GBIOSDATA_SEL	15	/* BIOS interface (Data) */
23322347Spst#define	GBIOSUTIL_SEL	16	/* BIOS interface (Utility) */
23422347Spst#define	GBIOSARGS_SEL	17	/* BIOS interface (Arguments) */
23522347Spst#define	GNDIS_SEL	18	/* For the NDIS layer */
23622347Spst
23722347Spst#ifdef XEN
23822347Spst#define	NGDT 		9
23922347Spst#else
24022347Spst#define	NGDT 		19
24122347Spst#endif
24222347Spst
24322347Spst/*
24422347Spst * Entries in the Local Descriptor Table (LDT)
24522347Spst */
24622347Spst#define	LSYS5CALLS_SEL	0	/* forced by intel BCS */
24722347Spst#define	LSYS5SIGR_SEL	1
24822347Spst#define	L43BSDCALLS_SEL	2	/* notyet */
24922347Spst#define	LUCODE_SEL	3
25022347Spst#define LSOL26CALLS_SEL	4	/* Solaris >= 2.6 system call gate */
25122347Spst#define	LUDATA_SEL	5
25222347Spst/* separate stack, es,fs,gs sels ? */
25322347Spst/* #define	LPOSIXCALLS_SEL	5*/	/* notyet */
25422347Spst#define LBSDICALLS_SEL	16	/* BSDI system call gate */
25522347Spst#define NLDT		(LBSDICALLS_SEL + 1)
25622347Spst
25722347Spst#ifdef _KERNEL
25822347Spstextern int	_default_ldt;
25922347Spst#ifdef XEN
26022347Spstextern struct proc_ldt default_proc_ldt;
26122347Spstextern union descriptor *gdt;
26222347Spstextern union descriptor *ldt;
26322347Spst#else
26422347Spstextern union descriptor gdt[];
26522347Spstextern union descriptor ldt[NLDT];
26622347Spst#endif
26722347Spstextern struct soft_segment_descriptor gdt_segs[];
26822347Spstextern struct gate_descriptor *idt;
26922347Spstextern struct region_descriptor r_gdt, r_idt;
27022347Spst
27122347Spstvoid	lgdt(struct region_descriptor *rdp);
27222347Spstvoid	sdtossd(struct segment_descriptor *sdp,
27322347Spst	    struct soft_segment_descriptor *ssdp);
27422347Spstvoid	ssdtosd(struct soft_segment_descriptor *ssdp,
27522347Spst	    struct segment_descriptor *sdp);
27622347Spst#endif /* _KERNEL */
27722347Spst
27822347Spst#endif /* !_MACHINE_SEGMENTS_H_ */
27922347Spst