diff --git a/headers/private/kernel/arch/x86/arch_cpu.h b/headers/private/kernel/arch/x86/arch_cpu.h index 281ae54a92..7d717d8b90 100644 --- a/headers/private/kernel/arch/x86/arch_cpu.h +++ b/headers/private/kernel/arch/x86/arch_cpu.h @@ -271,8 +271,8 @@ struct arch_thread; void __x86_setup_system_time(uint32 conversionFactor, uint32 conversionFactorNsecs, bool conversionFactorNsecsShift); -void i386_context_switch(struct arch_thread* oldState, - struct arch_thread* newState, uint32 newPageDir); +void x86_context_switch(struct arch_thread* oldState, + struct arch_thread* newState); void x86_userspace_thread_exit(void); void x86_end_userspace_thread_exit(void); void x86_enter_userspace(addr_t entry, addr_t stackTop); diff --git a/src/system/kernel/arch/x86/arch_thread.cpp b/src/system/kernel/arch/x86/arch_thread.cpp index 2628165ce6..65cec6dd4e 100644 --- a/src/system/kernel/arch/x86/arch_thread.cpp +++ b/src/system/kernel/arch/x86/arch_thread.cpp @@ -370,7 +370,6 @@ arch_thread_context_switch(struct thread *from, struct thread *to) = cpuData->arch.active_paging_structures; VMAddressSpace* toAddressSpace = to->team->address_space; - uint32 newPageDirectory; X86PagingStructures* toPagingStructures; if (toAddressSpace != NULL && (toPagingStructures = static_cast( @@ -382,23 +381,21 @@ arch_thread_context_switch(struct thread *from, struct thread *to) ~((uint32)1 << cpu)); atomic_or(&toPagingStructures->active_on_cpus, (uint32)1 << cpu); - activePagingStructures->RemoveReference(); -// TODO: This might cause deferred deletion, which on SMP machines could happen -// right now on another CPU! - // assign the new paging structures to the CPU toPagingStructures->AddReference(); cpuData->arch.active_paging_structures = toPagingStructures; - // get the new page directory - newPageDirectory = toPagingStructures->pgdir_phys; - } else { - newPageDirectory = 0; - // this means no change + // set the page directory, if it changes + uint32 newPageDirectory = toPagingStructures->pgdir_phys; + if (newPageDirectory != activePagingStructures->pgdir_phys) + x86_swap_pgdir(newPageDirectory); + + // This CPU no longer uses the previous paging structures. + activePagingStructures->RemoveReference(); } gX86SwapFPUFunc(from->arch_info.fpu_state, to->arch_info.fpu_state); - i386_context_switch(&from->arch_info, &to->arch_info, newPageDirectory); + x86_context_switch(&from->arch_info, &to->arch_info); } diff --git a/src/system/kernel/arch/x86/arch_x86.S b/src/system/kernel/arch/x86/arch_x86.S index 5b1516768a..056f170300 100644 --- a/src/system/kernel/arch/x86/arch_x86.S +++ b/src/system/kernel/arch/x86/arch_x86.S @@ -121,25 +121,20 @@ FUNCTION(x86_write_msr): ret FUNCTION_END(x86_write_msr) -/* void i386_context_switch(struct arch_thread *old_state, - struct arch_thread *new_state, uint32 new_pgdir); */ -FUNCTION(i386_context_switch): +/* void x86_context_switch(struct arch_thread* oldState, + struct arch_thread* newState); */ +FUNCTION(x86_context_switch): pusha /* pushes 8 words onto the stack */ - movl 36(%esp),%eax /* save old_state->current_stack */ + movl 36(%esp),%eax /* save oldState->current_stack */ movl %esp,(%eax) pushl %ss popl %edx movl %edx,4(%eax) - movl 44(%esp),%eax /* get possible new pgdir */ - orl %eax,%eax /* is it null? */ - je skip_pgdir_swap - movl %eax,%cr3 -skip_pgdir_swap: - movl 40(%esp),%eax /* get new new_state->current_stack */ + movl 40(%esp),%eax /* get new newState->current_stack */ lss (%eax),%esp popa ret -FUNCTION_END(i386_context_switch) +FUNCTION_END(x86_context_switch) /* void x86_swap_pgdir(uint32 newPageDir); */ FUNCTION(x86_swap_pgdir):