2023-03-01 21:21:08 +02:00
|
|
|
#include <kernel/GDT.h>
|
2024-03-06 00:02:18 +02:00
|
|
|
#include <kernel/Debug.h>
|
2023-03-01 21:21:08 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-03-06 00:02:18 +02:00
|
|
|
namespace Kernel
|
2023-03-01 21:21:08 +02:00
|
|
|
{
|
|
|
|
|
2024-03-06 00:02:18 +02:00
|
|
|
GDT* GDT::create()
|
2023-03-01 21:21:08 +02:00
|
|
|
{
|
2024-03-06 00:02:18 +02:00
|
|
|
auto* gdt = new GDT();
|
|
|
|
ASSERT(gdt);
|
2023-03-01 21:21:08 +02:00
|
|
|
|
2024-03-06 00:02:18 +02:00
|
|
|
gdt->write_entry(0x00, 0x00000000, 0x00000, 0x00, 0x0); // null
|
|
|
|
gdt->write_entry(0x08, 0x00000000, 0xFFFFF, 0x9A, 0xA); // kernel code
|
|
|
|
gdt->write_entry(0x10, 0x00000000, 0xFFFFF, 0x92, 0xC); // kernel data
|
|
|
|
gdt->write_entry(0x18, 0x00000000, 0xFFFFF, 0xFA, 0xA); // user code
|
|
|
|
gdt->write_entry(0x20, 0x00000000, 0xFFFFF, 0xF2, 0xC); // user data
|
|
|
|
gdt->write_tss();
|
2023-04-25 14:48:12 +03:00
|
|
|
|
2024-03-06 00:02:18 +02:00
|
|
|
gdt->flush_gdt();
|
|
|
|
gdt->flush_tss();
|
|
|
|
|
|
|
|
return gdt;
|
|
|
|
}
|
2023-03-01 21:21:08 +02:00
|
|
|
|
2024-03-06 00:02:18 +02:00
|
|
|
void GDT::write_entry(uint8_t offset, uint32_t base, uint32_t limit, uint8_t access, uint8_t flags)
|
2023-03-01 21:21:08 +02:00
|
|
|
{
|
2023-10-13 16:18:22 +03:00
|
|
|
ASSERT(offset % sizeof(SegmentDescriptor) == 0);
|
2024-03-06 00:02:18 +02:00
|
|
|
uint8_t idx = offset / sizeof(SegmentDescriptor);
|
2023-03-01 21:21:08 +02:00
|
|
|
|
2024-03-06 00:02:18 +02:00
|
|
|
auto& desc = m_gdt[idx];
|
2023-10-13 16:18:22 +03:00
|
|
|
desc.base1 = (base >> 0) & 0xFFFF;
|
|
|
|
desc.base2 = (base >> 16) & 0xFF;
|
|
|
|
desc.base3 = (base >> 24) & 0xFF;
|
2023-03-01 21:21:08 +02:00
|
|
|
|
2023-10-13 16:18:22 +03:00
|
|
|
desc.limit1 = (limit >> 0) & 0xFFFF;
|
|
|
|
desc.limit2 = (limit >> 16) & 0x0F;
|
2023-03-01 21:21:08 +02:00
|
|
|
|
2023-10-13 16:18:22 +03:00
|
|
|
desc.access = access & 0xFF;
|
|
|
|
|
|
|
|
desc.flags = flags & 0x0F;
|
2023-03-01 21:21:08 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 00:02:18 +02:00
|
|
|
void GDT::write_tss()
|
2023-03-01 21:21:08 +02:00
|
|
|
{
|
2024-03-06 00:02:18 +02:00
|
|
|
memset(&m_tss, 0x00, sizeof(TaskStateSegment));
|
|
|
|
m_tss.iopb = sizeof(TaskStateSegment);
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2024-03-06 00:02:18 +02:00
|
|
|
uint64_t base = reinterpret_cast<uint64_t>(&m_tss);
|
2023-03-01 21:21:08 +02:00
|
|
|
|
2024-03-06 00:02:18 +02:00
|
|
|
write_entry(0x28, (uint32_t)base, sizeof(TaskStateSegment), 0x89, 0x0);
|
2023-10-13 16:18:22 +03:00
|
|
|
|
2024-03-06 00:02:18 +02:00
|
|
|
auto& desc = m_gdt[0x30 / sizeof(SegmentDescriptor)];
|
2023-03-01 21:21:08 +02:00
|
|
|
desc.low = base >> 32;
|
|
|
|
desc.high = 0;
|
|
|
|
}
|
|
|
|
|
2024-01-24 14:43:46 +02:00
|
|
|
}
|