2
* m68k/ColdFire Semihosting ssycall interface
4
* Copyright (c) 2005 CodeSourcery, LLC. Written by Paul Brook.
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.
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.
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.
21
#include <sys/types.h>
34
#define HOSTED_PUTCHAR 1 /* Obsolete */
36
#define HOSTED_CLOSE 3
38
#define HOSTED_WRITE 5
39
#define HOSTED_LSEEK 6
40
#define HOSTED_RENAME 7
41
#define HOSTED_UNLINK 8
43
#define HOSTED_FSTAT 10
44
#define HOSTED_GETTIMEOFDAY 11
45
#define HOSTED_ISATTY 12
46
#define HOSTED_SYSTEM 13
48
typedef uint32_t gdb_mode_t;
49
typedef uint32_t gdb_time_t;
51
struct m68k_gdb_stat {
52
uint32_t gdb_st_dev; /* device */
53
uint32_t gdb_st_ino; /* inode */
54
gdb_mode_t gdb_st_mode; /* protection */
55
uint32_t gdb_st_nlink; /* number of hard links */
56
uint32_t gdb_st_uid; /* user ID of owner */
57
uint32_t gdb_st_gid; /* group ID of owner */
58
uint32_t gdb_st_rdev; /* device type (if inode device) */
59
uint64_t gdb_st_size; /* total size, in bytes */
60
uint64_t gdb_st_blksize; /* blocksize for filesystem I/O */
61
uint64_t gdb_st_blocks; /* number of blocks allocated */
62
gdb_time_t gdb_st_atime; /* time of last access */
63
gdb_time_t gdb_st_mtime; /* time of last modification */
64
gdb_time_t gdb_st_ctime; /* time of last change */
68
gdb_time_t tv_sec; /* second */
69
uint64_t tv_usec; /* microsecond */
72
#define GDB_O_RDONLY 0x0
73
#define GDB_O_WRONLY 0x1
74
#define GDB_O_RDWR 0x2
75
#define GDB_O_APPEND 0x8
76
#define GDB_O_CREAT 0x200
77
#define GDB_O_TRUNC 0x400
78
#define GDB_O_EXCL 0x800
80
static int translate_openflags(int flags)
84
if (flags & GDB_O_WRONLY)
86
else if (flags & GDB_O_RDWR)
91
if (flags & GDB_O_APPEND) hf |= O_APPEND;
92
if (flags & GDB_O_CREAT) hf |= O_CREAT;
93
if (flags & GDB_O_TRUNC) hf |= O_TRUNC;
94
if (flags & GDB_O_EXCL) hf |= O_EXCL;
99
static void translate_stat(struct m68k_gdb_stat *p, struct stat *s)
101
p->gdb_st_dev = tswap16(s->st_dev);
102
p->gdb_st_ino = tswap16(s->st_ino);
103
p->gdb_st_mode = tswap32(s->st_mode);
104
p->gdb_st_nlink = tswap16(s->st_nlink);
105
p->gdb_st_uid = tswap16(s->st_uid);
106
p->gdb_st_gid = tswap16(s->st_gid);
107
p->gdb_st_rdev = tswap16(s->st_rdev);
108
p->gdb_st_size = tswap32(s->st_size);
109
p->gdb_st_atime = tswap32(s->st_atime);
110
p->gdb_st_mtime = tswap32(s->st_mtime);
111
p->gdb_st_ctime = tswap32(s->st_ctime);
112
p->gdb_st_blksize = tswap32(s->st_blksize);
113
p->gdb_st_blocks = tswap32(s->st_blocks);
116
static inline uint32_t check_err(CPUM68KState *env, uint32_t code)
118
if (code == (uint32_t)-1) {
122
env->dregs[0] = code;
127
#define ARG(x) tswap32(args[x])
128
void do_m68k_semihosting(CPUM68KState *env, int nr)
132
args = (uint32_t *)env->dregs[1];
137
/* Assume name is NULL terminated. */
138
check_err(env, open((char *)ARG(0), translate_openflags(ARG(2)),
143
/* Ignore attempts to close stdin/out/err. */
146
check_err(env, close(fd));
152
check_err(env, read(ARG(0), (void *)ARG(1), ARG(2)));
155
check_err(env, write(ARG(0), (void *)ARG(1), ARG(2)));
160
off = (uint32_t)ARG(2) | ((uint64_t)ARG(1) << 32);
161
check_err(env, lseek(ARG(0), off, ARG(3)));
165
/* Assume names are NULL terminated. */
166
check_err(env, rename((char *)ARG(0), (char *)ARG(2)));
169
/* Assume name is NULL terminated. */
170
check_err(env, unlink((char *)ARG(0)));
173
/* Assume name is NULL terminated. */
177
rc = check_err(env, stat((char *)ARG(0), &s));
179
translate_stat((struct m68k_gdb_stat *)ARG(2), &s);
187
rc = check_err(env, fstat(ARG(0), &s));
189
translate_stat((struct m68k_gdb_stat *)ARG(1), &s);
193
case HOSTED_GETTIMEOFDAY:
196
struct gdb_timeval *p;
198
rc = check_err(env, gettimeofday(&tv, NULL));
200
p = (struct gdb_timeval *)ARG(0);
201
p->tv_sec = tswap32(tv.tv_sec);
202
p->tv_usec = tswap64(tv.tv_usec);
207
check_err(env, isatty(ARG(0)));
210
/* Assume name is NULL terminated. */
211
check_err(env, system((char *)ARG(0)));
214
cpu_abort(env, "Unsupported semihosting syscall %d\n", nr);