1 /*
2 * Copyright 2003-2011, 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 *
9 * Copyright 2001, Travis Geiselbrecht. All rights reserved.
10 * Distributed under the terms of the NewOS License.
11 */
12
13
14 #include <arch/cpu.h>
15 #include <arch/thread.h>
16 #include <boot/stage2.h>
17 #include <kernel.h>
18 #include <thread.h>
19 #include <vm/vm_types.h>
20 #include <vm/VMAddressSpace.h>
21 //#include <arch/vm_translation_map.h>
22
23 #include <string.h>
24
25 // Valid initial arch_thread state. We just memcpy() it when initializing
26 // a new thread structure.
27 static struct arch_thread sInitialState;
28
29 // Helper function for thread creation, defined in arch_asm.S.
30 extern "C" void ppc_kernel_thread_root();
31
32
33 void
ppc_push_iframe(struct iframe_stack * stack,struct iframe * frame)34 ppc_push_iframe(struct iframe_stack *stack, struct iframe *frame)
35 {
36 ASSERT(stack->index < IFRAME_TRACE_DEPTH);
37 stack->frames[stack->index++] = frame;
38 }
39
40
41 void
ppc_pop_iframe(struct iframe_stack * stack)42 ppc_pop_iframe(struct iframe_stack *stack)
43 {
44 ASSERT(stack->index > 0);
45 stack->index--;
46 }
47
48
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.
53 */
54 static struct iframe *
ppc_get_current_iframe(void)55 ppc_get_current_iframe(void)
56 {
57 Thread *thread = thread_get_current_thread();
58
59 ASSERT(thread->arch_info.iframes.index >= 0);
60 return thread->arch_info.iframes.frames[thread->arch_info.iframes.index - 1];
61 }
62
63
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).
69 */
70 struct iframe *
ppc_get_user_iframe(void)71 ppc_get_user_iframe(void)
72 {
73 Thread *thread = thread_get_current_thread();
74 int i;
75
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)
79 return frame;
80 }
81
82 return NULL;
83 }
84
85
86 // #pragma mark -
87
88
89 status_t
arch_thread_init(struct kernel_args * args)90 arch_thread_init(struct kernel_args *args)
91 {
92 // Initialize the static initial arch_thread state (sInitialState).
93 // Currently nothing to do, i.e. zero initialized is just fine.
94
95 return B_OK;
96 }
97
98
99 status_t
arch_team_init_team_struct(Team * team,bool kernel)100 arch_team_init_team_struct(Team *team, bool kernel)
101 {
102 // Nothing to do. The structure is empty.
103 return B_OK;
104 }
105
106
107 status_t
arch_thread_init_thread_struct(Thread * thread)108 arch_thread_init_thread_struct(Thread *thread)
109 {
110 // set up an initial state (stack & fpu)
111 memcpy(&thread->arch_info, &sInitialState, sizeof(struct arch_thread));
112
113 return B_OK;
114 }
115
116
117 void
arch_thread_init_kthread_stack(Thread * thread,void * _stack,void * _stackTop,void (* function)(void *),const void * data)118 arch_thread_init_kthread_stack(Thread* thread, void* _stack, void* _stackTop,
119 void (*function)(void*), const void* data)
120 {
121 #if 0
122 addr_t *kstack = (addr_t *)t->kernel_stack_base;
123 addr_t *kstackTop = (addr_t *)t->kernel_stack_top;
124
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,
129 KERNEL_STACK_SIZE);
130 # else
131 memset(kstack, 0, KERNEL_STACK_SIZE);
132 # endif
133 #else
134 memset(kstack, 0, KERNEL_STACK_SIZE);
135 #endif
136
137 // space for frame pointer and return address, and stack frames must be
138 // 16 byte aligned
139 kstackTop -= 2;
140 kstackTop = (addr_t*)((addr_t)kstackTop & ~0xf);
141
142 // LR, CR, r2, r13-r31, f13-f31, as pushed by ppc_context_switch()
143 kstackTop -= 22 + 2 * 19;
144
145 // let LR point to ppc_kernel_thread_root()
146 kstackTop[0] = (addr_t)&ppc_kernel_thread_root;
147
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;
153
154 // save this stack position
155 t->arch_info.sp = (void *)kstackTop;
156
157 return B_OK;
158 #else
159 panic("arch_thread_init_kthread_stack(): Implement me!");
160 #endif
161 }
162
163
164 status_t
arch_thread_init_tls(Thread * thread)165 arch_thread_init_tls(Thread *thread)
166 {
167 // TODO: Implement!
168 return B_OK;
169 }
170
171
172 void
arch_thread_context_switch(Thread * t_from,Thread * t_to)173 arch_thread_context_switch(Thread *t_from, Thread *t_to)
174 {
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));
179
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());
187 }
188 }
189
190 ppc_context_switch(&t_from->arch_info.sp, t_to->arch_info.sp);
191 }
192
193
194 void
arch_thread_dump_info(void * info)195 arch_thread_dump_info(void *info)
196 {
197 struct arch_thread *at = (struct arch_thread *)info;
198
199 dprintf("\tsp: %p\n", at->sp);
200 }
201
202
203 status_t
arch_thread_enter_userspace(Thread * thread,addr_t entry,void * arg1,void * arg2)204 arch_thread_enter_userspace(Thread *thread, addr_t entry, void *arg1, void *arg2)
205 {
206 panic("arch_thread_enter_uspace(): not yet implemented\n");
207 return B_ERROR;
208 }
209
210
211 bool
arch_on_signal_stack(Thread * thread)212 arch_on_signal_stack(Thread *thread)
213 {
214 return false;
215 }
216
217
218 status_t
arch_setup_signal_frame(Thread * thread,struct sigaction * sa,struct signal_frame_data * signalFrameData)219 arch_setup_signal_frame(Thread *thread, struct sigaction *sa,
220 struct signal_frame_data *signalFrameData)
221 {
222 return B_ERROR;
223 }
224
225
226 int64
arch_restore_signal_frame(struct signal_frame_data * signalFrameData)227 arch_restore_signal_frame(struct signal_frame_data* signalFrameData)
228 {
229 return 0;
230 }
231
232
233
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.
237 */
238
239 void
arch_store_fork_frame(struct arch_fork_arg * arg)240 arch_store_fork_frame(struct arch_fork_arg *arg)
241 {
242 }
243
244
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.
251 */
252
253 void
arch_restore_fork_frame(struct arch_fork_arg * arg)254 arch_restore_fork_frame(struct arch_fork_arg *arg)
255 {
256 }
257
258