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 
do_compressed(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])10 static int do_compressed(struct cmd_tbl *cmdtp, int flag, int argc,
11 			 char *const argv[])
12 {
13 	/* c.li a0, 0; c.li a0, 0 */
14 	asm volatile (".long 0x45014501\n");
15 	printf("The system supports compressed instructions.\n");
16 	return CMD_RET_SUCCESS;
17 }
18 
do_ebreak(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])19 static int do_ebreak(struct cmd_tbl *cmdtp, int flag, int argc,
20 			char *const argv[])
21 {
22 	asm volatile ("ebreak\n");
23 	return CMD_RET_FAILURE;
24 }
25 
do_ialign16(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])26 static int do_ialign16(struct cmd_tbl *cmdtp, int flag, int argc,
27 		       char *const argv[])
28 {
29 	asm volatile (
30 		/* jump skipping 2 bytes */
31 		".long 0x0060006f\n"
32 		".long 0x006f0000\n"
33 		".long 0x00000060\n"
34 	);
35 	printf("The system supports 16 bit aligned instructions.\n");
36 	return CMD_RET_SUCCESS;
37 }
38 
do_rdcycle(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])39 static int do_rdcycle(struct cmd_tbl *cmdtp, int flag, int argc,
40 		      char *const argv[])
41 {
42 	printf("cycle = 0x%lx\n", csr_read(CSR_CYCLE));
43 
44 	return CMD_RET_SUCCESS;
45 }
46 
do_unaligned(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])47 static int do_unaligned(struct cmd_tbl *cmdtp, int flag, int argc,
48 			char *const argv[])
49 {
50 	asm volatile (
51 		"auipc a1, 0\n"
52 		"ori   a1, a1, 3\n"
53 		"lw    a2, (0)(a1)\n"
54 	);
55 	printf("The system supports unaligned access.\n");
56 	return CMD_RET_SUCCESS;
57 }
58 
do_undefined(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])59 static int do_undefined(struct cmd_tbl *cmdtp, int flag, int argc,
60 			char *const argv[])
61 {
62 	asm volatile (".word 0xffffffff\n");
63 	return CMD_RET_FAILURE;
64 }
65 
66 static struct cmd_tbl cmd_sub[] = {
67 	U_BOOT_CMD_MKENT(compressed, CONFIG_SYS_MAXARGS, 1, do_compressed,
68 			 "", ""),
69 	U_BOOT_CMD_MKENT(ebreak, CONFIG_SYS_MAXARGS, 1, do_ebreak,
70 			 "", ""),
71 	U_BOOT_CMD_MKENT(ialign16, CONFIG_SYS_MAXARGS, 1, do_ialign16,
72 			 "", ""),
73 	U_BOOT_CMD_MKENT(rdcycle, CONFIG_SYS_MAXARGS, 1, do_rdcycle,
74 			 "", ""),
75 	U_BOOT_CMD_MKENT(unaligned, CONFIG_SYS_MAXARGS, 1, do_unaligned,
76 			 "", ""),
77 	U_BOOT_CMD_MKENT(undefined, CONFIG_SYS_MAXARGS, 1, do_undefined,
78 			 "", ""),
79 };
80 
81 U_BOOT_LONGHELP(exception,
82 	"<ex>\n"
83 	"  The following exceptions are available:\n"
84 	"  compressed - compressed instruction\n"
85 	"  ebreak     - breakpoint\n"
86 	"  ialign16   - 16 bit aligned instruction\n"
87 	"  rdcycle    - read cycle CSR\n"
88 	"  unaligned  - load address misaligned\n"
89 	"  undefined  - illegal instruction\n");
90 
91 #include <exception.h>
92