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>
8 * François Revol <revol@free.fr>
10 * Copyright 2001, Travis Geiselbrecht. All rights reserved.
11 * Distributed under the terms of the NewOS License.
15 #include <arch_thread.h>
20 #include <arch/thread.h>
21 #include <boot/stage2.h>
24 #include <vm/vm_types.h>
25 #include <vm/VMAddressSpace.h>
27 //#include <arch/vm_translation_map.h>
29 #include "paging/M68KPagingMethod.h"
30 #include "paging/M68KPagingStructures.h"
31 #include "paging/M68KVMTranslationMap.h"
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
;
39 Thread
*gCurrentThread
;
41 // Helper function for thread creation, defined in arch_asm.S.
42 extern "C" void m68k_kernel_thread_root();
46 m68k_push_iframe(struct iframe_stack
*stack
, struct iframe
*frame
)
48 ASSERT(stack
->index
< IFRAME_TRACE_DEPTH
);
49 stack
->frames
[stack
->index
++] = frame
;
54 m68k_pop_iframe(struct iframe_stack
*stack
)
56 ASSERT(stack
->index
> 0);
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.
66 static struct iframe
*
67 m68k_get_current_iframe(void)
69 Thread
*thread
= thread_get_current_thread();
71 ASSERT(thread
->arch_info
.iframes
.index
>= 0);
72 return thread
->arch_info
.iframes
.frames
[thread
->arch_info
.iframes
.index
- 1];
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).
83 m68k_get_user_iframe(void)
85 Thread
*thread
= thread_get_current_thread();
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)
99 m68k_next_page_directory(Thread
*from
, Thread
*to
)
101 VMAddressSpace
* toAddressSpace
= to
->team
->address_space
;
102 if (from
->team
->address_space
== toAddressSpace
) {
103 // don't change the pgdir, same address space
107 if (toAddressSpace
== NULL
)
108 toAddressSpace
= VMAddressSpace::Kernel();
110 return static_cast<M68KVMTranslationMap
*>(toAddressSpace
->TranslationMap())
111 ->PagingStructures()->pgroot_phys
;
118 arch_thread_init(struct kernel_args
*args
)
120 // Initialize the static initial arch_thread state (sInitialState).
121 // Currently nothing to do, i.e. zero initialized is just fine.
128 arch_team_init_team_struct(Team
*team
, bool kernel
)
130 // Nothing to do. The structure is empty.
136 arch_thread_init_thread_struct(Thread
*thread
)
138 // set up an initial state (stack & fpu)
139 memcpy(&thread
->arch_info
, &sInitialState
, sizeof(struct arch_thread
));
146 arch_thread_init_kthread_stack(Thread
* thread
, void* _stack
, void* _stackTop
,
147 void (*function
)(void*), const void* data
)
150 addr_t
*kstack
= (addr_t
*)t
->kernel_stack_base
;
151 addr_t
*kstackTop
= (addr_t
*)t
->kernel_stack_base
;
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,
159 memset(kstack
, 0, KERNEL_STACK_SIZE
);
162 memset(kstack
, 0, KERNEL_STACK_SIZE
);
165 // space for frame pointer and return address, and stack frames must be
168 kstackTop
= (addr_t
*)((addr_t
)kstackTop
& ~0xf);
170 // LR, CR, r2, r13-r31, f13-f31, as pushed by m68k_context_switch()
171 kstackTop
-= 22 + 2 * 19;
173 // let LR point to m68k_kernel_thread_root()
174 kstackTop
[0] = (addr_t
)&m68k_kernel_thread_root
;
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
;
182 // save this stack position
183 t
->arch_info
.sp
= (void *)kstackTop
;
187 panic("arch_thread_init_kthread_stack(): Implement me!");
193 arch_thread_init_tls(Thread
*thread
)
201 arch_thread_context_switch(Thread
*from
, Thread
*to
)
203 addr_t newPageDirectory
;
205 newPageDirectory
= (addr_t
)m68k_next_page_directory(from
, to
);
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
211 //m68k_set_pgdir((void *)newPageDirectory);
212 gM68KPagingMethod
->SetPageRoot(newPageDirectory
);
214 m68k_context_switch(&from
->arch_info
.sp
, to
->arch_info
.sp
);
219 arch_thread_dump_info(void *info
)
221 struct arch_thread
*at
= (struct arch_thread
*)info
;
223 dprintf("\tsp: %p\n", at
->sp
);
228 arch_thread_enter_userspace(Thread
*thread
, addr_t entry
, void *arg1
, void *arg2
)
230 panic("arch_thread_enter_uspace(): not yet implemented\n");
236 arch_on_signal_stack(Thread
*thread
)
243 arch_setup_signal_frame(Thread
*thread
, struct sigaction
*sa
,
244 struct signal_frame_data
*signalFrameData
)
251 arch_restore_signal_frame(struct signal_frame_data
* signalFrameData
)
258 arch_check_syscall_restart(Thread
*thread
)
263 /** Saves everything needed to restore the frame in the child fork in the
264 * arch_fork_arg structure to be passed to arch_restore_fork_frame().
265 * Also makes sure to return the right value.
269 arch_store_fork_frame(struct arch_fork_arg
*arg
)
274 /** Restores the frame from a forked team as specified by the provided
275 * arch_fork_arg structure.
276 * Needs to be called from within the child team, ie. instead of
277 * arch_thread_enter_uspace() as thread "starter".
278 * This function does not return to the caller, but will enter userland
279 * in the child team at the same position where the parent team left of.
283 arch_restore_fork_frame(struct arch_fork_arg
*arg
)