libc - Check for invalid context in setcontext() and swapcontext() * Check for an invalid context in setcontext() and swapcontext() and return -1 instead of switching to the invalid context. * This is not an exhaustive check but will handle invalid states set by makecontext() if e.g. argc is messed up or the supplied stack is too small. In particular, if makecontext() fails and set/swap tries to switch to it, it will switch to the state from the getcontext() prior to the makecontext() instead of to the desired makecontext() state. Reported-by: zrj, tuxillo
<sys/cdefs.h>: Rework __weak_reference() macro. My LTO build blew away weak symbols from slim LTO objects. Use __strong_reference() + weak attribute to allow the compiler to catch extern declarations and not to fold weak symbols as local ones. Keep previous version as __weak_reference_asm() in _pthread_stubs.c for now due to several issues (there is a need to do it in a cleaner way). lib/libc/gen/ucontext.c: add missing __DECONST for ucp, shouldn't sigreturn take const ucontext_t? lib/libc/inet/inet_ntoa.c: add missing #undef inet_ntoa_r No symbol changes in libc and librt on normal compilation. While there, add __weak_symbol attribute for future additions.
libc - Add quick version for the context management functions. * Add makecontext_quick(), setcontext_quick(), and swapcontext_quick(). These functions work similarly to the non-quick versions but are designed for fast synchronous switching. These functions do not mess with the signal mask or stack at all and do not save or restore scratch registers. * These functions make no system calls. Switching time can be as low as ~5 nanoseconds. * These functions also provide optimizations for coroutine fall-through linkages. * Note that the coroutine / start-function callback arguments are somewhat different. Start functions are called back as cofunc(ucp, arg). Var-args are not supported and the stack is minimally aligned and initialized. * Remove the old internal set_mcontext() and get_mcontext() routines.
libc - Fix bugs in getcontext(), setcontext(), and swapcontext() * Fix multiple bugs revealed by qemu's use of these functions. Most of these fixes are accomplished by calling sigreturn(uctx) to restore the state instead of trying to roll our own in userland. This won't be much slower (if at all) because we had to save and restore the signal state in the userland code anyway, so we could not avoid making at least one system call. Using sigreturn() handles the signal mask atomicy for us so we don't have to deal with it and fixes numerous other issues. Along with this change, adjust getcontext() and makecontext() to fill out additional important fields in the ucontext that sigreturn() inspects. * Fixes two stack corruption bugs. First, getcontext() was calling get_mcontext() and get_mcontext() was setting up the setcontext return state to return 1 ... from get_mcontext(), NOT from getcontext(). If normal operations or signals mess with the stack, the double return will not work. Oops. Secondly, getcontext scribbled over the red-zone in a way that is not permitted. * setcontext() was restoring the context as saved by makecontext() or getcontext(), but setcontext() can also be called with the ucontext from the signal handler which requires a full restore. setcontext() was not restoring FPU or scratch registers or rflags. * Fixes signal restoration bug and corruption that can mess up emulation in qemu. * Fixes issues with qemu related to SMP startup and lack of preemption. Reported-by: ivadasz
Implement getcontext(), setcontext(), makecontext(), and swapcontext(). This is a preliminary libc-only implementation. sigprocmask() is used to mask signals during the context switch and to save and restore signals. FP registers are not currently saved or restored - that is, synchronous use is intended. Submitted-by: "Matt Emmerton" <matt@gsicomp.on.ca> Finished-up-by: "Matthew Dillon" <dillon@backplane.com>