The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
Loading...
Searching...
No Matches
gdt.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
common/stdint.h
"
4
5
#ifdef __x86_64__
6
7
/*
8
* the x86_64 GDT. the user data segment comes before the user code segment
9
* because SYSRET loads them as STAR[63:48] + 8 and + 16.
10
*/
11
12
/** kernel code segment selector (GDT index 1, RPL 0, 64-bit). */
13
#define GDT_KERNEL_CODE 0x08
14
15
/** kernel data segment selector (GDT index 2, RPL 0). */
16
#define GDT_KERNEL_DATA 0x10
17
18
/** user data segment selector (GDT index 3, RPL 3). */
19
#define GDT_USER_DATA 0x1B
20
21
/** user code segment selector (GDT index 4, RPL 3, 64-bit). */
22
#define GDT_USER_CODE 0x23
23
24
/** TSS segment selector (GDT indexes 5 and 6: a 16-byte descriptor). */
25
#define GDT_TSS 0x28
26
27
#else
28
29
/** kernel code segment selector (GDT index 1, RPL 0). */
30
#define GDT_KERNEL_CODE 0x08
31
32
/** kernel data segment selector (GDT index 2, RPL 0). */
33
#define GDT_KERNEL_DATA 0x10
34
35
/** user code segment selector (GDT index 3, RPL 3). */
36
#define GDT_USER_CODE 0x1B
37
38
/** user data segment selector (GDT index 4, RPL 3). */
39
#define GDT_USER_DATA 0x23
40
41
/** TSS segment selector (GDT index 5, RPL 0). */
42
#define GDT_TSS 0x28
43
44
/** double-fault task TSS selector (GDT index 6, RPL 0). */
45
#define GDT_DOUBLE_FAULT_TSS 0x30
46
47
// user thread-local storage, loaded in GS (entry 7, RPL 3)
48
#define GDT_USER_TLS 0x3B
49
50
// the per-CPU data of the core, loaded in FS in kernel mode (entry 8, RPL 0)
51
#define GDT_KERNEL_PERCPU 0x40
52
53
#endif
54
55
/**
56
* set up the GDT of a core with kernel segments, user segments and a Task
57
* State Segment, and load the GDT into GDTR and the TSS into TR. each core
58
* has its own GDT and TSS. call it on the core itself.
59
* @param core the index of the core (cpu_t.index), 0 for the boot core.
60
*/
61
void
gdt_initialize
(
uint32_t
core);
62
63
/**
64
* update the kernel stack pointer (ESP0) in the TSS of the core that runs
65
* the call. called on context switches so the CPU knows where to find the
66
* kernel stack when an interrupt occurs in ring 3.
67
* @param esp0 the kernel stack pointer to store.
68
*/
69
void
tss_set_kernel_stack
(
uintptr_t
esp0
);
70
71
#ifndef __x86_64__
72
/**
73
* set the base of the user TLS segment in the GDT of the core that runs
74
* the call, and load GS with it.
75
* @param base the thread pointer.
76
*/
77
void
gdt_set_tls
(
uint32_t
base
);
78
79
/**
80
* set the base of the per-CPU segment (the cpu_t of the core) and load FS
81
* with it. the interrupt entry loads FS with it again in kernel mode.
82
* @param core the index of the core that runs the call.
83
* @param base the address of the cpu_t.
84
*/
85
void
gdt_set_percpu
(
uint32_t
core,
uint32_t
base
);
86
#endif
87
88
#ifdef __x86_64__
89
90
/**
91
* set an interrupt stack table entry of the TSS of a core. an IDT gate with
92
* this IST index (1 to 7) switches to the stack, also in kernel mode.
93
* @param core the index of the core.
94
* @param index the IST index, 1 to 7.
95
* @param stack_top the top of the stack.
96
*/
97
void
tss_set_ist(
uint32_t
core,
int
index,
uintptr_t
stack_top);
98
99
#else
100
101
/**
102
* set up the double-fault task: a second TSS whose state the CPU loads when
103
* a double fault arrives through a task gate, so the handler runs on its own
104
* stack even when the faulting stack is unusable (a kernel stack overflow
105
* into its guard page). each core has its own task.
106
* @param core the index of the core.
107
* @param entry function the task starts in; it must never return.
108
* @param stack_top initial ESP for the task.
109
*/
110
void
tss_install_double_fault
(
uint32_t
core,
void
(*entry)(
void
),
uintptr_t
stack_top);
111
112
/**
113
* set the page directory the double-fault task of a core runs with.
114
* @param core the index of the core.
115
* @param cr3 physical address of the kernel page directory.
116
*/
117
void
tss_set_double_fault_cr3
(
uint32_t
core,
uint32_t
cr3
);
118
119
/**
120
* read the state of the task that was running when the double fault struck.
121
* the task switch saves it into the main TSS of the core that runs the
122
* call.
123
* @param eip receives the interrupted EIP.
124
* @param esp receives the interrupted ESP.
125
* @param ebp receives the interrupted EBP.
126
*/
127
void
tss_interrupted_state
(
uint32_t
*
eip
,
uint32_t
*
esp
,
uint32_t
*
ebp
);
128
129
#endif
// __x86_64__
stdint.h
gdt_set_tls
void gdt_set_tls(uint32_t base)
set the base of the user TLS segment in the GDT of the core that runs the call, and load GS with it.
Definition
gdt.c:142
base
uint32_t base
Definition
gdt.c:1
tss_interrupted_state
void tss_interrupted_state(uint32_t *eip, uint32_t *esp, uint32_t *ebp)
read the state of the task that was running when the double fault struck.
Definition
gdt.c:187
eip
uint32_t eip
Definition
gdt.c:8
tss_set_kernel_stack
void tss_set_kernel_stack(uintptr_t esp0)
update the kernel stack pointer (ESP0) in the TSS of the core that runs the call.
Definition
gdt.c:156
tss_set_double_fault_cr3
void tss_set_double_fault_cr3(uint32_t core, uint32_t cr3)
set the page directory the double-fault task of a core runs with.
Definition
gdt.c:182
gdt_initialize
void gdt_initialize(uint32_t core)
set up the GDT of a core with kernel segments, user segments and a Task State Segment,...
Definition
gdt.c:106
tss_install_double_fault
void tss_install_double_fault(uint32_t core, void(*entry)(void), uintptr_t stack_top)
set up the double-fault task: a second TSS whose state the CPU loads when a double fault arrives thro...
Definition
gdt.c:161
esp
uint32_t esp
Definition
gdt.c:14
cr3
uint32_t cr3
Definition
gdt.c:7
ebp
uint32_t ebp
Definition
gdt.c:15
gdt_set_percpu
void gdt_set_percpu(uint32_t core, uint32_t base)
set the base of the per-CPU segment (the cpu_t of the core) and load FS with it.
Definition
gdt.c:150
esp0
uint32_t esp0
Definition
gdt.c:1
uint32_t
unsigned int uint32_t
Definition
fb.h:17
uintptr_t
__UINTPTR_TYPE__ uintptr_t
Definition
stdint.h:19
src
arch
x86
cpu
gdt.h
Generated by
1.18.0