PatchworkOS
Loading...
Searching...
No Matches
gdt.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/cpu/tss.h>
4#include <kernel/defs.h>
5
6#include <stdint.h>
7
21typedef enum
22{
26 GDT_RING3 = 3
28
51
77
82typedef enum
83{
87 GDT_FLAGS_SIZE = 1 << 2,
88 GDT_FLAGS_4KB = 1 << 3
90
101
117
135
152
160extern void gdt_load_descriptor(gdt_desc_t* descriptor);
161
167void gdt_init(void);
168
176void gdt_cpu_load(void);
177
186void gdt_cpu_tss_load(tss_t* tss);
187
gdt_segment_selector_t
Segment selectors.
Definition gdt.h:37
void gdt_cpu_tss_load(tss_t *tss)
Load a TSS into the GDT and load it using the ltr instruction on the current CPU.
Definition gdt.c:63
gdt_flags_t
Flags byte.
Definition gdt.h:83
gdt_ring_t
Definition gdt.h:22
gdt_access_t
Access byte.
Definition gdt.h:57
void gdt_init(void)
Initialize the GDT.
Definition gdt.c:35
void gdt_load_descriptor(gdt_desc_t *descriptor)
Loads a GDT descriptor.
void gdt_cpu_load(void)
Load the GDT on the current CPU.
Definition gdt.c:55
@ GDT_KERNEL_DATA
Kernel data segment selector.
Definition gdt.h:40
@ GDT_CS_RING3
Value to load into the CS register for user code.
Definition gdt.h:48
@ GDT_USER_DATA
User data segment selector.
Definition gdt.h:41
@ GDT_KERNEL_CODE
Kernel code segment selector.
Definition gdt.h:39
@ GDT_TSS
TSS segment selector.
Definition gdt.h:43
@ GDT_CS_RING0
Value to load into the CS register for kernel code.
Definition gdt.h:45
@ GDT_SS_RING3
Value to load into the SS register for user data.
Definition gdt.h:49
@ GDT_SS_RING0
Value to load into the SS register for kernel data.
Definition gdt.h:46
@ GDT_NULL
Null segment selector, unused but the gdt must start with it.
Definition gdt.h:38
@ GDT_USER_CODE
User code segment selector.
Definition gdt.h:42
@ GDT_FLAGS_SIZE
If set, its a 32-bit segment. If clear, its a 16-bit segment. Ignored in 64-bit mode.
Definition gdt.h:87
@ GDT_FLAGS_RESERVED
Must be 0.
Definition gdt.h:85
@ GDT_FLAGS_4KB
If set, the limit is in 4KiB blocks. If clear, the limit is in bytes.
Definition gdt.h:88
@ GDT_FLAGS_NONE
No flags set.
Definition gdt.h:84
@ GDT_FLAGS_LONG_MODE
If set, its a 64-bit code segment.
Definition gdt.h:86
@ GDT_RING2
Definition gdt.h:25
@ GDT_RING3
Definition gdt.h:26
@ GDT_RING1
Definition gdt.h:24
@ GDT_RING0
Definition gdt.h:23
@ GDT_ACCESS_RING0
Descriptor Privilege Level 0 (kernel).
Definition gdt.h:70
@ GDT_ACCESS_RING1
Descriptor Privilege Level 1, unused.
Definition gdt.h:71
@ GDT_ACCESS_ACCESSED
Will be set to 1 when accessed, but its best to set it to 1 manually.
Definition gdt.h:58
@ GDT_ACCESS_TYPE_LDT
Local Descriptor Table. Only used if the SYSTEM bit is 0.
Definition gdt.h:66
@ GDT_ACCESS_EXEC
If set, the segment is a code segment. If clear, its a data segment.
Definition gdt.h:62
@ GDT_ACCESS_DIRECTION_CONFORMING
Definition gdt.h:60
@ GDT_ACCESS_SYSTEM
Is a system segment.
Definition gdt.h:63
@ GDT_ACCESS_READ_WRITE
If set on a code segment, its readable. If set on a data segment, its writable.
Definition gdt.h:59
@ GDT_ACCESS_PRESENT
Must be 1 for all valid segments.
Definition gdt.h:75
@ GDT_ACCESS_DATA_CODE
Is a data or code segment.
Definition gdt.h:64
@ GDT_ACCESS_TYPE_TSS_BUSY
Busy 64-bit Task State Segment. Only used if the SYSTEM bit is 0.
Definition gdt.h:68
@ GDT_ACCESS_RING2
Descriptor Privilege Level 2, unused.
Definition gdt.h:72
@ GDT_ACCESS_RING3
Descriptor Privilege Level 3 (user).
Definition gdt.h:73
@ GDT_ACCESS_TYPE_TSS_AVAILABLE
Available 64-bit Task State Segment. Only used if the SYSTEM bit is 0.
Definition gdt.h:67
#define PACKED
GCC packed attribute.
Definition defs.h:32
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
__UINT16_TYPE__ uint16_t
Definition stdint.h:13
GDT descriptor structure.
Definition gdt.h:97
uint64_t offset
Address of the GDT.
Definition gdt.h:99
uint16_t size
Size of the GDT - 1.
Definition gdt.h:98
A long mode system segment descriptor, used for TSS and LDT.
Definition gdt.h:125
uint8_t baseUpperMiddle
Definition gdt.h:131
uint8_t baseLowerMiddle
Definition gdt.h:128
uint8_t flagsAndLimitHigh
Definition gdt.h:130
A single GDT segment descriptor.
Definition gdt.h:109
uint16_t baseLow
Definition gdt.h:111
uint8_t baseHigh
Definition gdt.h:115
uint16_t limitLow
Definition gdt.h:110
uint8_t baseMiddle
Definition gdt.h:112
uint8_t flagsAndLimitHigh
Definition gdt.h:114
uint8_t access
Definition gdt.h:113
The actual GDT structure.
Definition gdt.h:144
gdt_segment_t kernelCode
Definition gdt.h:146
gdt_segment_t userData
Definition gdt.h:148
gdt_segment_t userCode
Definition gdt.h:149
gdt_long_system_segment_t tssDesc
Definition gdt.h:150
gdt_segment_t null
Definition gdt.h:145
gdt_segment_t kernelData
Definition gdt.h:147
Task State Segment structure.
Definition tss.h:88