Allow guests to register secondary vcpu_time_info
PATCH v2 1/1 Allow guests to register secondary vcpu_time_info
Allow a guest to register a second location for the VCPU time info structure for each vcpu. This is intended to allow the guest kernel to map this information into a usermode accessible page, so that usermode can efficiently calculate system time from the TSC without having to make a syscall.
The second vcpu_time_info structure is updated by copy, rather than being a shared page between the guest and Xen. It is not entirely updated by copy; instead, Xen preserves and increments the existing version number in place. This allows the guest to also update the version number (useful to indicate vcpu context switches to usermode). This assumes that the guest will only ever update the structure for a given vcpu on that vcpu (as Xen does, so there are never any cross-cpu accesses).
xen/arch/x86/domain.c +18 -0
@@ -964,6 +964,24 @@break ; } +case VCPUOP_register_vcpu_time_memory_area: + { +struct vcpu_register_time_memory_area area; + rc = -EFAULT ; +if (copy_from_guest (&area, arg,1 ) ) +break ; + +if ( !guest_handle_okay (area.addr.h,1 ) ) +break ; + + rc =0 ; + v->time_info_guest = area.addr.h; + +__update_vcpu_system_time (v,1 ); + +break ; + } +case VCPUOP_get_physid: {struct vcpu_get_physid cpu_id;
xen/arch/x86/time.c +34 -3
@@ -22,6 +22,7 @@#include <xen/irq.h> #include <xen/softirq.h> #include <xen/keyhandler.h> +#include <xen/guest_access.h> #include <asm/io.h> #include <asm/msr.h> #include <asm/mpspec.h>
@@ -826,7 +827,30 @@ (*version)++; } -void update_vcpu_system_time (struct vcpu *v) +static void update_guest_time_info (struct vcpu *v,struct vcpu_time_info *u) +{ +struct vcpu_time_info info; + +if (guest_handle_is_null (v->time_info_guest) ) +return ; + + info = *u; + +/* +* Update the guest copy of the time info. We need to make sure +* we update the guest's version of the version number rather than +* use a verbtim copy of the master one, because the guest may +* update the version for its own purposes. +*/ +if (__copy_field_from_guest (&info, v->time_info_guest, version) ) +return ; + + info.version = (info.version +2 ) & ~1 ; + +__copy_to_guest (v->time_info_guest, &info,1 ); +} + +void __update_vcpu_system_time (struct vcpu *v,int force) {struct cpu_time *t;struct vcpu_time_info *u;
@@ -839,7 +863,7 @@if ( v->domain->arch.vtsc ) { -if ( u->tsc_timestamp == t->stime_local_stamp ) +if ( !force && u->tsc_timestamp == t->stime_local_stamp )return ;version_update_begin (&u->version); u->tsc_timestamp = t->stime_local_stamp;
@@ -848,7 +872,7 @@ u->tsc_shift =1 ;version_update_end (&u->version); } -else if ( u->tsc_timestamp != t->local_tsc_stamp ) +else if ( force || u->tsc_timestamp != t->local_tsc_stamp ) {version_update_begin (&u->version); u->tsc_timestamp = t->local_tsc_stamp;
@@ -857,6 +881,13 @@ u->tsc_shift = (s8 )t->tsc_scale.shift;version_update_end (&u->version); } + +update_guest_time_info (v, u); +} + +void update_vcpu_system_time (struct vcpu *v) +{ +__update_vcpu_system_time (v,0 ); }void update_domain_wallclock_time (struct domain *d)
xen/include/public/vcpu.h +43 -0
49 lines not on this page · raw message
xen/include/xen/sched.h +3 -0
@@ -102,6 +102,9 @@ } runstate_guest; /* guest address */#endif +/* A secondary copy of the vcpu time info */ +XEN_GUEST_HANDLE (vcpu_time_info_t) time_info_guest; +/* last time when vCPU is scheduled out */ uint64_t last_run_time;
xen/include/xen/time.h +1 -0
@@ -54,6 +54,7 @@#define STIME_MAX ((s_time_t)((uint64_t)~0ull>>1))extern void update_vcpu_system_time (struct vcpu *v); +extern void __update_vcpu_system_time (struct vcpu *v,int force);extern void update_domain_wallclock_time (struct domain *d); extern void do_settime(