1 // SPDX-License-Identifier: GPL-2.0
3 * Xen stolen ticks accounting.
5 #include <linux/kernel.h>
6 #include <linux/kernel_stat.h>
7 #include <linux/math64.h>
10 #include <asm/paravirt.h>
11 #include <asm/xen/hypervisor.h>
12 #include <asm/xen/hypercall.h>
14 #include <xen/events.h>
15 #include <xen/features.h>
16 #include <xen/interface/xen.h>
17 #include <xen/interface/vcpu.h>
18 #include <xen/xen-ops.h>
20 /* runstate info updated by Xen */
21 static DEFINE_PER_CPU(struct vcpu_runstate_info
, xen_runstate
);
23 /* return an consistent snapshot of 64-bit time/counter value */
24 static u64
get64(const u64
*p
)
28 if (BITS_PER_LONG
< 64) {
33 * Read high then low, and then make sure high is
34 * still the same; this will only loop if low wraps
35 * and carries into high.
36 * XXX some clean way to make this endian-proof?
39 h
= READ_ONCE(p32
[1]);
40 l
= READ_ONCE(p32
[0]);
41 h2
= READ_ONCE(p32
[1]);
44 ret
= (((u64
)h
) << 32) | l
;
51 static void xen_get_runstate_snapshot_cpu(struct vcpu_runstate_info
*res
,
55 struct vcpu_runstate_info
*state
;
57 BUG_ON(preemptible());
59 state
= per_cpu_ptr(&xen_runstate
, cpu
);
62 state_time
= get64(&state
->state_entry_time
);
63 rmb(); /* Hypervisor might update data. */
64 *res
= READ_ONCE(*state
);
65 rmb(); /* Hypervisor might update data. */
66 } while (get64(&state
->state_entry_time
) != state_time
||
67 (state_time
& XEN_RUNSTATE_UPDATE
));
73 void xen_get_runstate_snapshot(struct vcpu_runstate_info
*res
)
75 xen_get_runstate_snapshot_cpu(res
, smp_processor_id());
78 /* return true when a vcpu could run but has no real cpu to run on */
79 bool xen_vcpu_stolen(int vcpu
)
81 return per_cpu(xen_runstate
, vcpu
).state
== RUNSTATE_runnable
;
84 u64
xen_steal_clock(int cpu
)
86 struct vcpu_runstate_info state
;
88 xen_get_runstate_snapshot_cpu(&state
, cpu
);
89 return state
.time
[RUNSTATE_runnable
] + state
.time
[RUNSTATE_offline
];
92 void xen_setup_runstate_info(int cpu
)
94 struct vcpu_register_runstate_memory_area area
;
96 area
.addr
.v
= &per_cpu(xen_runstate
, cpu
);
98 if (HYPERVISOR_vcpu_op(VCPUOP_register_runstate_memory_area
,
99 xen_vcpu_nr(cpu
), &area
))
103 void __init
xen_time_setup_guest(void)
105 bool xen_runstate_remote
;
107 xen_runstate_remote
= !HYPERVISOR_vm_assist(VMASST_CMD_enable
,
108 VMASST_TYPE_runstate_update_flag
);
110 pv_time_ops
.steal_clock
= xen_steal_clock
;
112 static_key_slow_inc(¶virt_steal_enabled
);
113 if (xen_runstate_remote
)
114 static_key_slow_inc(¶virt_steal_rq_enabled
);