]> git.proxmox.com Git - mirror_qemu.git/blame - target/m68k/m68k-semi.c
target: Unify QOM style
[mirror_qemu.git] / target / m68k / m68k-semi.c
CommitLineData
a87295e8
PB
1/*
2 * m68k/ColdFire Semihosting syscall interface
5fafdf24 3 *
a87295e8
PB
4 * Copyright (c) 2005-2007 CodeSourcery.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
8167ee88 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1321d844
PM
18 *
19 * The semihosting protocol implemented here is described in the
20 * libgloss sources:
21 * https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=libgloss/m68k/m68k-semi.txt;hb=HEAD
a87295e8
PB
22 */
23
d8416665 24#include "qemu/osdep.h"
a87295e8
PB
25
26#include "cpu.h"
c566080c 27#include "gdbstub/syscalls.h"
4ea5fe99 28#include "gdbstub/helpers.h"
95027250 29#include "semihosting/syscalls.h"
f14eced5 30#include "semihosting/uaccess.h"
5601d241 31#include "hw/boards.h"
63c91552 32#include "qemu/log.h"
a87295e8
PB
33
34#define HOSTED_EXIT 0
35#define HOSTED_INIT_SIM 1
36#define HOSTED_OPEN 2
37#define HOSTED_CLOSE 3
38#define HOSTED_READ 4
39#define HOSTED_WRITE 5
40#define HOSTED_LSEEK 6
41#define HOSTED_RENAME 7
42#define HOSTED_UNLINK 8
43#define HOSTED_STAT 9
44#define HOSTED_FSTAT 10
45#define HOSTED_GETTIMEOFDAY 11
46#define HOSTED_ISATTY 12
47#define HOSTED_SYSTEM 13
48
7327e602
RH
49static int host_to_gdb_errno(int err)
50{
51#define E(X) case E##X: return GDB_E##X
52 switch (err) {
53 E(PERM);
54 E(NOENT);
55 E(INTR);
56 E(BADF);
57 E(ACCES);
58 E(FAULT);
59 E(BUSY);
60 E(EXIST);
61 E(NODEV);
62 E(NOTDIR);
63 E(ISDIR);
64 E(INVAL);
65 E(NFILE);
66 E(MFILE);
67 E(FBIG);
68 E(NOSPC);
69 E(SPIPE);
70 E(ROFS);
71 E(NAMETOOLONG);
72 default:
73 return GDB_EUNKNOWN;
74 }
75#undef E
76}
77
ab294b6c 78static void m68k_semi_u32_cb(CPUState *cs, uint64_t ret, int err)
1073bfd8 79{
ab294b6c
RH
80 M68kCPU *cpu = M68K_CPU(cs);
81 CPUM68KState *env = &cpu->env;
82
1073bfd8
PM
83 target_ulong args = env->dregs[1];
84 if (put_user_u32(ret, args) ||
7327e602 85 put_user_u32(host_to_gdb_errno(err), args + 4)) {
808d77bc
LMP
86 /*
87 * The m68k semihosting ABI does not provide any way to report this
1073bfd8
PM
88 * error to the guest, so the best we can do is log it in qemu.
89 * It is always a guest error not to pass us a valid argument block.
90 */
91 qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value "
92 "discarded because argument block not writable\n");
93 }
94}
95
ab294b6c 96static void m68k_semi_u64_cb(CPUState *cs, uint64_t ret, int err)
1073bfd8 97{
ab294b6c
RH
98 M68kCPU *cpu = M68K_CPU(cs);
99 CPUM68KState *env = &cpu->env;
100
1073bfd8
PM
101 target_ulong args = env->dregs[1];
102 if (put_user_u32(ret >> 32, args) ||
103 put_user_u32(ret, args + 4) ||
7327e602 104 put_user_u32(host_to_gdb_errno(err), args + 8)) {
1073bfd8
PM
105 /* No way to report this via m68k semihosting ABI; just log it */
106 qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value "
107 "discarded because argument block not writable\n");
108 }
109}
110
808d77bc
LMP
111/*
112 * Read the input value from the argument block; fail the semihosting
7ba6c104
PM
113 * call if the memory read fails.
114 */
115#define GET_ARG(n) do { \
116 if (get_user_ual(arg ## n, args + (n) * 4)) { \
7ba6c104
PM
117 goto failed; \
118 } \
119} while (0)
120
95027250
RH
121#define GET_ARG64(n) do { \
122 if (get_user_ual(arg ## n, args + (n) * 4)) { \
123 goto failed64; \
124 } \
125} while (0)
126
127
a87295e8
PB
128void do_m68k_semihosting(CPUM68KState *env, int nr)
129{
ab294b6c 130 CPUState *cs = env_cpu(env);
a87295e8 131 uint32_t args;
7ba6c104 132 target_ulong arg0, arg1, arg2, arg3;
a87295e8
PB
133
134 args = env->dregs[1];
135 switch (nr) {
136 case HOSTED_EXIT:
ad9dcb20 137 gdb_exit(env->dregs[0]);
a87295e8 138 exit(env->dregs[0]);
95027250 139
a87295e8 140 case HOSTED_OPEN:
7ba6c104
PM
141 GET_ARG(0);
142 GET_ARG(1);
143 GET_ARG(2);
144 GET_ARG(3);
95027250 145 semihost_sys_open(cs, m68k_semi_u32_cb, arg0, arg1, arg2, arg3);
a87295e8 146 break;
95027250 147
a87295e8 148 case HOSTED_CLOSE:
95027250
RH
149 GET_ARG(0);
150 semihost_sys_close(cs, m68k_semi_u32_cb, arg0);
151 break;
152
a87295e8 153 case HOSTED_READ:
7ba6c104
PM
154 GET_ARG(0);
155 GET_ARG(1);
156 GET_ARG(2);
95027250 157 semihost_sys_read(cs, m68k_semi_u32_cb, arg0, arg1, arg2);
a87295e8 158 break;
95027250 159
a87295e8 160 case HOSTED_WRITE:
7ba6c104
PM
161 GET_ARG(0);
162 GET_ARG(1);
163 GET_ARG(2);
95027250 164 semihost_sys_write(cs, m68k_semi_u32_cb, arg0, arg1, arg2);
a87295e8 165 break;
95027250 166
a87295e8 167 case HOSTED_LSEEK:
95027250
RH
168 GET_ARG64(0);
169 GET_ARG64(1);
170 GET_ARG64(2);
171 GET_ARG64(3);
172 semihost_sys_lseek(cs, m68k_semi_u64_cb, arg0,
8caaae73 173 deposit64(arg2, 32, 32, arg1), arg3);
95027250
RH
174 break;
175
a87295e8 176 case HOSTED_RENAME:
7ba6c104
PM
177 GET_ARG(0);
178 GET_ARG(1);
179 GET_ARG(2);
180 GET_ARG(3);
95027250 181 semihost_sys_rename(cs, m68k_semi_u32_cb, arg0, arg1, arg2, arg3);
a87295e8 182 break;
95027250 183
a87295e8 184 case HOSTED_UNLINK:
7ba6c104
PM
185 GET_ARG(0);
186 GET_ARG(1);
95027250 187 semihost_sys_remove(cs, m68k_semi_u32_cb, arg0, arg1);
a87295e8 188 break;
95027250 189
a87295e8 190 case HOSTED_STAT:
7ba6c104
PM
191 GET_ARG(0);
192 GET_ARG(1);
193 GET_ARG(2);
95027250 194 semihost_sys_stat(cs, m68k_semi_u32_cb, arg0, arg1, arg2);
a87295e8 195 break;
95027250 196
a87295e8 197 case HOSTED_FSTAT:
7ba6c104
PM
198 GET_ARG(0);
199 GET_ARG(1);
95027250 200 semihost_sys_fstat(cs, m68k_semi_u32_cb, arg0, arg1);
a87295e8 201 break;
95027250 202
a87295e8 203 case HOSTED_GETTIMEOFDAY:
7ba6c104
PM
204 GET_ARG(0);
205 GET_ARG(1);
95027250 206 semihost_sys_gettimeofday(cs, m68k_semi_u32_cb, arg0, arg1);
a87295e8 207 break;
95027250 208
a87295e8 209 case HOSTED_ISATTY:
7ba6c104 210 GET_ARG(0);
95027250 211 semihost_sys_isatty(cs, m68k_semi_u32_cb, arg0);
a87295e8 212 break;
95027250 213
a87295e8 214 case HOSTED_SYSTEM:
7ba6c104
PM
215 GET_ARG(0);
216 GET_ARG(1);
95027250 217 semihost_sys_system(cs, m68k_semi_u32_cb, arg0, arg1);
a87295e8 218 break;
95027250 219
a87295e8 220 case HOSTED_INIT_SIM:
808d77bc
LMP
221 /*
222 * FIXME: This is wrong for boards where RAM does not start at
223 * address zero.
224 */
5601d241
PB
225 env->dregs[1] = current_machine->ram_size;
226 env->aregs[7] = current_machine->ram_size;
a87295e8 227 return;
95027250 228
a87295e8 229 default:
a8d92fd8 230 cpu_abort(env_cpu(env), "Unsupported semihosting syscall %d\n", nr);
95027250
RH
231
232 failed:
233 m68k_semi_u32_cb(cs, -1, EFAULT);
234 break;
235 failed64:
236 m68k_semi_u64_cb(cs, -1, EFAULT);
237 break;
a87295e8 238 }
a87295e8 239}