xref: /f-stack/freebsd/amd64/include/segments.h (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 1989, 1990 William F. Jolitz
5a9643ea8Slogwang  * Copyright (c) 1990 The Regents of the University of California.
6a9643ea8Slogwang  * All rights reserved.
7a9643ea8Slogwang  *
8a9643ea8Slogwang  * This code is derived from software contributed to Berkeley by
9a9643ea8Slogwang  * William Jolitz.
10a9643ea8Slogwang  *
11a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
12a9643ea8Slogwang  * modification, are permitted provided that the following conditions
13a9643ea8Slogwang  * are met:
14a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
15a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
16a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
17a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
18a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
19*22ce4affSfengbojiang  * 3. Neither the name of the University nor the names of its contributors
20a9643ea8Slogwang  *    may be used to endorse or promote products derived from this software
21a9643ea8Slogwang  *    without specific prior written permission.
22a9643ea8Slogwang  *
23a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33a9643ea8Slogwang  * SUCH DAMAGE.
34a9643ea8Slogwang  *
35a9643ea8Slogwang  *	from: @(#)segments.h	7.1 (Berkeley) 5/9/91
36a9643ea8Slogwang  * $FreeBSD$
37a9643ea8Slogwang  */
38a9643ea8Slogwang 
39a9643ea8Slogwang #ifndef _MACHINE_SEGMENTS_H_
40a9643ea8Slogwang #define	_MACHINE_SEGMENTS_H_
41a9643ea8Slogwang 
42a9643ea8Slogwang /*
43a9643ea8Slogwang  * AMD64 Segmentation Data Structures and definitions
44a9643ea8Slogwang  */
45a9643ea8Slogwang 
46a9643ea8Slogwang #include <x86/segments.h>
47a9643ea8Slogwang 
48a9643ea8Slogwang /*
49a9643ea8Slogwang  * System segment descriptors (128 bit wide)
50a9643ea8Slogwang  */
51a9643ea8Slogwang struct	system_segment_descriptor {
52a9643ea8Slogwang 	u_int64_t sd_lolimit:16;	/* segment extent (lsb) */
53a9643ea8Slogwang 	u_int64_t sd_lobase:24;		/* segment base address (lsb) */
54a9643ea8Slogwang 	u_int64_t sd_type:5;		/* segment type */
55a9643ea8Slogwang 	u_int64_t sd_dpl:2;		/* segment descriptor priority level */
56a9643ea8Slogwang 	u_int64_t sd_p:1;		/* segment descriptor present */
57a9643ea8Slogwang 	u_int64_t sd_hilimit:4;		/* segment extent (msb) */
58a9643ea8Slogwang 	u_int64_t sd_xx0:3;		/* unused */
59a9643ea8Slogwang 	u_int64_t sd_gran:1;		/* limit granularity (byte/page units)*/
60a9643ea8Slogwang 	u_int64_t sd_hibase:40 __packed;/* segment base address  (msb) */
61a9643ea8Slogwang 	u_int64_t sd_xx1:8;
62a9643ea8Slogwang 	u_int64_t sd_mbz:5;		/* MUST be zero */
63a9643ea8Slogwang 	u_int64_t sd_xx2:19;
64a9643ea8Slogwang } __packed;
65a9643ea8Slogwang 
66a9643ea8Slogwang /*
67a9643ea8Slogwang  * Software definitions are in this convenient format,
68a9643ea8Slogwang  * which are translated into inconvenient segment descriptors
69a9643ea8Slogwang  * when needed to be used by the 386 hardware
70a9643ea8Slogwang  */
71a9643ea8Slogwang 
72a9643ea8Slogwang struct	soft_segment_descriptor {
73a9643ea8Slogwang 	unsigned long ssd_base;		/* segment base address  */
74a9643ea8Slogwang 	unsigned long ssd_limit;	/* segment extent */
75a9643ea8Slogwang 	unsigned long ssd_type:5;	/* segment type */
76a9643ea8Slogwang 	unsigned long ssd_dpl:2;	/* segment descriptor priority level */
77a9643ea8Slogwang 	unsigned long ssd_p:1;		/* segment descriptor present */
78a9643ea8Slogwang 	unsigned long ssd_long:1;	/* long mode (for %cs) */
79a9643ea8Slogwang 	unsigned long ssd_def32:1;	/* default 32 vs 16 bit size */
80a9643ea8Slogwang 	unsigned long ssd_gran:1;	/* limit granularity (byte/page units)*/
81a9643ea8Slogwang } __packed;
82a9643ea8Slogwang 
83a9643ea8Slogwang /*
84a9643ea8Slogwang  * region descriptors, used to load gdt/idt tables before segments yet exist.
85a9643ea8Slogwang  */
86a9643ea8Slogwang struct region_descriptor {
87a9643ea8Slogwang 	uint64_t rd_limit:16;		/* segment extent */
88a9643ea8Slogwang 	uint64_t rd_base:64 __packed;	/* base address  */
89a9643ea8Slogwang } __packed;
90a9643ea8Slogwang 
91a9643ea8Slogwang #ifdef _KERNEL
92a9643ea8Slogwang extern struct soft_segment_descriptor gdt_segs[];
93a9643ea8Slogwang extern struct gate_descriptor *idt;
94*22ce4affSfengbojiang extern struct region_descriptor r_idt;
95a9643ea8Slogwang 
96a9643ea8Slogwang void	lgdt(struct region_descriptor *rdp);
97a9643ea8Slogwang void	sdtossd(struct user_segment_descriptor *sdp,
98a9643ea8Slogwang 	    struct soft_segment_descriptor *ssdp);
99a9643ea8Slogwang void	ssdtosd(struct soft_segment_descriptor *ssdp,
100a9643ea8Slogwang 	    struct user_segment_descriptor *sdp);
101a9643ea8Slogwang void	ssdtosyssd(struct soft_segment_descriptor *ssdp,
102a9643ea8Slogwang 	    struct system_segment_descriptor *sdp);
103a9643ea8Slogwang void	update_gdt_gsbase(struct thread *td, uint32_t base);
104a9643ea8Slogwang void	update_gdt_fsbase(struct thread *td, uint32_t base);
105a9643ea8Slogwang #endif /* _KERNEL */
106a9643ea8Slogwang 
107a9643ea8Slogwang #endif /* !_MACHINE_SEGMENTS_H_ */
108