Skip to contentSkip to conversation

linux-user: Add syscall dispatch support

PATCH Superseded Arusekk 2 participants 2 messages Jun 4, 2025 – Jun 23, 2025 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Arusekk

author
#0 lore raw

PATCH v2 1/1 linux-user: Add syscall dispatch support

This commit adds support for syscall user dispatch (SUD), the prctl(PR_SET_SYSCALL_USER_DISPATCH) function, in the Linux userspace emulator.

It is implemented as a fully host-independent function, by forcing a SIGSYS early during syscall handling, if the PC is outside the allowed range and is not invoked by a vdso sigreturn trampoline.

Actual kernel only does the vdso sigreturn bypass for x86 ia32 ABI. Other architectures just follow the normal pc range for vdso. The kernel support is also limited to architectures with CONFIG_GENERIC_ENTRY (currently x86, s390, riscv and loongarch). For simplicity and consistency, all architectures are made to behave the same way, in case the kernel implements SUD for some other architectures one day.

Tested on [uglendix][1], will probably also apply to software like tiny-wine, rpcsx, limbo, lazypoline, vicar, sysfail and endokernel, to name a few.

[1]: https://sr.ht/~arusekk/uglendix

---
Changes from v1: rebase, reword, use TARGET_* consts, add vdso sigreturn bypass.

Not sure, is it better to have wider support, or to precisely follow kernel logic?
I did the most laborous version, in case wider support is desired. And I think it is.
In the other case, all the added functions can just return false, and the prctl needs to return EINVAL on unsupported architectures.

The files are not in the alphabetic order, architecture-specific changes are moved to top for easier review.

v1: https://lore.kernel.org/qemu-devel/[email protected]/

 linux-user/aarch64/signal.c     |  5 +++
 linux-user/alpha/signal.c       |  6 +++
 linux-user/arm/signal.c         |  5 +++
 linux-user/hexagon/signal.c     |  5 +++
 linux-user/hppa/signal.c        |  5 +++
 linux-user/i386/signal.c        |  9 +++++
 linux-user/loongarch64/signal.c |  5 +++
 linux-user/m68k/signal.c        |  5 +++
 linux-user/microblaze/signal.c  |  5 +++
 linux-user/mips/signal.c        |  9 +++++
 linux-user/openrisc/signal.c    |  5 +++
 linux-user/ppc/signal.c         |  9 +++++
 linux-user/riscv/signal.c       |  5 +++
 linux-user/s390x/signal.c       |  5 +++
 linux-user/sh4/signal.c         |  5 +++
 linux-user/sparc/signal.c       |  9 +++++
 linux-user/xtensa/signal.c      |  5 +++
 linux-user/main.c               |  2 +
 linux-user/qemu.h               |  5 +++
 linux-user/signal-common.h      |  1 +
 linux-user/syscall.c            | 67 ++++++++++++++++++++++++++++++++-
 linux-user/syscall_defs.h       |  6 +++
 22 files changed, 182 insertions(+), 1 deletion(-)
linux-user/aarch64/signal.c +5 -0
index bc7a13800d..f15548b0d7 100644
@@ -746,3 +746,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = sigtramp_page;
     unlock_user(tramp, sigtramp_page, 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/alpha/signal.c +6 -0
index 896c2c148a..48d6e4f790 100644
@@ -275,3 +275,9 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 6 * 4);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_sigreturn + 3 * 4
+        || pc == default_rt_sigreturn + 3 * 4;
+}
linux-user/arm/signal.c +5 -0
index 8db1c4b233..53430908cd 100644
@@ -644,3 +644,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, total_size);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return (abi_ulong)(pc - default_sigreturn) <= 8 * RETCODE_BYTES;
+}
linux-user/hexagon/signal.c +5 -0
index 492b51f155..751155006e 100644
@@ -291,3 +291,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 4 * 2);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 4 * 2;
+}
linux-user/hppa/signal.c +5 -0
index f6f094c960..e17e3527b4 100644
@@ -216,3 +216,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = (sigtramp_page + 8) | 3;
     unlock_user(tramp, sigtramp_page, 6*4);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == (default_rt_sigreturn & ~3) + 4*4;
