[RFC PATCH v1 1/8] nolibc/sys: Implement `sigaction(2)` function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Ammar Faizi <ammarfaizi2@xxxxxxxxxxx>

This commit adds the initial implementation of nolibc `sigaction()`
function. Currently, this implementation is only available on the
x86-64 arch.

`sigaction()` needs an architecture-dependent "signal trampoline"
function that invokes the __rt_sigreturn syscall to resume the process
after a signal gets handled.

On Linux x86-64, the "signal trampoline" function has to be written in
inline Assembly to prevent the compiler from controlling the `%rsp`
(e.g., with `-fno-omit-frame-pointer`, every function has a `pushq
%rbp` that makes the `%rsp` no longer point to `struct rt_sigframe`).

The "signal trampoline" function is called `__arch_restore_rt` in this
implementation.

Signed-off-by: Ammar Faizi <ammarfaizi2@xxxxxxxxxxx>
---
 tools/include/nolibc/arch-x86_64.h | 12 +++++
 tools/include/nolibc/sys.h         | 80 ++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)

diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h
index 0e1e9eb8545d..b6470943e836 100644
--- a/tools/include/nolibc/arch-x86_64.h
+++ b/tools/include/nolibc/arch-x86_64.h
@@ -212,4 +212,16 @@ __asm__ (".section .text\n"
     "hlt\n"                     // ensure it does not return
     "");
 
+void __arch_restore_rt(void);
+
+__asm__ (
+".section .text\n"
+"__arch_restore_rt:\n\t"
+	"movl	$0xf, %eax\n\t" // __NR_rt_sigreturn == 0xf
+	"syscall\n\t"           // %rsp must point to a valid struct rt_sigframe.
+	"int3"
+);
+
+#define __HAVE_ARCH_RESTORE_RT
+
 #endif // _NOLIBC_ARCH_X86_64_H
diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 3db1dd8c74ee..91532a2fbe2c 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -1026,6 +1026,86 @@ pid_t setsid(void)
 	return ret;
 }
 
+typedef void (*sighandler_t)(int sig);
+
+/*
+ * int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
+ */
+
+static __attribute__((unused))
+int sys_sigaction(int signum, const struct sigaction *act,
+		  struct sigaction *oldact)
+{
+	return my_syscall4(__NR_rt_sigaction, signum, act, oldact,
+			   sizeof(sigset_t));
+}
+
+static __attribute__((unused))
+int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
+{
+#ifdef __HAVE_ARCH_RESTORE_RT
+	struct sigaction act2 = *act;
+	int ret;
+
+	/*
+	 * On Linux x86-64, libc's sigaction() always sets the
+	 * @act->sa_restorer when the caller passes a NULL.
+	 *
+	 * @act->sa_restorer is an arch-specific function used
+	 * as a "signal trampoline".
+	 *
+	 * @act->sa_handler is a signal handler provided by the
+	 * user.
+	 *
+	 * When the handled signal is caught, the %rip jumps to
+	 * @act->sa_handler with user stack already set by the
+	 * kernel as below:
+	 *
+	 *         |--------------------|
+	 * %rsp -> |  act->sa_restorer  | (return address)
+	 *         |--------------------|
+	 *         | struct rt_sigframe | (process context info)
+	 *         |                    |
+	 *         |                    |
+	 *          ....................
+	 *
+	 * Once this signal handler executes the "ret" instruction,
+	 * the %rip jumps to @act->sa_restorer. The sa_restorer
+	 * function has to invoke the __rt_sigreturn syscall with
+	 * %rsp pointing to the `struct rt_sigframe` that the kernel
+	 * constructed previously to resume the process.
+	 *
+	 * The "signal trampoline" function has to be written in
+	 * inline Assembly to prevent the compiler from controlling
+	 * the %rsp (e.g., with -fno-omit-frame-pointer, every
+	 * function has a `pushq %rbp` that makes the %rsp no longer
+	 * point to `struct rt_sigframe`).
+	 *
+	 * `struct rt_sigframe` contains the registers' value before
+	 * the signal is caught.
+	 *
+	 */
+	if (!act2.sa_restorer) {
+		act2.sa_flags |= SA_RESTORER;
+		act2.sa_restorer = __arch_restore_rt;
+	}
+
+	ret = sys_sigaction(signum, &act2, oldact);
+	if (ret < 0) {
+		SET_ERRNO(-ret);
+		ret = -1;
+	}
+	return ret;
+#else
+	/*
+	 * TODO: Implement sa_restorer ("signal trampoline") for
+	 *       other architectures.
+	 */
+	SET_ERRNO(ENOSYS);
+	return -1;
+#endif
+}
+
 
 /*
  * int stat(const char *path, struct stat *buf);
-- 
Ammar Faizi




[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux