AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
gdt.c File Reference

x86: the Global Descriptor Table (GDT) and the TSS. More...

#include <core/system.h>
#include <core/panic.h>
#include <core/assert.h>
#include <core/string.h>
#include <cpu/msr.h>
#include <sys/cpu.h>
#include "cpu.h"

Go to the source code of this file.

Macros

#define RW_DATA   0x2
#define XR_CODE   0xA
#define TSS_AVL   0x9
#define BASE   0
#define LIMIT   -1
#define DPL0   0
#define DPL3   3
#define L   1
#define D   0
#define MSR_GS_BASE   0xC0000101
#define MSR_KERNEL_GS_BASE   0xC0000102

Functions

static void segment_set (struct x86_gdt_entry *e, uintptr_t base, size_t limit)
 Sets the base and limit of a system or data segment descriptor, with byte granularity.
void x86_gdt_setup (struct cpu *cpu, uintptr_t sp)
 Sets up and loads the running CPU's GDT and TSS, and points gs (or the GS base, on x86_64) at its struct cpu, so that curcpu() works.
void x86_kernel_stack_set (uintptr_t sp)
 Sets the kernel stack that interrupts from user mode switch to, on the running CPU: the thread that is about to run's.

Variables

static const struct x86_gdt_entry gdt_template [X86_GDT_TSS]

Detailed Description

x86: the Global Descriptor Table (GDT) and the TSS.

Each CPU has its own GDT: the flat kernel and user code and data segments, which every CPU shares, then its own TSS (the stack that interrupts from user mode switch to) and, on i386, a segment for its struct cpu, which curcpu() reads through gs.

Definition in file gdt.c.

Macro Definition Documentation

◆ BASE

#define BASE   0

Definition at line 25 of file gdt.c.

◆ D

#define D   0

Definition at line 36 of file gdt.c.

◆ DPL0

#define DPL0   0

Definition at line 28 of file gdt.c.

Referenced by x86_gdt_setup(), and x86_idt_gate_set().

◆ DPL3

#define DPL3   3

Definition at line 29 of file gdt.c.

Referenced by x86_gdt_setup(), and x86_idt_gate_user_set().

◆ L

#define L   1

Definition at line 35 of file gdt.c.

◆ LIMIT

#define LIMIT   -1

Definition at line 26 of file gdt.c.

◆ MSR_GS_BASE

#define MSR_GS_BASE   0xC0000101

Definition at line 57 of file gdt.c.

Referenced by x86_gdt_setup().

◆ MSR_KERNEL_GS_BASE

#define MSR_KERNEL_GS_BASE   0xC0000102

Definition at line 58 of file gdt.c.

Referenced by x86_gdt_setup().

◆ RW_DATA

#define RW_DATA   0x2

Definition at line 21 of file gdt.c.

Referenced by x86_gdt_setup().

◆ TSS_AVL

#define TSS_AVL   0x9

Definition at line 23 of file gdt.c.

Referenced by x86_gdt_setup().

◆ XR_CODE

#define XR_CODE   0xA

Definition at line 22 of file gdt.c.

Function Documentation

◆ segment_set()

void segment_set ( struct x86_gdt_entry * e,
uintptr_t base,
size_t limit )
static

Sets the base and limit of a system or data segment descriptor, with byte granularity.

Definition at line 62 of file gdt.c.

References x86_gdt_entry::base_hi, x86_gdt_entry::base_lo, x86_gdt_entry::base_mid, x86_gdt_entry::limit_hi, x86_gdt_entry::limit_lo, and segment_set().

Referenced by segment_set(), and x86_gdt_setup().

◆ x86_gdt_setup()

void x86_gdt_setup ( struct cpu * cpu,
uintptr_t sp )

Sets up and loads the running CPU's GDT and TSS, and points gs (or the GS base, on x86_64) at its struct cpu, so that curcpu() works.

Parameters
cputhe running CPU
spthe kernel stack that interrupts from user mode switch to (each thread sets its own later, x86_kernel_stack_set())

Definition at line 79 of file gdt.c.

References x86_tss::_, x86_tss::__, cpu::arch, assert_alignof, assert_sizeof, x86_gdt_entry::db, x86_gdt_entry::dpl, DPL0, DPL3, arch_cpu::gdt, gdt_template, memcpy(), memset(), MSR_GS_BASE, MSR_KERNEL_GS_BASE, x86_gdt_entry::p, RW_DATA, x86_gdt_entry::s, segment_set(), cpu::self, x86_tss::sp, arch_cpu::tss, TSS_AVL, x86_gdt_entry::type, X86_GDT_CPU, x86_gdt_setup(), X86_GDT_TSS, x86_lgdt(), x86_ltr(), and X86_SEL_CPU.

Referenced by x86_ap_start(), x86_cpu_init(), and x86_gdt_setup().

◆ x86_kernel_stack_set()

void x86_kernel_stack_set ( uintptr_t sp)

Sets the kernel stack that interrupts from user mode switch to, on the running CPU: the thread that is about to run's.

Parameters
spthe top of the stack

Definition at line 152 of file gdt.c.

References x86_kernel_stack_set().

Referenced by arch_idle(), arch_thread_kill(), arch_thread_spawn(), arch_thread_switch(), and x86_kernel_stack_set().

Variable Documentation

◆ gdt_template

const struct x86_gdt_entry gdt_template[X86_GDT_TSS]
static
Initial value:
= {
{0},
{LIMIT, BASE, BASE, XR_CODE, 1, DPL0, 1, LIMIT, 0, L, D, 1, BASE},
{LIMIT, BASE, BASE, RW_DATA, 1, DPL0, 1, LIMIT, 0, L, D, 1, BASE},
{LIMIT, BASE, BASE, XR_CODE, 1, DPL3, 1, LIMIT, 0, L, D, 1, BASE},
{LIMIT, BASE, BASE, RW_DATA, 1, DPL3, 1, LIMIT, 0, L, D, 1, BASE},
}
#define LIMIT
Definition gdt.c:26
#define DPL0
Definition gdt.c:28
#define DPL3
Definition gdt.c:29
#define BASE
Definition gdt.c:25
#define XR_CODE
Definition gdt.c:22
#define L
Definition gdt.c:35
#define D
Definition gdt.c:36
#define RW_DATA
Definition gdt.c:21

Definition at line 40 of file gdt.c.

Referenced by x86_gdt_setup().