The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
gdt.h File Reference
#include "common/stdint.h"

Go to the source code of this file.

Macros

#define GDT_KERNEL_CODE   0x08
 kernel code segment selector (GDT index 1, RPL 0).
#define GDT_KERNEL_DATA   0x10
 kernel data segment selector (GDT index 2, RPL 0).
#define GDT_USER_CODE   0x1B
 user code segment selector (GDT index 3, RPL 3).
#define GDT_USER_DATA   0x23
 user data segment selector (GDT index 4, RPL 3).
#define GDT_TSS   0x28
 TSS segment selector (GDT index 5, RPL 0).
#define GDT_DOUBLE_FAULT_TSS   0x30
 double-fault task TSS selector (GDT index 6, RPL 0).
#define GDT_USER_TLS   0x3B
#define GDT_KERNEL_PERCPU   0x40

Functions

void gdt_initialize (uint32_t core)
 set up the GDT of a core with kernel segments, user segments and a Task State Segment, and load the GDT into GDTR and the TSS into TR.
void tss_set_kernel_stack (uintptr_t esp0)
 update the kernel stack pointer (ESP0) in the TSS of the core that runs the call.
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.
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.
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 through a task gate, so the handler runs on its own stack even when the faulting stack is unusable (a kernel stack overflow into its guard page).
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.
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.

Macro Definition Documentation

◆ GDT_KERNEL_CODE

#define GDT_KERNEL_CODE   0x08

kernel code segment selector (GDT index 1, RPL 0).

◆ GDT_KERNEL_DATA

#define GDT_KERNEL_DATA   0x10

kernel data segment selector (GDT index 2, RPL 0).

◆ GDT_USER_CODE

#define GDT_USER_CODE   0x1B

user code segment selector (GDT index 3, RPL 3).

◆ GDT_USER_DATA

#define GDT_USER_DATA   0x23

user data segment selector (GDT index 4, RPL 3).

◆ GDT_TSS

#define GDT_TSS   0x28

TSS segment selector (GDT index 5, RPL 0).

◆ GDT_DOUBLE_FAULT_TSS

#define GDT_DOUBLE_FAULT_TSS   0x30

double-fault task TSS selector (GDT index 6, RPL 0).

◆ GDT_USER_TLS

#define GDT_USER_TLS   0x3B

◆ GDT_KERNEL_PERCPU

#define GDT_KERNEL_PERCPU   0x40

Function Documentation

◆ 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, and load the GDT into GDTR and the TSS into TR.

each core has its own GDT and TSS. call it on the core itself.

Parameters
corethe index of the core (cpu_t.index), 0 for the boot core.

◆ 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.

called on context switches so the CPU knows where to find the kernel stack when an interrupt occurs in ring 3.

Parameters
esp0the kernel stack pointer to store.

◆ 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.

Parameters
basethe thread pointer.

◆ 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.

the interrupt entry loads FS with it again in kernel mode.

Parameters
corethe index of the core that runs the call.
basethe address of the cpu_t.

◆ 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 through a task gate, so the handler runs on its own stack even when the faulting stack is unusable (a kernel stack overflow into its guard page).

each core has its own task.

Parameters
corethe index of the core.
entryfunction the task starts in; it must never return.
stack_topinitial ESP for the task.

◆ 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.

Parameters
corethe index of the core.
cr3physical address of the kernel page directory.

◆ 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.

the task switch saves it into the main TSS of the core that runs the call.

Parameters
eipreceives the interrupted EIP.
espreceives the interrupted ESP.
ebpreceives the interrupted EBP.