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
= true;
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
= true;
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
= true;
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
= true;
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);
125 arm
->pc
->dirty
= true;
126 } else if (arm
->core_state
== ARM_STATE_ARM
) {
127 /* return value in R0 */
128 buf_set_u32(arm
->core_cache
->reg_list
[0].value
, 0, 32, target
->semihosting
->result
);
129 arm
->core_cache
->reg_list
[0].dirty
= true;
131 uint32_t pc
= buf_get_u32(arm
->core_cache
->reg_list
[32].value
, 0, 32);
132 buf_set_u32(arm
->pc
->value
, 0, 32, pc
+ 4);
133 arm
->pc
->dirty
= true;
134 } else if (arm
->core_state
== ARM_STATE_THUMB
) {
135 /* return value in R0 */
136 buf_set_u32(arm
->core_cache
->reg_list
[0].value
, 0, 32, target
->semihosting
->result
);
137 arm
->core_cache
->reg_list
[0].dirty
= true;
139 uint32_t pc
= buf_get_u32(arm
->core_cache
->reg_list
[32].value
, 0, 32);
140 buf_set_u32(arm
->pc
->value
, 0, 32, pc
+ 2);
141 arm
->pc
->dirty
= true;
144 /* resume execution, this will be pc+2 to skip over the
145 * bkpt instruction */
147 /* return result in R0 */
148 buf_set_u32(arm
->core_cache
->reg_list
[0].value
, 0, 32, target
->semihosting
->result
);
149 arm
->core_cache
->reg_list
[0].dirty
= true;
156 * Initialize ARM semihosting support.
158 * @param target Pointer to the ARM target to initialize.
159 * @return An error status if there is a problem during initialization.
161 int arm_semihosting_init(struct target
*target
)
163 struct arm
*arm
= target_to_arm(target
);
164 assert(arm
->setup_semihosting
);
165 semihosting_common_init(target
, arm
->setup_semihosting
, post_result
);
171 * Checks for and processes an ARM semihosting request. This is meant
172 * to be called when the target is stopped due to a debug mode entry.
173 * If the value 0 is returned then there was nothing to process. A non-zero
174 * return value signifies that a request was processed and the target resumed,
175 * or an error was encountered, in which case the caller must return
178 * @param target Pointer to the ARM target to process. This target must
179 * not represent an ARMv6-M or ARMv7-M processor.
180 * @param retval Pointer to a location where the return code will be stored
181 * @return non-zero value if a request was processed or an error encountered
183 int arm_semihosting(struct target
*target
, int *retval
)
185 struct arm
*arm
= target_to_arm(target
);
186 struct armv7a_common
*armv7a
= target_to_armv7a(target
);
187 uint32_t pc
, lr
, spsr
;
190 struct semihosting
*semihosting
= target
->semihosting
;
194 if (!semihosting
->is_active
)
197 if (is_arm7_9(target_to_arm7_9(target
)) ||
199 uint32_t vbar
= 0x00000000;
201 if (arm
->core_mode
!= ARM_MODE_SVC
)
204 if (is_armv7a(armv7a
)) {
205 struct arm_dpm
*dpm
= armv7a
->arm
.dpm
;
207 *retval
= dpm
->prepare(dpm
);
208 if (*retval
== ERROR_OK
) {
209 *retval
= dpm
->instr_read_data_r0(dpm
,
210 ARMV4_5_MRC(15, 0, 0, 12, 0, 0),
215 if (*retval
!= ERROR_OK
)
222 /* Check for PC == 0x00000008 or 0xffff0008: Supervisor Call vector. */
224 pc
= buf_get_u32(r
->value
, 0, 32);
225 if (pc
!= (vbar
+ 0x00000008) && pc
!= 0xffff0008)
228 r
= arm_reg_current(arm
, 14);
229 lr
= buf_get_u32(r
->value
, 0, 32);
231 /* Core-specific code should make sure SPSR is retrieved
232 * when the above checks pass...
234 if (!arm
->spsr
->valid
) {
235 LOG_ERROR("SPSR not valid!");
236 *retval
= ERROR_FAIL
;
240 spsr
= buf_get_u32(arm
->spsr
->value
, 0, 32);
242 /* check instruction that triggered this trap */
243 if (spsr
& (1 << 5)) {
244 /* was in Thumb (or ThumbEE) mode */
248 *retval
= target_read_memory(target
, lr
-2, 2, 1, insn_buf
);
249 if (*retval
!= ERROR_OK
)
251 insn
= target_buffer_get_u16(target
, insn_buf
);
256 } else if (spsr
& (1 << 24)) {
257 /* was in Jazelle mode */
260 /* was in ARM mode */
264 *retval
= target_read_memory(target
, lr
-4, 4, 1, insn_buf
);
265 if (*retval
!= ERROR_OK
)
267 insn
= target_buffer_get_u32(target
, insn_buf
);
270 if (insn
!= 0xEF123456)
273 } else if (is_armv7m(target_to_armv7m(target
))) {
276 if (target
->debug_reason
!= DBG_REASON_BREAKPOINT
)
280 pc
= buf_get_u32(r
->value
, 0, 32);
283 *retval
= target_read_u16(target
, pc
, &insn
);
284 if (*retval
!= ERROR_OK
)
290 } else if (is_armv8(target_to_armv8(target
))) {
291 if (target
->debug_reason
!= DBG_REASON_BREAKPOINT
)
294 /* According to ARM Semihosting for AArch32 and AArch64:
295 * The HLT encodings are new in version 2.0 of the semihosting specification.
296 * Where possible, have semihosting callers continue to use the previously
297 * existing trap instructions to ensure compatibility with legacy semihosting
299 * These trap instructions are HLT for A64, SVC on A+R profile A32 or T32,
300 * and BKPT on M profile.
301 * However, it is necessary to change from SVC to HLT instructions to support
302 * AArch32 semihosting properly in a mixed AArch32/AArch64 system. */
304 if (arm
->core_state
== ARM_STATE_AARCH64
) {
307 uint64_t pc64
= buf_get_u64(r
->value
, 0, 64);
308 *retval
= target_read_u32(target
, pc64
, &insn
);
310 if (*retval
!= ERROR_OK
)
314 if (insn
!= 0xD45E0000)
316 } else if (arm
->core_state
== ARM_STATE_ARM
) {
318 pc
= buf_get_u32(arm
->pc
->value
, 0, 32);
320 /* A32 instruction => check for HLT 0xF000 (0xE10F0070) */
323 *retval
= target_read_u32(target
, pc
, &insn
);
325 if (*retval
!= ERROR_OK
)
329 if (insn
!= 0xE10F0070)
331 } else if (arm
->core_state
== ARM_STATE_THUMB
) {
333 pc
= buf_get_u32(arm
->pc
->value
, 0, 32);
335 /* T32 instruction => check for HLT 0x3C (0xBABC) */
337 *retval
= target_read_u16(target
, pc
, &insn
);
339 if (*retval
!= ERROR_OK
)
348 LOG_ERROR("Unsupported semi-hosting Target");
352 /* Perform semihosting if we are not waiting on a fileio
353 * operation to complete.
355 if (!semihosting
->hit_fileio
) {
356 if (is_armv8(target_to_armv8(target
)) &&
357 arm
->core_state
== ARM_STATE_AARCH64
) {
358 /* Read op and param from register x0 and x1 respectively. */
359 semihosting
->op
= buf_get_u64(arm
->core_cache
->reg_list
[0].value
, 0, 64);
360 semihosting
->param
= buf_get_u64(arm
->core_cache
->reg_list
[1].value
, 0, 64);
361 semihosting
->word_size_bytes
= 8;
363 /* Read op and param from register r0 and r1 respectively. */
364 semihosting
->op
= buf_get_u32(arm
->core_cache
->reg_list
[0].value
, 0, 32);
365 semihosting
->param
= buf_get_u32(arm
->core_cache
->reg_list
[1].value
, 0, 32);
366 semihosting
->word_size_bytes
= 4;
369 /* Check for ARM operation numbers. */
370 if (0 <= semihosting
->op
&& semihosting
->op
<= 0x31) {
371 *retval
= semihosting_common(target
);
372 if (*retval
!= ERROR_OK
) {
373 LOG_ERROR("Failed semihosting operation (0x%02X)", semihosting
->op
);
377 /* Unknown operation number, not a semihosting call. */
382 /* Resume if target it is resumable and we are not waiting on a fileio
383 * operation to complete:
385 if (semihosting
->is_resumable
&& !semihosting
->hit_fileio
)
386 return arm_semihosting_resume(target
, retval
);