+}
linux-user/i386/signal.c +9 -0
index 0f11dba831..63f0d2562d 100644
@@ -865,3 +865,12 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     unlock_user(tramp, sigtramp_page, 2 * 8);
 }
 #endif
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifndef TARGET_X86_64
+    if (pc == default_sigreturn + 8 || pc == default_rt_sigreturn + 7)
+        return true;
+#endif
+    return false;
+}
linux-user/loongarch64/signal.c +5 -0
index 1a322f9697..fedf297ca8 100644
@@ -452,3 +452,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = sigtramp_page;
     unlock_user(tramp, sigtramp_page, 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/m68k/signal.c +5 -0
index 77555781aa..c6af2baee9 100644
@@ -408,3 +408,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 4 + 6);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_sigreturn + 4 || pc == default_rt_sigreturn + 6;
+}
linux-user/microblaze/signal.c +5 -0
index f6d47d76ff..2027858025 100644
@@ -230,3 +230,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = sigtramp_page;
     unlock_user(tramp, sigtramp_page, 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/mips/signal.c +9 -0
index d69a5d73dd..88cbb081bf 100644
@@ -393,3 +393,12 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 2 * 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifdef TARGET_ARCH_HAS_SETUP_FRAME
+    if (pc == default_sigreturn + 8)
+        return true;
+#endif
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/openrisc/signal.c +5 -0
index cb74a9fe5e..c368069980 100644
@@ -175,3 +175,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = sigtramp_page;
     unlock_user(tramp, sigtramp_page, 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/ppc/signal.c +9 -0
index 24e5a02a78..b116aac415 100644
@@ -730,3 +730,12 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 2 * 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifdef TARGET_ARCH_HAS_SETUP_FRAME
+    if (pc == default_sigreturn + 8)
+        return true;
+#endif
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/riscv/signal.c +5 -0
index 358fa1d82d..5ae889e8b9 100644
@@ -213,3 +213,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = sigtramp_page;
     unlock_user(tramp, sigtramp_page, 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/s390x/signal.c +5 -0
index df49c24708..17a194104f 100644
@@ -419,3 +419,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 2 + 2);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_sigreturn + 2 || pc == default_rt_sigreturn + 2;
+}
linux-user/sh4/signal.c +5 -0
index 9ecc026fae..1c34dd27c1 100644
@@ -346,3 +346,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 2 * 6);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_sigreturn + 6 || pc == default_rt_sigreturn + 6;
+}
linux-user/sparc/signal.c +9 -0
index 8181b8b92c..e06b5ad2a2 100644
@@ -789,3 +789,12 @@ do_sigsegv:
     force_sig(TARGET_SIGSEGV);
 }
 #endif /* TARGET_SPARC64 */
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifdef TARGET_ABI32
+    if (pc == default_sigreturn + 8 || pc == default_rt_sigreturn + 8)
+        return true;
+#endif
+    return false;
+}
linux-user/xtensa/signal.c +5 -0
index ef8b0c3a27..0ce3d826d7 100644
@@ -303,3 +303,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     install_sigtramp(tramp);
     unlock_user(tramp, sigtramp_page, 6);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 6;
+}
linux-user/main.c +2 -0
index 5ac5b55dc6..4f40ec5a94 100644
@@ -231,6 +231,8 @@ void init_task_state(TaskState *ts)
         ts->start_boottime += bt.tv_nsec * (uint64_t) ticks_per_sec /
                               NANOSECONDS_PER_SECOND;
     }
+
+    ts->sys_dispatch_len = (abi_ulong)~0ULL;
 }
 
 CPUArchState *cpu_copy(CPUArchState *env)
linux-user/qemu.h +5 -0
index 0b19fa43e6..52cf9d67bd 100644
@@ -161,6 +161,11 @@ struct TaskState {
     /* This thread's sigaltstack, if it has one */
     struct target_sigaltstack sigaltstack_used;
 
+    /* This thread's SYSCALL_USER_DISPATCH state, len=~0 means disabled */
+    abi_ulong sys_dispatch;
+    abi_ulong sys_dispatch_len;
+    abi_ulong sys_dispatch_selector;
+
     /* Start time of task after system boot in clock ticks */
     uint64_t start_boottime;
 };
linux-user/signal-common.h +1 -0
index 196d2406f8..7b17ac3221 100644
@@ -27,6 +27,7 @@ extern abi_ulong default_sigreturn;
 extern abi_ulong default_rt_sigreturn;
 
 void setup_sigtramp(abi_ulong tramp_page);
