1 /* eBPF simulator support code
2 Copyright (C) 2020-2021 Free Software Foundation, Inc.
4 This file is part of GDB, the GNU debugger.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* This must come before any other includes. */
22 #define WANT_CPU_BPFBF
23 #define WANT_CPU bpfbf
32 #include "defs-le.h" /* For SCACHE */
33 #include "bpf-helpers.h"
35 /* It is not possible to include both defs-le.h and defs-be.h due to
36 duplicated definitions, so we need a bunch of forward declarations
38 extern void bpfbf_ebpfle_init_idesc_table (SIM_CPU
*);
39 extern void bpfbf_ebpfbe_init_idesc_table (SIM_CPU
*);
41 uint64_t skb_data_offset
;
48 bpfbf_fetch_register (SIM_CPU
*current_cpu
,
54 SETTDI (buf
, CPU_PC_GET (current_cpu
));
55 else if (0 <= rn
&& rn
< 10)
56 SETTDI (buf
, GET_H_GPR (rn
));
64 bpfbf_store_register (SIM_CPU
*current_cpu
,
70 CPU_PC_SET (current_cpu
, GETTDI (buf
));
71 else if (0 <= rn
&& rn
< 10)
72 SET_H_GPR (rn
, GETTDI (buf
));
80 bpfbf_model_insn_before (SIM_CPU
*current_cpu
, int first_p
)
86 bpfbf_model_insn_after (SIM_CPU
*current_cpu
, int first_p
)
92 /***** Instruction helpers. *****/
94 /* The semantic routines for most instructions are expressed in RTL in
95 the cpu/bpf.cpu file, and automatically translated to C in the
96 sem-*.c files in this directory.
98 However, some of the semantic routines make use of helper C
99 functions. This happens when the semantics of the instructions
100 can't be expressed in RTL alone in a satisfactory way, or not at
103 The following functions implement these C helpers. */
106 bpfbf_endle (SIM_CPU
*current_cpu
, DI value
, UINT bitsize
)
110 case 16: return endian_h2le_2(endian_t2h_2(value
));
111 case 32: return endian_h2le_4(endian_t2h_4(value
));
112 case 64: return endian_h2le_8(endian_t2h_8(value
));
119 bpfbf_endbe (SIM_CPU
*current_cpu
, DI value
, UINT bitsize
)
123 case 16: return endian_h2be_2(endian_t2h_2(value
));
124 case 32: return endian_h2be_4(endian_t2h_4(value
));
125 case 64: return endian_h2be_8(endian_t2h_8(value
));
132 bpfbf_skb_data_offset (SIM_CPU
*current_cpu
)
134 /* Simply return the user-configured value.
135 This will be 0 if it has not been set. */
136 return skb_data_offset
;
141 bpfbf_call (SIM_CPU
*current_cpu
, INT disp32
, UINT src
)
143 /* eBPF supports two kind of CALL instructions: the so called pseudo
144 calls ("bpf to bpf") and external calls ("bpf to helper").
146 Both kind of calls use the same instruction (CALL). However,
147 external calls are constructed by passing a constant argument to
148 the instruction, that identifies the helper, whereas pseudo calls
149 result from expressions involving symbols.
151 We distinguish calls from pseudo-calls with the later having a 1
152 stored in the SRC field of the instruction. */
156 /* This is a pseudo-call. */
158 /* XXX allocate a new stack frame and transfer control. For
159 that we need to analyze the target function, like the kernel
160 verifier does. We better populate a cache
161 (function_start_address -> frame_size) so we avoid
162 calculating this more than once. */
163 /* XXX note that disp32 is PC-relative in number of 64-bit
164 words, _minus one_. */
168 /* This is a call to a helper.
170 DISP32 contains the helper number. Dispatch to the
171 corresponding helper emulator in bpf-helpers.c. */
174 /* case TRACE_PRINTK: */
176 bpf_trace_printk (current_cpu
);
184 bpfbf_exit (SIM_CPU
*current_cpu
)
186 SIM_DESC sd
= CPU_STATE (current_cpu
);
188 /* r0 holds "return code" */
189 DI r0
= GET_H_GPR (0);
191 printf ("exit %ld (0x%lx)\n", r0
, r0
);
193 sim_engine_halt (sd
, current_cpu
, NULL
, CPU_PC_GET (current_cpu
),
194 sim_exited
, 0 /* sigrc */);
198 bpfbf_breakpoint (SIM_CPU
*current_cpu
)
200 SIM_DESC sd
= CPU_STATE (current_cpu
);
202 sim_engine_halt (sd
, current_cpu
, NULL
, CPU_PC_GET (current_cpu
),
203 sim_stopped
, SIM_SIGTRAP
);
206 /* We use the definitions below instead of the cgen-generated model.c,
207 because the later is not really able to work with cpus featuring
208 several ISAs. This should be fixed in CGEN. */
211 bpf_def_model_init (void)
217 bpfbf_prepare_run (SIM_CPU
*cpu
)
223 bpf_engine_run_full (SIM_CPU
*cpu
)
225 if (current_target_byte_order
== BFD_ENDIAN_LITTLE
)
229 bpfbf_ebpfle_init_idesc_table (cpu
);
230 bpf_idesc_le
= CPU_IDESC (cpu
);
233 CPU_IDESC (cpu
) = bpf_idesc_le
;
235 bpfbf_ebpfle_engine_run_full (cpu
);
241 bpfbf_ebpfbe_init_idesc_table (cpu
);
242 bpf_idesc_be
= CPU_IDESC (cpu
);
245 CPU_IDESC (cpu
) = bpf_idesc_be
;
247 bpfbf_ebpfbe_engine_run_full (cpu
);
254 bpf_engine_run_fast (SIM_CPU
*cpu
)
256 if (current_target_byte_order
== BFD_ENDIAN_LITTLE
)
260 bpfbf_ebpfle_init_idesc_table (cpu
);
261 bpf_idesc_le
= CPU_IDESC (cpu
);
264 CPU_IDESC (cpu
) = bpf_idesc_le
;
266 bpfbf_ebpfle_engine_run_fast (cpu
);
272 bpfbf_ebpfbe_init_idesc_table (cpu
);
273 bpf_idesc_be
= CPU_IDESC (cpu
);
276 CPU_IDESC (cpu
) = bpf_idesc_be
;
278 bpfbf_ebpfbe_engine_run_fast (cpu
);
282 #endif /* WITH_FAST */
284 static const CGEN_INSN
*
285 bpfbf_get_idata (SIM_CPU
*cpu
, int inum
)
287 return CPU_IDESC (cpu
) [inum
].idata
;
291 bpf_init_cpu (SIM_CPU
*cpu
)
293 CPU_REG_FETCH (cpu
) = bpfbf_fetch_register
;
294 CPU_REG_STORE (cpu
) = bpfbf_store_register
;
295 CPU_PC_FETCH (cpu
) = bpfbf_h_pc_get
;
296 CPU_PC_STORE (cpu
) = bpfbf_h_pc_set
;
297 CPU_GET_IDATA (cpu
) = bpfbf_get_idata
;
298 /* Only used by profiling. 0 disables it. */
299 CPU_MAX_INSNS (cpu
) = 0;
300 CPU_INSN_NAME (cpu
) = cgen_insn_name
;
301 CPU_FULL_ENGINE_FN (cpu
) = bpf_engine_run_full
;
303 CPU_FAST_ENGINE_FN (cpu
) = bpf_engine_run_fast
;
305 CPU_FAST_ENGINE_FN (cpu
) = bpf_engine_run_full
;
309 static const SIM_MODEL bpf_models
[] =
311 { "bpf-def", & bpf_mach
, MODEL_BPF_DEF
, NULL
, bpf_def_model_init
},
315 static const SIM_MACH_IMP_PROPERTIES bpfbf_imp_properties
=
325 const SIM_MACH bpf_mach
=
327 "bpf", "bpf", MACH_BPF
,
328 32, 32, & bpf_models
[0], & bpfbf_imp_properties
,