blob: dbea2b06fb27271579f2dc0f1ddd7291f0d65474 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Darwin Rambo261d2762014-06-09 11:12:59 -07002/*
Sean Anderson12a05b32022-03-22 16:59:18 -04003 * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com>
Darwin Rambo261d2762014-06-09 11:12:59 -07004 * Copyright 2014 Broadcom Corporation
Darwin Rambo261d2762014-06-09 11:12:59 -07005 */
6
7/*
Sean Anderson12a05b32022-03-22 16:59:18 -04008 * This code has been tested on arm64/aarch64 fastmodel only. An untested
9 * placeholder exists for armv7 architectures, but since they are commonly
10 * available in silicon now, fastmodel usage makes less sense for them.
Darwin Rambo261d2762014-06-09 11:12:59 -070011 */
12#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Sean Andersonb10f7242022-03-22 16:59:14 -040014#include <semihosting.h>
Darwin Rambo261d2762014-06-09 11:12:59 -070015
16#define SYSOPEN 0x01
17#define SYSCLOSE 0x02
Sean Anderson3ea744e2022-03-22 16:59:23 -040018#define SYSWRITEC 0x03
19#define SYSWRITE0 0x04
Sean Anderson12a05b32022-03-22 16:59:18 -040020#define SYSWRITE 0x05
Darwin Rambo261d2762014-06-09 11:12:59 -070021#define SYSREAD 0x06
Sean Anderson3ea744e2022-03-22 16:59:23 -040022#define SYSREADC 0x07
Sean Anderson385d69d2022-03-22 16:59:30 -040023#define SYSISERROR 0x08
Sean Anderson12a05b32022-03-22 16:59:18 -040024#define SYSSEEK 0x0A
Darwin Rambo261d2762014-06-09 11:12:59 -070025#define SYSFLEN 0x0C
Sean Anderson80e62cc2022-03-22 16:59:16 -040026#define SYSERRNO 0x13
Darwin Rambo261d2762014-06-09 11:12:59 -070027
Darwin Rambo261d2762014-06-09 11:12:59 -070028/*
29 * Call the handler
30 */
Linus Walleije769f682015-03-23 11:06:10 +010031static noinline long smh_trap(unsigned int sysnum, void *addr)
Darwin Rambo261d2762014-06-09 11:12:59 -070032{
Linus Walleij4e1ef152014-12-15 11:05:56 +010033 register long result asm("r0");
Darwin Rambo261d2762014-06-09 11:12:59 -070034#if defined(CONFIG_ARM64)
35 asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
Vadzim Dambrouski432a6242015-10-19 19:40:14 +030036#elif defined(CONFIG_CPU_V7M)
37 asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr));
Darwin Rambo261d2762014-06-09 11:12:59 -070038#else
39 /* Note - untested placeholder */
40 asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
41#endif
42 return result;
43}
44
Sean Anderson385d69d2022-03-22 16:59:30 -040045#if CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)
46static bool _semihosting_enabled = true;
47static bool try_semihosting = true;
48
49bool semihosting_enabled(void)
50{
51 if (try_semihosting) {
52 smh_trap(SYSERRNO, NULL);
53 try_semihosting = false;
54 }
55
56 return _semihosting_enabled;
57}
58
59void disable_semihosting(void)
60{
61 _semihosting_enabled = false;
62}
63#endif
64
Sean Anderson80e62cc2022-03-22 16:59:16 -040065/**
66 * smh_errno() - Read the host's errno
67 *
68 * This gets the value of the host's errno and negates it. The host's errno may
69 * or may not be set, so only call this function if a previous semihosting call
70 * has failed.
71 *
72 * Return: a negative error value
Linus Walleij9be5c662014-12-15 11:06:05 +010073 */
Sean Anderson80e62cc2022-03-22 16:59:16 -040074static int smh_errno(void)
75{
76 long ret = smh_trap(SYSERRNO, NULL);
77
78 if (ret > 0 && ret < INT_MAX)
79 return -ret;
80 return -EIO;
81}
82
Sean Andersoneff77c32022-03-22 16:59:15 -040083long smh_open(const char *fname, enum smh_open_mode mode)
Linus Walleij9be5c662014-12-15 11:06:05 +010084{
85 long fd;
Linus Walleij9be5c662014-12-15 11:06:05 +010086 struct smh_open_s {
87 const char *fname;
88 unsigned long mode;
89 size_t len;
90 } open;
91
Sean Andersoneff77c32022-03-22 16:59:15 -040092 debug("%s: file \'%s\', mode \'%u\'\n", __func__, fname, mode);
Linus Walleij9be5c662014-12-15 11:06:05 +010093
94 open.fname = fname;
95 open.len = strlen(fname);
96 open.mode = mode;
97
98 /* Open the file on the host */
99 fd = smh_trap(SYSOPEN, &open);
100 if (fd == -1)
Sean Anderson80e62cc2022-03-22 16:59:16 -0400101 return smh_errno();
Linus Walleij9be5c662014-12-15 11:06:05 +0100102 return fd;
103}
104
Sean Anderson12a05b32022-03-22 16:59:18 -0400105/**
106 * struct smg_rdwr_s - Arguments for read and write
107 * @fd: A file descriptor returned from smh_open()
108 * @memp: Pointer to a buffer of memory of at least @len bytes
109 * @len: The number of bytes to read or write
110 */
111struct smh_rdwr_s {
112 long fd;
113 void *memp;
114 size_t len;
115};
116
Sean Andersonb10f7242022-03-22 16:59:14 -0400117long smh_read(long fd, void *memp, size_t len)
Linus Walleij9be5c662014-12-15 11:06:05 +0100118{
119 long ret;
Sean Anderson12a05b32022-03-22 16:59:18 -0400120 struct smh_rdwr_s read;
Linus Walleij9be5c662014-12-15 11:06:05 +0100121
Vadzim Dambrouski7bdf75c2015-10-19 19:40:15 +0300122 debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
Linus Walleij9be5c662014-12-15 11:06:05 +0100123
124 read.fd = fd;
125 read.memp = memp;
126 read.len = len;
127
128 ret = smh_trap(SYSREAD, &read);
Sean Anderson80e62cc2022-03-22 16:59:16 -0400129 if (ret < 0)
130 return smh_errno();
131 return len - ret;
Linus Walleij9be5c662014-12-15 11:06:05 +0100132}
133
Sean Anderson12a05b32022-03-22 16:59:18 -0400134long smh_write(long fd, const void *memp, size_t len, ulong *written)
135{
136 long ret;
137 struct smh_rdwr_s write;
138
139 debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
140
141 write.fd = fd;
142 write.memp = (void *)memp;
143 write.len = len;
144
145 ret = smh_trap(SYSWRITE, &write);
146 *written = len - ret;
147 if (ret)
148 return smh_errno();
149 return 0;
150}
151
Sean Andersonb10f7242022-03-22 16:59:14 -0400152long smh_close(long fd)
Linus Walleij9be5c662014-12-15 11:06:05 +0100153{
154 long ret;
155
156 debug("%s: fd %ld\n", __func__, fd);
157
158 ret = smh_trap(SYSCLOSE, &fd);
159 if (ret == -1)
Sean Anderson80e62cc2022-03-22 16:59:16 -0400160 return smh_errno();
161 return 0;
Linus Walleij9be5c662014-12-15 11:06:05 +0100162}
163
Sean Andersonb10f7242022-03-22 16:59:14 -0400164long smh_flen(long fd)
Linus Walleij9be5c662014-12-15 11:06:05 +0100165{
166 long ret;
167
168 debug("%s: fd %ld\n", __func__, fd);
169
170 ret = smh_trap(SYSFLEN, &fd);
171 if (ret == -1)
Sean Anderson80e62cc2022-03-22 16:59:16 -0400172 return smh_errno();
Linus Walleij9be5c662014-12-15 11:06:05 +0100173 return ret;
174}
175
Sean Anderson12a05b32022-03-22 16:59:18 -0400176long smh_seek(long fd, long pos)
177{
178 long ret;
179 struct smh_seek_s {
180 long fd;
181 long pos;
182 } seek;
183
184 debug("%s: fd %ld pos %ld\n", __func__, fd, pos);
185
186 seek.fd = fd;
187 seek.pos = pos;
188
189 ret = smh_trap(SYSSEEK, &seek);
190 if (ret)
191 return smh_errno();
192 return 0;
193}
Sean Anderson3ea744e2022-03-22 16:59:23 -0400194
195int smh_getc(void)
196{
197 return smh_trap(SYSREADC, NULL);
198}
199
200void smh_putc(char ch)
201{
202 smh_trap(SYSWRITEC, &ch);
203}
204
205void smh_puts(const char *s)
206{
207 smh_trap(SYSWRITE0, (char *)s);
208}