blob: 6ea0bb7177f402e0fde6766d7e45a596ac07dc87 [file] [log] [blame]
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +02001/*
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
12#include <efi_selftest.h>
13
14static struct efi_event *event_notify;
15static struct efi_event *event_wait;
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020016static unsigned int notification_count;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020017static struct efi_boot_services *boottime;
18
19/*
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020020 * Notification function, increments the notification count.
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020021 *
22 * @event notified event
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020023 * @context pointer to the notification count
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020024 */
25static void EFIAPI notify(struct efi_event *event, void *context)
26{
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020027 unsigned int *count = context;
28
Heinrich Schuchardtc43fa8c2017-10-08 06:57:25 +020029 if (count)
30 ++*count;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020031}
32
33/*
34 * Setup unit test.
35 *
36 * Create two timer events.
37 * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
38 *
39 * @handle: handle of the loaded image
40 * @systable: system table
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020041 * @return: EFI_ST_SUCCESS for success
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020042 */
43static int setup(const efi_handle_t handle,
44 const struct efi_system_table *systable)
45{
46 efi_status_t ret;
47
48 boottime = systable->boottime;
49
50 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020051 TPL_CALLBACK, notify,
52 (void *)&notification_count,
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020053 &event_notify);
54 if (ret != EFI_SUCCESS) {
55 efi_st_error("could not create event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020056 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020057 }
58 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
59 TPL_HIGH_LEVEL, notify, NULL, &event_wait);
60 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 *
72 * @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
78 if (event_notify) {
79 ret = boottime->close_event(event_notify);
80 event_notify = NULL;
81 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 }
86 if (event_wait) {
87 ret = boottime->close_event(event_wait);
88 event_wait = NULL;
89 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 *
110 * @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 Schuchardtee0f65d2017-09-15 10:06:17 +0200120 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
121 if (ret != EFI_SUCCESS) {
122 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200123 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200124 }
125 /* Set 100 ms timer */
126 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
127 if (ret != EFI_SUCCESS) {
128 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200129 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200130 }
131 index = 5;
132 ret = boottime->wait_for_event(1, &event_wait, &index);
133 if (ret != EFI_SUCCESS) {
134 efi_st_error("Could not wait for event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200135 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200136 }
137 ret = boottime->check_event(event_wait);
138 if (ret != EFI_NOT_READY) {
139 efi_st_error("Signaled state was not cleared.\n");
140 efi_st_printf("ret = %u\n", (unsigned int)ret);
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200141 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200142 }
143 if (index != 0) {
144 efi_st_error("WaitForEvent returned wrong index\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200145 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200146 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200147 efi_st_printf("Notification count with TPL level TPL_APPLICATION: %u\n",
148 notification_count);
149 if (notification_count < 8 || notification_count > 12) {
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200150 efi_st_error("Incorrect timing of events\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200151 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200152 }
153 ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0);
154 if (index != 0) {
155 efi_st_error("Could not cancel timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200156 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200157 }
158 /* Raise TPL level */
159 old_tpl = boottime->raise_tpl(TPL_CALLBACK);
160 if (old_tpl != TPL_APPLICATION) {
161 efi_st_error("Initial TPL level was not TPL_APPLICATION");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200162 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200163 }
164 /* Set 10 ms timer */
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200165 notification_count = 0;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200166 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
167 if (index != 0) {
168 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200169 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200170 }
171 /* Set 100 ms timer */
172 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
173 if (ret != EFI_SUCCESS) {
174 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200175 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200176 }
177 do {
178 ret = boottime->check_event(event_wait);
179 } while (ret == EFI_NOT_READY);
180 if (ret != EFI_SUCCESS) {
181 efi_st_error("Could not check event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200182 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200183 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200184 efi_st_printf("Notification count with TPL level TPL_CALLBACK: %u\n",
185 notification_count);
186 if (notification_count != 0) {
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200187 efi_st_error("Suppressed timer fired\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200188 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200189 }
190 /* Set 1 ms timer */
191 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000);
192 if (ret != EFI_SUCCESS) {
193 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200194 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200195 }
196 /* Restore the old TPL level */
197 boottime->restore_tpl(TPL_APPLICATION);
198 ret = boottime->wait_for_event(1, &event_wait, &index);
199 if (ret != EFI_SUCCESS) {
200 efi_st_error("Could not wait for event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200201 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200202 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200203 efi_st_printf("Notification count with TPL level TPL_APPLICATION: %u\n",
204 notification_count);
205 if (notification_count < 1) {
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200206 efi_st_error("Queued timer event did not fire\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200207 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200208 }
209 ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
Heinrich Schuchardtf4ee2722017-10-13 01:00:05 +0200210 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200211 efi_st_error("Could not cancel timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200212 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200213 }
214
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200215 return EFI_ST_SUCCESS;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200216}
217
218EFI_UNIT_TEST(tpl) = {
219 .name = "task priority levels",
220 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
221 .setup = setup,
222 .execute = execute,
223 .teardown = teardown,
224};