Skip to content
Snippets Groups Projects
Commit dd044a71 authored by Anna Wilcox's avatar Anna Wilcox :fox:
Browse files

Merge branch 'libucontext-ppc64' into 'master'

Libucontext ppc64

Fix libucontext functions for ppc64

See merge request !185
parents d0720f7a a9ca3880
No related branches found
No related tags found
No related merge requests found
From 42c84cbe58c3c9089cc2a46a425210f20b47bcc9 Mon Sep 17 00:00:00 2001
From: "A. Wilcox" <AWilcox@Wilcox-Tech.com>
Date: Thu, 19 Jul 2018 17:17:26 -0500
Subject: [PATCH] Reorder registers in other assembler files
This fixes the previous commit which changed the register order in
swapcontext only, which caused setcontext to subtly corrupt the stack.
---
arch/x86_64/getcontext.S | 4 ++--
arch/x86_64/setcontext.S | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/arch/x86_64/getcontext.S b/arch/x86_64/getcontext.S
index 7e56a1a..24cc1d3 100644
--- a/arch/x86_64/getcontext.S
+++ b/arch/x86_64/getcontext.S
@@ -22,8 +22,8 @@ __getcontext:
movq %r14, 88(%rdi)
movq %r15, 96(%rdi)
movq %rdi, 104(%rdi)
- movq %rbp, 112(%rdi)
- movq %rsi, 120(%rdi)
+ movq %rsi, 112(%rdi)
+ movq %rbp, 120(%rdi)
movq %rbx, 128(%rdi)
movq %rdx, 136(%rdi)
movq $1, 144(%rdi) /* $1 is %rax */
diff --git a/arch/x86_64/setcontext.S b/arch/x86_64/setcontext.S
index 607d40c..aba3775 100644
--- a/arch/x86_64/setcontext.S
+++ b/arch/x86_64/setcontext.S
@@ -21,8 +21,8 @@ __setcontext:
movq 80(%rdi), %r13
movq 88(%rdi), %r14
movq 96(%rdi), %r15
- movq 112(%rdi), %rbp
- movq 120(%rdi), %rsi
+ movq 112(%rdi), %rsi
+ movq 120(%rdi), %rbp
movq 128(%rdi), %rbx
movq 136(%rdi), %rdx
movq 144(%rdi), %rax
--
2.15.0
# Maintainer: William Pitcock <nenolod@dereferenced.org>
pkgname=libucontext
pkgver=0.1.3
pkgrel=0
pkgrel=1
pkgdesc="ucontext function implementations"
url="https://git.dereferenced.org/kaniini/libucontext/"
arch="all"
......@@ -9,6 +9,8 @@ license="ISC"
depends=""
subpackages="$pkgname-dev"
source="https://distfiles.dereferenced.org/libucontext/libucontext-$pkgver.tar.xz
ppc64-assembly.patch
ppc64-stack-layout.patch
"
case "$CTARGET_ARCH" in
......@@ -33,4 +35,6 @@ package() {
make ARCH="$LIBUCONTEXT_ARCH" DESTDIR="$pkgdir" install
}
sha512sums="9e0694c09f159de714b655f6a4201a05c8d1871ee7376bb6bf824206b45475cfbd8269ca1caa75b9c322624d9f77d3fc3f938228a4bdd267a5ddceb57ca5c1ab libucontext-0.1.3.tar.xz"
sha512sums="9e0694c09f159de714b655f6a4201a05c8d1871ee7376bb6bf824206b45475cfbd8269ca1caa75b9c322624d9f77d3fc3f938228a4bdd267a5ddceb57ca5c1ab libucontext-0.1.3.tar.xz
dbb7599fe5cc1e470d944bf741c393c13e684d6c27bd794ec380a05ba1ba045597b102903a1ac9bf586a508941440373c2d29fad1d7372ee8c72ffb7f0e11f75 ppc64-assembly.patch
d6219487a5aa456726a01a5a6a39c3a7f29bc24a66f2fc77ff7f852e1b45ffe64854ad92cafad3cc03d3b52bd77dce786ef125e3e88ddb0398833a560afb5211 ppc64-stack-layout.patch"
From 7b864e86e3e8e64fe69363137384ee9b858fe78d Mon Sep 17 00:00:00 2001
From: Bobby Bingham <koorogi@koorogi.info>
Date: Sat, 23 Feb 2019 17:12:37 -0600
Subject: [PATCH 2/3] ppc64: rewrite get/set/swapcontext in assembly
If getcontext makes any function call, which it does to call syscall, then
it must spill its return address to the stack. After it returns to its
caller, that return address can be clobbered. When setcontext is called
to resume this saved context, the correct return address is not available.
---
Makefile | 10 ++-------
arch/ppc64/getcontext.S | 25 +++++++++++++++++++++
arch/ppc64/getcontext.c | 45 -------------------------------------
arch/ppc64/retfromsyscall.c | 24 ++++++++++++++++++++
arch/ppc64/setcontext.S | 26 +++++++++++++++++++++
arch/ppc64/setcontext.c | 45 -------------------------------------
arch/ppc64/swapcontext.S | 28 +++++++++++++++++++++++
arch/ppc64/swapcontext.c | 45 -------------------------------------
8 files changed, 105 insertions(+), 143 deletions(-)
create mode 100644 arch/ppc64/getcontext.S
delete mode 100644 arch/ppc64/getcontext.c
create mode 100644 arch/ppc64/retfromsyscall.c
create mode 100644 arch/ppc64/setcontext.S
delete mode 100644 arch/ppc64/setcontext.c
create mode 100644 arch/ppc64/swapcontext.S
delete mode 100644 arch/ppc64/swapcontext.c
diff --git a/Makefile b/Makefile
index 51365a3..d6ff1b0 100644
--- a/Makefile
+++ b/Makefile
@@ -2,14 +2,8 @@ ARCH := $(shell uname -m)
CFLAGS = -ggdb3 -O2 -Wall -Iarch/${ARCH}
-LIBUCONTEXT_C_SRC = \
- arch/${ARCH}/makecontext.c
-
-LIBUCONTEXT_S_SRC = \
- arch/${ARCH}/getcontext.S \
- arch/${ARCH}/setcontext.S \
- arch/${ARCH}/swapcontext.S \
- arch/${ARCH}/startcontext.S
+LIBUCONTEXT_C_SRC = $(wildcard arch/${ARCH}/*.c)
+LIBUCONTEXT_S_SRC = $(wildcard arch/${ARCH}/*.S)
LIBUCONTEXT_OBJ = ${LIBUCONTEXT_C_SRC:.c=.o} ${LIBUCONTEXT_S_SRC:.S=.o}
LIBUCONTEXT_SOVERSION = 0
diff --git a/arch/ppc64/getcontext.S b/arch/ppc64/getcontext.S
new file mode 100644
index 0000000..935edd2
--- /dev/null
+++ b/arch/ppc64/getcontext.S
@@ -0,0 +1,25 @@
+/*
+ * 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.
+ */
+
+.global __getcontext
+.hidden __swapcontext
+__getcontext:
+ addis 2, 12, .TOC.-__getcontext@ha
+ addi 2, 12, .TOC.-__getcontext@l
+
+ .localentry __getcontext,.-__getcontext
+
+ li 4, 0
+ b __swapcontext
+
+.weak getcontext
+getcontext = __getcontext
diff --git a/arch/ppc64/getcontext.c b/arch/ppc64/getcontext.c
deleted file mode 100644
index 5da9dfb..0000000
--- a/arch/ppc64/getcontext.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2018 William Pitcock <nenolod@dereferenced.org>
- *
- * 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.
- */
-
-#define _GNU_SOURCE
-#include <stddef.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <string.h>
-#include <stdint.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/syscall.h>
-
-
-int
-__getcontext(ucontext_t *ucp)
-{
-#ifdef SYS_swapcontext
- int r;
-
- r = syscall(SYS_swapcontext, ucp, NULL, sizeof(ucontext_t));
- if (r < 0)
- {
- errno = -r;
- return -1;
- }
-
- return 0;
-#else
- errno = ENOSYS;
- return -1;
-#endif
-}
-
-
-extern __typeof(__getcontext) getcontext __attribute__((weak, __alias__("__getcontext")));
diff --git a/arch/ppc64/retfromsyscall.c b/arch/ppc64/retfromsyscall.c
new file mode 100644
index 0000000..22c5134
--- /dev/null
+++ b/arch/ppc64/retfromsyscall.c
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+#include <errno.h>
+
+__attribute__ ((visibility ("hidden")))
+int __retfromsyscall(long retval)
+{
+ if (retval < 0) {
+ errno = -retval;
+ return -1;
+ }
+ return 0;
+}
+
diff --git a/arch/ppc64/setcontext.S b/arch/ppc64/setcontext.S
new file mode 100644
index 0000000..5a0cde3
--- /dev/null
+++ b/arch/ppc64/setcontext.S
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+
+.global __setcontext
+.hidden __swapcontext
+__setcontext:
+ addis 2, 12, .TOC.-__setcontext@ha
+ addi 2, 12, .TOC.-__setcontext@l
+
+ .localentry __setcontext,.-__setcontext
+
+ mr 4, 3
+ li 3, 0
+ b __swapcontext
+
+.weak setcontext
+setcontext = __setcontext
diff --git a/arch/ppc64/setcontext.c b/arch/ppc64/setcontext.c
deleted file mode 100644
index 59c65b4..0000000
--- a/arch/ppc64/setcontext.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2018 William Pitcock <nenolod@dereferenced.org>
- *
- * 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.
- */
-
-#define _GNU_SOURCE
-#include <stddef.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <string.h>
-#include <stdint.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/syscall.h>
-
-
-int
-__setcontext(const ucontext_t *ucp)
-{
-#ifdef SYS_swapcontext
- int r;
-
- r = syscall(SYS_swapcontext, NULL, (void *) ucp, sizeof(ucontext_t));
- if (r < 0)
- {
- errno = -r;
- return -1;
- }
-
- return r;
-#else
- errno = ENOSYS;
- return -1;
-#endif
-}
-
-
-extern __typeof(__setcontext) setcontext __attribute__((weak, __alias__("__setcontext")));
diff --git a/arch/ppc64/swapcontext.S b/arch/ppc64/swapcontext.S
new file mode 100644
index 0000000..982537a
--- /dev/null
+++ b/arch/ppc64/swapcontext.S
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+
+.global __swapcontext
+__swapcontext:
+ addis 2, 12, .TOC.-__swapcontext@ha
+ addi 2, 12, .TOC.-__swapcontext@l
+
+ .localentry __swapcontext,.-__swapcontext
+
+ li 0, 249 # SYS_swapcontext
+ li 5, 1696 # sizeof(ucontext_t)
+ sc
+
+.hidden __retfromsyscall
+ b __retfromsyscall
+
+.weak swapcontext
+swapcontext = __swapcontext
diff --git a/arch/ppc64/swapcontext.c b/arch/ppc64/swapcontext.c
deleted file mode 100644
index af14bc2..0000000
--- a/arch/ppc64/swapcontext.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2018 William Pitcock <nenolod@dereferenced.org>
- *
- * 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.
- */
-
-#define _GNU_SOURCE
-#include <stddef.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <string.h>
-#include <stdint.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/syscall.h>
-
-
-int
-__swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
-{
-#ifdef SYS_swapcontext
- int r;
-
- r = syscall(SYS_swapcontext, oucp, ucp, sizeof(ucontext_t));
- if (r < 0)
- {
- errno = -r;
- return -1;
- }
-
- return r;
-#else
- errno = ENOSYS;
- return -1;
-#endif
-}
-
-
-extern __typeof(__swapcontext) swapcontext __attribute__((weak, __alias__("__swapcontext")));
--
2.19.2
From a17b01bc9df4004a1852ba272d452b0f46f5927c Mon Sep 17 00:00:00 2001
From: Bobby Bingham <koorogi@koorogi.info>
Date: Tue, 19 Feb 2019 06:27:28 +0000
Subject: [PATCH 3/3] ppc64: fix stack frame layout
---
arch/ppc64/makecontext.c | 48 +++++++++++++++++-----------------------
1 file changed, 20 insertions(+), 28 deletions(-)
diff --git a/arch/ppc64/makecontext.c b/arch/ppc64/makecontext.c
index 9d6b998..6d9841c 100644
--- a/arch/ppc64/makecontext.c
+++ b/arch/ppc64/makecontext.c
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018 William Pitcock <nenolod@dereferenced.org>
+ * 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
@@ -11,32 +12,39 @@
*/
#define _GNU_SOURCE
-#include <stddef.h>
#include <stdarg.h>
#include <signal.h>
-#include <string.h>
#include <stdint.h>
-
#include "defs.h"
extern void __start_context(void);
+#define SAVEARG(i,val) \
+ do { \
+ *(i < 8 ? &ucp->uc_mcontext.gp_regs[3+i] : argp) = val; \
+ argp++; \
+ } while (0)
+
void
__makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
{
greg_t *sp, *argp;
va_list va;
- int i;
- unsigned int uc_link, stack_args;
+ unsigned int stack_args;
+
+ /* We pass uc_link as an argument, so in total we have argc+1 args */
+ stack_args = argc + 1;
- stack_args = argc > 8 ? argc - 8 : 0;
- uc_link = stack_args + 1;
+ /* We can pass 8 args before needing the stack. If any arg is passed
+ * on the stack, then stack space must be reserved for all args, even
+ * though the first 8 don't need to be stored there */
+ if (stack_args <= 8) stack_args = 0;
sp = (greg_t *) ((uintptr_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
- sp -= (uc_link + 1);
+ sp -= stack_args + 4;
sp = (greg_t *) (((uintptr_t) sp & -16L));
ucp->uc_mcontext.gp_regs[REG_NIP] = (uintptr_t) func;
@@ -45,29 +53,13 @@ __makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
ucp->uc_mcontext.gp_regs[REG_ENTRY] = (uintptr_t) func;
ucp->uc_mcontext.gp_regs[REG_R31] = (uintptr_t) ucp->uc_link;
- sp[0] = (uintptr_t) &__start_context;
- sp[uc_link] = (uintptr_t) ucp->uc_link;
- argp = &sp[2];
+ sp[0] = 0;
+ argp = &sp[4];
va_start(va, argc);
- for (i = 0; i < argc; i++)
- switch (i)
- {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- ucp->uc_mcontext.gp_regs[i + 3] = va_arg (va, greg_t);
- break;
- default:
- *argp++ = va_arg (va, greg_t);
- break;
- }
+ for (int i = 0; i < argc; i++) SAVEARG(i, va_arg(va, greg_t));
+ SAVEARG(argc, (uintptr_t) ucp->uc_link);
va_end(va);
}
--
2.19.2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment