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/vm_types.h> 22 #include <vm/VMAddressSpace.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( 106 &to->team->address_space->TranslationMap()); 107 } else if (from->team->address_space == NULL && to->team->address_space == NULL) { 108 // they must both be kernel space threads 109 return NULL; 110 } else if (to->team->address_space == NULL) { 111 // the one we're switching to is kernel space 112 return m68k_translation_map_get_pgdir( 113 &VMAddressSpace::Kernel()->TranslationMap()); 114 } 115 116 return m68k_translation_map_get_pgdir( 117 &to->team->address_space->TranslationMap()); 118 } 119 120 // #pragma mark - 121 122 123 status_t 124 arch_thread_init(struct kernel_args *args) 125 { 126 // Initialize the static initial arch_thread state (sInitialState). 127 // Currently nothing to do, i.e. zero initialized is just fine. 128 129 return B_OK; 130 } 131 132 133 status_t 134 arch_team_init_team_struct(struct team *team, bool kernel) 135 { 136 // Nothing to do. The structure is empty. 137 return B_OK; 138 } 139 140 141 status_t 142 arch_thread_init_thread_struct(struct thread *thread) 143 { 144 // set up an initial state (stack & fpu) 145 memcpy(&thread->arch_info, &sInitialState, sizeof(struct arch_thread)); 146 147 return B_OK; 148 } 149 150 151 status_t 152 arch_thread_init_kthread_stack(struct thread *t, int (*start_func)(void), 153 void (*entry_func)(void), void (*exit_func)(void)) 154 { 155 addr_t *kstack = (addr_t *)t->kernel_stack_base; 156 addr_t *kstackTop = (addr_t *)t->kernel_stack_base; 157 158 // clear the kernel stack 159 #ifdef DEBUG_KERNEL_STACKS 160 # ifdef STACK_GROWS_DOWNWARDS 161 memset((void *)((addr_t)kstack + KERNEL_STACK_GUARD_PAGES * B_PAGE_SIZE), 0, 162 KERNEL_STACK_SIZE); 163 # else 164 memset(kstack, 0, KERNEL_STACK_SIZE); 165 # endif 166 #else 167 memset(kstack, 0, KERNEL_STACK_SIZE); 168 #endif 169 170 // space for frame pointer and return address, and stack frames must be 171 // 16 byte aligned 172 kstackTop -= 2; 173 kstackTop = (addr_t*)((addr_t)kstackTop & ~0xf); 174 175 // LR, CR, r2, r13-r31, f13-f31, as pushed by m68k_context_switch() 176 kstackTop -= 22 + 2 * 19; 177 178 // let LR point to m68k_kernel_thread_root() 179 kstackTop[0] = (addr_t)&m68k_kernel_thread_root; 180 181 // the arguments of m68k_kernel_thread_root() are the functions to call, 182 // provided in registers r13-r15 183 kstackTop[3] = (addr_t)entry_func; 184 kstackTop[4] = (addr_t)start_func; 185 kstackTop[5] = (addr_t)exit_func; 186 187 // save this stack position 188 t->arch_info.sp = (void *)kstackTop; 189 190 return B_OK; 191 } 192 193 194 status_t 195 arch_thread_init_tls(struct thread *thread) 196 { 197 // TODO: Implement! 198 return B_OK; 199 } 200 201 202 void 203 arch_thread_switch_kstack_and_call(struct thread *t, addr_t newKstack, 204 void (*func)(void *), void *arg) 205 { 206 m68k_switch_stack_and_call(newKstack, func, arg); 207 } 208 209 210 void 211 arch_thread_context_switch(struct thread *from, struct thread *to) 212 { 213 addr_t newPageDirectory; 214 215 newPageDirectory = (addr_t)m68k_next_page_directory(from, to); 216 217 if ((newPageDirectory % B_PAGE_SIZE) != 0) 218 panic("arch_thread_context_switch: bad pgdir 0x%lx\n", newPageDirectory); 219 #warning M68K: export from arch_vm.c 220 m68k_set_pgdir((void *)newPageDirectory); 221 m68k_context_switch(&from->arch_info.sp, to->arch_info.sp); 222 } 223 224 225 void 226 arch_thread_dump_info(void *info) 227 { 228 struct arch_thread *at = (struct arch_thread *)info; 229 230 dprintf("\tsp: %p\n", at->sp); 231 } 232 233 234 status_t 235 arch_thread_enter_userspace(struct thread *thread, addr_t entry, void *arg1, void *arg2) 236 { 237 panic("arch_thread_enter_uspace(): not yet implemented\n"); 238 return B_ERROR; 239 } 240 241 242 bool 243 arch_on_signal_stack(struct thread *thread) 244 { 245 return false; 246 } 247 248 249 status_t 250 arch_setup_signal_frame(struct thread *thread, struct sigaction *sa, int sig, int sigMask) 251 { 252 return B_ERROR; 253 } 254 255 256 int64 257 arch_restore_signal_frame(void) 258 { 259 return 0; 260 } 261 262 263 void 264 arch_check_syscall_restart(struct thread *thread) 265 { 266 } 267 268 269 /** Saves everything needed to restore the frame in the child fork in the 270 * arch_fork_arg structure to be passed to arch_restore_fork_frame(). 271 * Also makes sure to return the right value. 272 */ 273 274 void 275 arch_store_fork_frame(struct arch_fork_arg *arg) 276 { 277 } 278 279 280 /** Restores the frame from a forked team as specified by the provided 281 * arch_fork_arg structure. 282 * Needs to be called from within the child team, ie. instead of 283 * arch_thread_enter_uspace() as thread "starter". 284 * This function does not return to the caller, but will enter userland 285 * in the child team at the same position where the parent team left of. 286 */ 287 288 void 289 arch_restore_fork_frame(struct arch_fork_arg *arg) 290 { 291 } 292 293