]>
Commit | Line | Data |
---|---|---|
e6e5906b PB |
1 | /* |
2 | * m68k simulator syscall interface | |
5fafdf24 | 3 | * |
e6e5906b PB |
4 | * Copyright (c) 2005 CodeSourcery, LLC. Written by Paul Brook. |
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 | |
530e7615 BS |
18 | * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | * MA 02110-1301, USA. | |
e6e5906b PB |
20 | */ |
21 | ||
22 | #include <sys/types.h> | |
23 | #include <sys/stat.h> | |
24 | #include <errno.h> | |
25 | #include <fcntl.h> | |
26 | #include <unistd.h> | |
27 | #include <stdlib.h> | |
28 | #include <stdio.h> | |
29 | #include <time.h> | |
30 | ||
31 | #include "qemu.h" | |
32 | ||
33 | #define SYS_EXIT 1 | |
34 | #define SYS_READ 3 | |
35 | #define SYS_WRITE 4 | |
36 | #define SYS_OPEN 5 | |
37 | #define SYS_CLOSE 6 | |
38 | #define SYS_BRK 17 | |
39 | #define SYS_FSTAT 28 | |
40 | #define SYS_ISATTY 29 | |
41 | #define SYS_LSEEK 199 | |
42 | ||
43 | struct m86k_sim_stat { | |
44 | uint16_t sim_st_dev; | |
45 | uint16_t sim_st_ino; | |
46 | uint32_t sim_st_mode; | |
47 | uint16_t sim_st_nlink; | |
48 | uint16_t sim_st_uid; | |
49 | uint16_t sim_st_gid; | |
50 | uint16_t sim_st_rdev; | |
51 | uint32_t sim_st_size; | |
52 | uint32_t sim_st_atime; | |
53 | uint32_t sim_st_mtime; | |
54 | uint32_t sim_st_ctime; | |
55 | uint32_t sim_st_blksize; | |
56 | uint32_t sim_st_blocks; | |
57 | }; | |
58 | ||
59 | static inline uint32_t check_err(CPUM68KState *env, uint32_t code) | |
60 | { | |
61 | env->dregs[0] = code; | |
62 | if (code == (uint32_t)-1) { | |
63 | env->dregs[1] = errno; | |
64 | } else { | |
65 | env->dregs[1] = 0; | |
66 | } | |
67 | return code; | |
68 | } | |
69 | ||
70 | #define SIM_O_APPEND 0x0008 | |
71 | #define SIM_O_CREAT 0x0200 | |
72 | #define SIM_O_TRUNC 0x0400 | |
73 | #define SIM_O_EXCL 0x0800 | |
74 | #define SIM_O_NONBLOCK 0x4000 | |
75 | #define SIM_O_NOCTTY 0x8000 | |
76 | #define SIM_O_SYNC 0x2000 | |
77 | ||
78 | static int translate_openflags(int flags) | |
79 | { | |
80 | int hf; | |
81 | ||
82 | switch (flags & 3) { | |
83 | case 0: hf = O_RDONLY; break; | |
84 | case 1: hf = O_WRONLY; break; | |
85 | case 2: hf = O_RDWR; break; | |
86 | default: hf = O_RDWR; break; | |
87 | } | |
88 | ||
89 | if (flags & SIM_O_APPEND) hf |= O_APPEND; | |
90 | if (flags & SIM_O_CREAT) hf |= O_CREAT; | |
91 | if (flags & SIM_O_TRUNC) hf |= O_TRUNC; | |
92 | if (flags & SIM_O_EXCL) hf |= O_EXCL; | |
93 | if (flags & SIM_O_NONBLOCK) hf |= O_NONBLOCK; | |
94 | if (flags & SIM_O_NOCTTY) hf |= O_NOCTTY; | |
95 | if (flags & SIM_O_SYNC) hf |= O_SYNC; | |
96 | ||
97 | return hf; | |
98 | } | |
99 | ||
100 | #define ARG(x) tswap32(args[x]) | |
101 | void do_m68k_simcall(CPUM68KState *env, int nr) | |
102 | { | |
103 | uint32_t *args; | |
104 | ||
526ccb7a | 105 | args = (uint32_t *)(unsigned long)(env->aregs[7] + 4); |
e6e5906b PB |
106 | switch (nr) { |
107 | case SYS_EXIT: | |
108 | exit(ARG(0)); | |
109 | case SYS_READ: | |
526ccb7a | 110 | check_err(env, read(ARG(0), (void *)(unsigned long)ARG(1), ARG(2))); |
e6e5906b PB |
111 | break; |
112 | case SYS_WRITE: | |
526ccb7a | 113 | check_err(env, write(ARG(0), (void *)(unsigned long)ARG(1), ARG(2))); |
e6e5906b PB |
114 | break; |
115 | case SYS_OPEN: | |
526ccb7a AZ |
116 | check_err(env, open((char *)(unsigned long)ARG(0), |
117 | translate_openflags(ARG(1)), ARG(2))); | |
e6e5906b PB |
118 | break; |
119 | case SYS_CLOSE: | |
120 | { | |
121 | /* Ignore attempts to close stdin/out/err. */ | |
122 | int fd = ARG(0); | |
123 | if (fd > 2) | |
124 | check_err(env, close(fd)); | |
125 | else | |
126 | check_err(env, 0); | |
127 | break; | |
128 | } | |
129 | case SYS_BRK: | |
130 | { | |
131 | int32_t ret; | |
132 | ||
ac2567b5 | 133 | ret = do_brk((abi_ulong)ARG(0)); |
e6e5906b PB |
134 | if (ret == -ENOMEM) |
135 | ret = -1; | |
136 | check_err(env, ret); | |
137 | } | |
138 | break; | |
139 | case SYS_FSTAT: | |
140 | { | |
141 | struct stat s; | |
142 | int rc; | |
143 | struct m86k_sim_stat *p; | |
144 | rc = check_err(env, fstat(ARG(0), &s)); | |
145 | if (rc == 0) { | |
526ccb7a | 146 | p = (struct m86k_sim_stat *)(unsigned long)ARG(1); |
e6e5906b PB |
147 | p->sim_st_dev = tswap16(s.st_dev); |
148 | p->sim_st_ino = tswap16(s.st_ino); | |
149 | p->sim_st_mode = tswap32(s.st_mode); | |
150 | p->sim_st_nlink = tswap16(s.st_nlink); | |
151 | p->sim_st_uid = tswap16(s.st_uid); | |
152 | p->sim_st_gid = tswap16(s.st_gid); | |
153 | p->sim_st_rdev = tswap16(s.st_rdev); | |
154 | p->sim_st_size = tswap32(s.st_size); | |
155 | p->sim_st_atime = tswap32(s.st_atime); | |
156 | p->sim_st_mtime = tswap32(s.st_mtime); | |
157 | p->sim_st_ctime = tswap32(s.st_ctime); | |
158 | p->sim_st_blksize = tswap32(s.st_blksize); | |
159 | p->sim_st_blocks = tswap32(s.st_blocks); | |
160 | } | |
161 | } | |
162 | break; | |
163 | case SYS_ISATTY: | |
164 | check_err(env, isatty(ARG(0))); | |
165 | break; | |
166 | case SYS_LSEEK: | |
167 | check_err(env, lseek(ARG(0), (int32_t)ARG(1), ARG(2))); | |
168 | break; | |
169 | default: | |
170 | cpu_abort(env, "Unsupported m68k sim syscall %d\n", nr); | |
171 | } | |
172 | } |