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 
68  SEMIHOSTING_USER_CMD_0X100 = 0x100, /* First user cmd op code */
69  SEMIHOSTING_USER_CMD_0X107 = 0x107, /* Last supported user cmd op code */
70  SEMIHOSTING_USER_CMD_0X1FF = 0x1FF, /* Last user cmd op code */
71 };
72 
74 #define SEMIHOSTING_MAX_TCL_COMMAND_FIELD_LENGTH (1024 * 1024)
75 
76 /*
77  * Codes used by SEMIHOSTING_SYS_EXIT (formerly
78  * SEMIHOSTING_REPORT_EXCEPTION).
79  * On 64-bits, the exit code is passed explicitly.
80  */
82  /* On 32 bits, use it for exit(0) */
83  ADP_STOPPED_APPLICATION_EXIT = ((2 << 16) + 38),
84  /* On 32 bits, use it for exit(1) */
85  ADP_STOPPED_RUN_TIME_ERROR = ((2 << 16) + 35),
86 };
87 
93 };
94 
96  SEMIHOSTING_NONE, /* Not halted for a semihosting call. */
97  SEMIHOSTING_HANDLED, /* Call handled, and target was resumed. */
98  SEMIHOSTING_WAITING, /* Call handled, target is halted waiting until we can resume. */
99  SEMIHOSTING_ERROR /* Something went wrong. */
100 };
101 
102 struct target;
103 
104 /*
105  * A pointer to this structure was added to the target structure.
106  */
107 struct semihosting {
108 
110  bool is_active;
111 
114 
117 
120 
122  bool is_fileio;
123 
126 
129 
149 
152 
154  int op;
155 
157  uint64_t param;
158 
164  int64_t result;
165 
168 
170  char *cmdline;
171 
173  clock_t setup_time;
174 
176  char *basedir;
177 
184 
185  int (*setup)(struct target *target, int enable);
186  int (*post_result)(struct target *target);
187 };
188 
189 int semihosting_common_init(struct target *target, void *setup,
190  void *post_result);
191 int semihosting_common(struct target *target);
192 
193 /* utility functions which may also be used by semihosting extensions (custom vendor-defined syscalls) */
194 int semihosting_read_fields(struct target *target, size_t number,
195  uint8_t *fields);
196 int semihosting_write_fields(struct target *target, size_t number,
197  uint8_t *fields);
198 uint64_t semihosting_get_field(struct target *target, size_t index,
199  uint8_t *fields);
200 void semihosting_set_field(struct target *target, uint64_t value,
201  size_t index,
202  uint8_t *fields);
203 
205 
206 #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_SYS_REMOVE
@ SEMIHOSTING_SYS_SEEK
@ 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:120