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

Macros

#define GDT_ENTRIES   9

Functions

struct gdt_entry __attribute__ ((packed))
void gdt_flush (uint32_t)
void tss_flush (void)
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 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_set_kernel_stack (uintptr_t esp0)
 update the kernel stack pointer (ESP0) in the TSS of the core that runs the call.
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.

Variables

uint16_t limit_low
uint16_t base_low
uint8_t base_middle
uint8_t access
uint8_t granularity
uint8_t base_high
uint16_t limit
uint32_t base
uint32_t prev_tss
uint32_t esp0
uint32_t ss0
uint32_t esp1
uint32_t ss1
uint32_t esp2
uint32_t ss2
uint32_t cr3
uint32_t eip
uint32_t eflags
uint32_t eax
uint32_t ecx
uint32_t edx
uint32_t ebx
uint32_t esp
uint32_t ebp
uint32_t esi
uint32_t edi
uint32_t es
uint32_t cs
uint32_t ss
uint32_t ds
uint32_t fs
uint32_t gs
uint32_t ldt
uint16_t trap
uint16_t iomap_base

Macro Definition Documentation

◆ GDT_ENTRIES

#define GDT_ENTRIES   9

Function Documentation

◆ __attribute__()

struct gdt_entry __attribute__ ( (packed) )

◆ gdt_flush()

void gdt_flush ( uint32_t )
extern

◆ tss_flush()

void tss_flush ( void )
extern

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

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

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

Variable Documentation

◆ limit_low

uint16_t limit_low

◆ base_low

uint16_t base_low

◆ base_middle

uint8_t base_middle

◆ access

uint8_t access

◆ granularity

uint8_t granularity

◆ base_high

uint8_t base_high

◆ limit

uint16_t limit

◆ base

uint32_t base

◆ prev_tss

uint32_t prev_tss

◆ esp0

uint32_t esp0

◆ ss0

uint32_t ss0

◆ esp1

uint32_t esp1

◆ ss1

uint32_t ss1

◆ esp2

uint32_t esp2

◆ ss2

uint32_t ss2

◆ cr3

uint32_t cr3

◆ eip

uint32_t eip

◆ eflags

uint32_t eflags

◆ eax

uint32_t eax

◆ ecx

uint32_t ecx

◆ edx

uint32_t edx

◆ ebx

uint32_t ebx

◆ esp

uint32_t esp

◆ ebp

uint32_t ebp

◆ esi

uint32_t esi

◆ edi

uint32_t edi

◆ es

◆ cs

◆ ss

◆ ds

◆ fs

◆ gs

◆ ldt

uint32_t ldt

◆ trap

uint16_t trap

◆ iomap_base

uint16_t iomap_base