blob: df776db3148043e4473293312d1961f399b38738 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 Stefan Kristiansson
3 * Based on arch/microblaze/thread.c
4 * Copyright (c) 2015 Travis Geiselbrecht
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction,
9 * including without limitation the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25#include <debug.h>
26#include <trace.h>
27#include <sys/types.h>
28#include <string.h>
29#include <stdlib.h>
30#include <kernel/thread.h>
31
32#define LOCAL_TRACE 0
33
34struct thread *_current_thread;
35
36static void initial_thread_func(void) __NO_RETURN;
37static void initial_thread_func(void)
38{
39 thread_t *ct = get_current_thread();
40
41#if LOCAL_TRACE
42 LTRACEF("thread %p calling %p with arg %p\n", ct, ct->entry, ct->arg);
43 dump_thread(ct);
44#endif
45
46 /* exit the implicit critical section we're within */
47 spin_unlock(&thread_lock);
48 arch_enable_ints();
49
50 int ret = ct->entry(ct->arg);
51
52 LTRACEF("thread %p exiting with %d\n", ct, ret);
53
54 thread_exit(ret);
55}
56
57void arch_thread_initialize(thread_t *t)
58{
59 LTRACEF("t %p (%s)\n", t, t->name);
60
61 /* some registers we want to clone for the new thread */
62 register uint32_t r2 asm("r2");
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.r9 = (vaddr_t)initial_thread_func;
70}
71
72void arch_context_switch(thread_t *oldthread, thread_t *newthread)
73{
74 LTRACEF("old %p (%s), new %p (%s)\n", oldthread, oldthread->name, newthread, newthread->name);
75
76 or1k_context_switch(&oldthread->arch.cs_frame, &newthread->arch.cs_frame);
77}
78
79void arch_dump_thread(thread_t *t)
80{
81 if (t->state != THREAD_RUNNING) {
82 dprintf(INFO, "\tarch: ");
83 dprintf(INFO, "sp 0x%x\n", t->arch.cs_frame.r1);
84 }
85}