source: trunk/libs/newlib/src/libgloss/sparc_leon/kernel_thread.c @ 668

Last change on this file since 668 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 6.2 KB
Line 
1/*
2 * Copyright (c) 2011 Aeroflex Gaisler
3 *
4 * BSD license:
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25
26#include <asm-leon/queue.h>
27/*#include <sys/fsu_pthread_queue.h>*/
28#include <asm-leon/contextswitch.h>
29#include <asm-leon/leonbare_kernel.h>
30#include <asm-leon/leonbare_debug.h>
31#include <asm-leon/stack.h>
32#include <asm-leon/leonstack.h>
33#include <asm-leon/irq.h>
34
35int
36leonbare_thread_resume (leonbare_thread_t thread)
37{
38  leonbare_thread_t i = 0;
39  if ((thread->th_flags & LEONBARE_TH_SUSPENDED) &&
40      !(thread->th_flags & (LEONBARE_TH_TERMINATED | LEONBARE_TH_FINISHED)))
41    {
42
43      LEONBARE_PROTECT_KERNEL_START ();
44      {
45        unsigned int idx = leonbare_thread_getqueueidx (thread);
46
47        if (idx != -1)
48          {
49            LBTAILQ_REMOVE (LEONBARE_KR_RUNQ (idx), thread, th_runq);
50          }
51
52        if (thread->th_pri_idx != -1)
53          {
54            thread->th_runq_idx = thread->th_pri_idx;
55            thread->th_runq_which = LEONBARE_KR_RUNQ_WHICH;
56            LBTAILQ_INSERT_HEAD (LEONBARE_KR_RUNQ (thread->th_runq_idx),
57                                 thread, th_runq);
58            LEONBARE_TH_SETSTATE (thread, LEONBARE_TH_READY);
59          }
60      }
61      LEONBARE_PROTECT_KERNEL_END ();
62    }
63
64}
65
66int
67leonbare_thread_terminate (leonbare_thread_t thread)
68{
69  unsigned int ret = 0;
70  leonbare_thread_t c = LEONBARE_KR_CURRENT;
71  LEONBARE_PROTECT_KERNEL_START ();
72  {
73    unsigned int idx = leonbare_thread_getqueueidx (thread);
74
75    if (LEONBARE_RUNQ_ISREADY (idx) || LEONBARE_RUNQ_ISPREPARE (idx) ||
76        LEONBARE_RUNQ_ISSUSPEND (idx))
77      {
78        LBTAILQ_REMOVE (LEONBARE_KR_RUNQ (idx), thread, th_runq);
79      }
80    else
81      {
82        LBPASSERT (LEONBARE_RUNQ_ISKILLED (idx),
83                   "thread %s is in no queue ",
84                   LEONBARE_TH_NAME_DBG (thread));
85      }
86    LEONBARE_TH_SETSTATE (thread, LEONBARE_TH_TERMINATED);
87    LBTAILQ_INSERT_HEAD (LEONBARE_KR_RUNQ (LEONBARE_RUNQ_KILLED_IDX), thread,
88                         th_runq);
89
90    LEONBARE_PRINTQUEUES ();
91
92    LEONBARE_VERIFYSCHED ();
93  }
94  if (thread == LEONBARE_KR_CURRENT)
95    {
96      ret = reschedule ();
97      /* never come here */
98      LEONBARE_STOPALL;
99    }
100  LEONBARE_PROTECT_KERNEL_END ();
101  return ret;
102}
103
104
105int
106current_suspend ()
107{
108  unsigned int ret = 0;
109  leonbare_thread_t c = LEONBARE_KR_CURRENT;
110  LEONBARE_PROTECT_KERNEL_START ();
111  {
112    LBPASSERT ((c->th_runq_which == LEONBARE_KR_RUNQ_WHICH),
113               "Current thread cannot be on the prepare queue", 0);
114
115    LBTAILQ_REMOVE (LEONBARE_KR_RUNQ (c->th_runq_idx), c, th_runq);
116    LBTAILQ_INSERT_TAIL (LEONBARE_KR_RUNQ (LEONBARE_RUNQ_SUSPENDED_IDX),
117                         c, th_runq);
118    c->th_runq_idx = LEONBARE_RUNQ_SUSPENDED_IDX;
119    LEONBARE_TH_SETSTATE (c, LEONBARE_TH_SUSPENDED);
120    LEONBARE_VERIFYSCHED ();
121  }
122  ret = reschedule ();
123  LEONBARE_PROTECT_KERNEL_END ();
124  return ret;
125}
126
127void
128_leonbare_thread_body ()
129{
130  LBDEBUG_FNCALL;
131  leonbare_thread_t thread = LEONBARE_KR_CURRENT;
132
133  LEONBARE_KR_IS_IN_KERNEL = 0;
134  thread->th_result = thread->th_func (thread->th_arg);
135
136  leonbare_thread_terminate (thread);
137
138  LBDEBUG_FNEXIT;
139}
140
141#define LEONBARE_BODY_OFFSET 200
142
143int
144leonbare_thread_create (struct leonbare_thread *thread, char *stack,
145                        int stacksize)
146{
147  LEONBARE_PROTECT_DECL (flags);
148  struct sparc_stackframe_regs *sp;
149  unsigned int v;
150  unsigned int fpspill, bodysp, bodyfp;
151  struct leonbare_thread_ctx *threadctx;
152  LBDEBUG_FNCALL;
153
154  thread->th_stack_base = (char *) LEONBARE_STACKALIGN (stack);
155  stacksize -= thread->th_stack_base - stack;
156  thread->th_stack_size = stacksize;
157  thread->th_runq_idx = 0;
158  thread->th_pri_idx = 0;
159  thread->th_account = 0;
160  thread->th_caccount = 0;
161
162  /* stack:
163   * 0:+--------------------------------+ <- thread.th_stack_base
164   *   |   ....                         |
165   *   +--------------------------------+ <- thread.th_ctx->out[6] (%sp)
166   *   |  _leonbare_thread_body() frame |
167   *   +--------------------------------+ <- thread.th_ctx->sf_ins[6]  (%fp)
168   *   |           WINDOWSPILL          |
169   *   +--------------------------------+ <- thread.th_ctx->fpu 
170   *   |  struct sparc_fpuwindow_regs   |
171   *   +--------------------------------+ <- thread.th_stack_base + thread->th_stack_size
172   *
173   */
174  v = (unsigned int) (thread->th_stack_base +
175                      LEONBARE_STACKALIGN (thread->th_stack_size -
176                                           (LEONBARE_BODY_OFFSET +
177                                            WINDOWSIZE + FW_REGS_SZ)));
178
179  bodysp = ((unsigned int) v);
180  bodyfp = ((unsigned int) bodysp) + LEONBARE_BODY_OFFSET;
181  fpspill = ((unsigned int) bodyfp) + WINDOWSIZE;
182
183  thread->th_ctx.outs[6] = (unsigned int) bodysp;
184  thread->th_ctx.outs[7] = (int) _leonbare_thread_body;
185  thread->th_ctx.outs[7] -= 8;
186  thread->th_ctx.sf_ins[6] = (unsigned int) bodyfp;
187  thread->th_ctx.fpu = (unsigned int) fpspill;
188  thread->th_ctx.magic = LEONBARE_THREAD_CTX_MAGIC;
189
190  thread->th_ctx.psr = 0x0e0;
191  thread->th_ctx.wim = 0x2;
192
193  LBDEBUG_HEADER_PRINTF (LBDEBUG_THREAD_NR,
194                         "Thread %s (0x%x): stack [0x%x-0x%x] \n",
195                         LEONBARE_TH_NAME_DBG (thread), thread,
196                         thread->th_stack_base,
197                         thread->th_stack_base + thread->th_stack_size);
198
199  /* newlibc reent */
200  thread->th_reentp = &(thread->th_reent);
201  _REENT_INIT_PTR (thread->th_reentp);
202
203  LEONBARE_PROTECT_KERNEL_START ();
204  /* queues */
205  LBTAILQ_INSERT_TAIL (LEONBARE_KR_ALLQ, thread, th_allq);
206  LBTAILQ_INSERT_TAIL (LEONBARE_KR_RUNQ (thread->th_runq_idx), thread,
207                       th_runq);
208
209  LEONBARE_PRINTQUEUES ();
210
211  LEONBARE_PROTECT_KERNEL_END ();
212
213  LBDEBUG_FNEXIT;
214}
Note: See TracBrowser for help on using the repository browser.