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 <stdarg.h>
34 
35 /*indent:
36         indent -kr -i4 -ts4 -sob -l80 -ss -ncs -nut newlib-1.13.0/libgloss/sparc_leon/kernel*.c
37 	indent -kr -i4 -ts4 -sob -l80 -ss -ncs -nut *.h
38 	indent -kr -i4 -ts4 -sob -l80 -ss -ncs -nut *.c
39 */
40 
41 void leonbare_sched_printqueue ();
42 
43 int
leonbare_sched_verify()44 leonbare_sched_verify ()
45 {
46 #ifdef LBDEBUG_DO_ASSERT
47   int i, j;
48   leonbare_thread_t c, d;
49   for (i = 0; i < LEONBARE_RUNQ_NR; i++)
50     {
51       LBTAILQ_FOREACH (c, LEONBARE_KR_RUNQ (i), th_runq)
52       {
53 	if (i < LEONBARE_RUNQ_READY_NR)
54 	  {
55 	    LBPASSERT (c->th_runq_idx == i,
56 		       "thread %s has wrong runq[%d] index (%d) ",
57 		       LEONBARE_TH_NAME_DBG (c), i, c->th_runq_idx);
58 	    LBPASSERT (c->th_runq_which == LEONBARE_KR_RUNQ_WHICH,
59 		       "thread %s in runqueue[%d] has wrong th_runq_which(%d) (!=LEONBARE_KR_RUNQ_WHICH(%d))",
60 		       LEONBARE_TH_NAME_DBG (c), i, c->th_runq_which,
61 		       LEONBARE_KR_RUNQ_WHICH);
62 	  }
63 	else if (i == LEONBARE_RUNQ_SUSPENDED_IDX)
64 	  {
65 	    LBPASSERT (c->th_flags & LEONBARE_TH_SUSPENDED,
66 		       "thread %s in suspension queue has LEONBARE_TH_SUSPENDED not set ",
67 		       LEONBARE_TH_NAME_DBG (c));
68 	  }
69 	else if (i == LEONBARE_RUNQ_KILLED_IDX)
70 	  {
71 	    LBPASSERT (c->
72 		       th_flags & (LEONBARE_TH_TERMINATED |
73 				   LEONBARE_TH_FINISHED),
74 		       "thread %s in killed queue has (LEONBARE_TH_TERMINATED | LEONBARE_TH_FINISHED) not set ",
75 		       LEONBARE_TH_NAME_DBG (c));
76 	  }
77 	else if (i >= LEONBARE_RUNQ_PREPARE_IDX &&
78 		 i < (LEONBARE_RUNQ_PREPARE_IDX + LEONBARE_RUNQ_READY_NR))
79 	  {
80 	    LBPASSERT (c->th_runq_idx == (i - LEONBARE_RUNQ_PREPARE_IDX),
81 		       "thread %s has wrong prepare-runq[%d] index (%d) ",
82 		       LEONBARE_TH_NAME_DBG (c),
83 		       i - LEONBARE_RUNQ_PREPARE_IDX, c->th_runq_idx);
84 	    LBPASSERT (c->th_runq_which != LEONBARE_KR_RUNQ_WHICH,
85 		       "thread %s in prepare-runqueue[%d] has wrong th_runq_which(%d) (==LEONBARE_KR_RUNQ_WHICH(%d))",
86 		       LEONBARE_TH_NAME_DBG (c), i, c->th_runq_which,
87 		       LEONBARE_KR_RUNQ_WHICH);
88 	  }
89 
90 	if (i != LEONBARE_RUNQ_KILLED_IDX)
91 	  {
92 	    LBPASSERT (!
93 		       (c->
94 			th_flags & (LEONBARE_TH_TERMINATED |
95 				    LEONBARE_TH_FINISHED)),
96 		       "thread %s not in killed queue has (LEONBARE_TH_TERMINATED | LEONBARE_TH_FINISHED) set ",
97 		       LEONBARE_TH_NAME_DBG (c));
98 	  }
99 	if (i != LEONBARE_RUNQ_SUSPENDED_IDX)
100 	  {
101 	    LBPASSERT (!(c->th_flags & (LEONBARE_TH_SUSPENDED)),
102 		       "thread %s not in suspend queue has LEONBARE_TH_SUSPENDED set ",
103 		       LEONBARE_TH_NAME_DBG (c));
104 	  }
105 
106 	if (LBTAILQ_NEXT (c, th_runq))
107 	  {
108 	    LBPASSERT (c->th_account <=
109 		       LBTAILQ_NEXT (c, th_runq)->th_account,
110 		       "thread %s account is not sorted (%d<=%d)",
111 		       LEONBARE_TH_NAME_DBG (c), c->th_account,
112 		       LBTAILQ_NEXT (c, th_runq)->th_account);
113 	  }
114       }
115     }
116   LBTAILQ_FOREACH (c, LEONBARE_KR_ALLQ, th_allq)
117   {
118     if ((j = c->th_runq_idx) != -1)
119       {
120 	LBPASSERT (j >= 0
121 		   && j < LEONBARE_RUNQ_NR,
122 		   "thread %s has wrong runq index (%d) ",
123 		   LEONBARE_TH_NAME_DBG (c), c->th_runq_idx);
124 	LBTAILQ_FOREACH (d, LEONBARE_KR_RUNQ (j), th_runq)
125 	{
126 	  if (d == c)
127 	    {
128 	      break;
129 	    }
130 	}
131 	/*LBPASSERT(d,"thread %s is not in runq[%d] ",LEONBARE_TH_NAME_DBG(c),j); */
132       }
133   }
134 #endif
135 }
136 
137 int
leonbare_debug_printf(const char * fmt,...)138 leonbare_debug_printf (const char *fmt, ...)
139 {
140   va_list ap;
141   va_start (ap, fmt);
142   vprintf (fmt, ap);
143   va_end (ap);
144   return 0;
145 }
146 
147 void
leonbare_sched_printqueue()148 leonbare_sched_printqueue ()
149 {
150   int i, j;
151   leonbare_thread_t c;
152   for (i = 0; i < LEONBARE_RUNQ_NR; i++)
153     {
154       LBDEBUG_HEADER_PRINTF (LBDEBUG_QUEUE_NR, "runq[%d]:[", i);
155       LBTAILQ_FOREACH (c, LEONBARE_KR_RUNQ (i), th_runq)
156       {
157 	LBDEBUG (LBDEBUG_QUEUE_NR, "%s[0x%x](%d),", LEONBARE_TH_NAME_DBG (c),
158 		 c, c->th_account);
159       }
160       LBDEBUG (LBDEBUG_QUEUE_NR, "]\n", 0);
161     }
162 }
163