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