! Fixed the root Makefile
[lightOS.git] / kernel / x86 / gdt.cpp
blob389f12441bea2d446f217e235e25821ac8fd00db
1 /*
2 lightOS kernel
3 Copyright (C) 2007-2009 Jörg Pfähler
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <kernel/x86_shared/gdt.hpp>
20 #include <kernel/processor.hpp>
21 using namespace kernel;
23 void gdt::load()
25 struct
27 uint16_t size;
28 uint32_t gdt;
29 } __attribute__((packed)) gdtr = {static_cast<uint16_t>(mSize * 8 - 1), reinterpret_cast<uintptr_t>(mGdt)};
31 asm volatile("lgdt %0" :: "m"(gdtr));
33 #ifdef _LIGHTOS_V86
34 void gdt::clear_tss_busy_bit(size_t index)
36 mGdt[index].x86.flags &= ~0x02;
38 #endif
39 size_t gdt::get_descriptor_count()
41 return 1 + // Zero descriptor
42 4 + // Kernel Data & Code, User Data & Code
43 #ifdef _LIGHTOS_V86
44 2 + // Virtual-8086-mode TSS & Kernel TSS
45 #endif
46 processor::count(); // Thread TSS
48 size_t gdt::tss_descriptor_size()
50 return 1;
52 void gdt::set_tss_descriptor( size_t index,
53 uintptr_t address)
55 set_segment_descriptor( index,
56 address,
57 sizeof(task_state_segment),
58 0x89,
59 0x00);
61 void gdt::set_segment_descriptor( size_t index,
62 uintptr_t address,
63 size_t limit,
64 uint8_t flags,
65 uint8_t flags2)
67 mGdt[index].x86.limit = limit & 0xFFFF;
68 mGdt[index].x86.address0 = address & 0xFFFF;
69 mGdt[index].x86.address1 = (address >> 16) & 0xFF;
70 mGdt[index].x86.flags = flags;
71 mGdt[index].x86.flags_limit = (flags2 << 4) | ((limit >> 16) & 0x0F);
72 mGdt[index].x86.address2 = (address >> 24) & 0xFF;