Skip to contentSkip to conversation

linux-user/ppc: Implement swapcontext syscall

PATCH In review Richard Henderson 5 participants 8 messages Jul 18, 2018 – Jul 22, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user/ppc: Implement swapcontext syscall

This allows the tests generated by debian-powerpc-user-cross to function properly, especially tests/test-coroutine.

Technically this syscall is available to both ppc32 and ppc64, but only ppc32 glibc actually uses it. Thus the ppc64 path is untested.

---
 linux-user/qemu.h       |  2 ++
 linux-user/ppc/signal.c | 56 +++++++++++++++++++++++++++++++++++++++++
 linux-user/syscall.c    |  6 +++++
 3 files changed, 64 insertions(+)
linux-user/qemu.h +2 -0
index bb85c81aa4..e0963676c7 100644
@@ -395,6 +395,8 @@ long do_sigreturn(CPUArchState *env);
 long do_rt_sigreturn(CPUArchState *env);
 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
+abi_long do_swapcontext(CPUArchState *env, abi_ulong uold_ctx,
+                        abi_ulong unew_ctx, abi_long ctx_size);
 /**
  * block_signals: block all signals while handling this guest syscall
  *
linux-user/ppc/signal.c +56 -0

59 lines not on this page · raw message

linux-user/syscall.c +6 -0
index 3df3bdffb2..dfc851cc35 100644
@@ -12790,6 +12790,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         ret = get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
         break;
 #endif
+#ifdef TARGET_NR_swapcontext
+    case TARGET_NR_swapcontext:
+        /* PowerPC specific.  */
+        ret = do_swapcontext(cpu_env, arg1, arg2, arg3);
+        break;
+#endif
 
     default:
     unimplemented:
Signature · 1 line
2.17.1
#1 lore raw

Hi Richard,

Quoted 32 lines · Richard Henderson

This allows the tests generated by debian-powerpc-user-cross to function properly, especially tests/test-coroutine.

Technically this syscall is available to both ppc32 and ppc64, but only ppc32 glibc actually uses it. Thus the ppc64 path is untested.

---
 linux-user/qemu.h       |  2 ++
 linux-user/ppc/signal.c | 56 +++++++++++++++++++++++++++++++++++++++++
 linux-user/syscall.c    |  6 +++++
 3 files changed, 64 insertions(+)
linux-user/qemu.h +2 -0
index bb85c81aa4..e0963676c7 100644
@@ -395,6 +395,8 @@ long do_sigreturn(CPUArchState *env);
 long do_rt_sigreturn(CPUArchState *env);
 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
+abi_long do_swapcontext(CPUArchState *env, abi_ulong uold_ctx,
+                        abi_ulong unew_ctx, abi_long ctx_size);
 /**
  * block_signals: block all signals while handling this guest syscall
  *
linux-user/ppc/signal.c +0 -0
index ef4c518f11..2ae120a2bc 100644

linux-user/ppc/signal.c sigsegv: · line 679

Quoted 5 lines
linux-user/ppc/signal.c +2 -0
@@ -675,3 +675,59 @@ sigsegv:
     force_sig(TARGET_SIGSEGV);
     return -TARGET_QEMU_ESIGRETURN;
 }
+
+/* This syscall implements {get,set,swap}context for userland.  */

This comment confuses me because do_setcontext() is available at line 625.

linux-user/ppc/signal.c sigsegv: · line 690

Quoted 11 lines
linux-user/ppc/signal.c +11 -0
+abi_long do_swapcontext(CPUArchState *env, abi_ulong uold_ctx,
+                        abi_ulong unew_ctx, abi_long ctx_size)
+{
+    struct target_ucontext *uctx;
+    struct target_mcontext *mctx;
+
+    /* For ppc32, ctx_size is "reserved for future use".
+     * For ppc64, we do not yet support the VSX extension.
+     */
+    if (ctx_size < sizeof(struct target_ucontext)) {
+        return -TARGET_EINVAL;

Shouldn't this be -TARGET_ENOMEM?

swapcontext(3):
ERRORS
ENOMEM Insufficient stack space left.

Quoted 61 lines
linux-user/ppc/signal.c +43 -0
+    }
+
+    if (uold_ctx) {
+        TaskState *ts = (TaskState *)thread_cpu->opaque;
+
+        if (!lock_user_struct(VERIFY_WRITE, uctx, uold_ctx, 1)) {
+            return -TARGET_EFAULT;
+        }
+
+#ifdef TARGET_PPC64
+        mctx = &uctx->tuc_sigcontext.mcontext;
+#else
+        /* ??? The kernel aligns the pointer down here into padding, but
+         * in setup_rt_frame we don't.  Be self-compatible for now.
+         */
+        mctx = &uctx->tuc_mcontext;
+        __put_user(h2g(mctx), &uctx->tuc_regs);
+#endif
+
+        save_user_regs(env, mctx);
+        host_to_target_sigset(&uctx->tuc_sigmask, &ts->signal_mask);
+
+        unlock_user_struct(uctx, uold_ctx, 1);
+    }
+
+    if (unew_ctx) {
+        int err;
+
+        if (!lock_user_struct(VERIFY_READ, uctx, unew_ctx, 1)) {
+            return -TARGET_EFAULT;
+        }
+        err = do_setcontext(uctx, env, 0);
+        unlock_user_struct(uctx, unew_ctx, 1);
+
+        if (err) {
+            /* We cannot return to a partially updated context.  */
+            force_sig(TARGET_SIGSEGV);
+        }
+        return -TARGET_QEMU_ESIGRETURN;
+    }
+
+    return 0;
+}
linux-user/syscall.c +6 -0
index 3df3bdffb2..dfc851cc35 100644
@@ -12790,6 +12790,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         ret = get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
         break;
 #endif
