Statistics
| Branch: | Revision:

root / m68k-semi.c @ 6e473128

History | View | Annotate | Download (10.5 kB)

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