1 /* syscalls.c --- implement system calls for the M32C simulator.
3 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
8 The GNU simulators are free software; you can redistribute them and/or
9 modify them under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 The GNU simulators are distributed in the hope that they will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with the GNU simulators; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 #include "gdb/callback.h"
38 /* The current syscall callbacks we're using. */
39 static struct host_callback_struct *callbacks;
42 set_callbacks (struct host_callback_struct *cb)
48 /* A16 ABI: arg1 in r1l (QI) or r1 (HI) or stack
49 arg2 in r2 (HI) or stack
53 A24 ABI: arg1 in r0l (QI) or r0 (HI) or stack
57 return value in r0l (QI) r0 (HI) r2r0 (SI)
58 structs: pointer pushed on stack last
102 rv = mem_get_qi (get_reg (sp) + stackp);
107 rv = mem_get_hi (get_reg (sp) + stackp);
110 rv = mem_get_psi (get_reg (sp) + stackp);
115 rv = mem_get_si (get_reg (sp) + stackp);
123 read_target (char *buffer, int address, int count, int asciiz)
128 byte = mem_get_qi (address++);
130 if (asciiz && (byte == 0))
137 write_target (char *buffer, int address, int count, int asciiz)
143 mem_put_qi (address++, byte);
144 if (asciiz && (byte == 0))
150 #define PTRSZ (A16 ? 2 : 3)
152 static char *callnames[] = {
178 m32c_syscall (int id)
180 static char buf[256];
184 stackp = A16 ? 3 : 4;
186 printf ("\033[31m/* SYSCALL(%d) = %s */\033[0m\n", id, callnames[id]);
193 printf ("[exit %d]\n", ec);
194 step_result = M32C_MAKE_EXITED (ec);
200 int path = arg (PTRSZ);
201 int oflags = arg (2);
202 int cflags = arg (2);
204 read_target (buf, path, 256, 1);
206 printf ("open(\"%s\",0x%x,%#o) = ", buf, oflags, cflags);
209 /* The callback vector ignores CFLAGS. */
210 rv = callbacks->open (callbacks, buf, oflags);
216 h_oflags |= O_WRONLY;
222 h_oflags |= O_APPEND;
225 rv = open (buf, h_oflags, cflags);
238 rv = callbacks->close (callbacks, fd);
244 printf ("close(%d) = %d\n", fd, rv);
252 int addr = arg (PTRSZ);
255 if (count > sizeof (buf))
256 count = sizeof (buf);
258 rv = callbacks->read (callbacks, fd, buf, count);
260 rv = read (fd, buf, count);
262 printf ("read(%d,%d) = %d\n", fd, count, rv);
264 write_target (buf, addr, rv, 0);
272 int addr = arg (PTRSZ);
275 if (count > sizeof (buf))
276 count = sizeof (buf);
278 printf ("write(%d,0x%x,%d)\n", fd, addr, count);
279 read_target (buf, addr, count, 0);
283 rv = callbacks->write (callbacks, fd, buf, count);
285 rv = write (fd, buf, count);
287 printf ("write(%d,%d) = %d\n", fd, count, rv);
296 case SYS_gettimeofday:
298 int tvaddr = arg (PTRSZ);
301 rv = gettimeofday (&tv, 0);
303 printf ("gettimeofday: %ld sec %ld usec to 0x%x\n", tv.tv_sec,
305 mem_put_si (tvaddr, tv.tv_sec);
306 mem_put_si (tvaddr + 4, tv.tv_usec);
318 printf ("[signal %d]\n", sig);
319 step_result = M32C_MAKE_STOPPED (sig);
326 int heaptop_arg = arg (PTRSZ);
328 printf ("sbrk: heap top set to %x\n", heaptop_arg);
329 heaptop = heaptop_arg;
331 heapbottom = heaptop_arg;