Skip to contentSkip to conversation

Patch queue, part V (the rest)

PATCH Applied Jan Kiszka 3 participants 26 messages Mar 15, 2011 – Mar 18, 2011 KVM

lore
Share & tools
lore
author
#1 lore raw

PATCH v2 1/20 Implement qemu_kvm_eat_signals only for CONFIG_LINUX

In reply to Jan Kiszka

qemu_kvm_eat_signals requires POSIX support with realtime extensions for sigtimedwait. Not all our target platforms provide this. Moreover, undefined sigbus_reraise was referenced on non-Linux as well.

CC Andreas Färber <[email protected]>
---
 cpus.c |   94 ++++++++++++++++++++++++++++++++--------------------------------
 1 files changed, 47 insertions(+), 47 deletions(-)
cpus.c +47 -47
index 077729c..26e5bba 100644
@@ -245,11 +245,58 @@ static void qemu_init_sigbus(void)
     prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
 }
 
+static void qemu_kvm_eat_signals(CPUState *env)
+{
+    struct timespec ts = { 0, 0 };
+    siginfo_t siginfo;
+    sigset_t waitset;
+    sigset_t chkset;
+    int r;
+
+    sigemptyset(&waitset);
+    sigaddset(&waitset, SIG_IPI);
+    sigaddset(&waitset, SIGBUS);
+
+    do {
+        r = sigtimedwait(&waitset, &siginfo, &ts);
+        if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
+            perror("sigtimedwait");
+            exit(1);
+        }
+
+        switch (r) {
+        case SIGBUS:
+            if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
+                sigbus_reraise();
+            }
+            break;
+        default:
+            break;
+        }
+
+        r = sigpending(&chkset);
+        if (r == -1) {
+            perror("sigpending");
+            exit(1);
+        }
+    } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
+
+#ifndef CONFIG_IOTHREAD
+    if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
+        qemu_notify_event();
+    }
+#endif
+}
+
 #else /* !CONFIG_LINUX */
 
 static void qemu_init_sigbus(void)
 {
 }
+
+static void qemu_kvm_eat_signals(CPUState *env)
+{
+}
 #endif /* !CONFIG_LINUX */
 
 #ifndef _WIN32
@@ -455,49 +502,6 @@ static void qemu_tcg_init_cpu_signals(void)
 #endif
 }
 
-static void qemu_kvm_eat_signals(CPUState *env)
-{
-    struct timespec ts = { 0, 0 };
-    siginfo_t siginfo;
-    sigset_t waitset;
-    sigset_t chkset;
-    int r;
-
-    sigemptyset(&waitset);
-    sigaddset(&waitset, SIG_IPI);
-    sigaddset(&waitset, SIGBUS);
-
-    do {
-        r = sigtimedwait(&waitset, &siginfo, &ts);
-        if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
-            perror("sigtimedwait");
-            exit(1);
-        }
-
-        switch (r) {
-        case SIGBUS:
-            if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
-                sigbus_reraise();
-            }
-            break;
-        default:
-            break;
-        }
-
-        r = sigpending(&chkset);
-        if (r == -1) {
-            perror("sigpending");
-            exit(1);
-        }
-    } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
-
-#ifndef CONFIG_IOTHREAD
-    if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
-        qemu_notify_event();
-    }
-#endif
-}
-
 #else /* _WIN32 */
 
 HANDLE qemu_event_handle;
@@ -526,10 +530,6 @@ static void qemu_event_increment(void)
     }
 }
 
-static void qemu_kvm_eat_signals(CPUState *env)
-{
-}
-
 static int qemu_signal_init(void)
 {
     return 0;
Signature · 1 line
1.7.1
author
#6 lore raw

PATCH v2 6/20 kvm: Install optimized interrupt handler

In reply to Jan Kiszka

KVM only requires to set the raised IRQ in CPUState and to kick the receiving vcpu if it is remote.

---
 kvm-all.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
kvm-all.c +11 -0
index 226843c..25ab545 100644
@@ -650,6 +650,15 @@ static CPUPhysMemoryClient kvm_cpu_phys_memory_client = {
     .log_stop = kvm_log_stop,
 };
 
+static void kvm_handle_interrupt(CPUState *env, int mask)
+{
+    env->interrupt_request |= mask;
+
+    if (!qemu_cpu_is_self(env)) {
+        qemu_cpu_kick(env);
+    }
+}
+
 int kvm_init(void)
 {
     static const char upgrade_note[] =
@@ -758,6 +767,8 @@ int kvm_init(void)
 
     s->many_ioeventfds = kvm_check_many_ioeventfds();
 
+    cpu_interrupt_handler = kvm_handle_interrupt;
+
     return 0;
 
 err:
Signature · 1 line
1.7.1
author
#12 lore raw

PATCH v2 8/20 kvm: x86: Do not leave halt if interrupts are disabled

In reply to Jan Kiszka

When an external interrupt is pending but IF is cleared, we must not leave the halt state prematurely.

---
 target-i386/kvm.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
target-i386/kvm.c +3 -1
index f7995bd..3a07fce 100644
@@ -1590,7 +1590,9 @@ int kvm_arch_process_async_events(CPUState *env)
         return 0;
     }
 
-    if (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI)) {
+    if (((env->interrupt_request & CPU_INTERRUPT_HARD) &&
+         (env->eflags & IF_MASK)) ||
+        (env->interrupt_request & CPU_INTERRUPT_NMI)) {
         env->halted = 0;
     }
     if (env->interrupt_request & CPU_INTERRUPT_INIT) {
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH v2 2/20 x86: Unbreak TCG support for hardware breakpoints

In reply to Jan Kiszka

Commit 83f338f73e broke x86 hardware breakpoint emulation by moving the debug exception handling out of cpu_exec. Fix this by moving all TCG related bits back, only leaving the generic guest debugging parts in cpus.c.

---
 cpu-exec.c |   27 +++++++++++++++++++++++++++
 cpus.c     |   27 +++------------------------
 2 files changed, 30 insertions(+), 24 deletions(-)
cpu-exec.c +27 -0
index 34eaedc..5cc9379 100644
@@ -196,6 +196,30 @@ static inline TranslationBlock *tb_find_fast(void)
     return tb;
 }
 
+static CPUDebugExcpHandler *debug_excp_handler;
+
+CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler)
+{
+    CPUDebugExcpHandler *old_handler = debug_excp_handler;
+
+    debug_excp_handler = handler;
+    return old_handler;
+}
+
+static void cpu_handle_debug_exception(CPUState *env)
+{
+    CPUWatchpoint *wp;
+
+    if (!env->watchpoint_hit) {
+        QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
+            wp->flags &= ~BP_WATCHPOINT_HIT;
+        }
+    }
+    if (debug_excp_handler) {
+        debug_excp_handler(env);
+    }
+}
+
 /* main execution loop */
 
 volatile sig_atomic_t exit_request;
