ppc32/64: rewrite get/set/swapcontext in assembly
getcontext cannot be correctly implemented in C.
If this calls another function, as it does to call syscall, it needs to
first spill its return address to the stack. If, after getcontext returns,
its caller then calls other functions, this saved return address can be
clobbered. When the context saved by getcontext is later restored, the
(now clobbered) return address will be reloaded from the stack, and the
second return from getcontext will return to the wrong location.
Because the powerpc swapcontext syscall allows either the old context or
new context pointers to be null, it is usable for implementing all of
get/set/swapcontext.
We therefore rewrite swapcontext in assembly, and get/setcontext as simple
assembly function wrappers around swapcontext.
The one piece we keep in C is the code to check the return value of the
system call and to set errno. This code was actually unnecessary before --
libc does this within syscall. However, now that the system call is made
directly in assembly, bypassing libc, it is truly necessary. Because errno
is thread-local and the details of how to set it can vary by libc, this
code remains written in C.
2019-02-23 23:12:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Bobby Bingham <koorogi@koorogi.info>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
2021-01-08 03:52:46 +00:00
|
|
|
#include "common-defs.h"
|
|
|
|
|
|
|
|
ALIAS(swapcontext, __libucontext_swapcontext)
|
2021-01-08 09:30:50 +00:00
|
|
|
ALIAS(__swapcontext, __libucontext_swapcontext)
|
2021-01-08 13:55:27 +00:00
|
|
|
|
|
|
|
/* make sure this is visible regardless of EXPORT_UNPREFIXED */
|
|
|
|
.weak libucontext_swapcontext
|
|
|
|
libucontext_swapcontext = __libucontext_swapcontext
|
2021-01-08 03:52:46 +00:00
|
|
|
|
|
|
|
FUNC(__libucontext_swapcontext)
|
ppc32/64: rewrite get/set/swapcontext in assembly
getcontext cannot be correctly implemented in C.
If this calls another function, as it does to call syscall, it needs to
first spill its return address to the stack. If, after getcontext returns,
its caller then calls other functions, this saved return address can be
clobbered. When the context saved by getcontext is later restored, the
(now clobbered) return address will be reloaded from the stack, and the
second return from getcontext will return to the wrong location.
Because the powerpc swapcontext syscall allows either the old context or
new context pointers to be null, it is usable for implementing all of
get/set/swapcontext.
We therefore rewrite swapcontext in assembly, and get/setcontext as simple
assembly function wrappers around swapcontext.
The one piece we keep in C is the code to check the return value of the
system call and to set errno. This code was actually unnecessary before --
libc does this within syscall. However, now that the system call is made
directly in assembly, bypassing libc, it is truly necessary. Because errno
is thread-local and the details of how to set it can vary by libc, this
code remains written in C.
2019-02-23 23:12:37 +00:00
|
|
|
li 0, 249 # SYS_swapcontext
|
|
|
|
li 5, 1184 # sizeof(ucontext_t)
|
|
|
|
sc
|
|
|
|
|
|
|
|
.hidden __retfromsyscall
|
|
|
|
b __retfromsyscall@local
|
2021-01-08 03:52:46 +00:00
|
|
|
END(__libucontext_swapcontext)
|