|
| 1 | +/** |
| 2 | + * Copyright (C) 2019 Assured Information Security, Inc. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along |
| 15 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <asm/boxy.h> |
| 20 | +#include <linux/time64.h> |
| 21 | +#include <linux/clocksource.h> |
| 22 | +#include <linux/clockchips.h> |
| 23 | + |
| 24 | +static uint64_t g_tsc_offset = 0; |
| 25 | +static uint64_t g_tsc_freq_khz = 0; |
| 26 | + |
| 27 | +/******************************************************************************/ |
| 28 | +/* helpers */ |
| 29 | +/******************************************************************************/ |
| 30 | + |
| 31 | +static uint64_t mul_div(uint64_t x, uint64_t n, uint64_t d) |
| 32 | +{ return ((x / d) * n) + (((x % d) * n) / d); } |
| 33 | + |
| 34 | +static uint64_t tsc_to_nsec(uint64_t tsc) |
| 35 | +{ return mul_div(tsc, 1000000, g_tsc_freq_khz); } |
| 36 | + |
| 37 | +/******************************************************************************/ |
| 38 | +/* clock source */ |
| 39 | +/******************************************************************************/ |
| 40 | + |
| 41 | +static u64 boxy_clocksource_read(struct clocksource *cs) |
| 42 | +{ return rdtsc_ordered() - g_tsc_offset; } |
| 43 | + |
| 44 | +static struct clocksource boxy_clocksource = { |
| 45 | + .name = "boxy-clocksource", |
| 46 | + .read = boxy_clocksource_read, |
| 47 | + .rating = 500, |
| 48 | + .mask = CLOCKSOURCE_MASK(64), |
| 49 | + .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_VALID_FOR_HRES, |
| 50 | + .archdata.vclock_mode = VCLOCK_TSC |
| 51 | +}; |
| 52 | + |
| 53 | +/******************************************************************************/ |
| 54 | +/* clock event */ |
| 55 | +/******************************************************************************/ |
| 56 | + |
| 57 | +static int boxy_set_next_event( |
| 58 | + unsigned long delta, struct clock_event_device *evt) |
| 59 | +{ |
| 60 | + if (hypercall_vclock_op__set_next_event(delta) != SUCCESS) { |
| 61 | + pr_err("hypercall_vclock_op__set_next_event failed"); |
| 62 | + BUG(); |
| 63 | + } |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
| 67 | + |
| 68 | +static struct clock_event_device boxy_clock_event_device = { |
| 69 | + .name = "boxy-clock-event-device", |
| 70 | + .features = CLOCK_EVT_FEAT_ONESHOT, |
| 71 | + .mult = 1, |
| 72 | + .shift = 0, |
| 73 | + .rating = 500, |
| 74 | + .set_next_event = boxy_set_next_event, |
| 75 | +}; |
| 76 | + |
| 77 | +void boxy_vclock_event_handler(void) |
| 78 | +{ boxy_clock_event_device.event_handler(&boxy_clock_event_device); } |
| 79 | + |
| 80 | +static void __init boxy_setup_percpu_clockev(void) |
| 81 | +{ |
| 82 | + boxy_clock_event_device.cpumask = cpumask_of(smp_processor_id()); |
| 83 | + |
| 84 | + clockevents_config_and_register( |
| 85 | + &boxy_clock_event_device, g_tsc_freq_khz * 1000, 0, ~0UL); |
| 86 | +} |
| 87 | + |
| 88 | +/******************************************************************************/ |
| 89 | +/* pv_ops */ |
| 90 | +/******************************************************************************/ |
| 91 | + |
| 92 | +static u64 boxy_sched_clock(void) |
| 93 | +{ return tsc_to_nsec(boxy_clocksource_read(0)); } |
| 94 | + |
| 95 | +static u64 boxy_steal_clock(int cpu) |
| 96 | +{ |
| 97 | + /** |
| 98 | + * Note: |
| 99 | + * |
| 100 | + * For now we do not support the steal clock. Timekeeping seems to work |
| 101 | + * fine without it, and implementing this would require not only an |
| 102 | + * additional VMCall on every sched_clock() call, but it would also |
| 103 | + * require the hypervisor to perform time keeping on every exit and |
| 104 | + * entry to account for the time that the VM is actually executing. |
| 105 | + */ |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +/******************************************************************************/ |
| 111 | +/* x86_platform_ops */ |
| 112 | +/******************************************************************************/ |
| 113 | + |
| 114 | +static unsigned long tsc_freq_khz(void) |
| 115 | +{ return g_tsc_freq_khz; } |
| 116 | + |
| 117 | +/******************************************************************************/ |
| 118 | +/* init functions */ |
| 119 | +/******************************************************************************/ |
| 120 | + |
| 121 | +static void wallclock_init(void) |
| 122 | +{ |
| 123 | + if (hypercall_vclock_op__reset_host_wallclock() != SUCCESS) { |
| 124 | + pr_err("hypercall_vclock_op__reset_host_wallclock failed"); |
| 125 | + BUG(); |
| 126 | + } |
| 127 | + |
| 128 | + if (hypercall_vclock_op__set_guest_wallclock_rtc() != SUCCESS) { |
| 129 | + pr_err("hypercall_vclock_op__set_guest_wallclock_rtc failed"); |
| 130 | + BUG(); |
| 131 | + } |
| 132 | + |
| 133 | + if (hypercall_vclock_op__set_guest_wallclock_tsc() != SUCCESS) { |
| 134 | + pr_err("hypercall_vclock_op__set_guest_wallclock_tsc failed"); |
| 135 | + BUG(); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +void __init read_persistent_wall_and_boot_offset( |
| 140 | + struct timespec64 *wall_time, struct timespec64 *boot_offset) |
| 141 | +{ |
| 142 | + uint64_t ret, tsc; |
| 143 | + struct timespec64 wallclock; |
| 144 | + |
| 145 | + ret = hypercall_vclock_op__get_guest_wallclock( |
| 146 | + &wallclock.tv_sec, &wallclock.tv_nsec, &tsc); |
| 147 | + if (ret != SUCCESS) { |
| 148 | + pr_err("hypercall_vclock_op__get_wallclock failed"); |
| 149 | + BUG(); |
| 150 | + } |
| 151 | + |
| 152 | + *wall_time = wallclock; |
| 153 | + *boot_offset = ns_to_timespec64(tsc_to_nsec(tsc - g_tsc_offset)); |
| 154 | +} |
| 155 | + |
| 156 | +void __init boxy_vclock_init(void) |
| 157 | +{ |
| 158 | + g_tsc_freq_khz = hypercall_vclock_op__get_tsc_freq_khz(); |
| 159 | + if (g_tsc_freq_khz == FAILURE) { |
| 160 | + pr_err("hypercall_vclock_op__get_tsc_freq_khz failed"); |
| 161 | + BUG(); |
| 162 | + } |
| 163 | + |
| 164 | + pv_ops.time.sched_clock = boxy_sched_clock; |
| 165 | + pv_ops.time.steal_clock = boxy_steal_clock; |
| 166 | + |
| 167 | + x86_init.timers.setup_percpu_clockev = boxy_setup_percpu_clockev; |
| 168 | + x86_init.timers.timer_init = x86_init_noop; |
| 169 | + x86_init.timers.wallclock_init = wallclock_init; |
| 170 | + |
| 171 | + x86_platform.calibrate_tsc = tsc_freq_khz; |
| 172 | + x86_platform.calibrate_cpu = tsc_freq_khz; |
| 173 | + |
| 174 | + g_tsc_offset = rdtsc_ordered(); |
| 175 | + clocksource_register_khz(&boxy_clocksource, g_tsc_freq_khz); |
| 176 | + |
| 177 | + setup_force_cpu_cap(X86_FEATURE_NONSTOP_TSC); |
| 178 | + setup_force_cpu_cap(X86_FEATURE_CONSTANT_TSC); |
| 179 | + setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE); |
| 180 | + setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ); |
| 181 | +} |
0 commit comments