From ca441162a79ac1c3cea16513f7f0d2171726be38 Mon Sep 17 00:00:00 2001 From: Andrew Boie Date: Wed, 19 Apr 2017 15:43:05 -0700 Subject: [PATCH] tests: add fatal test case We want to show that if a non-essential thread gets a fatal exception, that thread gets aborted but the rest of the system works properly. We also test that k_oops() does the same. Issue: ZEP-2052 Change-Id: I0f88bcae865bf12bb91bb55e50e8ac9721672434 Signed-off-by: Andrew Boie --- tests/kernel/fatal/Makefile | 5 +++ tests/kernel/fatal/README | 4 ++ tests/kernel/fatal/prj.conf | 2 + tests/kernel/fatal/src/Makefile | 3 ++ tests/kernel/fatal/src/main.c | 75 +++++++++++++++++++++++++++++++++ tests/kernel/fatal/testcase.ini | 2 + 6 files changed, 91 insertions(+) create mode 100644 tests/kernel/fatal/Makefile create mode 100644 tests/kernel/fatal/README create mode 100644 tests/kernel/fatal/prj.conf create mode 100644 tests/kernel/fatal/src/Makefile create mode 100644 tests/kernel/fatal/src/main.c create mode 100644 tests/kernel/fatal/testcase.ini diff --git a/tests/kernel/fatal/Makefile b/tests/kernel/fatal/Makefile new file mode 100644 index 00000000000..dfa3a4c1462 --- /dev/null +++ b/tests/kernel/fatal/Makefile @@ -0,0 +1,5 @@ +BOARD ?= qemu_x86 +CONF_FILE = prj.conf + + +include ${ZEPHYR_BASE}/Makefile.test diff --git a/tests/kernel/fatal/README b/tests/kernel/fatal/README new file mode 100644 index 00000000000..1008edb599d --- /dev/null +++ b/tests/kernel/fatal/README @@ -0,0 +1,4 @@ +This test case verifies that kernel fatal error handling works correctly. + +If a thread causes a CPU exception, and it is not in an ISR or "essential" +thread, the thread gets aborted and the rest of the system executes normally. diff --git a/tests/kernel/fatal/prj.conf b/tests/kernel/fatal/prj.conf new file mode 100644 index 00000000000..753149797b4 --- /dev/null +++ b/tests/kernel/fatal/prj.conf @@ -0,0 +1,2 @@ +CONFIG_MAIN_THREAD_PRIORITY=7 + diff --git a/tests/kernel/fatal/src/Makefile b/tests/kernel/fatal/src/Makefile new file mode 100644 index 00000000000..2b356966312 --- /dev/null +++ b/tests/kernel/fatal/src/Makefile @@ -0,0 +1,3 @@ +ccflags-y += -I${ZEPHYR_BASE}/tests/include + +obj-y = main.o diff --git a/tests/kernel/fatal/src/main.c b/tests/kernel/fatal/src/main.c new file mode 100644 index 00000000000..453e3f69a92 --- /dev/null +++ b/tests/kernel/fatal/src/main.c @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +#define STACKSIZE 1024 +#define PRIORITY 5 + +static char __noinit __stack alt_stack[STACKSIZE]; +volatile int rv; + +void alt_thread1(void) +{ +#if defined(CONFIG_X86) + __asm__ volatile ("ud2"); +#elif defined(CONFIG_NIOS2) + __asm__ volatile ("trap"); +#else + /* Triggers usage fault on ARM, illegal instruction on RISCV32, ARC, + * and xtensa + */ + { + int illegal = 0; + ((void(*)(void))&illegal)(); + } +#endif + rv = TC_FAIL; +} + + +void alt_thread2(void) +{ + k_oops(); + rv = TC_FAIL; +} + + +void main(void) +{ + rv = TC_PASS; + + TC_START("test_fatal"); + + printk("test alt thread 1: generic CPU exception\n"); + k_thread_spawn(alt_stack, STACKSIZE, (k_thread_entry_t)alt_thread1, + NULL, NULL, NULL, K_PRIO_PREEMPT(PRIORITY), 0, + K_NO_WAIT); + if (rv == TC_FAIL) { + printk("thread was not aborted\n"); + goto out; + } else { + printk("PASS\n"); + } + + printk("test alt thread 2: initiate kernel oops\n"); + k_thread_spawn(alt_stack, STACKSIZE, (k_thread_entry_t)alt_thread2, + NULL, NULL, NULL, K_PRIO_PREEMPT(PRIORITY), 0, + K_NO_WAIT); + if (rv == TC_FAIL) { + printk("thread was not aborted\n"); + goto out; + } else { + printk("PASS\n"); + } + +out: + TC_END_RESULT(rv); + TC_END_REPORT(rv); +} diff --git a/tests/kernel/fatal/testcase.ini b/tests/kernel/fatal/testcase.ini new file mode 100644 index 00000000000..914a14535fc --- /dev/null +++ b/tests/kernel/fatal/testcase.ini @@ -0,0 +1,2 @@ +[test] +tags = core