1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2019 Keith Packard
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #pragma once
37 
38 #include <stdio.h>
39 #include <stdbool.h>
40 #include <stdint.h>
41 
42 /* Semihost exceptions for sys_semihost_exit */
43 #define ADP_Stopped_BranchThroughZero	0x20000
44 #define ADP_Stopped_UndefinedInstr	0x20001
45 #define ADP_Stopped_SoftwareInterrupt	0x20002
46 #define ADP_Stopped_PrefetchAbort	0x20003
47 #define ADP_Stopped_DataAbort		0x20004
48 #define ADP_Stopped_AddressException	0x20005
49 #define ADP_Stopped_IRQ			0x20006
50 #define ADP_Stopped_FIQ			0x20007
51 #define ADP_Stopped_BreakPoint		0x20020
52 #define ADP_Stopped_WatchPoint		0x20021
53 #define ADP_Stopped_StepComplete	0x20022
54 #define ADP_Stopped_RunTimeErrorUnknown	0x20023
55 #define ADP_Stopped_InternalError	0x20024
56 #define ADP_Stopped_UserInterruption	0x20025
57 #define ADP_Stopped_ApplicationExit	0x20026
58 #define ADP_Stopped_StackOverflow	0x20027
59 #define ADP_Stopped_DivisionByZero	0x20028
60 #define ADP_Stopped_OSSpecific		0x20029
61 
62 /* Semihost extensions */
63 #define SH_EXT_EXIT_EXTENDED		0
64 #define SH_EXT_STDOUT_STDERR		1
65 #define SH_EXT_NUM			2
66 
67 /* Open modes */
68 #define SH_OPEN_R			0
69 #define SH_OPEN_R_B			1
70 #define SH_OPEN_R_PLUS			2
71 #define SH_OPEN_R_PLUS_B		3
72 #define SH_OPEN_W			4
73 #define SH_OPEN_W_B			5
74 #define SH_OPEN_W_PLUS			6
75 #define SH_OPEN_W_PLUS_B		7
76 #define SH_OPEN_A			8
77 #define SH_OPEN_A_B			9
78 #define SH_OPEN_A_PLUS			10
79 #define SH_OPEN_A_PLUS_B		11
80 
81 uintptr_t
82 sys_semihost_clock(void);
83 
84 int
85 sys_semihost_close(int fd);
86 
87 uint64_t
88 sys_semihost_elapsed(void);
89 
90 int
91 sys_semihost_errno(void);
92 
93 void
94 sys_semihost_exit(uintptr_t exception, uintptr_t subcode) _ATTRIBUTE((__noreturn__));
95 
96 void
97 sys_semihost_exit_extended(uintptr_t code) _ATTRIBUTE((__noreturn__));
98 
99 bool
100 sys_semihost_feature(uint8_t feature);
101 
102 uintptr_t
103 sys_semihost_flen(int fd);
104 
105 int
106 sys_semihost_get_cmdline(char *buf, int size);
107 
108 int
109 sys_semihost_getc(FILE *file);
110 
111 struct sys_semihost_block {
112 	void	*heap_base;
113 	void	*heap_limit;
114 	void	*stack_base;
115 	void	*stack_limit;
116 };
117 
118 void
119 sys_semihost_heapinfo(struct sys_semihost_block *block);
120 
121 int
122 sys_semihost_iserror(intptr_t status);
123 
124 int
125 sys_semihost_istty(int fd);
126 
127 int
128 sys_semihost_open(const char *pathname, int semiflags);
129 
130 int
131 sys_semihost_putc(char c, FILE *file);
132 
133 uintptr_t
134 sys_semihost_read(int fd, void *buf, size_t count);
135 
136 int
137 sys_semihost_remove(const char *pathname);
138 
139 int
140 sys_semihost_rename(const char *old_pathname, const char *new_pathname);
141 
142 int
143 sys_semihost_seek(int fd, uintptr_t pos);
144 
145 int
146 sys_semihost_system(const char *command);
147 
148 uintptr_t
149 sys_semihost_tickfreq(void);
150 
151 uintptr_t
152 sys_semihost_time(void);
153 
154 int
155 sys_semihost_tmpnam(char *pathname, int identifier, int maxpath);
156 
157 uintptr_t
158 sys_semihost_write(int fd, const void *buf, uintptr_t count);
159 
160 void
161 sys_semihost_write0(const char *string);
162