OpenOCD
semihosting_common.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /***************************************************************************
4  * Copyright (C) 2018 by Liviu Ionescu *
5  * <ilg@livius.net> *
6  * *
7  * Copyright (C) 2009 by Marvell Technology Group Ltd. *
8  * Written by Nicolas Pitre <nico@marvell.com> *
9  ***************************************************************************/
10 
11 #ifndef OPENOCD_TARGET_SEMIHOSTING_COMMON_H
12 #define OPENOCD_TARGET_SEMIHOSTING_COMMON_H
13 
14 #include <stdint.h>
15 #include <stdbool.h>
16 #include <time.h>
17 #include "helper/replacements.h"
18 #include <server/server.h>
19 
20 /*
21  * According to:
22  * "Semihosting for AArch32 and AArch64, 2025Q1"
23  * https://github.com/ARM-software/abi-aa/releases/download/2025Q1/semihosting.pdf
24  * and to:
25  * https://developer.arm.com/documentation/dui0203/latest/semihosting/about-semihosting/the-semihosting-interface
26  * from ARM Ltd.
27  *
28  * The available semihosting operation numbers passed in R0 are allocated
29  * as follows:
30  * - 0x00-0x31 Used by ARM.
31  * - 0x32-0xFF Reserved for future use by ARM.
32  * - 0x100-0x1FF Reserved for user applications. These are not used by ARM.
33  * However, if you are writing your own SVC operations, you are advised
34  * to use a different SVC number rather than using the semihosted
35  * SVC number and these operation type numbers.
36  * - 0x200-0xFFFFFFFF Undefined and currently unused. It is recommended
37  * that you do not use these.
38  */
39 
41  /*
42  * ARM semihosting operations, in lexicographic order.
43  */
44  SEMIHOSTING_ENTER_SVC = 0x17, /* DEPRECATED */
45 
72  SEMIHOSTING_USER_CMD_0X100 = 0x100, /* First user cmd op code */
73  SEMIHOSTING_USER_CMD_0X107 = 0x107, /* Last supported user cmd op code */
74  SEMIHOSTING_USER_CMD_0X1FF = 0x1FF, /* Last user cmd op code */
75 };
76 
78 #define SEMIHOSTING_MAX_TCL_COMMAND_FIELD_LENGTH (1024 * 1024)
79 
80 /*
81  * Codes used by SEMIHOSTING_SYS_EXIT (formerly
82  * SEMIHOSTING_REPORT_EXCEPTION).
83  * On 64-bits, the exit code is passed explicitly.
84  */
86  /* On 32 bits, use it for exit(0) */
87  ADP_STOPPED_APPLICATION_EXIT = ((2 << 16) + 38),
88  /* On 32 bits, use it for exit(1) */
89  ADP_STOPPED_RUN_TIME_ERROR = ((2 << 16) + 35),
90 };
91 
97 };
98 
100  SEMIHOSTING_NONE, /* Not halted for a semihosting call. */
101  SEMIHOSTING_HANDLED, /* Call handled, and target was resumed. */
102  SEMIHOSTING_WAITING, /* Call handled, target is halted waiting until we can resume. */
103  SEMIHOSTING_ERROR /* Something went wrong. */
104 };
105 
106 struct target;
107 
108 /*
109  * A pointer to this structure was added to the target structure.
110  */
111 struct semihosting {
112 
114  bool is_active;
115 
118 
121 
124 
126  bool is_fileio;
127 
130 
133 
153 
156 
158  int op;
159 
161  uint64_t param;
162 
168  int64_t result;
169 
172 
174  char *cmdline;
175 
177  clock_t setup_time;
178 
180  char *basedir;
181 
188 
189  int (*setup)(struct target *target, int enable);
190  int (*post_result)(struct target *target);
191 };
192 
193 int semihosting_common_init(struct target *target, void *setup,
194  void *post_result);
195 int semihosting_common(struct target *target);
196 
197 /* utility functions which may also be used by semihosting extensions (custom vendor-defined syscalls) */
198 int semihosting_read_fields(struct target *target, size_t number,
199  uint8_t *fields);
200 uint64_t semihosting_get_field(struct target *target, size_t index,
201  uint8_t *fields);
202 
204 
205 #endif /* OPENOCD_TARGET_SEMIHOSTING_COMMON_H */
static int post_result(struct target *target)
enum esirisc_reg_num number
Definition: esirisc.c:87
struct target * target
Definition: rtt/rtt.c:26
int semihosting_common_init(struct target *target, void *setup, void *post_result)
Initialize common semihosting support.
semihosting_redirect_config
@ SEMIHOSTING_REDIRECT_CFG_ALL
@ SEMIHOSTING_REDIRECT_CFG_DEBUG
@ SEMIHOSTING_REDIRECT_CFG_NONE
@ SEMIHOSTING_REDIRECT_CFG_STDIO
const struct command_registration semihosting_common_handlers[]
uint64_t semihosting_get_field(struct target *target, size_t index, uint8_t *fields)
Extract a field from the buffer, considering register size and endianness.
semihosting_reported_exceptions
@ ADP_STOPPED_RUN_TIME_ERROR
@ ADP_STOPPED_APPLICATION_EXIT
semihosting_operation_numbers
@ SEMIHOSTING_SYS_TICKFREQ
@ SEMIHOSTING_USER_CMD_0X107
@ SEMIHOSTING_SYS_EXIT
@ SEMIHOSTING_SYS_ISERROR
@ SEMIHOSTING_USER_CMD_0X1FF
@ SEMIHOSTING_SYS_WRITE
@ SEMIHOSTING_SYS_FLEN
@ SEMIHOSTING_SYS_GET_CMDLINE
@ SEMIHOSTING_SYS_EXIT_EXTENDED
@ SEMIHOSTING_ARM_RESERVED_END
@ SEMIHOSTING_SYS_REMOVE
@ SEMIHOSTING_SYS_SEEK
@ SEMIHOSTING_ARM_RESERVED_START
@ SEMIHOSTING_SYS_TMPNAM
@ SEMIHOSTING_SYS_TIME
@ SEMIHOSTING_SYS_CLOCK
@ SEMIHOSTING_SYS_WRITEC
@ SEMIHOSTING_SYS_ERRNO
@ SEMIHOSTING_SYS_ISTTY
@ SEMIHOSTING_SYS_HEAPINFO
@ SEMIHOSTING_SYS_WRITE0
@ SEMIHOSTING_SYS_ELAPSED
@ SEMIHOSTING_SYS_READ
@ SEMIHOSTING_SYS_SYSTEM
@ SEMIHOSTING_SYS_CLOSE
@ SEMIHOSTING_SYS_READC
@ SEMIHOSTING_SYS_OPEN
@ SEMIHOSTING_SYS_RENAME
@ SEMIHOSTING_ENTER_SVC
@ SEMIHOSTING_USER_CMD_0X100
semihosting_result
@ SEMIHOSTING_ERROR
@ SEMIHOSTING_HANDLED
@ SEMIHOSTING_WAITING
@ SEMIHOSTING_NONE
int semihosting_common(struct target *target)
Portable implementation of ARM semihosting calls.
int semihosting_read_fields(struct target *target, size_t number, uint8_t *fields)
Read all fields of a command from target to buffer.
bool is_resumable
Most are resumable, except the two exit calls.
int stdin_fd
Semihosting STDIO file descriptors.
bool has_resumable_exit
When SEMIHOSTING_SYS_EXIT is called outside a debug session, things are simple, the openocd process c...
int(* setup)(struct target *target, int enable)
int(* post_result)(struct target *target)
bool hit_fileio
A flag reporting whether semihosting fileio operation is active.
size_t word_size_bytes
The Target (hart) word size; 8 for 64-bits targets.
bool is_fileio
A flag reporting whether semihosting fileio is active.
int(* user_command_extension)(struct target *target)
Target's extension of semihosting user commands.
int64_t result
The current semihosting result to be returned to the application.
char * basedir
Base directory for semihosting I/O operations.
bool is_active
A flag reporting whether semihosting is active.
int op
The current semihosting operation (R0 on ARM).
struct connection * tcp_connection
Handle to redirect semihosting print via tcp.
int sys_errno
The value to be returned by semihosting SYS_ERRNO request.
enum semihosting_redirect_config redirect_cfg
redirection configuration, NONE by default
uint64_t param
The current semihosting parameter (R1 or ARM).
char * cmdline
The semihosting command line to be passed to the target.
clock_t setup_time
The current time when 'execution starts'.
Definition: target.h:119