11c32bb10SHeinrich Schuchardt // SPDX-License-Identifier: GPL-2.0+
21c32bb10SHeinrich Schuchardt /*
31c32bb10SHeinrich Schuchardt  * efi_selftest_exception
41c32bb10SHeinrich Schuchardt  *
51c32bb10SHeinrich Schuchardt  * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
61c32bb10SHeinrich Schuchardt  *
7*d8b2216cSHeinrich Schuchardt  * Test the handling of exceptions by trying to execute an undefined
81c32bb10SHeinrich Schuchardt  * instruction.
91c32bb10SHeinrich Schuchardt  */
101c32bb10SHeinrich Schuchardt 
111c32bb10SHeinrich Schuchardt #include <efi_selftest.h>
121c32bb10SHeinrich Schuchardt 
131c32bb10SHeinrich Schuchardt /**
141c32bb10SHeinrich Schuchardt  * undefined_instruction() - try to executed an undefined instruction
151c32bb10SHeinrich Schuchardt  */
161c32bb10SHeinrich Schuchardt static void undefined_instruction(void)
171c32bb10SHeinrich Schuchardt {
181c32bb10SHeinrich Schuchardt #if defined(CONFIG_ARM)
191c32bb10SHeinrich Schuchardt 	/*
201c32bb10SHeinrich Schuchardt 	 * 0xe7f...f.	is undefined in ARM mode
211c32bb10SHeinrich Schuchardt 	 * 0xde..	is undefined in Thumb mode
221c32bb10SHeinrich Schuchardt 	 */
231c32bb10SHeinrich Schuchardt 	asm volatile (".word 0xe7f7defb\n");
241c32bb10SHeinrich Schuchardt #elif defined(CONFIG_RISCV)
251c32bb10SHeinrich Schuchardt 	asm volatile (".word 0xffffffff\n");
261c32bb10SHeinrich Schuchardt #elif defined(CONFIG_X86)
271c32bb10SHeinrich Schuchardt 	asm volatile (".word 0xffff\n");
281c32bb10SHeinrich Schuchardt #endif
291c32bb10SHeinrich Schuchardt }
301c32bb10SHeinrich Schuchardt 
311c32bb10SHeinrich Schuchardt /**
321c32bb10SHeinrich Schuchardt  * execute() - execute unit test
331c32bb10SHeinrich Schuchardt  *
341c32bb10SHeinrich Schuchardt  * Return:	EFI_ST_SUCCESS for success
351c32bb10SHeinrich Schuchardt  */
361c32bb10SHeinrich Schuchardt static int execute(void)
371c32bb10SHeinrich Schuchardt {
381c32bb10SHeinrich Schuchardt 	undefined_instruction();
391c32bb10SHeinrich Schuchardt 
40*d8b2216cSHeinrich Schuchardt 	efi_st_error("An undefined instruction exception was not raised\n");
411c32bb10SHeinrich Schuchardt 
421c32bb10SHeinrich Schuchardt 	return EFI_ST_FAILURE;
431c32bb10SHeinrich Schuchardt }
441c32bb10SHeinrich Schuchardt 
451c32bb10SHeinrich Schuchardt EFI_UNIT_TEST(exception) = {
461c32bb10SHeinrich Schuchardt 	.name = "exception",
471c32bb10SHeinrich Schuchardt 	.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
481c32bb10SHeinrich Schuchardt 	.execute = execute,
491c32bb10SHeinrich Schuchardt 	.on_request = true,
501c32bb10SHeinrich Schuchardt };
51