Skip to contentSkip to conversation

linux-user: Implement x86_64 vsyscalls

PATCH Superseded Richard Henderson 5 participants 18 messages Jan 16, 2020 – Jan 28, 2020

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover linux-user: Implement x86_64 vsyscalls

Changes since v2:

* Add /proc/self/maps line

I'm not sure this is really necessary. The linux kernel self-test checks for it, and modifies the set of tests that it runs based on it. But otherwise I think it's unused.

* Fix errors in base gettimeofday syscall

This is also checked by test_vsyscall, as noticed by AJB.

r~

Original blurb:

The x86_64 abi has a legacy vsyscall page. The kernel folk have been trying to deprecate this since at least v3.1, but

(1) We don't implement the vdso that replaces vsyscalls,
(2) As of v5.5, the vsyscall page is still enabled by default.

This lack is affecting Peter's linux-user testing.

The dependency is not obvious because Peter is running the tests on x86_64, so the host is providing a vsyscall page to qemu.

Because of how user-only memory operations are handled, with no validation of guest vs host pages, so long as qemu chooses to run with guest_base == 0, the guest may Just So Happen to read the host's vsyscall page.

Complicating this, new OS releases may use a kernel configured with CONFIG_LEGACY_VSYSCALL_XONLY=y, which means the the vsyscall page cannot be read, only executed. Which means that the guest then cannot read the host vsyscall page during translation and will SIGSEGV.

Exactly which of these many variables is affecting Peter's testing with Ubuntu 18.04 of my TCG merge, I'm not exactly sure. I suspect that it is the change to drop the textseg_addr adjustment to user-only static binaries. IIRC bionic does not support -static-pie, which is the preferred replacement. This could mean that the host and guest binaries overlap, which leads to guest_base != 0.

I vaguely remember someone (Paolo?) implementing something like this many years ago, but clearly it never got merged.

In any case, this emulation has been missing for too long.

Richard Henderson (5):
target/i386: Renumber EXCP_SYSCALL linux-user/i386: Split out gen_signal linux-user/i386: Emulate x86_64 vsyscalls
linux-user: Add x86_64 vsyscall page to /proc/self/maps
linux-user: Flush out implementation of gettimeofday

 target/i386/cpu.h          |   6 +-
 linux-user/i386/cpu_loop.c | 198 ++++++++++++++++++++++++++-----------
 linux-user/syscall.c       |  36 ++++++-
 target/i386/translate.c    |  16 ++-
 4 files changed, 190 insertions(+), 66 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/5 target/i386: Renumber EXCP_SYSCALL

We are not short of numbers for EXCP_*. There is no need to confuse things by having EXCP_VMEXIT and EXCP_SYSCALL overlap, even though the former is only used for system mode and the latter is only used for user mode.

---
 target/i386/cpu.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
target/i386/cpu.h +2 -3
index 594326a794..164d038d1f 100644
@@ -998,9 +998,8 @@ typedef uint64_t FeatureWordArray[FEATURE_WORDS];
 #define EXCP11_ALGN	17
 #define EXCP12_MCHK	18
 
-#define EXCP_SYSCALL    0x100 /* only happens in user only emulation
-                                 for syscall instruction */
-#define EXCP_VMEXIT     0x100
+#define EXCP_VMEXIT     0x100 /* only for system emulation */
+#define EXCP_SYSCALL    0x101 /* only for user emulation */
 
 /* i386-specific interrupt pending bits.  */
 #define CPU_INTERRUPT_POLL      CPU_INTERRUPT_TGT_EXT_1
Signature · 1 line
2.20.1
#2 lore raw

PATCH v2 2/5 linux-user/i386: Split out gen_signal

In reply to Richard Henderson

This is a bit tidier than open-coding the 5 lines necessary to initialize the target_siginfo_t. In addition, this zeros the remaining bytes of the target_siginfo_t, rather than passing in garbage.

---
 linux-user/i386/cpu_loop.c | 93 ++++++++++++++------------------------
 1 file changed, 33 insertions(+), 60 deletions(-)
linux-user/i386/cpu_loop.c +33 -60
index 024b6f4d58..e217cca5ee 100644
@@ -81,13 +81,23 @@ static void set_idt(int n, unsigned int dpl)
 }
 #endif
 
