1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * The 'exception' command can be used for testing exception handling.
4  *
5  * Copyright (c) 2018, Heinrich Schuchardt <xypron.glpk@gmx.de>
6  */
7 
8 #include <command.h>
9 #include <linux/bitops.h>
10 
do_undefined(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])11 static int do_undefined(struct cmd_tbl *cmdtp, int flag, int argc,
12 			char *const argv[])
13 {
14 	/*
15 	 * Instructions starting with the upper 16 bits all 0 are permanently
16 	 * undefined. The lower 16 bits can be used for some kind of immediate.
17 	 * --- ARMv8 ARM (ARM DDI 0487G.a C6.2.339: "UDF")
18 	 */
19 	asm volatile (".word 0x00001234\n");
20 
21 	return CMD_RET_FAILURE;
22 }
23 
24 /*
25  * The ID_AA64MMFR2_EL1 register name is only know to binutils for ARMv8.2
26  * and later architecture revisions. However the register is valid regardless
27  * of binutils architecture support or the core the code is running on, so
28  * just use the generic encoding.
29  */
30 #define ID_AA64MMFR2_EL1 "S3_0_C0_C7_2"
31 
do_unaligned(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])32 static int do_unaligned(struct cmd_tbl *cmdtp, int flag, int argc,
33 			char *const argv[])
34 {
35 	uint64_t reg;
36 
37 	/*
38 	 * The unaligned LDAR access below is only guaranteed to generate an
39 	 * alignment fault on cores not implementing FEAT_LSE2. To avoid false
40 	 * negatives, check this condition before we exectute LDAR.
41 	 */
42 	asm ("mrs %0, "ID_AA64MMFR2_EL1"\n" : "=r" (reg));
43 	if (reg & GENMASK(35, 32)) {
44 		printf("unaligned access check only supported on pre-v8.4 cores\n");
45 		return CMD_RET_FAILURE;
46 	}
47 
48 	/*
49 	 * The load acquire instruction requires the data source to be
50 	 * naturally aligned, and will fault even if strict alignment fault
51 	 * checking is disabled (but only without FEAT_LSE2).
52 	 * --- ARMv8 ARM (ARM DDI 0487G.a B2.5.2: "Alignment of data accesses")
53 	 */
54 	asm volatile (
55 		"mov	x1, sp\n\t"
56 		"orr	x1, x1, #3\n\t"
57 		"ldar	x0, [x1]\n"
58 		::: "x0", "x1" );
59 
60 	return CMD_RET_FAILURE;
61 }
62 
do_breakpoint(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])63 static int do_breakpoint(struct cmd_tbl *cmdtp, int flag, int argc,
64 			 char *const argv[])
65 {
66 	asm volatile ("brk	#123\n");
67 
68 	return CMD_RET_FAILURE;
69 }
70 
71 static struct cmd_tbl cmd_sub[] = {
72 	U_BOOT_CMD_MKENT(breakpoint, CONFIG_SYS_MAXARGS, 1, do_breakpoint,
73 			 "", ""),
74 	U_BOOT_CMD_MKENT(unaligned, CONFIG_SYS_MAXARGS, 1, do_unaligned,
75 			 "", ""),
76 	U_BOOT_CMD_MKENT(undefined, CONFIG_SYS_MAXARGS, 1, do_undefined,
77 			 "", ""),
78 };
79 
80 U_BOOT_LONGHELP(exception,
81 	"<ex>\n"
82 	"  The following exceptions are available:\n"
83 	"  breakpoint - breakpoint instruction exception\n"
84 	"  unaligned  - unaligned LDAR data abort\n"
85 	"  undefined  - undefined instruction exception\n");
86 
87 #include <exception.h>
88