2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-09-15 08:06:17 +00:00
|
|
|
/*
|
2018-09-27 18:44:40 +00:00
|
|
|
* efi_selftest_tpl
|
2017-09-15 08:06:17 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
|
|
|
|
*
|
|
|
|
* This unit test uses timer events to check the handling of
|
|
|
|
* task priority levels.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <efi_selftest.h>
|
|
|
|
|
|
|
|
static struct efi_event *event_notify;
|
|
|
|
static struct efi_event *event_wait;
|
2017-10-04 13:31:26 +00:00
|
|
|
static unsigned int notification_count;
|
2017-09-15 08:06:17 +00:00
|
|
|
static struct efi_boot_services *boottime;
|
|
|
|
|
|
|
|
/*
|
2017-10-04 13:31:26 +00:00
|
|
|
* Notification function, increments the notification count.
|
2017-09-15 08:06:17 +00:00
|
|
|
*
|
|
|
|
* @event notified event
|
2017-10-04 13:31:26 +00:00
|
|
|
* @context pointer to the notification count
|
2017-09-15 08:06:17 +00:00
|
|
|
*/
|
|
|
|
static void EFIAPI notify(struct efi_event *event, void *context)
|
|
|
|
{
|
2017-10-04 13:31:26 +00:00
|
|
|
unsigned int *count = context;
|
|
|
|
|
2017-10-08 04:57:25 +00:00
|
|
|
if (count)
|
|
|
|
++*count;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup unit test.
|
|
|
|
*
|
|
|
|
* Create two timer events.
|
|
|
|
* One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
|
|
|
|
*
|
|
|
|
* @handle: handle of the loaded image
|
|
|
|
* @systable: system table
|
2022-01-20 18:48:20 +00:00
|
|
|
* Return: EFI_ST_SUCCESS for success
|
2017-09-15 08:06:17 +00:00
|
|
|
*/
|
|
|
|
static int setup(const efi_handle_t handle,
|
|
|
|
const struct efi_system_table *systable)
|
|
|
|
{
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
boottime = systable->boottime;
|
|
|
|
|
|
|
|
ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
|
2017-10-04 13:31:26 +00:00
|
|
|
TPL_CALLBACK, notify,
|
|
|
|
(void *)¬ification_count,
|
2017-09-15 08:06:17 +00:00
|
|
|
&event_notify);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("could not create event\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
|
2021-06-10 21:10:52 +00:00
|
|
|
TPL_NOTIFY, notify, NULL, &event_wait);
|
2017-09-15 08:06:17 +00:00
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("could not create event\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_SUCCESS;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tear down unit test.
|
|
|
|
*
|
|
|
|
* Close the events created in setup.
|
2017-10-04 13:31:26 +00:00
|
|
|
*
|
2022-01-20 18:48:20 +00:00
|
|
|
* Return: EFI_ST_SUCCESS for success
|
2017-09-15 08:06:17 +00:00
|
|
|
*/
|
|
|
|
static int teardown(void)
|
|
|
|
{
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
if (event_notify) {
|
|
|
|
ret = boottime->close_event(event_notify);
|
|
|
|
event_notify = NULL;
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("could not close event\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (event_wait) {
|
|
|
|
ret = boottime->close_event(event_wait);
|
|
|
|
event_wait = NULL;
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("could not close event\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
boottime->restore_tpl(TPL_APPLICATION);
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_SUCCESS;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute unit test.
|
|
|
|
*
|
|
|
|
* Run a 10 ms periodic timer and check that it is called 10 times
|
|
|
|
* while waiting for 100 ms single shot timer.
|
|
|
|
*
|
|
|
|
* Raise the TPL level to the level of the 10 ms timer and observe
|
|
|
|
* that the notification function is not called again.
|
|
|
|
*
|
|
|
|
* Lower the TPL level and check that the queued notification
|
|
|
|
* function is called.
|
2017-10-04 13:31:26 +00:00
|
|
|
*
|
2022-01-20 18:48:20 +00:00
|
|
|
* Return: EFI_ST_SUCCESS for success
|
2017-09-15 08:06:17 +00:00
|
|
|
*/
|
|
|
|
static int execute(void)
|
|
|
|
{
|
2017-11-06 20:17:48 +00:00
|
|
|
efi_uintn_t index;
|
2017-09-15 08:06:17 +00:00
|
|
|
efi_status_t ret;
|
2017-11-06 20:17:47 +00:00
|
|
|
efi_uintn_t old_tpl;
|
2017-09-15 08:06:17 +00:00
|
|
|
|
|
|
|
/* Set 10 ms timer */
|
2017-10-04 13:31:26 +00:00
|
|
|
notification_count = 0;
|
2017-09-15 08:06:17 +00:00
|
|
|
ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("Could not set timer\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
/* Set 100 ms timer */
|
|
|
|
ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("Could not set timer\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
index = 5;
|
|
|
|
ret = boottime->wait_for_event(1, &event_wait, &index);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("Could not wait for event\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
ret = boottime->check_event(event_wait);
|
|
|
|
if (ret != EFI_NOT_READY) {
|
|
|
|
efi_st_error("Signaled state was not cleared.\n");
|
|
|
|
efi_st_printf("ret = %u\n", (unsigned int)ret);
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
if (index != 0) {
|
|
|
|
efi_st_error("WaitForEvent returned wrong index\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
2017-10-04 13:31:26 +00:00
|
|
|
if (notification_count < 8 || notification_count > 12) {
|
2017-12-22 18:21:03 +00:00
|
|
|
efi_st_printf(
|
|
|
|
"Notification count with TPL level TPL_APPLICATION: %u\n",
|
|
|
|
notification_count);
|
2017-09-15 08:06:17 +00:00
|
|
|
efi_st_error("Incorrect timing of events\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0);
|
2019-01-06 15:44:16 +00:00
|
|
|
if (ret != EFI_SUCCESS) {
|
2017-09-15 08:06:17 +00:00
|
|
|
efi_st_error("Could not cancel timer\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
/* Raise TPL level */
|
|
|
|
old_tpl = boottime->raise_tpl(TPL_CALLBACK);
|
|
|
|
if (old_tpl != TPL_APPLICATION) {
|
|
|
|
efi_st_error("Initial TPL level was not TPL_APPLICATION");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
/* Set 10 ms timer */
|
2017-10-04 13:31:26 +00:00
|
|
|
notification_count = 0;
|
2017-09-15 08:06:17 +00:00
|
|
|
ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
|
2019-01-06 15:44:16 +00:00
|
|
|
if (ret != EFI_SUCCESS) {
|
2017-09-15 08:06:17 +00:00
|
|
|
efi_st_error("Could not set timer\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
/* Set 100 ms timer */
|
|
|
|
ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("Could not set timer\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
ret = boottime->check_event(event_wait);
|
|
|
|
} while (ret == EFI_NOT_READY);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("Could not check event\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
2017-10-04 13:31:26 +00:00
|
|
|
if (notification_count != 0) {
|
2017-12-22 18:21:03 +00:00
|
|
|
efi_st_printf(
|
|
|
|
"Notification count with TPL level TPL_CALLBACK: %u\n",
|
|
|
|
notification_count);
|
2017-09-15 08:06:17 +00:00
|
|
|
efi_st_error("Suppressed timer fired\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
/* Set 1 ms timer */
|
|
|
|
ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("Could not set timer\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
/* Restore the old TPL level */
|
|
|
|
boottime->restore_tpl(TPL_APPLICATION);
|
|
|
|
ret = boottime->wait_for_event(1, &event_wait, &index);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("Could not wait for event\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
2017-10-04 13:31:26 +00:00
|
|
|
if (notification_count < 1) {
|
2017-12-22 18:21:03 +00:00
|
|
|
efi_st_printf(
|
|
|
|
"Notification count with TPL level TPL_APPLICATION: %u\n",
|
|
|
|
notification_count);
|
2017-09-15 08:06:17 +00:00
|
|
|
efi_st_error("Queued timer event did not fire\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
|
2017-10-12 23:00:05 +00:00
|
|
|
if (ret != EFI_SUCCESS) {
|
2017-09-15 08:06:17 +00:00
|
|
|
efi_st_error("Could not cancel timer\n");
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_FAILURE;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_SUCCESS;
|
2017-09-15 08:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EFI_UNIT_TEST(tpl) = {
|
|
|
|
.name = "task priority levels",
|
|
|
|
.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
|
|
|
|
.setup = setup,
|
|
|
|
.execute = execute,
|
|
|
|
.teardown = teardown,
|
|
|
|
};
|