+static void gen_signal(CPUX86State *env, int sig, int code, abi_ptr addr)
+{
+    target_siginfo_t info = {
+        .si_signo = sig,
+        .si_code = code,
+        ._sifields._sigfault._addr = addr
+    };
+
+    queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+}
+
 void cpu_loop(CPUX86State *env)
 {
     CPUState *cs = env_cpu(env);
     int trapnr;
     abi_ulong pc;
     abi_ulong ret;
-    target_siginfo_t info;
 
     for(;;) {
         cpu_exec_start(cs);
@@ -134,70 +144,45 @@ void cpu_loop(CPUX86State *env)
 #endif
         case EXCP0B_NOSEG:
         case EXCP0C_STACK:
-            info.si_signo = TARGET_SIGBUS;
-            info.si_errno = 0;
-            info.si_code = TARGET_SI_KERNEL;
-            info._sifields._sigfault._addr = 0;
-            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+            gen_signal(env, TARGET_SIGBUS, TARGET_SI_KERNEL, 0);
             break;
         case EXCP0D_GPF:
             /* XXX: potential problem if ABI32 */
 #ifndef TARGET_X86_64
             if (env->eflags & VM_MASK) {
                 handle_vm86_fault(env);
-            } else
-#endif
-            {
-                info.si_signo = TARGET_SIGSEGV;
-                info.si_errno = 0;
-                info.si_code = TARGET_SI_KERNEL;
-                info._sifields._sigfault._addr = 0;
-                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+                break;
             }
+#endif
+            gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
             break;
         case EXCP0E_PAGE:
-            info.si_signo = TARGET_SIGSEGV;
-            info.si_errno = 0;
-            if (!(env->error_code & 1))
-                info.si_code = TARGET_SEGV_MAPERR;
-            else
-                info.si_code = TARGET_SEGV_ACCERR;
-            info._sifields._sigfault._addr = env->cr[2];
-            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+            gen_signal(env, TARGET_SIGSEGV,
+                       (env->error_code & 1 ?
+                        TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR),
+                       env->cr[2]);
             break;
         case EXCP00_DIVZ:
 #ifndef TARGET_X86_64
             if (env->eflags & VM_MASK) {
                 handle_vm86_trap(env, trapnr);
-            } else
-#endif
-            {
-                /* division by zero */
-                info.si_signo = TARGET_SIGFPE;
-                info.si_errno = 0;
-                info.si_code = TARGET_FPE_INTDIV;
-                info._sifields._sigfault._addr = env->eip;
-                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+                break;
             }
+#endif
+            gen_signal(env, TARGET_SIGFPE, TARGET_FPE_INTDIV, env->eip);
             break;
         case EXCP01_DB:
         case EXCP03_INT3:
 #ifndef TARGET_X86_64
             if (env->eflags & VM_MASK) {
                 handle_vm86_trap(env, trapnr);
-            } else
+                break;
+            }
 #endif
-            {
-                info.si_signo = TARGET_SIGTRAP;
-                info.si_errno = 0;
-                if (trapnr == EXCP01_DB) {
-                    info.si_code = TARGET_TRAP_BRKPT;
-                    info._sifields._sigfault._addr = env->eip;
-                } else {
-                    info.si_code = TARGET_SI_KERNEL;
-                    info._sifields._sigfault._addr = 0;
-                }
-                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+            if (trapnr == EXCP01_DB) {
+                gen_signal(env, TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->eip);
+            } else {
+                gen_signal(env, TARGET_SIGTRAP, TARGET_SI_KERNEL, 0);
             }
             break;
         case EXCP04_INTO:
@@ -205,31 +190,19 @@ void cpu_loop(CPUX86State *env)
 #ifndef TARGET_X86_64
             if (env->eflags & VM_MASK) {
                 handle_vm86_trap(env, trapnr);
-            } else
-#endif
-            {
-                info.si_signo = TARGET_SIGSEGV;
-                info.si_errno = 0;
-                info.si_code = TARGET_SI_KERNEL;
-                info._sifields._sigfault._addr = 0;
-                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+                break;
             }
+#endif
+            gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
             break;
         case EXCP06_ILLOP:
-            info.si_signo = TARGET_SIGILL;
-            info.si_errno = 0;
-            info.si_code = TARGET_ILL_ILLOPN;
-            info._sifields._sigfault._addr = env->eip;
-            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+            gen_signal(env, TARGET_SIGILL, TARGET_ILL_ILLOPN, env->eip);
             break;
         case EXCP_INTERRUPT:
             /* just indicate that signals should be handled asap */
             break;
         case EXCP_DEBUG:
-            info.si_signo = TARGET_SIGTRAP;
-            info.si_errno = 0;
-            info.si_code = TARGET_TRAP_BRKPT;
-            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+            gen_signal(env, TARGET_SIGTRAP, TARGET_TRAP_BRKPT, 0);
             break;
         case EXCP_ATOMIC:
             cpu_exec_step_atomic(cs);
Signature · 1 line
2.20.1
#3 lore raw

PATCH v2 3/5 linux-user/i386: Emulate x86_64 vsyscalls

In reply to Richard Henderson

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.

---
 target/i386/cpu.h          |   1 +
 linux-user/i386/cpu_loop.c | 105 +++++++++++++++++++++++++++++++++++++
 target/i386/translate.c    |  16 +++++-
 3 files changed, 121 insertions(+), 1 deletion(-)
target/i386/cpu.h +1 -0
index 164d038d1f..3fb2d2a986 100644
@@ -1000,6 +1000,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
linux-user/i386/cpu_loop.c +105 -0

117 lines not on this page · raw message

target/i386/translate.c +15 -1
index 7c99ef1385..391b4ef149 100644
@@ -8555,7 +8555,21 @@ 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;
+
+#if defined(TARGET_X86_64) && \
+    defined(CONFIG_USER_ONLY) && \
+    defined(CONFIG_LINUX)
+    /*
+     * Detect entry into the vsyscall page and invoke the syscall.
+     */
+    if ((dc->base.pc_next & TARGET_PAGE_MASK) == 0xffffffffff600000ull) {
+        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
Signature · 1 line
2.20.1
Reviewed-by on v2 3/5
#4 lore raw

Re: [PATCH v2 3/5] linux-user/i386: Emulate x86_64 vsyscalls

Quoted 64 lines · Richard Henderson

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.

---
 target/i386/cpu.h          |   1 +
 linux-user/i386/cpu_loop.c | 105 +++++++++++++++++++++++++++++++++++++
 target/i386/translate.c    |  16 +++++-
 3 files changed, 121 insertions(+), 1 deletion(-)
target/i386/cpu.h +1 -0
index 164d038d1f..3fb2d2a986 100644
@@ -1000,6 +1000,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
linux-user/i386/cpu_loop.c +32 -0
index e217cca5ee..f9bf6cec27 100644
@@ -92,6 +92,106 @@ 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, now verify the offset.
+     */
+    switch (env->eip & ~TARGET_PAGE_MASK) {
+    case 0x000:

linux-user/i386/cpu_loop.c static void gen_signal(CPUX86State *env, int sig, int code, abi_ptr addr) · line 136

Quoted 10 lines
linux-user/i386/cpu_loop.c +10 -0
+        syscall = TARGET_NR_gettimeofday;
+        break;
+    case 0x400:
+        syscall = TARGET_NR_time;
+        break;
+    case 0x800:
+        syscall = TARGET_NR_getcpu;
+        break;
+    default:
+    sigsegv:

this label looks a little extraneous.

Otherwise:

Signature · 1 line
Alex Bennée
#5 lore raw

Re: [PATCH v2 3/5] linux-user/i386: Emulate x86_64 vsyscalls

Quoted 9 lines · Alex Bennée

linux-user/i386/cpu_loop.c static void gen_signal(CPUX86State *env, int sig, int code, abi_ptr addr) · line 136

Quoted 2 lines
linux-user/i386/cpu_loop.c +2 -0
+    default:
+    sigsegv:

this label looks a little extraneous.

Otherwise:

Look a little further down:

Quoted 4 lines
linux-user/i386/cpu_loop.c +4 -0
+    default:
+    sigsegv:
+        /* Like force_sig(SIGSEGV).  */
+        gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);

linux-user/i386/cpu_loop.c static void gen_signal(CPUX86State *env, int sig, int code, abi_ptr addr) · line 148

Quoted 10 lines
linux-user/i386/cpu_loop.c +10 -0
+        return;
+    }
+
+    /*
+     * 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;
+    }

r~

#6 lore raw

Re: [PATCH v2 3/5] linux-user/i386: Emulate x86_64 vsyscalls

Quoted 4 lines · Richard Henderson
Quoted 3 lines · Alex Bennée
Quoted 2 lines
linux-user/i386/cpu_loop.c +2 -0
+    default:
+    sigsegv:
Quoted 23 lines
Quoted 6 lines

this label looks a little extraneous.

Otherwise:

Look a little further down:

Quoted 14 lines
linux-user/i386/cpu_loop.c +14 -0
+    default:
+    sigsegv:
+        /* Like force_sig(SIGSEGV).  */
+        gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
+        return;
+    }
+
+    /*
+     * 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;
+    }

Wouldn't this read better:

    /*
     * Validate the entry point.  We have already validated the page
     * during translation, 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:
        syscall = -1;
        break;
    }

    /*
     * If we have an invalid entry point or an invalid return address we
     * generate a SIGSEG.
     */
    if (syscall < 0 || get_user_u64(caller, env->regs[R_ESP])) {
        gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
        return;
    }
Signature · 1 line
Alex Bennée
#7 lore raw

Re: [PATCH v2 3/5] linux-user/i386: Emulate x86_64 vsyscalls

Quoted 52 lines · Alex Bennée
Quoted 4 lines · Richard Henderson
Quoted 3 lines · Alex Bennée
Quoted 2 lines
linux-user/i386/cpu_loop.c +2 -0
+    default:
+    sigsegv:
Quoted 23 lines
Quoted 6 lines

this label looks a little extraneous.

Otherwise:

Look a little further down:

Quoted 14 lines
linux-user/i386/cpu_loop.c +14 -0
+    default:
+    sigsegv:
+        /* Like force_sig(SIGSEGV).  */
+        gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
+        return;
+    }
+
+    /*
+     * 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;
+    }

Wouldn't this read better:

    /*
     * Validate the entry point.  We have already validated the page
     * during translation, 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:
        syscall = -1;
        break;
    }
Quoted 9 lines
    /*
     * If we have an invalid entry point or an invalid return address we
     * generate a SIGSEG.
     */
    if (syscall < 0 || get_user_u64(caller, env->regs[R_ESP])) {
        gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
        return;
    }

