aboutsummaryrefslogtreecommitdiff
path: root/core/gdt.c
blob: 819262eed812cdfcd391a61abd174b6ea1975ef6 (plain)
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
/*
 * Copyright (C) 2017 Robin Krahl <robin.krahl@ireas.org>
 * 
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#include <garmos/gdt.h>
#include <garmos/types.h>

struct gdt_sgmt {
	uint16_t limit_lo;
	uint16_t base_lo;
	uint8_t base_hi;
	uint8_t access;
	uint8_t flags_limit_h;
	uint8_t base_vhi;
} __attribute__((packed));

enum gdt_sgmts {
	GDT_SGMT_NULL,
	GDT_SGMT_CODE,
	GDT_SGMT_DATA,
	GDT_SGMT_COUNT
};

struct gdt_sgmt gdt[GDT_SGMT_COUNT];

struct gdtp {
	uint16_t limit;
	void *pointer;
} __attribute__((packed));

struct gdtp gdtp = {
	.limit = GDT_SGMT_COUNT * sizeof(*gdt) - 1,
	.pointer = gdt,
};

static void gdt_sgmt_set(uint32_t base, uint32_t limit, uint8_t access,
		struct gdt_sgmt *s)
{
	if (limit <= 0xFFFF) {
		s->flags_limit_h = 0x40;
	} else {
		if ((limit & 0xFFF) != 0xFFF) {
			limit = (limit >> 12) - 1;
		} else {
			limit >>= 12;
		}
		s->flags_limit_h = 0xC0;
	}

	s->limit_lo = limit & 0xFFFF;
	s->flags_limit_h |= (limit >> 16) & 0xF;

	s->base_lo = base & 0xFFFF;
	s->base_hi = (base >> 16) & 0xFF;
	s->base_vhi = (base >> 24) & 0xFF;

	s->access = access;
}

void gdt_init(void)
{
	gdt_sgmt_set(0, 0, 0, &gdt[GDT_SGMT_NULL]);
	gdt_sgmt_set(0, 64 * 1024 * 1024, 0x9A, &gdt[GDT_SGMT_CODE]);
	gdt_sgmt_set(0, 64 * 1024 * 1024, 0x92, &gdt[GDT_SGMT_NULL]);

	asm volatile("lgdt %0" : : "m" (gdtp));
}