Xyris  0.5
gdt.cpp
Go to the documentation of this file.
1 /**
2  * @file gdt.cpp
3  * @author Keeton Feavel ([email protected])
4  * @brief The Global Descriptor Table (GDT) is specific to the IA32 architecture.
5  * @version 0.3
6  * @date 2019-11-14
7  *
8  * @copyright Copyright the Xyris Contributors (c) 2019
9  *
10  */
11 #include <Arch/i686/gdt.hpp>
13 #include <Library/string.hpp>
14 
15 #define ARCH_GDT_MAX_ENTRIES 5
16 
17 namespace GDT {
18 
20 struct Registers::GDTR gdtr;
21 
22 void init()
23 {
24  uint8_t gdtIndex = 0;
25  const union Base nullSegmentBase = { .value = 0 };
26  const union Limit nullSegmentLimit = { .value = 0 };
27  gdt[gdtIndex++] = {
28  .limit_low = nullSegmentLimit.section.low,
29  .base_low = nullSegmentBase.section.low,
30  .accessed = 0,
31  .rw = 0,
32  .dc = 0,
33  .executable = 0,
34  .system = 0,
35  .privilege = 0,
36  .present = 0,
37  .limit_high = nullSegmentLimit.section.high,
38  .reserved = 0,
39  .longMode = 0,
40  .size = 0,
41  .granulatity = 0,
42  .base_high = nullSegmentBase.section.high,
43  };
44 
45  const union Base kernelCodeBase = { .value = 0 };
46  const union Limit kernelCodeLimit = { .value = 0x000FFFFF };
47  gdt[gdtIndex++] = {
48  .limit_low = kernelCodeLimit.section.low,
49  .base_low = kernelCodeBase.section.low,
50  .accessed = 0,
51  .rw = 1,
52  .dc = 0,
53  .executable = 1,
54  .system = 1,
55  .privilege = 0,
56  .present = 1,
57  .limit_high = kernelCodeLimit.section.high,
58  .reserved = 0,
59  .longMode = 0,
60  .size = 1,
61  .granulatity = 1,
62  .base_high = kernelCodeBase.section.high,
63  };
64 
65  const union Base kernelDataBase = { .value = 0 };
66  const union Limit kernelDataLimit = { .value = 0x000FFFFF };
67  gdt[gdtIndex++] = {
68  .limit_low = kernelDataLimit.section.low,
69  .base_low = kernelDataBase.section.low,
70  .accessed = 0,
71  .rw = 1,
72  .dc = 0,
73  .executable = 0,
74  .system = 1,
75  .privilege = 0,
76  .present = 1,
77  .limit_high = kernelDataLimit.section.high,
78  .reserved = 0,
79  .longMode = 0,
80  .size = 1,
81  .granulatity = 1,
82  .base_high = kernelDataBase.section.high,
83  };
84 
85  const union Base userCodeBase = { .value = 0 };
86  const union Limit userCodeLimit = { .value = 0x000FFFFF };
87  gdt[gdtIndex++] = {
88  .limit_low = userCodeLimit.section.low,
89  .base_low = userCodeBase.section.low,
90  .accessed = 0,
91  .rw = 1,
92  .dc = 0,
93  .executable = 1,
94  .system = 1,
95  .privilege = 3,
96  .present = 1,
97  .limit_high = userCodeLimit.section.high,
98  .reserved = 0,
99  .longMode = 0,
100  .size = 1,
101  .granulatity = 1,
102  .base_high = userCodeBase.section.high,
103  };
104 
105  const union Base userDataBase = { .value = 0 };
106  const union Limit userDataLimit = { .value = 0x000FFFFF };
107  gdt[gdtIndex++] = {
108  .limit_low = userDataLimit.section.low,
109  .base_low = userDataBase.section.low,
110  .accessed = 0,
111  .rw = 1,
112  .dc = 0,
113  .executable = 0,
114  .system = 1,
115  .privilege = 3,
116  .present = 1,
117  .limit_high = userDataLimit.section.high,
118  .reserved = 0,
119  .longMode = 0,
120  .size = 1,
121  .granulatity = 1,
122  .base_high = userDataBase.section.high,
123  };
124 
125  // Update GDT register and flush
126  gdtr.size = sizeof(gdt) - 1;
127  gdtr.base = (uint32_t)&gdt;
128 
129  gdt_flush((uint32_t)&gdtr);
130 }
131 
132 } // !namespace GDT
GDT::Entry::limit_low
uint16_t limit_low
Definition: gdt.hpp:43
GDT::Base::BaseSections::low
uint32_t low
Definition: gdt.hpp:30
GDT::Base::value
uint32_t value
Definition: gdt.hpp:33
GDT::Limit::section
struct GDT::Limit::LimitSections section
GDT::Entry
GDT Code & Data Segment Selector Struct See https://wiki.osdev.org/Descriptors#Code....
Definition: gdt.hpp:41
GDT::Limit::LimitSections::low
uint16_t low
Definition: gdt.hpp:21
GDT
Definition: gdt.cpp:17
string.hpp
Standard string and memory utility library.
GDT::Base::BaseSections::high
uint8_t high
Definition: gdt.hpp:31
Flush.h
Header for assembly functions defined in flush.s.
GDT::gdtr
struct Registers::GDTR gdtr
Definition: gdt.cpp:20
GDT::gdt
struct Entry gdt[ARCH_GDT_MAX_ENTRIES]
Definition: gdt.cpp:19
GDT::Limit::value
uint32_t value
Definition: gdt.hpp:24
GDT::Base
Definition: gdt.hpp:27
gdt_flush
void gdt_flush(uintptr_t gdt)
Flush the global descriptor table and use the one provided.
GDT::Limit
Definition: gdt.hpp:18
GDT::Base::section
struct GDT::Base::BaseSections section
gdt.hpp
The Global Descriptor Table (GDT) is specific to the IA32 architecture. It contains entries telling t...
ARCH_GDT_MAX_ENTRIES
#define ARCH_GDT_MAX_ENTRIES
Definition: gdt.cpp:15
GDT::init
void init()
Setup and install the GDT onto the system.
Definition: gdt.cpp:22
GDT::Limit::LimitSections::high
uint8_t high
Definition: gdt.hpp:22