1 /* syscalls.c --- implement system calls for the M32C simulator.
3 Copyright (C) 2005-2018 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "gdb/callback.h"
36 /* The current syscall callbacks we're using. */
37 static struct host_callback_struct
*callbacks
;
40 set_callbacks (struct host_callback_struct
*cb
)
46 /* A16 ABI: arg1 in r1l (QI) or r1 (HI) or stack
47 arg2 in r2 (HI) or stack
51 A24 ABI: arg1 in r0l (QI) or r0 (HI) or stack
55 return value in r0l (QI) r0 (HI) r2r0 (SI)
56 structs: pointer pushed on stack last
100 rv
= mem_get_qi (get_reg (sp
) + stackp
);
105 rv
= mem_get_hi (get_reg (sp
) + stackp
);
108 rv
= mem_get_psi (get_reg (sp
) + stackp
);
113 rv
= mem_get_si (get_reg (sp
) + stackp
);
121 read_target (char *buffer
, int address
, int count
, int asciiz
)
126 byte
= mem_get_qi (address
++);
128 if (asciiz
&& (byte
== 0))
135 write_target (char *buffer
, int address
, int count
, int asciiz
)
141 mem_put_qi (address
++, byte
);
142 if (asciiz
&& (byte
== 0))
148 #define PTRSZ (A16 ? 2 : 3)
150 static char *callnames
[] = {
176 m32c_syscall (int id
)
178 static char buf
[256];
182 stackp
= A16
? 3 : 4;
184 printf ("\033[31m/* SYSCALL(%d) = %s */\033[0m\n", id
, callnames
[id
]);
191 printf ("[exit %d]\n", ec
);
192 step_result
= M32C_MAKE_EXITED (ec
);
198 int path
= arg (PTRSZ
);
199 int oflags
= arg (2);
200 int cflags
= arg (2);
202 read_target (buf
, path
, 256, 1);
204 printf ("open(\"%s\",0x%x,%#o) = ", buf
, oflags
, cflags
);
207 /* The callback vector ignores CFLAGS. */
208 rv
= callbacks
->open (callbacks
, buf
, oflags
);
214 h_oflags
|= O_WRONLY
;
220 h_oflags
|= O_APPEND
;
223 rv
= open (buf
, h_oflags
, cflags
);
236 rv
= callbacks
->close (callbacks
, fd
);
242 printf ("close(%d) = %d\n", fd
, rv
);
250 int addr
= arg (PTRSZ
);
253 if (count
> sizeof (buf
))
254 count
= sizeof (buf
);
256 rv
= callbacks
->read (callbacks
, fd
, buf
, count
);
258 rv
= read (fd
, buf
, count
);
260 printf ("read(%d,%d) = %d\n", fd
, count
, rv
);
262 write_target (buf
, addr
, rv
, 0);
270 int addr
= arg (PTRSZ
);
273 if (count
> sizeof (buf
))
274 count
= sizeof (buf
);
276 printf ("write(%d,0x%x,%d)\n", fd
, addr
, count
);
277 read_target (buf
, addr
, count
, 0);
281 rv
= callbacks
->write (callbacks
, fd
, buf
, count
);
283 rv
= write (fd
, buf
, count
);
285 printf ("write(%d,%d) = %d\n", fd
, count
, rv
);
294 case SYS_gettimeofday
:
296 int tvaddr
= arg (PTRSZ
);
299 rv
= gettimeofday (&tv
, 0);
301 printf ("gettimeofday: %ld sec %ld usec to 0x%x\n", tv
.tv_sec
,
303 mem_put_si (tvaddr
, tv
.tv_sec
);
304 mem_put_si (tvaddr
+ 4, tv
.tv_usec
);
316 printf ("[signal %d]\n", sig
);
317 step_result
= M32C_MAKE_STOPPED (sig
);
324 int heaptop_arg
= arg (PTRSZ
);
326 printf ("sbrk: heap top set to %x\n", heaptop_arg
);
327 heaptop
= heaptop_arg
;
329 heapbottom
= heaptop_arg
;