main.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2016 Red Hat, Inc.
  3. * Author: Michael S. Tsirkin <mst@redhat.com>
  4. * This work is licensed under the terms of the GNU GPL, version 2.
  5. *
  6. * Common macros and functions for ring benchmarking.
  7. */
  8. #ifndef MAIN_H
  9. #define MAIN_H
  10. #include <stdbool.h>
  11. extern int param;
  12. extern bool do_exit;
  13. #if defined(__x86_64__) || defined(__i386__)
  14. #include "x86intrin.h"
  15. static inline void wait_cycles(unsigned long long cycles)
  16. {
  17. unsigned long long t;
  18. t = __rdtsc();
  19. while (__rdtsc() - t < cycles) {}
  20. }
  21. #define VMEXIT_CYCLES 500
  22. #define VMENTRY_CYCLES 500
  23. #elif defined(__s390x__)
  24. static inline void wait_cycles(unsigned long long cycles)
  25. {
  26. asm volatile("0: brctg %0,0b" : : "d" (cycles));
  27. }
  28. /* tweak me */
  29. #define VMEXIT_CYCLES 200
  30. #define VMENTRY_CYCLES 200
  31. #else
  32. static inline void wait_cycles(unsigned long long cycles)
  33. {
  34. _Exit(5);
  35. }
  36. #define VMEXIT_CYCLES 0
  37. #define VMENTRY_CYCLES 0
  38. #endif
  39. static inline void vmexit(void)
  40. {
  41. if (!do_exit)
  42. return;
  43. wait_cycles(VMEXIT_CYCLES);
  44. }
  45. static inline void vmentry(void)
  46. {
  47. if (!do_exit)
  48. return;
  49. wait_cycles(VMENTRY_CYCLES);
  50. }
  51. /* implemented by ring */
  52. void alloc_ring(void);
  53. /* guest side */
  54. int add_inbuf(unsigned, void *, void *);
  55. void *get_buf(unsigned *, void **);
  56. void disable_call();
  57. bool used_empty();
  58. bool enable_call();
  59. void kick_available();
  60. /* host side */
  61. void disable_kick();
  62. bool avail_empty();
  63. bool enable_kick();
  64. bool use_buf(unsigned *, void **);
  65. void call_used();
  66. /* implemented by main */
  67. extern bool do_sleep;
  68. void kick(void);
  69. void wait_for_kick(void);
  70. void call(void);
  71. void wait_for_call(void);
  72. extern unsigned ring_size;
  73. /* Compiler barrier - similar to what Linux uses */
  74. #define barrier() asm volatile("" ::: "memory")
  75. /* Is there a portable way to do this? */
  76. #if defined(__x86_64__) || defined(__i386__)
  77. #define cpu_relax() asm ("rep; nop" ::: "memory")
  78. #elif defined(__s390x__)
  79. #define cpu_relax() barrier()
  80. #else
  81. #define cpu_relax() assert(0)
  82. #endif
  83. extern bool do_relax;
  84. static inline void busy_wait(void)
  85. {
  86. if (do_relax)
  87. cpu_relax();
  88. else
  89. /* prevent compiler from removing busy loops */
  90. barrier();
  91. }
  92. /*
  93. * Not using __ATOMIC_SEQ_CST since gcc docs say they are only synchronized
  94. * with other __ATOMIC_SEQ_CST calls.
  95. */
  96. #define smp_mb() __sync_synchronize()
  97. /*
  98. * This abuses the atomic builtins for thread fences, and
  99. * adds a compiler barrier.
  100. */
  101. #define smp_release() do { \
  102. barrier(); \
  103. __atomic_thread_fence(__ATOMIC_RELEASE); \
  104. } while (0)
  105. #define smp_acquire() do { \
  106. __atomic_thread_fence(__ATOMIC_ACQUIRE); \
  107. barrier(); \
  108. } while (0)
  109. #endif