aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/386.c
blob: 3fbf0afe6d9fe063b1d5a74531b3c71c57b4c922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "threadimpl.h"
/*
 * To use this you need some patches to Valgrind that
 * let it help out with detecting stack overflow. 
 */
#define USEVALGRIND 0
#ifdef USEVALGRIND
#include <valgrind/memcheck.h>
#endif

static void
launcher386(void (*f)(void *arg), void *arg)
{
	Proc *p;
	Thread *t;

	p = _threadgetproc();
	t = p->thread;
	_threadstacklimit(t->stk);

	(*f)(arg);
	threadexits(nil);
}

void
_threadinitstack(Thread *t, void (*f)(void*), void *arg)
{
	ulong *tos;

	tos = (ulong*)&t->stk[t->stksize&~7];
	*--tos = (ulong)arg;
	*--tos = (ulong)f;
	t->sched.pc = (ulong)launcher386;
	t->sched.sp = (ulong)tos - 8;		/* old PC and new PC */
}

void
_threadinswitch(int enter)
{
	USED(enter);
#ifdef USEVALGRIND
	if(enter)
		VALGRIND_SET_STACK_LIMIT(0, 0, 1);
	else
		VALGRIND_SET_STACK_LIMIT(0, 0, 0);
#endif
}

void
_threadstacklimit(void *addr)
{
	USED(addr);

#ifdef USEVALGRIND
	VALGRIND_SET_STACK_LIMIT(1, addr, 0);
#endif
}