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