linux-user: Implement x86_64 vsyscalls
v3 cover linux-user: Implement x86_64 vsyscalls
Changes for v3:
* Add TARGET_VSYSCALL_PAGE define.
* Move the sigsegv goto around.
v2: https://lists.gnu.org/archive/html/qemu-devel/2020-01/msg03474.html
v1: https://lists.gnu.org/archive/html/qemu-devel/2020-01/msg02841.html
r~
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 | 12 ++- linux-user/i386/cpu_loop.c | 201 ++++++++++++++++++++++++++----------- linux-user/syscall.c | 37 ++++++- target/i386/translate.c | 14 ++- 4 files changed, 198 insertions(+), 66 deletions(-)
Signature · 1 line
2.20.1
PATCH v3 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
@@ -999,9 +999,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
PATCH v3 2/5 linux-user/i386: Split out gen_signal
In reply to Richard HendersonThis 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
@@ -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
PATCH v3 3/5 linux-user/i386: Emulate x86_64 vsyscalls
In reply to Richard HendersonNotice 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.
---
v3: Adjust the gotos. Define TARGET_VSYSCALL_PAGE.
--- target/i386/cpu.h | 7 +++ linux-user/i386/cpu_loop.c | 108 +++++++++++++++++++++++++++++++++++++ target/i386/translate.c | 14 ++++- 3 files changed, 128 insertions(+), 1 deletion(-)
target/i386/cpu.h +7 -0
@@ -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 */
linux-user/i386/cpu_loop.c +108 -0
120 lines not on this page · raw message
target/i386/translate.c +13 -1
@@ -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
Signature · 1 line
2.20.1
PATCH v3 4/5 linux-user: Add x86_64 vsyscall page to /proc/self/maps
In reply to Richard HendersonThe 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.
---
v3: Use TARGET_VSYSCALL_PAGE.
--- linux-user/syscall.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
linux-user/syscall.c +10 -0
@@ -7012,6 +7012,16 @@ static int open_self_maps(void *cpu_env, int fd) } } +#ifdef TARGET_VSYSCALL_PAGE +/* +* We only support execution from the vsyscall page. +* This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3. +*/ +dprintf (fd, TARGET_FMT_lx"-" TARGET_FMT_lx +" --xp 00000000 00:00 0 [vsyscall]\n" , +TARGET_VSYSCALL_PAGE ,TARGET_VSYSCALL_PAGE +TARGET_PAGE_SIZE ); +#endif +free (line);fclose (fp);
Signature · 1 line
2.20.1
PATCH v3 5/5 linux-user: Flush out implementation of gettimeofday
In reply to Richard HendersonThe 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
@@ -1224,6 +1224,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) {
@@ -8575,10 +8592,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 &©_to_user_timeval (arg1, &tv)) {return -TARGET_EFAULT ; + } +if (arg2 &©_to_user_timezone (arg2, &tz)) { +return -TARGET_EFAULT ; + } } }return ret;
Signature · 1 line
2.20.1
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Subject: [PATCH v3 0/5] linux-user: Implement x86_64 vsyscalls
Message-id: [email protected]
Type: series
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 Switched to a new branch 'test'
4f8c326 linux-user: Flush out implementation of gettimeofday 0337695 linux-user: Add x86_64 vsyscall page to /proc/self/maps 1035d94 linux-user/i386: Emulate x86_64 vsyscalls a06e82b linux-user/i386: Split out gen_signal
70797ff target/i386: Renumber EXCP_SYSCALL
=== OUTPUT BEGIN ===
1/5 Checking commit 70797ffa7857 (target/i386: Renumber EXCP_SYSCALL)
2/5 Checking commit a06e82b1d3e0 (linux-user/i386: Split out gen_signal) 3/5 Checking commit 1035d94360a8 (linux-user/i386: Emulate x86_64 vsyscalls) 4/5 Checking commit 0337695b9a91 (linux-user: Add x86_64 vsyscall page to /proc/self/maps)
ERROR: trailing whitespace
#30: FILE: linux-user/syscall.c:7020:
+ dprintf(fd, TARGET_FMT_lx "-" TARGET_FMT_lx $
total: 1 errors, 0 warnings, 16 lines checked
Patch 4/5 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
5/5 Checking commit 4f8c32688f82 (linux-user: Flush out implementation of gettimeofday) === OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]
Quoted 7 lines · Richard Henderson
Changes for v3:
* Add
TARGET_VSYSCALL_PAGEdefine.
* Move the sigsegv goto around.v2: https://lists.gnu.org/archive/html/qemu-devel/2020-01/msg03474.html
v1: https://lists.gnu.org/archive/html/qemu-devel/2020-01/msg02841.html
Ping?
r~
Laurent Vivier
AppliedLe 25/02/2020 à 20:59, Richard Henderson a écrit :
Quoted 10 lines · Richard Henderson
Quoted 7 lines
Changes for v3:
* Add
TARGET_VSYSCALL_PAGEdefine.
* Move the sigsegv goto around.v2: https://lists.gnu.org/archive/html/qemu-devel/2020-01/msg03474.html
v1: https://lists.gnu.org/archive/html/qemu-devel/2020-01/msg02841.htmlPing?
Applied to my linux-user branch.
I've removed the trailing whitespace reported by checkpatch.pl
Thanks,
Laurent