2 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Open Source and Linux Lab nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "qemu/osdep.h"
30 #include "chardev/char-fe.h"
31 #include "exec/helper-proto.h"
32 #include "exec/semihost.h"
33 #include "qapi/error.h"
35 #include "sysemu/sysemu.h"
37 static CharBackend *xtensa_sim_console;
45 TARGET_SYS_lseek = 19,
46 TARGET_SYS_select_one = 29,
48 TARGET_SYS_argc = 1000,
49 TARGET_SYS_argv_sz = 1001,
50 TARGET_SYS_argv = 1002,
51 TARGET_SYS_memset = 1004,
57 SELECT_ONE_EXCEPT = 3,
99 static uint32_t errno_h2g(int host_errno)
101 static const uint32_t guest_errno[] = {
102 [EPERM] = TARGET_EPERM,
103 [ENOENT] = TARGET_ENOENT,
104 [ESRCH] = TARGET_ESRCH,
105 [EINTR] = TARGET_EINTR,
107 [ENXIO] = TARGET_ENXIO,
108 [E2BIG] = TARGET_E2BIG,
109 [ENOEXEC] = TARGET_ENOEXEC,
110 [EBADF] = TARGET_EBADF,
111 [ECHILD] = TARGET_ECHILD,
112 [EAGAIN] = TARGET_EAGAIN,
113 [ENOMEM] = TARGET_ENOMEM,
114 [EACCES] = TARGET_EACCES,
115 [EFAULT] = TARGET_EFAULT,
117 [ENOTBLK] = TARGET_ENOTBLK,
119 [EBUSY] = TARGET_EBUSY,
120 [EEXIST] = TARGET_EEXIST,
121 [EXDEV] = TARGET_EXDEV,
122 [ENODEV] = TARGET_ENODEV,
123 [ENOTDIR] = TARGET_ENOTDIR,
124 [EISDIR] = TARGET_EISDIR,
125 [EINVAL] = TARGET_EINVAL,
126 [ENFILE] = TARGET_ENFILE,
127 [EMFILE] = TARGET_EMFILE,
128 [ENOTTY] = TARGET_ENOTTY,
130 [ETXTBSY] = TARGET_ETXTBSY,
132 [EFBIG] = TARGET_EFBIG,
133 [ENOSPC] = TARGET_ENOSPC,
134 [ESPIPE] = TARGET_ESPIPE,
135 [EROFS] = TARGET_EROFS,
136 [EMLINK] = TARGET_EMLINK,
137 [EPIPE] = TARGET_EPIPE,
138 [EDOM] = TARGET_EDOM,
139 [ERANGE] = TARGET_ERANGE,
140 [ENOSYS] = TARGET_ENOSYS,
142 [ELOOP] = TARGET_ELOOP,
146 if (host_errno == 0) {
148 } else if (host_errno > 0 && host_errno < ARRAY_SIZE(guest_errno) &&
149 guest_errno[host_errno]) {
150 return guest_errno[host_errno];
152 return TARGET_EINVAL;
156 void xtensa_sim_open_console(Chardev *chr)
158 static CharBackend console;
160 qemu_chr_fe_init(&console, chr, &error_abort);
161 qemu_chr_fe_set_handlers(&console, NULL, NULL, NULL, NULL, NULL, NULL, true);
162 xtensa_sim_console = &console;
165 void HELPER(simcall)(CPUXtensaState *env)
167 CPUState *cs = CPU(xtensa_env_get_cpu(env));
168 uint32_t *regs = env->regs;
171 case TARGET_SYS_exit:
172 qemu_log("exit(%d) simcall\n", regs[3]);
176 case TARGET_SYS_read:
177 case TARGET_SYS_write:
179 bool is_write = regs[2] == TARGET_SYS_write;
180 uint32_t fd = regs[3];
181 uint32_t vaddr = regs[4];
182 uint32_t len = regs[5];
183 uint32_t len_done = 0;
186 hwaddr paddr = cpu_get_phys_page_debug(cs, vaddr);
188 TARGET_PAGE_SIZE - (vaddr & (TARGET_PAGE_SIZE - 1));
189 uint32_t io_sz = page_left < len ? page_left : len;
191 void *buf = cpu_physical_memory_map(paddr, &sz, !is_write);
198 if (fd < 3 && xtensa_sim_console) {
199 if (is_write && (fd == 1 || fd == 2)) {
200 io_done = qemu_chr_fe_write_all(xtensa_sim_console,
202 regs[3] = errno_h2g(errno);
204 qemu_log_mask(LOG_GUEST_ERROR,
205 "%s fd %d is not supported with chardev console\n",
207 "writing to" : "reading from", fd);
209 regs[3] = TARGET_EBADF;
213 write(fd, buf, io_sz) :
214 read(fd, buf, io_sz);
215 regs[3] = errno_h2g(errno);
221 cpu_physical_memory_unmap(buf, sz, !is_write, io_done);
224 regs[3] = TARGET_EINVAL;
234 if (io_done < io_sz) {
242 case TARGET_SYS_open:
248 for (i = 0; i < ARRAY_SIZE(name); ++i) {
249 rc = cpu_memory_rw_debug(cs, regs[3] + i,
250 (uint8_t *)name + i, 1, 0);
251 if (rc != 0 || name[i] == 0) {
256 if (rc == 0 && i < ARRAY_SIZE(name)) {
257 regs[2] = open(name, regs[4], regs[5]);
258 regs[3] = errno_h2g(errno);
261 regs[3] = TARGET_EINVAL;
266 case TARGET_SYS_close:
268 regs[2] = regs[3] = 0;
270 regs[2] = close(regs[3]);
271 regs[3] = errno_h2g(errno);
275 case TARGET_SYS_lseek:
276 regs[2] = lseek(regs[3], (off_t)(int32_t)regs[4], regs[5]);
277 regs[3] = errno_h2g(errno);
280 case TARGET_SYS_select_one:
282 uint32_t fd = regs[3];
283 uint32_t rq = regs[4];
284 uint32_t target_tv = regs[5];
285 uint32_t target_tvv[2];
287 struct timeval tv = {0};
290 cpu_memory_rw_debug(cs, target_tv,
291 (uint8_t *)target_tvv, sizeof(target_tvv), 0);
292 tv.tv_sec = (int32_t)tswap32(target_tvv[0]);
293 tv.tv_usec = (int32_t)tswap32(target_tvv[1]);
295 if (fd < 3 && xtensa_sim_console) {
296 if ((fd == 1 || fd == 2) && rq == SELECT_ONE_WRITE) {
307 regs[2] = select(fd + 1,
308 rq == SELECT_ONE_READ ? &fdset : NULL,
309 rq == SELECT_ONE_WRITE ? &fdset : NULL,
310 rq == SELECT_ONE_EXCEPT ? &fdset : NULL,
311 target_tv ? &tv : NULL);
312 regs[3] = errno_h2g(errno);
317 case TARGET_SYS_argc:
318 regs[2] = semihosting_get_argc();
322 case TARGET_SYS_argv_sz:
324 int argc = semihosting_get_argc();
325 int sz = (argc + 1) * sizeof(uint32_t);
328 for (i = 0; i < argc; ++i) {
329 sz += 1 + strlen(semihosting_get_arg(i));
336 case TARGET_SYS_argv:
338 int argc = semihosting_get_argc();
339 int str_offset = (argc + 1) * sizeof(uint32_t);
343 for (i = 0; i < argc; ++i) {
344 const char *str = semihosting_get_arg(i);
345 int str_size = strlen(str) + 1;
347 argptr = tswap32(regs[3] + str_offset);
349 cpu_memory_rw_debug(cs,
350 regs[3] + i * sizeof(uint32_t),
351 (uint8_t *)&argptr, sizeof(argptr), 1);
352 cpu_memory_rw_debug(cs,
353 regs[3] + str_offset,
354 (uint8_t *)str, str_size, 1);
355 str_offset += str_size;
358 cpu_memory_rw_debug(cs,
359 regs[3] + i * sizeof(uint32_t),
360 (uint8_t *)&argptr, sizeof(argptr), 1);
365 case TARGET_SYS_memset:
367 uint32_t base = regs[3];
368 uint32_t sz = regs[5];
372 void *buf = cpu_physical_memory_map(base, &len, 1);
375 memset(buf, regs[4], len);
376 cpu_physical_memory_unmap(buf, len, 1, len);
389 qemu_log_mask(LOG_GUEST_ERROR, "%s(%d): not implemented\n", __func__, regs[2]);
391 regs[3] = TARGET_ENOSYS;