xref: /haiku/src/system/kernel/arch/arm/arch_thread.cpp (revision 191eff4135d354eb093fda1046371a63c67979ca)
1c9f536d7SFrançois Revol /*
260b19d7eSDavid Karoly  * Copyright 2003-2023, Haiku Inc. All rights reserved.
3c9f536d7SFrançois Revol  * Distributed under the terms of the MIT License.
4c9f536d7SFrançois Revol  *
5c9f536d7SFrançois Revol  * Authors:
6c9f536d7SFrançois Revol  * 		Axel Dörfler <axeld@pinc-software.de>
7c9f536d7SFrançois Revol  * 		Ingo Weinhold <bonefish@cs.tu-berlin.de>
8c9f536d7SFrançois Revol  * 		François Revol <revol@free.fr>
9c9f536d7SFrançois Revol  *
10c9f536d7SFrançois Revol  * Copyright 2001, Travis Geiselbrecht. All rights reserved.
11c9f536d7SFrançois Revol  * Distributed under the terms of the NewOS License.
12c9f536d7SFrançois Revol  */
13c9f536d7SFrançois Revol 
14924a3e5fSIngo Weinhold 
158bbaee61SIthamar R. Adema #include <thread.h>
16c9f536d7SFrançois Revol #include <arch_thread.h>
17c9f536d7SFrançois Revol 
18c9f536d7SFrançois Revol #include <arch_cpu.h>
19c9f536d7SFrançois Revol #include <arch/thread.h>
20c9f536d7SFrançois Revol #include <boot/stage2.h>
216804f6c7SDavid Karoly #include <commpage.h>
22c9f536d7SFrançois Revol #include <kernel.h>
23c9f536d7SFrançois Revol #include <thread.h>
24e50cf876SIngo Weinhold #include <vm/vm_types.h>
25e50cf876SIngo Weinhold #include <vm/VMAddressSpace.h>
26c9f536d7SFrançois Revol #include <arch_vm.h>
274fc1daddSIthamar R. Adema #include <arch/vm_translation_map.h>
28c9f536d7SFrançois Revol 
29c9f536d7SFrançois Revol #include <string.h>
30c9f536d7SFrançois Revol 
31b22c6f6fSDavid Karoly #include "ARMPagingStructures.h"
32b22c6f6fSDavid Karoly #include "ARMVMTranslationMap.h"
33b22c6f6fSDavid Karoly 
34ff163585SIthamar R. Adema //#define TRACE_ARCH_THREAD
354fc1daddSIthamar R. Adema #ifdef TRACE_ARCH_THREAD
36764c0853SDavid Karoly #	define TRACE(x...) dprintf(x)
374fc1daddSIthamar R. Adema #else
38764c0853SDavid Karoly #	define TRACE(x...) ;
394fc1daddSIthamar R. Adema #endif
40258d4ef9SAlexander von Gluck IV 
41c9f536d7SFrançois Revol // Valid initial arch_thread state. We just memcpy() it when initializing
42c9f536d7SFrançois Revol // a new thread structure.
43c9f536d7SFrançois Revol static struct arch_thread sInitialState;
44c9f536d7SFrançois Revol 
45c9f536d7SFrançois Revol 
46cc65466fSIthamar R. Adema void
arm_push_iframe(struct iframe_stack * stack,struct iframe * frame)47cc65466fSIthamar R. Adema arm_push_iframe(struct iframe_stack *stack, struct iframe *frame)
48cc65466fSIthamar R. Adema {
49cc65466fSIthamar R. Adema 	ASSERT(stack->index < IFRAME_TRACE_DEPTH);
50cc65466fSIthamar R. Adema 	stack->frames[stack->index++] = frame;
51cc65466fSIthamar R. Adema }
52cc65466fSIthamar R. Adema 
53cc65466fSIthamar R. Adema 
54cc65466fSIthamar R. Adema void
arm_pop_iframe(struct iframe_stack * stack)55cc65466fSIthamar R. Adema arm_pop_iframe(struct iframe_stack *stack)
56cc65466fSIthamar R. Adema {
57cc65466fSIthamar R. Adema 	ASSERT(stack->index > 0);
58cc65466fSIthamar R. Adema 	stack->index--;
59cc65466fSIthamar R. Adema }
60cc65466fSIthamar R. Adema 
61cc65466fSIthamar R. Adema 
62cc65466fSIthamar R. Adema 
63c9f536d7SFrançois Revol status_t
arch_thread_init(struct kernel_args * args)64c9f536d7SFrançois Revol arch_thread_init(struct kernel_args *args)
65c9f536d7SFrançois Revol {
66c9f536d7SFrançois Revol 	// Initialize the static initial arch_thread state (sInitialState).
67c9f536d7SFrançois Revol 	// Currently nothing to do, i.e. zero initialized is just fine.
68c9f536d7SFrançois Revol 
69c9f536d7SFrançois Revol 	return B_OK;
70c9f536d7SFrançois Revol }
71c9f536d7SFrançois Revol 
72c9f536d7SFrançois Revol 
73c9f536d7SFrançois Revol status_t
arch_team_init_team_struct(Team * team,bool kernel)744535495dSIngo Weinhold arch_team_init_team_struct(Team *team, bool kernel)
75c9f536d7SFrançois Revol {
76c9f536d7SFrançois Revol 	// Nothing to do. The structure is empty.
77c9f536d7SFrançois Revol 	return B_OK;
78c9f536d7SFrançois Revol }
79c9f536d7SFrançois Revol 
80c9f536d7SFrançois Revol 
81c9f536d7SFrançois Revol status_t
arch_thread_init_thread_struct(Thread * thread)824535495dSIngo Weinhold arch_thread_init_thread_struct(Thread *thread)
83c9f536d7SFrançois Revol {
84c9f536d7SFrançois Revol 	// set up an initial state (stack & fpu)
85c9f536d7SFrançois Revol 	memcpy(&thread->arch_info, &sInitialState, sizeof(struct arch_thread));
86c9f536d7SFrançois Revol 
87c9f536d7SFrançois Revol 	return B_OK;
88c9f536d7SFrançois Revol }
89c9f536d7SFrançois Revol 
90c9f536d7SFrançois Revol 
9124df6592SIngo Weinhold void
arch_thread_init_kthread_stack(Thread * thread,void * _stack,void * _stackTop,void (* function)(void *),const void * data)9224df6592SIngo Weinhold arch_thread_init_kthread_stack(Thread* thread, void* _stack, void* _stackTop,
9324df6592SIngo Weinhold 	void (*function)(void*), const void* data)
94c9f536d7SFrançois Revol {
954fc1daddSIthamar R. Adema 	addr_t* stackTop = (addr_t*)_stackTop;
964fc1daddSIthamar R. Adema 
97764c0853SDavid Karoly 	TRACE("arch_thread_init_kthread_stack(%s): stack top %p, function %p, data: "
98764c0853SDavid Karoly 		"%p\n", thread->name, stackTop, function, data);
994fc1daddSIthamar R. Adema 
1004fc1daddSIthamar R. Adema 	// push the function address -- that's the return address used after the
1014fc1daddSIthamar R. Adema 	// context switch (lr/r14 register)
1024fc1daddSIthamar R. Adema 	*--stackTop = (addr_t)function;
1034fc1daddSIthamar R. Adema 
1044fc1daddSIthamar R. Adema 	// simulate storing registers r1-r12
1054fc1daddSIthamar R. Adema 	for (int i = 1; i <= 12; i++)
1064fc1daddSIthamar R. Adema 		*--stackTop = 0;
1074fc1daddSIthamar R. Adema 
1084fc1daddSIthamar R. Adema 	// push the function argument as r0
1094fc1daddSIthamar R. Adema 	*--stackTop = (addr_t)data;
1104fc1daddSIthamar R. Adema 
1114fc1daddSIthamar R. Adema 	// save the stack position
1124fc1daddSIthamar R. Adema 	thread->arch_info.sp = stackTop;
113c9f536d7SFrançois Revol }
114c9f536d7SFrançois Revol 
115c9f536d7SFrançois Revol 
116c9f536d7SFrançois Revol status_t
arch_thread_init_tls(Thread * thread)1174535495dSIngo Weinhold arch_thread_init_tls(Thread *thread)
118c9f536d7SFrançois Revol {
119d86a240aSAugustin Cavalier 	thread->user_local_storage =
120d86a240aSAugustin Cavalier 		thread->user_stack_base + thread->user_stack_size;
121d86a240aSAugustin Cavalier 	return B_OK;
122c9f536d7SFrançois Revol }
123c9f536d7SFrançois Revol 
124d86a240aSAugustin Cavalier 
125b22c6f6fSDavid Karoly void
arm_swap_pgdir(uint32_t pageDirectoryAddress)126b22c6f6fSDavid Karoly arm_swap_pgdir(uint32_t pageDirectoryAddress)
127b22c6f6fSDavid Karoly {
128*6c58b765SDavid Karoly 	arm_set_ttbr0(pageDirectoryAddress);
129b22c6f6fSDavid Karoly 	isb();
130b22c6f6fSDavid Karoly 
131b22c6f6fSDavid Karoly 	arch_cpu_global_TLB_invalidate();
132b22c6f6fSDavid Karoly 
133b22c6f6fSDavid Karoly 	//TODO: update Context ID (incl. ASID)
134b22c6f6fSDavid Karoly 	//TODO: check if any additional TLB or Cache maintenance is needed
135b22c6f6fSDavid Karoly }
136b22c6f6fSDavid Karoly 
137b22c6f6fSDavid Karoly 
138c9f536d7SFrançois Revol void
arch_thread_context_switch(Thread * from,Thread * to)1394535495dSIngo Weinhold arch_thread_context_switch(Thread *from, Thread *to)
140c9f536d7SFrançois Revol {
141*6c58b765SDavid Karoly 	arm_set_tpidruro(to->user_local_storage);
142883858e6SDavid Karoly 
143b22c6f6fSDavid Karoly 	VMAddressSpace *oldAddressSpace = from->team->address_space;
144b22c6f6fSDavid Karoly 	VMTranslationMap *oldTranslationMap = oldAddressSpace->TranslationMap();
145b22c6f6fSDavid Karoly 	phys_addr_t oldPageDirectoryAddress =
146b22c6f6fSDavid Karoly 		((ARMVMTranslationMap *)oldTranslationMap)->PagingStructures()->pgdir_phys;
147b22c6f6fSDavid Karoly 
148b22c6f6fSDavid Karoly 	VMAddressSpace *newAddressSpace = to->team->address_space;
149b22c6f6fSDavid Karoly 	VMTranslationMap *newTranslationMap = newAddressSpace->TranslationMap();
150b22c6f6fSDavid Karoly 	phys_addr_t newPageDirectoryAddress =
151b22c6f6fSDavid Karoly 		((ARMVMTranslationMap *)newTranslationMap)->PagingStructures()->pgdir_phys;
152b22c6f6fSDavid Karoly 
153b22c6f6fSDavid Karoly 	if (oldPageDirectoryAddress != newPageDirectoryAddress) {
154764c0853SDavid Karoly 		TRACE("arch_thread_context_switch: swap pgdir: "
155b22c6f6fSDavid Karoly 			"0x%08" B_PRIxPHYSADDR " -> 0x%08" B_PRIxPHYSADDR "\n",
156764c0853SDavid Karoly 			oldPageDirectoryAddress, newPageDirectoryAddress);
157b22c6f6fSDavid Karoly 		arm_swap_pgdir(newPageDirectoryAddress);
158b22c6f6fSDavid Karoly 	}
159b22c6f6fSDavid Karoly 
160764c0853SDavid Karoly 	TRACE("arch_thread_context_switch: %p(%s/%p) -> %p(%s/%p)\n",
161764c0853SDavid Karoly 		from, from->name, from->arch_info.sp, to, to->name, to->arch_info.sp);
16267406e51SDavid Karoly 	arm_save_fpu(&from->arch_info.fpuContext);
16367406e51SDavid Karoly 	arm_restore_fpu(&to->arch_info.fpuContext);
1644fc1daddSIthamar R. Adema 	arm_context_switch(&from->arch_info, &to->arch_info);
165764c0853SDavid Karoly 	TRACE("arch_thread_context_switch %p %p\n", to, from);
166c9f536d7SFrançois Revol }
167c9f536d7SFrançois Revol 
168c9f536d7SFrançois Revol 
169c9f536d7SFrançois Revol void
arch_thread_dump_info(void * info)170c9f536d7SFrançois Revol arch_thread_dump_info(void *info)
171c9f536d7SFrançois Revol {
172c9f536d7SFrançois Revol 	struct arch_thread *at = (struct arch_thread *)info;
173c9f536d7SFrançois Revol 
174c9f536d7SFrançois Revol 	dprintf("\tsp: %p\n", at->sp);
175c9f536d7SFrançois Revol }
176c9f536d7SFrançois Revol 
177c9f536d7SFrançois Revol 
178c9f536d7SFrançois Revol status_t
arch_thread_enter_userspace(Thread * thread,addr_t entry,void * args1,void * args2)179258d4ef9SAlexander von Gluck IV arch_thread_enter_userspace(Thread *thread, addr_t entry,
1806804f6c7SDavid Karoly 	void *args1, void *args2)
181c9f536d7SFrançois Revol {
182*6c58b765SDavid Karoly 	arm_set_tpidruro(thread->user_local_storage);
1836804f6c7SDavid Karoly 
1846804f6c7SDavid Karoly 	addr_t stackTop = thread->user_stack_base + thread->user_stack_size;
1856804f6c7SDavid Karoly 
186764c0853SDavid Karoly 	TRACE("arch_thread_enter_userspace: entry 0x%" B_PRIxADDR ", args %p %p, "
187764c0853SDavid Karoly 		"ustack_top 0x%" B_PRIxADDR "\n", entry, args1, args2, stackTop);
1886804f6c7SDavid Karoly 
1896804f6c7SDavid Karoly 	//stackTop = arch_randomize_stack_pointer(stackTop - sizeof(args));
1906804f6c7SDavid Karoly 
1916804f6c7SDavid Karoly 	// Copy the address of the stub that calls exit_thread() when the thread
1926804f6c7SDavid Karoly 	// entry function returns to LR to act as the return address.
1936804f6c7SDavid Karoly 	// The stub is inside commpage.
1946804f6c7SDavid Karoly 	addr_t commPageAddress = (addr_t)thread->team->commpage_address;
1956804f6c7SDavid Karoly 
1966804f6c7SDavid Karoly 	disable_interrupts();
1976804f6c7SDavid Karoly 
1986804f6c7SDavid Karoly 	// prepare the user iframe
1996804f6c7SDavid Karoly 	iframe frame = {};
2006804f6c7SDavid Karoly 	frame.r0 = (uint32)args1;
2016804f6c7SDavid Karoly 	frame.r1 = (uint32)args2;
2026804f6c7SDavid Karoly 	frame.usr_sp = stackTop;
2036804f6c7SDavid Karoly 	frame.usr_lr = ((addr_t*)commPageAddress)[COMMPAGE_ENTRY_ARM_THREAD_EXIT]
2046804f6c7SDavid Karoly 		+ commPageAddress;
2056804f6c7SDavid Karoly 	frame.pc = entry;
2066804f6c7SDavid Karoly 
2076804f6c7SDavid Karoly 	// return to userland
2086804f6c7SDavid Karoly 	arch_return_to_userland(&frame);
2096804f6c7SDavid Karoly 
2106804f6c7SDavid Karoly 	// normally we don't get here
211c9f536d7SFrançois Revol 	return B_ERROR;
212c9f536d7SFrançois Revol }
213c9f536d7SFrançois Revol 
214c9f536d7SFrançois Revol 
215c9f536d7SFrançois Revol bool
arch_on_signal_stack(Thread * thread)2164535495dSIngo Weinhold arch_on_signal_stack(Thread *thread)
217c9f536d7SFrançois Revol {
21860b19d7eSDavid Karoly 	struct iframe* frame = thread->arch_info.userFrame;
21960b19d7eSDavid Karoly 	if (frame == NULL) {
22060b19d7eSDavid Karoly 		panic("arch_on_signal_stack(): No user iframe!");
221c9f536d7SFrançois Revol 		return false;
222c9f536d7SFrançois Revol 	}
223c9f536d7SFrançois Revol 
22460b19d7eSDavid Karoly 	return frame->usr_sp >= thread->signal_stack_base
22560b19d7eSDavid Karoly 		&& frame->usr_sp < thread->signal_stack_base
22660b19d7eSDavid Karoly 			+ thread->signal_stack_size;
22760b19d7eSDavid Karoly }
22860b19d7eSDavid Karoly 
22960b19d7eSDavid Karoly 
23060b19d7eSDavid Karoly static uint8*
get_signal_stack(Thread * thread,struct iframe * frame,struct sigaction * action,size_t spaceNeeded)23160b19d7eSDavid Karoly get_signal_stack(Thread* thread, struct iframe* frame,
23260b19d7eSDavid Karoly 	struct sigaction* action, size_t spaceNeeded)
23360b19d7eSDavid Karoly {
23460b19d7eSDavid Karoly 	// use the alternate signal stack if we should and can
23560b19d7eSDavid Karoly 	if (thread->signal_stack_enabled && (action->sa_flags & SA_ONSTACK) != 0
23660b19d7eSDavid Karoly 			&& (frame->usr_sp < thread->signal_stack_base
23760b19d7eSDavid Karoly 			|| frame->usr_sp >= thread->signal_stack_base + thread->signal_stack_size)) {
23860b19d7eSDavid Karoly 		addr_t stackTop = thread->signal_stack_base + thread->signal_stack_size;
23960b19d7eSDavid Karoly 		return (uint8*)ROUNDDOWN(stackTop - spaceNeeded, 16);
24060b19d7eSDavid Karoly 	}
24160b19d7eSDavid Karoly 
24260b19d7eSDavid Karoly 	return (uint8*)ROUNDDOWN(frame->usr_sp - spaceNeeded, 16);
24360b19d7eSDavid Karoly }
24460b19d7eSDavid Karoly 
245c9f536d7SFrançois Revol 
246c9f536d7SFrançois Revol status_t
arch_setup_signal_frame(Thread * thread,struct sigaction * sa,struct signal_frame_data * signalFrameData)24724df6592SIngo Weinhold arch_setup_signal_frame(Thread *thread, struct sigaction *sa,
24824df6592SIngo Weinhold 	struct signal_frame_data *signalFrameData)
249c9f536d7SFrançois Revol {
25060b19d7eSDavid Karoly 	iframe* frame = thread->arch_info.userFrame;
25160b19d7eSDavid Karoly 	if (frame == NULL) {
25260b19d7eSDavid Karoly 		panic("arch_setup_signal_frame(): No user iframe!");
253c9f536d7SFrançois Revol 		return B_ERROR;
254c9f536d7SFrançois Revol 	}
255c9f536d7SFrançois Revol 
25660b19d7eSDavid Karoly 	// store the register state in signalFrameData->context.uc_mcontext
25760b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r0   = frame->r0;
25860b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r1   = frame->r1;
25960b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r2   = frame->r2;
26060b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r3   = frame->r3;
26160b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r4   = frame->r4;
26260b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r5   = frame->r5;
26360b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r6   = frame->r6;
26460b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r7   = frame->r7;
26560b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r8   = frame->r8;
26660b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r9   = frame->r9;
26760b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r10  = frame->r10;
26860b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r11  = frame->r11;
26960b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r12  = frame->r12;
27060b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r13  = frame->usr_sp;
27160b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r14  = frame->usr_lr;
27260b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.r15  = frame->pc;
27360b19d7eSDavid Karoly 	signalFrameData->context.uc_mcontext.cpsr = frame->spsr;
27460b19d7eSDavid Karoly 
27560b19d7eSDavid Karoly 	arm_save_fpu((arch_fpu_context*)&signalFrameData->context.uc_mcontext.d[0]);
27660b19d7eSDavid Karoly 
27760b19d7eSDavid Karoly 	// Fill in signalFrameData->context.uc_stack
27860b19d7eSDavid Karoly 	signal_get_user_stack(frame->usr_sp, &signalFrameData->context.uc_stack);
27960b19d7eSDavid Karoly 
28060b19d7eSDavid Karoly 	// store oldR0 in syscall_restart_return_value
28160b19d7eSDavid Karoly 	signalFrameData->syscall_restart_return_value = thread->arch_info.oldR0;
28260b19d7eSDavid Karoly 
28360b19d7eSDavid Karoly 	// get the stack to use -- that's either the current one or a special signal stack
28460b19d7eSDavid Karoly 	uint8* userStack = get_signal_stack(thread, frame, sa,
28560b19d7eSDavid Karoly 		sizeof(*signalFrameData));
28660b19d7eSDavid Karoly 
28760b19d7eSDavid Karoly 	// copy the signal frame data onto the stack
28860b19d7eSDavid Karoly 	status_t res = user_memcpy(userStack, signalFrameData,
28960b19d7eSDavid Karoly 		sizeof(*signalFrameData));
29060b19d7eSDavid Karoly 	if (res < B_OK)
29160b19d7eSDavid Karoly 		return res;
29260b19d7eSDavid Karoly 
29360b19d7eSDavid Karoly 	// prepare the user stack frame for a function call to the signal handler wrapper function
29460b19d7eSDavid Karoly 	addr_t commpageAddr = (addr_t)thread->team->commpage_address;
29560b19d7eSDavid Karoly 	addr_t signalHandlerAddr;
29660b19d7eSDavid Karoly 	ASSERT(user_memcpy(&signalHandlerAddr,
29760b19d7eSDavid Karoly 		&((addr_t*)commpageAddr)[COMMPAGE_ENTRY_ARM_SIGNAL_HANDLER],
29860b19d7eSDavid Karoly 		sizeof(signalHandlerAddr)) >= B_OK);
29960b19d7eSDavid Karoly 	signalHandlerAddr += commpageAddr;
30060b19d7eSDavid Karoly 
30160b19d7eSDavid Karoly 	frame->usr_lr = frame->pc;
30260b19d7eSDavid Karoly 	frame->usr_sp = (addr_t)userStack;
30360b19d7eSDavid Karoly 	frame->pc = signalHandlerAddr;
30460b19d7eSDavid Karoly 	frame->r0 = frame->usr_sp;
30560b19d7eSDavid Karoly 
30660b19d7eSDavid Karoly 	return B_OK;
30760b19d7eSDavid Karoly }
30860b19d7eSDavid Karoly 
309c9f536d7SFrançois Revol 
310c9f536d7SFrançois Revol int64
arch_restore_signal_frame(struct signal_frame_data * signalFrameData)31124df6592SIngo Weinhold arch_restore_signal_frame(struct signal_frame_data* signalFrameData)
312c9f536d7SFrançois Revol {
31360b19d7eSDavid Karoly 	iframe* frame = thread_get_current_thread()->arch_info.userFrame;
31460b19d7eSDavid Karoly 	if (frame == NULL) {
31560b19d7eSDavid Karoly 		panic("arch_restore_signal_frame(): No user iframe!");
316c9f536d7SFrançois Revol 		return 0;
317c9f536d7SFrançois Revol 	}
318c9f536d7SFrançois Revol 
31960b19d7eSDavid Karoly 	thread_get_current_thread()->arch_info.oldR0
32060b19d7eSDavid Karoly 		= signalFrameData->syscall_restart_return_value;
32160b19d7eSDavid Karoly 
32260b19d7eSDavid Karoly 	frame->r0     = signalFrameData->context.uc_mcontext.r0;
32360b19d7eSDavid Karoly 	frame->r1     = signalFrameData->context.uc_mcontext.r1;
32460b19d7eSDavid Karoly 	frame->r2     = signalFrameData->context.uc_mcontext.r2;
32560b19d7eSDavid Karoly 	frame->r3     = signalFrameData->context.uc_mcontext.r3;
32660b19d7eSDavid Karoly 	frame->r4     = signalFrameData->context.uc_mcontext.r4;
32760b19d7eSDavid Karoly 	frame->r5     = signalFrameData->context.uc_mcontext.r5;
32860b19d7eSDavid Karoly 	frame->r6     = signalFrameData->context.uc_mcontext.r6;
32960b19d7eSDavid Karoly 	frame->r7     = signalFrameData->context.uc_mcontext.r7;
33060b19d7eSDavid Karoly 	frame->r8     = signalFrameData->context.uc_mcontext.r8;
33160b19d7eSDavid Karoly 	frame->r9     = signalFrameData->context.uc_mcontext.r9;
33260b19d7eSDavid Karoly 	frame->r10    = signalFrameData->context.uc_mcontext.r10;
33360b19d7eSDavid Karoly 	frame->r11    = signalFrameData->context.uc_mcontext.r11;
33460b19d7eSDavid Karoly 	frame->r12    = signalFrameData->context.uc_mcontext.r12;
33560b19d7eSDavid Karoly 	frame->usr_sp = signalFrameData->context.uc_mcontext.r13;
33660b19d7eSDavid Karoly 	frame->usr_lr = signalFrameData->context.uc_mcontext.r14;
33760b19d7eSDavid Karoly 	frame->pc     = signalFrameData->context.uc_mcontext.r15;
33860b19d7eSDavid Karoly 	frame->spsr   = signalFrameData->context.uc_mcontext.cpsr;
33960b19d7eSDavid Karoly 
34060b19d7eSDavid Karoly 	arm_restore_fpu((arch_fpu_context*)&signalFrameData->context.uc_mcontext.d[0]);
34160b19d7eSDavid Karoly 
34260b19d7eSDavid Karoly 	return frame->r0;
34360b19d7eSDavid Karoly }
34460b19d7eSDavid Karoly 
345c9f536d7SFrançois Revol 
346c9f536d7SFrançois Revol /**	Saves everything needed to restore the frame in the child fork in the
347c9f536d7SFrançois Revol  *	arch_fork_arg structure to be passed to arch_restore_fork_frame().
348c9f536d7SFrançois Revol  *	Also makes sure to return the right value.
349c9f536d7SFrançois Revol  */
350c9f536d7SFrançois Revol void
arch_store_fork_frame(struct arch_fork_arg * arg)351c9f536d7SFrançois Revol arch_store_fork_frame(struct arch_fork_arg *arg)
352c9f536d7SFrançois Revol {
35360b19d7eSDavid Karoly 	struct iframe* frame = thread_get_current_thread()->arch_info.userFrame;
35460b19d7eSDavid Karoly 	if (frame == NULL) {
35560b19d7eSDavid Karoly 		panic("arch_store_fork_frame(): No user iframe!");
35660b19d7eSDavid Karoly 	}
35760b19d7eSDavid Karoly 
35860b19d7eSDavid Karoly 	arg->frame = *frame;
35960b19d7eSDavid Karoly 	arg->frame.r0 = 0; // fork return value
360c9f536d7SFrançois Revol }
361c9f536d7SFrançois Revol 
362c9f536d7SFrançois Revol 
363c9f536d7SFrançois Revol /** Restores the frame from a forked team as specified by the provided
364c9f536d7SFrançois Revol  *	arch_fork_arg structure.
365c9f536d7SFrançois Revol  *	Needs to be called from within the child team, ie. instead of
366c9f536d7SFrançois Revol  *	arch_thread_enter_uspace() as thread "starter".
367c9f536d7SFrançois Revol  *	This function does not return to the caller, but will enter userland
368c9f536d7SFrançois Revol  *	in the child team at the same position where the parent team left of.
369c9f536d7SFrançois Revol  */
370c9f536d7SFrançois Revol void
arch_restore_fork_frame(struct arch_fork_arg * arg)371c9f536d7SFrançois Revol arch_restore_fork_frame(struct arch_fork_arg *arg)
372c9f536d7SFrançois Revol {
37360b19d7eSDavid Karoly 	disable_interrupts();
37460b19d7eSDavid Karoly 	arch_return_to_userland(&arg->frame);
375c9f536d7SFrançois Revol }
376