More assembler inlines moved to asm.hpp

This commit is contained in:
Felix Queißner 2016-05-18 10:38:47 +02:00
parent cb806c19ec
commit 5384c20f09
3 changed files with 33 additions and 25 deletions

View file

@ -1,5 +1,11 @@
#pragma once #pragma once
#include <stddef.h>
#include <inttypes.h>
#define ASM_READ_REG(reg, var) asm volatile("mov %%" #reg ", %0" : "=r" (var));
#define ASM_WRITE_REG(reg, var) asm volatile("mov %0, %%" #reg : : "r" (var));
namespace ASM namespace ASM
{ {
static inline void sti() static inline void sti()
@ -23,4 +29,22 @@ namespace ASM
asm volatile ("inb %1, %0" : "=a" (data) : "d" (port)); asm volatile ("inb %1, %0" : "=a" (data) : "d" (port));
return data; return data;
} }
static inline void invlpg(void* m)
{
/* Clobber memory to avoid optimizer re-ordering access before invlpg, which may cause nasty bugs. */
asm volatile ( "invlpg (%0)" : : "b"(m) : "memory" );
}
static inline void lidt(void *idt, size_t length)
{
struct {
uint16_t limit;
void* pointer;
} __attribute__((packed)) idtp = {
.limit = uint16_t(length * 8 - 1),
.pointer = idt,
};
asm volatile("lidt %0" : : "m" (idtp));
}
} }

View file

@ -1,5 +1,5 @@
#include "idt.hpp" #include "idt.hpp"
#include "io.hpp" #include "asm.hpp"
#include "bsod.hpp" #include "bsod.hpp"
#include "pic.hpp" #include "pic.hpp"
@ -42,14 +42,7 @@ void IDT::initialize()
#undef ISR #undef ISR
#undef ISR_ERR #undef ISR_ERR
struct { ASM::lidt(IDT::descriptors, IDT::length);
uint16_t limit;
void* pointer;
} __attribute__((packed)) idtp = {
.limit = IDT::length * sizeof(InterruptDescriptor) - 1,
.pointer = IDT::descriptors,
};
asm volatile("lidt %0" : : "m" (idtp));
IDT::setupPIC(); IDT::setupPIC();
} }

View file

@ -1,22 +1,19 @@
#include "vmm.hpp" #include "vmm.hpp"
#include <new> #include <new>
#include "console.hpp" #include "console.hpp"
#include "asm.hpp"
void VMM::enable() void VMM::enable()
{ {
uint32_t cr0; uint32_t val;
asm volatile("mov %%cr0, %0" : "=r" (cr0)); ASM_READ_REG(cr0, val);
cr0 |= (1 << 31); val |= (1 << 31);
asm volatile("mov %0, %%cr0" : : "r" (cr0)); ASM_WRITE_REG(cr0, val);
} }
void VMM::activate(VMMContext & context) void VMM::activate(VMMContext & context)
{ {
/* ASM_WRITE_REG(cr3, context.directory);
Console::main
<< "Activate PD " << context.directory << "\n";
//*/
asm volatile("mov %0, %%cr3" : : "r" (context.directory));
} }
VMMContext::VMMContext() : VMMContext::VMMContext() :
@ -68,12 +65,6 @@ void VMMContext::provide(virtual_t virt, VMMFlags flags)
this->map(virt, PMM::alloc(), flags | VMMFlags::SystemAllocated); this->map(virt, PMM::alloc(), flags | VMMFlags::SystemAllocated);
} }
static inline void invlpg(void* m)
{
/* Clobber memory to avoid optimizer re-ordering access before invlpg, which may cause nasty bugs. */
asm volatile ( "invlpg (%0)" : : "b"(m) : "memory" );
}
/** /**
* Maps a given page into the virtual memory. * Maps a given page into the virtual memory.
*/ */
@ -92,7 +83,7 @@ void VMMContext::map(virtual_t virt, physical_t phys, VMMFlags flags)
Console::main << Console::main <<
"Mapping " << virt << " -> " << phys << " [" << bin(static_cast<int>(flags)) << "]: " << hex(pageDesc) << "\n"; "Mapping " << virt << " -> " << phys << " [" << bin(static_cast<int>(flags)) << "]: " << hex(pageDesc) << "\n";
//*/ //*/
invlpg(virt.data()); ASM::invlpg(virt.data());
} }
/** /**