1 /* syscalls.c --- implement system calls for the M32C simulator.
3 Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Red Hat, Inc.
7 This file is part of the GNU simulators.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "gdb/callback.h"
37 /* The current syscall callbacks we're using. */
38 static struct host_callback_struct *callbacks;
41 set_callbacks (struct host_callback_struct *cb)
47 /* A16 ABI: arg1 in r1l (QI) or r1 (HI) or stack
48 arg2 in r2 (HI) or stack
52 A24 ABI: arg1 in r0l (QI) or r0 (HI) or stack
56 return value in r0l (QI) r0 (HI) r2r0 (SI)
57 structs: pointer pushed on stack last
101 rv = mem_get_qi (get_reg (sp) + stackp);
106 rv = mem_get_hi (get_reg (sp) + stackp);
109 rv = mem_get_psi (get_reg (sp) + stackp);
114 rv = mem_get_si (get_reg (sp) + stackp);
122 read_target (char *buffer, int address, int count, int asciiz)
127 byte = mem_get_qi (address++);
129 if (asciiz && (byte == 0))
136 write_target (char *buffer, int address, int count, int asciiz)
142 mem_put_qi (address++, byte);
143 if (asciiz && (byte == 0))
149 #define PTRSZ (A16 ? 2 : 3)
151 static char *callnames[] = {
177 m32c_syscall (int id)
179 static char buf[256];
183 stackp = A16 ? 3 : 4;
185 printf ("\033[31m/* SYSCALL(%d) = %s */\033[0m\n", id, callnames[id]);
192 printf ("[exit %d]\n", ec);
193 step_result = M32C_MAKE_EXITED (ec);
199 int path = arg (PTRSZ);
200 int oflags = arg (2);
201 int cflags = arg (2);
203 read_target (buf, path, 256, 1);
205 printf ("open(\"%s\",0x%x,%#o) = ", buf, oflags, cflags);
208 /* The callback vector ignores CFLAGS. */
209 rv = callbacks->open (callbacks, buf, oflags);
215 h_oflags |= O_WRONLY;
221 h_oflags |= O_APPEND;
224 rv = open (buf, h_oflags, cflags);
237 rv = callbacks->close (callbacks, fd);
243 printf ("close(%d) = %d\n", fd, rv);
251 int addr = arg (PTRSZ);
254 if (count > sizeof (buf))
255 count = sizeof (buf);
257 rv = callbacks->read (callbacks, fd, buf, count);
259 rv = read (fd, buf, count);
261 printf ("read(%d,%d) = %d\n", fd, count, rv);
263 write_target (buf, addr, rv, 0);
271 int addr = arg (PTRSZ);
274 if (count > sizeof (buf))
275 count = sizeof (buf);
277 printf ("write(%d,0x%x,%d)\n", fd, addr, count);
278 read_target (buf, addr, count, 0);
282 rv = callbacks->write (callbacks, fd, buf, count);
284 rv = write (fd, buf, count);
286 printf ("write(%d,%d) = %d\n", fd, count, rv);
295 case SYS_gettimeofday:
297 int tvaddr = arg (PTRSZ);
300 rv = gettimeofday (&tv, 0);
302 printf ("gettimeofday: %ld sec %ld usec to 0x%x\n", tv.tv_sec,
304 mem_put_si (tvaddr, tv.tv_sec);
305 mem_put_si (tvaddr + 4, tv.tv_usec);
317 printf ("[signal %d]\n", sig);
318 step_result = M32C_MAKE_STOPPED (sig);
325 int heaptop_arg = arg (PTRSZ);
327 printf ("sbrk: heap top set to %x\n", heaptop_arg);
328 heaptop = heaptop_arg;
330 heapbottom = heaptop_arg;