@@ -269,6 +293,9 @@ int cpu_exec(CPUState *env1)
                 if (env->exception_index >= EXCP_INTERRUPT) {
                     /* exit request from the cpu execution loop */
                     ret = env->exception_index;
+                    if (ret == EXCP_DEBUG) {
+                        cpu_handle_debug_exception(env);
+                    }
                     break;
                 } else {
 #if defined(CONFIG_USER_ONLY)
cpus.c +3 -24
index 26e5bba..975a6ce 100644
@@ -166,29 +166,8 @@ static bool all_cpu_threads_idle(void)
     return true;
 }
 
-static CPUDebugExcpHandler *debug_excp_handler;
-
-CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler)
-{
-    CPUDebugExcpHandler *old_handler = debug_excp_handler;
-
-    debug_excp_handler = handler;
-    return old_handler;
-}
-
-static void cpu_handle_debug_exception(CPUState *env)
+static void cpu_handle_guest_debug(CPUState *env)
 {
-    CPUWatchpoint *wp;
-
-    if (!env->watchpoint_hit) {
-        QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
-            wp->flags &= ~BP_WATCHPOINT_HIT;
-        }
-    }
-    if (debug_excp_handler) {
-        debug_excp_handler(env);
-    }
-
     gdb_set_stop_cpu(env);
     qemu_system_debug_request();
 #ifdef CONFIG_IOTHREAD
@@ -818,7 +797,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)
         if (cpu_can_run(env)) {
             r = kvm_cpu_exec(env);
             if (r == EXCP_DEBUG) {
-                cpu_handle_debug_exception(env);
+                cpu_handle_guest_debug(env);
             }
         }
         qemu_kvm_wait_io_event(env);
@@ -1110,7 +1089,7 @@ bool cpu_exec_all(void)
                 r = tcg_cpu_exec(env);
             }
             if (r == EXCP_DEBUG) {
-                cpu_handle_debug_exception(env);
+                cpu_handle_guest_debug(env);
                 break;
             }
         } else if (env->stop || env->stopped) {
Signature · 1 line
1.7.1
author
#3 lore raw

PATCH v2 3/20 s390: Detect invalid invocations of qemu_ram_free/remap

In reply to Jan Kiszka

This both detects invalid invocations of qemu_ram_free and qemu_ram_remap when mem_path is non-NULL and fixes a build error on s390 ("'area' may be used uninitialized in this function").

CC Alexander Graf <[email protected]>
---
 exec.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
exec.c +4 -0
index 723ace4..c5358c3 100644
@@ -2931,6 +2931,8 @@ void qemu_ram_free(ram_addr_t addr)
                 } else {
                     qemu_vfree(block->host);
                 }
+#else
+                abort();
 #endif
             } else {
 #if defined(TARGET_S390X) && defined(CONFIG_KVM)
@@ -2979,6 +2981,8 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
                         area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
                                     flags, -1, 0);
                     }
+#else
+                    abort();
 #endif
                 } else {
 #if defined(TARGET_S390X) && defined(CONFIG_KVM)
Signature · 1 line
1.7.1
author
#4 lore raw

PATCH v2 4/20 Break up user and system cpu_interrupt implementations

In reply to Jan Kiszka

Both have only two lines in common, and we will convert the system service into a callback which is of no use for user mode operation.

---
 exec.c |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)
exec.c +10 -4
index c5358c3..12ea582 100644
@@ -1627,6 +1627,7 @@ static void cpu_unlink_tb(CPUState *env)
     spin_unlock(&interrupt_lock);
 }
 
