tests: avoid ioctl_kvm_run test failure when built with gcc -O0

When built with -O0, gcc (rightfully) generates function prologue, which
results in writing %rbp to the stack, causing premature KVM_EXIT_MMIO.
It could be possible to avoid such problems by "naked" attribute but,
unfortunately, the latter is not available on x86 with older GCC.
A trick suggested in [1] is used instead: assembly is moved
to the global scope.

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242#c3

* tests/ioctl_kvm_run.c (code): Remove function.
Add globally scoped __asm__ with the function code and its size.
(code, code_size): New extern symbols declarations.
(run_kvm): Remove code_size definition and initialization.

Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
This commit is contained in:
Eugene Syromyatnikov 2018-02-13 02:38:54 +01:00 committed by Dmitry V. Levin
parent 9730e10317
commit 101621d4c7

View File

@ -59,11 +59,23 @@ static const char vm_dev[] = "anon_inode:kvm-vm";
static const char vcpu_dev[] = "anon_inode:kvm-vcpu";
static size_t page_size;
static void
code(void)
{
__asm__("mov $0xd80003f8, %edx; mov $'\n', %al; out %al, (%dx); hlt");
}
extern const char code[];
extern const unsigned short code_size;
__asm__(
".type code, @object \n"
"code: \n"
" mov $0xd80003f8, %edx \n"
" mov $'\n', %al \n"
" out %al, (%dx) \n"
" hlt \n"
".size code, . - code \n"
".type code_size, @object \n"
"code_size: \n"
" .short . - code \n"
".size code_size, . - code_size \n"
);
static void
run_kvm(const int vcpu_fd, struct kvm_run *const run, const size_t mmap_size,
@ -107,10 +119,7 @@ run_kvm(const int vcpu_fd, struct kvm_run *const run, const size_t mmap_size,
(uintmax_t) regs.rsp, (uintmax_t) regs.rbp,
(uintmax_t) regs.rip, (uintmax_t) regs.rflags);
/* Copy the code till the end of page */
size_t code_size = page_size - ((uintptr_t) code & (page_size - 1));
if (code_size < 16)
code_size = 16;
/* Copy the code */
memcpy(mem, code, code_size);
const char *p = "\n";