1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/kernel/x86-family/idt.cpp
2014-03-17 19:22:13 +01:00

78 lines
2.3 KiB
C++

/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
This file is part of Sortix.
Sortix is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
Sortix. If not, see <http://www.gnu.org/licenses/>.
x86-family/idt.cpp
Initializes and handles the interrupt descriptor table.
*******************************************************************************/
#include <stdint.h>
#include <string.h>
#include "idt.h"
namespace Sortix {
namespace IDT {
static struct idt_entry idt_entries[256];
void Init()
{
memset(&idt_entries, 0, sizeof(idt_entries));
Set(idt_entries, 256);
}
void Set(struct idt_entry* table, size_t length)
{
size_t limit = sizeof(idt_entry) * length - 1;
#if defined(__x86_64__)
asm volatile ("subq $10, %%rsp\n\t"
"movw %w0, 0(%%rsp)\n\t"
"movq %1, 2(%%rsp)\n\t"
"lidt (%%rsp)\n\t"
"addq $10, %%rsp" : : "rN"(limit), "r"(table));
#else
asm volatile ("subl $6, %%esp\n\t"
"movw %w0, 0(%%esp)\n\t"
"movl %1, 2(%%esp)\n\t"
"lidt (%%esp)\n\t"
"addl $6, %%esp" : : "rN"(limit), "r"(table));
#endif
}
void SetEntry(struct idt_entry* entry, uintptr_t handler, uint16_t selector, uint8_t flags, uint8_t ist)
{
entry->flags = flags;
entry->ist = ist;
entry->selector = selector;
entry->handler_low = handler >> 0 & 0xFFFF;
entry->handler_high = handler >> 16 & 0xFFFF;
#if defined(__x86_64__)
entry->handler_highest = handler >> 32 & 0xFFFFFFFFU;
entry->reserved1 = 0;
#endif
}
void SetEntry(uint8_t num, uintptr_t handler, uint16_t selector, uint8_t flags, uint8_t ist)
{
SetEntry(idt_entries + num, handler, selector, flags, ist);
}
} // namespace IDT
} // namespace Sortix