|
| 1 | +/* |
| 2 | + * Copyright (c) 2015-2017 Contributors as noted in the AUTHORS file |
| 3 | + * |
| 4 | + * This file is part of Solo5, a unikernel base layer. |
| 5 | + * |
| 6 | + * Permission to use, copy, modify, and/or distribute this software |
| 7 | + * for any purpose with or without fee is hereby granted, provided |
| 8 | + * that the above copyright notice and this permission notice appear |
| 9 | + * in all copies. |
| 10 | + * |
| 11 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
| 12 | + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
| 13 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE |
| 14 | + * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR |
| 15 | + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
| 16 | + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 17 | + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 18 | + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | + */ |
| 20 | + |
| 21 | +#include "kernel.h" |
| 22 | + |
| 23 | +/* Wall clock offset at monotonic time base. */ |
| 24 | +static uint64_t wc_epochoffset; |
| 25 | + |
| 26 | +/* Base time values at the last call to tscclock_monotonic(). */ |
| 27 | +static uint64_t time_base; |
| 28 | +static uint64_t tsc_base; |
| 29 | + |
| 30 | +/* Multiplier for converting TSC ticks to nsecs. (0.32) fixed point. */ |
| 31 | +static uint32_t tsc_mult; |
| 32 | + |
| 33 | +/* |
| 34 | + * Beturn monotonic time using TSC clock. |
| 35 | + */ |
| 36 | +uint64_t tscclock_monotonic(void) |
| 37 | +{ |
| 38 | + uint64_t tsc_now, tsc_delta; |
| 39 | + |
| 40 | + /* |
| 41 | + * Update time_base (monotonic time) and tsc_base (TSC time). |
| 42 | + */ |
| 43 | + tsc_now = cpu_rdtsc(); |
| 44 | + tsc_delta = tsc_now - tsc_base; |
| 45 | + time_base += mul64_32(tsc_delta, tsc_mult); |
| 46 | + tsc_base = tsc_now; |
| 47 | + |
| 48 | + return time_base; |
| 49 | +} |
| 50 | + |
| 51 | +/* |
| 52 | + * Initialise TSC clock. |
| 53 | + * |
| 54 | + * Implementation notes: This is a purely TSC-based clock with the following |
| 55 | + * requirements: |
| 56 | + * |
| 57 | + * 1. The host TSC MUST be invariant, as defined in Intel SDM section 17.15.1 |
| 58 | + * "Invariant TSC". |
| 59 | + * 2. The host hypervisor MUST NOT implement any RDTSC emulation. |
| 60 | + * |
| 61 | + * It is up to the monitor to ensure that these requirements are met, and to |
| 62 | + * supply the TSC frequency to the guest. |
| 63 | + */ |
| 64 | +int tscclock_init(uint64_t tsc_freq) |
| 65 | +{ |
| 66 | + /* |
| 67 | + * Calculate TSC scaling multiplier. |
| 68 | + * |
| 69 | + * (0.32) tsc_mult = NSEC_PER_SEC (32.32) / tsc_freq (32.0) |
| 70 | + */ |
| 71 | + tsc_mult = (NSEC_PER_SEC << 32) / tsc_freq; |
| 72 | + |
| 73 | + /* |
| 74 | + * Monotonic time begins at tsc_base (first read of TSC before |
| 75 | + * calibration). |
| 76 | + */ |
| 77 | + tsc_base = cpu_rdtsc(); |
| 78 | + time_base = mul64_32(tsc_base, tsc_mult); |
| 79 | + |
| 80 | + /* |
| 81 | + * Compute wall clock epoch offset by subtracting monotonic time_base from |
| 82 | + * wall time at boot. |
| 83 | + * |
| 84 | + * TODO: This arrangement minimises the use of hypercalls, but is subject |
| 85 | + * to clock skew over time and cannot get corrections from the host (via |
| 86 | + * e.g. NTP). |
| 87 | + */ |
| 88 | + struct ukvm_walltime t; |
| 89 | + ukvm_do_hypercall(UKVM_HYPERCALL_WALLTIME, &t); |
| 90 | + wc_epochoffset = t.nsecs - time_base; |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +/* |
| 96 | + * Return epoch offset (wall time offset to monotonic clock start). |
| 97 | + */ |
| 98 | +uint64_t tscclock_epochoffset(void) |
| 99 | +{ |
| 100 | + return wc_epochoffset; |
| 101 | +} |
0 commit comments