Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <sys/types.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <debug.h> |
| 27 | #include <kernel/thread.h> |
| 28 | #include <arch/arm.h> |
| 29 | |
| 30 | struct context_switch_frame { |
| 31 | vaddr_t r4; |
| 32 | vaddr_t r5; |
| 33 | vaddr_t r6; |
| 34 | vaddr_t r7; |
| 35 | vaddr_t r8; |
| 36 | vaddr_t r9; |
| 37 | vaddr_t r10; |
| 38 | vaddr_t r11; |
| 39 | vaddr_t lr; |
| 40 | vaddr_t usp; |
| 41 | vaddr_t ulr; |
| 42 | }; |
| 43 | |
| 44 | extern void arm_context_switch(addr_t *old_sp, addr_t new_sp); |
| 45 | |
| 46 | static void initial_thread_func(void) __NO_RETURN; |
| 47 | static void initial_thread_func(void) |
| 48 | { |
| 49 | int ret; |
| 50 | |
| 51 | // dprintf("initial_thread_func: thread %p calling %p with arg %p\n", current_thread, current_thread->entry, current_thread->arg); |
| 52 | // dump_thread(current_thread); |
| 53 | |
| 54 | /* exit the implicit critical section we're within */ |
| 55 | exit_critical_section(); |
| 56 | |
| 57 | ret = current_thread->entry(current_thread->arg); |
| 58 | |
Travis Geiselbrecht | 887061f | 2008-09-05 01:47:07 -0700 | [diff] [blame] | 59 | // dprintf("initial_thread_func: thread %p exiting with %d\n", current_thread, ret); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 60 | |
| 61 | thread_exit(ret); |
| 62 | } |
| 63 | |
| 64 | void arch_thread_initialize(thread_t *t) |
| 65 | { |
| 66 | // create a default stack frame on the stack |
| 67 | vaddr_t stack_top = (vaddr_t)t->stack + t->stack_size; |
| 68 | |
| 69 | // make sure the top of the stack is 8 byte aligned for EABI compliance |
| 70 | stack_top = ROUNDDOWN(stack_top, 8); |
| 71 | |
| 72 | struct context_switch_frame *frame = (struct context_switch_frame *)(stack_top); |
| 73 | frame--; |
| 74 | |
| 75 | // fill it in |
| 76 | memset(frame, 0, sizeof(*frame)); |
| 77 | frame->lr = (vaddr_t)&initial_thread_func; |
| 78 | |
| 79 | // set the stack pointer |
| 80 | t->arch.sp = (vaddr_t)frame; |
| 81 | } |
| 82 | |
| 83 | void arch_context_switch(thread_t *oldthread, thread_t *newthread) |
| 84 | { |
| 85 | // dprintf("arch_context_switch: old %p (%s), new %p (%s)\n", oldthread, oldthread->name, newthread, newthread->name); |
| 86 | arm_context_switch(&oldthread->arch.sp, newthread->arch.sp); |
| 87 | } |
| 88 | |