1 /* eBPF simulator support code
2 Copyright (C) 2020-2022 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
27 #include "sim-signal.h"
33 #include "decode-be.h"
34 #include "decode-le.h"
36 #include "defs-le.h" /* For SCACHE */
37 #include "bpf-helpers.h"
39 uint64_t skb_data_offset
;
46 bpfbf_fetch_register (SIM_CPU
*current_cpu
,
52 SETTDI (buf
, CPU_PC_GET (current_cpu
));
53 else if (0 <= rn
&& rn
< 10)
54 SETTDI (buf
, GET_H_GPR (rn
));
62 bpfbf_store_register (SIM_CPU
*current_cpu
,
68 CPU_PC_SET (current_cpu
, GETTDI (buf
));
69 else if (0 <= rn
&& rn
< 10)
70 SET_H_GPR (rn
, GETTDI (buf
));
78 bpfbf_model_insn_before (SIM_CPU
*current_cpu
, int first_p
)
84 bpfbf_model_insn_after (SIM_CPU
*current_cpu
, int first_p
, int cycles
)
90 /***** Instruction helpers. *****/
92 /* The semantic routines for most instructions are expressed in RTL in
93 the cpu/bpf.cpu file, and automatically translated to C in the
94 sem-*.c files in this directory.
96 However, some of the semantic routines make use of helper C
97 functions. This happens when the semantics of the instructions
98 can't be expressed in RTL alone in a satisfactory way, or not at
101 The following functions implement these C helpers. */
104 bpfbf_endle (SIM_CPU
*current_cpu
, DI value
, UINT bitsize
)
108 case 16: return endian_h2le_2(endian_t2h_2(value
));
109 case 32: return endian_h2le_4(endian_t2h_4(value
));
110 case 64: return endian_h2le_8(endian_t2h_8(value
));
117 bpfbf_endbe (SIM_CPU
*current_cpu
, DI value
, UINT bitsize
)
121 case 16: return endian_h2be_2(endian_t2h_2(value
));
122 case 32: return endian_h2be_4(endian_t2h_4(value
));
123 case 64: return endian_h2be_8(endian_t2h_8(value
));
130 bpfbf_skb_data_offset (SIM_CPU
*current_cpu
)
132 /* Simply return the user-configured value.
133 This will be 0 if it has not been set. */
134 return skb_data_offset
;
139 bpfbf_call (SIM_CPU
*current_cpu
, INT disp32
, UINT src
)
141 /* eBPF supports two kind of CALL instructions: the so called pseudo
142 calls ("bpf to bpf") and external calls ("bpf to helper").
144 Both kind of calls use the same instruction (CALL). However,
145 external calls are constructed by passing a constant argument to
146 the instruction, that identifies the helper, whereas pseudo calls
147 result from expressions involving symbols.
149 We distinguish calls from pseudo-calls with the later having a 1
150 stored in the SRC field of the instruction. */
154 /* This is a pseudo-call. */
156 /* XXX allocate a new stack frame and transfer control. For
157 that we need to analyze the target function, like the kernel
158 verifier does. We better populate a cache
159 (function_start_address -> frame_size) so we avoid
160 calculating this more than once. */
161 /* XXX note that disp32 is PC-relative in number of 64-bit
162 words, _minus one_. */
166 /* This is a call to a helper.
168 DISP32 contains the helper number. Dispatch to the
169 corresponding helper emulator in bpf-helpers.c. */
172 /* case TRACE_PRINTK: */
174 bpf_trace_printk (current_cpu
);
182 bpfbf_exit (SIM_CPU
*current_cpu
)
184 SIM_DESC sd
= CPU_STATE (current_cpu
);
186 /* r0 holds "return code" */
187 DI r0
= GET_H_GPR (0);
189 printf ("exit %" PRId64
" (0x%" PRIx64
")\n", r0
, r0
);
191 sim_engine_halt (sd
, current_cpu
, NULL
, CPU_PC_GET (current_cpu
),
192 sim_exited
, 0 /* sigrc */);
196 bpfbf_breakpoint (SIM_CPU
*current_cpu
)
198 SIM_DESC sd
= CPU_STATE (current_cpu
);
200 sim_engine_halt (sd
, current_cpu
, NULL
, CPU_PC_GET (current_cpu
),
201 sim_stopped
, SIM_SIGTRAP
);
204 /* We use the definitions below instead of the cgen-generated model.c,
205 because the later is not really able to work with cpus featuring
206 several ISAs. This should be fixed in CGEN. */
209 bpf_def_model_init (SIM_CPU
*cpu
)
215 bpfbf_prepare_run (SIM_CPU
*cpu
)
221 bpf_engine_run_full (SIM_CPU
*cpu
)
223 if (CURRENT_TARGET_BYTE_ORDER
== BFD_ENDIAN_LITTLE
)
227 bpfbf_ebpfle_init_idesc_table (cpu
);
228 bpf_idesc_le
= CPU_IDESC (cpu
);
231 CPU_IDESC (cpu
) = bpf_idesc_le
;
233 bpfbf_ebpfle_engine_run_full (cpu
);
239 bpfbf_ebpfbe_init_idesc_table (cpu
);
240 bpf_idesc_be
= CPU_IDESC (cpu
);
243 CPU_IDESC (cpu
) = bpf_idesc_be
;
245 bpfbf_ebpfbe_engine_run_full (cpu
);
252 bpf_engine_run_fast (SIM_CPU
*cpu
)
254 if (CURRENT_TARGET_BYTE_ORDER
== BFD_ENDIAN_LITTLE
)
258 bpfbf_ebpfle_init_idesc_table (cpu
);
259 bpf_idesc_le
= CPU_IDESC (cpu
);
262 CPU_IDESC (cpu
) = bpf_idesc_le
;
264 bpfbf_ebpfle_engine_run_fast (cpu
);
270 bpfbf_ebpfbe_init_idesc_table (cpu
);
271 bpf_idesc_be
= CPU_IDESC (cpu
);
274 CPU_IDESC (cpu
) = bpf_idesc_be
;
276 bpfbf_ebpfbe_engine_run_fast (cpu
);
280 #endif /* WITH_FAST */
282 static const CGEN_INSN
*
283 bpfbf_get_idata (SIM_CPU
*cpu
, int inum
)
285 return CPU_IDESC (cpu
) [inum
].idata
;
289 bpf_init_cpu (SIM_CPU
*cpu
)
291 CPU_REG_FETCH (cpu
) = bpfbf_fetch_register
;
292 CPU_REG_STORE (cpu
) = bpfbf_store_register
;
293 CPU_PC_FETCH (cpu
) = bpfbf_h_pc_get
;
294 CPU_PC_STORE (cpu
) = bpfbf_h_pc_set
;
295 CPU_GET_IDATA (cpu
) = bpfbf_get_idata
;
296 /* Only used by profiling. 0 disables it. */
297 CPU_MAX_INSNS (cpu
) = 0;
298 CPU_INSN_NAME (cpu
) = cgen_insn_name
;
299 CPU_FULL_ENGINE_FN (cpu
) = bpf_engine_run_full
;
301 CPU_FAST_ENGINE_FN (cpu
) = bpf_engine_run_fast
;
303 CPU_FAST_ENGINE_FN (cpu
) = bpf_engine_run_full
;
307 static const SIM_MODEL bpf_models
[] =
309 { "bpf-def", & bpf_mach
, MODEL_BPF_DEF
, NULL
, bpf_def_model_init
},
313 static const SIM_MACH_IMP_PROPERTIES bpfbf_imp_properties
=
323 const SIM_MACH bpf_mach
=
325 "bpf", "bpf", MACH_BPF
,
326 32, 32, & bpf_models
[0], & bpfbf_imp_properties
,