1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2022 Ventana Micro Systems Inc.
*
* Authors:
* Anup Patel <apatel@ventanamicro.com>
* Kautuk Consul <kconsul@ventanamicro.com>
*/
#include <sbi/sbi_console.h>
#include <sbi/sbi_string.h>
#include <sbi/sbi_error.h>
#include <sbi_utils/serial/semihosting.h>
#define SYSOPEN 0x01
#define SYSWRITEC 0x03
#define SYSWRITE 0x05
#define SYSREAD 0x06
#define SYSREADC 0x07
#define SYSERRNO 0x13
static long semihosting_trap(int sysnum, void *addr)
{
register int ret asm ("a0") = sysnum;
register void *param0 asm ("a1") = addr;
asm volatile (
" .align 4\n"
" .option push\n"
" .option norvc\n"
" slli zero, zero, 0x1f\n"
" ebreak\n"
" srai zero, zero, 7\n"
" .option pop\n"
: "+r" (ret) : "r" (param0) : "memory");
return ret;
}
static bool _semihosting_enabled = true;
static bool try_semihosting = true;
bool semihosting_enabled(void)
{
register int ret asm ("a0") = SYSERRNO;
register void *param0 asm ("a1") = NULL;
unsigned long tmp = 0;
if (!try_semihosting)
return _semihosting_enabled;
asm volatile (
" .align 4\n"
" .option push\n"
" .option norvc\n"
" j _semihost_test_vector_next\n"
" .align 4\n"
"_semihost_test_vector:\n"
" csrr %[en], mepc\n"
" addi %[en], %[en], 4\n"
" csrw mepc, %[en]\n"
" add %[en], zero, zero\n"
" mret\n"
"_semihost_test_vector_next:\n"
" lla %[tmp], _semihost_test_vector\n"
" csrrw %[tmp], mtvec, %[tmp]\n"
" .align 4\n"
" slli zero, zero, 0x1f\n"
" ebreak\n"
" srai zero, zero, 7\n"
" csrw mtvec, %[tmp]\n"
" .option pop\n"
: [tmp] "+r" (tmp), [en] "+r" (_semihosting_enabled),
[ret] "+r" (ret)
: "r" (param0) : "memory");
try_semihosting = false;
return _semihosting_enabled;
}
static int semihosting_errno(void)
{
long ret = semihosting_trap(SYSERRNO, NULL);
if (ret > 0)
return -ret;
return SBI_EIO;
}
static int semihosting_infd = SBI_ENODEV;
static int semihosting_outfd = SBI_ENODEV;
static long semihosting_open(const char *fname, enum semihosting_open_mode mode)
{
long fd;
struct semihosting_open_s {
const char *fname;
unsigned long mode;
size_t len;
} open;
open.fname = fname;
open.len = sbi_strlen(fname);
open.mode = mode;
/* Open the file on the host */
fd = semihosting_trap(SYSOPEN, &open);
if (fd == -1)
return semihosting_errno();
return fd;
}
/**
* struct semihosting_rdwr_s - Arguments for read and write
* @fd: A file descriptor returned from semihosting_open()
* @memp: Pointer to a buffer of memory of at least @len bytes
* @len: The number of bytes to read or write
*/
struct semihosting_rdwr_s {
long fd;
void *memp;
size_t len;
};
static long semihosting_read(long fd, void *memp, size_t len)
{
long ret;
struct semihosting_rdwr_s read;
read.fd = fd;
read.memp = memp;
read.len = len;
ret = semihosting_trap(SYSREAD, &read);
if (ret < 0)
return semihosting_errno();
return len - ret;
}
static long semihosting_write(long fd, const void *memp, size_t len)
{
long ret;
struct semihosting_rdwr_s write;
write.fd = fd;
write.memp = (void *)memp;
write.len = len;
ret = semihosting_trap(SYSWRITE, &write);
if (ret < 0)
return semihosting_errno();
return len - ret;
}
/* clang-format on */
static unsigned long semihosting_puts(const char *str, unsigned long len)
{
char ch;
long ret;
unsigned long i;
if (semihosting_outfd < 0) {
for (i = 0; i < len; i++) {
ch = str[i];
semihosting_trap(SYSWRITEC, &ch);
}
ret = len;
} else
ret = semihosting_write(semihosting_outfd, str, len);
return (ret < 0) ? 0 : ret;
}
static int semihosting_getc(void)
{
char ch = 0;
int ret;
if (semihosting_infd < 0) {
ret = semihosting_trap(SYSREADC, NULL);
ret = ret < 0 ? -1 : ret;
} else
ret = semihosting_read(semihosting_infd, &ch, 1) > 0 ? ch : -1;
return ret;
}
static struct sbi_console_device semihosting_console = {
.name = "semihosting",
.console_puts = semihosting_puts,
.console_getc = semihosting_getc
};
int semihosting_init(void)
{
semihosting_infd = semihosting_open(":tt", MODE_READ);
semihosting_outfd = semihosting_open(":tt", MODE_WRITE);
sbi_console_set_device(&semihosting_console);
return 0;
}
|