Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 2 | /* |
Sean Anderson | 12a05b3 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 3 | * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com> |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 4 | * Copyright 2014 Broadcom Corporation |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
Sean Anderson | 12a05b3 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 8 | * 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 Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 11 | */ |
| 12 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Sean Anderson | b10f724 | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 14 | #include <semihosting.h> |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 15 | |
| 16 | #define SYSOPEN 0x01 |
| 17 | #define SYSCLOSE 0x02 |
Sean Anderson | 3ea744e | 2022-03-22 16:59:23 -0400 | [diff] [blame] | 18 | #define SYSWRITEC 0x03 |
| 19 | #define SYSWRITE0 0x04 |
Sean Anderson | 12a05b3 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 20 | #define SYSWRITE 0x05 |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 21 | #define SYSREAD 0x06 |
Sean Anderson | 3ea744e | 2022-03-22 16:59:23 -0400 | [diff] [blame] | 22 | #define SYSREADC 0x07 |
Sean Anderson | 385d69d | 2022-03-22 16:59:30 -0400 | [diff] [blame] | 23 | #define SYSISERROR 0x08 |
Sean Anderson | 12a05b3 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 24 | #define SYSSEEK 0x0A |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 25 | #define SYSFLEN 0x0C |
Sean Anderson | 80e62cc | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 26 | #define SYSERRNO 0x13 |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 27 | |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 28 | /* |
| 29 | * Call the handler |
| 30 | */ |
Linus Walleij | e769f68 | 2015-03-23 11:06:10 +0100 | [diff] [blame] | 31 | static noinline long smh_trap(unsigned int sysnum, void *addr) |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 32 | { |
Linus Walleij | 4e1ef15 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 33 | register long result asm("r0"); |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 34 | #if defined(CONFIG_ARM64) |
| 35 | asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr)); |
Vadzim Dambrouski | 432a624 | 2015-10-19 19:40:14 +0300 | [diff] [blame] | 36 | #elif defined(CONFIG_CPU_V7M) |
| 37 | asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr)); |
Darwin Rambo | 261d276 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 38 | #else |
| 39 | /* Note - untested placeholder */ |
| 40 | asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr)); |
| 41 | #endif |
| 42 | return result; |
| 43 | } |
| 44 | |
Sean Anderson | 385d69d | 2022-03-22 16:59:30 -0400 | [diff] [blame] | 45 | #if CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK) |
| 46 | static bool _semihosting_enabled = true; |
| 47 | static bool try_semihosting = true; |
| 48 | |
| 49 | bool 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 | |
| 59 | void disable_semihosting(void) |
| 60 | { |
| 61 | _semihosting_enabled = false; |
| 62 | } |
| 63 | #endif |
| 64 | |
Sean Anderson | 80e62cc | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 65 | /** |
| 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 Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 73 | */ |
Sean Anderson | 80e62cc | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 74 | static 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 Anderson | eff77c3 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 83 | long smh_open(const char *fname, enum smh_open_mode mode) |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 84 | { |
| 85 | long fd; |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 86 | struct smh_open_s { |
| 87 | const char *fname; |
| 88 | unsigned long mode; |
| 89 | size_t len; |
| 90 | } open; |
| 91 | |
Sean Anderson | eff77c3 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 92 | debug("%s: file \'%s\', mode \'%u\'\n", __func__, fname, mode); |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 93 | |
| 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 Anderson | 80e62cc | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 101 | return smh_errno(); |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 102 | return fd; |
| 103 | } |
| 104 | |
Sean Anderson | 12a05b3 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 105 | /** |
| 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 | */ |
| 111 | struct smh_rdwr_s { |
| 112 | long fd; |
| 113 | void *memp; |
| 114 | size_t len; |
| 115 | }; |
| 116 | |
Sean Anderson | b10f724 | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 117 | long smh_read(long fd, void *memp, size_t len) |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 118 | { |
| 119 | long ret; |
Sean Anderson | 12a05b3 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 120 | struct smh_rdwr_s read; |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 121 | |
Vadzim Dambrouski | 7bdf75c | 2015-10-19 19:40:15 +0300 | [diff] [blame] | 122 | debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len); |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 123 | |
| 124 | read.fd = fd; |
| 125 | read.memp = memp; |
| 126 | read.len = len; |
| 127 | |
| 128 | ret = smh_trap(SYSREAD, &read); |
Sean Anderson | 80e62cc | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 129 | if (ret < 0) |
| 130 | return smh_errno(); |
| 131 | return len - ret; |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Sean Anderson | 12a05b3 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 134 | long 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 Anderson | b10f724 | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 152 | long smh_close(long fd) |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 153 | { |
| 154 | long ret; |
| 155 | |
| 156 | debug("%s: fd %ld\n", __func__, fd); |
| 157 | |
| 158 | ret = smh_trap(SYSCLOSE, &fd); |
| 159 | if (ret == -1) |
Sean Anderson | 80e62cc | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 160 | return smh_errno(); |
| 161 | return 0; |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Sean Anderson | b10f724 | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 164 | long smh_flen(long fd) |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 165 | { |
| 166 | long ret; |
| 167 | |
| 168 | debug("%s: fd %ld\n", __func__, fd); |
| 169 | |
| 170 | ret = smh_trap(SYSFLEN, &fd); |
| 171 | if (ret == -1) |
Sean Anderson | 80e62cc | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 172 | return smh_errno(); |
Linus Walleij | 9be5c66 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
Sean Anderson | 12a05b3 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 176 | long 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 Anderson | 3ea744e | 2022-03-22 16:59:23 -0400 | [diff] [blame] | 194 | |
| 195 | int smh_getc(void) |
| 196 | { |
| 197 | return smh_trap(SYSREADC, NULL); |
| 198 | } |
| 199 | |
| 200 | void smh_putc(char ch) |
| 201 | { |
| 202 | smh_trap(SYSWRITEC, &ch); |
| 203 | } |
| 204 | |
| 205 | void smh_puts(const char *s) |
| 206 | { |
| 207 | smh_trap(SYSWRITE0, (char *)s); |
| 208 | } |