Skip to content

Commit

Permalink
feat(arch, cpu): gdtr done
Browse files Browse the repository at this point in the history
Signed-off-by: Zone.N <[email protected]>
  • Loading branch information
MRNIU committed Jul 2, 2024
1 parent 89a242e commit 1ef02e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 53 deletions.
68 changes: 16 additions & 52 deletions src/kernel/arch/x86_64/arch_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,6 @@ static void Fillrect(uint8_t *vram, uint32_t pitch, uint8_t r, uint8_t g,
}
}

struct GDTEntry {
uint16_t Limit0 = 0;
uint16_t Base0 = 0;
uint8_t Base1 = 0;
uint8_t AccessByte = 0;
uint8_t Limit1_Flags = 0;
uint8_t Base2 = 0;
};

GDTEntry gdt[5];

void gdt_set_gate(int num, uint32_t base, uint32_t limit, uint8_t access,
uint8_t gran) {
// gdt[num].Base0 = (base & 0xFFFF);
// gdt[num].Base1 = (base >> 16) & 0xFF;
// gdt[num].Base2 = (base >> 24) & 0xFF;

// gdt[num].Limit0 = (limit & 0xFFFF);
// gdt[num].Limit1_Flags = (limit >> 16) & 0x0F;

gdt[num].Limit1_Flags |= gran & 0xF0;
gdt[num].AccessByte = access;
}

uint32_t ArchInit(uint32_t argc, uint8_t *argv) {
if (argc != 1) {
Err("argc != 1 [%d]\n", argc);
Expand Down Expand Up @@ -142,45 +118,33 @@ uint32_t ArchInit(uint32_t argc, uint8_t *argv) {
cpu::reginfo::GdtrInfo::SegmentDescriptor::kPresent,
cpu::reginfo::GdtrInfo::SegmentDescriptor::kNotAvailable,
cpu::reginfo::GdtrInfo::SegmentDescriptor::k64Bit);

// std::cout << segment_descriptors[0] << std::endl;
// std::cout << segment_descriptors[1] << std::endl;
// std::cout << segment_descriptors[2] << std::endl;
// std::cout << segment_descriptors[3] << std::endl;
// std::cout << segment_descriptors[4] << std::endl;

gdt_set_gate(0, 0, 0, 0, 0);
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xAF);
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xAF);
gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xAF);
gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xAF);

cpu::reginfo::GdtrInfo::Gdtr gdtr{};
gdtr.limit = (sizeof(cpu::reginfo::GdtrInfo::SegmentDescriptor) *
// 加载描述符
cpu::reginfo::GdtrInfo::Gdtr gdtr{
.limit = (sizeof(cpu::reginfo::GdtrInfo::SegmentDescriptor) *
cpu::reginfo::GdtrInfo::kGdtMaxCount) -
1;
// gdtr.base = segment_descriptors;
gdtr.base = (cpu::reginfo::GdtrInfo::SegmentDescriptor *)gdt;
1,
.base = segment_descriptors,
};
cpu::kAllCr.gdtr.Write(gdtr);

// 刷新段选择子
cpu::kAllCr.ds.Write(0x10);
cpu::kAllCr.es.Write(0x10);
cpu::kAllCr.fs.Write(0x10);
cpu::kAllCr.gs.Write(0x10);
cpu::kAllCr.ss.Write(0x10);
cpu::kAllCr.cs.Write(0x8);

std::cout << cpu::kAllCr.es << std::endl;
std::cout << cpu::kAllCr.cs << std::endl;
std::cout << cpu::kAllCr.ss << std::endl;
std::cout << cpu::kAllCr.ds << std::endl;
std::cout << cpu::kAllCr.fs << std::endl;
std::cout << cpu::kAllCr.gs << std::endl;
std::cout << "es: " << cpu::kAllCr.es << std::endl;
std::cout << "cs: " << cpu::kAllCr.cs << std::endl;
std::cout << "ss: " << cpu::kAllCr.ss << std::endl;
std::cout << "ds: " << cpu::kAllCr.ds << std::endl;
std::cout << "fs: " << cpu::kAllCr.fs << std::endl;
std::cout << "gs: " << cpu::kAllCr.gs << std::endl;

for (auto i = 0; i < (cpu::kAllCr.gdtr.Read().limit + 1) /
sizeof(cpu::reginfo::GdtrInfo::SegmentDescriptor);
for (size_t i = 0; i < (cpu::kAllCr.gdtr.Read().limit + 1) /
sizeof(cpu::reginfo::GdtrInfo::SegmentDescriptor);
i++) {
printf("[0x%X]0x%p\n", i, cpu::kAllCr.gdtr.Read().base + i);
printf("gdtr[%d] 0x%p\n", i, cpu::kAllCr.gdtr.Read().base + i);
std::cout << *(cpu::kAllCr.gdtr.Read().base + i) << std::endl;
}

Expand Down
2 changes: 1 addition & 1 deletion src/kernel/arch/x86_64/include/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ struct GdtrInfo : public RegInfoBase {
SegmentDescriptor(SegmentDescriptor &&) = default;
auto operator=(const SegmentDescriptor &) -> SegmentDescriptor & = default;
auto operator=(SegmentDescriptor &&) -> SegmentDescriptor & = default;
virtual ~SegmentDescriptor() = default;
~SegmentDescriptor() = default;
/// @}

friend std::ostream &operator<<(
Expand Down

0 comments on commit 1ef02e4

Please sign in to comment.