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