1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-11-26 11:17:59 +00:00
Files
CLK/InstructionSets/x86/Descriptors.hpp
2025-03-27 18:08:09 -04:00

111 lines
2.3 KiB
C++

//
// Descriptors.hpp
// Clock Signal
//
// Created by Thomas Harte on 19/03/2025.
// Copyright © 2025 Thomas Harte. All rights reserved.
//
#pragma once
#include "Instruction.hpp"
namespace InstructionSet::x86 {
enum class DescriptorTable {
Global, Local, Interrupt,
};
struct DescriptorTablePointer {
uint16_t limit;
uint32_t base;
};
struct Descriptor {
void set_segment(const uint16_t segment) {
base_ = uint32_t(segment) << 4;
limit_ = 0xffff;
present_ = true;
}
void set(const uint16_t descriptor[4]) {
base_ = uint32_t(descriptor[1] | ((descriptor[2] & 0xff) << 16));
limit_ = descriptor[0];
printf("%04x %04x %04x %04x\n", descriptor[0], descriptor[1], descriptor[2], descriptor[3]);
present_ = descriptor[2] & 0x8000;
privilege_level_ = (descriptor[2] >> 13) & 3;
// TODO: need to know more about the below.
if(descriptor[2] & 0x1000) {
executable_ = descriptor[2] & 0x800;
if(executable_) {
conforming_ = descriptor[2] & 0x400;
readable_ = descriptor[2] & 0x200;
} else {
// expand down = descriptor[2] & 0x400;
// writeable_ = descriptor[2] & 0x200;
}
} else {
assert(false);
}
}
uint32_t to_linear(const uint32_t address) const {
return base_ + address;
}
uint32_t base() const { return base_; }
uint32_t limit() const { return limit_; }
int privilege_level() const {
return privilege_level_;
}
private:
uint32_t base_;
uint32_t limit_;
// TODO: permissions, type, etc.
int privilege_level_;
enum class Type {
AvailableTaskStateSegment = 1,
LDTDescriptor = 2,
BusyTaskStateSegment = 3,
Invalid0 = 0, Invalid8 = 8,
Control4 = 4, Control5 = 5, Control6 = 6, Control7 = 7,
Reserved9 = 9, ReservedA = 10, ReservedB = 11, ReservedC = 12,
ReservedD = 13, ReservedE = 14, ReservedF = 15,
} type_;
bool present_;
bool readable_;
bool conforming_;
bool executable_;
};
template <typename SegmentT>
struct SegmentRegisterSet {
SegmentT &operator[](const Source segment) {
return values_[index_of(segment)];
}
const SegmentT &operator[](const Source segment) const {
return values_[index_of(segment)];
}
private:
std::array<SegmentT, 6> values_;
static constexpr size_t index_of(const Source segment) {
assert(is_segment_register(segment));
return size_t(segment) - size_t(Source::ES);
}
};
}