xref: /haiku/src/system/kernel/arch/arm/arch_thread.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
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 
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 	return false;
238 }
239 
240 
241 status_t
242 arch_setup_signal_frame(Thread *thread, struct sigaction *sa,
243 	struct signal_frame_data *signalFrameData)
244 {
245 	return B_ERROR;
246 }
247 
248 
249 int64
250 arch_restore_signal_frame(struct signal_frame_data* signalFrameData)
251 {
252 	return 0;
253 }
254 
255 
256 void
257 arch_check_syscall_restart(Thread *thread)
258 {
259 }
260 
261 
262 /**	Saves everything needed to restore the frame in the child fork in the
263  *	arch_fork_arg structure to be passed to arch_restore_fork_frame().
264  *	Also makes sure to return the right value.
265  */
266 void
267 arch_store_fork_frame(struct arch_fork_arg *arg)
268 {
269 }
270 
271 
272 /** Restores the frame from a forked team as specified by the provided
273  *	arch_fork_arg structure.
274  *	Needs to be called from within the child team, ie. instead of
275  *	arch_thread_enter_uspace() as thread "starter".
276  *	This function does not return to the caller, but will enter userland
277  *	in the child team at the same position where the parent team left of.
278  */
279 void
280 arch_restore_fork_frame(struct arch_fork_arg *arg)
281 {
282 }
283