1 /* 2 * Copyright 2003-2022, 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 <commpage.h> 22 #include <kernel.h> 23 #include <thread.h> 24 #include <tls.h> 25 #include <vm/vm_types.h> 26 #include <vm/VMAddressSpace.h> 27 #include <arch_vm.h> 28 #include <arch/vm_translation_map.h> 29 30 #include <string.h> 31 32 #include "ARMPagingStructures.h" 33 #include "ARMVMTranslationMap.h" 34 35 //#define TRACE_ARCH_THREAD 36 #ifdef TRACE_ARCH_THREAD 37 # define TRACE(x) dprintf x 38 #else 39 # define TRACE(x) ; 40 #endif 41 42 // Valid initial arch_thread state. We just memcpy() it when initializing 43 // a new thread structure. 44 static struct arch_thread sInitialState; 45 46 Thread *gCurrentThread; 47 48 49 void 50 arm_push_iframe(struct iframe_stack *stack, struct iframe *frame) 51 { 52 ASSERT(stack->index < IFRAME_TRACE_DEPTH); 53 stack->frames[stack->index++] = frame; 54 } 55 56 57 void 58 arm_pop_iframe(struct iframe_stack *stack) 59 { 60 ASSERT(stack->index > 0); 61 stack->index--; 62 } 63 64 65 66 status_t 67 arch_thread_init(struct kernel_args *args) 68 { 69 // Initialize the static initial arch_thread state (sInitialState). 70 // Currently nothing to do, i.e. zero initialized is just fine. 71 72 return B_OK; 73 } 74 75 76 status_t 77 arch_team_init_team_struct(Team *team, bool kernel) 78 { 79 // Nothing to do. The structure is empty. 80 return B_OK; 81 } 82 83 84 status_t 85 arch_thread_init_thread_struct(Thread *thread) 86 { 87 // set up an initial state (stack & fpu) 88 memcpy(&thread->arch_info, &sInitialState, sizeof(struct arch_thread)); 89 90 return B_OK; 91 } 92 93 94 void 95 arch_thread_init_kthread_stack(Thread* thread, void* _stack, void* _stackTop, 96 void (*function)(void*), const void* data) 97 { 98 addr_t* stackTop = (addr_t*)_stackTop; 99 100 TRACE(("arch_thread_init_kthread_stack(%s): stack top %p, function %p, data: " 101 "%p\n", thread->name, stackTop, function, data)); 102 103 // push the function address -- that's the return address used after the 104 // context switch (lr/r14 register) 105 *--stackTop = (addr_t)function; 106 107 // simulate storing registers r1-r12 108 for (int i = 1; i <= 12; i++) 109 *--stackTop = 0; 110 111 // push the function argument as r0 112 *--stackTop = (addr_t)data; 113 114 // save the stack position 115 thread->arch_info.sp = stackTop; 116 } 117 118 119 status_t 120 arch_thread_init_tls(Thread *thread) 121 { 122 uint32 tls[TLS_FIRST_FREE_SLOT]; 123 124 thread->user_local_storage = thread->user_stack_base 125 + thread->user_stack_size; 126 127 // initialize default TLS fields 128 memset(tls, 0, sizeof(tls)); 129 tls[TLS_BASE_ADDRESS_SLOT] = thread->user_local_storage; 130 tls[TLS_THREAD_ID_SLOT] = thread->id; 131 tls[TLS_USER_THREAD_SLOT] = (addr_t)thread->user_thread; 132 133 return user_memcpy((void *)thread->user_local_storage, tls, sizeof(tls)); 134 } 135 136 extern "C" void arm_context_switch(void *from, void *to); 137 138 139 void 140 arm_swap_pgdir(uint32_t pageDirectoryAddress) 141 { 142 // Set translation table base 143 asm volatile("MCR p15, 0, %[addr], c2, c0, 0"::[addr] "r" (pageDirectoryAddress)); 144 isb(); 145 146 arch_cpu_global_TLB_invalidate(); 147 148 //TODO: update Context ID (incl. ASID) 149 //TODO: check if any additional TLB or Cache maintenance is needed 150 } 151 152 153 void 154 arm_set_tls_context(Thread *thread) 155 { 156 // Set TPIDRURO to point to TLS base 157 asm volatile("MCR p15, 0, %0, c13, c0, 3" 158 : : "r" (thread->user_local_storage)); 159 } 160 161 162 void 163 arch_thread_context_switch(Thread *from, Thread *to) 164 { 165 arm_set_tls_context(to); 166 167 VMAddressSpace *oldAddressSpace = from->team->address_space; 168 VMTranslationMap *oldTranslationMap = oldAddressSpace->TranslationMap(); 169 phys_addr_t oldPageDirectoryAddress = 170 ((ARMVMTranslationMap *)oldTranslationMap)->PagingStructures()->pgdir_phys; 171 172 VMAddressSpace *newAddressSpace = to->team->address_space; 173 VMTranslationMap *newTranslationMap = newAddressSpace->TranslationMap(); 174 phys_addr_t newPageDirectoryAddress = 175 ((ARMVMTranslationMap *)newTranslationMap)->PagingStructures()->pgdir_phys; 176 177 if (oldPageDirectoryAddress != newPageDirectoryAddress) { 178 TRACE(("arch_thread_context_switch: swap pgdir: " 179 "0x%08" B_PRIxPHYSADDR " -> 0x%08" B_PRIxPHYSADDR "\n", 180 oldPageDirectoryAddress, newPageDirectoryAddress)); 181 arm_swap_pgdir(newPageDirectoryAddress); 182 } 183 184 TRACE(("arch_thread_context_switch: %p(%s/%p) -> %p(%s/%p)\n", 185 from, from->name, from->arch_info.sp, to, to->name, to->arch_info.sp)); 186 arm_context_switch(&from->arch_info, &to->arch_info); 187 TRACE(("arch_thread_context_switch %p %p\n", to, from)); 188 } 189 190 191 void 192 arch_thread_dump_info(void *info) 193 { 194 struct arch_thread *at = (struct arch_thread *)info; 195 196 dprintf("\tsp: %p\n", at->sp); 197 } 198 199 200 status_t 201 arch_thread_enter_userspace(Thread *thread, addr_t entry, 202 void *args1, void *args2) 203 { 204 arm_set_tls_context(thread); 205 206 addr_t stackTop = thread->user_stack_base + thread->user_stack_size; 207 208 TRACE(("arch_thread_enter_userspace: entry 0x%" B_PRIxADDR ", args %p %p, " 209 "ustack_top 0x%" B_PRIxADDR "\n", entry, args1, args2, stackTop)); 210 211 //stackTop = arch_randomize_stack_pointer(stackTop - sizeof(args)); 212 213 // Copy the address of the stub that calls exit_thread() when the thread 214 // entry function returns to LR to act as the return address. 215 // The stub is inside commpage. 216 addr_t commPageAddress = (addr_t)thread->team->commpage_address; 217 218 disable_interrupts(); 219 220 // prepare the user iframe 221 iframe frame = {}; 222 frame.r0 = (uint32)args1; 223 frame.r1 = (uint32)args2; 224 frame.usr_sp = stackTop; 225 frame.usr_lr = ((addr_t*)commPageAddress)[COMMPAGE_ENTRY_ARM_THREAD_EXIT] 226 + commPageAddress; 227 frame.pc = entry; 228 229 // return to userland 230 arch_return_to_userland(&frame); 231 232 // normally we don't get here 233 return B_ERROR; 234 } 235 236 237 bool 238 arch_on_signal_stack(Thread *thread) 239 { 240 return false; 241 } 242 243 244 status_t 245 arch_setup_signal_frame(Thread *thread, struct sigaction *sa, 246 struct signal_frame_data *signalFrameData) 247 { 248 return B_ERROR; 249 } 250 251 252 int64 253 arch_restore_signal_frame(struct signal_frame_data* signalFrameData) 254 { 255 return 0; 256 } 257 258 259 void 260 arch_check_syscall_restart(Thread *thread) 261 { 262 } 263 264 265 /** Saves everything needed to restore the frame in the child fork in the 266 * arch_fork_arg structure to be passed to arch_restore_fork_frame(). 267 * Also makes sure to return the right value. 268 */ 269 void 270 arch_store_fork_frame(struct arch_fork_arg *arg) 271 { 272 } 273 274 275 /** Restores the frame from a forked team as specified by the provided 276 * arch_fork_arg structure. 277 * Needs to be called from within the child team, ie. instead of 278 * arch_thread_enter_uspace() as thread "starter". 279 * This function does not return to the caller, but will enter userland 280 * in the child team at the same position where the parent team left of. 281 */ 282 void 283 arch_restore_fork_frame(struct arch_fork_arg *arg) 284 { 285 } 286