| /* |
| * Copyright (c) 2015 Travis Geiselbrecht |
| * |
| * Permission is hereby granted, free of charge, to any person obtaining |
| * a copy of this software and associated documentation files |
| * (the "Software"), to deal in the Software without restriction, |
| * including without limitation the rights to use, copy, modify, merge, |
| * publish, distribute, sublicense, and/or sell copies of the Software, |
| * and to permit persons to whom the Software is furnished to do so, |
| * subject to the following conditions: |
| * |
| * The above copyright notice and this permission notice shall be |
| * included in all copies or substantial portions of the Software. |
| * |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| */ |
| #include <debug.h> |
| #include <trace.h> |
| #include <sys/types.h> |
| #include <string.h> |
| #include <stdlib.h> |
| #include <kernel/thread.h> |
| #include <arch/mips.h> |
| |
| #define LOCAL_TRACE 0 |
| |
| struct thread *_current_thread; |
| |
| static void initial_thread_func(void) __NO_RETURN; |
| static void initial_thread_func(void) |
| { |
| thread_t *ct = get_current_thread(); |
| |
| #if LOCAL_TRACE |
| LTRACEF("thread %p calling %p with arg %p\n", ct, ct->entry, ct->arg); |
| dump_thread(ct); |
| #endif |
| |
| /* release the thread lock that was implicitly held across the reschedule */ |
| spin_unlock(&thread_lock); |
| arch_enable_ints(); |
| |
| int ret = ct->entry(ct->arg); |
| |
| LTRACEF("thread %p exiting with %d\n", ct, ret); |
| |
| thread_exit(ret); |
| } |
| |
| void arch_thread_initialize(thread_t *t) |
| { |
| LTRACEF("t %p (%s)\n", t, t->name); |
| |
| /* zero out the thread context */ |
| memset(&t->arch.cs_frame, 0, sizeof(t->arch.cs_frame)); |
| |
| t->arch.cs_frame.ra = (vaddr_t)&initial_thread_func; |
| t->arch.cs_frame.sp = (vaddr_t)t->stack + t->stack_size; |
| } |
| |
| void arch_context_switch(thread_t *oldthread, thread_t *newthread) |
| { |
| LTRACEF("old %p (%s), new %p (%s)\n", oldthread, oldthread->name, newthread, newthread->name); |
| |
| mips_context_switch(&oldthread->arch.cs_frame, &newthread->arch.cs_frame); |
| } |
| |
| void arch_dump_thread(thread_t *t) |
| { |
| if (t->state != THREAD_RUNNING) { |
| dprintf(INFO, "\tarch: "); |
| dprintf(INFO, "sp 0x%x\n", t->arch.cs_frame.sp); |
| } |
| } |
| |