sched.c 8.0 KB

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