From 4e8fbfb2d158de7b1cadd1c060acee51a7d67309 Mon Sep 17 00:00:00 2001 From: Alex Smith Date: Tue, 3 Jul 2012 20:55:36 +0100 Subject: [PATCH] x86_{read,write}_cr{0,4} can just be implemented as macros, put an x86_ prefix on the other read/write macros for consistency. --- headers/private/kernel/arch/x86/arch_cpu.h | 47 ++++++++++++++----- headers/private/kernel/arch/x86/arch_int.h | 4 +- headers/private/kernel/arch/x86/arch_thread.h | 5 +- .../kernel/arch/x86/{arch_x86.S => 32/arch.S} | 26 ---------- src/system/kernel/arch/x86/32/int.cpp | 7 +-- src/system/kernel/arch/x86/64/int.cpp | 5 +- src/system/kernel/arch/x86/Jamfile | 2 +- src/system/kernel/arch/x86/arch_cpu.cpp | 2 +- src/system/kernel/arch/x86/arch_debug.cpp | 16 +++---- .../x86/paging/32bit/X86PagingMethod32Bit.cpp | 7 ++- .../paging/32bit/X86PagingStructures32Bit.cpp | 3 +- .../x86/paging/pae/X86PagingMethodPAE.cpp | 9 ++-- 12 files changed, 61 insertions(+), 72 deletions(-) rename src/system/kernel/arch/x86/{arch_x86.S => 32/arch.S} (94%) diff --git a/headers/private/kernel/arch/x86/arch_cpu.h b/headers/private/kernel/arch/x86/arch_cpu.h index d5e284e006..a70f55de23 100644 --- a/headers/private/kernel/arch/x86/arch_cpu.h +++ b/headers/private/kernel/arch/x86/arch_cpu.h @@ -270,19 +270,46 @@ typedef struct arch_cpu_info { #define nop() __asm__ ("nop"::) -#define read_cr2(value) \ - __asm__("mov %%cr2,%0" : "=r" (value)) +#define x86_read_cr0() ({ \ + size_t _v; \ + __asm__("mov %%cr0,%0" : "=r" (_v)); \ + _v; \ +}) -#define read_cr3(value) \ - __asm__("mov %%cr3,%0" : "=r" (value)) +#define x86_write_cr0(value) \ + __asm__("mov %0,%%cr0" : : "r" (value)) -#define write_cr3(value) \ +#define x86_read_cr2() ({ \ + size_t _v; \ + __asm__("mov %%cr2,%0" : "=r" (_v)); \ + _v; \ +}) + +#define x86_read_cr3() ({ \ + size_t _v; \ + __asm__("mov %%cr3,%0" : "=r" (_v)); \ + _v; \ +}) + +#define x86_write_cr3(value) \ __asm__("mov %0,%%cr3" : : "r" (value)) -#define read_dr3(value) \ - __asm__("mov %%dr3,%0" : "=r" (value)) +#define x86_read_cr4() ({ \ + size_t _v; \ + __asm__("mov %%cr4,%0" : "=r" (_v)); \ + _v; \ +}) -#define write_dr3(value) \ +#define x86_write_cr4(value) \ + __asm__("mov %0,%%cr4" : : "r" (value)) + +#define x86_read_dr3() ({ \ + size_t _v; \ + __asm__("mov %%dr3,%0" : "=r" (_v)); \ + _v; \ +}) + +#define x86_write_dr3(value) \ __asm__("mov %0,%%dr3" : : "r" (value)) #define invalidate_TLB(va) \ @@ -360,10 +387,6 @@ void i386_noop_swap(void* oldFpuState, const void* newFpuState); void i386_fnsave_swap(void* oldFpuState, const void* newFpuState); void i386_fxsave_swap(void* oldFpuState, const void* newFpuState); uint32 x86_read_ebp(); -uint32 x86_read_cr0(); -void x86_write_cr0(uint32 value); -uint32 x86_read_cr4(); -void x86_write_cr4(uint32 value); uint64 x86_read_msr(uint32 registerNumber); void x86_write_msr(uint32 registerNumber, uint64 value); void x86_set_task_gate(int32 cpu, int32 n, int32 segment); diff --git a/headers/private/kernel/arch/x86/arch_int.h b/headers/private/kernel/arch/x86/arch_int.h index f00b56e91e..da4943726d 100644 --- a/headers/private/kernel/arch/x86/arch_int.h +++ b/headers/private/kernel/arch/x86/arch_int.h @@ -21,7 +21,7 @@ arch_int_enable_interrupts_inline(void) static inline int arch_int_disable_interrupts_inline(void) { - unsigned long flags; + size_t flags; asm volatile("pushf;\n" "pop %0;\n" @@ -41,7 +41,7 @@ arch_int_restore_interrupts_inline(int oldState) static inline bool arch_int_are_interrupts_enabled_inline(void) { - unsigned long flags; + size_t flags; asm volatile("pushf;\n" "pop %0;\n" : "=g" (flags)); diff --git a/headers/private/kernel/arch/x86/arch_thread.h b/headers/private/kernel/arch/x86/arch_thread.h index 567d4caff0..cf2bb57034 100644 --- a/headers/private/kernel/arch/x86/arch_thread.h +++ b/headers/private/kernel/arch/x86/arch_thread.h @@ -57,8 +57,7 @@ void arch_syscall_64_bit_return_value(void); static inline Thread* arch_thread_get_current_thread(void) { - Thread* t; - read_dr3(t); + Thread* t = (Thread*)x86_read_dr3(); return t; } @@ -66,7 +65,7 @@ arch_thread_get_current_thread(void) static inline void arch_thread_set_current_thread(Thread* t) { - write_dr3(t); + x86_write_dr3(t); } diff --git a/src/system/kernel/arch/x86/arch_x86.S b/src/system/kernel/arch/x86/32/arch.S similarity index 94% rename from src/system/kernel/arch/x86/arch_x86.S rename to src/system/kernel/arch/x86/32/arch.S index 1ebcae0e82..0d256a5512 100644 --- a/src/system/kernel/arch/x86/arch_x86.S +++ b/src/system/kernel/arch/x86/32/arch.S @@ -86,32 +86,6 @@ FUNCTION(x86_read_ebp): ret FUNCTION_END(x86_read_ebp) -/* uint32 x86_read_cr0(); */ -FUNCTION(x86_read_cr0): - movl %cr0, %eax - ret -FUNCTION_END(x86_read_cr0) - -/* void x86_write_cr0(uint32 value); */ -FUNCTION(x86_write_cr0): - movl 4(%esp), %eax - movl %eax, %cr0 - ret -FUNCTION_END(x86_write_cr0) - -/* uint32 x86_read_cr4(); */ -FUNCTION(x86_read_cr4): - movl %cr4, %eax - ret -FUNCTION_END(x86_read_cr4) - -/* void x86_write_cr4(uint32 value); */ -FUNCTION(x86_write_cr4): - movl 4(%esp), %eax - movl %eax, %cr4 - ret -FUNCTION_END(x86_write_cr4) - /* uint64 x86_read_msr(uint32 register); */ FUNCTION(x86_read_msr): movl 4(%esp), %ecx diff --git a/src/system/kernel/arch/x86/32/int.cpp b/src/system/kernel/arch/x86/32/int.cpp index fe54d5800f..d35e3f0c63 100644 --- a/src/system/kernel/arch/x86/32/int.cpp +++ b/src/system/kernel/arch/x86/32/int.cpp @@ -322,8 +322,7 @@ x86_double_fault_exception(struct iframe* frame) void x86_page_fault_exception_double_fault(struct iframe* frame) { - uint32 cr2; - asm("movl %%cr2, %0" : "=r" (cr2)); + addr_t cr2 = x86_read_cr2(); // Only if this CPU has a fault handler, we're allowed to be here. cpu_ent& cpu = gCPU[x86_double_fault_get_cpu()]; @@ -351,11 +350,9 @@ static void page_fault_exception(struct iframe* frame) { Thread *thread = thread_get_current_thread(); - uint32 cr2; + addr_t cr2 = x86_read_cr2(); addr_t newip; - asm("movl %%cr2, %0" : "=r" (cr2)); - if (debug_debugger_running()) { // If this CPU or this thread has a fault handler, we're allowed to be // here. diff --git a/src/system/kernel/arch/x86/64/int.cpp b/src/system/kernel/arch/x86/64/int.cpp index d9f3a0a76a..e1ce5e817c 100644 --- a/src/system/kernel/arch/x86/64/int.cpp +++ b/src/system/kernel/arch/x86/64/int.cpp @@ -53,7 +53,7 @@ extern void hardware_interrupt(struct iframe* frame); static const char* -exception_name(unsigned long number, char* buffer, size_t bufferSize) +exception_name(uint64 number, char* buffer, size_t bufferSize) { if (number >= 0 && number < (sizeof(kInterruptNames) / sizeof(kInterruptNames[0]))) @@ -97,8 +97,7 @@ unexpected_exception(iframe* frame) static void page_fault_exception(iframe* frame) { - unsigned long cr2; - read_cr2(cr2); + addr_t cr2 = x86_read_cr2(); panic("page fault exception at ip %#lx on %#lx, error code %#lx\n", frame->rip, cr2, frame->error_code); diff --git a/src/system/kernel/arch/x86/Jamfile b/src/system/kernel/arch/x86/Jamfile index c2b29afaaa..d8019117b0 100644 --- a/src/system/kernel/arch/x86/Jamfile +++ b/src/system/kernel/arch/x86/Jamfile @@ -28,6 +28,7 @@ if $(TARGET_ARCH) = x86_64 { SEARCH_SOURCE += [ FDirName $(SUBDIR) 32 ] ; archSpecificSources = + arch.S int.cpp interrupts.S @@ -42,7 +43,6 @@ if $(TARGET_ARCH) = x86_64 { arch_timer.cpp arch_vm.cpp arch_vm_translation_map.cpp - arch_x86.S arch_system_info.cpp arch_user_debugger.cpp apic.cpp diff --git a/src/system/kernel/arch/x86/arch_cpu.cpp b/src/system/kernel/arch/x86/arch_cpu.cpp index 6d5a322218..2617c2e579 100644 --- a/src/system/kernel/arch/x86/arch_cpu.cpp +++ b/src/system/kernel/arch/x86/arch_cpu.cpp @@ -355,7 +355,7 @@ init_double_fault(int cpuNum) tss->sp0 = (uint32)x86_get_double_fault_stack(cpuNum, &stackSize); tss->sp0 += stackSize; tss->ss0 = KERNEL_DATA_SEG; - read_cr3(tss->cr3); + tss->cr3 = x86_read_cr3(); // copy the current cr3 to the double fault cr3 tss->eip = (uint32)&double_fault; tss->es = KERNEL_DATA_SEG; diff --git a/src/system/kernel/arch/x86/arch_debug.cpp b/src/system/kernel/arch/x86/arch_debug.cpp index d3c6b8b1e9..f00cb70f67 100644 --- a/src/system/kernel/arch/x86/arch_debug.cpp +++ b/src/system/kernel/arch/x86/arch_debug.cpp @@ -401,8 +401,8 @@ setup_for_thread(char *arg, Thread **_thread, uint32 *_ebp, thread_get_current_thread(), thread); if (newPageDirectory != 0) { - read_cr3(*_oldPageDirectory); - write_cr3(newPageDirectory); + *_oldPageDirectory = x86_read_cr3(); + x86_write_cr3(newPageDirectory); } if (thread->state == B_THREAD_RUNNING) { @@ -669,7 +669,7 @@ stack_trace(int argc, char **argv) if (oldPageDirectory != 0) { // switch back to the previous page directory to no cause any troubles - write_cr3(oldPageDirectory); + x86_write_cr3(oldPageDirectory); } return 0; @@ -829,7 +829,7 @@ show_call(int argc, char **argv) if (oldPageDirectory != 0) { // switch back to the previous page directory to not cause any troubles - write_cr3(oldPageDirectory); + x86_write_cr3(oldPageDirectory); } return 0; @@ -938,8 +938,8 @@ cmd_in_context(int argc, char** argv) thread_get_current_thread(), thread); if (newPageDirectory != 0) { - read_cr3(oldPageDirectory); - write_cr3(newPageDirectory); + oldPageDirectory = x86_read_cr3(); + x86_write_cr3(newPageDirectory); } } @@ -951,7 +951,7 @@ cmd_in_context(int argc, char** argv) // reset the page directory if (oldPageDirectory) - write_cr3(oldPageDirectory); + x86_write_cr3(oldPageDirectory); return 0; } @@ -1128,7 +1128,7 @@ arch_debug_get_interrupt_pc(bool* _isSyscall) void arch_debug_unset_current_thread(void) { - write_dr3(NULL); + x86_write_dr3(NULL); } diff --git a/src/system/kernel/arch/x86/paging/32bit/X86PagingMethod32Bit.cpp b/src/system/kernel/arch/x86/paging/32bit/X86PagingMethod32Bit.cpp index 674c4b5a60..ad0c812357 100644 --- a/src/system/kernel/arch/x86/paging/32bit/X86PagingMethod32Bit.cpp +++ b/src/system/kernel/arch/x86/paging/32bit/X86PagingMethod32Bit.cpp @@ -419,9 +419,8 @@ X86PagingMethod32Bit::IsKernelPageAccessible(addr_t virtualAddress, { // We only trust the kernel team's page directory. So switch to it first. // Always set it to make sure the TLBs don't contain obsolete data. - uint32 physicalPageDirectory; - read_cr3(physicalPageDirectory); - write_cr3(fKernelPhysicalPageDirectory); + uint32 physicalPageDirectory = x86_read_cr3(); + x86_write_cr3(fKernelPhysicalPageDirectory); // get the page directory entry for the address page_directory_entry pageDirectoryEntry; @@ -465,7 +464,7 @@ X86PagingMethod32Bit::IsKernelPageAccessible(addr_t virtualAddress, // switch back to the original page directory if (physicalPageDirectory != fKernelPhysicalPageDirectory) - write_cr3(physicalPageDirectory); + x86_write_cr3(physicalPageDirectory); if ((pageTableEntry & X86_PTE_PRESENT) == 0) return false; diff --git a/src/system/kernel/arch/x86/paging/32bit/X86PagingStructures32Bit.cpp b/src/system/kernel/arch/x86/paging/32bit/X86PagingStructures32Bit.cpp index 4b86a85611..3538951466 100644 --- a/src/system/kernel/arch/x86/paging/32bit/X86PagingStructures32Bit.cpp +++ b/src/system/kernel/arch/x86/paging/32bit/X86PagingStructures32Bit.cpp @@ -100,8 +100,7 @@ X86PagingStructures32Bit::Delete() #if 0 // this sanity check can be enabled when corruption due to // overwriting an active page directory is suspected - uint32 activePageDirectory; - read_cr3(activePageDirectory); + uint32 activePageDirectory = x86_read_cr3(); if (activePageDirectory == pgdir_phys) panic("deleting a still active page directory\n"); #endif diff --git a/src/system/kernel/arch/x86/paging/pae/X86PagingMethodPAE.cpp b/src/system/kernel/arch/x86/paging/pae/X86PagingMethodPAE.cpp index 280dd143cb..4156b3ad33 100644 --- a/src/system/kernel/arch/x86/paging/pae/X86PagingMethodPAE.cpp +++ b/src/system/kernel/arch/x86/paging/pae/X86PagingMethodPAE.cpp @@ -163,7 +163,7 @@ struct X86PagingMethodPAE::ToPAESwitcher { private: static void _EnablePAE(void* physicalPDPT, int cpu) { - write_cr3((addr_t)physicalPDPT); + x86_write_cr3((addr_t)physicalPDPT); x86_write_cr4(x86_read_cr4() | IA32_CR4_PAE | IA32_CR4_GLOBAL_PAGES); } @@ -681,9 +681,8 @@ X86PagingMethodPAE::IsKernelPageAccessible(addr_t virtualAddress, // We only trust the kernel team's page directories. So switch to the // kernel PDPT first. Always set it to make sure the TLBs don't contain // obsolete data. - uint32 physicalPDPT; - read_cr3(physicalPDPT); - write_cr3(fKernelPhysicalPageDirPointerTable); + uint32 physicalPDPT = x86_read_cr3(); + x86_write_cr3(fKernelPhysicalPageDirPointerTable); // get the PDPT entry for the address pae_page_directory_pointer_table_entry pdptEntry = 0; @@ -734,7 +733,7 @@ X86PagingMethodPAE::IsKernelPageAccessible(addr_t virtualAddress, // switch back to the original page directory if (physicalPDPT != fKernelPhysicalPageDirPointerTable) - write_cr3(physicalPDPT); + x86_write_cr3(physicalPDPT); if ((pageTableEntry & X86_PAE_PTE_PRESENT) == 0) return false;