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 <stdint.h>
39 #include <semihost.h>
40
41 #define SYS_CLOCK (0x10)
42 #define SYS_CLOSE (0x02)
43 #define SYS_ELAPSED (0x30)
44 #define SYS_ERRNO (0x13)
45 #define SYS_EXIT (0x18)
46 #define SYS_EXIT_EXTENDED (0x20)
47 #define SYS_FLEN (0x0C)
48 #define SYS_GET_CMDLINE (0x15)
49 #define SYS_HEAPINFO (0x16)
50 #define SYS_ISERROR (0x08)
51 #define SYS_ISTTY (0x09)
52 #define SYS_OPEN (0x01)
53 #define SYS_READ (0x06)
54 #define SYS_READC (0x07)
55 #define SYS_REMOVE (0x0E)
56 #define SYS_RENAME (0x0F)
57 #define SYS_SEEK (0x0A)
58 #define SYS_SYSTEM (0x12)
59 #define SYS_TICKFREQ (0x31)
60 #define SYS_TIME (0x11)
61 #define SYS_TMPNAM (0x0D)
62 #define SYS_WRITE (0x05)
63 #define SYS_WRITEC (0x03)
64 #define SYS_WRITE0 (0x04)
65
66 #define SHFB_MAGIC_0 0x53
67 #define SHFB_MAGIC_1 0x48
68 #define SHFB_MAGIC_2 0x46
69 #define SHFB_MAGIC_3 0x42
70
71 #ifdef __aarch64__
72 typedef unsigned long long int sh_param_t;
73 #else
74 typedef uintptr_t sh_param_t;
75 #endif
76
77 uintptr_t
78 sys_semihost(uintptr_t op, uintptr_t param);
79
80 /* Helper functions to simplify semihosting calls with indirect arguments. */
81 static inline uintptr_t
sys_semihost1(long op,sh_param_t arg1)82 sys_semihost1(long op, sh_param_t arg1)
83 {
84 struct {
85 sh_param_t field1;
86 } indirect_args = {
87 .field1 = arg1,
88 };
89 return sys_semihost(op, (uintptr_t)&indirect_args);
90 }
91
92 static inline uintptr_t
sys_semihost2(long op,sh_param_t arg1,sh_param_t arg2)93 sys_semihost2(long op, sh_param_t arg1, sh_param_t arg2)
94 {
95 struct {
96 sh_param_t field1;
97 sh_param_t field2;
98 } indirect_args = {
99 .field1 = arg1,
100 .field2 = arg2,
101 };
102 return sys_semihost(op, (uintptr_t)&indirect_args);
103 }
104
105 static inline uintptr_t
sys_semihost3(long op,sh_param_t arg1,sh_param_t arg2,sh_param_t arg3)106 sys_semihost3(long op, sh_param_t arg1, sh_param_t arg2, sh_param_t arg3)
107 {
108 struct {
109 sh_param_t field1;
110 sh_param_t field2;
111 sh_param_t field3;
112 } indirect_args = {
113 .field1 = arg1,
114 .field2 = arg2,
115 .field3 = arg3,
116 };
117 return sys_semihost(op, (uintptr_t)&indirect_args);
118 }
119
120 int
121 _map_stdio(int fd);
122