move swap on/off variants into same file
[trinity.git] / tables-biarch.c
blob27a3aa476e202e4d5872d42e1da9265e66bdf48c
1 /*
2 * Functions for handling the system call tables.
3 * These functions are only used on architectures that have both 32 and 64 bit syscalls.
4 */
6 #include <string.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
11 #include "trinity.h" // ARRAY_SIZE, alloc_shared
12 #include "arch.h"
13 #include "syscall.h"
14 #include "params.h"
15 #include "log.h"
16 #include "shm.h"
17 #include "tables.h"
19 #define NOTFOUND (unsigned int)-1
21 const struct syscalltable *syscalls_32bit;
22 const struct syscalltable *syscalls_64bit;
24 unsigned int max_nr_32bit_syscalls;
25 unsigned int max_nr_64bit_syscalls;
27 bool use_32bit = FALSE;
28 bool use_64bit = FALSE;
30 void activate_syscall32(unsigned int calln)
32 activate_syscall_in_table(calln, &shm->nr_active_32bit_syscalls, syscalls_32bit, shm->active_syscalls32);
35 void activate_syscall64(unsigned int calln)
37 activate_syscall_in_table(calln, &shm->nr_active_64bit_syscalls, syscalls_64bit, shm->active_syscalls64);
40 void deactivate_syscall32(unsigned int calln)
42 deactivate_syscall_in_table(calln, &shm->nr_active_32bit_syscalls, syscalls_32bit, shm->active_syscalls32);
45 void deactivate_syscall64(unsigned int calln)
47 deactivate_syscall_in_table(calln, &shm->nr_active_64bit_syscalls, syscalls_64bit, shm->active_syscalls64);
51 int validate_syscall_table_64(void)
53 if (shm->nr_active_64bit_syscalls == 0)
54 use_64bit = FALSE;
55 else
56 use_64bit = TRUE;
58 return use_64bit;
61 int validate_syscall_table_32(void)
63 if (shm->nr_active_32bit_syscalls == 0)
64 use_32bit = FALSE;
65 else
66 use_32bit = TRUE;
68 return use_32bit;
71 void toggle_syscall_biarch_n(int calln, const struct syscalltable *table, bool onlyflag, bool doflag, bool state, void (*activate)(unsigned int), int arch_bits, const char *arg_name)
73 if (calln != -1) {
74 struct syscallentry *entry = table[calln].entry;
76 validate_specific_syscall(table, calln);
78 if ((state == TRUE) && onlyflag && doflag) {
79 entry->flags |= ACTIVE;
80 (*activate)(calln);
81 } else {
82 entry->flags |= TO_BE_DEACTIVATED;
86 if ((arch_bits != 0) && (calln != -1))
87 output(0, "Marking %d-bit syscall %s (%d) as to be %sabled.\n",
88 arch_bits, arg_name, calln,
89 state ? "en" : "dis");
92 void toggle_syscall_biarch(const char *arg, bool state)
94 int specific_syscall32 = 0;
95 int specific_syscall64 = 0;
96 char *arg_name = NULL;
97 bool only_32bit = TRUE;
98 bool only_64bit = TRUE;
100 check_user_specified_arch(arg, &arg_name, &only_64bit, &only_32bit);
102 /* If we found a 64bit syscall, validate it. */
103 specific_syscall64 = search_syscall_table(syscalls_64bit, max_nr_64bit_syscalls, arg_name);
104 toggle_syscall_biarch_n(specific_syscall64, syscalls_64bit, only_64bit, do_64_arch, state, &activate_syscall64, 0, arg_name);
106 /* Search for and validate 32bit */
107 specific_syscall32 = search_syscall_table(syscalls_32bit, max_nr_32bit_syscalls, arg_name);
108 toggle_syscall_biarch_n(specific_syscall32, syscalls_32bit, only_32bit, do_32_arch, state, &activate_syscall32, 0, arg_name);
111 if ((!only_32bit) && (!only_64bit)) {
112 outputerr("No idea what architecture for syscall (%s) is.\n", arg);
113 exit(EXIT_FAILURE);
116 if ((specific_syscall64 == -1) && (specific_syscall32 == -1)) {
117 outputerr("No idea what syscall (%s) is.\n", arg);
118 exit(EXIT_FAILURE);
121 if ((specific_syscall64 != -1) && (specific_syscall32 != -1)) {
122 output(0, "Marking syscall %s (64bit:%d 32bit:%d) as to be %sabled.\n",
123 arg_name, specific_syscall64, specific_syscall32,
124 state ? "en" : "dis");
125 goto out;
128 if (specific_syscall64 != -1) {
129 output(0, "Marking 64-bit syscall %s (%d) as to be %sabled.\n",
130 arg, specific_syscall64,
131 state ? "en" : "dis");
132 goto out;
135 if (specific_syscall32 != -1) {
136 output(0, "Marking 32-bit syscall %s (%d) as to be %sabled.\n",
137 arg, specific_syscall32,
138 state ? "en" : "dis");
140 out:
141 clear_check_user_specified_arch(arg, &arg_name);
142 return;
146 void enable_random_syscalls_biarch(void)
148 unsigned int call32 = NOTFOUND, call64 = NOTFOUND;
150 retry:
152 //Search for 64 bit version
153 if (do_64_arch) {
154 struct syscallentry *entry = syscalls_64bit[call64].entry;
156 call64 = rand() % max_nr_64bit_syscalls;
157 if (validate_specific_syscall_silent(syscalls_64bit, call64) == FALSE)
158 goto retry;
160 if (no_files == TRUE)
161 if (is_syscall_net_related(syscalls_64bit, call64) == FALSE)
162 goto retry;
164 if (entry->flags & TO_BE_DEACTIVATED)
165 goto try32bit;
167 if (entry->active_number != 0)
168 goto try32bit;
170 // If we got so far, then activate it.
171 toggle_syscall_biarch_n(call64, syscalls_64bit, TRUE, do_64_arch, TRUE,
172 &activate_syscall64, 64, entry->name);
175 try32bit:
176 //Search for 32 bit version
177 if (do_32_arch) {
178 struct syscallentry *entry = syscalls_32bit[call32].entry;
180 // FIXME: WTF is going on here?
181 if (do_64_arch) {
182 call32 = search_syscall_table(syscalls_32bit, max_nr_32bit_syscalls, syscalls_64bit[call64].entry->name);
184 if (syscalls_64bit[call64].entry->flags & TO_BE_DEACTIVATED)
185 call64 = NOTFOUND; //mark as not found in order not to increment i.
186 } else {
187 call32 = rand() % max_nr_32bit_syscalls;
190 if (validate_specific_syscall_silent(syscalls_32bit, call32) == FALSE) {
191 if (call64 == NOTFOUND)
192 goto retry;
193 else
194 return;
197 if (no_files == TRUE) {
198 if (is_syscall_net_related(syscalls_32bit, call32) == FALSE) {
199 if (call64 == NOTFOUND)
200 goto retry;
201 else
202 return;
206 if ((entry->flags & TO_BE_DEACTIVATED) || (entry->active_number != 0)) {
207 if (call64 == NOTFOUND)
208 goto retry;
209 else
210 return;
213 //If we got so far, then active it.
214 toggle_syscall_biarch_n(call32, syscalls_32bit, TRUE, do_32_arch, TRUE,
215 &activate_syscall32, 32, entry->name);
219 void disable_non_net_syscalls_biarch(void)
221 struct syscallentry *entry;
222 unsigned int i;
224 for_each_64bit_syscall(i) {
225 entry = syscalls_64bit[i].entry;
227 if (validate_specific_syscall_silent(syscalls_64bit, i) == FALSE)
228 continue;
230 if (entry->flags & ACTIVE) {
231 if (is_syscall_net_related(syscalls_64bit, i) == FALSE) {
232 toggle_syscall_biarch_n(i, syscalls_64bit, FALSE, do_64_arch, FALSE,
233 &activate_syscall64, 64, entry->name);
238 for_each_32bit_syscall(i) {
239 entry = syscalls_32bit[i].entry;
241 if (validate_specific_syscall_silent(syscalls_32bit, i) == FALSE)
242 continue;
244 if (entry->flags & ACTIVE) {
245 if (is_syscall_net_related(syscalls_32bit, i) == FALSE) {
246 toggle_syscall_biarch_n(i, syscalls_32bit, FALSE, do_32_arch, FALSE,
247 &activate_syscall32, 32, entry->name);
253 int setup_syscall_group_biarch(unsigned int group)
255 unsigned int i;
257 for_each_32bit_syscall(i) {
258 if (syscalls_32bit[i].entry->group == group)
259 activate_syscall32(i);
262 if (shm->nr_active_32bit_syscalls == 0)
263 outputstd("No 32-bit syscalls in group\n");
264 else
265 outputstd("Found %d 32-bit syscalls in group\n", shm->nr_active_32bit_syscalls);
267 /* now the 64 bit table*/
268 for_each_64bit_syscall(i) {
269 if (syscalls_64bit[i].entry->group == group)
270 activate_syscall64(i);
273 if (shm->nr_active_64bit_syscalls == 0) {
274 outputstd("No 64-bit syscalls in group\n");
275 return FALSE;
276 } else {
277 outputstd("Found %d 64-bit syscalls in group\n", shm->nr_active_64bit_syscalls);
280 return TRUE;
283 void mark_all_syscalls_active_biarch(void)
285 unsigned int i;
287 if (do_32_arch) {
288 for_each_32bit_syscall(i) {
289 syscalls_32bit[i].entry->flags |= ACTIVE;
290 activate_syscall32(i);
294 if (do_64_arch) {
295 for_each_64bit_syscall(i) {
296 syscalls_64bit[i].entry->flags |= ACTIVE;
297 activate_syscall64(i);
302 void init_syscalls_biarch(void)
304 struct syscallentry *entry;
305 unsigned int i;
307 for_each_64bit_syscall(i) {
308 entry = syscalls_64bit[i].entry;
309 if (entry->flags & ACTIVE)
310 if (entry->init)
311 entry->init();
314 for_each_32bit_syscall(i) {
315 entry = syscalls_32bit[i].entry;
316 if (entry->flags & ACTIVE)
317 if (entry->init)
318 entry->init();
322 void deactivate_disabled_syscalls_biarch(void)
324 struct syscallentry *entry;
325 unsigned int i;
327 for_each_64bit_syscall(i) {
328 entry = syscalls_64bit[i].entry;
329 if (entry->flags & TO_BE_DEACTIVATED) {
330 entry->flags &= ~(ACTIVE|TO_BE_DEACTIVATED);
331 deactivate_syscall64(i);
332 output(0, "Marked 64-bit syscall %s (%d) as deactivated.\n",
333 entry->name, entry->number);
337 for_each_32bit_syscall(i) {
338 entry = syscalls_32bit[i].entry;
339 if (entry->flags & TO_BE_DEACTIVATED) {
340 entry->flags &= ~(ACTIVE|TO_BE_DEACTIVATED);
341 deactivate_syscall32(i);
342 output(0, "Marked 32-bit syscall %s (%d) as deactivated.\n",
343 entry->name, entry->number);
348 void dump_syscall_tables_biarch(void)
350 struct syscallentry *entry;
351 unsigned int i;
353 outputstd("syscalls: %d [32-bit]\n", max_nr_32bit_syscalls);
354 outputstd("syscalls: %d [64-bit]\n", max_nr_64bit_syscalls);
356 for_each_32bit_syscall(i) {
357 entry = syscalls_32bit[i].entry;
358 outputstd("entrypoint %d %s : [32-bit] ",
359 entry->number, entry->name);
360 show_state(entry->flags & ACTIVE);
362 if (entry->flags & AVOID_SYSCALL)
363 outputstd(" AVOID");
365 outputstd("\n");
368 for_each_64bit_syscall(i) {
369 entry = syscalls_64bit[i].entry;
371 outputstd("entrypoint %d %s : [64-bit] ",
372 entry->number, entry->name);
373 show_state(entry->flags & ACTIVE);
375 if (entry->flags & AVOID_SYSCALL)
376 outputstd(" AVOID");
378 outputstd("\n");
382 void display_enabled_syscalls_biarch(void)
384 struct syscallentry *entry;
385 unsigned int i;
387 for_each_64bit_syscall(i) {
388 entry = syscalls_64bit[i].entry;
389 if (entry->flags & ACTIVE)
390 output(0, "64-bit syscall %d:%s enabled.\n", i, entry->name);
393 for_each_32bit_syscall(i) {
394 entry = syscalls_32bit[i].entry;
395 if (entry->flags & ACTIVE)
396 output(0, "32-bit syscall %d:%s enabled.\n", i, entry->name);