2 * Nios II Semihosting syscall interface.
3 * This code is derived from m68k-semi.c.
4 * The semihosting protocol implemented here is described in the
6 * https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=libgloss/nios2/nios2-semi.txt;hb=HEAD
8 * Copyright (c) 2017-2019 Mentor Graphics
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/osdep.h"
26 #include "gdbstub/syscalls.h"
27 #include "gdbstub/helpers.h"
28 #include "semihosting/syscalls.h"
29 #include "semihosting/softmmu-uaccess.h"
33 #define HOSTED_INIT_SIM 1
35 #define HOSTED_CLOSE 3
37 #define HOSTED_WRITE 5
38 #define HOSTED_LSEEK 6
39 #define HOSTED_RENAME 7
40 #define HOSTED_UNLINK 8
42 #define HOSTED_FSTAT 10
43 #define HOSTED_GETTIMEOFDAY 11
44 #define HOSTED_ISATTY 12
45 #define HOSTED_SYSTEM 13
47 static int host_to_gdb_errno(int err
)
49 #define E(X) case E##X: return GDB_E##X
76 static void nios2_semi_u32_cb(CPUState
*cs
, uint64_t ret
, int err
)
78 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
79 CPUNios2State
*env
= &cpu
->env
;
80 target_ulong args
= env
->regs
[R_ARG1
];
82 if (put_user_u32(ret
, args
) ||
83 put_user_u32(host_to_gdb_errno(err
), args
+ 4)) {
85 * The nios2 semihosting ABI does not provide any way to report this
86 * error to the guest, so the best we can do is log it in qemu.
87 * It is always a guest error not to pass us a valid argument block.
89 qemu_log_mask(LOG_GUEST_ERROR
, "nios2-semihosting: return value "
90 "discarded because argument block not writable\n");
94 static void nios2_semi_u64_cb(CPUState
*cs
, uint64_t ret
, int err
)
96 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
97 CPUNios2State
*env
= &cpu
->env
;
98 target_ulong args
= env
->regs
[R_ARG1
];
100 if (put_user_u32(ret
>> 32, args
) ||
101 put_user_u32(ret
, args
+ 4) ||
102 put_user_u32(host_to_gdb_errno(err
), args
+ 8)) {
103 /* No way to report this via nios2 semihosting ABI; just log it */
104 qemu_log_mask(LOG_GUEST_ERROR
, "nios2-semihosting: return value "
105 "discarded because argument block not writable\n");
110 * Read the input value from the argument block; fail the semihosting
111 * call if the memory read fails.
113 #define GET_ARG(n) do { \
114 if (get_user_ual(arg ## n, args + (n) * 4)) { \
119 #define GET_ARG64(n) do { \
120 if (get_user_ual(arg ## n, args + (n) * 4)) { \
125 void do_nios2_semihosting(CPUNios2State
*env
)
127 CPUState
*cs
= env_cpu(env
);
130 target_ulong arg0
, arg1
, arg2
, arg3
;
132 nr
= env
->regs
[R_ARG0
];
133 args
= env
->regs
[R_ARG1
];
136 gdb_exit(env
->regs
[R_ARG0
]);
137 exit(env
->regs
[R_ARG0
]);
144 semihost_sys_open(cs
, nios2_semi_u32_cb
, arg0
, arg1
, arg2
, arg3
);
149 semihost_sys_close(cs
, nios2_semi_u32_cb
, arg0
);
156 semihost_sys_read(cs
, nios2_semi_u32_cb
, arg0
, arg1
, arg2
);
163 semihost_sys_write(cs
, nios2_semi_u32_cb
, arg0
, arg1
, arg2
);
171 semihost_sys_lseek(cs
, nios2_semi_u64_cb
, arg0
,
172 deposit64(arg2
, arg1
, 32, 32), arg3
);
180 semihost_sys_rename(cs
, nios2_semi_u32_cb
, arg0
, arg1
, arg2
, arg3
);
186 semihost_sys_remove(cs
, nios2_semi_u32_cb
, arg0
, arg1
);
193 semihost_sys_stat(cs
, nios2_semi_u32_cb
, arg0
, arg1
, arg2
);
199 semihost_sys_fstat(cs
, nios2_semi_u32_cb
, arg0
, arg1
);
202 case HOSTED_GETTIMEOFDAY
:
205 semihost_sys_gettimeofday(cs
, nios2_semi_u32_cb
, arg0
, arg1
);
210 semihost_sys_isatty(cs
, nios2_semi_u32_cb
, arg0
);
216 semihost_sys_system(cs
, nios2_semi_u32_cb
, arg0
, arg1
);
220 qemu_log_mask(LOG_GUEST_ERROR
, "nios2-semihosting: unsupported "
221 "semihosting syscall %d\n", nr
);
222 nios2_semi_u32_cb(cs
, -1, ENOSYS
);
226 nios2_semi_u32_cb(cs
, -1, EFAULT
);
229 nios2_semi_u64_cb(cs
, -1, EFAULT
);