]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/sim-syscall.c
sim: switch config.h usage to defs.h
[thirdparty/binutils-gdb.git] / sim / common / sim-syscall.c
CommitLineData
61a0c964
MF
1/* Simulator system call support.
2
3666a048 3 Copyright 2002-2021 Free Software Foundation, Inc.
61a0c964
MF
4
5 This file is part of simulators.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
6df01ab8
MF
20/* This must come before any other includes. */
21#include "defs.h"
61a0c964 22
7d5c6c43
MF
23#include <errno.h>
24
61a0c964
MF
25#include "sim-main.h"
26#include "sim-syscall.h"
7d5c6c43 27#include "targ-vals.h"
61a0c964
MF
28\f
29/* Read/write functions for system call interface. */
30
31int
32sim_syscall_read_mem (host_callback *cb ATTRIBUTE_UNUSED, struct cb_syscall *sc,
33 unsigned long taddr, char *buf, int bytes)
34{
35 SIM_DESC sd = (SIM_DESC) sc->p1;
36 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
37
38 TRACE_MEMORY (cpu, "READ (syscall) %i bytes @ 0x%08lx", bytes, taddr);
39
40 return sim_core_read_buffer (sd, cpu, read_map, buf, taddr, bytes);
41}
42
43int
44sim_syscall_write_mem (host_callback *cb ATTRIBUTE_UNUSED, struct cb_syscall *sc,
45 unsigned long taddr, const char *buf, int bytes)
46{
47 SIM_DESC sd = (SIM_DESC) sc->p1;
48 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
49
50 TRACE_MEMORY (cpu, "WRITE (syscall) %i bytes @ 0x%08lx", bytes, taddr);
51
52 return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes);
53}
7d5c6c43
MF
54\f
55/* Main syscall callback for simulators. */
56
57void
58sim_syscall_multi (SIM_CPU *cpu, int func, long arg1, long arg2, long arg3,
59 long arg4, long *result, long *result2, int *errcode)
60{
61 SIM_DESC sd = CPU_STATE (cpu);
62 host_callback *cb = STATE_CALLBACK (sd);
63 CB_SYSCALL sc;
57b42d64 64 const char unknown_syscall[] = "<UNKNOWN SYSCALL>";
7d5c6c43
MF
65 const char *syscall;
66
67 CB_SYSCALL_INIT (&sc);
68
69 sc.func = func;
70 sc.arg1 = arg1;
71 sc.arg2 = arg2;
72 sc.arg3 = arg3;
73 sc.arg4 = arg4;
74
64654371
MF
75 sc.p1 = sd;
76 sc.p2 = cpu;
7d5c6c43
MF
77 sc.read_mem = sim_syscall_read_mem;
78 sc.write_mem = sim_syscall_write_mem;
79
80 if (cb_syscall (cb, &sc) != CB_RC_OK)
81 {
82 /* The cb_syscall func never returns an error, so this is more of a
83 sanity check. */
84 sim_engine_abort (sd, cpu, sim_pc_get (cpu), "cb_syscall failed");
85 }
86
87 syscall = cb_target_str_syscall (cb, func);
88 if (!syscall)
57b42d64 89 syscall = unknown_syscall;
7d5c6c43
MF
90
91 if (sc.result == -1)
92 TRACE_SYSCALL (cpu, "%s[%i](%#lx, %#lx, %#lx) = %li (error = %s[%i])",
93 syscall, func, arg1, arg2, arg3, sc.result,
94 cb_target_str_errno (cb, sc.errcode), sc.errcode);
95 else
96 TRACE_SYSCALL (cpu, "%s[%i](%#lx, %#lx, %#lx) = %li",
97 syscall, func, arg1, arg2, arg3, sc.result);
98
99 if (cb_target_to_host_syscall (cb, func) == CB_SYS_exit)
100 sim_engine_halt (sd, cpu, NULL, sim_pc_get (cpu), sim_exited, arg1);
7d5c6c43
MF
101
102 *result = sc.result;
103 *result2 = sc.result2;
104 *errcode = sc.errcode;
105}
106
107long
108sim_syscall (SIM_CPU *cpu, int func, long arg1, long arg2, long arg3, long arg4)
109{
110 long result, result2;
111 int errcode;
112
113 sim_syscall_multi (cpu, func, arg1, arg2, arg3, arg4, &result, &result2,
114 &errcode);
115 if (result == -1)
116 return -errcode;
117 else
118 return result;
119}