toaruos/kernel/cpu/gdt.c

142 lines
2.7 KiB
C
Raw Normal View History

/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
2011-04-15 06:02:44 +04:00
* Global Descriptor Tables module
*
* Part of the ToAruOS Kernel
* (C) 2011 Kevin Lange
2011-03-30 06:08:56 +04:00
*/
2011-01-16 04:59:11 +03:00
#include <system.h>
2011-12-27 05:23:58 +04:00
#include <logging.h>
2011-01-16 04:59:11 +03:00
2011-03-30 06:08:56 +04:00
static void write_tss(int32_t, uint16_t, uint32_t);
tss_entry_t tss_entry;
2011-01-16 04:59:11 +03:00
/*
2011-03-05 06:05:03 +03:00
* Global Descriptor Table Entry
2011-01-16 04:59:11 +03:00
*/
struct gdt_entry {
2011-03-05 06:05:03 +03:00
/* Limits */
2011-01-16 04:59:11 +03:00
unsigned short limit_low;
2011-03-05 06:05:03 +03:00
/* Segment address */
2011-01-16 04:59:11 +03:00
unsigned short base_low;
unsigned char base_middle;
2011-03-05 06:05:03 +03:00
/* Access modes */
2011-01-16 04:59:11 +03:00
unsigned char access;
unsigned char granularity;
unsigned char base_high;
} __attribute__((packed));
/*
* GDT pointer
*/
struct gdt_ptr {
unsigned short limit;
unsigned int base;
} __attribute__((packed));
2011-03-30 06:08:56 +04:00
struct gdt_entry gdt[6];
2011-01-16 04:59:11 +03:00
struct gdt_ptr gp;
2011-04-15 06:02:44 +04:00
/**
2011-01-16 04:59:11 +03:00
* (ASM) gdt_flush
* Reloads the segment registers
*/
2013-06-06 10:10:36 +04:00
extern void gdt_flush(void);
2011-01-16 04:59:11 +03:00
2011-04-15 06:02:44 +04:00
/**
2011-01-16 04:59:11 +03:00
* Set a GDT descriptor
2011-04-15 06:02:44 +04:00
*
* @param num The number for the descriptor to set.
* @param base Base address
* @param limit Limit
* @param access Access permissions
* @param gran Granularity
2011-01-16 04:59:11 +03:00
*/
void
gdt_set_gate(
2013-06-06 10:10:36 +04:00
size_t num,
2011-01-16 04:59:11 +03:00
unsigned long base,
unsigned long limit,
unsigned char access,
unsigned char gran
) {
/* Base Address */
gdt[num].base_low = (base & 0xFFFF);
gdt[num].base_middle = (base >> 16) & 0xFF;
gdt[num].base_high = (base >> 24) & 0xFF;
/* Limits */
gdt[num].limit_low = (limit & 0xFFFF);
gdt[num].granularity = (limit >> 16) & 0X0F;
/* Granularity */
gdt[num].granularity |= (gran & 0xF0);
/* Access flags */
gdt[num].access = access;
}
/*
* gdt_install
* Install the kernel's GDTs
*/
void
2013-06-06 10:10:36 +04:00
gdt_install(void) {
2011-01-16 04:59:11 +03:00
/* GDT pointer and limits */
2011-03-30 06:08:56 +04:00
gp.limit = (sizeof(struct gdt_entry) * 6) - 1;
2011-01-16 19:56:44 +03:00
gp.base = (unsigned int)&gdt;
2011-01-16 04:59:11 +03:00
/* NULL */
gdt_set_gate(0, 0, 0, 0, 0);
/* Code segment */
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
/* Data segment */
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
2011-03-30 06:08:56 +04:00
/* User code */
gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
/* User data */
gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);
write_tss(5, 0x10, 0x0);
2011-01-16 04:59:11 +03:00
/* Go go go */
gdt_flush();
2011-03-30 06:08:56 +04:00
tss_flush();
2011-01-16 04:59:11 +03:00
}
2011-03-30 06:08:56 +04:00
2011-04-15 06:02:44 +04:00
/**
* Write a TSS (we only do this once)
*/
2011-03-30 06:08:56 +04:00
static void
write_tss(
int32_t num,
uint16_t ss0,
uint32_t esp0
) {
uintptr_t base = (uintptr_t)&tss_entry;
uintptr_t limit = base + sizeof(tss_entry);
/* Add the TSS descriptor to the GDT */
gdt_set_gate(num, base, limit, 0xE9, 0x00);
memset(&tss_entry, 0x0, sizeof(tss_entry));
tss_entry.ss0 = ss0;
tss_entry.esp0 = esp0;
2011-04-15 06:02:44 +04:00
/* Zero out the descriptors */
2011-03-30 06:08:56 +04:00
tss_entry.cs = 0x0b;
tss_entry.ss =
tss_entry.ds =
tss_entry.es =
tss_entry.fs =
tss_entry.gs = 0x13;
tss_entry.iomap_base = sizeof(tss_entry);
2011-03-30 06:08:56 +04:00
}
2011-04-15 06:02:44 +04:00
/**
* Set the kernel stack.
*
* @param stack Pointer to a the stack pointer for the kernel.
*/
2011-03-30 06:08:56 +04:00
void
set_kernel_stack(
uintptr_t stack
) {
tss_entry.esp0 = stack;
}