summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/gdt.c
blob: 3756d21d5288612090705e86ae6454a9a5730de8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "arch/x86_64/tables.h"
#include "arch/x86_64/processor.h"
#include "string.h"
#include "print.h"

static segment_descriptor_t s_gdt[] = {
    [GDT_ENTRY_KERNEL_NULL] =
    { 0 }, //Kernel NULL
    [GDT_ENTRY_KERNEL_CODE] = 
    {      //Kernel Code
        .limit_0_15 = 0xFFFF,
        .base_0_15 = 0x0,
        .type_flags = CD_SEGMENT_TYPE_CODE | CD_SEGMENT_TYPE_WRITEABLE |
                      GDT_SEGMENT_FLAG_S |
                      GDT_SEGMENT_FLAG_DPL(0) |
                      GDT_SEGMENT_FLAG_P,
        .limit_16_19_avlx = 0xF | GDT_SEGMENT_AVLX_L | GDT_SEGMENT_AVLX_G,
        .base_24_31 = 0x0
    },
    [GDT_ENTRY_KERNEL_DATA] = 
    {      //Kernel Data
        .limit_0_15 = 0xFFFF,
        .base_0_15 = 0x0,
        .type_flags = CD_SEGMENT_TYPE_WRITEABLE |
                      GDT_SEGMENT_FLAG_S |
                      GDT_SEGMENT_FLAG_DPL(0) |
                      GDT_SEGMENT_FLAG_P,
        .limit_16_19_avlx = 0xF | GDT_SEGMENT_AVLX_L | GDT_SEGMENT_AVLX_G,
        .base_24_31 = 0x0
    },
    [GDT_ENTRY_USER_NULL] = 
    { 0 }, //User NULL
    [GDT_ENTRY_USER_DATA] = 
    {      //User Data
        .limit_0_15 = 0xFFFF,
        .base_0_15 = 0x0,
        .type_flags = CD_SEGMENT_TYPE_WRITEABLE |
                      GDT_SEGMENT_FLAG_S |
                      GDT_SEGMENT_FLAG_DPL(3) |
                      GDT_SEGMENT_FLAG_P,
        .limit_16_19_avlx = 0xF | GDT_SEGMENT_AVLX_L | GDT_SEGMENT_AVLX_G,
        .base_24_31 = 0x0
    },
    [GDT_ENTRY_USER_CODE] = 
    {      //User Code
        .limit_0_15 = 0xFFFF,
        .base_0_15 = 0x0,
        .type_flags = CD_SEGMENT_TYPE_CODE | CD_SEGMENT_TYPE_WRITEABLE |
                      GDT_SEGMENT_FLAG_S |
                      GDT_SEGMENT_FLAG_DPL(3) |
                      GDT_SEGMENT_FLAG_P,
        .limit_16_19_avlx = 0xF | GDT_SEGMENT_AVLX_L | GDT_SEGMENT_AVLX_G,
        .base_24_31 = 0x0
    },
    [GDT_ENTRY_TSS_LOW] = 
    {       //TSS Low
        .limit_0_15 = 0,
        .base_0_15 = 0,
        .base_16_23 = 0,
        .type_flags = GDT_SEGMENT_TYPE_TSS_AVAIL |
                      GDT_SEGMENT_FLAG_DPL(0) |
                      GDT_SEGMENT_FLAG_P,
        .limit_16_19_avlx = 0,
        .base_24_31 = 0
    },
    [GDT_ENTRY_TSS_HIGH] =
    { 0 }   //TSS High
};

void
gdt_setup(processor_t *processor)
{
    size_t gdtw = GDT_ENTRY_COUNT * sizeof(segment_descriptor_t);
    klogf("%X\n", gdtw);
    memcpy(processor->gdt, &s_gdt, gdtw);

    processor->gdtr.base = (uintptr_t)&s_gdt;
    processor->gdtr.length = gdtw - 1;

    segment_descriptor_t *tss_lo = &processor->gdt[GDT_ENTRY_TSS_LOW];
    segment_descriptor_t *tss_hi = &processor->gdt[GDT_ENTRY_TSS_HIGH];

    uintptr_t tssb = (uintptr_t)&processor->tss;
    size_t tssl = sizeof(tss_t);

    tss_lo->limit_0_15 = tssl & 0xFF;
    tss_lo->base_0_15 = tssb & 0xFFFF;
    tss_lo->base_16_23 = (tssb >> 16) & 0xFF;
    tss_lo->base_24_31 = (tssb >> 24) & 0xFF;
    tss_hi->limit_0_15 = (tssb >> 32) & 0xFFFF;
    tss_hi->base_0_15 = (tssb >> 48) & 0xFFFF;

    tss_lo->limit_16_19_avlx = (tssl >> 16) & 0xF;

    processor->tss.iopb = tssl;
    
    extern void gdt_load(void*);
    gdt_load(&processor->gdtr);

    extern void tss_flush(uint16_t);
    tss_flush(GDT_OFFSET_TSS);
}