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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Real mode transition self-tests
  23. *
  24. * This file allows for easy measurement of the time taken to perform
  25. * real mode transitions, which may have a substantial overhead when
  26. * running under a hypervisor.
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <ipxe/test.h>
  32. #include <ipxe/profile.h>
  33. #include <realmode.h>
  34. /** Number of sample iterations for profiling */
  35. #define PROFILE_COUNT 4096
  36. /** Protected-to-real mode transition profiler */
  37. static struct profiler p2r_profiler __profiler = { .name = "p2r" };
  38. /** Real-to-protected mode transition profiler */
  39. static struct profiler r2p_profiler __profiler = { .name = "r2p" };
  40. /** Real-mode call profiler */
  41. static struct profiler real_call_profiler __profiler = { .name = "real_call" };
  42. /** Protected-mode call profiler */
  43. static struct profiler prot_call_profiler __profiler = { .name = "prot_call" };
  44. /**
  45. * Dummy protected-mode function
  46. */
  47. static void librm_test_prot_call ( void ) {
  48. /* Do nothing */
  49. }
  50. /**
  51. * Perform real mode transition self-tests
  52. *
  53. */
  54. static void librm_test_exec ( void ) {
  55. unsigned int i;
  56. unsigned long timestamp;
  57. unsigned long started;
  58. unsigned long stopped;
  59. unsigned int discard_d;
  60. /* Profile mode transitions. We want to profile each
  61. * direction of the transition separately, so perform an RDTSC
  62. * while in real mode and tweak the profilers' start/stop
  63. * times appropriately.
  64. */
  65. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  66. profile_start ( &p2r_profiler );
  67. __asm__ __volatile__ ( REAL_CODE ( "rdtsc\n\t" )
  68. : "=a" ( timestamp ), "=d" ( discard_d )
  69. : );
  70. profile_start_at ( &r2p_profiler, timestamp );
  71. profile_stop ( &r2p_profiler );
  72. profile_stop_at ( &p2r_profiler, timestamp );
  73. }
  74. /* Profile complete real-mode call cycle */
  75. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  76. profile_start ( &real_call_profiler );
  77. __asm__ __volatile__ ( REAL_CODE ( "" ) : : );
  78. profile_stop ( &real_call_profiler );
  79. }
  80. /* Profile complete protected-mode call cycle */
  81. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  82. __asm__ __volatile__ ( REAL_CODE ( "rdtsc\n\t"
  83. "movl %0, %2\n\t"
  84. "pushl %3\n\t"
  85. "pushw %%cs\n\t"
  86. "call prot_call\n\t"
  87. "addw $4, %%sp\n\t"
  88. "rdtsc\n\t" )
  89. : "=a" ( stopped ), "=d" ( discard_d ),
  90. "=r" ( started )
  91. : "i" ( librm_test_prot_call ) );
  92. profile_start_at ( &prot_call_profiler, started );
  93. profile_stop_at ( &prot_call_profiler, stopped );
  94. }
  95. }
  96. /** Real mode transition self-test */
  97. struct self_test librm_test __self_test = {
  98. .name = "librm",
  99. .exec = librm_test_exec,
  100. };
  101. REQUIRE_OBJECT ( test );