+bool is_vdso_sigreturn(abi_ulong pc);
 
 int on_sig_stack(unsigned long sp);
 int sas_ss_flags(unsigned long sp);
linux-user/syscall.c +66 -1

103 lines not on this page · raw message

linux-user/syscall_defs.h +6 -0
index 5d22759992..7812253455 100644
@@ -693,6 +693,12 @@ typedef struct target_siginfo {
 #define TARGET_TRAP_HWBKPT      (4)     /* hardware breakpoint/watchpoint */
 #define TARGET_TRAP_UNK         (5)     /* undiagnosed trap */
 
+/*
+ * SIGSYS si_codes
+ */
+#define TARGET_SYS_SECCOMP       (1)  /* seccomp triggered */
+#define TARGET_SYS_USER_DISPATCH (2)  /* syscall user dispatch triggered */
+
 /*
  * SIGEMT si_codes
  */
Signature · 1 line
2.49.0
#1 lore raw
Quoted 5 lines · Arusekk
linux-user/i386/signal.c +0 -0
@@ -865,3 +865,12 @@ void setup_sigtramp(abi_ulong sigtramp_page)
      unlock_user(tramp, sigtramp_page, 2 * 8);
  }

linux-user/i386/signal.c void setup_sigtramp(abi_ulong sigtramp_page) · line 876

Quoted 10 lines
linux-user/i386/signal.c +9 -0
  #endif
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifndef TARGET_X86_64
+    if (pc == default_sigreturn + 8 || pc == default_rt_sigreturn + 7)
+        return true;
+#endif
+    return false;
+}

Braces required.

linux-user/mips/signal.c void setup_sigtramp(abi_ulong sigtramp_page) · line 402

Quoted 13 lines
linux-user/mips/signal.c +7 -0
@@ -393,3 +393,12 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
      unlock_user(tramp, sigtramp_page, 2 * 8);
  }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifdef TARGET_ARCH_HAS_SETUP_FRAME
+    if (pc == default_sigreturn + 8)
+        return true;
+#endif

Likewise.

linux-user/mips/signal.c void setup_sigtramp(abi_ulong sigtramp_page) · line 739

Quoted 12 lines
linux-user/mips/signal.c +8 -0
+++ b/linux-user/ppc/signal.c
@@ -730,3 +730,12 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
      unlock_user(tramp, sigtramp_page, 2 * 8);
  }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifdef TARGET_ARCH_HAS_SETUP_FRAME
+    if (pc == default_sigreturn + 8)
+        return true;
+#endif

Likewise.

linux-user/mips/signal.c do_sigsegv: · line 798

Quoted 12 lines
linux-user/mips/signal.c +8 -0
+++ b/linux-user/sparc/signal.c
@@ -789,3 +789,12 @@ do_sigsegv:
      force_sig(TARGET_SIGSEGV);
  }
  #endif /* TARGET_SPARC64 */
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifdef TARGET_ABI32
+    if (pc == default_sigreturn + 8 || pc == default_rt_sigreturn + 8)
+        return true;
+#endif

Likewise.

