blob: 41410bd81abdc589040c3d777d85031409c55beb [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 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 <debug.h>
24#include <trace.h>
25#include <sys/types.h>
26#include <string.h>
27#include <stdlib.h>
28#include <kernel/thread.h>
29#include <arch/microblaze.h>
30
31#define LOCAL_TRACE 0
32
33struct thread *_current_thread;
34
35static void initial_thread_func(void) __NO_RETURN;
36static void initial_thread_func(void)
37{
38 thread_t *ct = get_current_thread();
39
40#if LOCAL_TRACE
41 LTRACEF("thread %p calling %p with arg %p\n", ct, ct->entry, ct->arg);
42 dump_thread(ct);
43#endif
44
45 /* release the thread lock that was implicitly held across the reschedule */
46 spin_unlock(&thread_lock);
47 arch_enable_ints();
48
49 int ret = ct->entry(ct->arg);
50
51 LTRACEF("thread %p exiting with %d\n", ct, ret);
52
53 thread_exit(ret);
54}
55
56void arch_thread_initialize(thread_t *t)
57{
58 LTRACEF("t %p (%s)\n", t, t->name);
59
60 /* some registers we want to clone for the new thread */
61 register uint32_t r2 asm("r2");
62 register uint32_t r13 asm("r13");
63
64 /* zero out the thread context */
65 memset(&t->arch.cs_frame, 0, sizeof(t->arch.cs_frame));
66
67 t->arch.cs_frame.r1 = (vaddr_t)t->stack + t->stack_size;
68 t->arch.cs_frame.r2 = r2;
69 t->arch.cs_frame.r13 = r13;
70 t->arch.cs_frame.r15 = (vaddr_t)&initial_thread_func;
71 // NOTE: appears to be bug in binutils 2.25 that forces us to -8 from the offset
72 // using this method if gc-sections is enabled.
73 *(volatile uint32_t *)&t->arch.cs_frame.r15 -= 8;
74}
75
76void arch_context_switch(thread_t *oldthread, thread_t *newthread)
77{
78 LTRACEF("old %p (%s), new %p (%s)\n", oldthread, oldthread->name, newthread, newthread->name);
79
80 microblaze_context_switch(&oldthread->arch.cs_frame, &newthread->arch.cs_frame);
81}
82
83void arch_dump_thread(thread_t *t)
84{
85 if (t->state != THREAD_RUNNING) {
86 dprintf(INFO, "\tarch: ");
87 dprintf(INFO, "sp 0x%x\n", t->arch.cs_frame.r1);
88 }
89}
90