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 * 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 <arch_thread.h>
16
17 #include <string.h>
18
19 #include <arch_cpu.h>
20 #include <arch/thread.h>
21 #include <boot/stage2.h>
22 #include <kernel.h>
23 #include <thread.h>
24 #include <vm/vm_types.h>
25 #include <vm/VMAddressSpace.h>
26 #include <arch_vm.h>
27 //#include <arch/vm_translation_map.h>
28
29 #include "paging/M68KPagingMethod.h"
30 #include "paging/M68KPagingStructures.h"
31 #include "paging/M68KVMTranslationMap.h"
32
33
34 #warning M68K: writeme!
35 // Valid initial arch_thread state. We just memcpy() it when initializing
36 // a new thread structure.
37 static struct arch_thread sInitialState;
38
39 Thread *gCurrentThread;
40
41 // Helper function for thread creation, defined in arch_asm.S.
42 extern "C" void m68k_kernel_thread_root();
43
44
45 void
m68k_push_iframe(struct iframe_stack * stack,struct iframe * frame)46 m68k_push_iframe(struct iframe_stack *stack, struct iframe *frame)
47 {
48 ASSERT(stack->index < IFRAME_TRACE_DEPTH);
49 stack->frames[stack->index++] = frame;
50 }
51
52
53 void
m68k_pop_iframe(struct iframe_stack * stack)54 m68k_pop_iframe(struct iframe_stack *stack)
55 {
56 ASSERT(stack->index > 0);
57 stack->index--;
58 }
59
60
61 /** Returns the current iframe structure of the running thread.
62 * This function must only be called in a context where it's actually
63 * sure that such iframe exists; ie. from syscalls, but usually not
64 * from standard kernel threads.
65 */
66 static struct iframe *
m68k_get_current_iframe(void)67 m68k_get_current_iframe(void)
68 {
69 Thread *thread = thread_get_current_thread();
70
71 ASSERT(thread->arch_info.iframes.index >= 0);
72 return thread->arch_info.iframes.frames[thread->arch_info.iframes.index - 1];
73 }
74
75
76 /** \brief Returns the current thread's topmost (i.e. most recent)
77 * userland->kernel transition iframe (usually the first one, save for
78 * interrupts in signal handlers).
79 * \return The iframe, or \c NULL, if there is no such iframe (e.g. when
80 * the thread is a kernel thread).
81 */
82 struct iframe *
m68k_get_user_iframe(void)83 m68k_get_user_iframe(void)
84 {
85 Thread *thread = thread_get_current_thread();
86 int i;
87
88 for (i = thread->arch_info.iframes.index - 1; i >= 0; i--) {
89 struct iframe *frame = thread->arch_info.iframes.frames[i];
90 if ((frame->cpu.sr & (1 << M68K_SR_S)) == 0)
91 return frame;
92 }
93
94 return NULL;
95 }
96
97
98 uint32
m68k_next_page_directory(Thread * from,Thread * to)99 m68k_next_page_directory(Thread *from, Thread *to)
100 {
101 VMAddressSpace* toAddressSpace = to->team->address_space;
102 if (from->team->address_space == toAddressSpace) {
103 // don't change the pgdir, same address space
104 return 0;
105 }
106
107 if (toAddressSpace == NULL)
108 toAddressSpace = VMAddressSpace::Kernel();
109
110 return static_cast<M68KVMTranslationMap*>(toAddressSpace->TranslationMap())
111 ->PagingStructures()->pgroot_phys;
112 }
113
114 // #pragma mark -
115
116
117 status_t
arch_thread_init(struct kernel_args * args)118 arch_thread_init(struct kernel_args *args)
119 {
120 // Initialize the static initial arch_thread state (sInitialState).
121 // Currently nothing to do, i.e. zero initialized is just fine.
122
123 return B_OK;
124 }
125
126
127 status_t
arch_team_init_team_struct(Team * team,bool kernel)128 arch_team_init_team_struct(Team *team, bool kernel)
129 {
130 // Nothing to do. The structure is empty.
131 return B_OK;
132 }
133
134
135 status_t
arch_thread_init_thread_struct(Thread * thread)136 arch_thread_init_thread_struct(Thread *thread)
137 {
138 // set up an initial state (stack & fpu)
139 memcpy(&thread->arch_info, &sInitialState, sizeof(struct arch_thread));
140
141 return B_OK;
142 }
143
144
145 void
arch_thread_init_kthread_stack(Thread * thread,void * _stack,void * _stackTop,void (* function)(void *),const void * data)146 arch_thread_init_kthread_stack(Thread* thread, void* _stack, void* _stackTop,
147 void (*function)(void*), const void* data)
148 {
149 #if 0
150 addr_t *kstack = (addr_t *)t->kernel_stack_base;
151 addr_t *kstackTop = (addr_t *)t->kernel_stack_base;
152
153 // clear the kernel stack
154 #ifdef DEBUG_KERNEL_STACKS
155 # ifdef STACK_GROWS_DOWNWARDS
156 memset((void *)((addr_t)kstack + KERNEL_STACK_GUARD_PAGES * B_PAGE_SIZE), 0,
157 KERNEL_STACK_SIZE);
158 # else
159 memset(kstack, 0, KERNEL_STACK_SIZE);
160 # endif
161 #else
162 memset(kstack, 0, KERNEL_STACK_SIZE);
163 #endif
164
165 // space for frame pointer and return address, and stack frames must be
166 // 16 byte aligned
167 kstackTop -= 2;
168 kstackTop = (addr_t*)((addr_t)kstackTop & ~0xf);
169
170 // LR, CR, r2, r13-r31, f13-f31, as pushed by m68k_context_switch()
171 kstackTop -= 22 + 2 * 19;
172
173 // let LR point to m68k_kernel_thread_root()
174 kstackTop[0] = (addr_t)&m68k_kernel_thread_root;
175
176 // the arguments of m68k_kernel_thread_root() are the functions to call,
177 // provided in registers r13-r15
178 kstackTop[3] = (addr_t)entry_func;
179 kstackTop[4] = (addr_t)start_func;
180 kstackTop[5] = (addr_t)exit_func;
181
182 // save this stack position
183 t->arch_info.sp = (void *)kstackTop;
184
185 return B_OK;
186 #else
187 panic("arch_thread_init_kthread_stack(): Implement me!");
188 #endif
189 }
190
191
192 status_t
arch_thread_init_tls(Thread * thread)193 arch_thread_init_tls(Thread *thread)
194 {
195 // TODO: Implement!
196 return B_OK;
197 }
198
199
200 void
arch_thread_context_switch(Thread * from,Thread * to)201 arch_thread_context_switch(Thread *from, Thread *to)
202 {
203 addr_t newPageDirectory;
204
205 newPageDirectory = (addr_t)m68k_next_page_directory(from, to);
206
207 if ((newPageDirectory % B_PAGE_SIZE) != 0)
208 panic("arch_thread_context_switch: bad pgdir 0x%lx\n", newPageDirectory);
209 #warning M68K: export from arch_vm.c
210
211 //m68k_set_pgdir((void *)newPageDirectory);
212 gM68KPagingMethod->SetPageRoot(newPageDirectory);
213
214 m68k_context_switch(&from->arch_info.sp, to->arch_info.sp);
215 }
216
217
218 void
arch_thread_dump_info(void * info)219 arch_thread_dump_info(void *info)
220 {
221 struct arch_thread *at = (struct arch_thread *)info;
222
223 dprintf("\tsp: %p\n", at->sp);
224 }
225
226
227 status_t
arch_thread_enter_userspace(Thread * thread,addr_t entry,void * arg1,void * arg2)228 arch_thread_enter_userspace(Thread *thread, addr_t entry, void *arg1, void *arg2)
229 {
230 panic("arch_thread_enter_uspace(): not yet implemented\n");
231 return B_ERROR;
232 }
233
234
235 bool
arch_on_signal_stack(Thread * thread)236 arch_on_signal_stack(Thread *thread)
237 {
238 return false;
239 }
240
241
242 status_t
arch_setup_signal_frame(Thread * thread,struct sigaction * sa,struct signal_frame_data * signalFrameData)243 arch_setup_signal_frame(Thread *thread, struct sigaction *sa,
244 struct signal_frame_data *signalFrameData)
245 {
246 return B_ERROR;
247 }
248
249
250 int64
arch_restore_signal_frame(struct signal_frame_data * signalFrameData)251 arch_restore_signal_frame(struct signal_frame_data* signalFrameData)
252 {
253 return 0;
254 }
255
256
257 /** Saves everything needed to restore the frame in the child fork in the
258 * arch_fork_arg structure to be passed to arch_restore_fork_frame().
259 * Also makes sure to return the right value.
260 */
261
262 void
arch_store_fork_frame(struct arch_fork_arg * arg)263 arch_store_fork_frame(struct arch_fork_arg *arg)
264 {
265 }
266
267
268 /** Restores the frame from a forked team as specified by the provided
269 * arch_fork_arg structure.
270 * Needs to be called from within the child team, ie. instead of
271 * arch_thread_enter_uspace() as thread "starter".
272 * This function does not return to the caller, but will enter userland
273 * in the child team at the same position where the parent team left of.
274 */
275
276 void
arch_restore_fork_frame(struct arch_fork_arg * arg)277 arch_restore_fork_frame(struct arch_fork_arg *arg)
278 {
279 }
280
281