• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GNU Binutils with patches for OS216


Commit MetaInfo

修訂7d5c6c43ca8a5dd5491f4a58e977ec5501386ee3 (tree)
時間2015-06-18 02:19:51
作者Mike Frysinger <vapier@gent...>
CommiterMike Frysinger

Log Message

sim: syscall: add common sim_syscall helpers

Many ports have the same sim syscall logic, so add some helpers to handle
all the common details. The arches still have to deal with the unpacking
and packing of the syscall arguments, but the rest of the sim<->callback
glue is now shared.

Change Summary

差異

--- a/sim/common/ChangeLog
+++ b/sim/common/ChangeLog
@@ -1,5 +1,12 @@
11 2015-06-17 Mike Frysinger <vapier@gentoo.org>
22
3+ * sim-syscall.c: Include errno.h and targ-vals.h.
4+ (sim_syscall_multi, sim_syscall): Define.
5+ * sim-syscall.h (sim_syscall_multi, sim_syscall): Declare.
6+ * syscall.c (cb_syscall): Extend comment.
7+
8+2015-06-17 Mike Frysinger <vapier@gentoo.org>
9+
310 * Make-common.in (SIM_NEW_COMMON_OBJS): Add sim-syscall.o.
411 * sim-syscall.c: New file.
512 * sim-syscall.h: New file.
--- a/sim/common/sim-syscall.c
+++ b/sim/common/sim-syscall.c
@@ -19,8 +19,11 @@
1919
2020 #include "config.h"
2121
22+#include <errno.h>
23+
2224 #include "sim-main.h"
2325 #include "sim-syscall.h"
26+#include "targ-vals.h"
2427
2528 /* Read/write functions for system call interface. */
2629
@@ -47,3 +50,77 @@ sim_syscall_write_mem (host_callback *cb ATTRIBUTE_UNUSED, struct cb_syscall *sc
4750
4851 return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes);
4952 }
53+
54+/* Main syscall callback for simulators. */
55+
56+void
57+sim_syscall_multi (SIM_CPU *cpu, int func, long arg1, long arg2, long arg3,
58+ long arg4, long *result, long *result2, int *errcode)
59+{
60+ SIM_DESC sd = CPU_STATE (cpu);
61+ host_callback *cb = STATE_CALLBACK (sd);
62+ CB_SYSCALL sc;
63+ char unknown_syscall[30];
64+ const char *syscall;
65+
66+ CB_SYSCALL_INIT (&sc);
67+
68+ sc.func = func;
69+ sc.arg1 = arg1;
70+ sc.arg2 = arg2;
71+ sc.arg3 = arg3;
72+ sc.arg4 = arg4;
73+
74+ sc.p1 = (PTR) sd;
75+ sc.p2 = (PTR) cpu;
76+ sc.read_mem = sim_syscall_read_mem;
77+ sc.write_mem = sim_syscall_write_mem;
78+
79+ if (cb_syscall (cb, &sc) != CB_RC_OK)
80+ {
81+ /* The cb_syscall func never returns an error, so this is more of a
82+ sanity check. */
83+ sim_engine_abort (sd, cpu, sim_pc_get (cpu), "cb_syscall failed");
84+ }
85+
86+ syscall = cb_target_str_syscall (cb, func);
87+ if (!syscall)
88+ {
89+ sprintf (unknown_syscall, "syscall_%i", func);
90+ syscall = unknown_syscall;
91+ }
92+
93+ if (sc.result == -1)
94+ TRACE_SYSCALL (cpu, "%s[%i](%#lx, %#lx, %#lx) = %li (error = %s[%i])",
95+ syscall, func, arg1, arg2, arg3, sc.result,
96+ cb_target_str_errno (cb, sc.errcode), sc.errcode);
97+ else
98+ TRACE_SYSCALL (cpu, "%s[%i](%#lx, %#lx, %#lx) = %li",
99+ syscall, func, arg1, arg2, arg3, sc.result);
100+
101+ if (cb_target_to_host_syscall (cb, func) == CB_SYS_exit)
102+ sim_engine_halt (sd, cpu, NULL, sim_pc_get (cpu), sim_exited, arg1);
103+ else if (sc.result == -1)
104+ {
105+ cb->last_errno = errno;
106+ sc.errcode = cb->get_errno (cb);
107+ }
108+
109+ *result = sc.result;
110+ *result2 = sc.result2;
111+ *errcode = sc.errcode;
112+}
113+
114+long
115+sim_syscall (SIM_CPU *cpu, int func, long arg1, long arg2, long arg3, long arg4)
116+{
117+ long result, result2;
118+ int errcode;
119+
120+ sim_syscall_multi (cpu, func, arg1, arg2, arg3, arg4, &result, &result2,
121+ &errcode);
122+ if (result == -1)
123+ return -errcode;
124+ else
125+ return result;
126+}
--- a/sim/common/sim-syscall.h
+++ b/sim/common/sim-syscall.h
@@ -20,6 +20,21 @@
2020 #ifndef SIM_SYSCALL_H
2121 #define SIM_SYSCALL_H
2222
23+/* Perform a syscall on the behalf of the target program. The error/result are
24+ normalized into a single value (like a lot of operating systems do). If you
25+ want the split values, see the other function below.
26+
27+ Note: While cb_syscall requires you handle the exit syscall yourself, that is
28+ not the case with these helpers.
29+
30+ Note: Types here match the gdb callback interface. */
31+long sim_syscall (SIM_CPU *, int func, long arg1, long arg2, long arg3,
32+ long arg4);
33+
34+/* Same as sim_syscall, but return the split values by referenced. */
35+void sim_syscall_multi (SIM_CPU *, int func, long arg1, long arg2, long arg3,
36+ long arg4, long *result, long *result2, int *errcode);
37+
2338 /* Simple memory callbacks for cb_syscall's read_mem/write_mem that assume
2439 cb_syscall's p1 and p2 are set to the SIM_DESC and SIM_CPU respectively. */
2540 int sim_syscall_read_mem (host_callback *, struct cb_syscall *, unsigned long,
--- a/sim/common/syscall.c
+++ b/sim/common/syscall.c
@@ -240,7 +240,7 @@ cb_syscall (host_callback *cb, CB_SYSCALL *sc)
240240 #endif /* wip */
241241
242242 case CB_SYS_exit :
243- /* Caller must catch and handle. */
243+ /* Caller must catch and handle; see sim_syscall as an example. */
244244 break;
245245
246246 case CB_SYS_open :
--- a/sim/lm32/ChangeLog
+++ b/sim/lm32/ChangeLog
@@ -1,5 +1,10 @@
11 2015-06-17 Mike Frysinger <vapier@gentoo.org>
22
3+ * traps.c (lm32bf_scall_insn): Replace call to cb_syscall with
4+ sim_syscall_multi.
5+
6+2015-06-17 Mike Frysinger <vapier@gentoo.org>
7+
38 * traps.c: Include sim-syscall.h.
49 (syscall_read_mem, syscall_write_mem): Delete.
510 (lm32bf_scall_insn): Change syscall_read_mem/syscall_write_mem
--- a/sim/lm32/traps.c
+++ b/sim/lm32/traps.c
@@ -132,26 +132,18 @@ lm32bf_scall_insn (SIM_CPU * current_cpu, IADDR pc)
132132 || (GET_H_GR (8) == TARGET_SYS_exit))
133133 {
134134 /* Delegate system call to host O/S. */
135- CB_SYSCALL s;
136- CB_SYSCALL_INIT (&s);
137- s.p1 = (PTR) sd;
138- s.p2 = (PTR) current_cpu;
139- s.read_mem = sim_syscall_read_mem;
140- s.write_mem = sim_syscall_write_mem;
141- /* Extract parameters. */
142- s.func = GET_H_GR (8);
143- s.arg1 = GET_H_GR (1);
144- s.arg2 = GET_H_GR (2);
145- s.arg3 = GET_H_GR (3);
146- /* Halt the simulator if the requested system call is _exit. */
147- if (s.func == TARGET_SYS_exit)
148- sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, s.arg1);
135+ long result, result2;
136+ int errcode;
137+
149138 /* Perform the system call. */
150- cb_syscall (cb, &s);
139+ sim_syscall_multi (current_cpu, GET_H_GR (8), GET_H_GR (1), GET_H_GR (2),
140+ GET_H_GR (3), GET_H_GR (4), &result, &result2,
141+ &errcode);
151142 /* Store the return value in the CPU's registers. */
152- SET_H_GR (1, s.result);
153- SET_H_GR (2, s.result2);
154- SET_H_GR (3, s.errcode);
143+ SET_H_GR (1, result);
144+ SET_H_GR (2, result2);
145+ SET_H_GR (3, errcode);
146+
155147 /* Skip over scall instruction. */
156148 return pc + 4;
157149 }
--- a/sim/m32r/ChangeLog
+++ b/sim/m32r/ChangeLog
@@ -1,5 +1,11 @@
11 2015-06-17 Mike Frysinger <vapier@gentoo.org>
22
3+ * traps.c (m32r_trap): Replace call to cb_syscall with
4+ sim_syscall_multi.
5+ * traps-linux.c (m32r_trap): Likewise.
6+
7+2015-06-17 Mike Frysinger <vapier@gentoo.org>
8+
39 * traps-linux.c: Include sim-syscall.h.
410 (syscall_read_mem, syscall_write_mem): Delete.
511 (m32r_trap): Change syscall_read_mem/syscall_write_mem
--- a/sim/m32r/traps-linux.c
+++ b/sim/m32r/traps-linux.c
@@ -213,28 +213,21 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
213213 {
214214 case TRAP_ELF_SYSCALL :
215215 {
216- CB_SYSCALL s;
217-
218- CB_SYSCALL_INIT (&s);
219- s.func = m32rbf_h_gr_get (current_cpu, 0);
220- s.arg1 = m32rbf_h_gr_get (current_cpu, 1);
221- s.arg2 = m32rbf_h_gr_get (current_cpu, 2);
222- s.arg3 = m32rbf_h_gr_get (current_cpu, 3);
223-
224- if (s.func == TARGET_SYS_exit)
225- {
226- sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, s.arg1);
227- }
228-
229- s.p1 = (PTR) sd;
230- s.p2 = (PTR) current_cpu;
231- s.read_mem = sim_syscall_read_mem;
232- s.write_mem = sim_syscall_write_mem;
233- cb_syscall (cb, &s);
234- m32rbf_h_gr_set (current_cpu, 2, s.errcode);
235- m32rbf_h_gr_set (current_cpu, 0, s.result);
236- m32rbf_h_gr_set (current_cpu, 1, s.result2);
237- break;
216+ long result, result2;
217+ int errcode;
218+
219+ sim_syscall_multi (current_cpu,
220+ m32rbf_h_gr_get (current_cpu, 0),
221+ m32rbf_h_gr_get (current_cpu, 1),
222+ m32rbf_h_gr_get (current_cpu, 2),
223+ m32rbf_h_gr_get (current_cpu, 3),
224+ m32rbf_h_gr_get (current_cpu, 4),
225+ &result, &result2, &errcode);
226+
227+ m32rbf_h_gr_set (current_cpu, 2, errcode);
228+ m32rbf_h_gr_set (current_cpu, 0, result);
229+ m32rbf_h_gr_set (current_cpu, 1, result2);
230+ break;
238231 }
239232
240233 case TRAP_LINUX_SYSCALL :
--- a/sim/m32r/traps.c
+++ b/sim/m32r/traps.c
@@ -129,27 +129,20 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
129129 {
130130 case TRAP_SYSCALL :
131131 {
132- CB_SYSCALL s;
133-
134- CB_SYSCALL_INIT (&s);
135- s.func = m32rbf_h_gr_get (current_cpu, 0);
136- s.arg1 = m32rbf_h_gr_get (current_cpu, 1);
137- s.arg2 = m32rbf_h_gr_get (current_cpu, 2);
138- s.arg3 = m32rbf_h_gr_get (current_cpu, 3);
139-
140- if (s.func == TARGET_SYS_exit)
141- {
142- sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, s.arg1);
143- }
144-
145- s.p1 = (PTR) sd;
146- s.p2 = (PTR) current_cpu;
147- s.read_mem = sim_syscall_read_mem;
148- s.write_mem = sim_syscall_write_mem;
149- cb_syscall (cb, &s);
150- m32rbf_h_gr_set (current_cpu, 2, s.errcode);
151- m32rbf_h_gr_set (current_cpu, 0, s.result);
152- m32rbf_h_gr_set (current_cpu, 1, s.result2);
132+ long result, result2;
133+ int errcode;
134+
135+ sim_syscall_multi (current_cpu,
136+ m32rbf_h_gr_get (current_cpu, 0),
137+ m32rbf_h_gr_get (current_cpu, 1),
138+ m32rbf_h_gr_get (current_cpu, 2),
139+ m32rbf_h_gr_get (current_cpu, 3),
140+ m32rbf_h_gr_get (current_cpu, 4),
141+ &result, &result2, &errcode);
142+
143+ m32rbf_h_gr_set (current_cpu, 2, errcode);
144+ m32rbf_h_gr_set (current_cpu, 0, result);
145+ m32rbf_h_gr_set (current_cpu, 1, result2);
153146 break;
154147 }
155148
--- a/sim/mcore/ChangeLog
+++ b/sim/mcore/ChangeLog
@@ -1,5 +1,10 @@
11 2015-06-17 Mike Frysinger <vapier@gentoo.org>
22
3+ * interp.c (handle_trap1): Replace call to cb_syscall with
4+ sim_syscall.
5+
6+2015-06-17 Mike Frysinger <vapier@gentoo.org>
7+
38 * interp.c: Include sim-syscall.h.
49 (syscall_read_mem, syscall_write_mem): Delete.
510 (m32r_trap): Change syscall_read_mem/syscall_write_mem
--- a/sim/mcore/interp.c
+++ b/sim/mcore/interp.c
@@ -203,26 +203,10 @@ set_initial_gprs (SIM_CPU *scpu)
203203 static void
204204 handle_trap1 (SIM_DESC sd)
205205 {
206- host_callback *cb = STATE_CALLBACK (sd);
207- CB_SYSCALL sc;
208-
209- CB_SYSCALL_INIT (&sc);
210-
211- sc.func = cpu.gr[TRAPCODE];
212- sc.arg1 = cpu.gr[PARM1];
213- sc.arg2 = cpu.gr[PARM2];
214- sc.arg3 = cpu.gr[PARM3];
215- sc.arg4 = cpu.gr[PARM4];
216-
217- sc.p1 = (PTR) sd;
218- sc.p2 = (PTR) STATE_CPU (sd, 0);
219- sc.read_mem = sim_syscall_read_mem;
220- sc.write_mem = sim_syscall_write_mem;
221-
222- cb_syscall (cb, &sc);
223-
224206 /* XXX: We don't pass back the actual errno value. */
225- cpu.gr[RET1] = sc.result;
207+ cpu.gr[RET1] = sim_syscall (STATE_CPU (sd, 0), cpu.gr[TRAPCODE],
208+ cpu.gr[PARM1], cpu.gr[PARM2], cpu.gr[PARM3],
209+ cpu.gr[PARM4]);
226210 }
227211
228212 static void
--- a/sim/mn10300/ChangeLog
+++ b/sim/mn10300/ChangeLog
@@ -1,5 +1,10 @@
11 2015-06-17 Mike Frysinger <vapier@gentoo.org>
22
3+ * op_utils.c (do_syscall): Replace call to cb_syscall with
4+ sim_syscall_multi.
5+
6+2015-06-17 Mike Frysinger <vapier@gentoo.org>
7+
38 * mn10300_sim.h (syscall_read_mem, syscall_write_mem): Delete.
49 * op_utils.c: Include sim-syscall.h.
510 (syscall_read_mem, syscall_write_mem): Delete.
--- a/sim/mn10300/op_utils.c
+++ b/sim/mn10300/op_utils.c
@@ -145,6 +145,15 @@ genericBtst(unsigned32 leftOpnd, unsigned32 rightOpnd)
145145 INLINE_SIM_MAIN (void)
146146 do_syscall (void)
147147 {
148+ /* Registers passed to trap 0. */
149+
150+ /* Function number. */
151+ reg_t func = State.regs[0];
152+ /* Parameters. */
153+ reg_t parm1 = State.regs[1];
154+ reg_t parm2 = load_word (State.regs[REG_SP] + 12);
155+ reg_t parm3 = load_word (State.regs[REG_SP] + 16);
156+ reg_t parm4 = load_word (State.regs[REG_SP] + 20);
148157
149158 /* We use this for simulated system calls; we may need to change
150159 it to a reserved instruction if we conflict with uses at
@@ -152,46 +161,24 @@ do_syscall (void)
152161 int save_errno = errno;
153162 errno = 0;
154163
155-/* Registers passed to trap 0 */
156-
157-/* Function number. */
158-#define FUNC (State.regs[0])
159-
160-/* Parameters. */
161-#define PARM1 (State.regs[1])
162-#define PARM2 (load_word (State.regs[REG_SP] + 12))
163-#define PARM3 (load_word (State.regs[REG_SP] + 16))
164-
165-/* Registers set by trap 0 */
166-
167-#define RETVAL State.regs[0] /* return value */
168-#define RETERR State.regs[1] /* return error code */
169-
170- if ( FUNC == TARGET_SYS_exit )
164+ if (func == TARGET_SYS_exit)
171165 {
172- /* EXIT - caller can look in PARM1 to work out the reason */
166+ /* EXIT - caller can look in parm1 to work out the reason */
173167 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
174- (PARM1 == 0xdead ? SIM_SIGABRT : sim_exited), PARM1);
168+ (parm1 == 0xdead ? SIM_SIGABRT : sim_exited), parm1);
175169 }
176170 else
177171 {
178- CB_SYSCALL syscall;
179-
180- CB_SYSCALL_INIT (&syscall);
181- syscall.arg1 = PARM1;
182- syscall.arg2 = PARM2;
183- syscall.arg3 = PARM3;
184- syscall.func = FUNC;
185- syscall.p1 = (PTR) simulator;
186- syscall.p2 = (PTR) STATE_CPU (simulator, 0);
187- syscall.read_mem = sim_syscall_read_mem;
188- syscall.write_mem = sim_syscall_write_mem;
189- cb_syscall (STATE_CALLBACK (simulator), &syscall);
190- RETERR = syscall.errcode;
191- RETVAL = syscall.result;
192- }
172+ long result, result2;
173+ int errcode;
193174
175+ sim_syscall_multi (STATE_CPU (simulator, 0), func, parm1, parm2,
176+ parm3, parm4, &result, &result2, &errcode);
177+
178+ /* Registers set by trap 0. */
179+ State.regs[0] = errcode;
180+ State.regs[1] = result;
181+ }
194182
195183 errno = save_errno;
196184 }
197-
--- a/sim/msp430/ChangeLog
+++ b/sim/msp430/ChangeLog
@@ -1,5 +1,10 @@
11 2015-06-17 Mike Frysinger <vapier@gentoo.org>
22
3+ * msp430-sim.c (maybe_perform_syscall): Replace call to cb_syscall
4+ with sim_syscall.
5+
6+2015-06-17 Mike Frysinger <vapier@gentoo.org>
7+
38 * msp430-sim.c: Include sim-syscall.h.
49 (syscall_read_mem, syscall_write_mem): Delete.
510 (maybe_perform_syscall): Change syscall_read_mem/syscall_write_mem
--- a/sim/msp430/msp430-sim.c
+++ b/sim/msp430/msp430-sim.c
@@ -1022,62 +1022,14 @@ maybe_perform_syscall (SIM_DESC sd, int call_addr)
10221022 {
10231023 /* Syscall! */
10241024 int syscall_num = call_addr & 0x3f;
1025- host_callback *cb = STATE_CALLBACK (sd);
1026- CB_SYSCALL sc;
1027-
1028- CB_SYSCALL_INIT (&sc);
1029-
1030- sc.func = syscall_num;
1031- sc.arg1 = MSP430_CPU (sd)->state.regs[12];
1032- sc.arg2 = MSP430_CPU (sd)->state.regs[13];
1033- sc.arg3 = MSP430_CPU (sd)->state.regs[14];
1034- sc.arg4 = MSP430_CPU (sd)->state.regs[15];
1035-
1036- if (TRACE_SYSCALL_P (MSP430_CPU (sd)))
1037- {
1038- const char *syscall_name = "*unknown*";
1039-
1040- switch (syscall_num)
1041- {
1042- case TARGET_SYS_exit:
1043- syscall_name = "exit(%d)";
1044- break;
1045- case TARGET_SYS_open:
1046- syscall_name = "open(%#x,%#x)";
1047- break;
1048- case TARGET_SYS_close:
1049- syscall_name = "close(%d)";
1050- break;
1051- case TARGET_SYS_read:
1052- syscall_name = "read(%d,%#x,%d)";
1053- break;
1054- case TARGET_SYS_write:
1055- syscall_name = "write(%d,%#x,%d)";
1056- break;
1057- }
1058- trace_generic (sd, MSP430_CPU (sd), TRACE_SYSCALL_IDX,
1059- syscall_name, sc.arg1, sc.arg2, sc.arg3, sc.arg4);
1060- }
1061-
1062- /* Handle SYS_exit here. */
1063- if (syscall_num == 1)
1064- {
1065- sim_engine_halt (sd, MSP430_CPU (sd), NULL,
1066- MSP430_CPU (sd)->state.regs[0],
1067- sim_exited, sc.arg1);
1068- return 1;
1069- }
1070-
1071- sc.p1 = sd;
1072- sc.p2 = MSP430_CPU (sd);
1073- sc.read_mem = sim_syscall_read_mem;
1074- sc.write_mem = sim_syscall_write_mem;
1075-
1076- cb_syscall (cb, &sc);
1077-
1078- TRACE_SYSCALL (MSP430_CPU (sd), "returns %ld", sc.result);
1079-
1080- MSP430_CPU (sd)->state.regs[12] = sc.result;
1025+ int arg1 = MSP430_CPU (sd)->state.regs[12];
1026+ int arg2 = MSP430_CPU (sd)->state.regs[13];
1027+ int arg3 = MSP430_CPU (sd)->state.regs[14];
1028+ int arg4 = MSP430_CPU (sd)->state.regs[15];
1029+
1030+ MSP430_CPU (sd)->state.regs[12] = sim_syscall (MSP430_CPU (sd),
1031+ syscall_num, arg1, arg2,
1032+ arg3, arg4);
10811033 return 1;
10821034 }
10831035