1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/* syscalls.c --- implement system calls for the RX simulator.
Copyright (C) 2005, 2007-2012 Free Software Foundation, Inc.
Contributed by Red Hat, Inc.
This file is part of the GNU simulators.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include "gdb/callback.h"
#include "cpu.h"
#include "mem.h"
#include "syscalls.h"
#include "syscall.h"
/* The current syscall callbacks we're using. */
static struct host_callback_struct *callbacks;
void
set_callbacks (struct host_callback_struct *cb)
{
callbacks = cb;
}
/* Arguments 1..4 are in R1..R4, remainder on stack.
Return value in R1..R4 as needed.
structs bigger than 16 bytes: pointer pushed on stack last
We only support arguments that fit in general registers.
The system call number is in R5. We expect ssycalls to look like
this in libgloss:
_exit:
mov #SYS_exit, r5
int #255
rts
*/
int argp, stackp;
static int
arg ()
{
int rv = 0;
argp++;
if (argp < 4)
return get_reg (argp);
rv = mem_get_si (get_reg (sp) + stackp);
stackp += 4;
return rv;
}
static void
read_target (char *buffer, int address, int count, int asciiz)
{
char byte;
while (count > 0)
{
byte = mem_get_qi (address++);
*buffer++ = byte;
if (asciiz && (byte == 0))
return;
count--;
}
}
static void
write_target (char *buffer, int address, int count, int asciiz)
{
char byte;
while (count > 0)
{
byte = *buffer++;
mem_put_qi (address++, byte);
if (asciiz && (byte == 0))
return;
count--;
}
}
#define PTRSZ (A16 ? 2 : 3)
static char *callnames[] = {
"SYS_zero",
"SYS_exit",
"SYS_open",
"SYS_close",
"SYS_read",
"SYS_write",
"SYS_lseek",
"SYS_unlink",
"SYS_getpid",
"SYS_kill",
"SYS_fstat",
"SYS_sbrk",
"SYS_argvlen",
"SYS_argv",
"SYS_chdir",
"SYS_stat",
"SYS_chmod",
"SYS_utime",
"SYS_time",
"SYS_gettimeofday",
"SYS_times",
"SYS_link"
};
int
rx_syscall (int id)
{
static char buf[256];
int rv;
argp = 0;
stackp = 4;
if (trace)
printf ("\033[31m/* SYSCALL(%d) = %s */\033[0m\n", id, id <= SYS_link ? callnames[id] : "unknown");
switch (id)
{
case SYS_exit:
{
int ec = arg ();
if (verbose)
printf ("[exit %d]\n", ec);
return RX_MAKE_EXITED (ec);
}
break;
case SYS_open:
{
int path = arg ();
/* The open function is defined as taking a variable number of arguments
because the third parameter to it is optional:
open (const char * filename, int flags, ...);
Hence the oflags and cflags arguments will be on the stack and we need
to skip the (empty) argument registers r3 and r4. */
argp = 4;
int oflags = arg ();
int cflags = arg ();
read_target (buf, path, 256, 1);
if (trace)
printf ("open(\"%s\",0x%x,%#o) = ", buf, oflags, cflags);
if (callbacks)
/* The callback vector ignores CFLAGS. */
rv = callbacks->open (callbacks, buf, oflags);
else
{
int h_oflags = 0;
if (oflags & 0x0001)
h_oflags |= O_WRONLY;
if (oflags & 0x0002)
h_oflags |= O_RDWR;
if (oflags & 0x0200)
h_oflags |= O_CREAT;
if (oflags & 0x0008)
h_oflags |= O_APPEND;
if (oflags & 0x0400)
h_oflags |= O_TRUNC;
rv = open (buf, h_oflags, cflags);
}
if (trace)
printf ("%d\n", rv);
put_reg (1, rv);
}
break;
case SYS_close:
{
int fd = arg ();
if (callbacks)
rv = callbacks->close (callbacks, fd);
else if (fd > 2)
rv = close (fd);
else
rv = 0;
if (trace)
printf ("close(%d) = %d\n", fd, rv);
put_reg (1, rv);
}
break;
case SYS_read:
{
int fd = arg ();
int addr = arg ();
int count = arg ();
if (count > sizeof (buf))
count = sizeof (buf);
if (callbacks)
rv = callbacks->read (callbacks, fd, buf, count);
else
rv = read (fd, buf, count);
if (trace)
printf ("read(%d,%d) = %d\n", fd, count, rv);
if (rv > 0)
write_target (buf, addr, rv, 0);
put_reg (1, rv);
}
break;
case SYS_write:
{
int fd = arg ();
int addr = arg ();
int count = arg ();
if (count > sizeof (buf))
count = sizeof (buf);
if (trace)
printf ("write(%d,0x%x,%d)\n", fd, addr, count);
read_target (buf, addr, count, 0);
if (trace)
fflush (stdout);
if (callbacks)
rv = callbacks->write (callbacks, fd, buf, count);
else
rv = write (fd, buf, count);
if (trace)
printf ("write(%d,%d) = %d\n", fd, count, rv);
put_reg (1, rv);
}
break;
case SYS_getpid:
put_reg (1, 42);
break;
case SYS_gettimeofday:
{
int tvaddr = arg ();
struct timeval tv;
rv = gettimeofday (&tv, 0);
if (trace)
printf ("gettimeofday: %ld sec %ld usec to 0x%x\n", tv.tv_sec,
tv.tv_usec, tvaddr);
mem_put_si (tvaddr, tv.tv_sec);
mem_put_si (tvaddr + 4, tv.tv_usec);
put_reg (1, rv);
}
break;
case SYS_kill:
{
int pid = arg ();
int sig = arg ();
if (pid == 42)
{
if (verbose)
printf ("[signal %d]\n", sig);
return RX_MAKE_STOPPED (sig);
}
}
break;
case 11:
{
int heaptop_arg = arg ();
if (trace)
printf ("sbrk: heap top set to %x\n", heaptop_arg);
heaptop = heaptop_arg;
if (heapbottom == 0)
heapbottom = heaptop_arg;
}
break;
case 255:
{
int addr = arg ();
mem_put_si (addr, rx_cycles + mem_usage_cycles());
}
break;
}
return RX_MAKE_STEPPED ();
}
|