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