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 "hw/semihosting/semihost.h"
33 #include "qapi/error.h"
42 TARGET_SYS_lseek
= 19,
43 TARGET_SYS_select_one
= 29,
45 TARGET_SYS_argc
= 1000,
46 TARGET_SYS_argv_sz
= 1001,
47 TARGET_SYS_argv
= 1002,
48 TARGET_SYS_memset
= 1004,
54 SELECT_ONE_EXCEPT
= 3,
96 static uint32_t errno_h2g(int host_errno
)
98 static const uint32_t guest_errno
[] = {
99 [EPERM
] = TARGET_EPERM
,
100 [ENOENT
] = TARGET_ENOENT
,
101 [ESRCH
] = TARGET_ESRCH
,
102 [EINTR
] = TARGET_EINTR
,
104 [ENXIO
] = TARGET_ENXIO
,
105 [E2BIG
] = TARGET_E2BIG
,
106 [ENOEXEC
] = TARGET_ENOEXEC
,
107 [EBADF
] = TARGET_EBADF
,
108 [ECHILD
] = TARGET_ECHILD
,
109 [EAGAIN
] = TARGET_EAGAIN
,
110 [ENOMEM
] = TARGET_ENOMEM
,
111 [EACCES
] = TARGET_EACCES
,
112 [EFAULT
] = TARGET_EFAULT
,
114 [ENOTBLK
] = TARGET_ENOTBLK
,
116 [EBUSY
] = TARGET_EBUSY
,
117 [EEXIST
] = TARGET_EEXIST
,
118 [EXDEV
] = TARGET_EXDEV
,
119 [ENODEV
] = TARGET_ENODEV
,
120 [ENOTDIR
] = TARGET_ENOTDIR
,
121 [EISDIR
] = TARGET_EISDIR
,
122 [EINVAL
] = TARGET_EINVAL
,
123 [ENFILE
] = TARGET_ENFILE
,
124 [EMFILE
] = TARGET_EMFILE
,
125 [ENOTTY
] = TARGET_ENOTTY
,
127 [ETXTBSY
] = TARGET_ETXTBSY
,
129 [EFBIG
] = TARGET_EFBIG
,
130 [ENOSPC
] = TARGET_ENOSPC
,
131 [ESPIPE
] = TARGET_ESPIPE
,
132 [EROFS
] = TARGET_EROFS
,
133 [EMLINK
] = TARGET_EMLINK
,
134 [EPIPE
] = TARGET_EPIPE
,
135 [EDOM
] = TARGET_EDOM
,
136 [ERANGE
] = TARGET_ERANGE
,
137 [ENOSYS
] = TARGET_ENOSYS
,
139 [ELOOP
] = TARGET_ELOOP
,
143 if (host_errno
== 0) {
145 } else if (host_errno
> 0 && host_errno
< ARRAY_SIZE(guest_errno
) &&
146 guest_errno
[host_errno
]) {
147 return guest_errno
[host_errno
];
149 return TARGET_EINVAL
;
153 typedef struct XtensaSimConsole
{
161 static XtensaSimConsole
*sim_console
;
163 static IOCanReadHandler sim_console_can_read
;
164 static int sim_console_can_read(void *opaque
)
166 XtensaSimConsole
*p
= opaque
;
168 return sizeof(p
->input
.buffer
) - p
->input
.offset
;
171 static IOReadHandler sim_console_read
;
172 static void sim_console_read(void *opaque
, const uint8_t *buf
, int size
)
174 XtensaSimConsole
*p
= opaque
;
175 size_t copy
= sizeof(p
->input
.buffer
) - p
->input
.offset
;
180 memcpy(p
->input
.buffer
+ p
->input
.offset
, buf
, copy
);
181 p
->input
.offset
+= copy
;
184 void xtensa_sim_open_console(Chardev
*chr
)
186 static XtensaSimConsole console
;
188 qemu_chr_fe_init(&console
.be
, chr
, &error_abort
);
189 qemu_chr_fe_set_handlers(&console
.be
,
190 sim_console_can_read
,
192 NULL
, NULL
, &console
,
194 sim_console
= &console
;
197 void HELPER(simcall
)(CPUXtensaState
*env
)
199 CPUState
*cs
= env_cpu(env
);
200 uint32_t *regs
= env
->regs
;
203 case TARGET_SYS_exit
:
207 case TARGET_SYS_read
:
208 case TARGET_SYS_write
:
210 bool is_write
= regs
[2] == TARGET_SYS_write
;
211 uint32_t fd
= regs
[3];
212 uint32_t vaddr
= regs
[4];
213 uint32_t len
= regs
[5];
214 uint32_t len_done
= 0;
217 hwaddr paddr
= cpu_get_phys_page_debug(cs
, vaddr
);
219 TARGET_PAGE_SIZE
- (vaddr
& (TARGET_PAGE_SIZE
- 1));
220 uint32_t io_sz
= page_left
< len
? page_left
: len
;
222 void *buf
= cpu_physical_memory_map(paddr
, &sz
, !is_write
);
229 if (fd
< 3 && sim_console
) {
230 if (is_write
&& (fd
== 1 || fd
== 2)) {
231 io_done
= qemu_chr_fe_write_all(&sim_console
->be
,
233 regs
[3] = errno_h2g(errno
);
234 } else if (!is_write
&& fd
== 0) {
235 if (sim_console
->input
.offset
) {
236 io_done
= sim_console
->input
.offset
;
237 if (io_sz
< io_done
) {
240 memcpy(buf
, sim_console
->input
.buffer
, io_done
);
241 memmove(sim_console
->input
.buffer
,
242 sim_console
->input
.buffer
+ io_done
,
243 sim_console
->input
.offset
- io_done
);
244 sim_console
->input
.offset
-= io_done
;
245 qemu_chr_fe_accept_input(&sim_console
->be
);
248 regs
[3] = TARGET_EAGAIN
;
251 qemu_log_mask(LOG_GUEST_ERROR
,
252 "%s fd %d is not supported with chardev console\n",
254 "writing to" : "reading from", fd
);
256 regs
[3] = TARGET_EBADF
;
260 write(fd
, buf
, io_sz
) :
261 read(fd
, buf
, io_sz
);
262 regs
[3] = errno_h2g(errno
);
268 cpu_physical_memory_unmap(buf
, sz
, !is_write
, io_done
);
271 regs
[3] = TARGET_EINVAL
;
281 if (io_done
< io_sz
) {
289 case TARGET_SYS_open
:
295 for (i
= 0; i
< ARRAY_SIZE(name
); ++i
) {
296 rc
= cpu_memory_rw_debug(cs
, regs
[3] + i
,
297 (uint8_t *)name
+ i
, 1, 0);
298 if (rc
!= 0 || name
[i
] == 0) {
303 if (rc
== 0 && i
< ARRAY_SIZE(name
)) {
304 regs
[2] = open(name
, regs
[4], regs
[5]);
305 regs
[3] = errno_h2g(errno
);
308 regs
[3] = TARGET_EINVAL
;
313 case TARGET_SYS_close
:
315 regs
[2] = regs
[3] = 0;
317 regs
[2] = close(regs
[3]);
318 regs
[3] = errno_h2g(errno
);
322 case TARGET_SYS_lseek
:
323 regs
[2] = lseek(regs
[3], (off_t
)(int32_t)regs
[4], regs
[5]);
324 regs
[3] = errno_h2g(errno
);
327 case TARGET_SYS_select_one
:
329 uint32_t fd
= regs
[3];
330 uint32_t rq
= regs
[4];
331 uint32_t target_tv
= regs
[5];
332 uint32_t target_tvv
[2];
334 struct timeval tv
= {0};
337 cpu_memory_rw_debug(cs
, target_tv
,
338 (uint8_t *)target_tvv
, sizeof(target_tvv
), 0);
339 tv
.tv_sec
= (int32_t)tswap32(target_tvv
[0]);
340 tv
.tv_usec
= (int32_t)tswap32(target_tvv
[1]);
342 if (fd
< 3 && sim_console
) {
343 if ((fd
== 1 || fd
== 2) && rq
== SELECT_ONE_WRITE
) {
345 } else if (fd
== 0 && rq
== SELECT_ONE_READ
) {
346 regs
[2] = sim_console
->input
.offset
> 0;
356 regs
[2] = select(fd
+ 1,
357 rq
== SELECT_ONE_READ
? &fdset
: NULL
,
358 rq
== SELECT_ONE_WRITE
? &fdset
: NULL
,
359 rq
== SELECT_ONE_EXCEPT
? &fdset
: NULL
,
360 target_tv
? &tv
: NULL
);
361 regs
[3] = errno_h2g(errno
);
366 case TARGET_SYS_argc
:
367 regs
[2] = semihosting_get_argc();
371 case TARGET_SYS_argv_sz
:
373 int argc
= semihosting_get_argc();
374 int sz
= (argc
+ 1) * sizeof(uint32_t);
377 for (i
= 0; i
< argc
; ++i
) {
378 sz
+= 1 + strlen(semihosting_get_arg(i
));
385 case TARGET_SYS_argv
:
387 int argc
= semihosting_get_argc();
388 int str_offset
= (argc
+ 1) * sizeof(uint32_t);
392 for (i
= 0; i
< argc
; ++i
) {
393 const char *str
= semihosting_get_arg(i
);
394 int str_size
= strlen(str
) + 1;
396 argptr
= tswap32(regs
[3] + str_offset
);
398 cpu_memory_rw_debug(cs
,
399 regs
[3] + i
* sizeof(uint32_t),
400 (uint8_t *)&argptr
, sizeof(argptr
), 1);
401 cpu_memory_rw_debug(cs
,
402 regs
[3] + str_offset
,
403 (uint8_t *)str
, str_size
, 1);
404 str_offset
+= str_size
;
407 cpu_memory_rw_debug(cs
,
408 regs
[3] + i
* sizeof(uint32_t),
409 (uint8_t *)&argptr
, sizeof(argptr
), 1);
414 case TARGET_SYS_memset
:
416 uint32_t base
= regs
[3];
417 uint32_t sz
= regs
[5];
421 void *buf
= cpu_physical_memory_map(base
, &len
, 1);
424 memset(buf
, regs
[4], len
);
425 cpu_physical_memory_unmap(buf
, len
, 1, len
);
438 qemu_log_mask(LOG_GUEST_ERROR
, "%s(%d): not implemented\n", __func__
, regs
[2]);
440 regs
[3] = TARGET_ENOSYS
;