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 #ifdef USEVALGRIND
7 #include <valgrind/memcheck.h>
8 #endif
10 static void
11 launcher386(void (*f)(void *arg), void *arg)
12 {
13 Proc *p;
14 Thread *t;
16 p = _threadgetproc();
17 t = p->thread;
18 _threadstacklimit(t->stk);
20 (*f)(arg);
21 threadexits(nil);
22 }
24 void
25 _threadinitstack(Thread *t, void (*f)(void*), void *arg)
26 {
27 ulong *tos;
29 tos = (ulong*)&t->stk[t->stksize&~7];
30 *--tos = (ulong)arg;
31 *--tos = (ulong)f;
32 t->sched.pc = (ulong)launcher386;
33 t->sched.sp = (ulong)tos - 8; /* old PC and new PC */
34 }
36 void
37 _threadinswitch(int enter)
38 {
39 USED(enter);
40 #ifdef USEVALGRIND
41 if(enter)
42 VALGRIND_SET_STACK_LIMIT(0, 0, 1);
43 else
44 VALGRIND_SET_STACK_LIMIT(0, 0, 0);
45 #endif
46 }
48 void
49 _threadstacklimit(void *addr)
50 {
51 USED(addr);
53 #ifdef USEVALGRIND
54 VALGRIND_SET_STACK_LIMIT(1, addr, 0);
55 #endif
56 }