Pip
Architecture-dependent parts of Pip: MAL, IAL and boot
gdt.h
Go to the documentation of this file.
1 /*******************************************************************************/
2 /* © Université de Lille, The Pip Development Team (2015-2021) */
3 /* */
4 /* This software is a computer program whose purpose is to run a minimal, */
5 /* hypervisor relying on proven properties such as memory isolation. */
6 /* */
7 /* This software is governed by the CeCILL license under French law and */
8 /* abiding by the rules of distribution of free software. You can use, */
9 /* modify and/ or redistribute the software under the terms of the CeCILL */
10 /* license as circulated by CEA, CNRS and INRIA at the following URL */
11 /* "http://www.cecill.info". */
12 /* */
13 /* As a counterpart to the access to the source code and rights to copy, */
14 /* modify and redistribute granted by the license, users are provided only */
15 /* with a limited warranty and the software's author, the holder of the */
16 /* economic rights, and the successive licensors have only limited */
17 /* liability. */
18 /* */
19 /* In this respect, the user's attention is drawn to the risks associated */
20 /* with loading, using, modifying and/or developing or reproducing the */
21 /* software by the user in light of its specific status of free software, */
22 /* that may mean that it is complicated to manipulate, and that also */
23 /* therefore means that it is reserved for developers and experienced */
24 /* professionals having in-depth computer knowledge. Users are therefore */
25 /* encouraged to load and test the software's suitability as regards their */
26 /* requirements in conditions enabling the security of their systems and/or */
27 /* data to be ensured and, more generally, to use and operate it in the */
28 /* same conditions as regards security. */
29 /* */
30 /* The fact that you are presently reading this means that you have had */
31 /* knowledge of the CeCILL license and that you accept its terms. */
32 /*******************************************************************************/
33 
39 #ifndef __GDT__
40 #define __GDT__
41 
42 #include <stdint.h>
43 #include "maldefines.h"
44 
60 {
61  unsigned limit_low : 16;
62  unsigned base_low : 16;
63  unsigned base_middle : 8;
64  unsigned type : 4;
65  unsigned s : 1;
66  unsigned dpl : 2;
67  unsigned present : 1;
68  unsigned limit_high : 4;
69  unsigned avl : 1;
70  unsigned l : 1;
71  unsigned d_b : 1;
72  unsigned granularity : 1;
73  unsigned base_high : 8;
74 };
75 
76 #define GRANULARITY_1 0
77 #define GRANULARITY_4096 1
78 
87 #define SEG_DATA_READONLY_EXPANDUP_TYPE 0b0000
88 #define SEG_DATA_READWRITE_EXPANDUP_TYPE 0b0010
89 #define SEG_DATA_READONLY_EXPANDDOWN_TYPE 0b0100
90 #define SEG_DATA_READWRITE_EXPANDDOWN_TYPE 0b0110
91 
92 #define SEG_CODE_EXECONLY_NONCONFORMING_TYPE 0b1000
93 #define SEG_CODE_EXECREAD_NONCONFORMING_TYPE 0b1010
94 #define SEG_CODE_EXECONLY_CONFORMING_TYPE 0b1100
95 #define SEG_CODE_EXECREAD_CONFORMING_TYPE 0b1110
96 
98 
104  unsigned offset_low : 16;
105  unsigned segment : 16;
106  unsigned args : 5;
107  unsigned reserved : 3;
108  unsigned type : 4;
109  unsigned zero : 1;
110  unsigned dpl : 2;
111  unsigned present : 1;
112  unsigned offset_high : 16;
113 };
115 #define GDT_CALLGATE_TYPE 0b1100
123  unsigned limit_low : 16; //<! Segment limit 15..0
124  unsigned base_low : 16; //<! Segment base address 15..0
125  unsigned base_middle : 8; //<! Segment base address 23..16
126  unsigned type : 4; //<! GDT_TSS_BUSY_TYPE or GDT_TSS_INACTIVE_TYPE
127  unsigned zero : 1; //<! Always zero smh
128  unsigned dpl : 2; //<! Descriptor Privilege Level
129  unsigned present : 1; //<! Present flag (validity of descriptor)
130  unsigned limit_high : 4; //<! Segment limit 19..16
131  unsigned avl : 1; //<! Available for use by system software
132  unsigned zero2 : 2; //<! Also always zero smh
133  unsigned granularity : 1; //<! 0 => segment limit range from 1B to 1MB, byte per byte
134  //<! 1 => segment limit range from 4kB to 4GB, 4kB per 4kB
135  /* When the G flag is 0 in a TSS descriptor for a 32-bit
136  * TSS, the limit field must have a value equal to or
137  * greater than 67H, one byte less than the minimum size
138  * of a TSS. Otherwise, TS fault is generated.
139  */
140  unsigned base_high : 8; //<! Segment base address 31..24
141 };
142 
143 #define GDT_TSS_INACTIVE_TYPE 0b1001 //inactive task
144 #define GDT_TSS_BUSY_TYPE 0b1011 //running or suspended task (interrupted state)
145 
147 
148 union gdt_entry {
152  uint64_t null_desc;
153 };
154 
155 typedef union gdt_entry gdt_entry_t;
156 
161 struct gdt_ptr
162 {
163  unsigned short limit;
164  unsigned int base;
165 } __attribute__((packed));
166 
172 struct tss_s {
173  unsigned prev_tss : 16;
174  unsigned reserved0 : 16;
175  unsigned esp0 : 32;
176  unsigned ss0 : 16;
177  unsigned reserved1 : 16;
178  unsigned esp1 : 32;
179  unsigned ss1 : 16;
180  unsigned reserved2 : 16;
181  unsigned esp2 : 32;
182  unsigned ss2 : 16;
183  unsigned reserved3 : 16;
184  unsigned cr3 : 32;
185  unsigned eip : 32;
186  unsigned eflags : 32;
187  unsigned eax : 32;
188  unsigned ecx : 32;
189  unsigned edx : 32;
190  unsigned ebx : 32;
191  unsigned esp : 32;
192  unsigned ebp : 32;
193  unsigned esi : 32;
194  unsigned edi : 32;
195  unsigned es : 16;
196  unsigned reserved4 : 16;
197  unsigned cs : 16;
198  unsigned reserved5 : 16;
199  unsigned ss : 16;
200  unsigned reserved6 : 16;
201  unsigned ds : 16;
202  unsigned reserved7 : 16;
203  unsigned fs : 16;
204  unsigned reserved8 : 16;
205  unsigned gs : 16;
206  unsigned reserved9 : 16;
207  unsigned ldt : 16;
208  unsigned reserved10 : 16;
209  unsigned trap : 1;
210  unsigned reserved11 : 15;
211  unsigned iomap_base : 16;
212 } __attribute__((packed));
213 
214 typedef struct tss_s tss_t;
215 extern tss_t tss;
216 
217 void gdt_init();
218 
219 void setKernelStack(uint32_t stack);
220 
221 #endif
unsigned ds
Segment selector DS (prior to task switch)
Definition: gdt.h:201
unsigned es
Segment selector ES (prior to task switch)
Definition: gdt.h:195
Pointer to the GDT.
Definition: gdt.h:161
unsigned esp2
Ring-2 ESP (static)
Definition: gdt.h:181
unsigned eip
Execution pointer (prior to task switch)
Definition: gdt.h:185
unsigned int base
Base address.
Definition: gdt.h:164
unsigned ss
Segment selector SS (prior to task switch)
Definition: gdt.h:199
unsigned reserved4
Definition: gdt.h:196
unsigned reserved0
Definition: gdt.h:174
unsigned base_high
Definition: gdt.h:140
unsigned ebx
General register EBX (prior to task switch)
Definition: gdt.h:190
uint32_t zero(void)
Returns zero.
Definition: armv7/MAL/malinternal.c:79
unsigned fs
Segment selector FS (prior to task switch)
Definition: gdt.h:203
unsigned cr3
Page directory address (static)
Definition: gdt.h:184
unsigned trap
Flag to raise an exception when a task switch to this task occurs (static)
Definition: gdt.h:209
unsigned edi
General register EDI (prior to task switch)
Definition: gdt.h:194
unsigned type
See below defines.
Definition: gdt.h:64
callgate_descriptor_t callgate_desc
Definition: gdt.h:150
unsigned s
0 => system segment, 1 => code or data segment (see 3/)
Definition: gdt.h:65
Meant to be written inside the GDT. Provides the processor with the size and location of a segment...
Definition: gdt.h:59
unsigned edx
General register EDX (prior to task switch)
Definition: gdt.h:189
unsigned reserved9
Definition: gdt.h:206
unsigned reserved2
Definition: gdt.h:180
unsigned avl
Available for use by system software.
Definition: gdt.h:69
unsigned reserved10
Definition: gdt.h:208
unsigned reserved6
Definition: gdt.h:200
unsigned ss2
Ring-2 stack segment (static)
Definition: gdt.h:182
unsigned cs
Segment selector CS (prior to task switch)
Definition: gdt.h:197
unsigned eax
General register EAX (prior to task switch)
Definition: gdt.h:187
A callgate descriptor for the GDT/LDT Intel 64 and IA-32 Architectures Software Developer&#39;s Manual -...
Definition: gdt.h:103
unsigned prev_tss
Pointer to the previous TSS entry (updated on a task switch)
Definition: gdt.h:173
segment_descriptor_t segment_desc
Definition: gdt.h:149
unsigned base_middle
Middle bits of base address (bits 23..16)
Definition: gdt.h:63
unsigned reserved11
Definition: gdt.h:210
unsigned granularity
granularity (see 2/ in above comment)
Definition: gdt.h:72
unsigned reserved7
Definition: gdt.h:202
Definition: gdt.h:148
tss_descriptor_t tss_desc
Definition: gdt.h:151
unsigned ss0
Kernel-mode stack segment (static)
Definition: gdt.h:176
Definition: gdt.h:122
unsigned ss1
Ring-1 stack segment (static)
Definition: gdt.h:179
unsigned iomap_base
IOMMU base.
Definition: gdt.h:211
unsigned reserved1
Definition: gdt.h:177
unsigned base_high
Higher bits of base address (bits 31..24)
Definition: gdt.h:73
unsigned reserved3
Definition: gdt.h:183
unsigned ecx
General register ECX (prior to task switch)
Definition: gdt.h:188
unsigned short limit
Address limit.
Definition: gdt.h:163
unsigned esp1
Ring-1 ESP (static)
Definition: gdt.h:178
struct gdt_ptr __attribute__((packed))
void setKernelStack(uint32_t stack)
Updates the kernel stack address into the TSS.
Definition: gdt.c:201
unsigned esi
General register ESI (prior to task switch)
Definition: gdt.h:193
Task State Segment structure .
Definition: gdt.h:172
unsigned eflags
CPU flags (prior to task switch)
Definition: gdt.h:186
unsigned ldt
Pointer to the LDT (static)
Definition: gdt.h:207
unsigned dpl
Descriptor privilege level.
Definition: gdt.h:66
unsigned d_b
default operation size (0 => 16-bit, 1 => 32-bit)
Definition: gdt.h:71
unsigned limit_high
Higher bits of the size of the segment (bits 19..16)
Definition: gdt.h:68
unsigned ebp
User-mode EBP (prior to task switch)
Definition: gdt.h:192
void gdt_init()
Installs the GDT into the CPU.
Definition: gdt.c:262
unsigned gs
Segment selector GS (prior to task switch)
Definition: gdt.h:205
unsigned reserved8
Definition: gdt.h:204
unsigned esp
User-mode ESP (prior to task switch)
Definition: gdt.h:191
unsigned present
Preset flag (validity of descriptor)
Definition: gdt.h:67
unsigned reserved5
Definition: gdt.h:198
uint64_t null_desc
Definition: gdt.h:152
unsigned esp0
Kernel-mode ESP (static)
Definition: gdt.h:175
tss_t tss
Generic TSS for userland-to-kernel switch.
Definition: gdt.c:47
unsigned limit_low
Lower bits of the size of the segment (bits 15..0)
Definition: gdt.h:61
unsigned l
Long? flag (only useful in IA-32e) 64 bits code segment.
Definition: gdt.h:70
unsigned base_low
Lower bits of base address (bits 15..0)
Definition: gdt.h:62