2018-10-16 12:01:38 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "types.h"
|
2018-11-12 15:25:16 +03:00
|
|
|
#include "kprintf.h"
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2018-11-08 16:35:30 +03:00
|
|
|
#define PAGE_SIZE 4096
|
2018-11-08 14:59:16 +03:00
|
|
|
#define PAGE_MASK 0xfffff000
|
2018-10-18 14:05:00 +03:00
|
|
|
|
2018-10-16 12:01:38 +03:00
|
|
|
union Descriptor {
|
|
|
|
struct {
|
|
|
|
WORD limit_lo;
|
|
|
|
WORD base_lo;
|
|
|
|
BYTE base_hi;
|
|
|
|
BYTE type : 4;
|
|
|
|
BYTE descriptor_type : 1;
|
|
|
|
BYTE dpl : 2;
|
|
|
|
BYTE segment_present : 1;
|
|
|
|
BYTE limit_hi : 4;
|
|
|
|
BYTE : 1;
|
|
|
|
BYTE zero : 1;
|
|
|
|
BYTE operation_size : 1;
|
|
|
|
BYTE granularity : 1;
|
|
|
|
BYTE base_hi2;
|
|
|
|
};
|
|
|
|
struct {
|
|
|
|
DWORD low;
|
|
|
|
DWORD high;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Type {
|
|
|
|
Invalid = 0,
|
|
|
|
AvailableTSS_16bit = 0x1,
|
|
|
|
LDT = 0x2,
|
|
|
|
BusyTSS_16bit = 0x3,
|
|
|
|
CallGate_16bit = 0x4,
|
|
|
|
TaskGate = 0x5,
|
|
|
|
InterruptGate_16bit = 0x6,
|
|
|
|
TrapGate_16bit = 0x7,
|
|
|
|
AvailableTSS_32bit = 0x9,
|
|
|
|
BusyTSS_32bit = 0xb,
|
|
|
|
CallGate_32bit = 0xc,
|
|
|
|
InterruptGate_32bit = 0xe,
|
|
|
|
TrapGate_32bit = 0xf,
|
|
|
|
};
|
|
|
|
|
|
|
|
void setBase(void* b)
|
|
|
|
{
|
|
|
|
base_lo = (DWORD)(b) & 0xffff;
|
|
|
|
base_hi = ((DWORD)(b) >> 16) & 0xff;
|
|
|
|
base_hi2 = ((DWORD)(b) >> 24) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setLimit(DWORD l)
|
|
|
|
{
|
|
|
|
limit_lo = (DWORD)l & 0xffff;
|
|
|
|
limit_hi = ((DWORD)l >> 16) & 0xff;
|
|
|
|
}
|
|
|
|
} PACKED;
|
|
|
|
|
2018-10-22 13:58:29 +03:00
|
|
|
class IRQHandler;
|
|
|
|
|
2018-10-16 12:01:38 +03:00
|
|
|
void gdt_init();
|
|
|
|
void idt_init();
|
|
|
|
void registerInterruptHandler(BYTE number, void (*f)());
|
|
|
|
void registerUserCallableInterruptHandler(BYTE number, void (*f)());
|
2018-10-22 13:58:29 +03:00
|
|
|
void registerIRQHandler(BYTE number, IRQHandler&);
|
|
|
|
void unregisterIRQHandler(BYTE number, IRQHandler&);
|
2018-10-16 12:01:38 +03:00
|
|
|
void flushIDT();
|
|
|
|
void flushGDT();
|
2018-11-07 23:38:18 +03:00
|
|
|
void load_task_register(WORD selector);
|
2018-11-01 18:23:12 +03:00
|
|
|
word gdt_alloc_entry();
|
|
|
|
void gdt_free_entry(word);
|
2018-10-16 12:01:38 +03:00
|
|
|
Descriptor& getGDTEntry(WORD selector);
|
|
|
|
void writeGDTEntry(WORD selector, Descriptor&);
|
|
|
|
|
|
|
|
#define HANG asm volatile( "cli; hlt" );
|
|
|
|
#define LSW(x) ((DWORD)(x) & 0xFFFF)
|
|
|
|
#define MSW(x) (((DWORD)(x) >> 16) & 0xFFFF)
|
|
|
|
#define LSB(x) ((x) & 0xFF)
|
|
|
|
#define MSB(x) (((x)>>8) & 0xFF)
|
|
|
|
|
2018-10-19 12:28:43 +03:00
|
|
|
#define cli() asm volatile("cli")
|
|
|
|
#define sti() asm volatile("sti")
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2018-10-24 12:07:53 +03:00
|
|
|
static inline dword cpuFlags()
|
|
|
|
{
|
|
|
|
dword flags;
|
|
|
|
asm volatile(
|
|
|
|
"pushf\n"
|
|
|
|
"pop %0\n"
|
|
|
|
:"=rm"(flags)
|
|
|
|
::"memory");
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2018-11-08 18:09:05 +03:00
|
|
|
inline bool are_interrupts_enabled()
|
|
|
|
{
|
|
|
|
return cpuFlags() & 0x200;
|
|
|
|
}
|
|
|
|
|
2018-10-24 12:07:53 +03:00
|
|
|
class InterruptDisabler {
|
|
|
|
public:
|
|
|
|
InterruptDisabler()
|
|
|
|
{
|
|
|
|
m_flags = cpuFlags();
|
|
|
|
cli();
|
|
|
|
}
|
|
|
|
|
|
|
|
~InterruptDisabler()
|
|
|
|
{
|
|
|
|
if (m_flags & 0x200)
|
|
|
|
sti();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
dword m_flags;
|
|
|
|
};
|
|
|
|
|
2018-10-16 12:01:38 +03:00
|
|
|
/* Map IRQ0-15 @ ISR 0x50-0x5F */
|
|
|
|
#define IRQ_VECTOR_BASE 0x50
|
2018-10-18 00:49:32 +03:00
|
|
|
|
2018-10-18 14:05:00 +03:00
|
|
|
struct PageFaultFlags {
|
|
|
|
enum Flags {
|
|
|
|
NotPresent = 0x00,
|
|
|
|
ProtectionViolation = 0x01,
|
|
|
|
Read = 0x00,
|
|
|
|
Write = 0x02,
|
|
|
|
UserMode = 0x04,
|
|
|
|
SupervisorMode = 0x00,
|
|
|
|
InstructionFetch = 0x08,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
class PageFault {
|
|
|
|
public:
|
2018-11-05 12:29:19 +03:00
|
|
|
PageFault(word code, LinearAddress laddr)
|
2018-10-18 14:05:00 +03:00
|
|
|
: m_code(code)
|
2018-11-05 12:29:19 +03:00
|
|
|
, m_laddr(laddr)
|
2018-10-18 14:05:00 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-05 12:29:19 +03:00
|
|
|
LinearAddress laddr() const { return m_laddr; }
|
2018-10-18 14:05:00 +03:00
|
|
|
word code() const { return m_code; }
|
|
|
|
|
2018-11-05 12:29:19 +03:00
|
|
|
bool is_not_present() const { return (m_code & 1) == PageFaultFlags::NotPresent; }
|
|
|
|
bool is_protection_violation() const { return (m_code & 1) == PageFaultFlags::ProtectionViolation; }
|
|
|
|
bool is_read() const { return (m_code & 2) == PageFaultFlags::Read; }
|
|
|
|
bool is_write() const { return (m_code & 2) == PageFaultFlags::Write; }
|
|
|
|
bool is_user() const { return (m_code & 4) == PageFaultFlags::UserMode; }
|
|
|
|
bool is_supervisor() const { return (m_code & 4) == PageFaultFlags::SupervisorMode; }
|
|
|
|
bool is_instruction_fetch() const { return (m_code & 8) == PageFaultFlags::InstructionFetch; }
|
2018-10-18 14:05:00 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
word m_code;
|
2018-11-05 12:29:19 +03:00
|
|
|
LinearAddress m_laddr;
|
2018-10-18 14:05:00 +03:00
|
|
|
};
|
|
|
|
|
2018-10-18 00:49:32 +03:00
|
|
|
struct RegisterDump {
|
2018-10-23 11:12:50 +03:00
|
|
|
WORD ss;
|
2018-10-18 00:49:32 +03:00
|
|
|
WORD gs;
|
|
|
|
WORD fs;
|
|
|
|
WORD es;
|
|
|
|
WORD ds;
|
|
|
|
DWORD edi;
|
|
|
|
DWORD esi;
|
|
|
|
DWORD ebp;
|
|
|
|
DWORD esp;
|
|
|
|
DWORD ebx;
|
|
|
|
DWORD edx;
|
|
|
|
DWORD ecx;
|
|
|
|
DWORD eax;
|
|
|
|
DWORD eip;
|
|
|
|
WORD cs;
|
|
|
|
WORD __csPadding;
|
|
|
|
DWORD eflags;
|
2018-10-18 15:53:00 +03:00
|
|
|
DWORD esp_if_crossRing;
|
|
|
|
WORD ss_if_crossRing;
|
2018-10-18 00:49:32 +03:00
|
|
|
} PACKED;
|
|
|
|
|
2018-11-05 15:48:07 +03:00
|
|
|
struct RegisterDumpWithExceptionCode {
|
|
|
|
WORD ss;
|
|
|
|
WORD gs;
|
|
|
|
WORD fs;
|
|
|
|
WORD es;
|
|
|
|
WORD ds;
|
|
|
|
DWORD edi;
|
|
|
|
DWORD esi;
|
|
|
|
DWORD ebp;
|
|
|
|
DWORD esp;
|
|
|
|
DWORD ebx;
|
|
|
|
DWORD edx;
|
|
|
|
DWORD ecx;
|
|
|
|
DWORD eax;
|
|
|
|
WORD exception_code;
|
|
|
|
WORD __exception_code_padding;
|
|
|
|
DWORD eip;
|
|
|
|
WORD cs;
|
|
|
|
WORD __csPadding;
|
|
|
|
DWORD eflags;
|
|
|
|
DWORD esp_if_crossRing;
|
|
|
|
WORD ss_if_crossRing;
|
|
|
|
} PACKED;
|
|
|
|
|
2018-10-18 14:05:00 +03:00
|
|
|
inline constexpr dword pageBaseOf(dword address)
|
|
|
|
{
|
|
|
|
return address & 0xfffff000;
|
|
|
|
}
|
|
|
|
|
2018-11-02 11:25:23 +03:00
|
|
|
class CPUID {
|
|
|
|
public:
|
|
|
|
CPUID(dword function) { asm volatile("cpuid" : "=a" (m_eax), "=b" (m_ebx), "=c" (m_ecx), "=d" (m_edx) : "a" (function), "c" (0)); }
|
|
|
|
dword eax() const { return m_eax; }
|
|
|
|
dword ebx() const { return m_ebx; }
|
|
|
|
dword ecx() const { return m_ecx; }
|
|
|
|
dword edx() const { return m_edx; }
|
|
|
|
private:
|
|
|
|
dword m_eax { 0xffffffff };
|
|
|
|
dword m_ebx { 0xffffffff };
|
|
|
|
dword m_ecx { 0xffffffff };
|
|
|
|
dword m_edx { 0xffffffff };
|
|
|
|
};
|
2018-11-12 15:25:16 +03:00
|
|
|
|
|
|
|
inline void read_tsc(dword& lsw, dword& msw)
|
|
|
|
{
|
|
|
|
asm volatile("rdtsc":"=d"(msw),"=a"(lsw));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Stopwatch {
|
|
|
|
public:
|
|
|
|
Stopwatch(const char* name)
|
|
|
|
: m_name(name)
|
|
|
|
{
|
|
|
|
read_tsc(m_start_lsw, m_start_msw);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Stopwatch()
|
|
|
|
{
|
|
|
|
dword end_lsw;
|
|
|
|
dword end_msw;
|
|
|
|
read_tsc(end_lsw, end_msw);
|
|
|
|
if (m_start_msw != end_msw) {
|
2018-11-13 02:17:30 +03:00
|
|
|
dbgprintf("stopwatch: differing msw, no result for %s\n", m_name);
|
2018-11-12 15:25:16 +03:00
|
|
|
}
|
|
|
|
dword diff = end_lsw - m_start_lsw;
|
|
|
|
dbgprintf("Stopwatch(%s): %u ticks\n", m_name, diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* m_name { nullptr };
|
|
|
|
dword m_start_lsw { 0 };
|
|
|
|
dword m_start_msw { 0 };
|
|
|
|
};
|