1 /* syscalls.c --- implement system calls for the M32C simulator.
3 Copyright (C) 2005-2024 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/>. */
21 /* This must come before any other includes. */
30 #include "sim/callback.h"
35 #include "target-newlib-syscall.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
]);
188 case TARGET_NEWLIB_SYS_exit
:
192 printf ("[exit %d]\n", ec
);
193 step_result
= M32C_MAKE_EXITED (ec
);
197 case TARGET_NEWLIB_SYS_open
:
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
);
232 case TARGET_NEWLIB_SYS_close
:
237 rv
= callbacks
->close (callbacks
, fd
);
243 printf ("close(%d) = %d\n", fd
, rv
);
248 case TARGET_NEWLIB_SYS_read
:
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);
268 case TARGET_NEWLIB_SYS_write
:
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
);
291 case TARGET_NEWLIB_SYS_getpid
:
295 case TARGET_NEWLIB_SYS_gettimeofday
:
297 int tvaddr
= arg (PTRSZ
);
300 rv
= gettimeofday (&tv
, 0);
302 printf ("gettimeofday: %" PRId64
" sec %" PRId64
" usec to 0x%x\n",
303 (int64_t)tv
.tv_sec
, (int64_t)tv
.tv_usec
, tvaddr
);
304 mem_put_si (tvaddr
, tv
.tv_sec
);
305 mem_put_si (tvaddr
+ 4, tv
.tv_usec
);
310 case TARGET_NEWLIB_SYS_kill
:
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
;