]> Git Repo - binutils.git/blob - sim/bpf/sim-if.c
Automatic date update in version.in
[binutils.git] / sim / bpf / sim-if.c
1 /* Main simulator entry points specific to the eBPF.
2    Copyright (C) 2020-2022 Free Software Foundation, Inc.
3
4    This file is part of GDB, the GNU debugger.
5
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.
10
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.
15
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/>.  */
18
19 /* This must come before any other includes.  */
20 #include "defs.h"
21
22 #include <stdlib.h>
23
24 #include "sim/callback.h"
25 #include "sim-main.h"
26 #include "sim-options.h"
27 #include "libiberty.h"
28 #include "bfd.h"
29
30 /* Globals.  */
31
32 /* String with the name of the section containing the BPF program to
33    run.  */
34 static char *bpf_program_section = NULL;
35
36 extern uint64_t skb_data_offset;
37
38 \f
39 /* Handle BPF-specific options.  */
40
41 static SIM_RC bpf_option_handler (SIM_DESC, sim_cpu *, int, char *, int);
42
43 typedef enum
44 {
45  OPTION_BPF_SET_PROGRAM = OPTION_START,
46  OPTION_BPF_LIST_PROGRAMS,
47  OPTION_BPF_VERIFY_PROGRAM,
48  OPTION_BPF_SKB_DATA_OFFSET,
49 } BPF_OPTION;
50
51 static const OPTION bpf_options[] =
52 {
53  { {"bpf-set-program", required_argument, NULL, OPTION_BPF_SET_PROGRAM},
54    '\0', "SECTION_NAME", "Set the entry point",
55    bpf_option_handler },
56  { {"bpf-list-programs", no_argument, NULL, OPTION_BPF_LIST_PROGRAMS},
57    '\0', "", "List loaded bpf programs",
58    bpf_option_handler },
59  { {"bpf-verify-program", required_argument, NULL, OPTION_BPF_VERIFY_PROGRAM},
60    '\0', "PROGRAM", "Run the verifier on the given BPF program",
61    bpf_option_handler },
62  { {"skb-data-offset", required_argument, NULL, OPTION_BPF_SKB_DATA_OFFSET},
63    '\0', "OFFSET", "Configure offsetof(struct sk_buff, data)",
64    bpf_option_handler },
65
66  { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
67 };
68
69 static SIM_RC
70 bpf_option_handler (SIM_DESC sd, sim_cpu *cpu ATTRIBUTE_UNUSED, int opt,
71                     char *arg, int is_command ATTRIBUTE_UNUSED)
72 {
73   switch ((BPF_OPTION) opt)
74     {
75     case OPTION_BPF_VERIFY_PROGRAM:
76       /* XXX call the verifier. */
77       sim_io_printf (sd, "Verifying BPF program %s...\n", arg);
78       break;
79
80     case OPTION_BPF_LIST_PROGRAMS:
81       /* XXX list programs.  */
82       sim_io_printf (sd, "BPF programs available:\n");
83       break;
84
85     case OPTION_BPF_SET_PROGRAM:
86       /* XXX: check that the section exists and tell the user about a
87          new start_address.  */
88       bpf_program_section = xstrdup (arg);
89       break;
90
91     case OPTION_BPF_SKB_DATA_OFFSET:
92       skb_data_offset = strtoul (arg, NULL, 0);
93       break;
94
95     default:
96       sim_io_eprintf (sd, "Unknown option `%s'\n", arg);
97       return SIM_RC_FAIL;
98     }
99
100   return SIM_RC_OK;
101 }
102
103 /* Like sim_state_free, but free the cpu buffers as well.  */
104
105 static void
106 bpf_free_state (SIM_DESC sd)
107 {
108   if (STATE_MODULES (sd) != NULL)
109     sim_module_uninstall (sd);
110
111   sim_cpu_free_all (sd);
112   sim_state_free (sd);
113 }
114
115 extern const SIM_MACH * const bpf_sim_machs[];
116
117 /* Create an instance of the simulator.  */
118
119 SIM_DESC
120 sim_open (SIM_OPEN_KIND kind,
121           host_callback *callback,
122           struct bfd *abfd,
123           char * const *argv)
124 {
125   /* XXX Analyze the program, and collect per-function information
126      like the kernel verifier does.  The implementation of the CALL
127      instruction will need that information, to update %fp.  */
128
129   SIM_DESC sd = sim_state_alloc (kind, callback);
130
131   /* Set default options before parsing user options.  */
132   STATE_MACHS (sd) = bpf_sim_machs;
133   STATE_MODEL_NAME (sd) = "bpf-def";
134
135   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
136     goto error;
137
138   if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
139     goto error;
140
141   /* Add the BPF-specific option list to the simulator.  */
142   if (sim_add_option_table (sd, NULL, bpf_options) != SIM_RC_OK)
143     {
144       bpf_free_state (sd);
145       return 0;
146     }
147
148   if (sim_parse_args (sd, argv) != SIM_RC_OK)
149     goto error;
150
151   if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
152     goto error;
153
154   if (sim_config (sd) != SIM_RC_OK)
155     goto error;
156
157   if (sim_post_argv_init (sd) != SIM_RC_OK)
158     goto error;
159
160   /* ... */
161
162   /* Initialize the CPU descriptors and the disassemble in the cpu
163      descriptor table entries.  */
164   {
165     int i;
166     CGEN_CPU_DESC cd = bpf_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
167                                             CGEN_ENDIAN_LITTLE);
168
169     /* We have one cpu per installed program! MAX_NR_PROCESSORS is an
170        arbitrary upper limit.  XXX where is it defined?  */
171     for (i = 0; i < MAX_NR_PROCESSORS; ++i)
172       {
173         SIM_CPU *cpu = STATE_CPU (sd, i);
174
175         CPU_CPU_DESC (cpu) = cd;
176         CPU_DISASSEMBLER (cpu) = sim_cgen_disassemble_insn;
177       }
178
179     bpf_cgen_init_dis (cd);
180   }
181
182   /* XXX do eBPF sim specific initializations.  */
183
184   return sd;
185
186  error:
187       bpf_free_state (sd);
188       return NULL;
189 }
190 \f
191
192 SIM_RC
193 sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
194                      char *const *argv, char *const *env)
195 {
196   SIM_CPU *current_cpu = STATE_CPU (sd, 0);
197   host_callback *cb = STATE_CALLBACK (sd);
198   SIM_ADDR addr;
199
200   /* Determine the start address.
201
202      XXX acknowledge bpf_program_section.  If it is NULL, emit a
203      warning explaining that we are using the ELF file start address,
204      which often is not what is actually wanted.  */
205   if (abfd != NULL)
206     addr = bfd_get_start_address (abfd);
207   else
208     addr = 0;
209
210   sim_pc_set (current_cpu, addr);
211
212   if (STATE_PROG_ARGV (sd) != argv)
213     {
214       freeargv (STATE_PROG_ARGV (sd));
215       STATE_PROG_ARGV (sd) = dupargv (argv);
216     }
217
218   if (STATE_PROG_ENVP (sd) != env)
219     {
220       freeargv (STATE_PROG_ENVP (sd));
221       STATE_PROG_ENVP (sd) = dupargv (env);
222     }
223
224   cb->argv = STATE_PROG_ARGV (sd);
225   cb->envp = STATE_PROG_ENVP (sd);
226
227   return SIM_RC_OK;
228 }
This page took 0.041101 seconds and 4 git commands to generate.