efi_selftest_tpl.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * efi_selftest_events
  3. *
  4. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * This unit test uses timer events to check the handling of
  9. * task priority levels.
  10. */
  11. #include <efi_selftest.h>
  12. static struct efi_event *event_notify;
  13. static struct efi_event *event_wait;
  14. static unsigned int notification_count;
  15. static struct efi_boot_services *boottime;
  16. /*
  17. * Notification function, increments the notification count.
  18. *
  19. * @event notified event
  20. * @context pointer to the notification count
  21. */
  22. static void EFIAPI notify(struct efi_event *event, void *context)
  23. {
  24. unsigned int *count = context;
  25. if (count)
  26. ++*count;
  27. }
  28. /*
  29. * Setup unit test.
  30. *
  31. * Create two timer events.
  32. * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
  33. *
  34. * @handle: handle of the loaded image
  35. * @systable: system table
  36. * @return: EFI_ST_SUCCESS for success
  37. */
  38. static int setup(const efi_handle_t handle,
  39. const struct efi_system_table *systable)
  40. {
  41. efi_status_t ret;
  42. boottime = systable->boottime;
  43. ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
  44. TPL_CALLBACK, notify,
  45. (void *)&notification_count,
  46. &event_notify);
  47. if (ret != EFI_SUCCESS) {
  48. efi_st_error("could not create event\n");
  49. return EFI_ST_FAILURE;
  50. }
  51. ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
  52. TPL_HIGH_LEVEL, notify, NULL, &event_wait);
  53. if (ret != EFI_SUCCESS) {
  54. efi_st_error("could not create event\n");
  55. return EFI_ST_FAILURE;
  56. }
  57. return EFI_ST_SUCCESS;
  58. }
  59. /*
  60. * Tear down unit test.
  61. *
  62. * Close the events created in setup.
  63. *
  64. * @return: EFI_ST_SUCCESS for success
  65. */
  66. static int teardown(void)
  67. {
  68. efi_status_t ret;
  69. if (event_notify) {
  70. ret = boottime->close_event(event_notify);
  71. event_notify = NULL;
  72. if (ret != EFI_SUCCESS) {
  73. efi_st_error("could not close event\n");
  74. return EFI_ST_FAILURE;
  75. }
  76. }
  77. if (event_wait) {
  78. ret = boottime->close_event(event_wait);
  79. event_wait = NULL;
  80. if (ret != EFI_SUCCESS) {
  81. efi_st_error("could not close event\n");
  82. return EFI_ST_FAILURE;
  83. }
  84. }
  85. boottime->restore_tpl(TPL_APPLICATION);
  86. return EFI_ST_SUCCESS;
  87. }
  88. /*
  89. * Execute unit test.
  90. *
  91. * Run a 10 ms periodic timer and check that it is called 10 times
  92. * while waiting for 100 ms single shot timer.
  93. *
  94. * Raise the TPL level to the level of the 10 ms timer and observe
  95. * that the notification function is not called again.
  96. *
  97. * Lower the TPL level and check that the queued notification
  98. * function is called.
  99. *
  100. * @return: EFI_ST_SUCCESS for success
  101. */
  102. static int execute(void)
  103. {
  104. efi_uintn_t index;
  105. efi_status_t ret;
  106. efi_uintn_t old_tpl;
  107. /* Set 10 ms timer */
  108. notification_count = 0;
  109. ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
  110. if (ret != EFI_SUCCESS) {
  111. efi_st_error("Could not set timer\n");
  112. return EFI_ST_FAILURE;
  113. }
  114. /* Set 100 ms timer */
  115. ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
  116. if (ret != EFI_SUCCESS) {
  117. efi_st_error("Could not set timer\n");
  118. return EFI_ST_FAILURE;
  119. }
  120. index = 5;
  121. ret = boottime->wait_for_event(1, &event_wait, &index);
  122. if (ret != EFI_SUCCESS) {
  123. efi_st_error("Could not wait for event\n");
  124. return EFI_ST_FAILURE;
  125. }
  126. ret = boottime->check_event(event_wait);
  127. if (ret != EFI_NOT_READY) {
  128. efi_st_error("Signaled state was not cleared.\n");
  129. efi_st_printf("ret = %u\n", (unsigned int)ret);
  130. return EFI_ST_FAILURE;
  131. }
  132. if (index != 0) {
  133. efi_st_error("WaitForEvent returned wrong index\n");
  134. return EFI_ST_FAILURE;
  135. }
  136. if (notification_count < 8 || notification_count > 12) {
  137. efi_st_printf(
  138. "Notification count with TPL level TPL_APPLICATION: %u\n",
  139. notification_count);
  140. efi_st_error("Incorrect timing of events\n");
  141. return EFI_ST_FAILURE;
  142. }
  143. ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0);
  144. if (index != 0) {
  145. efi_st_error("Could not cancel timer\n");
  146. return EFI_ST_FAILURE;
  147. }
  148. /* Raise TPL level */
  149. old_tpl = boottime->raise_tpl(TPL_CALLBACK);
  150. if (old_tpl != TPL_APPLICATION) {
  151. efi_st_error("Initial TPL level was not TPL_APPLICATION");
  152. return EFI_ST_FAILURE;
  153. }
  154. /* Set 10 ms timer */
  155. notification_count = 0;
  156. ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
  157. if (index != 0) {
  158. efi_st_error("Could not set timer\n");
  159. return EFI_ST_FAILURE;
  160. }
  161. /* Set 100 ms timer */
  162. ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
  163. if (ret != EFI_SUCCESS) {
  164. efi_st_error("Could not set timer\n");
  165. return EFI_ST_FAILURE;
  166. }
  167. do {
  168. ret = boottime->check_event(event_wait);
  169. } while (ret == EFI_NOT_READY);
  170. if (ret != EFI_SUCCESS) {
  171. efi_st_error("Could not check event\n");
  172. return EFI_ST_FAILURE;
  173. }
  174. if (notification_count != 0) {
  175. efi_st_printf(
  176. "Notification count with TPL level TPL_CALLBACK: %u\n",
  177. notification_count);
  178. efi_st_error("Suppressed timer fired\n");
  179. return EFI_ST_FAILURE;
  180. }
  181. /* Set 1 ms timer */
  182. ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000);
  183. if (ret != EFI_SUCCESS) {
  184. efi_st_error("Could not set timer\n");
  185. return EFI_ST_FAILURE;
  186. }
  187. /* Restore the old TPL level */
  188. boottime->restore_tpl(TPL_APPLICATION);
  189. ret = boottime->wait_for_event(1, &event_wait, &index);
  190. if (ret != EFI_SUCCESS) {
  191. efi_st_error("Could not wait for event\n");
  192. return EFI_ST_FAILURE;
  193. }
  194. if (notification_count < 1) {
  195. efi_st_printf(
  196. "Notification count with TPL level TPL_APPLICATION: %u\n",
  197. notification_count);
  198. efi_st_error("Queued timer event did not fire\n");
  199. return EFI_ST_FAILURE;
  200. }
  201. ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
  202. if (ret != EFI_SUCCESS) {
  203. efi_st_error("Could not cancel timer\n");
  204. return EFI_ST_FAILURE;
  205. }
  206. return EFI_ST_SUCCESS;
  207. }
  208. EFI_UNIT_TEST(tpl) = {
  209. .name = "task priority levels",
  210. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  211. .setup = setup,
  212. .execute = execute,
  213. .teardown = teardown,
  214. };