1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2018 by Liviu Ionescu *
7 * Copyright (C) 2009 by Marvell Technology Group Ltd. *
8 * Written by Nicolas Pitre <nico@marvell.com> *
10 * Copyright (C) 2010 by Spencer Oliver *
11 * spen@spen-soft.co.uk *
13 * Copyright (C) 2016 by Square, Inc. *
14 * Steven Stallion <stallion@squareup.com> *
16 * This program is free software; you can redistribute it and/or modify *
17 * it under the terms of the GNU General Public License as published by *
18 * the Free Software Foundation; either version 2 of the License, or *
19 * (at your option) any later version. *
21 * This program is distributed in the hope that it will be useful, *
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
24 * GNU General Public License for more details. *
26 * You should have received a copy of the GNU General Public License *
27 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
28 ***************************************************************************/
32 * Hold RISC-V semihosting support.
34 * The RISC-V code is inspired from ARM semihosting.
36 * Details can be found in chapter 8 of DUI0203I_rvct_developer_guide.pdf
44 #include <helper/log.h>
46 #include "target/target.h"
49 static int riscv_semihosting_setup(struct target *target, int enable);
50 static int riscv_semihosting_post_result(struct target *target);
53 * Initialize RISC-V semihosting. Use common ARM code.
55 void riscv_semihosting_init(struct target *target)
57 semihosting_common_init(target, riscv_semihosting_setup,
58 riscv_semihosting_post_result);
62 * Check for and process a semihosting request using the ARM protocol). This
63 * is meant to be called when the target is stopped due to a debug mode entry.
65 * @param target Pointer to the target to process.
66 * @param retval Pointer to a location where the return code will be stored
67 * @return non-zero value if a request was processed or an error encountered
69 enum semihosting_result riscv_semihosting(struct target *target, int *retval)
71 struct semihosting *semihosting = target->semihosting;
73 LOG_DEBUG(" -> NONE (!semihosting)");
74 return SEMIHOSTING_NONE;
77 if (!semihosting->is_active) {
78 LOG_DEBUG(" -> NONE (!semihosting->is_active)");
79 return SEMIHOSTING_NONE;
83 int result = riscv_get_register(target, &pc, GDB_REGNO_PC);
84 if (result != ERROR_OK)
85 return SEMIHOSTING_ERROR;
89 /* Read three uncompressed instructions: The previous, the current one (pointed to by PC) and the next one */
90 for (int i = 0; i < 3; i++) {
91 /* Instruction memories may not support arbitrary read size. Use any size that will work. */
92 *retval = riscv_read_by_any_size(target, (pc - 4) + 4 * i, 4, tmp_buf + 4 * i);
93 if (*retval != ERROR_OK)
94 return SEMIHOSTING_ERROR;
98 * The instructions that trigger a semihosting call,
99 * always uncompressed, should look like:
101 * 01f01013 slli zero,zero,0x1f
103 * 40705013 srai zero,zero,0x7
105 uint32_t pre = target_buffer_get_u32(target, tmp_buf);
106 uint32_t ebreak = target_buffer_get_u32(target, tmp_buf + 4);
107 uint32_t post = target_buffer_get_u32(target, tmp_buf + 8);
108 LOG_DEBUG("check %08x %08x %08x from 0x%" PRIx64 "-4", pre, ebreak, post, pc);
110 if (pre != 0x01f01013 || ebreak != 0x00100073 || post != 0x40705013) {
111 /* Not the magic sequence defining semihosting. */
112 LOG_DEBUG(" -> NONE (no magic)");
113 return SEMIHOSTING_NONE;
117 * Perform semihosting call if we are not waiting on a fileio
118 * operation to complete.
120 if (!semihosting->hit_fileio) {
121 /* RISC-V uses A0 and A1 to pass function arguments */
125 result = riscv_get_register(target, &r0, GDB_REGNO_A0);
126 if (result != ERROR_OK) {
127 LOG_DEBUG(" -> ERROR (couldn't read a0)");
128 return SEMIHOSTING_ERROR;
131 result = riscv_get_register(target, &r1, GDB_REGNO_A1);
132 if (result != ERROR_OK) {
133 LOG_DEBUG(" -> ERROR (couldn't read a1)");
134 return SEMIHOSTING_ERROR;
137 semihosting->op = r0;
138 semihosting->param = r1;
139 semihosting->word_size_bytes = riscv_xlen(target) / 8;
141 /* Check for ARM operation numbers. */
142 if ((semihosting->op >= 0 && semihosting->op <= 0x31) ||
143 (semihosting->op >= 0x100 && semihosting->op <= 0x107)) {
145 *retval = semihosting_common(target);
146 if (*retval != ERROR_OK) {
147 LOG_ERROR("Failed semihosting operation (0x%02X)", semihosting->op);
148 return SEMIHOSTING_ERROR;
151 /* Unknown operation number, not a semihosting call. */
152 LOG_DEBUG(" -> NONE (unknown operation number)");
153 return SEMIHOSTING_NONE;
158 * Resume target if we are not waiting on a fileio
159 * operation to complete.
161 if (semihosting->is_resumable && !semihosting->hit_fileio) {
162 /* Resume right after the EBREAK 4 bytes instruction. */
163 *retval = riscv_set_register(target, GDB_REGNO_PC, pc + 4);
164 if (*retval != ERROR_OK)
165 return SEMIHOSTING_ERROR;
167 LOG_DEBUG(" -> HANDLED");
168 return SEMIHOSTING_HANDLED;
171 LOG_DEBUG(" -> WAITING");
172 return SEMIHOSTING_WAITING;
175 /* -------------------------------------------------------------------------
176 * Local functions. */
179 * Called via semihosting->setup() later, after the target is known,
180 * usually on the first semihosting command.
182 static int riscv_semihosting_setup(struct target *target, int enable)
184 LOG_DEBUG("[%s] enable=%d", target_name(target), enable);
186 struct semihosting *semihosting = target->semihosting;
188 semihosting->setup_time = clock();
193 static int riscv_semihosting_post_result(struct target *target)
195 struct semihosting *semihosting = target->semihosting;
197 /* If not enabled, silently ignored. */
201 LOG_DEBUG("0x%" PRIx64, semihosting->result);
202 riscv_set_register(target, GDB_REGNO_A0, semihosting->result);