blob: 0b78ee759568b7e90b5a32ce449dc412d88b76eb [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
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 Schuchardt1afe5f72017-10-04 15:31:26 +020040 * @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 Schuchardtee0f65d2017-09-15 10:06:17 +020052 &event_notify);
53 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,
58 TPL_HIGH_LEVEL, notify, NULL, &event_wait);
59 if (ret != EFI_SUCCESS) {
60 efi_st_error("could not create event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020061 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020062 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020063 return EFI_ST_SUCCESS;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020064}
65
66/*
67 * Tear down unit test.
68 *
69 * Close the events created in setup.
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020070 *
71 * @return: EFI_ST_SUCCESS for success
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020072 */
73static int teardown(void)
74{
75 efi_status_t ret;
76
77 if (event_notify) {
78 ret = boottime->close_event(event_notify);
79 event_notify = NULL;
80 if (ret != EFI_SUCCESS) {
81 efi_st_error("could not close event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020082 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020083 }
84 }
85 if (event_wait) {
86 ret = boottime->close_event(event_wait);
87 event_wait = NULL;
88 if (ret != EFI_SUCCESS) {
89 efi_st_error("could not close event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020090 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020091 }
92 }
93 boottime->restore_tpl(TPL_APPLICATION);
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020094 return EFI_ST_SUCCESS;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +020095}
96
97/*
98 * Execute unit test.
99 *
100 * Run a 10 ms periodic timer and check that it is called 10 times
101 * while waiting for 100 ms single shot timer.
102 *
103 * Raise the TPL level to the level of the 10 ms timer and observe
104 * that the notification function is not called again.
105 *
106 * Lower the TPL level and check that the queued notification
107 * function is called.
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200108 *
109 * @return: EFI_ST_SUCCESS for success
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200110 */
111static int execute(void)
112{
Heinrich Schuchardt4df5dc52017-10-05 16:35:54 +0200113 size_t index;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200114 efi_status_t ret;
115 UINTN old_tpl;
116
117 /* Set 10 ms timer */
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200118 notification_count = 0;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200119 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
120 if (ret != EFI_SUCCESS) {
121 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200122 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200123 }
124 /* Set 100 ms timer */
125 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
126 if (ret != EFI_SUCCESS) {
127 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200128 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200129 }
130 index = 5;
131 ret = boottime->wait_for_event(1, &event_wait, &index);
132 if (ret != EFI_SUCCESS) {
133 efi_st_error("Could not wait for event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200134 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200135 }
136 ret = boottime->check_event(event_wait);
137 if (ret != EFI_NOT_READY) {
138 efi_st_error("Signaled state was not cleared.\n");
139 efi_st_printf("ret = %u\n", (unsigned int)ret);
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200140 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200141 }
142 if (index != 0) {
143 efi_st_error("WaitForEvent returned wrong index\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200144 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200145 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200146 efi_st_printf("Notification count with TPL level TPL_APPLICATION: %u\n",
147 notification_count);
148 if (notification_count < 8 || notification_count > 12) {
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200149 efi_st_error("Incorrect timing of events\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200150 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200151 }
152 ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0);
153 if (index != 0) {
154 efi_st_error("Could not cancel timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200155 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200156 }
157 /* Raise TPL level */
158 old_tpl = boottime->raise_tpl(TPL_CALLBACK);
159 if (old_tpl != TPL_APPLICATION) {
160 efi_st_error("Initial TPL level was not TPL_APPLICATION");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200161 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200162 }
163 /* Set 10 ms timer */
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200164 notification_count = 0;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200165 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
166 if (index != 0) {
167 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200168 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200169 }
170 /* Set 100 ms timer */
171 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
172 if (ret != EFI_SUCCESS) {
173 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200174 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200175 }
176 do {
177 ret = boottime->check_event(event_wait);
178 } while (ret == EFI_NOT_READY);
179 if (ret != EFI_SUCCESS) {
180 efi_st_error("Could not check event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200181 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200182 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200183 efi_st_printf("Notification count with TPL level TPL_CALLBACK: %u\n",
184 notification_count);
185 if (notification_count != 0) {
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200186 efi_st_error("Suppressed timer fired\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200187 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200188 }
189 /* Set 1 ms timer */
190 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000);
191 if (ret != EFI_SUCCESS) {
192 efi_st_error("Could not set timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200193 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200194 }
195 /* Restore the old TPL level */
196 boottime->restore_tpl(TPL_APPLICATION);
197 ret = boottime->wait_for_event(1, &event_wait, &index);
198 if (ret != EFI_SUCCESS) {
199 efi_st_error("Could not wait for event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200200 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200201 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200202 efi_st_printf("Notification count with TPL level TPL_APPLICATION: %u\n",
203 notification_count);
204 if (notification_count < 1) {
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200205 efi_st_error("Queued timer event did not fire\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200206 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200207 }
208 ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
209 if (index != 0) {
210 efi_st_error("Could not cancel timer\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200211 return EFI_ST_FAILURE;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200212 }
213
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200214 return EFI_ST_SUCCESS;
Heinrich Schuchardtee0f65d2017-09-15 10:06:17 +0200215}
216
217EFI_UNIT_TEST(tpl) = {
218 .name = "task priority levels",
219 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
220 .setup = setup,
221 .execute = execute,
222 .teardown = teardown,
223};