/* syscalls.c --- implement system calls for the M32C simulator.
|
/* syscalls.c --- implement system calls for the M32C simulator.
|
|
|
Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
|
Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
|
Contributed by Red Hat, Inc.
|
Contributed by Red Hat, Inc.
|
|
|
This file is part of the GNU simulators.
|
This file is part of the GNU simulators.
|
|
|
This program is free software; you can redistribute it and/or modify
|
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
|
it under the terms of the GNU General Public License as published by
|
the Free Software Foundation; either version 3 of the License, or
|
the Free Software Foundation; either version 3 of the License, or
|
(at your option) any later version.
|
(at your option) any later version.
|
|
|
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU General Public License for more details.
|
GNU General Public License for more details.
|
|
|
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#include <sys/time.h>
|
#include <sys/time.h>
|
|
|
#include "gdb/callback.h"
|
#include "gdb/callback.h"
|
|
|
#include "cpu.h"
|
#include "cpu.h"
|
#include "mem.h"
|
#include "mem.h"
|
#include "syscalls.h"
|
#include "syscalls.h"
|
|
|
#include "syscall.h"
|
#include "syscall.h"
|
|
|
/* The current syscall callbacks we're using. */
|
/* The current syscall callbacks we're using. */
|
static struct host_callback_struct *callbacks;
|
static struct host_callback_struct *callbacks;
|
|
|
void
|
void
|
set_callbacks (struct host_callback_struct *cb)
|
set_callbacks (struct host_callback_struct *cb)
|
{
|
{
|
callbacks = cb;
|
callbacks = cb;
|
}
|
}
|
|
|
|
|
/* A16 ABI: arg1 in r1l (QI) or r1 (HI) or stack
|
/* A16 ABI: arg1 in r1l (QI) or r1 (HI) or stack
|
arg2 in r2 (HI) or stack
|
arg2 in r2 (HI) or stack
|
arg3..N on stack
|
arg3..N on stack
|
padding: none
|
padding: none
|
|
|
A24 ABI: arg1 in r0l (QI) or r0 (HI) or stack
|
A24 ABI: arg1 in r0l (QI) or r0 (HI) or stack
|
arg2..N on stack
|
arg2..N on stack
|
padding: qi->hi
|
padding: qi->hi
|
|
|
return value in r0l (QI) r0 (HI) r2r0 (SI)
|
return value in r0l (QI) r0 (HI) r2r0 (SI)
|
structs: pointer pushed on stack last
|
structs: pointer pushed on stack last
|
|
|
*/
|
*/
|
|
|
int argp, stackp;
|
int argp, stackp;
|
|
|
static int
|
static int
|
arg (int bytes)
|
arg (int bytes)
|
{
|
{
|
int rv = 0;
|
int rv = 0;
|
argp++;
|
argp++;
|
if (A16)
|
if (A16)
|
{
|
{
|
switch (argp)
|
switch (argp)
|
{
|
{
|
case 1:
|
case 1:
|
if (bytes == 1)
|
if (bytes == 1)
|
return get_reg (r1l);
|
return get_reg (r1l);
|
if (bytes == 2)
|
if (bytes == 2)
|
return get_reg (r1);
|
return get_reg (r1);
|
break;
|
break;
|
case 2:
|
case 2:
|
if (bytes == 2)
|
if (bytes == 2)
|
return get_reg (r2);
|
return get_reg (r2);
|
break;
|
break;
|
}
|
}
|
}
|
}
|
else
|
else
|
{
|
{
|
switch (argp)
|
switch (argp)
|
{
|
{
|
case 1:
|
case 1:
|
if (bytes == 1)
|
if (bytes == 1)
|
return get_reg (r0l);
|
return get_reg (r0l);
|
if (bytes == 2)
|
if (bytes == 2)
|
return get_reg (r0);
|
return get_reg (r0);
|
break;
|
break;
|
}
|
}
|
}
|
}
|
if (bytes == 0)
|
if (bytes == 0)
|
bytes = 2;
|
bytes = 2;
|
switch (bytes)
|
switch (bytes)
|
{
|
{
|
case 1:
|
case 1:
|
rv = mem_get_qi (get_reg (sp) + stackp);
|
rv = mem_get_qi (get_reg (sp) + stackp);
|
if (A24)
|
if (A24)
|
stackp++;
|
stackp++;
|
break;
|
break;
|
case 2:
|
case 2:
|
rv = mem_get_hi (get_reg (sp) + stackp);
|
rv = mem_get_hi (get_reg (sp) + stackp);
|
break;
|
break;
|
case 3:
|
case 3:
|
rv = mem_get_psi (get_reg (sp) + stackp);
|
rv = mem_get_psi (get_reg (sp) + stackp);
|
if (A24)
|
if (A24)
|
stackp++;
|
stackp++;
|
break;
|
break;
|
case 4:
|
case 4:
|
rv = mem_get_si (get_reg (sp) + stackp);
|
rv = mem_get_si (get_reg (sp) + stackp);
|
break;
|
break;
|
}
|
}
|
stackp += bytes;
|
stackp += bytes;
|
return rv;
|
return rv;
|
}
|
}
|
|
|
static void
|
static void
|
read_target (char *buffer, int address, int count, int asciiz)
|
read_target (char *buffer, int address, int count, int asciiz)
|
{
|
{
|
char byte;
|
char byte;
|
while (count > 0)
|
while (count > 0)
|
{
|
{
|
byte = mem_get_qi (address++);
|
byte = mem_get_qi (address++);
|
*buffer++ = byte;
|
*buffer++ = byte;
|
if (asciiz && (byte == 0))
|
if (asciiz && (byte == 0))
|
return;
|
return;
|
count--;
|
count--;
|
}
|
}
|
}
|
}
|
|
|
static void
|
static void
|
write_target (char *buffer, int address, int count, int asciiz)
|
write_target (char *buffer, int address, int count, int asciiz)
|
{
|
{
|
char byte;
|
char byte;
|
while (count > 0)
|
while (count > 0)
|
{
|
{
|
byte = *buffer++;
|
byte = *buffer++;
|
mem_put_qi (address++, byte);
|
mem_put_qi (address++, byte);
|
if (asciiz && (byte == 0))
|
if (asciiz && (byte == 0))
|
return;
|
return;
|
count--;
|
count--;
|
}
|
}
|
}
|
}
|
|
|
#define PTRSZ (A16 ? 2 : 3)
|
#define PTRSZ (A16 ? 2 : 3)
|
|
|
static char *callnames[] = {
|
static char *callnames[] = {
|
"SYS_zero",
|
"SYS_zero",
|
"SYS_exit",
|
"SYS_exit",
|
"SYS_open",
|
"SYS_open",
|
"SYS_close",
|
"SYS_close",
|
"SYS_read",
|
"SYS_read",
|
"SYS_write",
|
"SYS_write",
|
"SYS_lseek",
|
"SYS_lseek",
|
"SYS_unlink",
|
"SYS_unlink",
|
"SYS_getpid",
|
"SYS_getpid",
|
"SYS_kill",
|
"SYS_kill",
|
"SYS_fstat",
|
"SYS_fstat",
|
"SYS_sbrk",
|
"SYS_sbrk",
|
"SYS_argvlen",
|
"SYS_argvlen",
|
"SYS_argv",
|
"SYS_argv",
|
"SYS_chdir",
|
"SYS_chdir",
|
"SYS_stat",
|
"SYS_stat",
|
"SYS_chmod",
|
"SYS_chmod",
|
"SYS_utime",
|
"SYS_utime",
|
"SYS_time",
|
"SYS_time",
|
"SYS_gettimeofday",
|
"SYS_gettimeofday",
|
"SYS_times",
|
"SYS_times",
|
"SYS_link"
|
"SYS_link"
|
};
|
};
|
|
|
void
|
void
|
m32c_syscall (int id)
|
m32c_syscall (int id)
|
{
|
{
|
static char buf[256];
|
static char buf[256];
|
int rv;
|
int rv;
|
|
|
argp = 0;
|
argp = 0;
|
stackp = A16 ? 3 : 4;
|
stackp = A16 ? 3 : 4;
|
if (trace)
|
if (trace)
|
printf ("\033[31m/* SYSCALL(%d) = %s */\033[0m\n", id, callnames[id]);
|
printf ("\033[31m/* SYSCALL(%d) = %s */\033[0m\n", id, callnames[id]);
|
switch (id)
|
switch (id)
|
{
|
{
|
case SYS_exit:
|
case SYS_exit:
|
{
|
{
|
int ec = arg (2);
|
int ec = arg (2);
|
if (verbose)
|
if (verbose)
|
printf ("[exit %d]\n", ec);
|
printf ("[exit %d]\n", ec);
|
step_result = M32C_MAKE_EXITED (ec);
|
step_result = M32C_MAKE_EXITED (ec);
|
}
|
}
|
break;
|
break;
|
|
|
case SYS_open:
|
case SYS_open:
|
{
|
{
|
int path = arg (PTRSZ);
|
int path = arg (PTRSZ);
|
int oflags = arg (2);
|
int oflags = arg (2);
|
int cflags = arg (2);
|
int cflags = arg (2);
|
|
|
read_target (buf, path, 256, 1);
|
read_target (buf, path, 256, 1);
|
if (trace)
|
if (trace)
|
printf ("open(\"%s\",0x%x,%#o) = ", buf, oflags, cflags);
|
printf ("open(\"%s\",0x%x,%#o) = ", buf, oflags, cflags);
|
|
|
if (callbacks)
|
if (callbacks)
|
/* The callback vector ignores CFLAGS. */
|
/* The callback vector ignores CFLAGS. */
|
rv = callbacks->open (callbacks, buf, oflags);
|
rv = callbacks->open (callbacks, buf, oflags);
|
else
|
else
|
{
|
{
|
int h_oflags = 0;
|
int h_oflags = 0;
|
|
|
if (oflags & 0x0001)
|
if (oflags & 0x0001)
|
h_oflags |= O_WRONLY;
|
h_oflags |= O_WRONLY;
|
if (oflags & 0x0002)
|
if (oflags & 0x0002)
|
h_oflags |= O_RDWR;
|
h_oflags |= O_RDWR;
|
if (oflags & 0x0200)
|
if (oflags & 0x0200)
|
h_oflags |= O_CREAT;
|
h_oflags |= O_CREAT;
|
if (oflags & 0x0008)
|
if (oflags & 0x0008)
|
h_oflags |= O_APPEND;
|
h_oflags |= O_APPEND;
|
if (oflags & 0x0400)
|
if (oflags & 0x0400)
|
h_oflags |= O_TRUNC;
|
h_oflags |= O_TRUNC;
|
rv = open (buf, h_oflags, cflags);
|
rv = open (buf, h_oflags, cflags);
|
}
|
}
|
if (trace)
|
if (trace)
|
printf ("%d\n", rv);
|
printf ("%d\n", rv);
|
put_reg (r0, rv);
|
put_reg (r0, rv);
|
}
|
}
|
break;
|
break;
|
|
|
case SYS_close:
|
case SYS_close:
|
{
|
{
|
int fd = arg (2);
|
int fd = arg (2);
|
|
|
if (callbacks)
|
if (callbacks)
|
rv = callbacks->close (callbacks, fd);
|
rv = callbacks->close (callbacks, fd);
|
else if (fd > 2)
|
else if (fd > 2)
|
rv = close (fd);
|
rv = close (fd);
|
else
|
else
|
rv = 0;
|
rv = 0;
|
if (trace)
|
if (trace)
|
printf ("close(%d) = %d\n", fd, rv);
|
printf ("close(%d) = %d\n", fd, rv);
|
put_reg (r0, rv);
|
put_reg (r0, rv);
|
}
|
}
|
break;
|
break;
|
|
|
case SYS_read:
|
case SYS_read:
|
{
|
{
|
int fd = arg (2);
|
int fd = arg (2);
|
int addr = arg (PTRSZ);
|
int addr = arg (PTRSZ);
|
int count = arg (2);
|
int count = arg (2);
|
|
|
if (count > sizeof (buf))
|
if (count > sizeof (buf))
|
count = sizeof (buf);
|
count = sizeof (buf);
|
if (callbacks)
|
if (callbacks)
|
rv = callbacks->read (callbacks, fd, buf, count);
|
rv = callbacks->read (callbacks, fd, buf, count);
|
else
|
else
|
rv = read (fd, buf, count);
|
rv = read (fd, buf, count);
|
if (trace)
|
if (trace)
|
printf ("read(%d,%d) = %d\n", fd, count, rv);
|
printf ("read(%d,%d) = %d\n", fd, count, rv);
|
if (rv > 0)
|
if (rv > 0)
|
write_target (buf, addr, rv, 0);
|
write_target (buf, addr, rv, 0);
|
put_reg (r0, rv);
|
put_reg (r0, rv);
|
}
|
}
|
break;
|
break;
|
|
|
case SYS_write:
|
case SYS_write:
|
{
|
{
|
int fd = arg (2);
|
int fd = arg (2);
|
int addr = arg (PTRSZ);
|
int addr = arg (PTRSZ);
|
int count = arg (2);
|
int count = arg (2);
|
|
|
if (count > sizeof (buf))
|
if (count > sizeof (buf))
|
count = sizeof (buf);
|
count = sizeof (buf);
|
if (trace)
|
if (trace)
|
printf ("write(%d,0x%x,%d)\n", fd, addr, count);
|
printf ("write(%d,0x%x,%d)\n", fd, addr, count);
|
read_target (buf, addr, count, 0);
|
read_target (buf, addr, count, 0);
|
if (trace)
|
if (trace)
|
fflush (stdout);
|
fflush (stdout);
|
if (callbacks)
|
if (callbacks)
|
rv = callbacks->write (callbacks, fd, buf, count);
|
rv = callbacks->write (callbacks, fd, buf, count);
|
else
|
else
|
rv = write (fd, buf, count);
|
rv = write (fd, buf, count);
|
if (trace)
|
if (trace)
|
printf ("write(%d,%d) = %d\n", fd, count, rv);
|
printf ("write(%d,%d) = %d\n", fd, count, rv);
|
put_reg (r0, rv);
|
put_reg (r0, rv);
|
}
|
}
|
break;
|
break;
|
|
|
case SYS_getpid:
|
case SYS_getpid:
|
put_reg (r0, 42);
|
put_reg (r0, 42);
|
break;
|
break;
|
|
|
case SYS_gettimeofday:
|
case SYS_gettimeofday:
|
{
|
{
|
int tvaddr = arg (PTRSZ);
|
int tvaddr = arg (PTRSZ);
|
struct timeval tv;
|
struct timeval tv;
|
|
|
rv = gettimeofday (&tv, 0);
|
rv = gettimeofday (&tv, 0);
|
if (trace)
|
if (trace)
|
printf ("gettimeofday: %ld sec %ld usec to 0x%x\n", tv.tv_sec,
|
printf ("gettimeofday: %ld sec %ld usec to 0x%x\n", tv.tv_sec,
|
tv.tv_usec, tvaddr);
|
tv.tv_usec, tvaddr);
|
mem_put_si (tvaddr, tv.tv_sec);
|
mem_put_si (tvaddr, tv.tv_sec);
|
mem_put_si (tvaddr + 4, tv.tv_usec);
|
mem_put_si (tvaddr + 4, tv.tv_usec);
|
put_reg (r0, rv);
|
put_reg (r0, rv);
|
}
|
}
|
break;
|
break;
|
|
|
case SYS_kill:
|
case SYS_kill:
|
{
|
{
|
int pid = arg (2);
|
int pid = arg (2);
|
int sig = arg (2);
|
int sig = arg (2);
|
if (pid == 42)
|
if (pid == 42)
|
{
|
{
|
if (verbose)
|
if (verbose)
|
printf ("[signal %d]\n", sig);
|
printf ("[signal %d]\n", sig);
|
step_result = M32C_MAKE_STOPPED (sig);
|
step_result = M32C_MAKE_STOPPED (sig);
|
}
|
}
|
}
|
}
|
break;
|
break;
|
|
|
case 11:
|
case 11:
|
{
|
{
|
int heaptop_arg = arg (PTRSZ);
|
int heaptop_arg = arg (PTRSZ);
|
if (trace)
|
if (trace)
|
printf ("sbrk: heap top set to %x\n", heaptop_arg);
|
printf ("sbrk: heap top set to %x\n", heaptop_arg);
|
heaptop = heaptop_arg;
|
heaptop = heaptop_arg;
|
if (heapbottom == 0)
|
if (heapbottom == 0)
|
heapbottom = heaptop_arg;
|
heapbottom = heaptop_arg;
|
}
|
}
|
break;
|
break;
|
|
|
}
|
}
|
}
|
}
|
|
|