Skip to content

Commit

Permalink
Update to 2021-07-09 version
Browse files Browse the repository at this point in the history
  • Loading branch information
allanjude committed Jul 12, 2021
1 parent 0c282ad commit 23db61a
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 167 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
KMOD= kvmclock

SRCS= kvm_clock.c pvclock.c
SRCS= kvm_clock.c pvclock.c rdtsc_ordered.c
SRCS+= bus_if.h clock_if.h device_if.h

CFLAGS+= -Iinclude
CFLAGS+= -DINVARIANTS -DINVARIANT_SUPPORT

.include <bsd.kmod.mk>
120 changes: 120 additions & 0 deletions include/x86/_rdtsc_ordered.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2021 Juniper Networks, Inc.
* Copyright (c) 2021 Klara, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/

#ifndef _X86__RDTSC_ORDERED_H_
#define _X86__RDTSC_ORDERED_H_

#include <sys/types.h>
#include <machine/cpufunc.h>
#include <machine/cputypes.h>
#include <machine/specialreg.h>
#include <x86/ifunc.h>
#include <x86/rdtsc_ordered.h>

#ifdef _KERNEL
#include <sys/pcpu.h>
#include <machine/md_var.h>
#else /* !_KERNEL */
#include <stdbool.h>
#include <string.h>
#endif /* _KERNEL */

#define DEFINE_RDTSC_ORDERED_COMMON() \
static uint64_t \
rdtsc_ordered_lfence(void) \
{ \
lfence(); \
return (rdtsc()); \
} \
static uint64_t \
rdtsc_ordered_mfence(void) \
{ \
mfence(); \
return (rdtsc()); \
} \
static inline uint64_t (*rdtsc_ordered_select(u_int amd_feature, \
u_int cpu_feature, bool cpu_is_amd))(void) \
{ \
if ((amd_feature & AMDID_RDTSCP) != 0) \
return (rdtscp); \
else if ((cpu_feature & CPUID_SSE2) != 0) \
if (cpu_is_amd) \
return (rdtsc_ordered_mfence); \
else \
return (rdtsc_ordered_lfence); \
else \
return (rdtsc); \
}

#ifdef _KERNEL
#define DEFINE_RDTSC_ORDERED() \
DEFINE_RDTSC_ORDERED_COMMON() \
DEFINE_IFUNC(, uint64_t, rdtsc_ordered, (void)) \
{ \
bool cpu_is_amd = cpu_vendor_id == CPU_VENDOR_AMD || \
cpu_vendor_id == CPU_VENDOR_HYGON; \
\
return (rdtsc_ordered_select(amd_feature, cpu_feature, \
cpu_is_amd)); \
}
#else /* !_KERNEL */
#define DEFINE_RDTSC_ORDERED() \
DEFINE_RDTSC_ORDERED_COMMON() \
DEFINE_UIFUNC(, uint64_t, rdtsc_ordered, (void)) \
{ \
u_int amd_feature, cpu_exthigh, p[4], v[3]; \
static const char amd_id[] = "AuthenticAMD"; \
static const char hygon_id[] = "HygonGenuine"; \
bool cpu_is_amd; \
\
do_cpuid(0, p); \
v[0] = p[1]; \
v[1] = p[3]; \
v[2] = p[2]; \
cpu_is_amd = memcmp(v, amd_id, sizeof(amd_id) - 1) == 0 || \
memcmp(v, hygon_id, sizeof(hygon_id) - 1) == 0; \
if (cpu_feature != 0) { \
do_cpuid(0x80000000, p); \
cpu_exthigh = p[0]; \
} else { \
cpu_exthigh = 0; \
} \
if (cpu_exthigh >= 0x80000001) { \
do_cpuid(0x80000001, p); \
amd_feature = p[3]; \
} else { \
amd_feature = 0; \
} \
return (rdtsc_ordered_select(amd_feature, cpu_feature, \
cpu_is_amd)); \
}
#endif /* _KERNEL */

#endif /* !_X86__RDTSC_ORDERED_H_ */
29 changes: 13 additions & 16 deletions include/x86/pvclock.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#ifndef X86_PVCLOCK
#define X86_PVCLOCK

#include <sys/types.h>
#include <machine/atomic.h>
#include <x86/rdtsc_ordered.h>

#ifdef _KERNEL
#include <sys/timetc.h>
#endif /* _KERNEL */
Expand Down Expand Up @@ -62,7 +66,6 @@ pvclock_scale_delta(uint64_t delta, uint32_t mul_frac, int shift)
delta >>= -shift;
else
delta <<= shift;

#if defined(__i386__)
{
uint32_t tmp1, tmp2;
Expand Down Expand Up @@ -98,7 +101,6 @@ pvclock_scale_delta(uint64_t delta, uint32_t mul_frac, int shift)
#else
#error "pvclock: unsupported x86 architecture?"
#endif

return (product);
}

Expand All @@ -107,8 +109,7 @@ pvclock_get_nsec_offset(struct pvclock_vcpu_time_info *ti)
{
uint64_t delta;

delta = rdtsc() - ti->tsc_timestamp;

delta = rdtsc_ordered() - ti->tsc_timestamp;
return (pvclock_scale_delta(delta, ti->tsc_to_system_mul,
ti->tsc_shift));
}
Expand All @@ -120,11 +121,10 @@ pvclock_read_time_info(struct pvclock_vcpu_time_info *ti,
uint32_t version;

do {
version = ti->version;
rmb();
version = atomic_load_acq_32(&ti->version);
*ns = ti->system_time + pvclock_get_nsec_offset(ti);
*flags = ti->flags;
rmb();
atomic_thread_fence_acq();
} while ((ti->version & 1) != 0 || ti->version != version);
}

Expand All @@ -140,14 +140,17 @@ struct pvclock_wall_clock {
};

struct pvclock {
struct timecounter tc;
struct cdev *cdev;
/* Public; initialized by the caller of 'pvclock_init()': */
pvclock_get_curcpu_timeinfo_t *get_curcpu_ti;
void *get_curcpu_ti_arg;
pvclock_get_wallclock_t *get_wallclock;
void *get_wallclock_arg;
struct pvclock_vcpu_time_info *ti_vcpu0_page;
bool stable_flag_supported;

/* Private; initialized by the 'pvclock' API: */
struct timecounter tc;
struct cdev *cdev;
};

void pvclock_resume(void);
Expand All @@ -158,13 +161,7 @@ void pvclock_get_wallclock(struct pvclock_wall_clock *wc,
struct timespec *ts);

void pvclock_init(struct pvclock *pvc, device_t dev,
const char *tc_name, int tc_quality, u_int tc_flags,
pvclock_get_curcpu_timeinfo_t *get_curcpu_ti,
void *get_curcpu_ti_arg,
pvclock_get_wallclock_t *get_wallclock,
void *get_wallclock_arg,
struct pvclock_vcpu_time_info *ti_vcpu0_page,
bool stable_flag_supported);
const char *tc_name, int tc_quality, u_int tc_flags);
void pvclock_gettime(struct pvclock *pvc, struct timespec *ts);
int pvclock_destroy(struct pvclock *pvc);

Expand Down
38 changes: 38 additions & 0 deletions include/x86/rdtsc_ordered.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2021 Juniper Networks, Inc.
* Copyright (c) 2021 Klara, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/

#ifndef _X86_RDTSC_ORDERED_H_
#define _X86_RDTSC_ORDERED_H_

#include <sys/types.h>

uint64_t rdtsc_ordered(void);

#endif /* !_X86_RDTSC_ORDERED_H_ */
Loading

0 comments on commit 23db61a

Please sign in to comment.