Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 2 | /* |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 3 | * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com> |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 4 | * Copyright 2014 Broadcom Corporation |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 7 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 9 | #include <semihosting.h> |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 10 | |
| 11 | #define SYSOPEN 0x01 |
| 12 | #define SYSCLOSE 0x02 |
Sean Anderson | 1e9f443 | 2022-03-22 16:59:23 -0400 | [diff] [blame] | 13 | #define SYSWRITEC 0x03 |
| 14 | #define SYSWRITE0 0x04 |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 15 | #define SYSWRITE 0x05 |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 16 | #define SYSREAD 0x06 |
Sean Anderson | 1e9f443 | 2022-03-22 16:59:23 -0400 | [diff] [blame] | 17 | #define SYSREADC 0x07 |
Sean Anderson | 52cf0f0 | 2022-03-22 16:59:30 -0400 | [diff] [blame] | 18 | #define SYSISERROR 0x08 |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 19 | #define SYSSEEK 0x0A |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 20 | #define SYSFLEN 0x0C |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 21 | #define SYSERRNO 0x13 |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 22 | |
Andre Przywara | b94ebde | 2022-10-05 17:38:48 +0100 | [diff] [blame] | 23 | /* |
| 24 | * Macro to force the compiler to *populate* memory (for an array or struct) |
| 25 | * before passing the pointer to an inline assembly call. |
| 26 | */ |
| 27 | #define USE_PTR(ptr) *(const char (*)[]) (ptr) |
| 28 | |
Andre Przywara | c1b6d76 | 2022-10-05 17:38:47 +0100 | [diff] [blame] | 29 | #if defined(CONFIG_ARM64) |
| 30 | #define SMH_TRAP "hlt #0xf000" |
| 31 | #elif defined(CONFIG_CPU_V7M) |
| 32 | #define SMH_TRAP "bkpt #0xAB" |
| 33 | #elif defined(CONFIG_SYS_THUMB_BUILD) |
| 34 | #define SMH_TRAP "svc #0xab" |
| 35 | #else |
| 36 | #define SMH_TRAP "svc #0x123456" |
| 37 | #endif |
| 38 | |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 39 | /* |
| 40 | * Call the handler |
| 41 | */ |
Andre Przywara | 0cd671b | 2022-10-05 17:38:49 +0100 | [diff] [blame] | 42 | static long smh_trap(unsigned int sysnum, void *addr) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 43 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 44 | register long result asm("r0"); |
Andre Przywara | 0cd671b | 2022-10-05 17:38:49 +0100 | [diff] [blame] | 45 | register void *_addr asm("r1") = addr; |
Andre Przywara | c1b6d76 | 2022-10-05 17:38:47 +0100 | [diff] [blame] | 46 | |
Andre Przywara | b94ebde | 2022-10-05 17:38:48 +0100 | [diff] [blame] | 47 | /* |
| 48 | * We need a memory clobber (aka compiler barrier) for two reasons: |
| 49 | * - The compiler needs to populate any data structures pointed to |
| 50 | * by "addr" *before* the trap instruction is called. |
| 51 | * - At least the SYSREAD function puts the result into memory pointed |
| 52 | * to by "addr", so the compiler must not use a cached version of |
| 53 | * the previous content, after the call has finished. |
| 54 | */ |
Andre Przywara | c1b6d76 | 2022-10-05 17:38:47 +0100 | [diff] [blame] | 55 | asm volatile (SMH_TRAP |
| 56 | : "=r" (result) |
Andre Przywara | 0cd671b | 2022-10-05 17:38:49 +0100 | [diff] [blame] | 57 | : "0"(sysnum), "r"(USE_PTR(_addr)) |
Andre Przywara | c1b6d76 | 2022-10-05 17:38:47 +0100 | [diff] [blame] | 58 | : "memory"); |
| 59 | |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 60 | return result; |
| 61 | } |
| 62 | |
Sean Anderson | 52cf0f0 | 2022-03-22 16:59:30 -0400 | [diff] [blame] | 63 | #if CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK) |
| 64 | static bool _semihosting_enabled = true; |
| 65 | static bool try_semihosting = true; |
| 66 | |
| 67 | bool semihosting_enabled(void) |
| 68 | { |
| 69 | if (try_semihosting) { |
| 70 | smh_trap(SYSERRNO, NULL); |
| 71 | try_semihosting = false; |
| 72 | } |
| 73 | |
| 74 | return _semihosting_enabled; |
| 75 | } |
| 76 | |
| 77 | void disable_semihosting(void) |
| 78 | { |
| 79 | _semihosting_enabled = false; |
| 80 | } |
| 81 | #endif |
| 82 | |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 83 | /** |
| 84 | * smh_errno() - Read the host's errno |
| 85 | * |
| 86 | * This gets the value of the host's errno and negates it. The host's errno may |
| 87 | * or may not be set, so only call this function if a previous semihosting call |
| 88 | * has failed. |
| 89 | * |
| 90 | * Return: a negative error value |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 91 | */ |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 92 | static int smh_errno(void) |
| 93 | { |
| 94 | long ret = smh_trap(SYSERRNO, NULL); |
| 95 | |
| 96 | if (ret > 0 && ret < INT_MAX) |
| 97 | return -ret; |
| 98 | return -EIO; |
| 99 | } |
| 100 | |
Sean Anderson | a3a6c69 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 101 | long smh_open(const char *fname, enum smh_open_mode mode) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 102 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 103 | long fd; |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 104 | struct smh_open_s { |
| 105 | const char *fname; |
| 106 | unsigned long mode; |
| 107 | size_t len; |
| 108 | } open; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 109 | |
Sean Anderson | a3a6c69 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 110 | debug("%s: file \'%s\', mode \'%u\'\n", __func__, fname, mode); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 111 | |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 112 | open.fname = fname; |
| 113 | open.len = strlen(fname); |
| 114 | open.mode = mode; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 115 | |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 116 | /* Open the file on the host */ |
| 117 | fd = smh_trap(SYSOPEN, &open); |
| 118 | if (fd == -1) |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 119 | return smh_errno(); |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 120 | return fd; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 123 | /** |
| 124 | * struct smg_rdwr_s - Arguments for read and write |
| 125 | * @fd: A file descriptor returned from smh_open() |
| 126 | * @memp: Pointer to a buffer of memory of at least @len bytes |
| 127 | * @len: The number of bytes to read or write |
| 128 | */ |
| 129 | struct smh_rdwr_s { |
| 130 | long fd; |
| 131 | void *memp; |
| 132 | size_t len; |
| 133 | }; |
| 134 | |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 135 | long smh_read(long fd, void *memp, size_t len) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 136 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 137 | long ret; |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 138 | struct smh_rdwr_s read; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 139 | |
Vadzim Dambrouski | 2738215 | 2015-10-19 19:40:15 +0300 | [diff] [blame] | 140 | debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 141 | |
| 142 | read.fd = fd; |
| 143 | read.memp = memp; |
| 144 | read.len = len; |
| 145 | |
| 146 | ret = smh_trap(SYSREAD, &read); |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 147 | if (ret < 0) |
| 148 | return smh_errno(); |
| 149 | return len - ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 152 | long smh_write(long fd, const void *memp, size_t len, ulong *written) |
| 153 | { |
| 154 | long ret; |
| 155 | struct smh_rdwr_s write; |
| 156 | |
| 157 | debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len); |
| 158 | |
| 159 | write.fd = fd; |
| 160 | write.memp = (void *)memp; |
| 161 | write.len = len; |
| 162 | |
| 163 | ret = smh_trap(SYSWRITE, &write); |
| 164 | *written = len - ret; |
| 165 | if (ret) |
| 166 | return smh_errno(); |
| 167 | return 0; |
| 168 | } |
| 169 | |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 170 | long smh_close(long fd) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 171 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 172 | long ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 173 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 174 | debug("%s: fd %ld\n", __func__, fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 175 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 176 | ret = smh_trap(SYSCLOSE, &fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 177 | if (ret == -1) |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 178 | return smh_errno(); |
| 179 | return 0; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 182 | long smh_flen(long fd) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 183 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 184 | long ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 185 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 186 | debug("%s: fd %ld\n", __func__, fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 187 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 188 | ret = smh_trap(SYSFLEN, &fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 189 | if (ret == -1) |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 190 | return smh_errno(); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 191 | return ret; |
| 192 | } |
| 193 | |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 194 | long smh_seek(long fd, long pos) |
| 195 | { |
| 196 | long ret; |
| 197 | struct smh_seek_s { |
| 198 | long fd; |
| 199 | long pos; |
| 200 | } seek; |
| 201 | |
| 202 | debug("%s: fd %ld pos %ld\n", __func__, fd, pos); |
| 203 | |
| 204 | seek.fd = fd; |
| 205 | seek.pos = pos; |
| 206 | |
| 207 | ret = smh_trap(SYSSEEK, &seek); |
| 208 | if (ret) |
| 209 | return smh_errno(); |
| 210 | return 0; |
| 211 | } |
Sean Anderson | 1e9f443 | 2022-03-22 16:59:23 -0400 | [diff] [blame] | 212 | |
| 213 | int smh_getc(void) |
| 214 | { |
| 215 | return smh_trap(SYSREADC, NULL); |
| 216 | } |
| 217 | |
| 218 | void smh_putc(char ch) |
| 219 | { |
| 220 | smh_trap(SYSWRITEC, &ch); |
| 221 | } |
| 222 | |
| 223 | void smh_puts(const char *s) |
| 224 | { |
| 225 | smh_trap(SYSWRITE0, (char *)s); |
| 226 | } |