blob: 909c78a1c23fde9e0b5c92b83cb0556f0a27401f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +02002/*
Heinrich Schuchardt702e7782018-09-27 20:44:40 +02003 * efi_selftest_tpl
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +02004 *
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 *
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +02007 * This unit test uses timer events to check the handling of
8 * task priority levels.
9 */
10
11#include <efi_selftest.h>
12
Heinrich Schuchardt03964c52022-10-06 07:28:19 +020013static struct efi_event *efi_st_event_notify;
14static struct efi_event *efi_st_event_wait;
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020015static unsigned int notification_count;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020016static struct efi_boot_services *boottime;
17
18/*
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020019 * Notification function, increments the notification count.
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020020 *
21 * @event notified event
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020022 * @context pointer to the notification count
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020023 */
24static void EFIAPI notify(struct efi_event *event, void *context)
25{
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020026 unsigned int *count = context;
27
Heinrich Schuchardtc43fa8c2017-10-08 06:57:25 +020028 if (count)
29 ++*count;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020030}
31
32/*
33 * Setup unit test.
34 *
35 * Create two timer events.
36 * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
37 *
38 * @handle: handle of the loaded image
39 * @systable: system table
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +010040 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020041 */
42static int setup(const efi_handle_t handle,
43 const struct efi_system_table *systable)
44{
45 efi_status_t ret;
46
47 boottime = systable->boottime;
48
49 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020050 TPL_CALLBACK, notify,
51 (void *)&notification_count,
Heinrich Schuchardt03964c52022-10-06 07:28:19 +020052 &efi_st_event_notify);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020053 if (ret != EFI_SUCCESS) {
54 efi_st_error("could not create event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020055 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020056 }
57 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
Heinrich Schuchardt03964c52022-10-06 07:28:19 +020058 TPL_NOTIFY, notify, NULL,
59 &efi_st_event_wait);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020060 if (ret != EFI_SUCCESS) {
61 efi_st_error("could not create event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020062 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020063 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020064 return EFI_ST_SUCCESS;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020065}
66
67/*
68 * Tear down unit test.
69 *
70 * Close the events created in setup.
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020071 *
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +010072 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020073 */
74static int teardown(void)
75{
76 efi_status_t ret;
77
Heinrich Schuchardt03964c52022-10-06 07:28:19 +020078 if (efi_st_event_notify) {
79 ret = boottime->close_event(efi_st_event_notify);
80 efi_st_event_notify = NULL;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020081 if (ret != EFI_SUCCESS) {
82 efi_st_error("could not close event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020083 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020084 }
85 }
Heinrich Schuchardt03964c52022-10-06 07:28:19 +020086 if (efi_st_event_wait) {
87 ret = boottime->close_event(efi_st_event_wait);
88 efi_st_event_wait = NULL;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020089 if (ret != EFI_SUCCESS) {
90 efi_st_error("could not close event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020091 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020092 }
93 }
94 boottime->restore_tpl(TPL_APPLICATION);
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020095 return EFI_ST_SUCCESS;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020096}
97
98/*
99 * Execute unit test.
100 *
101 * Run a 10 ms periodic timer and check that it is called 10 times
102 * while waiting for 100 ms single shot timer.
103 *
104 * Raise the TPL level to the level of the 10 ms timer and observe
105 * that the notification function is not called again.
106 *
107 * Lower the TPL level and check that the queued notification
108 * function is called.
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200109 *
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +0100110 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200111 */
112static int execute(void)
113{
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100114 efi_uintn_t index;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200115 efi_status_t ret;
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100116 efi_uintn_t old_tpl;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200117
118 /* Set 10 ms timer */
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200119 notification_count = 0;
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200120 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_PERIODIC,
121 100000);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200122 if (ret != EFI_SUCCESS) {
123 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200124 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200125 }
126 /* Set 100 ms timer */
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200127 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE,
128 1000000);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200129 if (ret != EFI_SUCCESS) {
130 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200131 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200132 }
133 index = 5;
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200134 ret = boottime->wait_for_event(1, &efi_st_event_wait, &index);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200135 if (ret != EFI_SUCCESS) {
136 efi_st_error("Could not wait for event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200137 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200138 }
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200139 ret = boottime->check_event(efi_st_event_wait);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200140 if (ret != EFI_NOT_READY) {
141 efi_st_error("Signaled state was not cleared.\n");
142 efi_st_printf("ret = %u\n", (unsigned int)ret);
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200143 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200144 }
145 if (index != 0) {
146 efi_st_error("WaitForEvent returned wrong index\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200147 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200148 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200149 if (notification_count < 8 || notification_count > 12) {
Heinrich Schuchardt5cf88b62017-12-22 19:21:03 +0100150 efi_st_printf(
151 "Notification count with TPL level TPL_APPLICATION: %u\n",
152 notification_count);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200153 efi_st_error("Incorrect timing of events\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200154 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200155 }
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200156 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_STOP, 0);
Heinrich Schuchardtdcbf5f52019-01-06 16:44:16 +0100157 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200158 efi_st_error("Could not cancel timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200159 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200160 }
161 /* Raise TPL level */
162 old_tpl = boottime->raise_tpl(TPL_CALLBACK);
163 if (old_tpl != TPL_APPLICATION) {
164 efi_st_error("Initial TPL level was not TPL_APPLICATION");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200165 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200166 }
167 /* Set 10 ms timer */
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200168 notification_count = 0;
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200169 ret = boottime->set_timer(efi_st_event_notify, EFI_TIMER_PERIODIC,
170 100000);
Heinrich Schuchardtdcbf5f52019-01-06 16:44:16 +0100171 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200172 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200173 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200174 }
175 /* Set 100 ms timer */
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200176 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE,
177 1000000);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200178 if (ret != EFI_SUCCESS) {
179 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200180 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200181 }
182 do {
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200183 ret = boottime->check_event(efi_st_event_wait);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200184 } while (ret == EFI_NOT_READY);
185 if (ret != EFI_SUCCESS) {
186 efi_st_error("Could not check event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200187 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200188 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200189 if (notification_count != 0) {
Heinrich Schuchardt5cf88b62017-12-22 19:21:03 +0100190 efi_st_printf(
191 "Notification count with TPL level TPL_CALLBACK: %u\n",
192 notification_count);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200193 efi_st_error("Suppressed timer fired\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200194 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200195 }
196 /* Set 1 ms timer */
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200197 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_RELATIVE, 1000);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200198 if (ret != EFI_SUCCESS) {
199 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200200 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200201 }
202 /* Restore the old TPL level */
203 boottime->restore_tpl(TPL_APPLICATION);
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200204 ret = boottime->wait_for_event(1, &efi_st_event_wait, &index);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200205 if (ret != EFI_SUCCESS) {
206 efi_st_error("Could not wait for event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200207 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200208 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200209 if (notification_count < 1) {
Heinrich Schuchardt5cf88b62017-12-22 19:21:03 +0100210 efi_st_printf(
211 "Notification count with TPL level TPL_APPLICATION: %u\n",
212 notification_count);
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200213 efi_st_error("Queued timer event did not fire\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200214 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200215 }
Heinrich Schuchardt03964c52022-10-06 07:28:19 +0200216 ret = boottime->set_timer(efi_st_event_wait, EFI_TIMER_STOP, 0);
Heinrich Schuchardtf4ee2722017-10-13 01:00:05 +0200217 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200218 efi_st_error("Could not cancel timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200219 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200220 }
221
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200222 return EFI_ST_SUCCESS;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200223}
224
225EFI_UNIT_TEST(tpl) = {
226 .name = "task priority levels",
227 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
228 .setup = setup,
229 .execute = execute,
230 .teardown = teardown,
231};