timing.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #ifndef MBEDTLS_TIMING_H
  24. #define MBEDTLS_TIMING_H
  25. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #if !defined(MBEDTLS_TIMING_ALT)
  31. // Regular implementation
  32. //
  33. #include <stdint.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * \brief timer structure
  39. */
  40. struct mbedtls_timing_hr_time
  41. {
  42. unsigned char opaque[32];
  43. };
  44. /**
  45. * \brief Context for mbedtls_timing_set/get_delay()
  46. */
  47. typedef struct
  48. {
  49. struct mbedtls_timing_hr_time timer;
  50. uint32_t int_ms;
  51. uint32_t fin_ms;
  52. } mbedtls_timing_delay_context;
  53. extern volatile int mbedtls_timing_alarmed;
  54. /**
  55. * \brief Return the CPU cycle counter value
  56. *
  57. * \warning This is only a best effort! Do not rely on this!
  58. * In particular, it is known to be unreliable on virtual
  59. * machines.
  60. */
  61. unsigned long mbedtls_timing_hardclock( void );
  62. /**
  63. * \brief Return the elapsed time in milliseconds
  64. *
  65. * \param val points to a timer structure
  66. * \param reset if set to 1, the timer is restarted
  67. */
  68. unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
  69. /**
  70. * \brief Setup an alarm clock
  71. *
  72. * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
  73. *
  74. * \warning Only one alarm at a time is supported. In a threaded
  75. * context, this means one for the whole process, not one per
  76. * thread.
  77. */
  78. void mbedtls_set_alarm( int seconds );
  79. /**
  80. * \brief Set a pair of delays to watch
  81. * (See \c mbedtls_timing_get_delay().)
  82. *
  83. * \param data Pointer to timing data
  84. * Must point to a valid \c mbedtls_timing_delay_context struct.
  85. * \param int_ms First (intermediate) delay in milliseconds.
  86. * \param fin_ms Second (final) delay in milliseconds.
  87. * Pass 0 to cancel the current delay.
  88. */
  89. void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
  90. /**
  91. * \brief Get the status of delays
  92. * (Memory helper: number of delays passed.)
  93. *
  94. * \param data Pointer to timing data
  95. * Must point to a valid \c mbedtls_timing_delay_context struct.
  96. *
  97. * \return -1 if cancelled (fin_ms = 0)
  98. * 0 if none of the delays are passed,
  99. * 1 if only the intermediate delay is passed,
  100. * 2 if the final delay is passed.
  101. */
  102. int mbedtls_timing_get_delay( void *data );
  103. #ifdef __cplusplus
  104. }
  105. #endif
  106. #else /* MBEDTLS_TIMING_ALT */
  107. #include "timing_alt.h"
  108. #endif /* MBEDTLS_TIMING_ALT */
  109. #ifdef __cplusplus
  110. extern "C" {
  111. #endif
  112. #if defined(MBEDTLS_SELF_TEST)
  113. /**
  114. * \brief Checkup routine
  115. *
  116. * \return 0 if successful, or 1 if a test failed
  117. */
  118. int mbedtls_timing_self_test( int verbose );
  119. #endif
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif /* timing.h */