linux-user/mips/signal.c struct TaskState { · line 167

Quoted 9 lines
linux-user/mips/signal.c +5 -0
+++ b/linux-user/qemu.h
@@ -161,6 +161,11 @@ struct TaskState {
      /* This thread's sigaltstack, if it has one */
      struct target_sigaltstack sigaltstack_used;
 
+    /* This thread's SYSCALL_USER_DISPATCH state, len=~0 means disabled */
+    abi_ulong sys_dispatch;
+    abi_ulong sys_dispatch_len;
+    abi_ulong sys_dispatch_selector;

dispatch and selector should probably be "vaddr" for self-documentation.

Quoted 27 lines
linux-user/syscall.c +12 -0
index fc37028597..027e4237b9 100644
@@ -6340,6 +6340,10 @@ abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  #endif
  #ifndef PR_SET_SYSCALL_USER_DISPATCH
  # define PR_SET_SYSCALL_USER_DISPATCH 59
+# define PR_SYS_DISPATCH_OFF 0
+# define PR_SYS_DISPATCH_ON 1
+# define SYSCALL_DISPATCH_FILTER_ALLOW 0
+# define SYSCALL_DISPATCH_FILTER_BLOCK 1
  #endif
  #ifndef PR_SME_SET_VL
  # define PR_SME_SET_VL  63
@@ -6469,6 +6473,37 @@ static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
      case PR_SET_UNALIGN:
          return do_prctl_set_unalign(env, arg2);
 
+    case PR_SET_SYSCALL_USER_DISPATCH:
+        {
+            TaskState *ts = get_task_state(env_cpu(env));
+            switch (arg2) {
+            case PR_SYS_DISPATCH_OFF:
+                if (arg3 || arg4 || arg5) {
+                    return -TARGET_EINVAL;
+                }

linux-user/syscall.c static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2, · line 6493

Quoted 10 lines
linux-user/syscall.c +10 -0
+                ts->sys_dispatch_len = (abi_ulong)~0ULL;
+                return 0;
+            case PR_SYS_DISPATCH_ON:
+                {
+                    uint8_t sb;
+
+                    if (arg3 && arg3 + arg4 <= arg3) {
+                        return -TARGET_EINVAL;
+                    }
+                    if (arg5 && get_user_u8(sb, arg5)) {

Use access_ok, just like the kernel.

linux-user/syscall.c static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2, · line 6505

Quoted 12 lines
linux-user/syscall.c +12 -0
+                        return -TARGET_EFAULT;
+                    }
+                    (void)sb;  /* used later, only checked for access */
+                    ts->sys_dispatch = arg3;
+                    ts->sys_dispatch_len = arg4;
+                    ts->sys_dispatch_selector = arg5;
+                    return 0;
+                }
+            default:
+                return -TARGET_EINVAL;
+            }
+        }

I think this should be split out to do_prctl_syscall_user_dispatch().

Quoted 13 lines
linux-user/syscall.c +1 -1
+
      case PR_CAP_AMBIENT:
      case PR_CAPBSET_READ:
      case PR_CAPBSET_DROP:
@@ -6523,7 +6558,6 @@ static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
      case PR_SET_MM:
      case PR_GET_SECCOMP:
      case PR_SET_SECCOMP:
-    case PR_SET_SYSCALL_USER_DISPATCH:
      case PR_GET_THP_DISABLE:
      case PR_SET_THP_DISABLE:
      case PR_GET_TSC:

linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 13919

Quoted 5 lines
linux-user/syscall.c +2 -0
@@ -13881,12 +13915,37 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
      return ret;
  }
 
+static bool sys_dispatch(abi_ulong pc, TaskState *ts)
+{

Move pc - offset < len check here.

Quoted 33 lines
linux-user/syscall.c +23 -0
+    if (unlikely(is_vdso_sigreturn(pc))) {
+        return false;
+    }
+    if (likely(ts->sys_dispatch_selector)) {
+        uint8_t sb;
+        if (get_user_u8(sb, ts->sys_dispatch_selector)) {
+            force_sig(TARGET_SIGSEGV);
+            return true;
+        }
+        if (likely(sb == SYSCALL_DISPATCH_FILTER_ALLOW)) {
+            return false;
+        }
+        if (unlikely(sb != SYSCALL_DISPATCH_FILTER_BLOCK)) {
+            force_sig(TARGET_SIGSYS);
+            return true;
+        }
+    }
+    force_sig_fault(TARGET_SIGSYS, TARGET_SYS_USER_DISPATCH, pc);
+    return true;
+}
+
  abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
                      abi_long arg2, abi_long arg3, abi_long arg4,
                      abi_long arg5, abi_long arg6, abi_long arg7,
                      abi_long arg8)
  {
      CPUState *cpu = env_cpu(cpu_env);
+    TaskState *ts = get_task_state(cpu);
+    vaddr pc = cpu->cc->get_pc(cpu);
      abi_long ret;
 
  #ifdef DEBUG_ERESTARTSYS

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

Quoted 4 lines
linux-user/syscall.c +1 -0
@@ -13903,6 +13962,12 @@ abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
      }
  #endif
 
+    if (unlikely(pc - ts->sys_dispatch >= ts->sys_dispatch_len)) {

This isn't correct. The kernel expression is gated by a bit in the work flags.

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

Quoted 4 lines
linux-user/syscall.c +4 -0
+        if (sys_dispatch(pc, ts)) {
+            return -QEMU_ESIGRETURN;
+        }
+    }

Better as:

    if (unlikely(ts->sys_dispatch_len != ~0)) {
        vaddr pc = cpu->cc->get_pc(cpu);
        if (sys_dispatch(pc, ts)) {
            return -QEMU_ESIGRETURN;
        }
    }

r~