os3/host/gdtCreator/gdtCreator.cpp
John Stefanelli d57078cc2d
[MBR] Add GDT setup
[Host/Kernelcopy] Add support for direct writing into MBR's DAP
[Rawimg] Add kernelcopy support
2023-01-13 20:57:40 +01:00

51 lines
No EOL
1.5 KiB
C++

//
// Created by John Stefanelli on 13/01/23.
//
#include <iostream>
uint64_t makeEntry(uint32_t base, uint16_t limit, bool present, unsigned char dpl, bool executable, bool exec_write) {
uint64_t value = 0x0;
//auto* target = (uint8_t*) &value;
uint8_t access_byte = (present ? 0x80 : 0x00) + ((dpl & 0x3) << 5) + 16 + (executable ? 8 : 0) + (exec_write ? 2 : 0);
uint8_t flags = 0xC;
value |= limit & 0xFFFF; //Low 16 bits of limit
value |= (base & 0xFFFFFF) << 16; //Low 24 bits of base
value |= ((uint64_t) access_byte) << 40;
value |= ((uint64_t) limit & 0xF0000) << 32;
value |= ((uint64_t) flags) << 52;
value |= ((uint64_t) base & 0xFF000000) << 32;
/*
// Encode the limit
target[0] = limit & 0xFF;
target[1] = (limit >> 8) & 0xFF;
target[6] = (limit >> 16) & 0x0F;
// Encode the base
target[2] = base & 0xFF;
target[3] = (base >> 8) & 0xFF;
target[4] = (base >> 16) & 0xFF;
target[7] = (base >> 24) & 0xFF;
// Encode the access byte
target[5] = access_byte;
// Encode the flags
target[6] |= (flags << 4);
*/
return value;
}
int main(int argc, char** argv) {
std::cout << std::hex << "Code: " << makeEntry(0, 0xFFFF, true, 0x0, true, true) << std::endl;
std::cout << std::hex << "Data: " << makeEntry(0, 0xFFFF, true, 0x0, false, true) << std::endl;
std::cout << std::hex << "User Code: " << makeEntry(0, 0xFFFF, true, 0x3, true, true) << std::endl;
std::cout << std::hex << "User Data: " << makeEntry(0, 0xFFFF, true, 0x3, false, true) << std::endl;
return 0;
}