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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /** Protected-mode call profiler */
  47. static struct profiler prot_call_profiler __profiler = { .name = "prot_call" };
  48. /**
  49. * Dummy protected-mode function
  50. */
  51. static void librm_test_prot_call ( void ) {
  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. unsigned long started;
  62. unsigned long stopped;
  63. unsigned int discard_d;
  64. /* Profile mode transitions. We want to profile each
  65. * direction of the transition separately, so perform an RDTSC
  66. * while in real mode and tweak the profilers' start/stop
  67. * times appropriately.
  68. */
  69. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  70. profile_start ( &p2r_profiler );
  71. __asm__ __volatile__ ( REAL_CODE ( "rdtsc\n\t" )
  72. : "=a" ( timestamp ), "=d" ( discard_d )
  73. : );
  74. profile_start_at ( &r2p_profiler, timestamp );
  75. profile_stop ( &r2p_profiler );
  76. profile_stop_at ( &p2r_profiler, timestamp );
  77. }
  78. /* Profile complete real-mode call cycle */
  79. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  80. profile_start ( &real_call_profiler );
  81. __asm__ __volatile__ ( REAL_CODE ( "" ) : : );
  82. profile_stop ( &real_call_profiler );
  83. }
  84. /* Profile complete protected-mode call cycle */
  85. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  86. __asm__ __volatile__ ( REAL_CODE ( "rdtsc\n\t"
  87. "movl %0, %2\n\t"
  88. "pushl %3\n\t"
  89. "pushw %%cs\n\t"
  90. "call prot_call\n\t"
  91. "addw $4, %%sp\n\t"
  92. "rdtsc\n\t" )
  93. : "=a" ( stopped ), "=d" ( discard_d ),
  94. "=r" ( started )
  95. : "i" ( librm_test_prot_call ) );
  96. profile_start_at ( &prot_call_profiler, started );
  97. profile_stop_at ( &prot_call_profiler, stopped );
  98. }
  99. }
  100. /** Real mode transition self-test */
  101. struct self_test librm_test __self_test = {
  102. .name = "librm",
  103. .exec = librm_test_exec,
  104. };
  105. REQUIRING_SYMBOL ( librm_test );
  106. REQUIRE_OBJECT ( test );