diff --git a/src/system/kernel/arch/arm/arch_thread.cpp b/src/system/kernel/arch/arm/arch_thread.cpp index 3d3d38cecf..2ae92ae4cc 100644 --- a/src/system/kernel/arch/arm/arch_thread.cpp +++ b/src/system/kernel/arch/arm/arch_thread.cpp @@ -20,13 +20,20 @@ #include #include #include +#include #include #include #include -//#include +#include #include +#define TRACE_ARCH_THREAD +#ifdef TRACE_ARCH_THREAD +# define TRACE(x) dprintf x +#else +# define TRACE(x) ; +#endif // Valid initial arch_thread state. We just memcpy() it when initializing // a new thread structure. @@ -67,35 +74,53 @@ void arch_thread_init_kthread_stack(Thread* thread, void* _stack, void* _stackTop, void (*function)(void*), const void* data) { -#warning ARM:WRITEME + addr_t* stackTop = (addr_t*)_stackTop; + + TRACE(("arch_thread_init_kthread_stack(%s): stack top %p, function %p, data: " + "%p\n", thread->name, stackTop, function, data)); + + // push the function address -- that's the return address used after the + // context switch (lr/r14 register) + *--stackTop = (addr_t)function; + + // simulate storing registers r1-r12 + for (int i = 1; i <= 12; i++) + *--stackTop = 0; + + // push the function argument as r0 + *--stackTop = (addr_t)data; + + // save the stack position + thread->arch_info.sp = stackTop; } status_t arch_thread_init_tls(Thread *thread) { - // TODO: Implement! - return B_OK; + uint32 tls[TLS_USER_THREAD_SLOT + 1]; + + thread->user_local_storage = thread->user_stack_base + + thread->user_stack_size; + + // initialize default TLS fields + memset(tls, 0, sizeof(tls)); + tls[TLS_BASE_ADDRESS_SLOT] = thread->user_local_storage; + tls[TLS_THREAD_ID_SLOT] = thread->id; + tls[TLS_USER_THREAD_SLOT] = (addr_t)thread->user_thread; + + return user_memcpy((void *)thread->user_local_storage, tls, sizeof(tls)); } +extern "C" void arm_context_switch(void *from, void *to); void arch_thread_context_switch(Thread *from, Thread *to) { - #if 0 - addr_t newPageDirectory; - - newPageDirectory = (addr_t)m68k_next_page_directory(from, to); - - if ((newPageDirectory % B_PAGE_SIZE) != 0) - panic("arch_thread_context_switch: bad pgdir 0x%lx\n", - newPageDirectory); - #warning M68K: export from arch_vm.c - m68k_set_pgdir(newPageDirectory); - m68k_context_switch(&from->arch_info.sp, to->arch_info.sp); - #endif - - #warning ARM:WRITEME + TRACE(("arch_thread_context_switch: %p(%s/%p) -> %p(%s/%p)\n", + from, from->name, from->arch_info.sp, to, to->name, to->arch_info.sp)); + arm_context_switch(&from->arch_info, &to->arch_info); + TRACE(("arch_thread_context_switch %p %p\n", to, from)); }