block/block-copy: refactor interfaces to use bytes instead of end
[qemu/ar7.git] / util / coroutine-ucontext.c
blobbd593e61bc0d743df7dbd3bd8b8ba228e3a97016
1 /*
2 * ucontext coroutine initialization code
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2011 Kevin Wolf <kwolf@redhat.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.0 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 /* XXX Is there a nicer way to disable glibc's stack check for longjmp? */
22 #ifdef _FORTIFY_SOURCE
23 #undef _FORTIFY_SOURCE
24 #endif
25 #include "qemu/osdep.h"
26 #include <ucontext.h>
27 #include "qemu/coroutine_int.h"
29 #ifdef CONFIG_VALGRIND_H
30 #include <valgrind/valgrind.h>
31 #endif
33 #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
34 #ifdef CONFIG_ASAN_IFACE_FIBER
35 #define CONFIG_ASAN 1
36 #include <sanitizer/asan_interface.h>
37 #endif
38 #endif
40 typedef struct {
41 Coroutine base;
42 void *stack;
43 size_t stack_size;
44 sigjmp_buf env;
46 #ifdef CONFIG_VALGRIND_H
47 unsigned int valgrind_stack_id;
48 #endif
50 } CoroutineUContext;
52 /**
53 * Per-thread coroutine bookkeeping
55 static __thread CoroutineUContext leader;
56 static __thread Coroutine *current;
59 * va_args to makecontext() must be type 'int', so passing
60 * the pointer we need may require several int args. This
61 * union is a quick hack to let us do that
63 union cc_arg {
64 void *p;
65 int i[2];
68 static void finish_switch_fiber(void *fake_stack_save)
70 #ifdef CONFIG_ASAN
71 const void *bottom_old;
72 size_t size_old;
74 __sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old);
76 if (!leader.stack) {
77 leader.stack = (void *)bottom_old;
78 leader.stack_size = size_old;
80 #endif
83 static void start_switch_fiber(void **fake_stack_save,
84 const void *bottom, size_t size)
86 #ifdef CONFIG_ASAN
87 __sanitizer_start_switch_fiber(fake_stack_save, bottom, size);
88 #endif
91 static void coroutine_trampoline(int i0, int i1)
93 union cc_arg arg;
94 CoroutineUContext *self;
95 Coroutine *co;
96 void *fake_stack_save = NULL;
98 finish_switch_fiber(NULL);
100 arg.i[0] = i0;
101 arg.i[1] = i1;
102 self = arg.p;
103 co = &self->base;
105 /* Initialize longjmp environment and switch back the caller */
106 if (!sigsetjmp(self->env, 0)) {
107 start_switch_fiber(&fake_stack_save,
108 leader.stack, leader.stack_size);
109 siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
112 finish_switch_fiber(fake_stack_save);
114 while (true) {
115 co->entry(co->entry_arg);
116 qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
120 Coroutine *qemu_coroutine_new(void)
122 CoroutineUContext *co;
123 ucontext_t old_uc, uc;
124 sigjmp_buf old_env;
125 union cc_arg arg = {0};
126 void *fake_stack_save = NULL;
128 /* The ucontext functions preserve signal masks which incurs a
129 * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not
130 * preserve signal masks but only works on the current stack.
131 * Since we need a way to create and switch to a new stack, use
132 * the ucontext functions for that but sigsetjmp()/siglongjmp() for
133 * everything else.
136 if (getcontext(&uc) == -1) {
137 abort();
140 co = g_malloc0(sizeof(*co));
141 co->stack_size = COROUTINE_STACK_SIZE;
142 co->stack = qemu_alloc_stack(&co->stack_size);
143 co->base.entry_arg = &old_env; /* stash away our jmp_buf */
145 uc.uc_link = &old_uc;
146 uc.uc_stack.ss_sp = co->stack;
147 uc.uc_stack.ss_size = co->stack_size;
148 uc.uc_stack.ss_flags = 0;
150 #ifdef CONFIG_VALGRIND_H
151 co->valgrind_stack_id =
152 VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size);
153 #endif
155 arg.p = co;
157 makecontext(&uc, (void (*)(void))coroutine_trampoline,
158 2, arg.i[0], arg.i[1]);
160 /* swapcontext() in, siglongjmp() back out */
161 if (!sigsetjmp(old_env, 0)) {
162 start_switch_fiber(&fake_stack_save, co->stack, co->stack_size);
163 swapcontext(&old_uc, &uc);
166 finish_switch_fiber(fake_stack_save);
168 return &co->base;
171 #ifdef CONFIG_VALGRIND_H
172 #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__)
173 /* Work around an unused variable in the valgrind.h macro... */
174 #pragma GCC diagnostic push
175 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
176 #endif
177 static inline void valgrind_stack_deregister(CoroutineUContext *co)
179 VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
181 #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__)
182 #pragma GCC diagnostic pop
183 #endif
184 #endif
186 void qemu_coroutine_delete(Coroutine *co_)
188 CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
190 #ifdef CONFIG_VALGRIND_H
191 valgrind_stack_deregister(co);
192 #endif
194 qemu_free_stack(co->stack, co->stack_size);
195 g_free(co);
198 /* This function is marked noinline to prevent GCC from inlining it
199 * into coroutine_trampoline(). If we allow it to do that then it
200 * hoists the code to get the address of the TLS variable "current"
201 * out of the while() loop. This is an invalid transformation because
202 * the sigsetjmp() call may be called when running thread A but
203 * return in thread B, and so we might be in a different thread
204 * context each time round the loop.
206 CoroutineAction __attribute__((noinline))
207 qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
208 CoroutineAction action)
210 CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
211 CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
212 int ret;
213 void *fake_stack_save = NULL;
215 current = to_;
217 ret = sigsetjmp(from->env, 0);
218 if (ret == 0) {
219 start_switch_fiber(action == COROUTINE_TERMINATE ?
220 NULL : &fake_stack_save, to->stack, to->stack_size);
221 siglongjmp(to->env, action);
224 finish_switch_fiber(fake_stack_save);
226 return ret;
229 Coroutine *qemu_coroutine_self(void)
231 if (!current) {
232 current = &leader.base;
234 return current;
237 bool qemu_in_coroutine(void)
239 return current && current->caller;