qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Laurent Vivier <laurent@vivier.eu>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Riku Voipio <riku.voipio@iki.fi>,
	Richard Henderson <richard.henderson@linaro.org>,
	Laurent Vivier <laurent@vivier.eu>
Subject: [PULL v2 03/37] linux-user/i386: Emulate x86_64 vsyscalls
Date: Tue, 17 Mar 2020 16:50:42 +0100	[thread overview]
Message-ID: <20200317155116.1227513-4-laurent@vivier.eu> (raw)
In-Reply-To: <20200317155116.1227513-1-laurent@vivier.eu>

From: Richard Henderson <richard.henderson@linaro.org>

Notice the magic page during translate, much like we already
do for the arm32 commpage.  At runtime, raise an exception to
return cpu_loop for emulation.

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20200213032223.14643-4-richard.henderson@linaro.org>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
 linux-user/i386/cpu_loop.c | 108 +++++++++++++++++++++++++++++++++++++
 target/i386/cpu.h          |   7 +++
 target/i386/translate.c    |  14 ++++-
 3 files changed, 128 insertions(+), 1 deletion(-)

diff --git a/linux-user/i386/cpu_loop.c b/linux-user/i386/cpu_loop.c
index e217cca5ee1e..70cde417e605 100644
--- a/linux-user/i386/cpu_loop.c
+++ b/linux-user/i386/cpu_loop.c
@@ -92,6 +92,109 @@ static void gen_signal(CPUX86State *env, int sig, int code, abi_ptr addr)
     queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
 }
 
+#ifdef TARGET_X86_64
+static bool write_ok_or_segv(CPUX86State *env, abi_ptr addr, size_t len)
+{
+    /*
+     * For all the vsyscalls, NULL means "don't write anything" not
+     * "write it at address 0".
+     */
+    if (addr == 0 || access_ok(VERIFY_WRITE, addr, len)) {
+        return true;
+    }
+
+    env->error_code = PG_ERROR_W_MASK | PG_ERROR_U_MASK;
+    gen_signal(env, TARGET_SIGSEGV, TARGET_SEGV_MAPERR, addr);
+    return false;
+}
+
+/*
+ * Since v3.1, the kernel traps and emulates the vsyscall page.
+ * Entry points other than the official generate SIGSEGV.
+ */
+static void emulate_vsyscall(CPUX86State *env)
+{
+    int syscall;
+    abi_ulong ret;
+    uint64_t caller;
+
+    /*
+     * Validate the entry point.  We have already validated the page
+     * during translation to get here; now verify the offset.
+     */
+    switch (env->eip & ~TARGET_PAGE_MASK) {
+    case 0x000:
+        syscall = TARGET_NR_gettimeofday;
+        break;
+    case 0x400:
+        syscall = TARGET_NR_time;
+        break;
+    case 0x800:
+        syscall = TARGET_NR_getcpu;
+        break;
+    default:
+        goto sigsegv;
+    }
+
+    /*
+     * Validate the return address.
+     * Note that the kernel treats this the same as an invalid entry point.
+     */
+    if (get_user_u64(caller, env->regs[R_ESP])) {
+        goto sigsegv;
+    }
+
+    /*
+     * Validate the the pointer arguments.
+     */
+    switch (syscall) {
+    case TARGET_NR_gettimeofday:
+        if (!write_ok_or_segv(env, env->regs[R_EDI],
+                              sizeof(struct target_timeval)) ||
+            !write_ok_or_segv(env, env->regs[R_ESI],
+                              sizeof(struct target_timezone))) {
+            return;
+        }
+        break;
+    case TARGET_NR_time:
+        if (!write_ok_or_segv(env, env->regs[R_EDI], sizeof(abi_long))) {
+            return;
+        }
+        break;
+    case TARGET_NR_getcpu:
+        if (!write_ok_or_segv(env, env->regs[R_EDI], sizeof(uint32_t)) ||
+            !write_ok_or_segv(env, env->regs[R_ESI], sizeof(uint32_t))) {
+            return;
+        }
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    /*
+     * Perform the syscall.  None of the vsyscalls should need restarting.
+     */
+    ret = do_syscall(env, syscall, env->regs[R_EDI], env->regs[R_ESI],
+                     env->regs[R_EDX], env->regs[10], env->regs[8],
+                     env->regs[9], 0, 0);
+    g_assert(ret != -TARGET_ERESTARTSYS);
+    g_assert(ret != -TARGET_QEMU_ESIGRETURN);
+    if (ret == -TARGET_EFAULT) {
+        goto sigsegv;
+    }
+    env->regs[R_EAX] = ret;
+
+    /* Emulate a ret instruction to leave the vsyscall page.  */
+    env->eip = caller;
+    env->regs[R_ESP] += 8;
+    return;
+
+ sigsegv:
+    /* Like force_sig(SIGSEGV).  */
+    gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
+}
+#endif
+
 void cpu_loop(CPUX86State *env)
 {
     CPUState *cs = env_cpu(env);
@@ -141,6 +244,11 @@ void cpu_loop(CPUX86State *env)
                 env->regs[R_EAX] = ret;
             }
             break;
+#endif
+#ifdef TARGET_X86_64
+        case EXCP_VSYSCALL:
+            emulate_vsyscall(env);
+            break;
 #endif
         case EXCP0B_NOSEG:
         case EXCP0C_STACK:
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index 08b4422f36bd..39be555db3da 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -1001,6 +1001,7 @@ typedef uint64_t FeatureWordArray[FEATURE_WORDS];
 
 #define EXCP_VMEXIT     0x100 /* only for system emulation */
 #define EXCP_SYSCALL    0x101 /* only for user emulation */
+#define EXCP_VSYSCALL   0x102 /* only for user emulation */
 
 /* i386-specific interrupt pending bits.  */
 #define CPU_INTERRUPT_POLL      CPU_INTERRUPT_TGT_EXT_1
@@ -2215,4 +2216,10 @@ static inline bool hyperv_feat_enabled(X86CPU *cpu, int feat)
     return !!(cpu->hyperv_features & BIT(feat));
 }
 