Only if you have a violent goto allergy.

r~

#8 lore raw

Re: [PATCH v2 3/5] linux-user/i386: Emulate x86_64 vsyscalls

Quoted 63 lines · Richard Henderson
Quoted 52 lines · Alex Bennée
Quoted 4 lines · Richard Henderson
Quoted 3 lines · Alex Bennée
Quoted 2 lines
linux-user/i386/cpu_loop.c +2 -0
+    default:
+    sigsegv:
Quoted 23 lines
Quoted 6 lines

this label looks a little extraneous.

Otherwise:

Look a little further down:

Quoted 14 lines
linux-user/i386/cpu_loop.c +14 -0
+    default:
+    sigsegv:
+        /* Like force_sig(SIGSEGV).  */
+        gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
+        return;
+    }
+
+    /*
+     * 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;
+    }

Wouldn't this read better:

    /*
     * Validate the entry point.  We have already validated the page
     * during translation, 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:
        syscall = -1;
        break;
    }
Quoted 9 lines
    /*
     * If we have an invalid entry point or an invalid return address we
     * generate a SIGSEG.
     */
    if (syscall < 0 || get_user_u64(caller, env->regs[R_ESP])) {
        gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
        return;
    }
Quoted 1 line

Only if you have a violent goto allergy.

gotos have their place but jumping backwards is confusing to eye. If the compiler want to mess with layout after then it is free to do so.

