Heinrich Schuchardt | ee0f65d | 2017-09-15 10:06:17 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 14 | static struct efi_event *event_notify; |
| 15 | static struct efi_event *event_wait; |
| 16 | static unsigned int counter; |
| 17 | static struct efi_boot_services *boottime; |
| 18 | |
| 19 | /* |
| 20 | * Notification function, increments a counter. |
| 21 | * |
| 22 | * @event notified event |
| 23 | * @context pointer to the counter |
| 24 | */ |
| 25 | static void EFIAPI notify(struct efi_event *event, void *context) |
| 26 | { |
| 27 | if (!context) |
| 28 | return; |
| 29 | ++*(unsigned int *)context; |
| 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 |
| 40 | */ |
| 41 | static int setup(const efi_handle_t handle, |
| 42 | const struct efi_system_table *systable) |
| 43 | { |
| 44 | efi_status_t ret; |
| 45 | |
| 46 | boottime = systable->boottime; |
| 47 | |
| 48 | ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, |
| 49 | TPL_CALLBACK, notify, (void *)&counter, |
| 50 | &event_notify); |
| 51 | if (ret != EFI_SUCCESS) { |
| 52 | efi_st_error("could not create event\n"); |
| 53 | return 1; |
| 54 | } |
| 55 | ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT, |
| 56 | TPL_HIGH_LEVEL, notify, NULL, &event_wait); |
| 57 | if (ret != EFI_SUCCESS) { |
| 58 | efi_st_error("could not create event\n"); |
| 59 | return 1; |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Tear down unit test. |
| 66 | * |
| 67 | * Close the events created in setup. |
| 68 | */ |
| 69 | static int teardown(void) |
| 70 | { |
| 71 | efi_status_t ret; |
| 72 | |
| 73 | if (event_notify) { |
| 74 | ret = boottime->close_event(event_notify); |
| 75 | event_notify = NULL; |
| 76 | if (ret != EFI_SUCCESS) { |
| 77 | efi_st_error("could not close event\n"); |
| 78 | return 1; |
| 79 | } |
| 80 | } |
| 81 | if (event_wait) { |
| 82 | ret = boottime->close_event(event_wait); |
| 83 | event_wait = NULL; |
| 84 | if (ret != EFI_SUCCESS) { |
| 85 | efi_st_error("could not close event\n"); |
| 86 | return 1; |
| 87 | } |
| 88 | } |
| 89 | boottime->restore_tpl(TPL_APPLICATION); |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Execute unit test. |
| 95 | * |
| 96 | * Run a 10 ms periodic timer and check that it is called 10 times |
| 97 | * while waiting for 100 ms single shot timer. |
| 98 | * |
| 99 | * Raise the TPL level to the level of the 10 ms timer and observe |
| 100 | * that the notification function is not called again. |
| 101 | * |
| 102 | * Lower the TPL level and check that the queued notification |
| 103 | * function is called. |
| 104 | */ |
| 105 | static int execute(void) |
| 106 | { |
| 107 | unsigned long index; |
| 108 | efi_status_t ret; |
| 109 | UINTN old_tpl; |
| 110 | |
| 111 | /* Set 10 ms timer */ |
| 112 | counter = 0; |
| 113 | ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000); |
| 114 | if (ret != EFI_SUCCESS) { |
| 115 | efi_st_error("Could not set timer\n"); |
| 116 | return 1; |
| 117 | } |
| 118 | /* Set 100 ms timer */ |
| 119 | ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000); |
| 120 | if (ret != EFI_SUCCESS) { |
| 121 | efi_st_error("Could not set timer\n"); |
| 122 | return 1; |
| 123 | } |
| 124 | index = 5; |
| 125 | ret = boottime->wait_for_event(1, &event_wait, &index); |
| 126 | if (ret != EFI_SUCCESS) { |
| 127 | efi_st_error("Could not wait for event\n"); |
| 128 | return 1; |
| 129 | } |
| 130 | ret = boottime->check_event(event_wait); |
| 131 | if (ret != EFI_NOT_READY) { |
| 132 | efi_st_error("Signaled state was not cleared.\n"); |
| 133 | efi_st_printf("ret = %u\n", (unsigned int)ret); |
| 134 | return 1; |
| 135 | } |
| 136 | if (index != 0) { |
| 137 | efi_st_error("WaitForEvent returned wrong index\n"); |
| 138 | return 1; |
| 139 | } |
| 140 | efi_st_printf("Counter with TPL level TPL_APPLICATION: %u\n", counter); |
| 141 | if (counter < 8 || counter > 12) { |
| 142 | efi_st_error("Incorrect timing of events\n"); |
| 143 | return 1; |
| 144 | } |
| 145 | ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0); |
| 146 | if (index != 0) { |
| 147 | efi_st_error("Could not cancel timer\n"); |
| 148 | return 1; |
| 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 1; |
| 155 | } |
| 156 | /* Set 10 ms timer */ |
| 157 | counter = 0; |
| 158 | ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000); |
| 159 | if (index != 0) { |
| 160 | efi_st_error("Could not set timer\n"); |
| 161 | return 1; |
| 162 | } |
| 163 | /* Set 100 ms timer */ |
| 164 | ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000); |
| 165 | if (ret != EFI_SUCCESS) { |
| 166 | efi_st_error("Could not set timer\n"); |
| 167 | return 1; |
| 168 | } |
| 169 | do { |
| 170 | ret = boottime->check_event(event_wait); |
| 171 | } while (ret == EFI_NOT_READY); |
| 172 | if (ret != EFI_SUCCESS) { |
| 173 | efi_st_error("Could not check event\n"); |
| 174 | return 1; |
| 175 | } |
| 176 | efi_st_printf("Counter with TPL level TPL_CALLBACK: %u\n", counter); |
| 177 | if (counter != 0) { |
| 178 | efi_st_error("Suppressed timer fired\n"); |
| 179 | return 1; |
| 180 | } |
| 181 | /* Set 1 ms timer */ |
| 182 | ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000); |
| 183 | if (ret != EFI_SUCCESS) { |
| 184 | efi_st_error("Could not set timer\n"); |
| 185 | return 1; |
| 186 | } |
| 187 | /* Restore the old TPL level */ |
| 188 | boottime->restore_tpl(TPL_APPLICATION); |
| 189 | ret = boottime->wait_for_event(1, &event_wait, &index); |
| 190 | if (ret != EFI_SUCCESS) { |
| 191 | efi_st_error("Could not wait for event\n"); |
| 192 | return 1; |
| 193 | } |
| 194 | efi_st_printf("Counter with TPL level TPL_APPLICATION: %u\n", counter); |
| 195 | if (counter < 1) { |
| 196 | efi_st_error("Queued timer event did not fire\n"); |
| 197 | return 1; |
| 198 | } |
| 199 | ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0); |
| 200 | if (index != 0) { |
| 201 | efi_st_error("Could not cancel timer\n"); |
| 202 | return 1; |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | EFI_UNIT_TEST(tpl) = { |
| 209 | .name = "task priority levels", |
| 210 | .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
| 211 | .setup = setup, |
| 212 | .execute = execute, |
| 213 | .teardown = teardown, |
| 214 | }; |