+#if defined(TARGET_X86_64) && \
+    defined(CONFIG_USER_ONLY) && \
+    defined(CONFIG_LINUX)
+# define TARGET_VSYSCALL_PAGE  (UINT64_C(-10) << 20)
+#endif
+
 #endif /* I386_CPU_H */
diff --git a/target/i386/translate.c b/target/i386/translate.c
index d9af8f4078b3..5e5dbb41b0ce 100644
--- a/target/i386/translate.c
+++ b/target/i386/translate.c
@@ -8555,7 +8555,19 @@ static bool i386_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
 static void i386_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
 {
     DisasContext *dc = container_of(dcbase, DisasContext, base);
-    target_ulong pc_next = disas_insn(dc, cpu);
+    target_ulong pc_next;
+
+#ifdef TARGET_VSYSCALL_PAGE
+    /*
+     * Detect entry into the vsyscall page and invoke the syscall.
+     */
+    if ((dc->base.pc_next & TARGET_PAGE_MASK) == TARGET_VSYSCALL_PAGE) {
+        gen_exception(dc, EXCP_VSYSCALL, dc->base.pc_next);
+        return;
+    }
+#endif
+
+    pc_next = disas_insn(dc, cpu);
 
     if (dc->tf || (dc->base.tb->flags & HF_INHIBIT_IRQ_MASK)) {
         /* if single step mode, we generate only one instruction and
-- 
2.24.1



  parent reply	other threads:[~2020-03-17 15:56 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-17 15:50 [PULL v2 00/37] Linux user for 5.0 patches Laurent Vivier
2020-03-17 15:50 ` [PULL v2 01/37] target/i386: Renumber EXCP_SYSCALL Laurent Vivier
2020-03-17 15:50 ` [PULL v2 02/37] linux-user/i386: Split out gen_signal Laurent Vivier
2020-03-17 15:50 ` Laurent Vivier [this message]
2020-03-17 15:50 ` [PULL v2 04/37] linux-user: Add x86_64 vsyscall page to /proc/self/maps Laurent Vivier
2020-03-17 15:50 ` [PULL v2 05/37] linux-user: Flush out implementation of gettimeofday Laurent Vivier
2020-03-17 15:50 ` [PULL v2 06/37] linux-user: Add AT_EXECFN auxval Laurent Vivier
2020-03-17 15:50 ` [PULL v2 07/37] linux-user: do prlimit selectively Laurent Vivier
2020-03-17 15:50 ` [PULL v2 08/37] linux-user: fix socket() strace Laurent Vivier
2020-03-17 15:50 ` [PULL v2 09/37] linux-user: Update TASK_UNMAPPED_BASE for aarch64 Laurent Vivier
2020-03-17 15:50 ` [PULL v2 10/37] linux-user: Protect more syscalls Laurent Vivier
2020-03-17 15:59   ` Alistair Francis
2020-03-17 15:50 ` [PULL v2 11/37] linux-user/syscall: Add support for clock_gettime64/clock_settime64 Laurent Vivier
2020-03-17 15:50 ` [PULL v2 12/37] linux-user/riscv: Update the syscall_nr's to the 5.5 kernel Laurent Vivier
2020-03-17 15:50 ` [PULL v2 13/37] linux-user: introduce parameters to generate syscall_nr.h Laurent Vivier
2020-03-17 15:50 ` [PULL v2 14/37] linux-user, alpha: add syscall table generation support Laurent Vivier
2020-03-17 15:50 ` [PULL v2 15/37] linux-user, hppa: " Laurent Vivier
2020-03-17 15:50 ` [PULL v2 16/37] linux-user, m68k: " Laurent Vivier
2020-03-17 15:50 ` [PULL v2 17/37] linux-user, xtensa: " Laurent Vivier
2020-03-17 15:50 ` [PULL v2 18/37] linux-user, sh4: " Laurent Vivier
2020-03-17 15:50 ` [PULL v2 19/37] linux-user, microblaze: " Laurent Vivier
2020-03-17 15:50 ` [PULL v2 20/37] linux-user, arm: " Laurent Vivier
2020-03-17 15:51 ` [PULL v2 21/37] linux-user, ppc: " Laurent Vivier
2020-03-17 15:51 ` [PULL v2 22/37] linux-user, s390x: remove syscall definitions for !TARGET_S390X Laurent Vivier
2020-03-17 15:51 ` [PULL v2 23/37] linux-user, s390x: add syscall table generation support Laurent Vivier
2020-03-17 15:51 ` [PULL v2 24/37] linux-user, sparc, sparc64: " Laurent Vivier
2020-03-17 15:51 ` [PULL v2 25/37] linux-user, x86_64, i386: cleanup TARGET_NR_arch_prctl Laurent Vivier
2020-03-17 15:51 ` [PULL v2 26/37] linux-user, i386: add syscall table generation support Laurent Vivier
2020-03-17 15:51 ` [PULL v2 27/37] linux-user, x86_64: " Laurent Vivier
2020-03-17 15:51 ` [PULL v2 28/37] linux-user, mips: " Laurent Vivier
2020-03-17 15:51 ` [PULL v2 29/37] linux-user, mips64: " Laurent Vivier
2020-03-17 15:51 ` [PULL v2 30/37] linux-user, scripts: add a script to update syscall.tbl Laurent Vivier
2020-03-17 15:51 ` [PULL v2 31/37] linux-user: update syscall.tbl from linux 0bf999f9c5e7 Laurent Vivier
2020-03-17 15:51 ` [PULL v2 32/37] linux-user,mips: move content of mips_syscall_args Laurent Vivier
2020-03-17 15:51 ` [PULL v2 33/37] linux-user,mips: update syscall-args-o32.c.inc Laurent Vivier
2020-03-17 15:51 ` [PULL v2 34/37] scripts: add a script to generate syscall_nr.h Laurent Vivier
2020-03-17 15:51 ` [PULL v2 35/37] linux-user, aarch64: sync syscall numbers with kernel v5.5 Laurent Vivier
2020-03-17 15:51 ` [PULL v2 36/37] linux-user, nios2: " Laurent Vivier
2020-03-17 15:51 ` [PULL v2 37/37] linux-user, openrisc: " Laurent Vivier
2020-03-18 13:57 ` [PULL v2 00/37] Linux user for 5.0 patches Peter Maydell
2020-03-18 14:08   ` Laurent Vivier
2020-03-18 19:46   ` Richard Henderson
2020-03-18 19:58     ` Laurent Vivier
2020-03-18 20:17       ` Richard Henderson
2020-03-18 20:23         ` Laurent Vivier
2020-03-18 20:42           ` Richard Henderson
2020-03-19  8:25             ` Laurent Vivier
2020-03-23 20:33     ` Laurent Vivier
2020-03-23 21:05       ` Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200317155116.1227513-4-laurent@vivier.eu \
    --to=laurent@vivier.eu \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=riku.voipio@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).