+/*** Low-level parsing of syscalls ***/
+
+#ifdef CONFIG_BOX_AMD64
+typedef uint64_t arg_t;
+#else
+typedef uint32_t arg_t;
+#endif
+
+struct syscall_args {
+ arg_t sys;
+ arg_t arg1, arg2, arg3;
+ arg_t result;
+ struct user user;
+};
+
+#ifdef CONFIG_BOX_AMD64
+
+static void
+get_syscall_args(struct syscall_args *a)
+{
+ if (ptrace(PTRACE_GETREGS, box_pid, NULL, &a->user) < 0)
+ die("ptrace(PTRACE_GETREGS): %m");
+ a->sys = a->user.regs.orig_rax;
+ a->arg1 = a->user.regs.rdi;
+ a->arg2 = a->user.regs.rsi;
+ a->arg3 = a->user.regs.rdx;
+ a->result = a->user.regs.rax;
+
+ // FIXME: Check that it's really a 64-bit syscall
+}
+
+static void
+set_syscall_nr(struct syscall_args *a, arg_t sys)
+{
+ a->sys = sys;
+ a->user.regs.orig_rax = sys;
+ if (ptrace(PTRACE_SETREGS, box_pid, NULL, &a->user) < 0)
+ die("ptrace(PTRACE_SETREGS): %m");
+}
+
+#else
+
+static void
+get_syscall_args(struct syscall_args *a)
+{
+ if (ptrace(PTRACE_GETREGS, box_pid, NULL, &a->user) < 0)
+ die("ptrace(PTRACE_GETREGS): %m");
+ a->sys = a->user.regs.orig_eax;
+ a->arg1 = a->user.regs.ebx;
+ a->arg2 = a->user.regs.ecx;
+ a->arg3 = a->user.regs.edx;
+ a->result = a->user.regs.eax;
+}
+
+static void
+set_syscall_nr(struct syscall_args *a, arg_t sys)
+{
+ a->sys = sys;
+ a->user.regs.orig_eax = sys;
+ if (ptrace(PTRACE_SETREGS, box_pid, NULL, &a->user) < 0)
+ die("ptrace(PTRACE_SETREGS): %m");
+}
+
+#endif
+