+#ifndef CONFIG_USER_ONLY
 /* mask must never be zero, except for A20 change call */
 void cpu_interrupt(CPUState *env, int mask)
 {
@@ -1635,7 +1636,6 @@ void cpu_interrupt(CPUState *env, int mask)
     old_mask = env->interrupt_request;
     env->interrupt_request |= mask;
 
-#ifndef CONFIG_USER_ONLY
     /*
      * If called from iothread context, wake the target cpu in
      * case its halted.
@@ -1644,21 +1644,27 @@ void cpu_interrupt(CPUState *env, int mask)
         qemu_cpu_kick(env);
         return;
     }
-#endif
 
     if (use_icount) {
         env->icount_decr.u16.high = 0xffff;
-#ifndef CONFIG_USER_ONLY
         if (!can_do_io(env)
             && (mask & ~old_mask) != 0) {
             cpu_abort(env, "Raised interrupt while not in I/O function");
         }
-#endif
     } else {
         cpu_unlink_tb(env);
     }
 }
 
+#else /* CONFIG_USER_ONLY */
+
+void cpu_interrupt(CPUState *env, int mask)
+{
+    env->interrupt_request |= mask;
+    cpu_unlink_tb(env);
+}
+#endif /* CONFIG_USER_ONLY */
+
 void cpu_reset_interrupt(CPUState *env, int mask)
 {
     env->interrupt_request &= ~mask;
Signature · 1 line
1.7.1
author
#5 lore raw

PATCH v2 5/20 Redirect cpu_interrupt to callback handler

In reply to Jan Kiszka

This allows to override the interrupt handling of QEMU in system mode. KVM will make use of it to set optimized handlers.

---
 cpu-all.h |   14 +++++++++++++-
 exec.c    |    4 +++-
 2 files changed, 16 insertions(+), 2 deletions(-)
cpu-all.h +13 -1
index 4f4631d..5835cfa 100644
@@ -790,7 +790,19 @@ extern CPUState *cpu_single_env;
 #define CPU_INTERRUPT_SIPI   0x800 /* SIPI pending. */
 #define CPU_INTERRUPT_MCE    0x1000 /* (x86 only) MCE pending. */
 
-void cpu_interrupt(CPUState *s, int mask);
+#ifndef CONFIG_USER_ONLY
+typedef void (*CPUInterruptHandler)(CPUState *, int);
+
+extern CPUInterruptHandler cpu_interrupt_handler;
+
+static inline void cpu_interrupt(CPUState *s, int mask)
+{
+    cpu_interrupt_handler(s, mask);
+}
+#else /* USER_ONLY */
+void cpu_interrupt(CPUState *env, int mask);
+#endif /* USER_ONLY */
+
 void cpu_reset_interrupt(CPUState *env, int mask);
 
 void cpu_exit(CPUState *s);
exec.c +3 -1
index 12ea582..b59f7ff 100644
@@ -1629,7 +1629,7 @@ static void cpu_unlink_tb(CPUState *env)
 
 #ifndef CONFIG_USER_ONLY
 /* mask must never be zero, except for A20 change call */
-void cpu_interrupt(CPUState *env, int mask)
+static void tcg_handle_interrupt(CPUState *env, int mask)
 {
     int old_mask;
 
@@ -1656,6 +1656,8 @@ void cpu_interrupt(CPUState *env, int mask)
     }
 }
 
+CPUInterruptHandler cpu_interrupt_handler = tcg_handle_interrupt;
+
 #else /* CONFIG_USER_ONLY */
 
 void cpu_interrupt(CPUState *env, int mask)
Signature · 1 line
1.7.1
author
#11 lore raw

PATCH v2 7/20 kvm: Add in-kernel irqchip awareness to cpu_thread_is_idle

In reply to Jan Kiszka

With in-kernel irqchip support enabled, the vcpu threads sleep in kernel space while halted. Account for this difference in cpu_thread_is_idle.

---
 cpus.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
cpus.c +2 -1
index 975a6ce..d310b7e 100644
@@ -148,7 +148,8 @@ static bool cpu_thread_is_idle(CPUState *env)
     if (env->stopped || !vm_running) {
         return true;
     }
-    if (!env->halted || qemu_cpu_has_work(env)) {
+    if (!env->halted || qemu_cpu_has_work(env) ||
+        (kvm_enabled() && kvm_irqchip_in_kernel())) {
         return false;
     }
     return true;
Signature · 1 line
1.7.1
author
#14 lore raw

PATCH v2 10/20 x86: Properly reset PAT MSR

In reply to Jan Kiszka

Conforming to the Intel spec, set the power-on value of PAT also on reset, but save it across INIT.

---
 target-i386/cpu.h    |    4 ++--
 target-i386/cpuid.c  |    1 -
 target-i386/helper.c |    5 +++++
 3 files changed, 7 insertions(+), 3 deletions(-)
target-i386/cpu.h +2 -2
index d0eae75..c7047d5 100644
@@ -685,8 +685,6 @@ typedef struct CPUX86State {
 
     uint64_t tsc;
 
-    uint64_t pat;
-
     uint64_t mcg_status;
 
     /* exception/interrupt handling */
@@ -707,6 +705,8 @@ typedef struct CPUX86State {
 
     CPU_COMMON
 
+    uint64_t pat;
+
     /* processor features (e.g. for CPUID insn) */
     uint32_t cpuid_level;
     uint32_t cpuid_vendor1;
target-i386/cpuid.c +0 -1
index 5382a28..814d13e 100644
@@ -847,7 +847,6 @@ int cpu_x86_register (CPUX86State *env, const char *cpu_model)
     env->cpuid_version |= ((def->model & 0xf) << 4) | ((def->model >> 4) << 16);
     env->cpuid_version |= def->stepping;
     env->cpuid_features = def->features;
-    env->pat = 0x0007040600070406ULL;
     env->cpuid_ext_features = def->ext_features;
     env->cpuid_ext2_features = def->ext2_features;
     env->cpuid_ext3_features = def->ext3_features;
target-i386/helper.c +5 -0
index a08309f..d15fca5 100644
@@ -99,6 +99,8 @@ void cpu_reset(CPUX86State *env)
 
     env->mxcsr = 0x1f80;
 
+    env->pat = 0x0007040600070406ULL;
+
     memset(env->dr, 0, sizeof(env->dr));
     env->dr[6] = DR6_FIXED_1;
     env->dr[7] = DR7_FIXED_1;
@@ -1280,8 +1282,11 @@ CPUX86State *cpu_x86_init(const char *cpu_model)
 void do_cpu_init(CPUState *env)
 {
     int sipi = env->interrupt_request & CPU_INTERRUPT_SIPI;
+    uint64_t pat = env->pat;
+
     cpu_reset(env);
     env->interrupt_request = sipi;
+    env->pat = pat;
     apic_init_reset(env->apic_state);
     env->halted = !cpu_is_bsp(env);
 }
Signature · 1 line
1.7.1
author
#16 lore raw

PATCH v2 12/20 kvm: x86: Synchronize PAT MSR with the kernel

In reply to Jan Kiszka
---
 target-i386/kvm.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
target-i386/kvm.c +5 -0
index 3a07fce..032bc3e 100644
@@ -861,6 +861,7 @@ static int kvm_put_msrs(CPUState *env, int level)
     kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs);
     kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
     kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
+    kvm_msr_entry_set(&msrs[n++], MSR_PAT, env->pat);
     if (has_msr_star) {
         kvm_msr_entry_set(&msrs[n++], MSR_STAR, env->star);
     }
@@ -1113,6 +1114,7 @@ static int kvm_get_msrs(CPUState *env)
     msrs[n++].index = MSR_IA32_SYSENTER_CS;
     msrs[n++].index = MSR_IA32_SYSENTER_ESP;
     msrs[n++].index = MSR_IA32_SYSENTER_EIP;
+    msrs[n++].index = MSR_PAT;
     if (has_msr_star) {
         msrs[n++].index = MSR_STAR;
     }
@@ -1168,6 +1170,9 @@ static int kvm_get_msrs(CPUState *env)
         case MSR_IA32_SYSENTER_EIP:
             env->sysenter_eip = msrs[i].data;
             break;
+        case MSR_PAT:
+            env->pat = msrs[i].data;
+            break;
         case MSR_STAR:
             env->star = msrs[i].data;
             break;
Signature · 1 line
1.7.1

Jan Kiszka

author
#0 lore raw

v2 cover Patch queue, part V (the rest)

This series catches "all the rest" to prepare QEMU's KVM support for merging with qemu-kvm. IOW, once these bits here are applied, qemu-kvm can switch its infrastructure to upstream and is effectively only adding own bits for in-kernel irqchip and device assignment support.

Topics of this series are:
- support for optimized interrupt handling by hooking cpu_interrupt
- another preparational step for in-kernel irqchip support
- x86: Do not leave halt if interrupts are disabled
- mark VCPU state dirty on creation (fixed deadlock on early hw_error)
- complete KVM support for PAT MSR, some related improvements for TCG
- further consolidation of inner kvm_cpu_exec loop
- expose VCPU host thread ID via "info cpus" and "query-cpus"

Changes in v2:
- Rebased over current uq/master
- Build fix for MAC OS (regression of previous round)
- Fix for x86 hardware breakpoints in TCG mode (regression of previous round)
- Build fix for s390 (regression of previous round)
- Removed premature optimization from "Install optimized interrupt handlers"
- Keep KVM_RUN return value in separate variable (cleanup)
- Reorder error handling of KVM_RUN (micro-optimization)

CC Alexander Graf <[email protected]>
CC Andreas Färber <[email protected]>
CC Riku Voipio <[email protected]>
CC TeLeMan <[email protected]>

Jan Kiszka (20):
Implement qemu_kvm_eat_signals only for CONFIG_LINUX
x86: Unbreak TCG support for hardware breakpoints
s390: Detect invalid invocations of qemu_ram_free/remap Break up user and system cpu_interrupt implementations Redirect cpu_interrupt to callback handler
kvm: Install optimized interrupt handler
kvm: Add in-kernel irqchip awareness to cpu_thread_is_idle
kvm: x86: Do not leave halt if interrupts are disabled
kvm: Mark VCPU state dirty on creation
x86: Properly reset PAT MSR
x86: Save/restore PAT MSR
kvm: x86: Synchronize PAT MSR with the kernel
kvm: Consider EXIT_DEBUG unknown without CAP_SET_GUEST_DEBUG
kvm: Keep KVM_RUN return value in separate variable
kvm: Reorder error handling of KVM_RUN
kvm: Rework inner loop of kvm_cpu_exec
kvm: Align kvm_arch_handle_exit to kvm_cpu_exec changes
kvm: x86: Reorder functions in kvm.c
kvm: x86: Push kvm_arch_debug to kvm_arch_handle_exit Expose thread_id in info cpus

 cpu-all.h             |   14 ++++-
 cpu-defs.h            |    1 +
 cpu-exec.c            |   27 +++++++++
 cpus.c                |  126 ++++++++++++++++++------------------------
 exec.c                |   25 +++++++--
 kvm-all.c             |   57 +++++++++----------
 kvm.h                 |    2 -
 monitor.c             |    4 +
 os-posix.c            |   10 +++
 os-win32.c            |    5 ++
 osdep.h               |    1 +
 qmp-commands.hx       |    3 +
 target-i386/cpu.h     |    4 +-
 target-i386/cpuid.c   |    1 -
 target-i386/helper.c  |    5 ++
 target-i386/kvm.c     |  146 +++++++++++++++++++++++++++----------------------
 target-i386/machine.c |    2 +
 target-ppc/kvm.c      |    8 +-
 target-s390x/kvm.c    |    5 ++
 19 files changed, 263 insertions(+), 183 deletions(-)
author
#13 lore raw

PATCH v2 9/20 kvm: Mark VCPU state dirty on creation

This avoids that early cpu_synchronize_state calls try to retrieve an uninitialized state from the kernel. That even causes a deadlock if io-thread is enabled.

---
 kvm-all.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
kvm-all.c +1 -0
index 25ab545..36553fe 100644
@@ -211,6 +211,7 @@ int kvm_init_vcpu(CPUState *env)
 
     env->kvm_fd = ret;
     env->kvm_state = s;
+    env->kvm_vcpu_dirty = 1;
 
     mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
     if (mmap_size < 0) {
Signature · 1 line
1.7.1
author
#15 lore raw

PATCH v2 11/20 x86: Save/restore PAT MSR

In reply to Jan Kiszka
---
 target-i386/machine.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
target-i386/machine.c +2 -0
index d78eceb..6384f54 100644
@@ -491,6 +491,8 @@ static const VMStateDescription vmstate_cpu = {
         VMSTATE_UINT64_V(xcr0, CPUState, 12),
         VMSTATE_UINT64_V(xstate_bv, CPUState, 12),
         VMSTATE_YMMH_REGS_VARS(ymmh_regs, CPUState, CPU_NB_REGS, 12),
+
+        VMSTATE_UINT64_V(pat, CPUState, 13),
         VMSTATE_END_OF_LIST()
         /* The above list is not sorted /wrt version numbers, watch out! */
     },
Signature · 1 line
1.7.1
author
#20 lore raw

PATCH v2 16/20 kvm: Rework inner loop of kvm_cpu_exec

In reply to Jan Kiszka

Let kvm_cpu_exec return EXCP_* values consistently and generate those codes already inside its inner loop. This means we will now re-enter the kernel while ret == 0.

Update kvm_handle_internal_error accordingly, but keep kvm_arch_handle_exit untouched, it will be converted in a separate step.

---
 kvm-all.c |   26 ++++++++++++++------------
 1 files changed, 14 insertions(+), 12 deletions(-)
kvm-all.c +14 -12
index 59276cd..e6ff95c 100644
@@ -842,7 +842,7 @@ static int kvm_handle_internal_error(CPUState *env, struct kvm_run *run)
         fprintf(stderr, "emulation failure\n");
         if (!kvm_arch_stop_on_emulation_error(env)) {
             cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
-            return 0;
+            return EXCP_INTERRUPT;
         }
     }
     /* FIXME: Should trigger a qmp message to let management know
@@ -942,14 +942,13 @@ int kvm_cpu_exec(CPUState *env)
         if (run_ret < 0) {
             if (run_ret == -EINTR || run_ret == -EAGAIN) {
                 DPRINTF("io window exit\n");
-                ret = 0;
+                ret = EXCP_INTERRUPT;
                 break;
             }
             DPRINTF("kvm run failed %s\n", strerror(-run_ret));
             abort();
         }
 
-        ret = 0; /* exit loop */
         switch (run->exit_reason) {
         case KVM_EXIT_IO:
             DPRINTF("handle_io\n");
@@ -958,7 +957,7 @@ int kvm_cpu_exec(CPUState *env)
                           run->io.direction,
                           run->io.size,
                           run->io.count);
-            ret = 1;
+            ret = 0;
             break;
         case KVM_EXIT_MMIO:
             DPRINTF("handle_mmio\n");
@@ -966,14 +965,16 @@ int kvm_cpu_exec(CPUState *env)
                                    run->mmio.data,
                                    run->mmio.len,
                                    run->mmio.is_write);
-            ret = 1;
+            ret = 0;
             break;
         case KVM_EXIT_IRQ_WINDOW_OPEN:
             DPRINTF("irq_window_open\n");
+            ret = EXCP_INTERRUPT;
             break;
         case KVM_EXIT_SHUTDOWN:
             DPRINTF("shutdown\n");
             qemu_system_reset_request();
+            ret = EXCP_INTERRUPT;
             break;
         case KVM_EXIT_UNKNOWN:
             fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
@@ -990,28 +991,29 @@ int kvm_cpu_exec(CPUState *env)
             DPRINTF("kvm_exit_debug\n");
             if (kvm_arch_debug(&run->debug.arch)) {
                 ret = EXCP_DEBUG;
-                goto out;
+                break;
             }
             /* re-enter, this exception was guest-internal */
-            ret = 1;
+            ret = 0;
             break;
 #endif /* KVM_CAP_SET_GUEST_DEBUG */
         default:
             DPRINTF("kvm_arch_handle_exit\n");
             ret = kvm_arch_handle_exit(env, run);
+            if (ret == 0) {
+                ret = EXCP_INTERRUPT;
+            } else if (ret > 0) {
+                ret = 0;
+            }
             break;
         }
-    } while (ret > 0);
+    } while (ret == 0);
 
     if (ret < 0) {
         cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
         vm_stop(VMSTOP_PANIC);
     }
