]> git.sur5r.net Git - openocd/blob - src/target/semihosting_common.h
Rework/update ARM semihosting
[openocd] / src / target / semihosting_common.h
1 /***************************************************************************
2  *   Copyright (C) 2018 by Liviu Ionescu                                   *
3  *   <ilg@livius.net>                                                      *
4  *                                                                         *
5  *   Copyright (C) 2009 by Marvell Technology Group Ltd.                   *
6  *   Written by Nicolas Pitre <nico@marvell.com>                           *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
20  ***************************************************************************/
21
22 #ifndef OPENOCD_TARGET_SEMIHOSTING_COMMON_H
23 #define OPENOCD_TARGET_SEMIHOSTING_COMMON_H
24
25 #include <stdint.h>
26 #include <stdbool.h>
27 #include <time.h>
28
29 /*
30  * According to:
31  * "Semihosting for AArch32 and AArch64, Release 2.0"
32  * https://static.docs.arm.com/100863/0200/semihosting.pdf
33  * from ARM Ltd.
34  *
35  * The available semihosting operation numbers passed in R0 are allocated
36  * as follows:
37  * - 0x00-0x31 Used by ARM.
38  * - 0x32-0xFF Reserved for future use by ARM.
39  * - 0x100-0x1FF Reserved for user applications. These are not used by ARM.
40  *   However, if you are writing your own SVC operations, you are advised
41  *   to use a different SVC number rather than using the semihosted
42  *   SVC number and these operation type numbers.
43  * - 0x200-0xFFFFFFFF Undefined and currently unused. It is recommended
44  *   that you do not use these.
45  */
46
47 enum semihosting_operation_numbers {
48         /*
49          * ARM semihosting operations, in lexicographic order.
50          */
51         SEMIHOSTING_ENTER_SVC = 0x17,   /* DEPRECATED */
52
53         SEMIHOSTING_SYS_CLOSE = 0x02,
54         SEMIHOSTING_SYS_CLOCK = 0x10,
55         SEMIHOSTING_SYS_ELAPSED = 0x30,
56         SEMIHOSTING_SYS_ERRNO = 0x13,
57         SEMIHOSTING_SYS_EXIT = 0x18,
58         SEMIHOSTING_SYS_EXIT_EXTENDED = 0x20,
59         SEMIHOSTING_SYS_FLEN = 0x0C,
60         SEMIHOSTING_SYS_GET_CMDLINE = 0x15,
61         SEMIHOSTING_SYS_HEAPINFO = 0x16,
62         SEMIHOSTING_SYS_ISERROR = 0x08,
63         SEMIHOSTING_SYS_ISTTY = 0x09,
64         SEMIHOSTING_SYS_OPEN = 0x01,
65         SEMIHOSTING_SYS_READ = 0x06,
66         SEMIHOSTING_SYS_READC = 0x07,
67         SEMIHOSTING_SYS_REMOVE = 0x0E,
68         SEMIHOSTING_SYS_RENAME = 0x0F,
69         SEMIHOSTING_SYS_SEEK = 0x0A,
70         SEMIHOSTING_SYS_SYSTEM = 0x12,
71         SEMIHOSTING_SYS_TICKFREQ = 0x31,
72         SEMIHOSTING_SYS_TIME = 0x11,
73         SEMIHOSTING_SYS_TMPNAM = 0x0D,
74         SEMIHOSTING_SYS_WRITE = 0x05,
75         SEMIHOSTING_SYS_WRITEC = 0x03,
76         SEMIHOSTING_SYS_WRITE0 = 0x04,
77 };
78
79 /*
80  * Codes used by SEMIHOSTING_SYS_EXIT (formerly
81  * SEMIHOSTING_REPORT_EXCEPTION).
82  * On 64-bits, the exit code is passed explicitly.
83  */
84 enum semihosting_reported_exceptions {
85         /* On 32 bits, use it for exit(0) */
86         ADP_STOPPED_APPLICATION_EXIT = ((2 << 16) + 38),
87         /* On 32 bits, use it for exit(1) */
88         ADP_STOPPED_RUN_TIME_ERROR = ((2 << 16) + 35),
89 };
90
91 struct target;
92
93 /*
94  * A pointer to this structure was added to the target structure.
95  */
96 struct semihosting {
97
98         /** A flag reporting whether semihosting is active. */
99         bool is_active;
100
101         /** A flag reporting whether semihosting fileio is active. */
102         bool is_fileio;
103
104         /** A flag reporting whether semihosting fileio operation is active. */
105         bool hit_fileio;
106
107         /** Most are resumable, except the two exit calls. */
108         bool is_resumable;
109
110         /**
111          * When SEMIHOSTING_SYS_EXIT is called outside a debug session,
112          * things are simple, the openocd process calls exit() and passes
113          * the value returned by the target.
114          * When SEMIHOSTING_SYS_EXIT is called during a debug session,
115          * by default execution returns to the debugger, leaving the
116          * debugger in a HALT state, similar to the state entered when
117          * encountering a break.
118          * In some use cases, it is useful to have SEMIHOSTING_SYS_EXIT
119          * return normally, as any semihosting call, and do not break
120          * to the debugger.
121          * The standard allows this to happen, but the condition
122          * to trigger it is a bit obscure ("by performing an RDI_Execute
123          * request or equivalent").
124          *
125          * To make the SEMIHOSTING_SYS_EXIT call return normally, enable
126          * this variable via the dedicated command (default: disabled).
127          */
128         bool has_resumable_exit;
129
130         /** The Target (hart) word size; 8 for 64-bits targets. */
131         size_t word_size_bytes;
132
133         /** The current semihosting operation (R0 on ARM). */
134         int op;
135
136         /** The current semihosting parameter (R1 or ARM). */
137         uint64_t param;
138
139         /**
140          * The current semihosting result to be returned to the application.
141          * Usually 0 for success, -1 for error,
142          * but sometimes a useful value, even a pointer.
143          */
144         int64_t result;
145
146         /** The value to be returned by semihosting SYS_ERRNO request. */
147         int sys_errno;
148
149         /** The semihosting command line to be passed to the target. */
150         char *cmdline;
151
152         /** The current time when 'execution starts' */
153         clock_t setup_time;
154
155         int (*setup)(struct target *target, int enable);
156         int (*post_result)(struct target *target);
157 };
158
159 int semihosting_common_init(struct target *target, void *setup,
160         void *post_result);
161 int semihosting_common(struct target *target);
162
163 #endif  /* OPENOCD_TARGET_SEMIHOSTING_COMMON_H */