Signature · 1 line
Alex Bennée
#9 lore raw

Re: [PATCH v2 3/5] linux-user/i386: Emulate x86_64 vsyscalls

Quoted 66 lines · Alex Bennée
Quoted 63 lines · Richard Henderson
Quoted 61 lines · Alex Bennée
Quoted 4 lines · Richard Henderson
Quoted 3 lines · Alex Bennée
Quoted 2 lines
linux-user/i386/cpu_loop.c +2 -0
+    default:
+    sigsegv:
Quoted 23 lines
Quoted 6 lines

this label looks a little extraneous.

Otherwise:

Look a little further down:

Quoted 14 lines
linux-user/i386/cpu_loop.c +14 -0
+    default:
+    sigsegv:
+        /* Like force_sig(SIGSEGV).  */
+        gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
+        return;
+    }
+
+    /*
+     * 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;
+    }

Wouldn't this read better:

    /*
     * Validate the entry point.  We have already validated the page
     * during translation, 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:
        syscall = -1;
        break;
    }

    /*
     * If we have an invalid entry point or an invalid return address we
     * generate a SIGSEG.
     */
    if (syscall < 0 || get_user_u64(caller, env->regs[R_ESP])) {
        gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
        return;
    }
Quoted 4 lines
Quoted 1 line

