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
27 #include "sim-signal.h"
33 #include "defs-le.h" /* For SCACHE */
34 #include "bpf-helpers.h"
36 /* It is not possible to include both defs-le.h and defs-be.h due to
37 duplicated definitions, so we need a bunch of forward declarations
39 extern void bpfbf_ebpfle_init_idesc_table (SIM_CPU
*);
40 extern void bpfbf_ebpfbe_init_idesc_table (SIM_CPU
*);
42 uint64_t skb_data_offset
;
49 bpfbf_fetch_register (SIM_CPU
*current_cpu
,
55 SETTDI (buf
, CPU_PC_GET (current_cpu
));
56 else if (0 <= rn
&& rn
< 10)
57 SETTDI (buf
, GET_H_GPR (rn
));
65 bpfbf_store_register (SIM_CPU
*current_cpu
,
71 CPU_PC_SET (current_cpu
, GETTDI (buf
));
72 else if (0 <= rn
&& rn
< 10)
73 SET_H_GPR (rn
, GETTDI (buf
));
81 bpfbf_model_insn_before (SIM_CPU
*current_cpu
, int first_p
)
87 bpfbf_model_insn_after (SIM_CPU
*current_cpu
, int first_p
)
93 /***** Instruction helpers. *****/
95 /* The semantic routines for most instructions are expressed in RTL in
96 the cpu/bpf.cpu file, and automatically translated to C in the
97 sem-*.c files in this directory.
99 However, some of the semantic routines make use of helper C
100 functions. This happens when the semantics of the instructions
101 can't be expressed in RTL alone in a satisfactory way, or not at
104 The following functions implement these C helpers. */
107 bpfbf_endle (SIM_CPU
*current_cpu
, DI value
, UINT bitsize
)
111 case 16: return endian_h2le_2(endian_t2h_2(value
));
112 case 32: return endian_h2le_4(endian_t2h_4(value
));
113 case 64: return endian_h2le_8(endian_t2h_8(value
));
120 bpfbf_endbe (SIM_CPU
*current_cpu
, DI value
, UINT bitsize
)
124 case 16: return endian_h2be_2(endian_t2h_2(value
));
125 case 32: return endian_h2be_4(endian_t2h_4(value
));
126 case 64: return endian_h2be_8(endian_t2h_8(value
));
133 bpfbf_skb_data_offset (SIM_CPU
*current_cpu
)
135 /* Simply return the user-configured value.
136 This will be 0 if it has not been set. */
137 return skb_data_offset
;
142 bpfbf_call (SIM_CPU
*current_cpu
, INT disp32
, UINT src
)
144 /* eBPF supports two kind of CALL instructions: the so called pseudo
145 calls ("bpf to bpf") and external calls ("bpf to helper").
147 Both kind of calls use the same instruction (CALL). However,
148 external calls are constructed by passing a constant argument to
149 the instruction, that identifies the helper, whereas pseudo calls
150 result from expressions involving symbols.
152 We distinguish calls from pseudo-calls with the later having a 1
153 stored in the SRC field of the instruction. */
157 /* This is a pseudo-call. */
159 /* XXX allocate a new stack frame and transfer control. For
160 that we need to analyze the target function, like the kernel
161 verifier does. We better populate a cache
162 (function_start_address -> frame_size) so we avoid
163 calculating this more than once. */
164 /* XXX note that disp32 is PC-relative in number of 64-bit
165 words, _minus one_. */
169 /* This is a call to a helper.
171 DISP32 contains the helper number. Dispatch to the
172 corresponding helper emulator in bpf-helpers.c. */
175 /* case TRACE_PRINTK: */
177 bpf_trace_printk (current_cpu
);
185 bpfbf_exit (SIM_CPU
*current_cpu
)
187 SIM_DESC sd
= CPU_STATE (current_cpu
);
189 /* r0 holds "return code" */
190 DI r0
= GET_H_GPR (0);
192 printf ("exit %ld (0x%lx)\n", r0
, r0
);
194 sim_engine_halt (sd
, current_cpu
, NULL
, CPU_PC_GET (current_cpu
),
195 sim_exited
, 0 /* sigrc */);
199 bpfbf_breakpoint (SIM_CPU
*current_cpu
)
201 SIM_DESC sd
= CPU_STATE (current_cpu
);
203 sim_engine_halt (sd
, current_cpu
, NULL
, CPU_PC_GET (current_cpu
),
204 sim_stopped
, SIM_SIGTRAP
);
207 /* We use the definitions below instead of the cgen-generated model.c,
208 because the later is not really able to work with cpus featuring
209 several ISAs. This should be fixed in CGEN. */
212 bpf_def_model_init (void)
218 bpfbf_prepare_run (SIM_CPU
*cpu
)
224 bpf_engine_run_full (SIM_CPU
*cpu
)
226 if (CURRENT_TARGET_BYTE_ORDER
== BFD_ENDIAN_LITTLE
)
230 bpfbf_ebpfle_init_idesc_table (cpu
);
231 bpf_idesc_le
= CPU_IDESC (cpu
);
234 CPU_IDESC (cpu
) = bpf_idesc_le
;
236 bpfbf_ebpfle_engine_run_full (cpu
);
242 bpfbf_ebpfbe_init_idesc_table (cpu
);
243 bpf_idesc_be
= CPU_IDESC (cpu
);
246 CPU_IDESC (cpu
) = bpf_idesc_be
;
248 bpfbf_ebpfbe_engine_run_full (cpu
);
255 bpf_engine_run_fast (SIM_CPU
*cpu
)
257 if (CURRENT_TARGET_BYTE_ORDER
== BFD_ENDIAN_LITTLE
)
261 bpfbf_ebpfle_init_idesc_table (cpu
);
262 bpf_idesc_le
= CPU_IDESC (cpu
);
265 CPU_IDESC (cpu
) = bpf_idesc_le
;
267 bpfbf_ebpfle_engine_run_fast (cpu
);
273 bpfbf_ebpfbe_init_idesc_table (cpu
);
274 bpf_idesc_be
= CPU_IDESC (cpu
);
277 CPU_IDESC (cpu
) = bpf_idesc_be
;
279 bpfbf_ebpfbe_engine_run_fast (cpu
);
283 #endif /* WITH_FAST */
285 static const CGEN_INSN
*
286 bpfbf_get_idata (SIM_CPU
*cpu
, int inum
)
288 return CPU_IDESC (cpu
) [inum
].idata
;
292 bpf_init_cpu (SIM_CPU
*cpu
)
294 CPU_REG_FETCH (cpu
) = bpfbf_fetch_register
;
295 CPU_REG_STORE (cpu
) = bpfbf_store_register
;
296 CPU_PC_FETCH (cpu
) = bpfbf_h_pc_get
;
297 CPU_PC_STORE (cpu
) = bpfbf_h_pc_set
;
298 CPU_GET_IDATA (cpu
) = bpfbf_get_idata
;
299 /* Only used by profiling. 0 disables it. */
300 CPU_MAX_INSNS (cpu
) = 0;
301 CPU_INSN_NAME (cpu
) = cgen_insn_name
;
302 CPU_FULL_ENGINE_FN (cpu
) = bpf_engine_run_full
;
304 CPU_FAST_ENGINE_FN (cpu
) = bpf_engine_run_fast
;
306 CPU_FAST_ENGINE_FN (cpu
) = bpf_engine_run_full
;
310 static const SIM_MODEL bpf_models
[] =
312 { "bpf-def", & bpf_mach
, MODEL_BPF_DEF
, NULL
, bpf_def_model_init
},
316 static const SIM_MACH_IMP_PROPERTIES bpfbf_imp_properties
=
326 const SIM_MACH bpf_mach
=
328 "bpf", "bpf", MACH_BPF
,
329 32, 32, & bpf_models
[0], & bpfbf_imp_properties
,