sched.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <common.h>
  3. #include <exports.h>
  4. /*
  5. * Author: Arun Dharankar <ADharankar@ATTBI.Com>
  6. *
  7. * A very simple thread/schedular model:
  8. * - only one master thread, and no parent child relation maintained
  9. * - parent thread cannot be stopped or deleted
  10. * - no permissions or credentials
  11. * - no elaborate safety checks
  12. * - cooperative multi threading
  13. * - Simple round-robin scheduleing with no priorities
  14. * - no metering/statistics collection
  15. *
  16. * Basic idea of implementing this is to allow more than one tests to
  17. * execute "simultaneously".
  18. *
  19. * This may be modified such thread_yield may be called in syscalls, and
  20. * timer interrupts.
  21. */
  22. #define MAX_THREADS 8
  23. #define CTX_SIZE 512
  24. #define STK_SIZE 8*1024
  25. #define STATE_EMPTY 0
  26. #define STATE_RUNNABLE 1
  27. #define STATE_STOPPED 2
  28. #define STATE_TERMINATED 2
  29. #define MASTER_THREAD 0
  30. #define RC_FAILURE (-1)
  31. #define RC_SUCCESS (0)
  32. typedef vu_char *jmp_ctx;
  33. unsigned long setctxsp (vu_char *sp);
  34. int ppc_setjmp(jmp_ctx env);
  35. void ppc_longjmp(jmp_ctx env, int val);
  36. #define setjmp ppc_setjmp
  37. #define longjmp ppc_longjmp
  38. struct lthread {
  39. int state;
  40. int retval;
  41. char stack[STK_SIZE];
  42. uchar context[CTX_SIZE];
  43. int (*func) (void *);
  44. void *arg;
  45. };
  46. static volatile struct lthread lthreads[MAX_THREADS];
  47. static volatile int current_tid = MASTER_THREAD;
  48. static uchar dbg = 0;
  49. #define PDEBUG(fmt, args...) { \
  50. if(dbg != 0) { \
  51. printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);\
  52. printf(fmt, ##args); \
  53. printf("\n"); \
  54. } \
  55. }
  56. static int testthread (void *);
  57. static void sched_init (void);
  58. static int thread_create (int (*func) (void *), void *arg);
  59. static int thread_start (int id);
  60. static void thread_yield (void);
  61. static int thread_delete (int id);
  62. static int thread_join (int *ret);
  63. #if 0 /* not used yet */
  64. static int thread_stop (int id);
  65. #endif /* not used yet */
  66. /* An example of schedular test */
  67. #define NUMTHREADS 7
  68. int sched (int ac, char *av[])
  69. {
  70. int i, j;
  71. int tid[NUMTHREADS];
  72. int names[NUMTHREADS];
  73. app_startup(av);
  74. sched_init ();
  75. for (i = 0; i < NUMTHREADS; i++) {
  76. names[i] = i;
  77. j = thread_create (testthread, (void *) &names[i]);
  78. if (j == RC_FAILURE)
  79. printf ("schedtest: Failed to create thread %d\n", i);
  80. if (j > 0) {
  81. printf ("schedtest: Created thread with id %d, name %d\n",
  82. j, i);
  83. tid[i] = j;
  84. }
  85. }
  86. printf ("schedtest: Threads created\n");
  87. printf ("sched_test: function=0x%08x\n", (unsigned)testthread);
  88. for (i = 0; i < NUMTHREADS; i++) {
  89. printf ("schedtest: Setting thread %d runnable\n", tid[i]);
  90. thread_start (tid[i]);
  91. thread_yield ();
  92. }
  93. printf ("schedtest: Started %d threads\n", NUMTHREADS);
  94. while (1) {
  95. printf ("schedtest: Waiting for threads to complete\n");
  96. if (tstc () && getc () == 0x3) {
  97. printf ("schedtest: Aborting threads...\n");
  98. for (i = 0; i < NUMTHREADS; i++) {
  99. printf ("schedtest: Deleting thread %d\n", tid[i]);
  100. thread_delete (tid[i]);
  101. }
  102. return RC_SUCCESS;
  103. }
  104. j = -1;
  105. i = thread_join (&j);
  106. if (i == RC_FAILURE) {
  107. printf ("schedtest: No threads pending, "
  108. "exiting schedular test\n");
  109. return RC_SUCCESS;
  110. }
  111. printf ("schedtest: thread is %d returned %d\n", i, j);
  112. thread_yield ();
  113. }
  114. return RC_SUCCESS;
  115. }
  116. static int testthread (void *name)
  117. {
  118. int i;
  119. printf ("testthread: Begin executing thread, myname %d, &i=0x%08x\n",
  120. *(int *) name, (unsigned)&i);
  121. printf ("Thread %02d, i=%d\n", *(int *) name, i);
  122. for (i = 0; i < 0xffff * (*(int *) name + 1); i++) {
  123. if (tstc () && getc () == 0x3) {
  124. printf ("testthread: myname %d terminating.\n",
  125. *(int *) name);
  126. return *(int *) name + 1;
  127. }
  128. if (i % 100 == 0)
  129. thread_yield ();
  130. }
  131. printf ("testthread: returning %d, i=0x%x\n",
  132. *(int *) name + 1, i);
  133. return *(int *) name + 1;
  134. }
  135. static void sched_init (void)
  136. {
  137. int i;
  138. for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++)
  139. lthreads[i].state = STATE_EMPTY;
  140. current_tid = MASTER_THREAD;
  141. lthreads[current_tid].state = STATE_RUNNABLE;
  142. PDEBUG ("sched_init: master context = 0x%08x",
  143. (unsigned)lthreads[current_tid].context);
  144. return;
  145. }
  146. static void thread_yield (void)
  147. {
  148. static int i;
  149. PDEBUG ("thread_yield: current tid=%d", current_tid);
  150. #define SWITCH(new) \
  151. if(lthreads[new].state == STATE_RUNNABLE) { \
  152. PDEBUG("thread_yield: %d match, ctx=0x%08x", \
  153. new, \
  154. (unsigned)lthreads[current_tid].context); \
  155. if(setjmp(lthreads[current_tid].context) == 0) { \
  156. current_tid = new; \
  157. PDEBUG("thread_yield: tid %d returns 0", \
  158. new); \
  159. longjmp(lthreads[new].context, 1); \
  160. } else { \
  161. PDEBUG("thread_yield: tid %d returns 1", \
  162. new); \
  163. return; \
  164. } \
  165. }
  166. for (i = current_tid + 1; i < MAX_THREADS; i++) {
  167. SWITCH (i);
  168. }
  169. if (current_tid != 0) {
  170. for (i = 0; i <= current_tid; i++) {
  171. SWITCH (i);
  172. }
  173. }
  174. PDEBUG ("thread_yield: returning from thread_yield");
  175. return;
  176. }
  177. static int thread_create (int (*func) (void *), void *arg)
  178. {
  179. int i;
  180. for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
  181. if (lthreads[i].state == STATE_EMPTY) {
  182. lthreads[i].state = STATE_STOPPED;
  183. lthreads[i].func = func;
  184. lthreads[i].arg = arg;
  185. PDEBUG ("thread_create: returns new tid %d", i);
  186. return i;
  187. }
  188. }
  189. PDEBUG ("thread_create: returns failure");
  190. return RC_FAILURE;
  191. }
  192. static int thread_delete (int id)
  193. {
  194. if (id <= MASTER_THREAD || id > MAX_THREADS)
  195. return RC_FAILURE;
  196. if (current_tid == id)
  197. return RC_FAILURE;
  198. lthreads[id].state = STATE_EMPTY;
  199. return RC_SUCCESS;
  200. }
  201. static void thread_launcher (void)
  202. {
  203. PDEBUG ("thread_launcher: invoking func=0x%08x",
  204. (unsigned)lthreads[current_tid].func);
  205. lthreads[current_tid].retval =
  206. lthreads[current_tid].func (lthreads[current_tid].arg);
  207. PDEBUG ("thread_launcher: tid %d terminated", current_tid);
  208. lthreads[current_tid].state = STATE_TERMINATED;
  209. thread_yield ();
  210. printf ("thread_launcher: should NEVER get here!\n");
  211. return;
  212. }
  213. static int thread_start (int id)
  214. {
  215. PDEBUG ("thread_start: id=%d", id);
  216. if (id <= MASTER_THREAD || id > MAX_THREADS) {
  217. return RC_FAILURE;
  218. }
  219. if (lthreads[id].state != STATE_STOPPED)
  220. return RC_FAILURE;
  221. if (setjmp (lthreads[current_tid].context) == 0) {
  222. lthreads[id].state = STATE_RUNNABLE;
  223. current_tid = id;
  224. PDEBUG ("thread_start: to be stack=0%08x",
  225. (unsigned)lthreads[id].stack);
  226. setctxsp ((vu_char *)&lthreads[id].stack[STK_SIZE]);
  227. thread_launcher ();
  228. }
  229. PDEBUG ("thread_start: Thread id=%d started, parent returns", id);
  230. return RC_SUCCESS;
  231. }
  232. #if 0 /* not used so far */
  233. static int thread_stop (int id)
  234. {
  235. if (id <= MASTER_THREAD || id >= MAX_THREADS)
  236. return RC_FAILURE;
  237. if (current_tid == id)
  238. return RC_FAILURE;
  239. lthreads[id].state = STATE_STOPPED;
  240. return RC_SUCCESS;
  241. }
  242. #endif /* not used so far */
  243. static int thread_join (int *ret)
  244. {
  245. int i, j = 0;
  246. PDEBUG ("thread_join: *ret = %d", *ret);
  247. if (!(*ret == -1 || *ret > MASTER_THREAD || *ret < MAX_THREADS)) {
  248. PDEBUG ("thread_join: invalid tid %d", *ret);
  249. return RC_FAILURE;
  250. }
  251. if (*ret == -1) {
  252. PDEBUG ("Checking for tid = -1");
  253. while (1) {
  254. /* PDEBUG("thread_join: start while-loopn"); */
  255. j = 0;
  256. for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
  257. if (lthreads[i].state == STATE_TERMINATED) {
  258. *ret = lthreads[i].retval;
  259. lthreads[i].state = STATE_EMPTY;
  260. /* PDEBUG("thread_join: returning retval %d of tid %d",
  261. ret, i); */
  262. return RC_SUCCESS;
  263. }
  264. if (lthreads[i].state != STATE_EMPTY) {
  265. PDEBUG ("thread_join: %d used slots tid %d state=%d",
  266. j, i, lthreads[i].state);
  267. j++;
  268. }
  269. }
  270. if (j == 0) {
  271. PDEBUG ("thread_join: all slots empty!");
  272. return RC_FAILURE;
  273. }
  274. /* PDEBUG("thread_join: yielding"); */
  275. thread_yield ();
  276. /* PDEBUG("thread_join: back from yield"); */
  277. }
  278. }
  279. if (lthreads[*ret].state == STATE_TERMINATED) {
  280. i = *ret;
  281. *ret = lthreads[*ret].retval;
  282. lthreads[*ret].state = STATE_EMPTY;
  283. PDEBUG ("thread_join: returing %d for tid %d", *ret, i);
  284. return RC_SUCCESS;
  285. }
  286. PDEBUG ("thread_join: thread %d is not terminated!", *ret);
  287. return RC_FAILURE;
  288. }