Only if you have a violent goto allergy.

gotos have their place but jumping backwards is confusing to eye. If the compiler want to mess with layout after then it is free to do so.

I agree, if anything I'd place the sigsegv label at the end of the function but Alex's version is just fine too.

Paolo

#10 lore raw

PATCH v2 4/5 linux-user: Add x86_64 vsyscall page to /proc/self/maps

In reply to Richard Henderson

The page isn't (necessarily) present in the host /proc/self/maps, and even if it might be it isn't present in page_flags, and even if it was it might not have the same set of page permissions.

The easiest thing to do, particularly when it comes to the "[vsyscall]" note at the end of line, is to special case it.

---
 linux-user/syscall.c | 9 +++++++++
 1 file changed, 9 insertions(+)
linux-user/syscall.c +9 -0
index 171c0caef3..eb867a5296 100644
@@ -7005,6 +7005,15 @@ static int open_self_maps(void *cpu_env, int fd)
         }
     }
 
+#ifdef TARGET_X86_64
+    /*
+     * We only support execution from the vsyscall page.
+     * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
+     */
+    dprintf(fd, "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0"
+                "          [vsyscall]\n");
+#endif
+
     free(line);
     fclose(fp);
 
Signature · 1 line
2.20.1
#11 lore raw

Re: [PATCH v2 4/5] linux-user: Add x86_64 vsyscall page to /proc/self/maps

Quoted 17 lines · Richard Henderson

The page isn't (necessarily) present in the host /proc/self/maps, and even if it might be it isn't present in page_flags, and even if it was it might not have the same set of page permissions.

The easiest thing to do, particularly when it comes to the "[vsyscall]" note at the end of line, is to special case it.

---
  linux-user/syscall.c | 9 +++++++++
  1 file changed, 9 insertions(+)
linux-user/syscall.c +0 -0
index 171c0caef3..eb867a5296 100644

linux-user/syscall.c static int open_self_maps(void *cpu_env, int fd) · line 7014

Quoted 10 lines
linux-user/syscall.c +7 -0
@@ -7005,6 +7005,15 @@ static int open_self_maps(void *cpu_env, int fd)
          }
      }
 
+#ifdef TARGET_X86_64
+    /*
+     * We only support execution from the vsyscall page.
+     * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
+     */
+    dprintf(fd, "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0"
+                "          [vsyscall]\n");

Can we add a definition for 0xffffffffff600000ull in the previous patch, and use it with TARGET_PAGE_MASK here?

Quoted 6 lines
linux-user/syscall.c +2 -0
+#endif
+
      free(line);
      fclose(fp);
 
#12 lore raw

Re: [PATCH v2 4/5] linux-user: Add x86_64 vsyscall page to /proc/self/maps

In reply to Richard Henderson
Quoted 19 lines · Richard Henderson

The page isn't (necessarily) present in the host /proc/self/maps, and even if it might be it isn't present in page_flags, and even if it was it might not have the same set of page permissions.

The easiest thing to do, particularly when it comes to the "[vsyscall]" note at the end of line, is to special case it.

---
 linux-user/syscall.c | 9 +++++++++
 1 file changed, 9 insertions(+)
linux-user/syscall.c +0 -0
index 171c0caef3..eb867a5296 100644
@@ -7005,6 +7005,15 @@ static int open_self_maps(void *cpu_env, int fd)
         }
     }