-    ret = EXCP_INTERRUPT;
 
-#ifdef KVM_CAP_SET_GUEST_DEBUG
-out:
-#endif
     env->exit_request = 0;
     cpu_single_env = NULL;
     return ret;
Signature · 1 line
1.7.1
author
#18 lore raw

PATCH v2 14/20 kvm: Keep KVM_RUN return value in separate variable

In reply to Jan Kiszka

Avoid using 'ret' both for the return value of KVM_RUN as well as the code kvm_cpu_exec is supposed to return. Both have no direct relation.

---
 kvm-all.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
kvm-all.c +5 -5
index 982e5cc..99abe82 100644
@@ -901,7 +901,7 @@ void kvm_cpu_synchronize_post_init(CPUState *env)
 int kvm_cpu_exec(CPUState *env)
 {
     struct kvm_run *run = env->kvm_run;
-    int ret;
+    int ret, run_ret;
 
     DPRINTF("kvm_cpu_exec()\n");
 
@@ -931,7 +931,7 @@ int kvm_cpu_exec(CPUState *env)
         cpu_single_env = NULL;
         qemu_mutex_unlock_iothread();
 
-        ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
+        run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
 
         qemu_mutex_lock_iothread();
         cpu_single_env = env;
@@ -939,14 +939,14 @@ int kvm_cpu_exec(CPUState *env)
 
         kvm_flush_coalesced_mmio_buffer();
 
-        if (ret == -EINTR || ret == -EAGAIN) {
+        if (run_ret == -EINTR || run_ret == -EAGAIN) {
             DPRINTF("io window exit\n");
             ret = 0;
             break;
         }
 
-        if (ret < 0) {
-            DPRINTF("kvm run failed %s\n", strerror(-ret));
+        if (run_ret < 0) {
+            DPRINTF("kvm run failed %s\n", strerror(-run_ret));
             abort();
         }
 
Signature · 1 line
1.7.1
author
#21 lore raw

PATCH v2 17/20 kvm: Align kvm_arch_handle_exit to kvm_cpu_exec changes

In reply to Jan Kiszka

Make the return code of kvm_arch_handle_exit directly usable for kvm_cpu_exec. This is straightforward for x86 and ppc, just s390 would require more work. Avoid this for now by pushing the return code translation logic into s390's kvm_arch_handle_exit.

CC Alexander Graf <[email protected]>
---
 kvm-all.c          |    5 -----
 target-i386/kvm.c  |    8 ++++----
 target-ppc/kvm.c   |    8 ++++----
 target-s390x/kvm.c |    5 +++++
 4 files changed, 13 insertions(+), 13 deletions(-)
kvm-all.c +0 -5
index e6ff95c..78e4fbf 100644
@@ -1000,11 +1000,6 @@ int kvm_cpu_exec(CPUState *env)
         default:
             DPRINTF("kvm_arch_handle_exit\n");
             ret = kvm_arch_handle_exit(env, run);
-            if (ret == 0) {
-                ret = EXCP_INTERRUPT;
-            } else if (ret > 0) {
-                ret = 0;
-            }
             break;
         }
     } while (ret == 0);
target-i386/kvm.c +4 -4
index 032bc3e..6f84610 100644
@@ -1618,10 +1618,10 @@ static int kvm_handle_halt(CPUState *env)
           (env->eflags & IF_MASK)) &&
         !(env->interrupt_request & CPU_INTERRUPT_NMI)) {
         env->halted = 1;
-        return 0;
+        return EXCP_HLT;
     }
 
