]>
Commit | Line | Data |
---|---|---|
00dccaf1 KW |
1 | /* |
2 | * ucontext coroutine initialization code | |
3 | * | |
4 | * Copyright (C) 2006 Anthony Liguori <[email protected]> | |
5 | * Copyright (C) 2011 Kevin Wolf <[email protected]> | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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/>. | |
19 | */ | |
20 | ||
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 | |
aafd7584 | 25 | #include "qemu/osdep.h" |
00dccaf1 | 26 | #include <setjmp.h> |
00dccaf1 KW |
27 | #include <ucontext.h> |
28 | #include "qemu-common.h" | |
10817bf0 | 29 | #include "qemu/coroutine_int.h" |
00dccaf1 | 30 | |
3f4349dc KW |
31 | #ifdef CONFIG_VALGRIND_H |
32 | #include <valgrind/valgrind.h> | |
33 | #endif | |
34 | ||
00dccaf1 KW |
35 | typedef struct { |
36 | Coroutine base; | |
37 | void *stack; | |
6ab7e546 | 38 | sigjmp_buf env; |
3f4349dc KW |
39 | |
40 | #ifdef CONFIG_VALGRIND_H | |
41 | unsigned int valgrind_stack_id; | |
42 | #endif | |
43 | ||
00dccaf1 KW |
44 | } CoroutineUContext; |
45 | ||
46 | /** | |
47 | * Per-thread coroutine bookkeeping | |
48 | */ | |
d1d1b206 PB |
49 | static __thread CoroutineUContext leader; |
50 | static __thread Coroutine *current; | |
00dccaf1 KW |
51 | |
52 | /* | |
53 | * va_args to makecontext() must be type 'int', so passing | |
54 | * the pointer we need may require several int args. This | |
55 | * union is a quick hack to let us do that | |
56 | */ | |
57 | union cc_arg { | |
58 | void *p; | |
59 | int i[2]; | |
60 | }; | |
61 | ||
00dccaf1 KW |
62 | static void coroutine_trampoline(int i0, int i1) |
63 | { | |
64 | union cc_arg arg; | |
65 | CoroutineUContext *self; | |
66 | Coroutine *co; | |
67 | ||
68 | arg.i[0] = i0; | |
69 | arg.i[1] = i1; | |
70 | self = arg.p; | |
71 | co = &self->base; | |
72 | ||
73 | /* Initialize longjmp environment and switch back the caller */ | |
6ab7e546 PM |
74 | if (!sigsetjmp(self->env, 0)) { |
75 | siglongjmp(*(sigjmp_buf *)co->entry_arg, 1); | |
00dccaf1 KW |
76 | } |
77 | ||
78 | while (true) { | |
79 | co->entry(co->entry_arg); | |
80 | qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); | |
81 | } | |
82 | } | |
83 | ||
40239784 | 84 | Coroutine *qemu_coroutine_new(void) |
00dccaf1 KW |
85 | { |
86 | const size_t stack_size = 1 << 20; | |
87 | CoroutineUContext *co; | |
88 | ucontext_t old_uc, uc; | |
6ab7e546 | 89 | sigjmp_buf old_env; |
32b74677 | 90 | union cc_arg arg = {0}; |
00dccaf1 | 91 | |
6ab7e546 PM |
92 | /* The ucontext functions preserve signal masks which incurs a |
93 | * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not | |
94 | * preserve signal masks but only works on the current stack. | |
95 | * Since we need a way to create and switch to a new stack, use | |
96 | * the ucontext functions for that but sigsetjmp()/siglongjmp() for | |
97 | * everything else. | |
00dccaf1 KW |
98 | */ |
99 | ||
100 | if (getcontext(&uc) == -1) { | |
101 | abort(); | |
102 | } | |
103 | ||
7267c094 AL |
104 | co = g_malloc0(sizeof(*co)); |
105 | co->stack = g_malloc(stack_size); | |
00dccaf1 KW |
106 | co->base.entry_arg = &old_env; /* stash away our jmp_buf */ |
107 | ||
108 | uc.uc_link = &old_uc; | |
109 | uc.uc_stack.ss_sp = co->stack; | |
110 | uc.uc_stack.ss_size = stack_size; | |
111 | uc.uc_stack.ss_flags = 0; | |
112 | ||
3f4349dc KW |
113 | #ifdef CONFIG_VALGRIND_H |
114 | co->valgrind_stack_id = | |
115 | VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size); | |
116 | #endif | |
117 | ||
00dccaf1 KW |
118 | arg.p = co; |
119 | ||
120 | makecontext(&uc, (void (*)(void))coroutine_trampoline, | |
121 | 2, arg.i[0], arg.i[1]); | |
122 | ||
6ab7e546 PM |
123 | /* swapcontext() in, siglongjmp() back out */ |
124 | if (!sigsetjmp(old_env, 0)) { | |
00dccaf1 KW |
125 | swapcontext(&old_uc, &uc); |
126 | } | |
127 | return &co->base; | |
128 | } | |
129 | ||
3f4349dc | 130 | #ifdef CONFIG_VALGRIND_H |
cc6e3ca9 | 131 | #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE |
3f4349dc | 132 | /* Work around an unused variable in the valgrind.h macro... */ |
e6f53fd5 | 133 | #pragma GCC diagnostic push |
3f4349dc | 134 | #pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
06d71fa1 | 135 | #endif |
3f4349dc KW |
136 | static inline void valgrind_stack_deregister(CoroutineUContext *co) |
137 | { | |
138 | VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id); | |
139 | } | |
cc6e3ca9 | 140 | #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE |
e6f53fd5 | 141 | #pragma GCC diagnostic pop |
3f4349dc | 142 | #endif |
06d71fa1 | 143 | #endif |
3f4349dc | 144 | |
00dccaf1 KW |
145 | void qemu_coroutine_delete(Coroutine *co_) |
146 | { | |
00dccaf1 KW |
147 | CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_); |
148 | ||
3f4349dc KW |
149 | #ifdef CONFIG_VALGRIND_H |
150 | valgrind_stack_deregister(co); | |
151 | #endif | |
152 | ||
7267c094 AL |
153 | g_free(co->stack); |
154 | g_free(co); | |
00dccaf1 KW |
155 | } |
156 | ||
d1d1b206 PB |
157 | /* This function is marked noinline to prevent GCC from inlining it |
158 | * into coroutine_trampoline(). If we allow it to do that then it | |
159 | * hoists the code to get the address of the TLS variable "current" | |
160 | * out of the while() loop. This is an invalid transformation because | |
161 | * the sigsetjmp() call may be called when running thread A but | |
162 | * return in thread B, and so we might be in a different thread | |
163 | * context each time round the loop. | |
164 | */ | |
165 | CoroutineAction __attribute__((noinline)) | |
166 | qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, | |
167 | CoroutineAction action) | |
00dccaf1 KW |
168 | { |
169 | CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_); | |
170 | CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_); | |
00dccaf1 KW |
171 | int ret; |
172 | ||
d1d1b206 | 173 | current = to_; |
00dccaf1 | 174 | |
6ab7e546 | 175 | ret = sigsetjmp(from->env, 0); |
00dccaf1 | 176 | if (ret == 0) { |
6ab7e546 | 177 | siglongjmp(to->env, action); |
00dccaf1 KW |
178 | } |
179 | return ret; | |
180 | } | |
181 | ||
182 | Coroutine *qemu_coroutine_self(void) | |
183 | { | |
d1d1b206 PB |
184 | if (!current) { |
185 | current = &leader.base; | |
186 | } | |
187 | return current; | |
00dccaf1 KW |
188 | } |
189 | ||
190 | bool qemu_in_coroutine(void) | |
191 | { | |
d1d1b206 | 192 | return current && current->caller; |
00dccaf1 | 193 | } |