1 /* Copyright 2019, Adrien Destugues, pulkomandy@pulkomandy.tk 2 * Distributed under the terms of the MIT License. 3 */ 4 5 6 #include <string.h> 7 8 #include <arch_cpu.h> 9 #include <arch_debug.h> 10 #include <arch/thread.h> 11 #include <boot/stage2.h> 12 #include <commpage.h> 13 #include <kernel.h> 14 #include <thread.h> 15 #include <team.h> 16 #include <vm/vm_types.h> 17 #include <vm/VMAddressSpace.h> 18 19 #include "RISCV64VMTranslationMap.h" 20 21 22 extern "C" void SVecU(); 23 24 25 status_t 26 arch_thread_init(struct kernel_args *args) 27 { 28 // Initialize the static initial arch_thread state (sInitialState). 29 // Currently nothing to do, i.e. zero initialized is just fine. 30 31 return B_OK; 32 } 33 34 35 status_t 36 arch_team_init_team_struct(Team *team, bool kernel) 37 { 38 // Nothing to do. The structure is empty. 39 return B_OK; 40 } 41 42 43 status_t 44 arch_thread_init_thread_struct(Thread *thread) 45 { 46 return B_OK; 47 } 48 49 50 static inline VMAddressSpace* 51 GetThreadAddressSpace(Thread* thread) 52 { 53 /* 54 if (thread->team == team_get_kernel_team()) 55 return VMAddressSpace::Kernel(); 56 */ 57 return thread->team->address_space; 58 } 59 60 61 void 62 arch_thread_init_kthread_stack(Thread* thread, void* _stack, void* _stackTop, 63 void (*function)(void*), const void* data) 64 { 65 memset(&thread->arch_info.context, 0, sizeof(arch_context)); 66 thread->arch_info.context.sp = (addr_t)_stackTop; 67 thread->arch_info.context.s[0] = 0; // fp 68 thread->arch_info.context.s[1] = (addr_t)function; 69 thread->arch_info.context.s[2] = (addr_t)data; 70 thread->arch_info.context.ra = (addr_t)arch_thread_entry; 71 RISCV64VMTranslationMap* map = (RISCV64VMTranslationMap*) 72 thread->team->address_space->TranslationMap(); 73 thread->arch_info.context.satp = map->Satp(); 74 75 memset(&thread->arch_info.fpuContext, 0, sizeof(fpu_context)); 76 } 77 78 79 status_t 80 arch_thread_init_tls(Thread *thread) 81 { 82 thread->user_local_storage = 83 thread->user_stack_base + thread->user_stack_size; 84 return B_OK; 85 } 86 87 88 void 89 arch_thread_context_switch(Thread *from, Thread *to) 90 { 91 /* 92 dprintf("arch_thread_context_switch(%p(%s), %p(%s))\n", from, from->name, 93 to, to->name); 94 */ 95 96 RISCV64VMTranslationMap* fromMap = (RISCV64VMTranslationMap*)from->team 97 ->address_space->TranslationMap(); 98 99 RISCV64VMTranslationMap* toMap = (RISCV64VMTranslationMap*)to->team 100 ->address_space->TranslationMap(); 101 102 int cpu = to->cpu->cpu_num; 103 toMap->ActiveOnCpus().SetBitAtomic(cpu); 104 fromMap->ActiveOnCpus().ClearBitAtomic(cpu); 105 106 // TODO: save/restore FPU only if needed 107 save_fpu(&from->arch_info.fpuContext); 108 restore_fpu(&to->arch_info.fpuContext); 109 110 arch_context_switch(&from->arch_info.context, &to->arch_info.context); 111 } 112 113 114 void 115 arch_thread_dump_info(void *info) 116 { 117 } 118 119 120 status_t 121 arch_thread_enter_userspace(Thread *thread, addr_t entry, void *arg1, 122 void *arg2) 123 { 124 //dprintf("arch_thread_enter_uspace(%" B_PRId32 "(%s))\n", thread->id, thread->name); 125 126 addr_t commpageAdr = (addr_t)thread->team->commpage_address; 127 addr_t threadExitAddr; 128 ASSERT(user_memcpy(&threadExitAddr, 129 &((addr_t*)commpageAdr)[COMMPAGE_ENTRY_RISCV64_THREAD_EXIT], 130 sizeof(threadExitAddr)) >= B_OK); 131 threadExitAddr += commpageAdr; 132 133 disable_interrupts(); 134 135 arch_stack* stackHeader = (arch_stack*)thread->kernel_stack_top - 1; 136 stackHeader->thread = thread; 137 138 iframe frame; 139 memset(&frame, 0, sizeof(frame)); 140 141 SstatusReg status{.val = Sstatus()}; 142 status.pie = (1 << modeS); // enable interrupts when enter userspace 143 status.spp = modeU; 144 145 frame.status = status.val; 146 frame.epc = entry; 147 frame.a0 = (addr_t)arg1; 148 frame.a1 = (addr_t)arg2; 149 frame.ra = threadExitAddr; 150 frame.sp = thread->user_stack_base + thread->user_stack_size; 151 frame.tp = thread->user_local_storage; 152 153 arch_load_user_iframe(stackHeader, &frame); 154 155 // never return 156 return B_ERROR; 157 } 158 159 160 bool 161 arch_on_signal_stack(Thread *thread) 162 { 163 struct iframe* frame = thread->arch_info.userFrame; 164 if (frame == NULL) { 165 panic("arch_on_signal_stack(): No user iframe!"); 166 return false; 167 } 168 169 return frame->sp >= thread->signal_stack_base 170 && frame->sp < thread->signal_stack_base 171 + thread->signal_stack_size; 172 } 173 174 175 static uint8* 176 get_signal_stack(Thread* thread, struct iframe* frame, 177 struct sigaction* action, size_t spaceNeeded) 178 { 179 // use the alternate signal stack if we should and can 180 if ( 181 thread->signal_stack_enabled && 182 (action->sa_flags & SA_ONSTACK) != 0 && ( 183 frame->sp < thread->signal_stack_base || 184 frame->sp >= thread->signal_stack_base + thread->signal_stack_size 185 ) 186 ) { 187 addr_t stackTop = thread->signal_stack_base 188 + thread->signal_stack_size; 189 return (uint8*)ROUNDDOWN(stackTop - spaceNeeded, 16); 190 } 191 return (uint8*)ROUNDDOWN(frame->sp - spaceNeeded, 16); 192 } 193 194 195 status_t 196 arch_setup_signal_frame(Thread *thread, struct sigaction *sa, 197 struct signal_frame_data *signalFrameData) 198 { 199 // dprintf("%s(%" B_PRId32 "(%s))\n", __func__, thread->id, thread->name); 200 iframe* frame = thread->arch_info.userFrame; 201 202 // fill signal context 203 signalFrameData->context.uc_mcontext.x[ 0] = frame->ra; 204 signalFrameData->context.uc_mcontext.x[ 1] = frame->sp; 205 signalFrameData->context.uc_mcontext.x[ 2] = frame->gp; 206 signalFrameData->context.uc_mcontext.x[ 3] = frame->tp; 207 signalFrameData->context.uc_mcontext.x[ 4] = frame->t0; 208 signalFrameData->context.uc_mcontext.x[ 5] = frame->t1; 209 signalFrameData->context.uc_mcontext.x[ 6] = frame->t2; 210 signalFrameData->context.uc_mcontext.x[ 7] = frame->fp; 211 signalFrameData->context.uc_mcontext.x[ 8] = frame->s1; 212 signalFrameData->context.uc_mcontext.x[ 9] = frame->a0; 213 signalFrameData->context.uc_mcontext.x[10] = frame->a1; 214 signalFrameData->context.uc_mcontext.x[11] = frame->a2; 215 signalFrameData->context.uc_mcontext.x[12] = frame->a3; 216 signalFrameData->context.uc_mcontext.x[13] = frame->a4; 217 signalFrameData->context.uc_mcontext.x[14] = frame->a5; 218 signalFrameData->context.uc_mcontext.x[15] = frame->a6; 219 signalFrameData->context.uc_mcontext.x[16] = frame->a7; 220 signalFrameData->context.uc_mcontext.x[17] = frame->s2; 221 signalFrameData->context.uc_mcontext.x[18] = frame->s3; 222 signalFrameData->context.uc_mcontext.x[19] = frame->s4; 223 signalFrameData->context.uc_mcontext.x[20] = frame->s5; 224 signalFrameData->context.uc_mcontext.x[21] = frame->s6; 225 signalFrameData->context.uc_mcontext.x[22] = frame->s7; 226 signalFrameData->context.uc_mcontext.x[23] = frame->s8; 227 signalFrameData->context.uc_mcontext.x[24] = frame->s9; 228 signalFrameData->context.uc_mcontext.x[25] = frame->s10; 229 signalFrameData->context.uc_mcontext.x[26] = frame->s11; 230 signalFrameData->context.uc_mcontext.x[27] = frame->t3; 231 signalFrameData->context.uc_mcontext.x[28] = frame->t4; 232 signalFrameData->context.uc_mcontext.x[29] = frame->t5; 233 signalFrameData->context.uc_mcontext.x[30] = frame->t6; 234 signalFrameData->context.uc_mcontext.pc = frame->epc; 235 // TODO: don't assume that kernel code don't use FPU 236 save_fpu((fpu_context*)&signalFrameData->context.uc_mcontext.f[0]); 237 // end of fill signal context 238 239 signal_get_user_stack(frame->sp, &signalFrameData->context.uc_stack); 240 /* 241 dprintf(" thread->signal_stack_enabled: %d\n", 242 thread->signal_stack_enabled); 243 if (thread->signal_stack_enabled) { 244 dprintf(" signal stack: 0x%" B_PRIxADDR " - 0x%" B_PRIxADDR "\n", 245 thread->signal_stack_base, 246 thread->signal_stack_base + thread->signal_stack_size 247 ); 248 } 249 */ 250 signalFrameData->syscall_restart_return_value = thread->arch_info.oldA0; 251 252 uint8* userStack = get_signal_stack(thread, frame, sa, 253 sizeof(*signalFrameData)); 254 // dprintf(" user stack: 0x%" B_PRIxADDR "\n", (addr_t)userStack); 255 status_t res = user_memcpy(userStack, signalFrameData, 256 sizeof(*signalFrameData)); 257 if (res < B_OK) 258 return res; 259 260 addr_t commpageAdr = (addr_t)thread->team->commpage_address; 261 // dprintf(" commpageAdr: 0x%" B_PRIxADDR "\n", commpageAdr); 262 addr_t signalHandlerAddr; 263 ASSERT(user_memcpy(&signalHandlerAddr, 264 &((addr_t*)commpageAdr)[COMMPAGE_ENTRY_RISCV64_SIGNAL_HANDLER], 265 sizeof(signalHandlerAddr)) >= B_OK); 266 signalHandlerAddr += commpageAdr; 267 268 frame->ra = frame->epc; 269 frame->sp = (addr_t)userStack; 270 frame->epc = signalHandlerAddr; 271 frame->a0 = frame->sp; 272 273 // WriteTrapInfo(); 274 275 return B_OK; 276 } 277 278 279 int64 280 arch_restore_signal_frame(struct signal_frame_data* signalFrameData) 281 { 282 // dprintf("arch_restore_signal_frame()\n"); 283 iframe* frame = thread_get_current_thread()->arch_info.userFrame; 284 285 thread_get_current_thread()->arch_info.oldA0 286 = signalFrameData->syscall_restart_return_value; 287 288 frame->ra = signalFrameData->context.uc_mcontext.x[ 0]; 289 frame->sp = signalFrameData->context.uc_mcontext.x[ 1]; 290 frame->gp = signalFrameData->context.uc_mcontext.x[ 2]; 291 frame->tp = signalFrameData->context.uc_mcontext.x[ 3]; 292 frame->t0 = signalFrameData->context.uc_mcontext.x[ 4]; 293 frame->t1 = signalFrameData->context.uc_mcontext.x[ 5]; 294 frame->t2 = signalFrameData->context.uc_mcontext.x[ 6]; 295 frame->fp = signalFrameData->context.uc_mcontext.x[ 7]; 296 frame->s1 = signalFrameData->context.uc_mcontext.x[ 8]; 297 frame->a0 = signalFrameData->context.uc_mcontext.x[ 9]; 298 frame->a1 = signalFrameData->context.uc_mcontext.x[10]; 299 frame->a2 = signalFrameData->context.uc_mcontext.x[11]; 300 frame->a3 = signalFrameData->context.uc_mcontext.x[12]; 301 frame->a4 = signalFrameData->context.uc_mcontext.x[13]; 302 frame->a5 = signalFrameData->context.uc_mcontext.x[14]; 303 frame->a6 = signalFrameData->context.uc_mcontext.x[15]; 304 frame->a7 = signalFrameData->context.uc_mcontext.x[16]; 305 frame->s2 = signalFrameData->context.uc_mcontext.x[17]; 306 frame->s3 = signalFrameData->context.uc_mcontext.x[18]; 307 frame->s4 = signalFrameData->context.uc_mcontext.x[19]; 308 frame->s5 = signalFrameData->context.uc_mcontext.x[20]; 309 frame->s6 = signalFrameData->context.uc_mcontext.x[21]; 310 frame->s7 = signalFrameData->context.uc_mcontext.x[22]; 311 frame->s8 = signalFrameData->context.uc_mcontext.x[23]; 312 frame->s9 = signalFrameData->context.uc_mcontext.x[24]; 313 frame->s10 = signalFrameData->context.uc_mcontext.x[25]; 314 frame->s11 = signalFrameData->context.uc_mcontext.x[26]; 315 frame->t3 = signalFrameData->context.uc_mcontext.x[27]; 316 frame->t4 = signalFrameData->context.uc_mcontext.x[28]; 317 frame->t5 = signalFrameData->context.uc_mcontext.x[29]; 318 frame->t6 = signalFrameData->context.uc_mcontext.x[30]; 319 frame->epc = signalFrameData->context.uc_mcontext.pc; 320 restore_fpu((fpu_context*)&signalFrameData->context.uc_mcontext.f[0]); 321 322 return frame->a0; 323 } 324 325 326 /** Saves everything needed to restore the frame in the child fork in the 327 * arch_fork_arg structure to be passed to arch_restore_fork_frame(). 328 * Also makes sure to return the right value. 329 */ 330 331 void 332 arch_store_fork_frame(struct arch_fork_arg *arg) 333 { 334 /* 335 dprintf("arch_store_fork_frame()\n"); 336 dprintf(" arg: %p\n", arg); 337 dprintf(" userFrame: %p\n", 338 thread_get_current_thread()->arch_info.userFrame); 339 */ 340 memcpy(&arg->frame, thread_get_current_thread()->arch_info.userFrame, 341 sizeof(iframe)); 342 arg->frame.a0 = 0; // fork return value 343 } 344 345 346 /** Restores the frame from a forked team as specified by the provided 347 * arch_fork_arg structure. 348 * Needs to be called from within the child team, ie. instead of 349 * arch_thread_enter_uspace() as thread "starter". 350 * This function does not return to the caller, but will enter userland 351 * in the child team at the same position where the parent team left of. 352 */ 353 354 void 355 arch_restore_fork_frame(struct arch_fork_arg *arg) 356 { 357 //dprintf("arch_restore_fork_frame(%p)\n", arg); 358 //dprintf(" thread: %" B_PRId32 "(%s))\n", thread_get_current_thread()->id, 359 // thread_get_current_thread()->name); 360 //dprintf(" kernel SP: %#" B_PRIxADDR "\n", thread_get_current_thread()->kernel_stack_top); 361 //dprintf(" user PC: "); WritePC(arg->frame.epc); dprintf("\n"); 362 363 disable_interrupts(); 364 365 arch_stack* stackHeader = (arch_stack*)thread_get_current_thread()->kernel_stack_top - 1; 366 stackHeader->thread = thread_get_current_thread(); 367 SstatusReg status{.val = Sstatus()}; 368 status.pie = (1 << modeS); // enable interrupts when enter userspace 369 status.spp = modeU; 370 arg->frame.status = status.val; 371 arch_load_user_iframe(stackHeader, &arg->frame); 372 } 373