Blob


1 #include "threadimpl.h"
2 /*
3 * To use this you need some patches to Valgrind that
4 * let it help out with detecting stack overflow.
5 */
6 #define USEVALGRIND 0
7 #ifdef USEVALGRIND
8 #include <valgrind/memcheck.h>
9 #endif
11 static void
12 launcher386(void (*f)(void *arg), void *arg)
13 {
14 Proc *p;
15 Thread *t;
17 p = _threadgetproc();
18 t = p->thread;
19 _threadstacklimit(t->stk);
21 (*f)(arg);
22 threadexits(nil);
23 }
25 void
26 _threadinitstack(Thread *t, void (*f)(void*), void *arg)
27 {
28 ulong *tos;
30 tos = (ulong*)&t->stk[t->stksize&~7];
31 *--tos = (ulong)arg;
32 *--tos = (ulong)f;
33 t->sched.pc = (ulong)launcher386;
34 t->sched.sp = (ulong)tos - 8; /* old PC and new PC */
35 }
37 void
38 _threadinswitch(int enter)
39 {
40 USED(enter);
41 #ifdef USEVALGRIND
42 if(enter)
43 VALGRIND_SET_STACK_LIMIT(0, 0, 1);
44 else
45 VALGRIND_SET_STACK_LIMIT(0, 0, 0);
46 #endif
47 }
49 void
50 _threadstacklimit(void *addr)
51 {
52 USED(addr);
54 #ifdef USEVALGRIND
55 VALGRIND_SET_STACK_LIMIT(1, addr, 0);
56 #endif
57 }