Module Name:    src
Committed By:   jmcneill
Date:           Thu Nov 23 19:53:21 UTC 2017

Modified Files:
        src/sys/arch/x86/x86: lapic.c

Log Message:
Add a workaround for local APIC timers running under KVM. It seems these
timers don't reload the current-count register in periodic mode when it
reaches 0, so we need to detect this condition and reload it ourselves.

XXX pullup


To generate a diff of this commit:
cvs rdiff -u -r1.63 -r1.64 src/sys/arch/x86/x86/lapic.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/lapic.c
diff -u src/sys/arch/x86/x86/lapic.c:1.63 src/sys/arch/x86/x86/lapic.c:1.64
--- src/sys/arch/x86/x86/lapic.c:1.63	Sat Nov  4 12:53:00 2017
+++ src/sys/arch/x86/x86/lapic.c	Thu Nov 23 19:53:20 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: lapic.c,v 1.63 2017/11/04 12:53:00 maxv Exp $	*/
+/*	$NetBSD: lapic.c,v 1.64 2017/11/23 19:53:20 jmcneill Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2008 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.63 2017/11/04 12:53:00 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.64 2017/11/23 19:53:20 jmcneill Exp $");
 
 #include "acpica.h"
 #include "ioapic.h"
@@ -124,6 +124,8 @@ bool x2apic_enable = true;
 bool x2apic_enable = false;
 #endif
 
+static bool lapic_broken_periodic __read_mostly;
+
 static uint32_t
 i82489_readreg(u_int reg)
 {
@@ -678,6 +680,16 @@ lapic_calibrate_timer(struct cpu_info *c
 			    32;
 
 		/*
+		 * Apply workaround for broken periodic timer under KVM
+		 */
+		if (vm_guest == VM_GUEST_KVM) {
+			lapic_broken_periodic = true;
+			lapic_timecounter.tc_quality = -100;
+			aprint_debug_dev(ci->ci_dev,
+			    "applying KVM timer workaround\n");
+		}
+
+		/*
 		 * Now that the timer's calibrated, use the apic timer routines
 		 * for all our timing needs..
 		 */
@@ -717,6 +729,12 @@ lapic_delay(unsigned int usec)
 
 	while (deltat > 0) {
 		xtick = lapic_gettick();
+		if (lapic_broken_periodic && xtick == 0 && otick == 0) {
+			lapic_initclocks();
+			xtick = lapic_gettick();
+			if (xtick == 0)
+				panic("lapic timer stopped ticking");
+		}
 		if (xtick > otick)
 			deltat -= lapic_tval - (xtick - otick);
 		else

Reply via email to