summaryrefslogtreecommitdiffstats
path: root/src/uctx.h
blob: 353b7327788b44f548eca8075170d8f487bb0147 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* uctx.h - assembly stack switching
 *
 * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or later as
 * published by the Free Software Foundation.
 *
 * See http://www.gnu.org/ for details.
 */

#include <libtf/fiber.h>
#include <libtf/memory.h>

#include <stdio.h>
#include <stdlib.h>

#ifdef VALGRIND
#include <valgrind/valgrind.h>
#endif

#define STACK_GUARD		0xbad57ac4

struct tf_uctx {
	int *			stack_guard;
	size_t			size;
	void *			alloc;
	void *			current_sp;
#ifdef VALGRIND
	unsigned int		stack_id;
#endif
};

#if defined(__i386__)

#define switch_fiber(prev, next)			\
	do {						\
		unsigned eax, ebx, ecx, edx, esi, edi;	\
		__asm__ __volatile__ (			\
		"call	1f				\n" \
		"1:					\n" \
		"addl	$2f-1b, (%%esp)			\n" \
		"push	%%ebp				\n" \
		"movl	%%esp, %[prev_sp]		\n" \
		"movl	%[next_sp], %%esp		\n" \
		"pop	%%ebp				\n" \
		"ret					\n" \
		"2:					\n" \
		: [prev_sp] "=m"(prev->current_sp),	\
		  "=a"(eax), "=b"(ebx), "=c"(ecx),	\
		  "=d"(edx), "=S"(esi), "=D"(edi)	\
		: [next_sp] "m"(next->current_sp)	\
		: "memory", "cc");			\
	} while (0)

#define STACK_GROWS_UP

#else

#error Your architecture is not supported. Contact timo.teras@iki.fi.

#endif

#if defined(STACK_GROWS_UP)
static inline void *stack_pointer(void *base, size_t size)
{
	return base + size;
}

static inline void *stack_guard(void *base, size_t size)
{
	return base;
}

static inline void *stack_push(void **stackptr, size_t size)
{
	(*stackptr) -= size;
	return (*stackptr);
}
#else
#error Stack growing direction undefined.
#endif

static inline void stack_push_ptr(void **stackptr, void *ptr)
{
	*(void**) stack_push(stackptr, sizeof(ptr)) = ptr;
}


static inline tf_uctx_t tf_uctx_create_self(struct tf_uctx *uctx)
{
	static int dummy_guard = STACK_GUARD;

	*uctx = (struct tf_uctx) {
		.stack_guard = &dummy_guard,
	};
	return uctx;
}

static inline void *
tf_uctx_create_embedded(
	size_t stack_size,
	size_t private_size,
	off_t uctx_offset,
	void (*stack_frame_main)(void*, void*),
	void *main_argument)
{
	size_t size = TF_STACK_SIZE;
	void *user_data;
	struct tf_uctx *uctx;
	void *stack, *stack_base;

	/* Allocate new stack */
	stack_base = tf_bmem_alloc(size);
	if (stack_base == NULL)
		return NULL;

	/* Create initial stack frame (cdecl convention) */
	stack = stack_pointer(stack_base, size);
	user_data = stack_push(&stack, TF_ALIGN(private_size, 16));
	uctx = stack_push(&stack, TF_ALIGN(sizeof(struct tf_uctx), 16));
	stack_push_ptr(&stack, main_argument);
	stack_push_ptr(&stack, user_data);
	stack_push_ptr(&stack, NULL);
	stack_push_ptr(&stack, stack_frame_main);	/* eip */
	stack_push_ptr(&stack, NULL);			/* ebp */

	*((tf_uctx_t *) (user_data + uctx_offset)) = uctx;

	*uctx = (struct tf_uctx) {
		.stack_guard = stack_guard(stack_base, size),
		.alloc = stack_base,
		.size = size,
		.current_sp = stack,
#ifdef VALGRIND
		.stack_id = VALGRIND_STACK_REGISTER(stack_base, stack_base+size),
#endif
	};
	*uctx->stack_guard = STACK_GUARD;

	return user_data;
}

static inline
void tf_uctx_destroy(tf_uctx_t ctx)
{
	struct tf_uctx *uctx = ctx;

	if (uctx->alloc != NULL) {
#ifdef VALGRIND
		VALGRIND_STACK_DEREGISTER(uctx->stack_id);
#endif
		tf_bmem_free(uctx->alloc, uctx->size);
	}
}

static inline
void tf_uctx_transfer(tf_uctx_t from, tf_uctx_t to)
{
	struct tf_uctx *ufrom = from;
	struct tf_uctx *uto = to;

	/* Switch stack pointers */
	TF_BUG_ON(*ufrom->stack_guard != STACK_GUARD);
	switch_fiber(ufrom, uto);
}