1 /***************************************************************************
2 * Copyright (C) 2009 by Marvell Technology Group Ltd. *
3 * Written by Nicolas Pitre <nico@marvell.com> *
5 * Copyright (C) 2010 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2016 by Square, Inc. *
9 * Steven Stallion <stallion@squareup.com> *
11 * Copyright (C) 2018 by Liviu Ionescu *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
26 ***************************************************************************/
30 * Hold ARM semihosting support.
32 * Semihosting enables code running on an ARM target to use the I/O
33 * facilities on the host computer. The target application must be linked
34 * against a library that forwards operation requests by using the SVC
35 * instruction trapped at the Supervisor Call vector by the debugger.
36 * Details can be found in chapter 8 of DUI0203I_rvct_developer_guide.pdf
46 #include "arm7_9_common.h"
52 #include "arm_opcodes.h"
53 #include "target_type.h"
54 #include "arm_semihosting.h"
55 #include <helper/binarybuffer.h>
56 #include <helper/log.h>
59 static int arm_semihosting_resume(struct target
*target
, int *retval
)
61 if (is_armv8(target_to_armv8(target
))) {
62 struct armv8_common
*armv8
= target_to_armv8(target
);
63 if (armv8
->last_run_control_op
== ARMV8_RUNCONTROL_RESUME
) {
64 *retval
= target_resume(target
, 1, 0, 0, 0);
65 if (*retval
!= ERROR_OK
) {
66 LOG_ERROR("Failed to resume target");
69 } else if (armv8
->last_run_control_op
== ARMV8_RUNCONTROL_STEP
)
70 target
->debug_reason
= DBG_REASON_SINGLESTEP
;
72 *retval
= target_resume(target
, 1, 0, 0, 0);
73 if (*retval
!= ERROR_OK
) {
74 LOG_ERROR("Failed to resume target");
81 static int post_result(struct target
*target
)
83 struct arm
*arm
= target_to_arm(target
);
85 if (!target
->semihosting
)
88 /* REVISIT this looks wrong ... ARM11 and Cortex-A8
89 * should work this way at least sometimes.
91 if (is_arm7_9(target_to_arm7_9(target
)) ||
92 is_armv7a(target_to_armv7a(target
))) {
95 /* return value in R0 */
96 buf_set_u32(arm
->core_cache
->reg_list
[0].value
, 0, 32, target
->semihosting
->result
);
97 arm
->core_cache
->reg_list
[0].dirty
= 1;
100 buf_set_u32(arm
->core_cache
->reg_list
[15].value
, 0, 32,
101 buf_get_u32(arm_reg_current(arm
, 14)->value
, 0, 32));
102 arm
->core_cache
->reg_list
[15].dirty
= 1;
104 /* saved PSR --> current PSR */
105 spsr
= buf_get_u32(arm
->spsr
->value
, 0, 32);
107 /* REVISIT should this be arm_set_cpsr(arm, spsr)
108 * instead of a partially unrolled version?
111 buf_set_u32(arm
->cpsr
->value
, 0, 32, spsr
);
112 arm
->cpsr
->dirty
= 1;
113 arm
->core_mode
= spsr
& 0x1f;
115 arm
->core_state
= ARM_STATE_THUMB
;
117 } else if (is_armv8(target_to_armv8(target
))) {
118 if (arm
->core_state
== ARM_STATE_AARCH64
) {
119 /* return value in R0 */
120 buf_set_u64(arm
->core_cache
->reg_list
[0].value
, 0, 64, target
->semihosting
->result
);
121 arm
->core_cache
->reg_list
[0].dirty
= 1;
123 uint64_t pc
= buf_get_u64(arm
->core_cache
->reg_list
[32].value
, 0, 64);
124 buf_set_u64(arm
->pc
->value
, 0, 64, pc
+ 4);
128 /* resume execution, this will be pc+2 to skip over the
129 * bkpt instruction */
131 /* return result in R0 */
132 buf_set_u32(arm
->core_cache
->reg_list
[0].value
, 0, 32, target
->semihosting
->result
);
133 arm
->core_cache
->reg_list
[0].dirty
= 1;
140 * Initialize ARM semihosting support.
142 * @param target Pointer to the ARM target to initialize.
143 * @return An error status if there is a problem during initialization.
145 int arm_semihosting_init(struct target
*target
)
147 struct arm
*arm
= target_to_arm(target
);
148 assert(arm
->setup_semihosting
);
149 semihosting_common_init(target
, arm
->setup_semihosting
, post_result
);
155 * Checks for and processes an ARM semihosting request. This is meant
156 * to be called when the target is stopped due to a debug mode entry.
157 * If the value 0 is returned then there was nothing to process. A non-zero
158 * return value signifies that a request was processed and the target resumed,
159 * or an error was encountered, in which case the caller must return
162 * @param target Pointer to the ARM target to process. This target must
163 * not represent an ARMv6-M or ARMv7-M processor.
164 * @param retval Pointer to a location where the return code will be stored
165 * @return non-zero value if a request was processed or an error encountered
167 int arm_semihosting(struct target
*target
, int *retval
)
169 struct arm
*arm
= target_to_arm(target
);
170 struct armv7a_common
*armv7a
= target_to_armv7a(target
);
171 uint32_t pc
, lr
, spsr
;
174 struct semihosting
*semihosting
= target
->semihosting
;
178 if (!semihosting
->is_active
)
181 if (is_arm7_9(target_to_arm7_9(target
)) ||
183 uint32_t vbar
= 0x00000000;
185 if (arm
->core_mode
!= ARM_MODE_SVC
)
188 if (is_armv7a(armv7a
)) {
189 struct arm_dpm
*dpm
= armv7a
->arm
.dpm
;
191 *retval
= dpm
->prepare(dpm
);
192 if (*retval
== ERROR_OK
) {
193 *retval
= dpm
->instr_read_data_r0(dpm
,
194 ARMV4_5_MRC(15, 0, 0, 12, 0, 0),
199 if (*retval
!= ERROR_OK
)
206 /* Check for PC == 0x00000008 or 0xffff0008: Supervisor Call vector. */
208 pc
= buf_get_u32(r
->value
, 0, 32);
209 if (pc
!= (vbar
+ 0x00000008) && pc
!= 0xffff0008)
212 r
= arm_reg_current(arm
, 14);
213 lr
= buf_get_u32(r
->value
, 0, 32);
215 /* Core-specific code should make sure SPSR is retrieved
216 * when the above checks pass...
218 if (!arm
->spsr
->valid
) {
219 LOG_ERROR("SPSR not valid!");
220 *retval
= ERROR_FAIL
;
224 spsr
= buf_get_u32(arm
->spsr
->value
, 0, 32);
226 /* check instruction that triggered this trap */
227 if (spsr
& (1 << 5)) {
228 /* was in Thumb (or ThumbEE) mode */
232 *retval
= target_read_memory(target
, lr
-2, 2, 1, insn_buf
);
233 if (*retval
!= ERROR_OK
)
235 insn
= target_buffer_get_u16(target
, insn_buf
);
240 } else if (spsr
& (1 << 24)) {
241 /* was in Jazelle mode */
244 /* was in ARM mode */
248 *retval
= target_read_memory(target
, lr
-4, 4, 1, insn_buf
);
249 if (*retval
!= ERROR_OK
)
251 insn
= target_buffer_get_u32(target
, insn_buf
);
254 if (insn
!= 0xEF123456)
257 } else if (is_armv7m(target_to_armv7m(target
))) {
260 if (target
->debug_reason
!= DBG_REASON_BREAKPOINT
)
264 pc
= buf_get_u32(r
->value
, 0, 32);
267 *retval
= target_read_u16(target
, pc
, &insn
);
268 if (*retval
!= ERROR_OK
)
274 } else if (is_armv8(target_to_armv8(target
))) {
275 if (target
->debug_reason
!= DBG_REASON_BREAKPOINT
)
278 if (arm
->core_state
== ARM_STATE_AARCH64
) {
281 uint64_t pc64
= buf_get_u64(r
->value
, 0, 64);
282 *retval
= target_read_u32(target
, pc64
, &insn
);
284 if (*retval
!= ERROR_OK
)
288 if (insn
!= 0xD45E0000)
293 LOG_ERROR("Unsupported semi-hosting Target");
297 /* Perform semihosting if we are not waiting on a fileio
298 * operation to complete.
300 if (!semihosting
->hit_fileio
) {
301 if (is_armv8(target_to_armv8(target
)) &&
302 arm
->core_state
== ARM_STATE_AARCH64
) {
303 /* Read op and param from register x0 and x1 respectively. */
304 semihosting
->op
= buf_get_u64(arm
->core_cache
->reg_list
[0].value
, 0, 64);
305 semihosting
->param
= buf_get_u64(arm
->core_cache
->reg_list
[1].value
, 0, 64);
306 semihosting
->word_size_bytes
= 8;
308 /* Read op and param from register r0 and r1 respectively. */
309 semihosting
->op
= buf_get_u32(arm
->core_cache
->reg_list
[0].value
, 0, 32);
310 semihosting
->param
= buf_get_u32(arm
->core_cache
->reg_list
[1].value
, 0, 32);
311 semihosting
->word_size_bytes
= 4;
314 /* Check for ARM operation numbers. */
315 if (0 <= semihosting
->op
&& semihosting
->op
<= 0x31) {
316 *retval
= semihosting_common(target
);
317 if (*retval
!= ERROR_OK
) {
318 LOG_ERROR("Failed semihosting operation");
322 /* Unknown operation number, not a semihosting call. */
327 /* Resume if target it is resumable and we are not waiting on a fileio
328 * operation to complete:
330 if (semihosting
->is_resumable
&& !semihosting
->hit_fileio
)
331 return arm_semihosting_resume(target
, retval
);