You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

librm_test.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Real mode transition self-tests
  27. *
  28. * This file allows for easy measurement of the time taken to perform
  29. * real mode transitions, which may have a substantial overhead when
  30. * running under a hypervisor.
  31. *
  32. */
  33. /* Forcibly enable assertions */
  34. #undef NDEBUG
  35. #include <ipxe/test.h>
  36. #include <ipxe/profile.h>
  37. #include <realmode.h>
  38. /** Number of sample iterations for profiling */
  39. #define PROFILE_COUNT 4096
  40. /** Protected-to-real mode transition profiler */
  41. static struct profiler p2r_profiler __profiler = { .name = "p2r" };
  42. /** Real-to-protected mode transition profiler */
  43. static struct profiler r2p_profiler __profiler = { .name = "r2p" };
  44. /** Real-mode call profiler */
  45. static struct profiler real_call_profiler __profiler = { .name = "real_call" };
  46. /** Virtual call profiler */
  47. static struct profiler virt_call_profiler __profiler = { .name = "virt_call" };
  48. /**
  49. * Dummy function for profiling tests
  50. */
  51. static __asmcall void librm_test_call ( struct i386_all_regs *ix86 __unused ) {
  52. /* Do nothing */
  53. }
  54. /**
  55. * Perform real mode transition self-tests
  56. *
  57. */
  58. static void librm_test_exec ( void ) {
  59. unsigned int i;
  60. unsigned long timestamp;
  61. uint32_t timestamp_lo;
  62. uint32_t timestamp_hi;
  63. uint32_t started;
  64. uint32_t stopped;
  65. uint32_t discard_d;
  66. /* Profile mode transitions. We want to profile each
  67. * direction of the transition separately, so perform an RDTSC
  68. * while in real mode and tweak the profilers' start/stop
  69. * times appropriately.
  70. */
  71. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  72. profile_start ( &p2r_profiler );
  73. __asm__ __volatile__ ( REAL_CODE ( "rdtsc\n\t" )
  74. : "=a" ( timestamp_lo ),
  75. "=d" ( timestamp_hi )
  76. : );
  77. timestamp = timestamp_lo;
  78. if ( sizeof ( timestamp ) > sizeof ( timestamp_lo ) )
  79. timestamp |= ( ( ( uint64_t ) timestamp_hi ) << 32 );
  80. profile_start_at ( &r2p_profiler, timestamp );
  81. profile_stop ( &r2p_profiler );
  82. profile_stop_at ( &p2r_profiler, timestamp );
  83. }
  84. /* Profile complete real-mode call cycle */
  85. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  86. profile_start ( &real_call_profiler );
  87. __asm__ __volatile__ ( REAL_CODE ( "" ) : );
  88. profile_stop ( &real_call_profiler );
  89. }
  90. /* Profile complete virtual call cycle */
  91. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  92. __asm__ __volatile__ ( REAL_CODE ( "rdtsc\n\t"
  93. "movl %k0, %k2\n\t"
  94. VIRT_CALL ( librm_test_call )
  95. "rdtsc\n\t" )
  96. : "=a" ( stopped ), "=d" ( discard_d ),
  97. "=R" ( started ) : );
  98. profile_start_at ( &virt_call_profiler, started );
  99. profile_stop_at ( &virt_call_profiler, stopped );
  100. }
  101. }
  102. /** Real mode transition self-test */
  103. struct self_test librm_test __self_test = {
  104. .name = "librm",
  105. .exec = librm_test_exec,
  106. };
  107. REQUIRING_SYMBOL ( librm_test );
  108. REQUIRE_OBJECT ( test );