Statistics
| Branch: | Revision:

root / linux-user / m68k-semi.c @ b1f9be31

History | View | Annotate | Download (6.2 kB)

1 e6e5906b pbrook
/*
2 e6e5906b pbrook
 *  m68k/ColdFire Semihosting ssycall interface
3 e6e5906b pbrook
 * 
4 e6e5906b pbrook
 *  Copyright (c) 2005 CodeSourcery, LLC. Written by Paul Brook.
5 e6e5906b pbrook
 *
6 e6e5906b pbrook
 *  This program is free software; you can redistribute it and/or modify
7 e6e5906b pbrook
 *  it under the terms of the GNU General Public License as published by
8 e6e5906b pbrook
 *  the Free Software Foundation; either version 2 of the License, or
9 e6e5906b pbrook
 *  (at your option) any later version.
10 e6e5906b pbrook
 *
11 e6e5906b pbrook
 *  This program is distributed in the hope that it will be useful,
12 e6e5906b pbrook
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 e6e5906b pbrook
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 e6e5906b pbrook
 *  GNU General Public License for more details.
15 e6e5906b pbrook
 *
16 e6e5906b pbrook
 *  You should have received a copy of the GNU General Public License
17 e6e5906b pbrook
 *  along with this program; if not, write to the Free Software
18 e6e5906b pbrook
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 e6e5906b pbrook
 */
