1 /* 2 * Copyright 2003-2010, Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Axel Dörfler <axeld@pinc-software.de> 7 * Ingo Weinhold <bonefish@cs.tu-berlin.de> 8 * François Revol <revol@free.fr> 9 * 10 * Copyright 2001, Travis Geiselbrecht. All rights reserved. 11 * Distributed under the terms of the NewOS License. 12 */ 13 14 15 #include <thread.h> 16 #include <arch_thread.h> 17 18 #include <arch_cpu.h> 19 #include <arch/thread.h> 20 #include <boot/stage2.h> 21 #include <kernel.h> 22 #include <thread.h> 23 #include <tls.h> 24 #include <vm/vm_types.h> 25 #include <vm/VMAddressSpace.h> 26 #include <arch_vm.h> 27 #include <arch/vm_translation_map.h> 28 29 #include <string.h> 30 31 //#define TRACE_ARCH_THREAD 32 #ifdef TRACE_ARCH_THREAD 33 # define TRACE(x) dprintf x 34 #else 35 # define TRACE(x) ; 36 #endif 37 38 // Valid initial arch_thread state. We just memcpy() it when initializing 39 // a new thread structure. 40 static struct arch_thread sInitialState; 41 42 Thread *gCurrentThread; 43 44 45 status_t 46 arch_thread_init(struct kernel_args *args) 47 { 48 // Initialize the static initial arch_thread state (sInitialState). 49 // Currently nothing to do, i.e. zero initialized is just fine. 50 51 return B_OK; 52 } 53 54 55 status_t 56 arch_team_init_team_struct(Team *team, bool kernel) 57 { 58 // Nothing to do. The structure is empty. 59 return B_OK; 60 } 61 62 63 status_t 64 arch_thread_init_thread_struct(Thread *thread) 65 { 66 // set up an initial state (stack & fpu) 67 memcpy(&thread->arch_info, &sInitialState, sizeof(struct arch_thread)); 68 69 return B_OK; 70 } 71 72 73 void 74 arch_thread_init_kthread_stack(Thread* thread, void* _stack, void* _stackTop, 75 void (*function)(void*), const void* data) 76 { 77 addr_t* stackTop = (addr_t*)_stackTop; 78 79 TRACE(("arch_thread_init_kthread_stack(%s): stack top %p, function %p, data: " 80 "%p\n", thread->name, stackTop, function, data)); 81 82 // push the function address -- that's the return address used after the 83 // context switch (lr/r14 register) 84 *--stackTop = (addr_t)function; 85 86 // simulate storing registers r1-r12 87 for (int i = 1; i <= 12; i++) 88 *--stackTop = 0; 89 90 // push the function argument as r0 91 *--stackTop = (addr_t)data; 92 93 // save the stack position 94 thread->arch_info.sp = stackTop; 95 } 96 97 98 status_t 99 arch_thread_init_tls(Thread *thread) 100 { 101 uint32 tls[TLS_USER_THREAD_SLOT + 1]; 102 103 thread->user_local_storage = thread->user_stack_base 104 + thread->user_stack_size; 105 106 // initialize default TLS fields 107 memset(tls, 0, sizeof(tls)); 108 tls[TLS_BASE_ADDRESS_SLOT] = thread->user_local_storage; 109 tls[TLS_THREAD_ID_SLOT] = thread->id; 110 tls[TLS_USER_THREAD_SLOT] = (addr_t)thread->user_thread; 111 112 return user_memcpy((void *)thread->user_local_storage, tls, sizeof(tls)); 113 } 114 115 extern "C" void arm_context_switch(void *from, void *to); 116 117 void 118 arch_thread_context_switch(Thread *from, Thread *to) 119 { 120 TRACE(("arch_thread_context_switch: %p(%s/%p) -> %p(%s/%p)\n", 121 from, from->name, from->arch_info.sp, to, to->name, to->arch_info.sp)); 122 arm_context_switch(&from->arch_info, &to->arch_info); 123 TRACE(("arch_thread_context_switch %p %p\n", to, from)); 124 } 125 126 127 void 128 arch_thread_dump_info(void *info) 129 { 130 struct arch_thread *at = (struct arch_thread *)info; 131 132 dprintf("\tsp: %p\n", at->sp); 133 } 134 135 136 status_t 137 arch_thread_enter_userspace(Thread *thread, addr_t entry, 138 void *arg1, void *arg2) 139 { 140 panic("arch_thread_enter_uspace(): not yet implemented\n"); 141 return B_ERROR; 142 } 143 144 145 bool 146 arch_on_signal_stack(Thread *thread) 147 { 148 return false; 149 } 150 151 152 status_t 153 arch_setup_signal_frame(Thread *thread, struct sigaction *sa, 154 struct signal_frame_data *signalFrameData) 155 { 156 return B_ERROR; 157 } 158 159 160 int64 161 arch_restore_signal_frame(struct signal_frame_data* signalFrameData) 162 { 163 return 0; 164 } 165 166 167 void 168 arch_check_syscall_restart(Thread *thread) 169 { 170 } 171 172 173 /** Saves everything needed to restore the frame in the child fork in the 174 * arch_fork_arg structure to be passed to arch_restore_fork_frame(). 175 * Also makes sure to return the right value. 176 */ 177 void 178 arch_store_fork_frame(struct arch_fork_arg *arg) 179 { 180 } 181 182 183 /** Restores the frame from a forked team as specified by the provided 184 * arch_fork_arg structure. 185 * Needs to be called from within the child team, ie. instead of 186 * arch_thread_enter_uspace() as thread "starter". 187 * This function does not return to the caller, but will enter userland 188 * in the child team at the same position where the parent team left of. 189 */ 190 void 191 arch_restore_fork_frame(struct arch_fork_arg *arg) 192 { 193 } 194