timing.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * \file timing.h
  3. *
  4. * \brief Portable interface to the CPU cycle counter
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_TIMING_H
  26. #define MBEDTLS_TIMING_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #if !defined(MBEDTLS_TIMING_ALT)
  33. // Regular implementation
  34. //
  35. #include <stdint.h>
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /**
  40. * \brief timer structure
  41. */
  42. struct mbedtls_timing_hr_time
  43. {
  44. unsigned char opaque[32];
  45. };
  46. /**
  47. * \brief Context for mbedtls_timing_set/get_delay()
  48. */
  49. typedef struct
  50. {
  51. struct mbedtls_timing_hr_time timer;
  52. uint32_t int_ms;
  53. uint32_t fin_ms;
  54. } mbedtls_timing_delay_context;
  55. extern volatile int mbedtls_timing_alarmed;
  56. /**
  57. * \brief Return the CPU cycle counter value
  58. *
  59. * \warning This is only a best effort! Do not rely on this!
  60. * In particular, it is known to be unreliable on virtual
  61. * machines.
  62. */
  63. unsigned long mbedtls_timing_hardclock( void );
  64. /**
  65. * \brief Return the elapsed time in milliseconds
  66. *
  67. * \param val points to a timer structure
  68. * \param reset if set to 1, the timer is restarted
  69. */
  70. unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
  71. /**
  72. * \brief Setup an alarm clock
  73. *
  74. * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
  75. *
  76. * \warning Only one alarm at a time is supported. In a threaded
  77. * context, this means one for the whole process, not one per
  78. * thread.
  79. */
  80. void mbedtls_set_alarm( int seconds );
  81. /**
  82. * \brief Set a pair of delays to watch
  83. * (See \c mbedtls_timing_get_delay().)
  84. *
  85. * \param data Pointer to timing data
  86. * Must point to a valid \c mbedtls_timing_delay_context struct.
  87. * \param int_ms First (intermediate) delay in milliseconds.
  88. * \param fin_ms Second (final) delay in milliseconds.
  89. * Pass 0 to cancel the current delay.
  90. */
  91. void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
  92. /**
  93. * \brief Get the status of delays
  94. * (Memory helper: number of delays passed.)
  95. *
  96. * \param data Pointer to timing data
  97. * Must point to a valid \c mbedtls_timing_delay_context struct.
  98. *
  99. * \return -1 if cancelled (fin_ms = 0)
  100. * 0 if none of the delays are passed,
  101. * 1 if only the intermediate delay is passed,
  102. * 2 if the final delay is passed.
  103. */
  104. int mbedtls_timing_get_delay( void *data );
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #else /* MBEDTLS_TIMING_ALT */
  109. #include "timing_alt.h"
  110. #endif /* MBEDTLS_TIMING_ALT */
  111. #ifdef __cplusplus
  112. extern "C" {
  113. #endif
  114. #if defined(MBEDTLS_SELF_TEST)
  115. /**
  116. * \brief Checkup routine
  117. *
  118. * \return 0 if successful, or 1 if a test failed
  119. */
  120. int mbedtls_timing_self_test( int verbose );
  121. #endif
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif /* timing.h */