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 <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 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 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 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(Thread *from, 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(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(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 void 150 arch_thread_init_kthread_stack(Thread* thread, void* _stack, void* _stackTop, 151 void (*function)(void*), const void* data) 152 { 153 #if 0 154 addr_t *kstack = (addr_t *)t->kernel_stack_base; 155 addr_t *kstackTop = (addr_t *)t->kernel_stack_base; 156 157 // clear the kernel stack 158 #ifdef DEBUG_KERNEL_STACKS 159 # ifdef STACK_GROWS_DOWNWARDS 160 memset((void *)((addr_t)kstack + KERNEL_STACK_GUARD_PAGES * B_PAGE_SIZE), 0, 161 KERNEL_STACK_SIZE); 162 # else 163 memset(kstack, 0, KERNEL_STACK_SIZE); 164 # endif 165 #else 166 memset(kstack, 0, KERNEL_STACK_SIZE); 167 #endif 168 169 // space for frame pointer and return address, and stack frames must be 170 // 16 byte aligned 171 kstackTop -= 2; 172 kstackTop = (addr_t*)((addr_t)kstackTop & ~0xf); 173 174 // LR, CR, r2, r13-r31, f13-f31, as pushed by m68k_context_switch() 175 kstackTop -= 22 + 2 * 19; 176 177 // let LR point to m68k_kernel_thread_root() 178 kstackTop[0] = (addr_t)&m68k_kernel_thread_root; 179 180 // the arguments of m68k_kernel_thread_root() are the functions to call, 181 // provided in registers r13-r15 182 kstackTop[3] = (addr_t)entry_func; 183 kstackTop[4] = (addr_t)start_func; 184 kstackTop[5] = (addr_t)exit_func; 185 186 // save this stack position 187 t->arch_info.sp = (void *)kstackTop; 188 189 return B_OK; 190 #else 191 panic("arch_thread_init_kthread_stack(): Implement me!"); 192 #endif 193 } 194 195 196 status_t 197 arch_thread_init_tls(Thread *thread) 198 { 199 // TODO: Implement! 200 return B_OK; 201 } 202 203 204 void 205 arch_thread_context_switch(Thread *from, Thread *to) 206 { 207 addr_t newPageDirectory; 208 209 newPageDirectory = (addr_t)m68k_next_page_directory(from, to); 210 211 if ((newPageDirectory % B_PAGE_SIZE) != 0) 212 panic("arch_thread_context_switch: bad pgdir 0x%lx\n", newPageDirectory); 213 #warning M68K: export from arch_vm.c 214 m68k_set_pgdir((void *)newPageDirectory); 215 m68k_context_switch(&from->arch_info.sp, to->arch_info.sp); 216 } 217 218 219 void 220 arch_thread_dump_info(void *info) 221 { 222 struct arch_thread *at = (struct arch_thread *)info; 223 224 dprintf("\tsp: %p\n", at->sp); 225 } 226 227 228 status_t 229 arch_thread_enter_userspace(Thread *thread, addr_t entry, void *arg1, void *arg2) 230 { 231 panic("arch_thread_enter_uspace(): not yet implemented\n"); 232 return B_ERROR; 233 } 234 235 236 bool 237 arch_on_signal_stack(Thread *thread) 238 { 239 return false; 240 } 241 242 243 status_t 244 arch_setup_signal_frame(Thread *thread, struct sigaction *sa, 245 struct signal_frame_data *signalFrameData) 246 { 247 return B_ERROR; 248 } 249 250 251 int64 252 arch_restore_signal_frame(struct signal_frame_data* signalFrameData) 253 { 254 return 0; 255 } 256 257 258 void 259 arch_check_syscall_restart(Thread *thread) 260 { 261 } 262 263 264 /** Saves everything needed to restore the frame in the child fork in the 265 * arch_fork_arg structure to be passed to arch_restore_fork_frame(). 266 * Also makes sure to return the right value. 267 */ 268 269 void 270 arch_store_fork_frame(struct arch_fork_arg *arg) 271 { 272 } 273 274 275 /** Restores the frame from a forked team as specified by the provided 276 * arch_fork_arg structure. 277 * Needs to be called from within the child team, ie. instead of 278 * arch_thread_enter_uspace() as thread "starter". 279 * This function does not return to the caller, but will enter userland 280 * in the child team at the same position where the parent team left of. 281 */ 282 283 void 284 arch_restore_fork_frame(struct arch_fork_arg *arg) 285 { 286 } 287 288