| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: xen_clock.c,v 1.14 2023/07/28 10:39:01 riastradh Exp $ */ | | 1 | /* $NetBSD: xen_clock.c,v 1.15 2023/07/28 10:39:14 riastradh Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2017, 2018 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2017, 2018 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Taylor R. Campbell. | | 8 | * by Taylor R. Campbell. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -26,27 +26,27 @@ | | | @@ -26,27 +26,27 @@ |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include "opt_xen.h" | | 32 | #include "opt_xen.h" |
33 | | | 33 | |
34 | #ifndef XEN_CLOCK_DEBUG | | 34 | #ifndef XEN_CLOCK_DEBUG |
35 | #define XEN_CLOCK_DEBUG 0 | | 35 | #define XEN_CLOCK_DEBUG 0 |
36 | #endif | | 36 | #endif |
37 | | | 37 | |
38 | #include <sys/cdefs.h> | | 38 | #include <sys/cdefs.h> |
39 | __KERNEL_RCSID(0, "$NetBSD: xen_clock.c,v 1.14 2023/07/28 10:39:01 riastradh Exp $"); | | 39 | __KERNEL_RCSID(0, "$NetBSD: xen_clock.c,v 1.15 2023/07/28 10:39:14 riastradh Exp $"); |
40 | | | 40 | |
41 | #include <sys/param.h> | | 41 | #include <sys/param.h> |
42 | #include <sys/types.h> | | 42 | #include <sys/types.h> |
43 | #include <sys/atomic.h> | | 43 | #include <sys/atomic.h> |
44 | #include <sys/callout.h> | | 44 | #include <sys/callout.h> |
45 | #include <sys/cpu.h> | | 45 | #include <sys/cpu.h> |
46 | #include <sys/device.h> | | 46 | #include <sys/device.h> |
47 | #include <sys/evcnt.h> | | 47 | #include <sys/evcnt.h> |
48 | #include <sys/intr.h> | | 48 | #include <sys/intr.h> |
49 | #include <sys/kernel.h> | | 49 | #include <sys/kernel.h> |
50 | #include <sys/lwp.h> | | 50 | #include <sys/lwp.h> |
51 | #include <sys/proc.h> | | 51 | #include <sys/proc.h> |
52 | #include <sys/sdt.h> | | 52 | #include <sys/sdt.h> |
| @@ -110,26 +110,33 @@ SDT_PROBE_DEFINE7(sdt, xen, clock, systi | | | @@ -110,26 +110,33 @@ SDT_PROBE_DEFINE7(sdt, xen, clock, systi |
110 | "int"/*tsc_shift*/, | | 110 | "int"/*tsc_shift*/, |
111 | "uint64_t"/*delta_ns*/, | | 111 | "uint64_t"/*delta_ns*/, |
112 | "uint64_t"/*tsc*/, | | 112 | "uint64_t"/*tsc*/, |
113 | "uint64_t"/*systime_ns*/); | | 113 | "uint64_t"/*systime_ns*/); |
114 | | | 114 | |
115 | SDT_PROBE_DEFINE3(sdt, xen, timecounter, backward, | | 115 | SDT_PROBE_DEFINE3(sdt, xen, timecounter, backward, |
116 | "uint64_t"/*local*/, | | 116 | "uint64_t"/*local*/, |
117 | "uint64_t"/*skew*/, | | 117 | "uint64_t"/*skew*/, |
118 | "uint64_t"/*global*/); | | 118 | "uint64_t"/*global*/); |
119 | | | 119 | |
120 | SDT_PROBE_DEFINE2(sdt, xen, hardclock, systime__backward, | | 120 | SDT_PROBE_DEFINE2(sdt, xen, hardclock, systime__backward, |
121 | "uint64_t"/*last_systime_ns*/, | | 121 | "uint64_t"/*last_systime_ns*/, |
122 | "uint64_t"/*this_systime_ns*/); | | 122 | "uint64_t"/*this_systime_ns*/); |
| | | 123 | SDT_PROBE_DEFINE2(sdt, xen, hardclock, tick, |
| | | 124 | "uint64_t"/*last_systime_ns*/, |
| | | 125 | "uint64_t"/*this_systime_ns*/); |
| | | 126 | SDT_PROBE_DEFINE3(sdt, xen, hardclock, jump, |
| | | 127 | "uint64_t"/*last_systime_ns*/, |
| | | 128 | "uint64_t"/*this_systime_ns*/, |
| | | 129 | "uint64_t"/*nticks*/); |
123 | SDT_PROBE_DEFINE3(sdt, xen, hardclock, missed, | | 130 | SDT_PROBE_DEFINE3(sdt, xen, hardclock, missed, |
124 | "uint64_t"/*last_systime_ns*/, | | 131 | "uint64_t"/*last_systime_ns*/, |
125 | "uint64_t"/*this_systime_ns*/, | | 132 | "uint64_t"/*this_systime_ns*/, |
126 | "uint64_t"/*remaining_ns*/); | | 133 | "uint64_t"/*remaining_ns*/); |
127 | | | 134 | |
128 | /* | | 135 | /* |
129 | * xen timecounter: | | 136 | * xen timecounter: |
130 | * | | 137 | * |
131 | * Xen vCPU system time, plus an adjustment with rdtsc. | | 138 | * Xen vCPU system time, plus an adjustment with rdtsc. |
132 | */ | | 139 | */ |
133 | static struct timecounter xen_timecounter = { | | 140 | static struct timecounter xen_timecounter = { |
134 | .tc_get_timecount = xen_get_timecount, | | 141 | .tc_get_timecount = xen_get_timecount, |
135 | .tc_poll_pps = NULL, | | 142 | .tc_poll_pps = NULL, |
| @@ -790,44 +797,49 @@ xen_timer_handler(void *cookie, struct c | | | @@ -790,44 +797,49 @@ xen_timer_handler(void *cookie, struct c |
790 | KASSERT(cpu_intr_p()); | | 797 | KASSERT(cpu_intr_p()); |
791 | KASSERT(cookie == ci); | | 798 | KASSERT(cookie == ci); |
792 | | | 799 | |
793 | #if defined(XENPV) | | 800 | #if defined(XENPV) |
794 | frame = NULL; /* We use values cached in curcpu() */ | | 801 | frame = NULL; /* We use values cached in curcpu() */ |
795 | #endif | | 802 | #endif |
796 | again: | | 803 | again: |
797 | /* | | 804 | /* |
798 | * Find how many nanoseconds of Xen system time has elapsed | | 805 | * Find how many nanoseconds of Xen system time has elapsed |
799 | * since the last hardclock tick. | | 806 | * since the last hardclock tick. |
800 | */ | | 807 | */ |
801 | last = ci->ci_xen_hardclock_systime_ns; | | 808 | last = ci->ci_xen_hardclock_systime_ns; |
802 | now = xen_vcputime_systime_ns(); | | 809 | now = xen_vcputime_systime_ns(); |
803 | if (now < last) { | | 810 | SDT_PROBE2(sdt, xen, hardclock, tick, last, now); |
| | | 811 | if (__predict_false(now < last)) { |
804 | SDT_PROBE2(sdt, xen, hardclock, systime__backward, | | 812 | SDT_PROBE2(sdt, xen, hardclock, systime__backward, |
805 | last, now); | | 813 | last, now); |
806 | #if XEN_CLOCK_DEBUG | | 814 | #if XEN_CLOCK_DEBUG |
807 | device_printf(ci->ci_dev, "xen systime ran backwards" | | 815 | device_printf(ci->ci_dev, "xen systime ran backwards" |
808 | " in hardclock %"PRIu64"ns\n", | | 816 | " in hardclock %"PRIu64"ns\n", |
809 | last - now); | | 817 | last - now); |
810 | #endif | | 818 | #endif |
811 | ci->ci_xen_systime_backwards_hardclock_evcnt.ev_count++; | | 819 | ci->ci_xen_systime_backwards_hardclock_evcnt.ev_count++; |
812 | now = last; | | 820 | now = last; |
813 | } | | 821 | } |
814 | delta = now - last; | | 822 | delta = now - last; |
815 | | | 823 | |
816 | /* | | 824 | /* |
817 | * Play hardclock catchup: run the hardclock timer as many | | 825 | * Play hardclock catchup: run the hardclock timer as many |
818 | * times as appears necessary based on how much time has | | 826 | * times as appears necessary based on how much time has |
819 | * passed. | | 827 | * passed. |
820 | */ | | 828 | */ |
| | | 829 | if (__predict_false(delta >= 2*ns_per_tick)) { |
| | | 830 | SDT_PROBE3(sdt, xen, hardclock, jump, |
| | | 831 | last, now, delta/ns_per_tick); |
| | | 832 | } |
821 | while (delta >= ns_per_tick) { | | 833 | while (delta >= ns_per_tick) { |
822 | ci->ci_xen_hardclock_systime_ns += ns_per_tick; | | 834 | ci->ci_xen_hardclock_systime_ns += ns_per_tick; |
823 | delta -= ns_per_tick; | | 835 | delta -= ns_per_tick; |
824 | hardclock(frame); | | 836 | hardclock(frame); |
825 | if (__predict_false(delta >= ns_per_tick)) { | | 837 | if (__predict_false(delta >= ns_per_tick)) { |
826 | SDT_PROBE3(sdt, xen, hardclock, missed, | | 838 | SDT_PROBE3(sdt, xen, hardclock, missed, |
827 | last, now, delta); | | 839 | last, now, delta); |
828 | ci->ci_xen_missed_hardclock_evcnt.ev_count++; | | 840 | ci->ci_xen_missed_hardclock_evcnt.ev_count++; |
829 | } | | 841 | } |
830 | } | | 842 | } |
831 | | | 843 | |
832 | /* | | 844 | /* |
833 | * Re-arm the timer. If it fails, it's probably because the | | 845 | * Re-arm the timer. If it fails, it's probably because the |