efi_selftest_tpl.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 *efi_st_event_notify;
  12. static struct efi_event *efi_st_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. &efi_st_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_NOTIFY, notify, NULL,
  52. &efi_st_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 (efi_st_event_notify) {
  70. ret = boottime->close_event(efi_st_event_notify);
  71. efi_st_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 (efi_st_event_wait) {
  78. ret = boottime->close_event(efi_st_event_wait);
  79. efi_st_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(efi_st_event_notify, EFI_TIMER_PERIODIC,
  110. 100000);
  111. if (ret != EFI_SUCCESS) {
  112. efi_st_error("Could not set timer\n");
  113. return EFI_ST_FAILURE;
  114. }
  115. /* Set 100 ms timer */
  116. ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE,
  117. 1000000);
  118. if (ret != EFI_SUCCESS) {
  119. efi_st_error("Could not set timer\n");
  120. return EFI_ST_FAILURE;
  121. }
  122. index = 5;
  123. ret = boottime->wait_for_event(1, &efi_st_event_wait, &index);
  124. if (ret != EFI_SUCCESS) {
  125. efi_st_error("Could not wait for event\n");
  126. return EFI_ST_FAILURE;
  127. }
  128. ret = boottime->check_event(efi_st_event_wait);
  129. if (ret != EFI_NOT_READY) {
  130. efi_st_error("Signaled state was not cleared.\n");
  131. efi_st_printf("ret = %u\n", (unsigned int)ret);
  132. return EFI_ST_FAILURE;
  133. }
  134. if (index != 0) {
  135. efi_st_error("WaitForEvent returned wrong index\n");
  136. return EFI_ST_FAILURE;
  137. }
  138. if (notification_count < 8 || notification_count > 12) {
  139. efi_st_printf(
  140. "Notification count with TPL level TPL_APPLICATION: %u\n",
  141. notification_count);
  142. efi_st_error("Incorrect timing of events\n");
  143. return EFI_ST_FAILURE;
  144. }
  145. ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_STOP, 0);
  146. if (ret != EFI_SUCCESS) {
  147. efi_st_error("Could not cancel timer\n");
  148. return EFI_ST_FAILURE;
  149. }
  150. /* Raise TPL level */
  151. old_tpl = boottime->raise_tpl(TPL_CALLBACK);
  152. if (old_tpl != TPL_APPLICATION) {
  153. efi_st_error("Initial TPL level was not TPL_APPLICATION");
  154. return EFI_ST_FAILURE;
  155. }
  156. /* Set 10 ms timer */
  157. notification_count = 0;
  158. ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_PERIODIC,
  159. 100000);
  160. if (ret != EFI_SUCCESS) {
  161. efi_st_error("Could not set timer\n");
  162. return EFI_ST_FAILURE;
  163. }
  164. /* Set 100 ms timer */
  165. ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE,
  166. 1000000);
  167. if (ret != EFI_SUCCESS) {
  168. efi_st_error("Could not set timer\n");
  169. return EFI_ST_FAILURE;
  170. }
  171. do {
  172. ret = boottime->check_event(efi_st_event_wait);
  173. } while (ret == EFI_NOT_READY);
  174. if (ret != EFI_SUCCESS) {
  175. efi_st_error("Could not check event\n");
  176. return EFI_ST_FAILURE;
  177. }
  178. if (notification_count != 0) {
  179. efi_st_printf(
  180. "Notification count with TPL level TPL_CALLBACK: %u\n",
  181. notification_count);
  182. efi_st_error("Suppressed timer fired\n");
  183. return EFI_ST_FAILURE;
  184. }
  185. /* Set 1 ms timer */
  186. ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE, 1000);
  187. if (ret != EFI_SUCCESS) {
  188. efi_st_error("Could not set timer\n");
  189. return EFI_ST_FAILURE;
  190. }
  191. /* Restore the old TPL level */
  192. boottime->restore_tpl(TPL_APPLICATION);
  193. ret = boottime->wait_for_event(1, &efi_st_event_wait, &index);
  194. if (ret != EFI_SUCCESS) {
  195. efi_st_error("Could not wait for event\n");
  196. return EFI_ST_FAILURE;
  197. }
  198. if (notification_count < 1) {
  199. efi_st_printf(
  200. "Notification count with TPL level TPL_APPLICATION: %u\n",
  201. notification_count);
  202. efi_st_error("Queued timer event did not fire\n");
  203. return EFI_ST_FAILURE;
  204. }
  205. ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_STOP, 0);
  206. if (ret != EFI_SUCCESS) {
  207. efi_st_error("Could not cancel timer\n");
  208. return EFI_ST_FAILURE;
  209. }
  210. return EFI_ST_SUCCESS;
  211. }
  212. EFI_UNIT_TEST(tpl) = {
  213. .name = "task priority levels",
  214. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  215. .setup = setup,
  216. .execute = execute,
  217. .teardown = teardown,
  218. };