+#ifdef TARGET_NR_swapcontext
+    case TARGET_NR_swapcontext:
+        /* PowerPC specific.  */
+        ret = do_swapcontext(cpu_env, arg1, arg2, arg3);
+        break;
+#endif
 
     default:
     unimplemented:
#2 lore raw
Quoted 4 lines · Philippe Mathieu-Daudé

linux-user/ppc/signal.c sigsegv: · line 679

Quoted 2 lines
linux-user/ppc/signal.c +2 -0
+
+/* This syscall implements {get,set,swap}context for userland.  */

This comment confuses me because do_setcontext() is available at line 625.

But that's not wired up as a syscall.

Quoted 11 lines

linux-user/ppc/signal.c sigsegv: · line 690

Quoted 5 lines
linux-user/ppc/signal.c +5 -0
+    /* For ppc32, ctx_size is "reserved for future use".
+     * For ppc64, we do not yet support the VSX extension.
+     */
+    if (ctx_size < sizeof(struct target_ucontext)) {
+        return -TARGET_EINVAL;

Shouldn't this be -TARGET_ENOMEM?

swapcontext(3):
ERRORS
ENOMEM Insufficient stack space left.

No. Please compare against the syscall, not the libc interface.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/powerpc/kernel/signal_32.c#n1045

r~

#3 lore raw
Quoted 14 lines · Richard Henderson
Quoted 4 lines · Philippe Mathieu-Daudé

linux-user/ppc/signal.c sigsegv: · line 679

Quoted 2 lines
linux-user/ppc/signal.c +2 -0
+
+/* This syscall implements {get,set,swap}context for userland.  */

This comment confuses me because do_setcontext() is available at line 625.

But that's not wired up as a syscall.

Quoted 6 lines
Quoted 5 lines
linux-user/ppc/signal.c +5 -0
+    /* For ppc32, ctx_size is "reserved for future use".
+     * For ppc64, we do not yet support the VSX extension.
+     */
+    if (ctx_size < sizeof(struct target_ucontext)) {
+        return -TARGET_EINVAL;
Quoted 7 lines
Quoted 5 lines

Shouldn't this be -TARGET_ENOMEM?

swapcontext(3):
ERRORS
ENOMEM Insufficient stack space left.

No. Please compare against the syscall, not the libc interface.

Thanks, I felt I was missing something with the 3rd syscall argument.

Quoted 3 lines

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/powerpc/kernel/signal_32.c#n1045

Tested-by Reviewed-by on v1 1/1
#4 lore raw
In reply to Richard Henderson
Quoted 8 lines · Richard Henderson

This allows the tests generated by debian-powerpc-user-cross to function properly, especially tests/test-coroutine.

Technically this syscall is available to both ppc32 and ppc64, but only ppc32 glibc actually uses it. Thus the ppc64 path is untested.

and a caveat-ed:

I'm confused by the lock_user_struct/unlock_user_struct which AFAICT are basically access checks. Is there an implied locking I'm missing?

Quoted 90 lines
---
 linux-user/qemu.h       |  2 ++
 linux-user/ppc/signal.c | 56 +++++++++++++++++++++++++++++++++++++++++
 linux-user/syscall.c    |  6 +++++
 3 files changed, 64 insertions(+)
linux-user/qemu.h +2 -0
index bb85c81aa4..e0963676c7 100644
@@ -395,6 +395,8 @@ long do_sigreturn(CPUArchState *env);
 long do_rt_sigreturn(CPUArchState *env);
 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
+abi_long do_swapcontext(CPUArchState *env, abi_ulong uold_ctx,
+                        abi_ulong unew_ctx, abi_long ctx_size);
 /**
  * block_signals: block all signals while handling this guest syscall
  *
linux-user/ppc/signal.c +56 -0
index ef4c518f11..2ae120a2bc 100644
@@ -675,3 +675,59 @@ sigsegv:
     force_sig(TARGET_SIGSEGV);
     return -TARGET_QEMU_ESIGRETURN;
 }
+
+/* This syscall implements {get,set,swap}context for userland.  */
+abi_long do_swapcontext(CPUArchState *env, abi_ulong uold_ctx,
+                        abi_ulong unew_ctx, abi_long ctx_size)
+{
+    struct target_ucontext *uctx;
+    struct target_mcontext *mctx;
+
+    /* For ppc32, ctx_size is "reserved for future use".
+     * For ppc64, we do not yet support the VSX extension.
+     */
+    if (ctx_size < sizeof(struct target_ucontext)) {
+        return -TARGET_EINVAL;
+    }
+
+    if (uold_ctx) {
+        TaskState *ts = (TaskState *)thread_cpu->opaque;
+
+        if (!lock_user_struct(VERIFY_WRITE, uctx, uold_ctx, 1)) {
+            return -TARGET_EFAULT;
+        }
+
+#ifdef TARGET_PPC64
+        mctx = &uctx->tuc_sigcontext.mcontext;
+#else
+        /* ??? The kernel aligns the pointer down here into padding, but
+         * in setup_rt_frame we don't.  Be self-compatible for now.
+         */
+        mctx = &uctx->tuc_mcontext;
+        __put_user(h2g(mctx), &uctx->tuc_regs);
+#endif
+
+        save_user_regs(env, mctx);
+        host_to_target_sigset(&uctx->tuc_sigmask, &ts->signal_mask);
+
+        unlock_user_struct(uctx, uold_ctx, 1);
+    }
+
+    if (unew_ctx) {
+        int err;
+
+        if (!lock_user_struct(VERIFY_READ, uctx, unew_ctx, 1)) {
+            return -TARGET_EFAULT;
+        }
+        err = do_setcontext(uctx, env, 0);
+        unlock_user_struct(uctx, unew_ctx, 1);
+
+        if (err) {
+            /* We cannot return to a partially updated context.  */
+            force_sig(TARGET_SIGSEGV);
+        }
+        return -TARGET_QEMU_ESIGRETURN;
+    }
+
+    return 0;
+}
linux-user/syscall.c +0 -0
index 3df3bdffb2..dfc851cc35 100644
@@ -12790,6 +12790,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         ret = get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
         break;

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 12801

Quoted 10 lines
linux-user/syscall.c +6 -0
 #endif
+#ifdef TARGET_NR_swapcontext
+    case TARGET_NR_swapcontext:
+        /* PowerPC specific.  */
+        ret = do_swapcontext(cpu_env, arg1, arg2, arg3);
+        break;
+#endif
 
     default:
     unimplemented:

--
Alex Bennée

#5 lore raw
Quoted 2 lines · Alex Bennée

I'm confused by the lock_user_struct/unlock_user_struct which AFAICT are basically access checks. Is there an implied locking I'm missing?

I have no idea why those have that name. There's no lock, just a validation of the underlying pages and a transform to the corresponding host address.

r~

#6 lore raw
Quoted 7 lines · Richard Henderson
Quoted 2 lines · Alex Bennée

I'm confused by the lock_user_struct/unlock_user_struct which AFAICT are basically access checks. Is there an implied locking I'm missing?

I have no idea why those have that name. There's no lock, just a validation of the underlying pages and a transform to the corresponding host address.

Mmm, it's unclear. It came in via 53a5960aadd542dd, and the impression I get is that the idea was to try to decouple from knowing that the guest memory layout is flat, as a preparation for possible support for softmmu-in-usermode...

thanks
-- PMM

Reviewed-by on v1 1/1
#7 lore raw
In reply to Richard Henderson

Le 18/07/2018 à 22:06, Richard Henderson a écrit :

Quoted 7 lines

This allows the tests generated by debian-powerpc-user-cross to function properly, especially tests/test-coroutine.

Technically this syscall is available to both ppc32 and ppc64, but only ppc32 glibc actually uses it. Thus the ppc64 path is untested.

Quoted 7 lines
---
 linux-user/qemu.h       |  2 ++
 linux-user/ppc/signal.c | 56 +++++++++++++++++++++++++++++++++++++++++
 linux-user/syscall.c    |  6 +++++
 3 files changed, 64 insertions(+)