linux-user/syscall.c static int open_self_maps(void *cpu_env, int fd) · line 7016

Quoted 10 lines
linux-user/syscall.c +9 -0
 
+#ifdef TARGET_X86_64
+    /*
+     * We only support execution from the vsyscall page.
+     * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
+     */
+    dprintf(fd, "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0"
+                "          [vsyscall]\n");
+#endif
+

I'm not sure what whitespace is wrong - tabs maybe? But looking at a real vsyscall system vs emulated things line up better:

  Welcome to Buildroot
  buildroot login: root
  # uname -a
  Linux buildroot 5.4.13 #1 SMP Mon Jan 20 14:36:20 GMT 2020 x86_64 GNU/Linux
  # cat /proc/self/maps
  00400000-004ab000 r-xp 00000000 00:02 7635                               /bin/busybox
  004ab000-004ac000 r-xp 000aa000 00:02 7635                               /bin/busybox
  004ac000-004ad000 rwxp 000ab000 00:02 7635                               /bin/busybox
  004ad000-004ae000 rwxp 00000000 00:00 0
  7fcc91566000-7fcc91567000 rwxp 00000000 00:00 0
  7fcc91567000-7fcc915de000 r-xp 00000000 00:02 7475                       /lib/libuClibc-1.0.32.so
  7fcc915de000-7fcc915df000 ---p 00000000 00:00 0
  7fcc915df000-7fcc915e0000 r-xp 00077000 00:02 7475                       /lib/libuClibc-1.0.32.so
  7fcc915e0000-7fcc915e1000 rwxp 00078000 00:02 7475                       /lib/libuClibc-1.0.32.so
  7fcc915e1000-7fcc915fc000 rwxp 00000000 00:00 0
  7fcc915fc000-7fcc91603000 r-xp 00000000 00:02 7480                       /lib/ld64-uClibc-1.0.32.so
  7fcc91603000-7fcc91604000 r-xp 00006000 00:02 7480                       /lib/ld64-uClibc-1.0.32.so
  7fcc91604000-7fcc91605000 rwxp 00007000 00:02 7480                       /lib/ld64-uClibc-1.0.32.so
  7ffd92001000-7ffd92022000 rwxp 00000000 00:00 0                          [stack]
  7ffd920c3000-7ffd920c6000 r--p 00000000 00:00 0                          [vvar]
  7ffd920c6000-7ffd920c7000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  # QEMU 4.2.50 monitor - type 'help' for more information
  (qemu) quit
  14:38:52 [alex.bennee@hackbox2:~/l/q/b/all] review/fix-user-brk|… + ./x86_64-linux-user/qemu-x86_64 /bin/cat /proc/self/maps
  4000000000-4000008000 r-xp 00000000 00:19 8131          /bin/cat
  4000008000-4000207000 ---p 00000000 00:00 0
  4000207000-4000208000 r--p 00007000 00:19 8131          /bin/cat
  4000208000-4000209000 rw-p 00008000 00:19 8131          /bin/cat
  4000209000-400022a000 rw-p 00000000 00:00 0
  4001209000-400120a000 ---p 00000000 00:00 0
  400120a000-4001a0a000 rw-p 00000000 00:00 0                [stack]
  4001a0a000-4001a31000 r-xp 00000000 00:19 3644          /lib/x86_64-linux-gnu/ld-2.27.so
  4001a31000-4001c31000 ---p 00000000 00:00 0
  4001c31000-4001c32000 r--p 00027000 00:19 3644          /lib/x86_64-linux-gnu/ld-2.27.so
  4001c32000-4001c33000 rw-p 00028000 00:19 3644          /lib/x86_64-linux-gnu/ld-2.27.so
  4001c33000-4001c36000 rw-p 00000000 00:00 0
  4001c6d000-4001e54000 r-xp 00000000 00:19 3648          /lib/x86_64-linux-gnu/libc-2.27.so
  4001e54000-4002054000 ---p 001e7000 00:19 3648          /lib/x86_64-linux-gnu/libc-2.27.so
  4002054000-4002058000 r--p 001e7000 00:19 3648          /lib/x86_64-linux-gnu/libc-2.27.so
  4002058000-400205a000 rw-p 001eb000 00:19 3648          /lib/x86_64-linux-gnu/libc-2.27.so
  400205a000-4002060000 rw-p 00000000 00:00 0
  4002060000-40023d2000 r--p 00000000 00:19 195276          /usr/lib/locale/locale-archive
  ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0          [vsyscall]