20 e6e5906b pbrook
21 e6e5906b pbrook
#include <sys/types.h>
22 e6e5906b pbrook
#include <sys/stat.h>
23 e6e5906b pbrook
#include <errno.h>
24 e6e5906b pbrook
#include <fcntl.h>
25 e6e5906b pbrook
#include <unistd.h>
26 e6e5906b pbrook
#include <stdlib.h>
27 e6e5906b pbrook
#include <stdio.h>
28 e6e5906b pbrook
#include <sys/time.h>
29 e6e5906b pbrook
#include <time.h>
30 e6e5906b pbrook
31 e6e5906b pbrook
#include "qemu.h"
32 e6e5906b pbrook
33 e6e5906b pbrook
#define HOSTED_EXIT  0
34 e6e5906b pbrook
#define HOSTED_PUTCHAR 1 /* Obsolete */
35 e6e5906b pbrook
#define HOSTED_OPEN 2
36 e6e5906b pbrook
#define HOSTED_CLOSE 3
37 e6e5906b pbrook
#define HOSTED_READ 4
38 e6e5906b pbrook
#define HOSTED_WRITE 5
39 e6e5906b pbrook
#define HOSTED_LSEEK 6
40 e6e5906b pbrook
#define HOSTED_RENAME 7
41 e6e5906b pbrook
#define HOSTED_UNLINK 8
42 e6e5906b pbrook
#define HOSTED_STAT 9
43 e6e5906b pbrook
#define HOSTED_FSTAT 10
44 e6e5906b pbrook
#define HOSTED_GETTIMEOFDAY 11
45 e6e5906b pbrook
#define HOSTED_ISATTY 12
46 e6e5906b pbrook
#define HOSTED_SYSTEM 13
47 e6e5906b pbrook
48 e6e5906b pbrook
typedef uint32_t gdb_mode_t;
49 e6e5906b pbrook
typedef uint32_t gdb_time_t;
50 e6e5906b pbrook
51 e6e5906b pbrook
struct m68k_gdb_stat {
52 e6e5906b pbrook
  uint32_t    gdb_st_dev;     /* device */
53 e6e5906b pbrook
  uint32_t    gdb_st_ino;     /* inode */
54 e6e5906b pbrook
  gdb_mode_t  gdb_st_mode;    /* protection */
55 e6e5906b pbrook
  uint32_t    gdb_st_nlink;   /* number of hard links */
56 e6e5906b pbrook
  uint32_t    gdb_st_uid;     /* user ID of owner */
57 e6e5906b pbrook
  uint32_t    gdb_st_gid;     /* group ID of owner */
58 e6e5906b pbrook
  uint32_t    gdb_st_rdev;    /* device type (if inode device) */
59 e6e5906b pbrook
  uint64_t    gdb_st_size;    /* total size, in bytes */
60 e6e5906b pbrook
  uint64_t    gdb_st_blksize; /* blocksize for filesystem I/O */
61 e6e5906b pbrook
  uint64_t    gdb_st_blocks;  /* number of blocks allocated */
62 e6e5906b pbrook
  gdb_time_t  gdb_st_atime;   /* time of last access */
63 e6e5906b pbrook
  gdb_time_t  gdb_st_mtime;   /* time of last modification */
64 e6e5906b pbrook
  gdb_time_t  gdb_st_ctime;   /* time of last change */
65 e6e5906b pbrook
};
66 e6e5906b pbrook
67 e6e5906b pbrook
struct gdb_timeval {
68 e6e5906b pbrook
  gdb_time_t tv_sec;  /* second */
69 e6e5906b pbrook
  uint64_t tv_usec;   /* microsecond */
70 e6e5906b pbrook
};
71 e6e5906b pbrook
72 e6e5906b pbrook
#define GDB_O_RDONLY   0x0
73 e6e5906b pbrook
#define GDB_O_WRONLY   0x1
74 e6e5906b pbrook
#define GDB_O_RDWR     0x2
75 e6e5906b pbrook
#define GDB_O_APPEND   0x8
76 e6e5906b pbrook
#define GDB_O_CREAT  0x200
77 e6e5906b pbrook
#define GDB_O_TRUNC  0x400
78 e6e5906b pbrook
#define GDB_O_EXCL   0x800
79 e6e5906b pbrook
80 e6e5906b pbrook
static int translate_openflags(int flags)
81 e6e5906b pbrook
{
82 e6e5906b pbrook
    int hf;
83 e6e5906b pbrook
84 e6e5906b pbrook
    if (flags & GDB_O_WRONLY)
85 e6e5906b pbrook
        hf = O_WRONLY;
86 e6e5906b pbrook
    else if (flags & GDB_O_RDWR)
87 e6e5906b pbrook
        hf = O_RDWR;
88 e6e5906b pbrook
    else
89 e6e5906b pbrook
        hf = O_RDONLY;
90 e6e5906b pbrook
91 e6e5906b pbrook
    if (flags & GDB_O_APPEND) hf |= O_APPEND;
92 e6e5906b pbrook
    if (flags & GDB_O_CREAT) hf |= O_CREAT;
93 e6e5906b pbrook
    if (flags & GDB_O_TRUNC) hf |= O_TRUNC;
94 e6e5906b pbrook
    if (flags & GDB_O_EXCL) hf |= O_EXCL;
95 e6e5906b pbrook
96 e6e5906b pbrook
    return hf;
97 e6e5906b pbrook
}
98 e6e5906b pbrook
99 e6e5906b pbrook
static void translate_stat(struct m68k_gdb_stat *p, struct stat *s)
100 e6e5906b pbrook
{
101 e6e5906b pbrook
    p->gdb_st_dev = tswap16(s->st_dev);
102 e6e5906b pbrook
    p->gdb_st_ino = tswap16(s->st_ino);
103 e6e5906b pbrook
    p->gdb_st_mode = tswap32(s->st_mode);
104 e6e5906b pbrook
    p->gdb_st_nlink = tswap16(s->st_nlink);
105 e6e5906b pbrook
    p->gdb_st_uid = tswap16(s->st_uid);
106 e6e5906b pbrook
    p->gdb_st_gid = tswap16(s->st_gid);
107 e6e5906b pbrook
    p->gdb_st_rdev = tswap16(s->st_rdev);
108 e6e5906b pbrook
    p->gdb_st_size = tswap32(s->st_size);
109 e6e5906b pbrook
    p->gdb_st_atime = tswap32(s->st_atime);
110 e6e5906b pbrook
    p->gdb_st_mtime = tswap32(s->st_mtime);
111 e6e5906b pbrook
    p->gdb_st_ctime = tswap32(s->st_ctime);
112 e6e5906b pbrook
    p->gdb_st_blksize = tswap32(s->st_blksize);
113 e6e5906b pbrook
    p->gdb_st_blocks = tswap32(s->st_blocks);
114 e6e5906b pbrook
}
115 e6e5906b pbrook
116 e6e5906b pbrook
static inline uint32_t check_err(CPUM68KState *env, uint32_t code)
117 e6e5906b pbrook
{
118 e6e5906b pbrook
  if (code == (uint32_t)-1) {
119 e6e5906b pbrook
      env->sr |= CCF_C;
120 e6e5906b pbrook
  } else {
121 e6e5906b pbrook
      env->sr &= ~CCF_C;
122 e6e5906b pbrook
      env->dregs[0] = code;
123 e6e5906b pbrook
  }
124 e6e5906b pbrook
  return code;
125 e6e5906b pbrook
}
126 e6e5906b pbrook
127 e6e5906b pbrook
#define ARG(x) tswap32(args[x])
128 e6e5906b pbrook
void do_m68k_semihosting(CPUM68KState *env, int nr)
129 e6e5906b pbrook
{
130 e6e5906b pbrook
    uint32_t *args;
131 e6e5906b pbrook
132 e6e5906b pbrook
    args = (uint32_t *)env->dregs[1];
133 e6e5906b pbrook
    switch (nr) {
134 e6e5906b pbrook
    case HOSTED_EXIT:
135 e6e5906b pbrook
        exit(env->dregs[0]);
136 e6e5906b pbrook
    case HOSTED_OPEN:
137 e6e5906b pbrook
        /* Assume name is NULL terminated.  */
138 e6e5906b pbrook
        check_err(env, open((char *)ARG(0), translate_openflags(ARG(2)),
139 e6e5906b pbrook
                            ARG(3)));
140 e6e5906b pbrook
        break;
141 e6e5906b pbrook
    case HOSTED_CLOSE:
142 e6e5906b pbrook
        {
143 e6e5906b pbrook
            /* Ignore attempts to close stdin/out/err.  */
144 e6e5906b pbrook
            int fd = ARG(0);
145 e6e5906b pbrook
            if (fd > 2)
146 e6e5906b pbrook
              check_err(env, close(fd));
147 e6e5906b pbrook
            else
148 e6e5906b pbrook
              check_err(env, 0);
149 e6e5906b pbrook
            break;
150 e6e5906b pbrook
        }
151 e6e5906b pbrook
    case HOSTED_READ:
152 e6e5906b pbrook
        check_err(env, read(ARG(0), (void *)ARG(1), ARG(2)));
153 e6e5906b pbrook
        break;
154 e6e5906b pbrook
    case HOSTED_WRITE:
155 e6e5906b pbrook
        check_err(env, write(ARG(0), (void *)ARG(1), ARG(2)));
156 e6e5906b pbrook
        break;
157 e6e5906b pbrook
    case HOSTED_LSEEK:
158 e6e5906b pbrook
        {
159 e6e5906b pbrook
            uint64_t off;
160 e6e5906b pbrook
            off = (uint32_t)ARG(2) | ((uint64_t)ARG(1) << 32);
161 e6e5906b pbrook
            check_err(env, lseek(ARG(0), off, ARG(3)));
162 e6e5906b pbrook
        }
163 e6e5906b pbrook
        break;
164 e6e5906b pbrook
    case HOSTED_RENAME:
165 e6e5906b pbrook
        /* Assume names are NULL terminated.  */
166 e6e5906b pbrook
        check_err(env, rename((char *)ARG(0), (char *)ARG(2)));
167 e6e5906b pbrook
        break;
168 e6e5906b pbrook
    case HOSTED_UNLINK:
169 e6e5906b pbrook
        /* Assume name is NULL terminated.  */
170 e6e5906b pbrook
        check_err(env, unlink((char *)ARG(0)));
171 e6e5906b pbrook
        break;
172 e6e5906b pbrook
    case HOSTED_STAT:
173 e6e5906b pbrook
        /* Assume name is NULL terminated.  */
174 e6e5906b pbrook
        {
175 e6e5906b pbrook
            struct stat s;
176 e6e5906b pbrook
            int rc;
177 e6e5906b pbrook
            rc = check_err(env, stat((char *)ARG(0), &s));
178 e6e5906b pbrook
            if (rc == 0) {
179 e6e5906b pbrook
                translate_stat((struct m68k_gdb_stat *)ARG(2), &s);
180 e6e5906b pbrook
            }
181 e6e5906b pbrook
        }
182 e6e5906b pbrook
        break;
183 e6e5906b pbrook
    case HOSTED_FSTAT:
184 e6e5906b pbrook
        {
185 e6e5906b pbrook
            struct stat s;
186 e6e5906b pbrook
            int rc;
187 e6e5906b pbrook
            rc = check_err(env, fstat(ARG(0), &s));
188 e6e5906b pbrook
            if (rc == 0) {
189 e6e5906b pbrook
                translate_stat((struct m68k_gdb_stat *)ARG(1), &s);
190 e6e5906b pbrook
            }
191 e6e5906b pbrook
        }
192 e6e5906b pbrook
        break;
193 e6e5906b pbrook
    case HOSTED_GETTIMEOFDAY:
194 e6e5906b pbrook
        {
195 e6e5906b pbrook
            struct timeval tv;
196 e6e5906b pbrook
            struct gdb_timeval *p;
197 e6e5906b pbrook
            int rc;
198 e6e5906b pbrook
            rc = check_err(env, gettimeofday(&tv, NULL));
199 e6e5906b pbrook
            if (rc != 0) {
200 e6e5906b pbrook
                p = (struct gdb_timeval *)ARG(0);
201 e6e5906b pbrook
                p->tv_sec = tswap32(tv.tv_sec);
202 e6e5906b pbrook
                p->tv_usec = tswap64(tv.tv_usec);
203 e6e5906b pbrook
            }
204 e6e5906b pbrook
        }
205 e6e5906b pbrook
        break;
206 e6e5906b pbrook
    case HOSTED_ISATTY:
207 e6e5906b pbrook
        check_err(env, isatty(ARG(0)));
208 e6e5906b pbrook
        break;
209 e6e5906b pbrook
    case HOSTED_SYSTEM:
210 e6e5906b pbrook
        /* Assume name is NULL terminated.  */
211 e6e5906b pbrook
        check_err(env, system((char *)ARG(0)));
212 e6e5906b pbrook
        break;
213 e6e5906b pbrook
    default:
214 e6e5906b pbrook
        cpu_abort(env, "Unsupported semihosting syscall %d\n", nr);
215 e6e5906b pbrook
    }
216 e6e5906b pbrook
}