xref: /haiku/src/system/kernel/arch/m68k/arch_thread.cpp (revision fce97ba360ff70fb19f9cd2a57a16152f0925c06)
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
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
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 *
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 *
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
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
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
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
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
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
193 arch_thread_init_tls(Thread *thread)
194 {
195 	// TODO: Implement!
196 	return B_OK;
197 }
198 
199 
200 void
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
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
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
236 arch_on_signal_stack(Thread *thread)
237 {
238 	return false;
239 }
240 
241 
242 status_t
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
251 arch_restore_signal_frame(struct signal_frame_data* signalFrameData)
252 {
253 	return 0;
254 }
255 
256 
257 void
258 arch_check_syscall_restart(Thread *thread)
259 {
260 }
261 
262 
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.
266  */
267 
268 void
269 arch_store_fork_frame(struct arch_fork_arg *arg)
270 {
271 }
272 
273 
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.
280  */
281 
282 void
283 arch_restore_fork_frame(struct arch_fork_arg *arg)
284 {
285 }
286 
287