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