From c32a30726af9dc2f0f5d1ba761f40fc326d5e6b0 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Tue, 17 Jul 2012 12:44:18 -0700 Subject: [PATCH] some code cleanups --- Makefile.example | 2 +- Makefile.scc | 2 +- arch/x86/include/asm/processor.h | 5 ---- arch/x86/kernel/entry32.asm | 10 ++++--- arch/x86/kernel/gdt.c | 47 +++++++++++++++++--------------- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/Makefile.example b/Makefile.example index cf89b10e..855559cc 100644 --- a/Makefile.example +++ b/Makefile.example @@ -44,7 +44,7 @@ INCLUDE = -I$(TOPDIR)/include -I$(TOPDIR)/arch/$(ARCH)/include -I$(TOPDIR)/lwip/ # Compiler options for final code CFLAGS = -g -m32 -march=i586 -Wall -O2 -fstrength-reduce -fomit-frame-pointer -finline-functions -ffreestanding $(INCLUDE) $(STACKPROT) # Compiler options for debuging -#CFLAGS = -g -O -m32 -march=i586 -Wall -DWITH_FRAME_POINTER -ffreestanding $(INCLUDE) $(STACKPROT) +#CFLAGS = -g -O -m32 -march=i586 -Wall -fomit-frame-pointer -ffreestanding $(INCLUDE) $(STACKPROT) ARFLAGS = rsv LDFLAGS = -T link$(BIT).ld -z max-page-size=4096 --defsym __BUILD_DATE=$(shell date +'%Y%m%d') --defsym __BUILD_TIME=$(shell date +'%H%M%S') STRIP_DEBUG = --strip-debug diff --git a/Makefile.scc b/Makefile.scc index fda39af2..7e466274 100644 --- a/Makefile.scc +++ b/Makefile.scc @@ -44,7 +44,7 @@ INCLUDE = -I$(TOPDIR)/include -I$(TOPDIR)/arch/$(ARCH)/include -I$(TOPDIR)/lwip/ # Compiler options for final code CFLAGS = -g -m32 -march=i586 -Wall -O2 -fstrength-reduce -fomit-frame-pointer -finline-functions -ffreestanding $(INCLUDE) $(STACKPROT) # Compiler options for debuging -#CFLAGS = -g -O -m32 -march=i586 -Wall -DWITH_FRAME_POINTER -ffreestanding $(INCLUDE) $(STACKPROT) +#CFLAGS = -g -O -m32 -march=i586 -Wall -fomit-frame-pointer -ffreestanding $(INCLUDE) $(STACKPROT) ARFLAGS = rsv LDFLAGS = -T link$(BIT).ld -z max-page-size=4096 --defsym __BUILD_DATE=$(shell date +'%Y%m%d') --defsym __BUILD_TIME=$(shell date +'%H%M%S') STRIP_DEBUG = --strip-debug diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index 661a58f8..dcc0d698 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -308,11 +308,6 @@ static inline size_t lsb(size_t i) return ret; } -/** @brief Read extended instruction pointer - * @return The EIP's value - */ -uint32_t read_eip(void); - /// A one-instruction-do-nothing #define NOP1 asm volatile ("nop") /// Do nothing for 2 instructions diff --git a/arch/x86/kernel/entry32.asm b/arch/x86/kernel/entry32.asm index c5096863..84542d87 100644 --- a/arch/x86/kernel/entry32.asm +++ b/arch/x86/kernel/entry32.asm @@ -109,10 +109,12 @@ flush2: ret ; determines the current instruction pointer (after the jmp) -global read_eip -read_eip: - pop eax ; Get the return address - jmp eax ; Return. Can't use RET because return +global read_ip +read_ip: + mov eax, [esp+4] + pop DWORD [eax] ; Get the return address + add esp, 4 ; Dirty Hack! read_ip cleanup the stacl + jmp [eax] ; Return. Can't use RET because return ; address popped off the stack. ; In just a few pages in this tutorial, we will add our Interrupt diff --git a/arch/x86/kernel/gdt.c b/arch/x86/kernel/gdt.c index 384d9cb8..c31797bb 100644 --- a/arch/x86/kernel/gdt.c +++ b/arch/x86/kernel/gdt.c @@ -17,6 +17,7 @@ * This file is part of MetalSVM. */ +#include #include #include #include @@ -58,51 +59,53 @@ size_t get_stack(uint32_t id) int arch_fork(task_t* task) { uint16_t cs = 0x08; - uint32_t id, esp; + uint32_t id; struct state* state; task_t* curr_task = per_core(current_task); + size_t esp, state_size; if (BUILTIN_EXPECT(!task, 0)) return -EINVAL; id = task->id; +#ifdef CONFIG_X86_32 + state_size = sizeof(struct state) - 2*sizeof(size_t); +#else + state_size = sizeof(struct state); +#endif + // copy kernel stack of the current task mb(); memcpy(kstacks[id], kstacks[curr_task->id], KERNEL_STACK_SIZE); - mb(); #ifdef CONFIG_X86_32 - asm volatile ("mov %%esp, %0" : "=r"(esp)); - esp -= (uint32_t) kstacks[curr_task->id]; - esp += (uint32_t) kstacks[id]; + asm volatile ("mov %%esp, %0" : "=m"(esp)); + esp -= (size_t) kstacks[curr_task->id]; + esp += (size_t) kstacks[id]; - state = (struct state*) (esp - sizeof(struct state) + 2*sizeof(size_t)); - memset(state, 0x00, sizeof(struct state) - 2*sizeof(size_t)); + state = (struct state*) (esp - state_size); + memset(state, 0x00, state_size); - asm volatile ("pusha; pop %0" : "=r"(state->edi)); - asm volatile ("pop %0" : "=r"(state->esi)); - asm volatile ("pop %0" : "=r"(state->ebp)); + asm volatile ("pusha; pop %0" : "=m"(state->edi)); + asm volatile ("pop %0" : "=m"(state->esi)); + asm volatile ("pop %0" : "=m"(state->ebp)); asm volatile ("add $4, %%esp" ::: "%esp"); - asm volatile ("pop %0" : "=r"(state->ebx)); - asm volatile ("pop %0" : "=r"(state->edx)); - asm volatile ("pop %0" : "=r"(state->ecx)); - asm volatile ("pop %0" : "=r"(state->eax)); + asm volatile ("pop %0" : "=m"(state->ebx)); + asm volatile ("pop %0" : "=m"(state->edx)); + asm volatile ("pop %0" : "=m"(state->ecx)); + asm volatile ("pop %0" : "=m"(state->eax)); -#ifdef WITH_FRAME_POINTER - state->ebp -= (uint32_t) kstacks[curr_task->id]; - state->ebp += (uint32_t) kstacks[id]; -#endif - state->esp = (uint32_t) state; + state->esp = esp; task->stack = (size_t*) state; state->int_no = 0xB16B00B5; state->error = 0xC03DB4B3; state->cs = cs; // store the current EFLAGS - asm volatile ("pushf; pop %%eax" : "=a"(state->eflags)); + asm volatile ("pushf; pop %0" : "=m"(state->eflags)); // enable interrupts state->eflags |= (1 << 9); - // This will be the entry point for the new task. - asm volatile ("call read_eip" : "=a"(state->eip)); + // This will be the entry point for the new task. read_ip cleanups the stack + asm volatile ("push %0; call read_ip" :: "r"(&state->eip) : "%eax"); #else #warning Currently, not supported! return -1;