]> git.proxmox.com Git - qemu.git/blob - m68k-semi.c
suppressed tgetx and tputx (initial patch by Thayne Harbaugh)
[qemu.git] / m68k-semi.c
1 /*
2 * m68k/ColdFire Semihosting syscall interface
3 *
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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/time.h>
29 #include <time.h>
30
31 #include "cpu.h"
32 #if defined(CONFIG_USER_ONLY)
33 #include "qemu.h"
34 #define SEMIHOSTING_HEAP_SIZE (128 * 1024 * 1024)
35 #else
36 #include "vl.h"
37 #include "softmmu-semi.h"
38 #endif
39
40 #define HOSTED_EXIT 0
41 #define HOSTED_INIT_SIM 1
42 #define HOSTED_OPEN 2
43 #define HOSTED_CLOSE 3
44 #define HOSTED_READ 4
45 #define HOSTED_WRITE 5
46 #define HOSTED_LSEEK 6
47 #define HOSTED_RENAME 7
48 #define HOSTED_UNLINK 8
49 #define HOSTED_STAT 9
50 #define HOSTED_FSTAT 10
51 #define HOSTED_GETTIMEOFDAY 11
52 #define HOSTED_ISATTY 12
53 #define HOSTED_SYSTEM 13
54
55 typedef uint32_t gdb_mode_t;
56 typedef uint32_t gdb_time_t;
57
58 struct m68k_gdb_stat {
59 uint32_t gdb_st_dev; /* device */
60 uint32_t gdb_st_ino; /* inode */
61 gdb_mode_t gdb_st_mode; /* protection */
62 uint32_t gdb_st_nlink; /* number of hard links */
63 uint32_t gdb_st_uid; /* user ID of owner */
64 uint32_t gdb_st_gid; /* group ID of owner */
65 uint32_t gdb_st_rdev; /* device type (if inode device) */
66 uint64_t gdb_st_size; /* total size, in bytes */
67 uint64_t gdb_st_blksize; /* blocksize for filesystem I/O */
68 uint64_t gdb_st_blocks; /* number of blocks allocated */
69 gdb_time_t gdb_st_atime; /* time of last access */
70 gdb_time_t gdb_st_mtime; /* time of last modification */
71 gdb_time_t gdb_st_ctime; /* time of last change */
72 } __attribute__((packed));
73
74 struct gdb_timeval {
75 gdb_time_t tv_sec; /* second */
76 uint64_t tv_usec; /* microsecond */
77 } __attribute__((packed));
78
79 #define GDB_O_RDONLY 0x0
80 #define GDB_O_WRONLY 0x1
81 #define GDB_O_RDWR 0x2
82 #define GDB_O_APPEND 0x8
83 #define GDB_O_CREAT 0x200
84 #define GDB_O_TRUNC 0x400
85 #define GDB_O_EXCL 0x800
86
87 static int translate_openflags(int flags)
88 {
89 int hf;
90
91 if (flags & GDB_O_WRONLY)
92 hf = O_WRONLY;
93 else if (flags & GDB_O_RDWR)
94 hf = O_RDWR;
95 else
96 hf = O_RDONLY;
97
98 if (flags & GDB_O_APPEND) hf |= O_APPEND;
99 if (flags & GDB_O_CREAT) hf |= O_CREAT;
100 if (flags & GDB_O_TRUNC) hf |= O_TRUNC;
101 if (flags & GDB_O_EXCL) hf |= O_EXCL;
102
103 return hf;
104 }
105
106 static void translate_stat(CPUState *env, target_ulong addr, struct stat *s)
107 {
108 struct m68k_gdb_stat *p;
109
110 if (!(p = lock_user(VERIFY_WRITE, addr, sizeof(struct m68k_gdb_stat), 0)))
111 /* FIXME - should this return an error code? */
112 return;
113 p->gdb_st_dev = cpu_to_be32(s->st_dev);
114 p->gdb_st_ino = cpu_to_be32(s->st_ino);
115 p->gdb_st_mode = cpu_to_be32(s->st_mode);
116 p->gdb_st_nlink = cpu_to_be32(s->st_nlink);
117 p->gdb_st_uid = cpu_to_be32(s->st_uid);
118 p->gdb_st_gid = cpu_to_be32(s->st_gid);
119 p->gdb_st_rdev = cpu_to_be32(s->st_rdev);
120 p->gdb_st_size = cpu_to_be64(s->st_size);
121 #ifdef _WIN32
122 /* Windows stat is missing some fields. */
123 p->gdb_st_blksize = 0;
124 p->gdb_st_blocks = 0;
125 #else
126 p->gdb_st_blksize = cpu_to_be64(s->st_blksize);
127 p->gdb_st_blocks = cpu_to_be64(s->st_blocks);
128 #endif
129 p->gdb_st_atime = cpu_to_be32(s->st_atime);
130 p->gdb_st_mtime = cpu_to_be32(s->st_mtime);
131 p->gdb_st_ctime = cpu_to_be32(s->st_ctime);
132 unlock_user(p, addr, sizeof(struct m68k_gdb_stat));
133 }
134
135 static int m68k_semi_is_fseek;
136
137 static void m68k_semi_cb(CPUState *env, target_ulong ret, target_ulong err)
138 {
139 target_ulong args;
140
141 args = env->dregs[1];
142 if (m68k_semi_is_fseek) {
143 /* FIXME: We've already lost the high bits of the fseek
144 return value. */
145 /* FIXME - handle put_user() failure */
146 put_user_u32(0, args);
147 args += 4;
148 m68k_semi_is_fseek = 0;
149 }
150 /* FIXME - handle put_user() failure */
151 put_user_u32(ret, args);
152 put_user_u32(errno, args + 4);
153 }
154
155 #define ARG(n) \
156 ({ \
157 target_ulong __arg; \
158 /* FIXME - handle get_user() failure */ \
159 get_user_ual(__arg, args + (n) * 4); \
160 __arg; \
161 })
162 #define PARG(x) ((unsigned long)ARG(x))
163 void do_m68k_semihosting(CPUM68KState *env, int nr)
164 {
165 uint32_t args;
166 void *p;
167 void *q;
168 uint32_t len;
169 uint32_t result;
170
171 args = env->dregs[1];
172 switch (nr) {
173 case HOSTED_EXIT:
174 exit(env->dregs[0]);
175 case HOSTED_OPEN:
176 if (use_gdb_syscalls()) {
177 gdb_do_syscall(m68k_semi_cb, "open,%s,%x,%x", ARG(0), (int)ARG(1),
178 ARG(2), ARG(3));
179 return;
180 } else {
181 if (!(p = lock_user_string(ARG(0)))) {
182 /* FIXME - check error code? */
183 result = -1;
184 } else {
185 result = open(p, translate_openflags(ARG(2)), ARG(3));
186 unlock_user(p, ARG(0), 0);
187 }
188 }
189 break;
190 case HOSTED_CLOSE:
191 {
192 /* Ignore attempts to close stdin/out/err. */
193 int fd = ARG(0);
194 if (fd > 2) {
195 if (use_gdb_syscalls()) {
196 gdb_do_syscall(m68k_semi_cb, "close,%x", ARG(0));
197 return;
198 } else {
199 result = close(fd);
200 }
201 } else {
202 result = 0;
203 }
204 break;
205 }
206 case HOSTED_READ:
207 len = ARG(2);
208 if (use_gdb_syscalls()) {
209 gdb_do_syscall(m68k_semi_cb, "read,%x,%x,%x",
210 ARG(0), ARG(1), len);
211 return;
212 } else {
213 if (!(p = lock_user(VERIFY_WRITE, ARG(1), len, 0))) {
214 /* FIXME - check error code? */
215 result = -1;
216 } else {
217 result = read(ARG(0), p, len);
218 unlock_user(p, ARG(1), len);
219 }
220 }
221 break;
222 case HOSTED_WRITE:
223 len = ARG(2);
224 if (use_gdb_syscalls()) {
225 gdb_do_syscall(m68k_semi_cb, "write,%x,%x,%x",
226 ARG(0), ARG(1), len);
227 return;
228 } else {
229 if (!(p = lock_user(VERIFY_READ, ARG(1), len, 1))) {
230 /* FIXME - check error code? */
231 result = -1;
232 } else {
233 result = write(ARG(0), p, len);
234 unlock_user(p, ARG(0), 0);
235 }
236 }
237 break;
238 case HOSTED_LSEEK:
239 {
240 uint64_t off;
241 off = (uint32_t)ARG(2) | ((uint64_t)ARG(1) << 32);
242 if (use_gdb_syscalls()) {
243 m68k_semi_is_fseek = 1;
244 gdb_do_syscall(m68k_semi_cb, "fseek,%x,%lx,%x",
245 ARG(0), off, ARG(3));
246 } else {
247 off = lseek(ARG(0), off, ARG(3));
248 /* FIXME - handle put_user() failure */
249 put_user_u32(off >> 32, args);
250 put_user_u32(off, args + 4);
251 put_user_u32(errno, args + 8);
252 }
253 return;
254 }
255 case HOSTED_RENAME:
256 if (use_gdb_syscalls()) {
257 gdb_do_syscall(m68k_semi_cb, "rename,%s,%s",
258 ARG(0), (int)ARG(1), ARG(2), (int)ARG(3));
259 return;
260 } else {
261 p = lock_user_string(ARG(0));
262 q = lock_user_string(ARG(2));
263 if (!p || !q) {
264 /* FIXME - check error code? */
265 result = -1;
266 } else {
267 result = rename(p, q);
268 }
269 unlock_user(p, ARG(0), 0);
270 unlock_user(q, ARG(2), 0);
271 }
272 break;
273 case HOSTED_UNLINK:
274 if (use_gdb_syscalls()) {
275 gdb_do_syscall(m68k_semi_cb, "unlink,%s",
276 ARG(0), (int)ARG(1));
277 return;
278 } else {
279 if (!(p = lock_user_string(ARG(0)))) {
280 /* FIXME - check error code? */
281 result = -1;
282 } else {
283 result = unlink(p);
284 unlock_user(p, ARG(0), 0);
285 }
286 }
287 break;
288 case HOSTED_STAT:
289 if (use_gdb_syscalls()) {
290 gdb_do_syscall(m68k_semi_cb, "stat,%s,%x",
291 ARG(0), (int)ARG(1), ARG(2));
292 return;
293 } else {
294 struct stat s;
295 if (!(p = lock_user_string(ARG(0)))) {
296 /* FIXME - check error code? */
297 result = -1;
298 } else {
299 result = stat(p, &s);
300 unlock_user(p, ARG(0), 0);
301 }
302 if (result == 0) {
303 translate_stat(env, ARG(2), &s);
304 }
305 }
306 break;
307 case HOSTED_FSTAT:
308 if (use_gdb_syscalls()) {
309 gdb_do_syscall(m68k_semi_cb, "fstat,%x,%x",
310 ARG(0), ARG(1));
311 return;
312 } else {
313 struct stat s;
314 result = fstat(ARG(0), &s);
315 if (result == 0) {
316 translate_stat(env, ARG(1), &s);
317 }
318 }
319 break;
320 case HOSTED_GETTIMEOFDAY:
321 if (use_gdb_syscalls()) {
322 gdb_do_syscall(m68k_semi_cb, "gettimeofday,%x,%x",
323 ARG(0), ARG(1));
324 return;
325 } else {
326 qemu_timeval tv;
327 struct gdb_timeval *p;
328 result = qemu_gettimeofday(&tv);
329 if (result != 0) {
330 if (!(p = lock_user(VERIFY_WRITE,
331 ARG(0), sizeof(struct gdb_timeval), 0))) {
332 /* FIXME - check error code? */
333 result = -1;
334 } else {
335 p->tv_sec = cpu_to_be32(tv.tv_sec);
336 p->tv_usec = cpu_to_be64(tv.tv_usec);
337 unlock_user(p, ARG(0), sizeof(struct gdb_timeval));
338 }
339 }
340 }
341 break;
342 case HOSTED_ISATTY:
343 if (use_gdb_syscalls()) {
344 gdb_do_syscall(m68k_semi_cb, "isatty,%x", ARG(0));
345 return;
346 } else {
347 result = isatty(ARG(0));
348 }
349 break;
350 case HOSTED_SYSTEM:
351 if (use_gdb_syscalls()) {
352 gdb_do_syscall(m68k_semi_cb, "system,%s",
353 ARG(0), (int)ARG(1));
354 return;
355 } else {
356 if (!(p = lock_user_string(ARG(0)))) {
357 /* FIXME - check error code? */
358 result = -1;
359 } else {
360 result = system(p);
361 unlock_user(p, ARG(0), 0);
362 }
363 }
364 break;
365 case HOSTED_INIT_SIM:
366 #if defined(CONFIG_USER_ONLY)
367 {
368 TaskState *ts = env->opaque;
369 /* Allocate the heap using sbrk. */
370 if (!ts->heap_limit) {
371 long ret;
372 uint32_t size;
373 uint32_t base;
374
375 base = do_brk(0);
376 size = SEMIHOSTING_HEAP_SIZE;
377 /* Try a big heap, and reduce the size if that fails. */
378 for (;;) {
379 ret = do_brk(base + size);
380 if (ret != -1)
381 break;
382 size >>= 1;
383 }
384 ts->heap_limit = base + size;
385 }
386 /* This call may happen before we have writable memory, so return
387 values directly in registers. */
388 env->dregs[1] = ts->heap_limit;
389 env->aregs[7] = ts->stack_base;
390 }
391 #else
392 /* FIXME: This is wrong for boards where RAM does not start at
393 address zero. */
394 env->dregs[1] = ram_size;
395 env->aregs[7] = ram_size;
396 #endif
397 return;
398 default:
399 cpu_abort(env, "Unsupported semihosting syscall %d\n", nr);
400 result = 0;
401 }
402 /* FIXME - handle put_user() failure */
403 put_user_u32(result, args);
404 put_user_u32(errno, args + 4);
405 }