[PATCH RFC 2/3] x86/xen/time: setup vcpu 0 time info page

From: Joao Martins
Date: Mon Dec 28 2015 - 16:53:28 EST


In order to support pvclock vdso on xen we need to setup the
time info page for vcpu 0 and register the page with Xen using
the VCPUOP_register_vcpu_time_memory_area hypercall. This
hypercall will also forcefully update the pvti which will set
some of the necessary flags for vdso. Afterwards we check if it
supports the PVCLOCK_TSC_STABLE_BIT flag which is mandatory for
having vdso/vsyscall support. And if so, it will set the cpu 0
pvti that will be later on used when mapping the vdso image.

The xen headers are also updated to include the new hypercall for
registering the secondary vcpu_time_info copy.

Signed-off-by: Joao Martins <joao.m.martins@xxxxxxxxxx>
---
arch/x86/xen/time.c | 66 ++++++++++++++++++++++++++++++++++++++++++++
include/xen/interface/vcpu.h | 28 +++++++++++++++++++
2 files changed, 94 insertions(+)

diff --git a/arch/x86/xen/time.c b/arch/x86/xen/time.c
index a0a4e55..c17b1b2 100644
--- a/arch/x86/xen/time.c
+++ b/arch/x86/xen/time.c
@@ -17,6 +17,7 @@
#include <linux/slab.h>
#include <linux/pvclock_gtod.h>
#include <linux/timekeeper_internal.h>
+#include <linux/memblock.h>

#include <asm/pvclock.h>
#include <asm/xen/hypervisor.h>
@@ -403,6 +404,69 @@ static const struct pv_time_ops xen_time_ops __initconst = {
.sched_clock = xen_clocksource_read,
};

+#ifdef CONFIG_XEN_TIME_VSYSCALL
+static struct pvclock_vsyscall_time_info *xen_clock __read_mostly;
+
+static int xen_setup_vsyscall_time_info(int cpu)
+{
+ struct pvclock_vsyscall_time_info *ti;
+ struct vcpu_register_time_memory_area t;
+ struct pvclock_vcpu_time_info *pvti;
+ unsigned long mem;
+ int ret, size;
+ u8 flags;
+
+ ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area,
+ cpu, NULL);
+ if (ret == -ENOSYS) {
+ pr_debug("xen: vcpu_time_info placement not supported\n");
+ return -ENOTSUPP;
+ }
+
+ size = PAGE_ALIGN(sizeof(struct pvclock_vsyscall_time_info));
+ mem = memblock_alloc(size, PAGE_SIZE);
+ if (!mem)
+ return -ENOMEM;
+
+ ti = __va(mem);
+ memset(ti, 0, size);
+
+ t.addr.v = &ti[cpu].pvti;
+
+ ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area,
+ cpu, &t);
+
+ if (ret) {
+ pr_debug("xen: cannot register vcpu_time_info err %d\n", ret);
+ memblock_free(mem, size);
+ return ret;
+ }
+
+ pvti = &ti[cpu].pvti;
+ flags = pvti->flags;
+
+ if (!(flags & PVCLOCK_TSC_STABLE_BIT)) {
+ pr_debug("xen: VCLOCK_PVCLOCK not supported\n");
+ memblock_free(mem, size);
+ return -ENOTSUPP;
+ }
+
+ xen_clock = ti;
+ pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
+ pvclock_set_pvti_cpu0_va(xen_clock);
+
+ xen_clocksource.archdata.vclock_mode = VCLOCK_PVCLOCK;
+
+ return 0;
+}
+#else
+static int xen_setup_vsyscall_time_info(int cpu)
+{
+ return -1;
+}
+
+#endif /* CONFIG_XEN_TIME_VSYSCALL */
+
static void __init xen_time_init(void)
{
int cpu = smp_processor_id();
@@ -431,6 +495,8 @@ static void __init xen_time_init(void)
xen_setup_timer(cpu);
xen_setup_cpu_clockevents();

+ xen_setup_vsyscall_time_info(cpu);
+
if (xen_initial_domain())
pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
}
diff --git a/include/xen/interface/vcpu.h b/include/xen/interface/vcpu.h
index b05288c..902b59e 100644
--- a/include/xen/interface/vcpu.h
+++ b/include/xen/interface/vcpu.h
@@ -172,4 +172,32 @@ DEFINE_GUEST_HANDLE_STRUCT(vcpu_register_vcpu_info);

/* Send an NMI to the specified VCPU. @extra_arg == NULL. */
#define VCPUOP_send_nmi 11
+
+/*
+ * Register a memory location to get a secondary copy of the vcpu time
+ * parameters. The master copy still exists as part of the vcpu shared
+ * memory area, and this secondary copy is updated whenever the master copy
+ * is updated (and using the same versioning scheme for synchronisation).
+ *
+ * The intent is that this copy may be mapped (RO) into userspace so
+ * that usermode can compute system time using the time info and the
+ * tsc. Usermode will see an array of vcpu_time_info structures, one
+ * for each vcpu, and choose the right one by an existing mechanism
+ * which allows it to get the current vcpu number (such as via a
+ * segment limit). It can then apply the normal algorithm to compute
+ * system time from the tsc.
+ *
+ * @extra_arg == pointer to vcpu_register_time_info_memory_area structure.
+ */
+#define VCPUOP_register_vcpu_time_memory_area 13
+DEFINE_GUEST_HANDLE_STRUCT(vcpu_time_info_t);
+struct vcpu_register_time_memory_area {
+ union {
+ GUEST_HANDLE(vcpu_time_info_t) h;
+ struct pvclock_vcpu_time_info *v;
+ uint64_t p;
+ } addr;
+};
+DEFINE_GUEST_HANDLE_STRUCT(vcpu_register_time_memory_area_t);
+
#endif /* __XEN_PUBLIC_VCPU_H__ */
--
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/