Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | 702e778 | 2018-09-27 20:44:40 +0200 | [diff] [blame] | 3 | * efi_selftest_tpl |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 6 | * |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 7 | * This unit test uses timer events to check the handling of |
| 8 | * task priority levels. |
| 9 | */ |
| 10 | |
| 11 | #include <efi_selftest.h> |
| 12 | |
| 13 | static struct efi_event *event_notify; |
| 14 | static struct efi_event *event_wait; |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 15 | static unsigned int notification_count; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 16 | static struct efi_boot_services *boottime; |
| 17 | |
| 18 | /* |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 19 | * Notification function, increments the notification count. |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 20 | * |
| 21 | * @event notified event |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 22 | * @context pointer to the notification count |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 23 | */ |
| 24 | static void EFIAPI notify(struct efi_event *event, void *context) |
| 25 | { |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 26 | unsigned int *count = context; |
| 27 | |
Heinrich Schuchardt | c43fa8c | 2017-10-08 06:57:25 +0200 | [diff] [blame] | 28 | if (count) |
| 29 | ++*count; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 30 | } |
| 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 40 | * @return: EFI_ST_SUCCESS for success |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 41 | */ |
| 42 | static 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 50 | TPL_CALLBACK, notify, |
| 51 | (void *)¬ification_count, |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 52 | &event_notify); |
| 53 | if (ret != EFI_SUCCESS) { |
| 54 | efi_st_error("could not create event\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 55 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 56 | } |
| 57 | ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT, |
Heinrich Schuchardt | cf7b04d | 2021-06-10 23:10:52 +0200 | [diff] [blame^] | 58 | TPL_NOTIFY, notify, NULL, &event_wait); |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 59 | if (ret != EFI_SUCCESS) { |
| 60 | efi_st_error("could not create event\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 61 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 62 | } |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 63 | return EFI_ST_SUCCESS; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Tear down unit test. |
| 68 | * |
| 69 | * Close the events created in setup. |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 70 | * |
| 71 | * @return: EFI_ST_SUCCESS for success |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 72 | */ |
| 73 | static 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 82 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 83 | } |
| 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 90 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | boottime->restore_tpl(TPL_APPLICATION); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 94 | return EFI_ST_SUCCESS; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 95 | } |
| 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 108 | * |
| 109 | * @return: EFI_ST_SUCCESS for success |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 110 | */ |
| 111 | static int execute(void) |
| 112 | { |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 113 | efi_uintn_t index; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 114 | efi_status_t ret; |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 115 | efi_uintn_t old_tpl; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 116 | |
| 117 | /* Set 10 ms timer */ |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 118 | notification_count = 0; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 119 | 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 122 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 123 | } |
| 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 128 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 129 | } |
| 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 134 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 135 | } |
| 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 140 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 141 | } |
| 142 | if (index != 0) { |
| 143 | efi_st_error("WaitForEvent returned wrong index\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 144 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 145 | } |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 146 | if (notification_count < 8 || notification_count > 12) { |
Heinrich Schuchardt | 5cf88b6 | 2017-12-22 19:21:03 +0100 | [diff] [blame] | 147 | efi_st_printf( |
| 148 | "Notification count with TPL level TPL_APPLICATION: %u\n", |
| 149 | notification_count); |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 150 | efi_st_error("Incorrect timing of events\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 151 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 152 | } |
| 153 | ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0); |
Heinrich Schuchardt | dcbf5f5 | 2019-01-06 16:44:16 +0100 | [diff] [blame] | 154 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 155 | efi_st_error("Could not cancel timer\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 156 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 157 | } |
| 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 162 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 163 | } |
| 164 | /* Set 10 ms timer */ |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 165 | notification_count = 0; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 166 | ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000); |
Heinrich Schuchardt | dcbf5f5 | 2019-01-06 16:44:16 +0100 | [diff] [blame] | 167 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 168 | efi_st_error("Could not set timer\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 169 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 170 | } |
| 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 175 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 176 | } |
| 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 Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 182 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 183 | } |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 184 | if (notification_count != 0) { |
Heinrich Schuchardt | 5cf88b6 | 2017-12-22 19:21:03 +0100 | [diff] [blame] | 185 | efi_st_printf( |
| 186 | "Notification count with TPL level TPL_CALLBACK: %u\n", |
| 187 | notification_count); |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 188 | efi_st_error("Suppressed timer fired\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 189 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 190 | } |
| 191 | /* Set 1 ms timer */ |
| 192 | ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000); |
| 193 | if (ret != EFI_SUCCESS) { |
| 194 | efi_st_error("Could not set timer\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 195 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 196 | } |
| 197 | /* Restore the old TPL level */ |
| 198 | boottime->restore_tpl(TPL_APPLICATION); |
| 199 | ret = boottime->wait_for_event(1, &event_wait, &index); |
| 200 | if (ret != EFI_SUCCESS) { |
| 201 | efi_st_error("Could not wait for event\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 202 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 203 | } |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 204 | if (notification_count < 1) { |
Heinrich Schuchardt | 5cf88b6 | 2017-12-22 19:21:03 +0100 | [diff] [blame] | 205 | efi_st_printf( |
| 206 | "Notification count with TPL level TPL_APPLICATION: %u\n", |
| 207 | notification_count); |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 208 | efi_st_error("Queued timer event did not fire\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 209 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 210 | } |
| 211 | ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0); |
Heinrich Schuchardt | f4ee272 | 2017-10-13 01:00:05 +0200 | [diff] [blame] | 212 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 213 | efi_st_error("Could not cancel timer\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 214 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 217 | return EFI_ST_SUCCESS; |
Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | EFI_UNIT_TEST(tpl) = { |
| 221 | .name = "task priority levels", |
| 222 | .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
| 223 | .setup = setup, |
| 224 | .execute = execute, |
| 225 | .teardown = teardown, |
| 226 | }; |