-    return 1;
+    return 0;
 }
 
 static bool host_supports_vmx(void)
@@ -1637,7 +1637,7 @@ static bool host_supports_vmx(void)
 int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run)
 {
     uint64_t code;
-    int ret = 0;
+    int ret;
 
     switch (run->exit_reason) {
     case KVM_EXIT_HLT:
@@ -1645,7 +1645,7 @@ int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run)
         ret = kvm_handle_halt(env);
         break;
     case KVM_EXIT_SET_TPR:
-        ret = 1;
+        ret = 0;
         break;
     case KVM_EXIT_FAIL_ENTRY:
         code = run->fail_entry.hardware_entry_failure_reason;
target-ppc/kvm.c +4 -4
index 6c99a16..593eb98 100644
@@ -271,7 +271,7 @@ static int kvmppc_handle_halt(CPUState *env)
         env->exception_index = EXCP_HLT;
     }
 
-    return 1;
+    return 0;
 }
 
 /* map dcr access to existing qemu dcr emulation */
@@ -280,7 +280,7 @@ static int kvmppc_handle_dcr_read(CPUState *env, uint32_t dcrn, uint32_t *data)
     if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0)
         fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
 
-    return 1;
+    return 0;
 }
 
 static int kvmppc_handle_dcr_write(CPUState *env, uint32_t dcrn, uint32_t data)
@@ -288,12 +288,12 @@ static int kvmppc_handle_dcr_write(CPUState *env, uint32_t dcrn, uint32_t data)
     if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0)
         fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
 
-    return 1;
+    return 0;
 }
 
 int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run)
 {
-    int ret = 0;
+    int ret;
 
     switch (run->exit_reason) {
     case KVM_EXIT_DCR:
target-s390x/kvm.c +5 -0
index a85ae0f..9123203 100644
@@ -497,6 +497,11 @@ int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run)
             break;
     }
 
+    if (ret == 0) {
+        ret = EXCP_INTERRUPT;
+    } else if (ret > 0) {
+        ret = 0;
+    }
     return ret;
 }
 
Signature · 1 line
1.7.1
author
#24 lore raw

PATCH v2 20/20 Expose thread_id in info cpus

In reply to Jan Kiszka

Based on patch by Glauber Costa:

To allow management applications like libvirt to apply CPU affinities to the VCPU threads, expose their ID via info cpus. This patch provides the pre-existing and used interface from qemu-kvm.

---
 cpu-defs.h      |    1 +
 cpus.c          |    2 ++
 exec.c          |    3 +++
 monitor.c       |    4 ++++
 os-posix.c      |   10 ++++++++++
 os-win32.c      |    5 +++++
 osdep.h         |    1 +
 qmp-commands.hx |    3 +++
 8 files changed, 29 insertions(+), 0 deletions(-)
cpu-defs.h +1 -0
index 2b59fa6..db48a7a 100644
@@ -203,6 +203,7 @@ typedef struct CPUWatchpoint {
     int nr_cores;  /* number of cores within this CPU package */        \
     int nr_threads;/* number of threads within this CPU */              \
     int running; /* Nonzero if cpu is currently running(usermode).  */  \
+    int thread_id;                                                      \
     /* user data */                                                     \
     void *opaque;                                                       \
                                                                         \
cpus.c +2 -0
index d310b7e..28c2da2 100644
@@ -776,6 +776,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)
 
     qemu_mutex_lock(&qemu_global_mutex);
     qemu_thread_get_self(env->thread);
+    env->thread_id = qemu_get_thread_id();
 
     r = kvm_init_vcpu(env);
     if (r < 0) {
@@ -817,6 +818,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
     /* signal CPU creation */
     qemu_mutex_lock(&qemu_global_mutex);
     for (env = first_cpu; env != NULL; env = env->next_cpu) {
+        env->thread_id = qemu_get_thread_id();
         env->created = 1;
     }
     qemu_cond_signal(&qemu_cpu_cond);
exec.c +3 -0
index b59f7ff..0c80f84 100644
@@ -638,6 +638,9 @@ void cpu_exec_init(CPUState *env)
     env->numa_node = 0;
     QTAILQ_INIT(&env->breakpoints);
     QTAILQ_INIT(&env->watchpoints);
+#ifndef CONFIG_USER_ONLY
+    env->thread_id = qemu_get_thread_id();
+#endif
     *penv = env;
 #if defined(CONFIG_USER_ONLY)
     cpu_list_unlock();
monitor.c +4 -0
index ae20927..481572d 100644
@@ -897,6 +897,9 @@ static void print_cpu_iter(QObject *obj, void *opaque)
         monitor_printf(mon, " (halted)");
     }
 
