efi_selftest_tpl.c 5.8 KB

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