From fce8249cafa7e841c89838378167702ad5f12d46 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Fri, 24 Jul 2015 06:27:46 +0200 Subject: [PATCH] code cleanups, increasing the readability, add some comments --- hermit/arch/x86/kernel/processor.c | 33 ++++++++++++++++++------------ hermit/kernel/tasks.c | 4 ++-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/hermit/arch/x86/kernel/processor.c b/hermit/arch/x86/kernel/processor.c index 0988765ac..e2ec26f5b 100644 --- a/hermit/arch/x86/kernel/processor.c +++ b/hermit/arch/x86/kernel/processor.c @@ -104,12 +104,12 @@ static void fpu_init_fxsr(union fpu_state* fpu) static void save_fpu_state_xsave(union fpu_state* state) { - asm volatile ("xsaveq %0" : "=m"(state->xsave) : "a"(1), "d"(1) : "memory"); + asm volatile ("xsaveq %0" : "=m"(state->xsave) : "a"(-1), "d"(-1) : "memory"); } static void restore_fpu_state_xsave(union fpu_state* state) { - asm volatile ("xrstorq %0" :: "m"(state->xsave), "a"(1), "d"(1)); + asm volatile ("xrstorq %0" :: "m"(state->xsave), "a"(-1), "d"(-1)); } static void fpu_init_xsave(union fpu_state* fpu) @@ -117,7 +117,7 @@ static void fpu_init_xsave(union fpu_state* fpu) xsave_t* xs = &fpu->xsave; memset(xs, 0x00, sizeof(xsave_t)); - //xs->fxsave.twd = 0xffffu; + xs->fxsave.cwd = 0x37f; xs->fxsave.mxcsr = 0x1f80; } @@ -218,8 +218,9 @@ int cpu_detection(void) { xcr0 |= 0x2; if (has_avx()) xcr0 |= 0x4; - kprintf("Set XCR0 to 0x%llx\n", xcr0); xsetbv(0, xcr0); + + kprintf("Set XCR0 to 0x%llx\n", xgetbv(0)); } if (cpu_info.feature3 & CPU_FEATURE_SYSCALL) { @@ -257,7 +258,11 @@ int cpu_detection(void) { } if (first_time) { - kprintf("CPU features: %s%s%s%s%s%s%s%s%s%s%s%s%s\n", + // reload feature list because we enabled OSXSAVE + a = b = c = d = 0; + cpuid(1, &a, &b, &cpu_info.feature2, &cpu_info.feature1); + + kprintf("CPU features: %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", has_sse() ? "SSE " : "", has_sse2() ? "SSE2 " : "", has_sse3() ? "SSE3 " : "", @@ -270,26 +275,26 @@ int cpu_detection(void) { has_x2apic() ? "X2APIC " : "", has_fpu() ? "FPU " : "", has_fxsr() ? "FXSR " : "", - has_xsave() ? "XSAVE " : ""); + has_xsave() ? "XSAVE " : "", + has_osxsave() ? "OSXSAVE " : ""); } - if (first_time && has_xsave()) { -#if 0 + if (first_time && has_osxsave()) { a = b = d = 0; c = 2; - cpuid(0, &a, &b, &c, &d); + cpuid(0xd, &a, &b, &c, &d); kprintf("Ext_Save_Area_2: offset %d, size %d\n", b, a); a = b = d = 0; c = 3; - cpuid(0, &a, &b, &c, &d); + cpuid(0xd, &a, &b, &c, &d); kprintf("Ext_Save_Area_3: offset %d, size %d\n", b, a); a = b = d = 0; c = 4; - cpuid(0, &a, &b, &c, &d); + cpuid(0xd, &a, &b, &c, &d); kprintf("Ext_Save_Area_4: offset %d, size %d\n", b, a); -#endif + save_fpu_state = save_fpu_state_xsave; restore_fpu_state = restore_fpu_state_xsave; fpu_init = fpu_init_xsave; @@ -315,8 +320,10 @@ int cpu_detection(void) { kprintf("Maximum input value for hypervisor: 0x%x\n", a); } - if (first_time) + if (first_time) { kprintf("CR0 0x%llx, CR4 0x%llx\n", read_cr0(), read_cr4()); + kprintf("size ofxsave_t: %d\n", sizeof(xsave_t)); + } return 0; } diff --git a/hermit/kernel/tasks.c b/hermit/kernel/tasks.c index 347458c32..2582713ca 100644 --- a/hermit/kernel/tasks.c +++ b/hermit/kernel/tasks.c @@ -111,12 +111,12 @@ void fpu_handler(struct state *s) spinlock_irqsave_lock(&readyqueues[core_id].lock); // did another already use the the FPU? => save FPU state if (readyqueues[core_id].fpu_owner) { - save_fpu_state(&task_table[readyqueues[core_id].fpu_owner].fpu); + save_fpu_state(&(task_table[readyqueues[core_id].fpu_owner].fpu)); readyqueues[core_id].fpu_owner = 0; } spinlock_irqsave_unlock(&readyqueues[core_id].lock); - if (!(task->flags & TASK_FPU_INIT)) { + if (BUILTIN_EXPECT(!(task->flags & TASK_FPU_INIT), 0)) { // use the FPU at the first time => Initialize FPU fpu_init(&task->fpu); task->flags |= TASK_FPU_INIT;