vfs: check userland buffers before reading them.
[haiku.git] / src / system / kernel / arch / m68k / arch_thread.cpp
blob892c527371f90a0f4be8150c0a8c5369cd5a3214
1 /*
2 * Copyright 2003-2011, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
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>
10 * Copyright 2001, Travis Geiselbrecht. All rights reserved.
11 * Distributed under the terms of the NewOS License.
15 #include <arch_thread.h>
17 #include <string.h>
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>
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();
45 void
46 m68k_push_iframe(struct iframe_stack *stack, struct iframe *frame)
48 ASSERT(stack->index < IFRAME_TRACE_DEPTH);
49 stack->frames[stack->index++] = frame;
53 void
54 m68k_pop_iframe(struct iframe_stack *stack)
56 ASSERT(stack->index > 0);
57 stack->index--;
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).
82 struct iframe *
83 m68k_get_user_iframe(void)
85 Thread *thread = thread_get_current_thread();
86 int i;
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;
94 return NULL;
98 uint32
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
104 return 0;
107 if (toAddressSpace == NULL)
108 toAddressSpace = VMAddressSpace::Kernel();
110 return static_cast<M68KVMTranslationMap*>(toAddressSpace->TranslationMap())
111 ->PagingStructures()->pgroot_phys;
114 // #pragma mark -
117 status_t
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.
123 return B_OK;
127 status_t
128 arch_team_init_team_struct(Team *team, bool kernel)
130 // Nothing to do. The structure is empty.
131 return B_OK;
135 status_t
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));
141 return B_OK;
145 void
146 arch_thread_init_kthread_stack(Thread* thread, void* _stack, void* _stackTop,
147 void (*function)(void*), const void* data)
149 #if 0
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,
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
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);
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;
185 return B_OK;
186 #else
187 panic("arch_thread_init_kthread_stack(): Implement me!");
188 #endif
192 status_t
193 arch_thread_init_tls(Thread *thread)
195 // TODO: Implement!
196 return B_OK;
200 void
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);
218 void
219 arch_thread_dump_info(void *info)
221 struct arch_thread *at = (struct arch_thread *)info;
223 dprintf("\tsp: %p\n", at->sp);
227 status_t
228 arch_thread_enter_userspace(Thread *thread, addr_t entry, void *arg1, void *arg2)
230 panic("arch_thread_enter_uspace(): not yet implemented\n");
231 return B_ERROR;
235 bool
236 arch_on_signal_stack(Thread *thread)
238 return false;
242 status_t
243 arch_setup_signal_frame(Thread *thread, struct sigaction *sa,
244 struct signal_frame_data *signalFrameData)
246 return B_ERROR;
250 int64
251 arch_restore_signal_frame(struct signal_frame_data* signalFrameData)
253 return 0;
257 void
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.
268 void
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.
282 void
283 arch_restore_fork_frame(struct arch_fork_arg *arg)