+    monitor_printf(mon, " thread_id=%" PRId64 " ",
+                   qdict_get_int(cpu, "thread_id"));
+
     monitor_printf(mon, "\n");
 }
 
@@ -941,6 +944,7 @@ static void do_info_cpus(Monitor *mon, QObject **ret_data)
 #elif defined(TARGET_MIPS)
         qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
 #endif
+        qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
 
         qlist_append(cpu_list, cpu);
     }
os-posix.c +10 -0
index 38c29d1..7971f86 100644
@@ -41,6 +41,7 @@
 
 #ifdef CONFIG_LINUX
 #include <sys/prctl.h>
+#include <sys/syscall.h>
 #endif
 
 #ifdef CONFIG_EVENTFD
@@ -382,3 +383,12 @@ int qemu_create_pidfile(const char *filename)
 
     return 0;
 }
+
+int qemu_get_thread_id(void)
+{
+#if defined (__linux__)
+    return syscall(SYS_gettid);
+#else
+    return getpid();
+#endif
+}
os-win32.c +5 -0
index c971d92..d6d54c6 100644
@@ -266,3 +266,8 @@ int qemu_create_pidfile(const char *filename)
     }
     return 0;
 }
+
+int qemu_get_thread_id(void)
+{
+    return GetCurrentThreadId();
+}
osdep.h +1 -0
index 27eedcf..748df54 100644
@@ -130,5 +130,6 @@ void qemu_vfree(void *ptr);
 int qemu_madvise(void *addr, size_t len, int advice);
 
 int qemu_create_pidfile(const char *filename);
+int qemu_get_thread_id(void);
 
 #endif
qmp-commands.hx +3 -0
index df40a3d..1f72a8d 100644
@@ -1194,6 +1194,7 @@ Return a json-array. Each CPU is represented by a json-object, which contains:
      "nip": PPC (json-int)
      "pc" and "npc": sparc (json-int)
      "PC": mips (json-int)
+- "thread_id": ID of the underlying host thread (json-int)
 
 Example:
 
@@ -1205,12 +1206,14 @@ Example:
             "current":true,
             "halted":false,
             "pc":3227107138
+            "thread_id":3134
          },
          {
             "CPU":1,
             "current":false,
             "halted":true,
             "pc":7108165
+            "thread_id":3135
          }
       ]
    }
Signature · 1 line
1.7.1
author
#17 lore raw

PATCH v2 13/20 kvm: Consider EXIT_DEBUG unknown without CAP_SET_GUEST_DEBUG

In reply to Jan Kiszka

Without KVM_CAP_SET_GUEST_DEBUG, we neither motivate the kernel to report KVM_EXIT_DEBUG nor do we expect such exits. So fall through to the arch code which will simply report an unknown exit reason.

---
 kvm-all.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kvm-all.c +2 -2
index 36553fe..982e5cc 100644
@@ -986,17 +986,17 @@ int kvm_cpu_exec(CPUState *env)
             ret = kvm_handle_internal_error(env, run);
             break;
 #endif
+#ifdef KVM_CAP_SET_GUEST_DEBUG
         case KVM_EXIT_DEBUG:
             DPRINTF("kvm_exit_debug\n");
-#ifdef KVM_CAP_SET_GUEST_DEBUG
             if (kvm_arch_debug(&run->debug.arch)) {
                 ret = EXCP_DEBUG;
                 goto out;
             }
             /* re-enter, this exception was guest-internal */
             ret = 1;
-#endif /* KVM_CAP_SET_GUEST_DEBUG */
             break;
+#endif /* KVM_CAP_SET_GUEST_DEBUG */
         default:
             DPRINTF("kvm_arch_handle_exit\n");
             ret = kvm_arch_handle_exit(env, run);
Signature · 1 line
1.7.1
author
#19 lore raw

PATCH v2 15/20 kvm: Reorder error handling of KVM_RUN

In reply to Jan Kiszka

Test for general errors first as this is the slower path.

---
 kvm-all.c |   11 +++++------
 1 files changed, 5 insertions(+), 6 deletions(-)
kvm-all.c +5 -6
index 99abe82..59276cd 100644
@@ -939,13 +939,12 @@ int kvm_cpu_exec(CPUState *env)
 
         kvm_flush_coalesced_mmio_buffer();
 
-        if (run_ret == -EINTR || run_ret == -EAGAIN) {
-            DPRINTF("io window exit\n");
-            ret = 0;
-            break;
-        }
-
         if (run_ret < 0) {
+            if (run_ret == -EINTR || run_ret == -EAGAIN) {
+                DPRINTF("io window exit\n");
+                ret = 0;
+                break;
+            }
             DPRINTF("kvm run failed %s\n", strerror(-run_ret));
             abort();
         }
Signature · 1 line
1.7.1
author
#23 lore raw

PATCH v2 19/20 kvm: x86: Push kvm_arch_debug to kvm_arch_handle_exit

In reply to Jan Kiszka

There are no generic bits remaining in the handling of KVM_EXIT_DEBUG. So push its logic completely into arch hands, i.e. only x86 so far.

---
 kvm-all.c         |   11 -----------
 kvm.h             |    2 --
 target-i386/kvm.c |   25 ++++++++++++++++---------
 3 files changed, 16 insertions(+), 22 deletions(-)
kvm-all.c +0 -11
index 78e4fbf..fd1fbfe 100644
@@ -986,17 +986,6 @@ int kvm_cpu_exec(CPUState *env)
             ret = kvm_handle_internal_error(env, run);
             break;
 #endif
-#ifdef KVM_CAP_SET_GUEST_DEBUG
-        case KVM_EXIT_DEBUG:
-            DPRINTF("kvm_exit_debug\n");
-            if (kvm_arch_debug(&run->debug.arch)) {
-                ret = EXCP_DEBUG;
-                break;
-            }
-            /* re-enter, this exception was guest-internal */
-            ret = 0;
-            break;
-#endif /* KVM_CAP_SET_GUEST_DEBUG */
         default:
             DPRINTF("kvm_arch_handle_exit\n");
             ret = kvm_arch_handle_exit(env, run);
kvm.h +0 -2
index 7bc04e0..d565dba 100644
@@ -136,8 +136,6 @@ struct kvm_sw_breakpoint {
 
 QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
 
-int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
-
 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
                                                  target_ulong pc);
 
