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
10static 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
19static 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
26static 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
39static int do_unaligned(struct cmd_tbl *cmdtp, int flag, int argc,
40			char *const argv[])
41{
42	asm volatile (
43		"auipc a1, 0\n"
44		"ori   a1, a1, 3\n"
45		"lw    a2, (0)(a1)\n"
46	);
47	printf("The system supports unaligned access.\n");
48	return CMD_RET_SUCCESS;
49}
50
51static int do_undefined(struct cmd_tbl *cmdtp, int flag, int argc,
52			char *const argv[])
53{
54	asm volatile (".word 0xffffffff\n");
55	return CMD_RET_FAILURE;
56}
57
58static struct cmd_tbl cmd_sub[] = {
59	U_BOOT_CMD_MKENT(compressed, CONFIG_SYS_MAXARGS, 1, do_compressed,
60			 "", ""),
61	U_BOOT_CMD_MKENT(ebreak, CONFIG_SYS_MAXARGS, 1, do_ebreak,
62			 "", ""),
63	U_BOOT_CMD_MKENT(ialign16, CONFIG_SYS_MAXARGS, 1, do_ialign16,
64			 "", ""),
65	U_BOOT_CMD_MKENT(unaligned, CONFIG_SYS_MAXARGS, 1, do_unaligned,
66			 "", ""),
67	U_BOOT_CMD_MKENT(undefined, CONFIG_SYS_MAXARGS, 1, do_undefined,
68			 "", ""),
69};
70
71static char exception_help_text[] =
72	"<ex>\n"
73	"  The following exceptions are available:\n"
74	"  compressed - compressed instruction\n"
75	"  ebreak     - breakpoint\n"
76	"  ialign16   - 16 bit aligned instruction\n"
77	"  undefined  - illegal instruction\n"
78	"  unaligned  - load address misaligned\n"
79	;
80
81#include <exception.h>
82