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, Release 2.0"
23  * https://static.docs.arm.com/100863/0200/semihosting.pdf
24  * from ARM Ltd.
25  *
26  * The available semihosting operation numbers passed in R0 are allocated
27  * as follows:
28  * - 0x00-0x31 Used by ARM.
29  * - 0x32-0xFF Reserved for future use by ARM.
30  * - 0x100-0x1FF Reserved for user applications. These are not used by ARM.
31  * However, if you are writing your own SVC operations, you are advised
32  * to use a different SVC number rather than using the semihosted
33  * SVC number and these operation type numbers.
34  * - 0x200-0xFFFFFFFF Undefined and currently unused. It is recommended
35  * that you do not use these.
36  */
37 
39  /*
40  * ARM semihosting operations, in lexicographic order.
41  */
42  SEMIHOSTING_ENTER_SVC = 0x17, /* DEPRECATED */
43 
70  SEMIHOSTING_USER_CMD_0X100 = 0x100, /* First user cmd op code */
71  SEMIHOSTING_USER_CMD_0X107 = 0x107, /* Last supported user cmd op code */
72  SEMIHOSTING_USER_CMD_0X1FF = 0x1FF, /* Last user cmd op code */
73 };
74 
76 #define SEMIHOSTING_MAX_TCL_COMMAND_FIELD_LENGTH (1024 * 1024)
77 
78 /*
79  * Codes used by SEMIHOSTING_SYS_EXIT (formerly
80  * SEMIHOSTING_REPORT_EXCEPTION).
81  * On 64-bits, the exit code is passed explicitly.
82  */
84  /* On 32 bits, use it for exit(0) */
85  ADP_STOPPED_APPLICATION_EXIT = ((2 << 16) + 38),
86  /* On 32 bits, use it for exit(1) */
87  ADP_STOPPED_RUN_TIME_ERROR = ((2 << 16) + 35),
88 };
89 
95 };
96 
98  SEMIHOSTING_NONE, /* Not halted for a semihosting call. */
99  SEMIHOSTING_HANDLED, /* Call handled, and target was resumed. */
100  SEMIHOSTING_WAITING, /* Call handled, target is halted waiting until we can resume. */
101  SEMIHOSTING_ERROR /* Something went wrong. */
102 };
103 
104 struct target;
105 
106 /*
107  * A pointer to this structure was added to the target structure.
108  */
109 struct semihosting {
110 
112  bool is_active;
113 
116 
119 
122 
124  bool is_fileio;
125 
128 
131 
151 
154 
156  int op;
157 
159  uint64_t param;
160 
166  int64_t result;
167 
170 
172  char *cmdline;
173 
175  clock_t setup_time;
176 
178  char *basedir;
179 
186 
187  int (*setup)(struct target *target, int enable);
188  int (*post_result)(struct target *target);
189 };
190 
196 const char *semihosting_opcode_to_str(uint64_t opcode);
197 
198 int semihosting_common_init(struct target *target, void *setup,
199  void *post_result);
200 int semihosting_common(struct target *target);
201 
202 /* utility functions which may also be used by semihosting extensions (custom vendor-defined syscalls) */
203 int semihosting_read_fields(struct target *target, size_t number,
204  uint8_t *fields);
205 int semihosting_write_fields(struct target *target, size_t number,
206  uint8_t *fields);
207 uint64_t semihosting_get_field(struct target *target, size_t index,
208  uint8_t *fields);
209 void semihosting_set_field(struct target *target, uint64_t value,
210  size_t index,
211  uint8_t *fields);
212 
214 
215 #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
const char * semihosting_opcode_to_str(uint64_t opcode)
Convert the syscall opcode to a human-readable string.
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.
void semihosting_set_field(struct target *target, uint64_t value, size_t index, uint8_t *fields)
Store a field in the buffer, considering register size and endianness.
int semihosting_read_fields(struct target *target, size_t number, uint8_t *fields)
Read all fields of a command from target to buffer.
int semihosting_write_fields(struct target *target, size_t number, uint8_t *fields)
Write all fields of a command from buffer to target.
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:116