target-i386/kvm.c +16 -9
index 3920444..a13599d 100644
@@ -1731,31 +1731,31 @@ void kvm_arch_remove_all_hw_breakpoints(void)
 
 static CPUWatchpoint hw_watchpoint;
 
-int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info)
+static int kvm_handle_debug(struct kvm_debug_exit_arch *arch_info)
 {
-    int handle = 0;
+    int ret = 0;
     int n;
 
     if (arch_info->exception == 1) {
         if (arch_info->dr6 & (1 << 14)) {
             if (cpu_single_env->singlestep_enabled) {
-                handle = 1;
+                ret = EXCP_DEBUG;
             }
         } else {
             for (n = 0; n < 4; n++) {
                 if (arch_info->dr6 & (1 << n)) {
                     switch ((arch_info->dr7 >> (16 + n*4)) & 0x3) {
                     case 0x0:
-                        handle = 1;
+                        ret = EXCP_DEBUG;
                         break;
                     case 0x1:
-                        handle = 1;
+                        ret = EXCP_DEBUG;
                         cpu_single_env->watchpoint_hit = &hw_watchpoint;
                         hw_watchpoint.vaddr = hw_breakpoint[n].addr;
                         hw_watchpoint.flags = BP_MEM_WRITE;
                         break;
                     case 0x3:
-                        handle = 1;
+                        ret = EXCP_DEBUG;
                         cpu_single_env->watchpoint_hit = &hw_watchpoint;
                         hw_watchpoint.vaddr = hw_breakpoint[n].addr;
                         hw_watchpoint.flags = BP_MEM_ACCESS;
@@ -1765,17 +1765,18 @@ int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info)
             }
         }
     } else if (kvm_find_sw_breakpoint(cpu_single_env, arch_info->pc)) {
-        handle = 1;
+        ret = EXCP_DEBUG;
     }
-    if (!handle) {
+    if (ret == 0) {
         cpu_synchronize_state(cpu_single_env);
         assert(cpu_single_env->exception_injected == -1);
 
+        /* pass to guest */
         cpu_single_env->exception_injected = arch_info->exception;
         cpu_single_env->has_error_code = 0;
     }
 
-    return handle;
+    return ret;
 }
 
 void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
@@ -1851,6 +1852,12 @@ int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run)
                 run->ex.exception, run->ex.error_code);
         ret = -1;
         break;
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+    case KVM_EXIT_DEBUG:
+        DPRINTF("kvm_exit_debug\n");
+        ret = kvm_handle_debug(&run->debug.arch);
+        break;
+#endif /* KVM_CAP_SET_GUEST_DEBUG */
     default:
         fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
         ret = -1;
Signature · 1 line
1.7.1
author
#22 lore raw

PATCH v2 18/20 kvm: x86: Reorder functions in kvm.c

In reply to Jan Kiszka

Required for next patch which will access guest debug services from kvm_arch_handle_exit. No functional changes.

---
 target-i386/kvm.c |  108 ++++++++++++++++++++++++++--------------------------
 1 files changed, 54 insertions(+), 54 deletions(-)
target-i386/kvm.c +54 -54
index 6f84610..3920444 100644
@@ -1624,60 +1624,6 @@ static int kvm_handle_halt(CPUState *env)
     return 0;
 }
 
-static bool host_supports_vmx(void)
-{
-    uint32_t ecx, unused;
-
-    host_cpuid(1, 0, &unused, &unused, &ecx, &unused);
-    return ecx & CPUID_EXT_VMX;
-}
-
-#define VMX_INVALID_GUEST_STATE 0x80000021
-
-int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run)
-{
-    uint64_t code;
-    int ret;
-
-    switch (run->exit_reason) {
-    case KVM_EXIT_HLT:
-        DPRINTF("handle_hlt\n");
-        ret = kvm_handle_halt(env);
-        break;
-    case KVM_EXIT_SET_TPR:
-        ret = 0;
-        break;
-    case KVM_EXIT_FAIL_ENTRY:
-        code = run->fail_entry.hardware_entry_failure_reason;
-        fprintf(stderr, "KVM: entry failed, hardware error 0x%" PRIx64 "\n",
-                code);
-        if (host_supports_vmx() && code == VMX_INVALID_GUEST_STATE) {
-            fprintf(stderr,
-                    "\nIf you're runnning a guest on an Intel machine without "
-                        "unrestricted mode\n"
-                    "support, the failure can be most likely due to the guest "
-                        "entering an invalid\n"
-                    "state for Intel VT. For example, the guest maybe running "
-                        "in big real mode\n"
-                    "which is not supported on less recent Intel processors."
-                        "\n\n");
-        }
-        ret = -1;
-        break;
-    case KVM_EXIT_EXCEPTION:
-        fprintf(stderr, "KVM: exception %d exit (error code 0x%x)\n",
-                run->ex.exception, run->ex.error_code);
-        ret = -1;
-        break;
-    default:
-        fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
-        ret = -1;
-        break;
-    }
-
-    return ret;
-}
-
 #ifdef KVM_CAP_SET_GUEST_DEBUG
 int kvm_arch_insert_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp)
 {
@@ -1860,6 +1806,60 @@ void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
 }
 #endif /* KVM_CAP_SET_GUEST_DEBUG */
 
