time.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Xen stolen ticks accounting.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/kernel_stat.h>
  6. #include <linux/math64.h>
  7. #include <linux/gfp.h>
  8. #include <asm/paravirt.h>
  9. #include <asm/xen/hypervisor.h>
  10. #include <asm/xen/hypercall.h>
  11. #include <xen/events.h>
  12. #include <xen/features.h>
  13. #include <xen/interface/xen.h>
  14. #include <xen/interface/vcpu.h>
  15. #include <xen/xen-ops.h>
  16. /* runstate info updated by Xen */
  17. static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate);
  18. /* return an consistent snapshot of 64-bit time/counter value */
  19. static u64 get64(const u64 *p)
  20. {
  21. u64 ret;
  22. if (BITS_PER_LONG < 64) {
  23. u32 *p32 = (u32 *)p;
  24. u32 h, l, h2;
  25. /*
  26. * Read high then low, and then make sure high is
  27. * still the same; this will only loop if low wraps
  28. * and carries into high.
  29. * XXX some clean way to make this endian-proof?
  30. */
  31. do {
  32. h = READ_ONCE(p32[1]);
  33. l = READ_ONCE(p32[0]);
  34. h2 = READ_ONCE(p32[1]);
  35. } while(h2 != h);
  36. ret = (((u64)h) << 32) | l;
  37. } else
  38. ret = READ_ONCE(*p);
  39. return ret;
  40. }
  41. static void xen_get_runstate_snapshot_cpu(struct vcpu_runstate_info *res,
  42. unsigned int cpu)
  43. {
  44. u64 state_time;
  45. struct vcpu_runstate_info *state;
  46. BUG_ON(preemptible());
  47. state = per_cpu_ptr(&xen_runstate, cpu);
  48. do {
  49. state_time = get64(&state->state_entry_time);
  50. rmb(); /* Hypervisor might update data. */
  51. *res = READ_ONCE(*state);
  52. rmb(); /* Hypervisor might update data. */
  53. } while (get64(&state->state_entry_time) != state_time ||
  54. (state_time & XEN_RUNSTATE_UPDATE));
  55. }
  56. /*
  57. * Runstate accounting
  58. */
  59. void xen_get_runstate_snapshot(struct vcpu_runstate_info *res)
  60. {
  61. xen_get_runstate_snapshot_cpu(res, smp_processor_id());
  62. }
  63. /* return true when a vcpu could run but has no real cpu to run on */
  64. bool xen_vcpu_stolen(int vcpu)
  65. {
  66. return per_cpu(xen_runstate, vcpu).state == RUNSTATE_runnable;
  67. }
  68. u64 xen_steal_clock(int cpu)
  69. {
  70. struct vcpu_runstate_info state;
  71. xen_get_runstate_snapshot_cpu(&state, cpu);
  72. return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline];
  73. }
  74. void xen_setup_runstate_info(int cpu)
  75. {
  76. struct vcpu_register_runstate_memory_area area;
  77. area.addr.v = &per_cpu(xen_runstate, cpu);
  78. if (HYPERVISOR_vcpu_op(VCPUOP_register_runstate_memory_area,
  79. xen_vcpu_nr(cpu), &area))
  80. BUG();
  81. }
  82. void __init xen_time_setup_guest(void)
  83. {
  84. bool xen_runstate_remote;
  85. xen_runstate_remote = !HYPERVISOR_vm_assist(VMASST_CMD_enable,
  86. VMASST_TYPE_runstate_update_flag);
  87. pv_time_ops.steal_clock = xen_steal_clock;
  88. static_key_slow_inc(&paravirt_steal_enabled);
  89. if (xen_runstate_remote)
  90. static_key_slow_inc(&paravirt_steal_rq_enabled);
  91. }