1 /* 2 * Copyright 2003-2023, 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 <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 #include "ARMPagingStructures.h" 32 #include "ARMVMTranslationMap.h" 33 34 //#define TRACE_ARCH_THREAD 35 #ifdef TRACE_ARCH_THREAD 36 # define TRACE(x...) dprintf(x) 37 #else 38 # define TRACE(x...) ; 39 #endif 40 41 // Valid initial arch_thread state. We just memcpy() it when initializing 42 // a new thread structure. 43 static struct arch_thread sInitialState; 44 45 46 void 47 arm_push_iframe(struct iframe_stack *stack, struct iframe *frame) 48 { 49 ASSERT(stack->index < IFRAME_TRACE_DEPTH); 50 stack->frames[stack->index++] = frame; 51 } 52 53 54 void 55 arm_pop_iframe(struct iframe_stack *stack) 56 { 57 ASSERT(stack->index > 0); 58 stack->index--; 59 } 60 61 62 63 status_t 64 arch_thread_init(struct kernel_args *args) 65 { 66 // Initialize the static initial arch_thread state (sInitialState). 67 // Currently nothing to do, i.e. zero initialized is just fine. 68 69 return B_OK; 70 } 71 72 73 status_t 74 arch_team_init_team_struct(Team *team, bool kernel) 75 { 76 // Nothing to do. The structure is empty. 77 return B_OK; 78 } 79 80 81 status_t 82 arch_thread_init_thread_struct(Thread *thread) 83 { 84 // set up an initial state (stack & fpu) 85 memcpy(&thread->arch_info, &sInitialState, sizeof(struct arch_thread)); 86 87 return B_OK; 88 } 89 90 91 void 92 arch_thread_init_kthread_stack(Thread* thread, void* _stack, void* _stackTop, 93 void (*function)(void*), const void* data) 94 { 95 addr_t* stackTop = (addr_t*)_stackTop; 96 97 TRACE("arch_thread_init_kthread_stack(%s): stack top %p, function %p, data: " 98 "%p\n", thread->name, stackTop, function, data); 99 100 // push the function address -- that's the return address used after the 101 // context switch (lr/r14 register) 102 *--stackTop = (addr_t)function; 103 104 // simulate storing registers r1-r12 105 for (int i = 1; i <= 12; i++) 106 *--stackTop = 0; 107 108 // push the function argument as r0 109 *--stackTop = (addr_t)data; 110 111 // save the stack position 112 thread->arch_info.sp = stackTop; 113 } 114 115 116 status_t 117 arch_thread_init_tls(Thread *thread) 118 { 119 thread->user_local_storage = 120 thread->user_stack_base + thread->user_stack_size; 121 return B_OK; 122 } 123 124 125 void 126 arm_swap_pgdir(uint32_t pageDirectoryAddress) 127 { 128 arm_set_ttbr0(pageDirectoryAddress); 129 isb(); 130 131 arch_cpu_global_TLB_invalidate(); 132 133 //TODO: update Context ID (incl. ASID) 134 //TODO: check if any additional TLB or Cache maintenance is needed 135 } 136 137 138 void 139 arch_thread_context_switch(Thread *from, Thread *to) 140 { 141 arm_set_tpidruro(to->user_local_storage); 142 143 VMAddressSpace *oldAddressSpace = from->team->address_space; 144 VMTranslationMap *oldTranslationMap = oldAddressSpace->TranslationMap(); 145 phys_addr_t oldPageDirectoryAddress = 146 ((ARMVMTranslationMap *)oldTranslationMap)->PagingStructures()->pgdir_phys; 147 148 VMAddressSpace *newAddressSpace = to->team->address_space; 149 VMTranslationMap *newTranslationMap = newAddressSpace->TranslationMap(); 150 phys_addr_t newPageDirectoryAddress = 151 ((ARMVMTranslationMap *)newTranslationMap)->PagingStructures()->pgdir_phys; 152 153 if (oldPageDirectoryAddress != newPageDirectoryAddress) { 154 TRACE("arch_thread_context_switch: swap pgdir: " 155 "0x%08" B_PRIxPHYSADDR " -> 0x%08" B_PRIxPHYSADDR "\n", 156 oldPageDirectoryAddress, newPageDirectoryAddress); 157 arm_swap_pgdir(newPageDirectoryAddress); 158 } 159 160 TRACE("arch_thread_context_switch: %p(%s/%p) -> %p(%s/%p)\n", 161 from, from->name, from->arch_info.sp, to, to->name, to->arch_info.sp); 162 arm_save_fpu(&from->arch_info.fpuContext); 163 arm_restore_fpu(&to->arch_info.fpuContext); 164 arm_context_switch(&from->arch_info, &to->arch_info); 165 TRACE("arch_thread_context_switch %p %p\n", to, from); 166 } 167 168 169 void 170 arch_thread_dump_info(void *info) 171 { 172 struct arch_thread *at = (struct arch_thread *)info; 173 174 dprintf("\tsp: %p\n", at->sp); 175 } 176 177 178 status_t 179 arch_thread_enter_userspace(Thread *thread, addr_t entry, 180 void *args1, void *args2) 181 { 182 arm_set_tpidruro(thread->user_local_storage); 183 184 addr_t stackTop = thread->user_stack_base + thread->user_stack_size; 185 186 TRACE("arch_thread_enter_userspace: entry 0x%" B_PRIxADDR ", args %p %p, " 187 "ustack_top 0x%" B_PRIxADDR "\n", entry, args1, args2, stackTop); 188 189 //stackTop = arch_randomize_stack_pointer(stackTop - sizeof(args)); 190 191 // Copy the address of the stub that calls exit_thread() when the thread 192 // entry function returns to LR to act as the return address. 193 // The stub is inside commpage. 194 addr_t commPageAddress = (addr_t)thread->team->commpage_address; 195 196 disable_interrupts(); 197 198 // prepare the user iframe 199 iframe frame = {}; 200 frame.r0 = (uint32)args1; 201 frame.r1 = (uint32)args2; 202 frame.usr_sp = stackTop; 203 frame.usr_lr = ((addr_t*)commPageAddress)[COMMPAGE_ENTRY_ARM_THREAD_EXIT] 204 + commPageAddress; 205 frame.pc = entry; 206 207 // return to userland 208 arch_return_to_userland(&frame); 209 210 // normally we don't get here 211 return B_ERROR; 212 } 213 214 215 bool 216 arch_on_signal_stack(Thread *thread) 217 { 218 struct iframe* frame = thread->arch_info.userFrame; 219 if (frame == NULL) { 220 panic("arch_on_signal_stack(): No user iframe!"); 221 return false; 222 } 223 224 return frame->usr_sp >= thread->signal_stack_base 225 && frame->usr_sp < thread->signal_stack_base 226 + thread->signal_stack_size; 227 } 228 229 230 static uint8* 231 get_signal_stack(Thread* thread, struct iframe* frame, 232 struct sigaction* action, size_t spaceNeeded) 233 { 234 // use the alternate signal stack if we should and can 235 if (thread->signal_stack_enabled && (action->sa_flags & SA_ONSTACK) != 0 236 && (frame->usr_sp < thread->signal_stack_base 237 || frame->usr_sp >= thread->signal_stack_base + thread->signal_stack_size)) { 238 addr_t stackTop = thread->signal_stack_base + thread->signal_stack_size; 239 return (uint8*)ROUNDDOWN(stackTop - spaceNeeded, 16); 240 } 241 242 return (uint8*)ROUNDDOWN(frame->usr_sp - spaceNeeded, 16); 243 } 244 245 246 status_t 247 arch_setup_signal_frame(Thread *thread, struct sigaction *sa, 248 struct signal_frame_data *signalFrameData) 249 { 250 iframe* frame = thread->arch_info.userFrame; 251 if (frame == NULL) { 252 panic("arch_setup_signal_frame(): No user iframe!"); 253 return B_ERROR; 254 } 255 256 // store the register state in signalFrameData->context.uc_mcontext 257 signalFrameData->context.uc_mcontext.r0 = frame->r0; 258 signalFrameData->context.uc_mcontext.r1 = frame->r1; 259 signalFrameData->context.uc_mcontext.r2 = frame->r2; 260 signalFrameData->context.uc_mcontext.r3 = frame->r3; 261 signalFrameData->context.uc_mcontext.r4 = frame->r4; 262 signalFrameData->context.uc_mcontext.r5 = frame->r5; 263 signalFrameData->context.uc_mcontext.r6 = frame->r6; 264 signalFrameData->context.uc_mcontext.r7 = frame->r7; 265 signalFrameData->context.uc_mcontext.r8 = frame->r8; 266 signalFrameData->context.uc_mcontext.r9 = frame->r9; 267 signalFrameData->context.uc_mcontext.r10 = frame->r10; 268 signalFrameData->context.uc_mcontext.r11 = frame->r11; 269 signalFrameData->context.uc_mcontext.r12 = frame->r12; 270 signalFrameData->context.uc_mcontext.r13 = frame->usr_sp; 271 signalFrameData->context.uc_mcontext.r14 = frame->usr_lr; 272 signalFrameData->context.uc_mcontext.r15 = frame->pc; 273 signalFrameData->context.uc_mcontext.cpsr = frame->spsr; 274 275 arm_save_fpu((arch_fpu_context*)&signalFrameData->context.uc_mcontext.d[0]); 276 277 // Fill in signalFrameData->context.uc_stack 278 signal_get_user_stack(frame->usr_sp, &signalFrameData->context.uc_stack); 279 280 // store oldR0 in syscall_restart_return_value 281 signalFrameData->syscall_restart_return_value = thread->arch_info.oldR0; 282 283 // get the stack to use -- that's either the current one or a special signal stack 284 uint8* userStack = get_signal_stack(thread, frame, sa, 285 sizeof(*signalFrameData)); 286 287 // copy the signal frame data onto the stack 288 status_t res = user_memcpy(userStack, signalFrameData, 289 sizeof(*signalFrameData)); 290 if (res < B_OK) 291 return res; 292 293 // prepare the user stack frame for a function call to the signal handler wrapper function 294 addr_t commpageAddr = (addr_t)thread->team->commpage_address; 295 addr_t signalHandlerAddr; 296 ASSERT(user_memcpy(&signalHandlerAddr, 297 &((addr_t*)commpageAddr)[COMMPAGE_ENTRY_ARM_SIGNAL_HANDLER], 298 sizeof(signalHandlerAddr)) >= B_OK); 299 signalHandlerAddr += commpageAddr; 300 301 frame->usr_lr = frame->pc; 302 frame->usr_sp = (addr_t)userStack; 303 frame->pc = signalHandlerAddr; 304 frame->r0 = frame->usr_sp; 305 306 return B_OK; 307 } 308 309 310 int64 311 arch_restore_signal_frame(struct signal_frame_data* signalFrameData) 312 { 313 iframe* frame = thread_get_current_thread()->arch_info.userFrame; 314 if (frame == NULL) { 315 panic("arch_restore_signal_frame(): No user iframe!"); 316 return 0; 317 } 318 319 thread_get_current_thread()->arch_info.oldR0 320 = signalFrameData->syscall_restart_return_value; 321 322 frame->r0 = signalFrameData->context.uc_mcontext.r0; 323 frame->r1 = signalFrameData->context.uc_mcontext.r1; 324 frame->r2 = signalFrameData->context.uc_mcontext.r2; 325 frame->r3 = signalFrameData->context.uc_mcontext.r3; 326 frame->r4 = signalFrameData->context.uc_mcontext.r4; 327 frame->r5 = signalFrameData->context.uc_mcontext.r5; 328 frame->r6 = signalFrameData->context.uc_mcontext.r6; 329 frame->r7 = signalFrameData->context.uc_mcontext.r7; 330 frame->r8 = signalFrameData->context.uc_mcontext.r8; 331 frame->r9 = signalFrameData->context.uc_mcontext.r9; 332 frame->r10 = signalFrameData->context.uc_mcontext.r10; 333 frame->r11 = signalFrameData->context.uc_mcontext.r11; 334 frame->r12 = signalFrameData->context.uc_mcontext.r12; 335 frame->usr_sp = signalFrameData->context.uc_mcontext.r13; 336 frame->usr_lr = signalFrameData->context.uc_mcontext.r14; 337 frame->pc = signalFrameData->context.uc_mcontext.r15; 338 frame->spsr = signalFrameData->context.uc_mcontext.cpsr; 339 340 arm_restore_fpu((arch_fpu_context*)&signalFrameData->context.uc_mcontext.d[0]); 341 342 return frame->r0; 343 } 344 345 346 /** Saves everything needed to restore the frame in the child fork in the 347 * arch_fork_arg structure to be passed to arch_restore_fork_frame(). 348 * Also makes sure to return the right value. 349 */ 350 void 351 arch_store_fork_frame(struct arch_fork_arg *arg) 352 { 353 struct iframe* frame = thread_get_current_thread()->arch_info.userFrame; 354 if (frame == NULL) { 355 panic("arch_store_fork_frame(): No user iframe!"); 356 } 357 358 arg->frame = *frame; 359 arg->frame.r0 = 0; // fork return value 360 } 361 362 363 /** Restores the frame from a forked team as specified by the provided 364 * arch_fork_arg structure. 365 * Needs to be called from within the child team, ie. instead of 366 * arch_thread_enter_uspace() as thread "starter". 367 * This function does not return to the caller, but will enter userland 368 * in the child team at the same position where the parent team left of. 369 */ 370 void 371 arch_restore_fork_frame(struct arch_fork_arg *arg) 372 { 373 disable_interrupts(); 374 arch_return_to_userland(&arg->frame); 375 } 376