Quoted 2 lines
linux-user/syscall.c +0 -0
     free(line);
     fclose(fp);
Signature · 1 line
Alex Bennée
#13 lore raw
In reply to Richard Henderson
Quoted 11 lines · Richard Henderson

Changes since v2:

* Add /proc/self/maps line

I'm not sure this is really necessary. The linux kernel self-test checks for it, and modifies the set of tests that it runs based on it. But otherwise I think it's unused.

* Fix errors in base gettimeofday syscall

This is also checked by test_vsyscall, as noticed by AJB.

Oh, and

* Set error_code in write_ok_or_segv (patch 2)

r~

#14 lore raw

PATCH v2 5/5 linux-user: Flush out implementation of gettimeofday

In reply to Richard Henderson

The first argument, timeval, is allowed to be NULL.

The second argument, timezone, was missing. While its use is deprecated, it is still present in the syscall.

---
 linux-user/syscall.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)
linux-user/syscall.c +25 -2
index eb867a5296..628b4de9a1 100644
@@ -1219,6 +1219,23 @@ static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
     return 0;
 }
 
+static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
+                                             struct timezone *tz)
+{
+    struct target_timezone *target_tz;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
+        return -TARGET_EFAULT;
+    }
+
+    __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
+    __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
+
+    unlock_user_struct(target_tz, target_tz_addr, 1);
+
+    return 0;
+}
+
 static inline abi_long copy_from_user_timezone(struct timezone *tz,
                                                abi_ulong target_tz_addr)
 {
@@ -8567,10 +8584,16 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_gettimeofday:
         {
             struct timeval tv;
-            ret = get_errno(gettimeofday(&tv, NULL));
+            struct timezone tz;
+
+            ret = get_errno(gettimeofday(&tv, &tz));
             if (!is_error(ret)) {
-                if (copy_to_user_timeval(arg1, &tv))
+                if (arg1 && copy_to_user_timeval(arg1, &tv)) {
                     return -TARGET_EFAULT;
+                }
+                if (arg2 && copy_to_user_timezone(arg2, &tz)) {
+                    return -TARGET_EFAULT;
+                }
             }
         }
         return ret;
Signature · 1 line
2.20.1
Reviewed-by on v2 5/5
#15 lore raw

Re: [PATCH v2 5/5] linux-user: Flush out implementation of gettimeofday

Quoted 48 lines · Richard Henderson

The first argument, timeval, is allowed to be NULL.

The second argument, timezone, was missing. While its use is deprecated, it is still present in the syscall.

---
  linux-user/syscall.c | 27 +++++++++++++++++++++++++--
  1 file changed, 25 insertions(+), 2 deletions(-)
linux-user/syscall.c +20 -1
index eb867a5296..628b4de9a1 100644
@@ -1219,6 +1219,23 @@ static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
      return 0;
  }
 
+static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
+                                             struct timezone *tz)
+{
+    struct target_timezone *target_tz;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
+        return -TARGET_EFAULT;
+    }
+
+    __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
+    __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
+
+    unlock_user_struct(target_tz, target_tz_addr, 1);
+
+    return 0;
+}
+
  static inline abi_long copy_from_user_timezone(struct timezone *tz,
                                                 abi_ulong target_tz_addr)
  {
@@ -8567,10 +8584,16 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
      case TARGET_NR_gettimeofday:
          {
              struct timeval tv;
-            ret = get_errno(gettimeofday(&tv, NULL));
+            struct timezone tz;
+
+            ret = get_errno(gettimeofday(&tv, &tz));
              if (!is_error(ret)) {

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

Quoted 10 lines
linux-user/syscall.c +5 -1
-                if (copy_to_user_timeval(arg1, &tv))
+                if (arg1 && copy_to_user_timeval(arg1, &tv)) {
                      return -TARGET_EFAULT;
+                }
+                if (arg2 && copy_to_user_timezone(arg2, &tz)) {
+                    return -TARGET_EFAULT;
+                }
              }
          }
          return ret;
Reviewed-by on v2 5/5
#16 lore raw

Re: [PATCH v2 5/5] linux-user: Flush out implementation of gettimeofday

In reply to Richard Henderson
Quoted 6 lines · Richard Henderson

The first argument, timeval, is allowed to be NULL.

The second argument, timezone, was missing. While its use is deprecated, it is still present in the syscall.

Quoted 51 lines
---
 linux-user/syscall.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)
