* [PATCH v3] target/i386: Restrict system-specific features from user emulation
@ 2023-09-11 14:27 Philippe Mathieu-Daudé
2023-09-11 14:38 ` Michael Tokarev
2023-09-11 16:38 ` Kevin Wolf
0 siblings, 2 replies; 3+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-09-11 14:27 UTC (permalink / raw)
To: qemu-devel
Cc: kvm, Stefan Hajnoczi, Daniel Henrique Barboza,
Daniel P . Berrangé, Michael Tokarev, Richard Henderson,
Paolo Bonzini, Marcelo Tosatti, Philippe Mathieu-Daudé,
Kevin Wolf
Since commits 3adce820cf ("target/i386: Remove unused KVM
stubs") and ef1cf6890f ("target/i386: Allow elision of
kvm_hv_vpindex_settable()"), when building on a x86 host
configured as:
$ ./configure --cc=clang \
--target-list=x86_64-linux-user,x86_64-softmmu \
--enable-debug
we get:
[71/71] Linking target qemu-x86_64
FAILED: qemu-x86_64
/usr/bin/ld: libqemu-x86_64-linux-user.fa.p/target_i386_cpu.c.o: in function `cpu_x86_cpuid':
cpu.c:(.text+0x1374): undefined reference to `kvm_arch_get_supported_cpuid'
/usr/bin/ld: libqemu-x86_64-linux-user.fa.p/target_i386_cpu.c.o: in function `x86_cpu_filter_features':
cpu.c:(.text+0x81c2): undefined reference to `kvm_arch_get_supported_cpuid'
/usr/bin/ld: cpu.c:(.text+0x81da): undefined reference to `kvm_arch_get_supported_cpuid'
/usr/bin/ld: cpu.c:(.text+0x81f2): undefined reference to `kvm_arch_get_supported_cpuid'
/usr/bin/ld: cpu.c:(.text+0x820a): undefined reference to `kvm_arch_get_supported_cpuid'
/usr/bin/ld: libqemu-x86_64-linux-user.fa.p/target_i386_cpu.c.o:cpu.c:(.text+0x8225): more undefined references to `kvm_arch_get_supported_cpuid' follow
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
libqemu-x86_64-linux-user.fa is user emulation specific, so
having system emulation code called there is dubious.
'--enable-debug' disables optimizations (CFLAGS=-O0).
While at this (un)optimization level GCC eliminate the
following dead code (CPP output of mentioned build):
static void x86_cpu_get_supported_cpuid(uint32_t func, uint32_t index,
uint32_t *eax, uint32_t *ebx,
uint32_t *ecx, uint32_t *edx)
{
if ((0)) {
*eax = kvm_arch_get_supported_cpuid(kvm_state, func, index, R_EAX);
*ebx = kvm_arch_get_supported_cpuid(kvm_state, func, index, R_EBX);
*ecx = kvm_arch_get_supported_cpuid(kvm_state, func, index, R_ECX);
*edx = kvm_arch_get_supported_cpuid(kvm_state, func, index, R_EDX);
} else if (0) {
*eax = 0;
*ebx = 0;
*ecx = 0;
*edx = 0;
} else {
*eax = 0;
*ebx = 0;
*ecx = 0;
*edx = 0;
}
}
Clang does not.
Instead of trying to deal with compiler specific checks around
__OPTIMIZE__ (see commit 2140cfa51d "i386: Fix build by providing
stub kvm_arch_get_supported_cpuid()"), simply restrict code
belonging to system emulation, easing user emulation linking.
Reported-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
target/i386/cpu.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index 24ee67b42d..83914d5d1b 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -6163,6 +6163,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
}
*edx = env->features[FEAT_7_0_EDX]; /* Feature flags */
+#ifndef CONFIG_USER_ONLY
/*
* SGX cannot be emulated in software. If hardware does not
* support enabling SGX and/or SGX flexible launch control,
@@ -6181,6 +6182,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
CPUID_7_0_ECX_SGX_LC))) {
*ecx &= ~CPUID_7_0_ECX_SGX_LC;
}
+#endif
} else if (count == 1) {
*eax = env->features[FEAT_7_1_EAX];
*edx = env->features[FEAT_7_1_EDX];
@@ -7152,6 +7154,7 @@ static void x86_cpu_filter_features(X86CPU *cpu, bool verbose)
mark_unavailable_features(cpu, w, unavailable_features, prefix);
}
+#ifndef CONFIG_USER_ONLY
if ((env->features[FEAT_7_0_EBX] & CPUID_7_0_EBX_INTEL_PT) &&
kvm_enabled()) {
KVMState *s = CPU(cpu)->kvm_state;
@@ -7179,6 +7182,7 @@ static void x86_cpu_filter_features(X86CPU *cpu, bool verbose)
mark_unavailable_features(cpu, FEAT_7_0_EBX, CPUID_7_0_EBX_INTEL_PT, prefix);
}
}
+#endif
}
static void x86_cpu_hyperv_realize(X86CPU *cpu)
--
2.41.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v3] target/i386: Restrict system-specific features from user emulation
2023-09-11 14:27 [PATCH v3] target/i386: Restrict system-specific features from user emulation Philippe Mathieu-Daudé
@ 2023-09-11 14:38 ` Michael Tokarev
2023-09-11 16:38 ` Kevin Wolf
1 sibling, 0 replies; 3+ messages in thread
From: Michael Tokarev @ 2023-09-11 14:38 UTC (permalink / raw)
To: Philippe Mathieu-Daudé, qemu-devel
Cc: kvm, Stefan Hajnoczi, Daniel Henrique Barboza,
Daniel P . Berrangé, Richard Henderson, Paolo Bonzini,
Marcelo Tosatti, Kevin Wolf
Reviewed-by: Michael Tokarev <mjt@tls.msk.ru>
Thank you for patience!
11.09.2023 17:27, Philippe Mathieu-Daudé:
> Since commits 3adce820cf ("target/i386: Remove unused KVM
> stubs") and ef1cf6890f ("target/i386: Allow elision of
> kvm_hv_vpindex_settable()"), when building on a x86 host
...
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v3] target/i386: Restrict system-specific features from user emulation
2023-09-11 14:27 [PATCH v3] target/i386: Restrict system-specific features from user emulation Philippe Mathieu-Daudé
2023-09-11 14:38 ` Michael Tokarev
@ 2023-09-11 16:38 ` Kevin Wolf
1 sibling, 0 replies; 3+ messages in thread
From: Kevin Wolf @ 2023-09-11 16:38 UTC (permalink / raw)
To: Philippe Mathieu-Daudé
Cc: qemu-devel, kvm, Stefan Hajnoczi, Daniel Henrique Barboza,
Daniel P . Berrangé, Michael Tokarev, Richard Henderson,
Paolo Bonzini, Marcelo Tosatti
Am 11.09.2023 um 16:27 hat Philippe Mathieu-Daudé geschrieben:
> Since commits 3adce820cf ("target/i386: Remove unused KVM
> stubs") and ef1cf6890f ("target/i386: Allow elision of
> kvm_hv_vpindex_settable()"), when building on a x86 host
> configured as:
>
> $ ./configure --cc=clang \
> --target-list=x86_64-linux-user,x86_64-softmmu \
> --enable-debug
>
> we get:
>
> [71/71] Linking target qemu-x86_64
> FAILED: qemu-x86_64
> /usr/bin/ld: libqemu-x86_64-linux-user.fa.p/target_i386_cpu.c.o: in function `cpu_x86_cpuid':
> cpu.c:(.text+0x1374): undefined reference to `kvm_arch_get_supported_cpuid'
> /usr/bin/ld: libqemu-x86_64-linux-user.fa.p/target_i386_cpu.c.o: in function `x86_cpu_filter_features':
> cpu.c:(.text+0x81c2): undefined reference to `kvm_arch_get_supported_cpuid'
> /usr/bin/ld: cpu.c:(.text+0x81da): undefined reference to `kvm_arch_get_supported_cpuid'
> /usr/bin/ld: cpu.c:(.text+0x81f2): undefined reference to `kvm_arch_get_supported_cpuid'
> /usr/bin/ld: cpu.c:(.text+0x820a): undefined reference to `kvm_arch_get_supported_cpuid'
> /usr/bin/ld: libqemu-x86_64-linux-user.fa.p/target_i386_cpu.c.o:cpu.c:(.text+0x8225): more undefined references to `kvm_arch_get_supported_cpuid' follow
> clang: error: linker command failed with exit code 1 (use -v to see invocation)
> ninja: build stopped: subcommand failed.
>
> libqemu-x86_64-linux-user.fa is user emulation specific, so
> having system emulation code called there is dubious.
>
> '--enable-debug' disables optimizations (CFLAGS=-O0).
>
> While at this (un)optimization level GCC eliminate the
> following dead code (CPP output of mentioned build):
>
> static void x86_cpu_get_supported_cpuid(uint32_t func, uint32_t index,
> uint32_t *eax, uint32_t *ebx,
> uint32_t *ecx, uint32_t *edx)
> {
> if ((0)) {
> *eax = kvm_arch_get_supported_cpuid(kvm_state, func, index, R_EAX);
> *ebx = kvm_arch_get_supported_cpuid(kvm_state, func, index, R_EBX);
> *ecx = kvm_arch_get_supported_cpuid(kvm_state, func, index, R_ECX);
> *edx = kvm_arch_get_supported_cpuid(kvm_state, func, index, R_EDX);
> } else if (0) {
> *eax = 0;
> *ebx = 0;
> *ecx = 0;
> *edx = 0;
> } else {
> *eax = 0;
> *ebx = 0;
> *ecx = 0;
> *edx = 0;
> }
> }
>
> Clang does not.
>
> Instead of trying to deal with compiler specific checks around
> __OPTIMIZE__ (see commit 2140cfa51d "i386: Fix build by providing
> stub kvm_arch_get_supported_cpuid()"), simply restrict code
> belonging to system emulation, easing user emulation linking.
>
> Reported-by: Kevin Wolf <kwolf@redhat.com>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Can we make the function declarations in the header file for the
functions without stubs conditional on !CONFIG_USER_ONLY, too, so that
trying to call them would already fail during compilation (and also with
-O2), not only when linking without optimisations?
Kevin
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-09-11 16:39 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-11 14:27 [PATCH v3] target/i386: Restrict system-specific features from user emulation Philippe Mathieu-Daudé
2023-09-11 14:38 ` Michael Tokarev
2023-09-11 16:38 ` Kevin Wolf
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).