+static bool host_supports_vmx(void)
+{
+    uint32_t ecx, unused;
+
+    host_cpuid(1, 0, &unused, &unused, &ecx, &unused);
+    return ecx & CPUID_EXT_VMX;
+}
+
+#define VMX_INVALID_GUEST_STATE 0x80000021
+
+int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run)
+{
+    uint64_t code;
+    int ret;
+
+    switch (run->exit_reason) {
+    case KVM_EXIT_HLT:
+        DPRINTF("handle_hlt\n");
+        ret = kvm_handle_halt(env);
+        break;
+    case KVM_EXIT_SET_TPR:
+        ret = 0;
+        break;
+    case KVM_EXIT_FAIL_ENTRY:
+        code = run->fail_entry.hardware_entry_failure_reason;
+        fprintf(stderr, "KVM: entry failed, hardware error 0x%" PRIx64 "\n",
+                code);
+        if (host_supports_vmx() && code == VMX_INVALID_GUEST_STATE) {
+            fprintf(stderr,
+                    "\nIf you're runnning a guest on an Intel machine without "
+                        "unrestricted mode\n"
+                    "support, the failure can be most likely due to the guest "
+                        "entering an invalid\n"
+                    "state for Intel VT. For example, the guest maybe running "
+                        "in big real mode\n"
+                    "which is not supported on less recent Intel processors."
+                        "\n\n");
+        }
+        ret = -1;
+        break;
+    case KVM_EXIT_EXCEPTION:
+        fprintf(stderr, "KVM: exception %d exit (error code 0x%x)\n",
+                run->ex.exception, run->ex.error_code);
+        ret = -1;
+        break;
+    default:
+        fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
+        ret = -1;
+        break;
+    }
+
+    return ret;
+}
+
 bool kvm_arch_stop_on_emulation_error(CPUState *env)
 {
     return !(env->cr[0] & CR0_PE_MASK) ||
Signature · 1 line
1.7.1
#7 lore raw

Re: [PATCH v2 06/20] kvm: Install optimized interrupt handler

In reply to Jan Kiszka
Quoted 15 lines · Jan Kiszka

KVM only requires to set the raised IRQ in CPUState and to kick the receiving vcpu if it is remote.

---
 kvm-all.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
kvm-all.c +0 -0
index 226843c..25ab545 100644
@@ -650,6 +650,15 @@ static CPUPhysMemoryClient kvm_cpu_phys_memory_client = {
     .log_stop = kvm_log_stop,
 };

kvm-all.c static CPUPhysMemoryClient kvm_cpu_phys_memory_client = { · line 661

Quoted 10 lines
kvm-all.c +9 -0
 
+static void kvm_handle_interrupt(CPUState *env, int mask)
+{
+    env->interrupt_request |= mask;
+
+    if (!qemu_cpu_is_self(env)) {
+        qemu_cpu_kick(env);
+    }
+}
+

Not sure its worthwhile to allow different handlers. The advantage over tcg version is that its shorter, without cpu_unlink_tb and icount handler?

#25 lore raw
In reply to Jan Kiszka
Quoted 14 lines · Jan Kiszka

This series catches "all the rest" to prepare QEMU's KVM support for merging with qemu-kvm. IOW, once these bits here are applied, qemu-kvm can switch its infrastructure to upstream and is effectively only adding own bits for in-kernel irqchip and device assignment support.

Topics of this series are:
- support for optimized interrupt handling by hooking cpu_interrupt
- another preparational step for in-kernel irqchip support
- x86: Do not leave halt if interrupts are disabled
- mark VCPU state dirty on creation (fixed deadlock on early hw_error)
- complete KVM support for PAT MSR, some related improvements for TCG
- further consolidation of inner kvm_cpu_exec loop
- expose VCPU host thread ID via "info cpus" and "query-cpus"

Quoted 10 lines

Changes in v2:
- Rebased over current uq/master
- Build fix for MAC OS (regression of previous round)
- Fix for x86 hardware breakpoints in TCG mode (regression of previous round)
- Build fix for s390 (regression of previous round)
- Removed premature optimization from "Install optimized interrupt handlers"
- Keep KVM_RUN return value in separate variable (cleanup)
- Reorder error handling of KVM_RUN (micro-optimization)

Applied all except the two cpu interrupt handler patches, thanks.

#8 lore raw

Re: [PATCH v2 06/20] kvm: Install optimized interrupt handler

In reply to Marcelo Tosatti
Quoted 17 lines · Marcelo Tosatti
Quoted 16 lines · Jan Kiszka

KVM only requires to set the raised IRQ in CPUState and to kick the receiving vcpu if it is remote.

---
 kvm-all.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
kvm-all.c +0 -0
index 226843c..25ab545 100644
@@ -650,6 +650,15 @@ static CPUPhysMemoryClient kvm_cpu_phys_memory_client = {
     .log_stop = kvm_log_stop,
 };
 
Quoted 13 lines

kvm-all.c static CPUPhysMemoryClient kvm_cpu_phys_memory_client = { · line 661

Quoted 9 lines
kvm-all.c +9 -0
+static void kvm_handle_interrupt(CPUState *env, int mask)
+{
+    env->interrupt_request |= mask;
+
+    if (!qemu_cpu_is_self(env)) {
+        qemu_cpu_kick(env);
+    }
+}
+

Not sure its worthwhile to allow different handlers. The advantage over tcg version is that its shorter, without cpu_unlink_tb and icount handler?

I thought about this again as well before posting, and I came to the conclusion that an important advantage is avoiding TCG surprises in KVM code paths. This way, KVM does not need to bother if cpu_unlink_tb or icount related code changes. Maybe I should have added this to the commit message.

Jan

#9 lore raw

Re: [PATCH v2 06/20] kvm: Install optimized interrupt handler

Quoted 32 lines · Jan Kiszka
Quoted 17 lines · Marcelo Tosatti
Quoted 16 lines · Jan Kiszka

KVM only requires to set the raised IRQ in CPUState and to kick the receiving vcpu if it is remote.

---
 kvm-all.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
kvm-all.c +0 -0
index 226843c..25ab545 100644
@@ -650,6 +650,15 @@ static CPUPhysMemoryClient kvm_cpu_phys_memory_client = {
     .log_stop = kvm_log_stop,
 };
 
Quoted 13 lines

kvm-all.c static CPUPhysMemoryClient kvm_cpu_phys_memory_client = { · line 661

Quoted 9 lines
kvm-all.c +9 -0
+static void kvm_handle_interrupt(CPUState *env, int mask)
+{
+    env->interrupt_request |= mask;
+
+    if (!qemu_cpu_is_self(env)) {
+        qemu_cpu_kick(env);
+    }
+}
+

Not sure its worthwhile to allow different handlers. The advantage over tcg version is that its shorter, without cpu_unlink_tb and icount handler?

Quoted 5 lines

I thought about this again as well before posting, and I came to the conclusion that an important advantage is avoiding TCG surprises in KVM code paths. This way, KVM does not need to bother if cpu_unlink_tb or icount related code changes. Maybe I should have added this to the commit message.

What's your opinion on this? Should I repost the remaining three with comments adjusted?

Jan

#10 lore raw

Re: [PATCH v2 06/20] kvm: Install optimized interrupt handler

Quoted 39 lines · Jan Kiszka
Quoted 32 lines · Jan Kiszka
Quoted 17 lines · Marcelo Tosatti
Quoted 16 lines · Jan Kiszka

KVM only requires to set the raised IRQ in CPUState and to kick the receiving vcpu if it is remote.

---
 kvm-all.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
kvm-all.c +0 -0
index 226843c..25ab545 100644
@@ -650,6 +650,15 @@ static CPUPhysMemoryClient kvm_cpu_phys_memory_client = {
     .log_stop = kvm_log_stop,
 };
 
Quoted 13 lines

kvm-all.c static CPUPhysMemoryClient kvm_cpu_phys_memory_client = { · line 661

Quoted 9 lines
kvm-all.c +9 -0
+static void kvm_handle_interrupt(CPUState *env, int mask)
+{
+    env->interrupt_request |= mask;
+
+    if (!qemu_cpu_is_self(env)) {
+        qemu_cpu_kick(env);
+    }
+}
+

Not sure its worthwhile to allow different handlers. The advantage over tcg version is that its shorter, without cpu_unlink_tb and icount handler?

Quoted 5 lines

I thought about this again as well before posting, and I came to the conclusion that an important advantage is avoiding TCG surprises in KVM code paths. This way, KVM does not need to bother if cpu_unlink_tb or icount related code changes. Maybe I should have added this to the commit message.

Quoted 5 lines

What's your opinion on this? Should I repost the remaining three with comments adjusted?

Jan

Its up to you. Your argument above makes sense.