2 * Copyright 2003-2011, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler <axeld@pinc-software.de>
7 * Ingo Weinhold <bonefish@cs.tu-berlin.de>
9 * Copyright 2001, Travis Geiselbrecht. All rights reserved.
10 * Distributed under the terms of the NewOS License.
15 #include <arch/thread.h>
16 #include <boot/stage2.h>
19 #include <vm/vm_types.h>
20 #include <vm/VMAddressSpace.h>
21 //#include <arch/vm_translation_map.h>
25 // Valid initial arch_thread state. We just memcpy() it when initializing
26 // a new thread structure.
27 static struct arch_thread sInitialState
;
29 // Helper function for thread creation, defined in arch_asm.S.
30 extern "C" void ppc_kernel_thread_root();
34 ppc_push_iframe(struct iframe_stack
*stack
, struct iframe
*frame
)
36 ASSERT(stack
->index
< IFRAME_TRACE_DEPTH
);
37 stack
->frames
[stack
->index
++] = frame
;
42 ppc_pop_iframe(struct iframe_stack
*stack
)
44 ASSERT(stack
->index
> 0);
49 /** Returns the current iframe structure of the running thread.
50 * This function must only be called in a context where it's actually
51 * sure that such iframe exists; ie. from syscalls, but usually not
52 * from standard kernel threads.
54 static struct iframe
*
55 ppc_get_current_iframe(void)
57 Thread
*thread
= thread_get_current_thread();
59 ASSERT(thread
->arch_info
.iframes
.index
>= 0);
60 return thread
->arch_info
.iframes
.frames
[thread
->arch_info
.iframes
.index
- 1];
64 /** \brief Returns the current thread's topmost (i.e. most recent)
65 * userland->kernel transition iframe (usually the first one, save for
66 * interrupts in signal handlers).
67 * \return The iframe, or \c NULL, if there is no such iframe (e.g. when
68 * the thread is a kernel thread).
71 ppc_get_user_iframe(void)
73 Thread
*thread
= thread_get_current_thread();
76 for (i
= thread
->arch_info
.iframes
.index
- 1; i
>= 0; i
--) {
77 struct iframe
*frame
= thread
->arch_info
.iframes
.frames
[i
];
78 if (frame
->srr1
& MSR_PRIVILEGE_LEVEL
)
90 arch_thread_init(struct kernel_args
*args
)
92 // Initialize the static initial arch_thread state (sInitialState).
93 // Currently nothing to do, i.e. zero initialized is just fine.
100 arch_team_init_team_struct(Team
*team
, bool kernel
)
102 // Nothing to do. The structure is empty.
108 arch_thread_init_thread_struct(Thread
*thread
)
110 // set up an initial state (stack & fpu)
111 memcpy(&thread
->arch_info
, &sInitialState
, sizeof(struct arch_thread
));
118 arch_thread_init_kthread_stack(Thread
* thread
, void* _stack
, void* _stackTop
,
119 void (*function
)(void*), const void* data
)
122 addr_t
*kstack
= (addr_t
*)t
->kernel_stack_base
;
123 addr_t
*kstackTop
= (addr_t
*)t
->kernel_stack_top
;
125 // clear the kernel stack
126 #ifdef DEBUG_KERNEL_STACKS
127 # ifdef STACK_GROWS_DOWNWARDS
128 memset((void *)((addr_t
)kstack
+ KERNEL_STACK_GUARD_PAGES
* B_PAGE_SIZE
), 0,
131 memset(kstack
, 0, KERNEL_STACK_SIZE
);
134 memset(kstack
, 0, KERNEL_STACK_SIZE
);
137 // space for frame pointer and return address, and stack frames must be
140 kstackTop
= (addr_t
*)((addr_t
)kstackTop
& ~0xf);
142 // LR, CR, r2, r13-r31, f13-f31, as pushed by ppc_context_switch()
143 kstackTop
-= 22 + 2 * 19;
145 // let LR point to ppc_kernel_thread_root()
146 kstackTop
[0] = (addr_t
)&ppc_kernel_thread_root
;
148 // the arguments of ppc_kernel_thread_root() are the functions to call,
149 // provided in registers r13-r15
150 kstackTop
[3] = (addr_t
)entry_func
;
151 kstackTop
[4] = (addr_t
)start_func
;
152 kstackTop
[5] = (addr_t
)exit_func
;
154 // save this stack position
155 t
->arch_info
.sp
= (void *)kstackTop
;
159 panic("arch_thread_init_kthread_stack(): Implement me!");
165 arch_thread_init_tls(Thread
*thread
)
173 arch_thread_context_switch(Thread
*t_from
, Thread
*t_to
)
175 // set the new kernel stack in the EAR register.
176 // this is used in the exception handler code to decide what kernel stack to
177 // switch to if the exception had happened when the processor was in user mode
178 asm("mtear %0" :: "g"(t_to
->kernel_stack_top
- 8));
180 // switch the asids if we need to
181 if (t_to
->team
->address_space
!= NULL
) {
182 // the target thread has is user space
183 if (t_from
->team
!= t_to
->team
) {
184 // switching to a new address space
185 ppc_translation_map_change_asid(
186 t_to
->team
->address_space
->TranslationMap());
190 ppc_context_switch(&t_from
->arch_info
.sp
, t_to
->arch_info
.sp
);
195 arch_thread_dump_info(void *info
)
197 struct arch_thread
*at
= (struct arch_thread
*)info
;
199 dprintf("\tsp: %p\n", at
->sp
);
204 arch_thread_enter_userspace(Thread
*thread
, addr_t entry
, void *arg1
, void *arg2
)
206 panic("arch_thread_enter_uspace(): not yet implemented\n");
212 arch_on_signal_stack(Thread
*thread
)
219 arch_setup_signal_frame(Thread
*thread
, struct sigaction
*sa
,
220 struct signal_frame_data
*signalFrameData
)
227 arch_restore_signal_frame(struct signal_frame_data
* signalFrameData
)
234 /** Saves everything needed to restore the frame in the child fork in the
235 * arch_fork_arg structure to be passed to arch_restore_fork_frame().
236 * Also makes sure to return the right value.
240 arch_store_fork_frame(struct arch_fork_arg
*arg
)
245 /** Restores the frame from a forked team as specified by the provided
246 * arch_fork_arg structure.
247 * Needs to be called from within the child team, ie. instead of
248 * arch_thread_enter_uspace() as thread "starter".
249 * This function does not return to the caller, but will enter userland
250 * in the child team at the same position where the parent team left of.
254 arch_restore_fork_frame(struct arch_fork_arg
*arg
)