linux-user/syscall.c +25 -2
index eb867a5296..628b4de9a1 100644
@@ -1219,6 +1219,23 @@ static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
     return 0;
 }
 
+static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
+                                             struct timezone *tz)
+{
+    struct target_timezone *target_tz;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
+        return -TARGET_EFAULT;
+    }
+
+    __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
+    __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
+
+    unlock_user_struct(target_tz, target_tz_addr, 1);
+
+    return 0;
+}
+
 static inline abi_long copy_from_user_timezone(struct timezone *tz,
                                                abi_ulong target_tz_addr)
 {
@@ -8567,10 +8584,16 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_gettimeofday:
         {
             struct timeval tv;
-            ret = get_errno(gettimeofday(&tv, NULL));
+            struct timezone tz;
+
+            ret = get_errno(gettimeofday(&tv, &tz));
             if (!is_error(ret)) {
-                if (copy_to_user_timeval(arg1, &tv))
+                if (arg1 && copy_to_user_timeval(arg1, &tv)) {
                     return -TARGET_EFAULT;
+                }
+                if (arg2 && copy_to_user_timezone(arg2, &tz)) {
+                    return -TARGET_EFAULT;
+                }
             }
         }
         return ret;
Signature · 1 line
Alex Bennée
#17 lore raw
In reply to Richard Henderson

Le 16/01/2020 à 20:43, Richard Henderson a écrit :

Quoted 4 lines

Changes since v2:

* Add /proc/self/maps line

Quoted 8 lines

I'm not sure this is really necessary. The linux kernel self-test checks for it, and modifies the set of tests that it runs based on it. But otherwise I think it's unused.

* Fix errors in base gettimeofday syscall

This is also checked by test_vsyscall, as noticed by AJB.

Hi Richard,

do you plan to send a v3?

Thanks,
Laurent

Quoted 53 lines

r~

Original blurb:

The x86_64 abi has a legacy vsyscall page. The kernel folk have been trying to deprecate this since at least v3.1, but

(1) We don't implement the vdso that replaces vsyscalls,
(2) As of v5.5, the vsyscall page is still enabled by default.

This lack is affecting Peter's linux-user testing.

The dependency is not obvious because Peter is running the tests on x86_64, so the host is providing a vsyscall page to qemu.

Because of how user-only memory operations are handled, with no validation of guest vs host pages, so long as qemu chooses to run with guest_base == 0, the guest may Just So Happen to read the host's vsyscall page.

Complicating this, new OS releases may use a kernel configured with CONFIG_LEGACY_VSYSCALL_XONLY=y, which means the the vsyscall page cannot be read, only executed. Which means that the guest then cannot read the host vsyscall page during translation and will SIGSEGV.

Exactly which of these many variables is affecting Peter's testing with Ubuntu 18.04 of my TCG merge, I'm not exactly sure. I suspect that it is the change to drop the textseg_addr adjustment to user-only static binaries. IIRC bionic does not support -static-pie, which is the preferred replacement. This could mean that the host and guest binaries overlap, which leads to guest_base != 0.

I vaguely remember someone (Paolo?) implementing something like this many years ago, but clearly it never got merged.

In any case, this emulation has been missing for too long.

Richard Henderson (5):
target/i386: Renumber EXCP_SYSCALL linux-user/i386: Split out gen_signal linux-user/i386: Emulate x86_64 vsyscalls
linux-user: Add x86_64 vsyscall page to /proc/self/maps
linux-user: Flush out implementation of gettimeofday

 target/i386/cpu.h          |   6 +-
 linux-user/i386/cpu_loop.c | 198 ++++++++++++++++++++++++++-----------
 linux-user/syscall.c       |  36 ++++++-
 target/i386/translate.c    |  16 ++-
 4 files changed, 190 insertions(+), 66 deletions(-)