Debian Codegraph @

src/arch-syscall-validate

raw
1 #!/bin/bash
2
3 #
4 # libseccomp syscall validation script
5 #
6 # Copyright (c) 2014 Red Hat <[email protected]>
7 # Copyright (c) 2020 Cisco Systems, Inc. <[email protected]>
8 # Copyright (c) 2022 Microsoft Corporation. <[email protected]>
9 #
10 # Author: Paul Moore <[email protected]>
11 #
12
13 #
14 # This library is free software; you can redistribute it and/or modify it
15 # under the terms of version 2.1 of the GNU Lesser General Public License as
16 # published by the Free Software Foundation.
17 #
18 # This library is distributed in the hope that it will be useful, but WITHOUT
19 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
21 # for more details.
22 #
23 # You should have received a copy of the GNU Lesser General Public License
24 # along with this library; if not, see <http://www.gnu.org/licenses>.
25 #
26
27 LIB_SYS_DUMP="./arch-syscall-dump"
28
29 ####
30 # functions
31
32 #
33 # Dependency check
34 #
35 # Arguments:
36 # 1 Dependency to check for
37 #
38 function check_deps() {
39 [[ -z "$1" ]] && return
40 which "$1" >& /dev/null
41 return $?
42 }
43
44 #
45 # Dependency verification
46 #
47 # Arguments:
48 # 1 Dependency to check for
49 #
50 function verify_deps() {
51 [[ -z "$1" ]] && return
52 if ! check_deps "$1"; then
53 echo "error: install \"$1\" and include it in your \$PATH"
54 exit 1
55 fi
56 }
57
58 #
59 # Print out script usage details
60 #
61 function usage() {
62 cat << EOF
63 usage: arch-syscall-validate [-h] [-c <file>] [-a <arch>] <kernel_directory>
64
65 libseccomp syscall validation script
66 optional arguments:
67 -h show this help message and exit
68 -a architecture
69 -l output the library's syscall definitions
70 -s output the kernel's syscall definitions
71 -c <file> generate a CSV of the kernel's syscall definitions
72 EOF
73 }
74
75 #
76 # Dump the kernel version
77 #
78 # Arguments:
79 # 1 path to the kernel source
80 #
81 # Dump the kernel's version information to stdout.
82 #
83 function kernel_version() {
84 local maj=$(cat $1/Makefile | \
85 grep "^VERSION =" | awk -F "= " '{ print $2 }')
86 local min=$(cat $1/Makefile | \
87 grep "^PATCHLEVEL =" | awk -F "= " '{ print $2 }')
88 local sub=$(cat $1/Makefile | \
89 grep "^SUBLEVEL =" | awk -F "= " '{ print $2 }')
90 local xtr=$(cat $1/Makefile | \
91 grep "^EXTRAVERSION =" | awk -F "= " '{ print $2 }')
92 echo "${maj}.${min}.${sub}${xtr}"
93 }
94
95 #
96 # Dump the library syscall table for a given architecture
97 #
98 # Arguments:
99 # 1 architecture
100 # 2 offset (optional)
101 #
102 #
103 # Dump the library's syscall table to stdout.
104 #
105 function dump_lib_arch() {
106 local offset_str=""
107
108 [[ -z $1 ]] && return
109
110 [[ -n $2 ]] && offset_str="-o $2"
111 $LIB_SYS_DUMP -a $1 $offset_str | sed 's/\t/,/' | sort
112 }
113
114 #
115 # Mangle the library pseudo syscall values
116 #
117 # Arguments:
118 # 1 architecture
119 #
120 # Mangle the supplied pseudo syscall to match the system values
121 #
122 function mangle_lib_syscall() {
123 local sed_filter=""
124
125 sed_filter+='s/accept4,-118/accept4,364/;'
126 sed_filter+='s/bind,-102/bind,361/;'
127 sed_filter+='s/connect,-103/connect,362/;'
128 sed_filter+='s/getpeername,-107/getpeername,368/;'
129 sed_filter+='s/getsockname,-106/getsockname,367/;'
130 sed_filter+='s/getsockopt,-115/getsockopt,365/;'
131 sed_filter+='s/listen,-104/listen,363/;'
132 sed_filter+='s/msgctl,-214/msgctl,402/;'
133 sed_filter+='s/msgget,-213/msgget,399/;'
134 sed_filter+='s/msgrcv,-212/msgrcv,401/;'
135 sed_filter+='s/msgsnd,-211/msgsnd,400/;'
136 sed_filter+='s/recvfrom,-112/recvfrom,371/;'
137 sed_filter+='s/recvmsg,-117/recvmsg,372/;'
138 sed_filter+='s/semctl,-203/semctl,394/;'
139 sed_filter+='s/semget,-202/semget,393/;'
140 sed_filter+='s/sendmsg,-116/sendmsg,370/;'
141 sed_filter+='s/sendto,-111/sendto,369/;'
142 sed_filter+='s/setsockopt,-114/setsockopt,366/;'
143 sed_filter+='s/shmat,-221/shmat,397/;'
144 sed_filter+='s/shmctl,-224/shmctl,396/;'
145 sed_filter+='s/shmdt,-222/shmdt,398/;'
146 sed_filter+='s/shmget,-223/shmget,395/;'
147 sed_filter+='s/shutdown,-113/shutdown,373/;'
148 sed_filter+='s/socket,-101/socket,359/;'
149 sed_filter+='s/socketpair,-108/socketpair,360/;'
150
151 case $1 in
152 s390|s390x)
153 sed_filter+='s/recvmmsg,-119/recvmmsg,357/;'
154 sed_filter+='s/sendmmsg,-120/sendmmsg,358/;'
155 ;;
156 *)
157 sed_filter+='s/recvmmsg,-119/recvmmsg,337/;'
158 sed_filter+='s/sendmmsg,-120/sendmmsg,345/;'
159 ;;
160 esac
161
162 sed $sed_filter | sed '/,-[0-9]\+$/d'
163 }
164
165 #
166 # Dump syscalls matching specified tags from the given syscall.tbl file
167 #
168 # Arguments:
169 # 1 path to the syscall.tbl file to dump
170 # (rest) tags to match (except "common" which is always included)
171 #
172 # Dump the matched syscall table entries to stdout.
173 #
174 function dump_from_syscall_tbl() {
175 local file="$1"
176 shift
177
178 local tag
179 local tag_regexp='^(common'
180 for tag in "$@"; do
181 tag_regexp="${tag_regexp}|${tag}"
182 done
183 tag_regexp="${tag_regexp}) "
184
185 cat "$file" | grep -v '^#\|^$' | awk '{ print $2,$3,$1 }' | \
186 grep -E "$tag_regexp" | awk '{ print $2","$3 }' | sort | \
187 grep -Ev '^(reserved|unused)[0-9]+,'
188 }
189
190 #
191 # Dump the x86 system syscall table
192 #
193 # Arguments:
194 # 1 path to the kernel source
195 #
196 # Dump the architecture's syscall table to stdout.
197 #
198 function dump_sys_x86() {
199 dump_from_syscall_tbl "$1/arch/x86/entry/syscalls/syscall_32.tbl" i386
200 }
201
202 #
203 # Dump the x86 library syscall table
204 #
205 # Dump the library's syscall table to stdout.
206 #
207 function dump_lib_x86() {
208 dump_lib_arch x86 | mangle_lib_syscall x86
209 }
210
211 #
212 # Dump the x86_64 system syscall table
213 #
214 # Arguments:
215 # 1 path to the kernel source
216 #
217 # Dump the architecture's syscall table to stdout.
218 #
219 function dump_sys_x86_64() {
220 dump_from_syscall_tbl "$1/arch/x86/entry/syscalls/syscall_64.tbl" 64
221 }
222
223 #
224 # Dump the x86_64 library syscall table
225 #
226 # Dump the library's syscall table to stdout.
227 #
228 function dump_lib_x86_64() {
229 dump_lib_arch x86_64 | mangle_lib_syscall x86_64
230 }
231
232 #
233 # Dump the x32 system syscall table
234 #
235 # Arguments:
236 # 1 path to the kernel source
237 #
238 # Dump the architecture's syscall table to stdout.
239 #
240 function dump_sys_x32() {
241 dump_from_syscall_tbl "$1/arch/x86/entry/syscalls/syscall_64.tbl" x32
242 }
243
244 #
245 # Dump the x32 library syscall table
246 #
247 # Dump the library's syscall table to stdout.
248 #
249 function dump_lib_x32() {
250 dump_lib_arch x32 | mangle_lib_syscall x32
251 }
252
253 #
254 # Dump the arm system syscall table
255 #
256 # Arguments:
257 # 1 path to the kernel source
258 #
259 # Dump the architecture's syscall table to stdout.
260 #
261 function dump_sys_arm() {
262 dump_from_syscall_tbl "$1/arch/arm/tools/syscall.tbl" eabi | (cat -; \
263 (cat $1/arch/arm/include/uapi/asm/unistd.h | \
264 grep "^#define __ARM_NR_" | \
265 grep -v "^#define __ARM_NR_BASE" | \
266 sed 's/#define __ARM_NR_\([a-z0-9_]*\)[ \t]\+(__ARM_NR_BASE+\(.*\))/\1 983040 + \2/' | \
267 awk '{ print $1","$2+$4 }')) | sort
268 }
269
270 #
271 # Dump the arm library syscall table
272 #
273 # Dump the library's syscall table to stdout.
274 #
275 function dump_lib_arm() {
276 # NOTE: arm_sync_file_range() and sync_file_range2() share values
277 dump_lib_arch arm | sed '/sync_file_range2,\+341/d' | \
278 mangle_lib_syscall arm
279 }
280
281 #
282 # Dump the aarch64 system syscall table
283 #
284 # Arguments:
285 # 1 path to the kernel source
286 #
287 # Dump the architecture's syscall table to stdout.
288 #
289 function dump_sys_aarch64() {
290 local syscall_tbl_file="$1/arch/arm64/tools/syscall_64.tbl"
291 if [[ -e $syscall_tbl_file ]]; then
292 dump_from_syscall_tbl "$syscall_tbl_file" \
293 64 renameat rlimit memfd_secret
294 return
295 fi
296
297 local sed_filter=""
298
299 sed_filter+='s/__NR3264_statfs/43/;'
300 sed_filter+='s/__NR3264_ftruncate/46/;'
301 sed_filter+='s/__NR3264_truncate/45/;'
302 sed_filter+='s/__NR3264_lseek/62/;'
303 sed_filter+='s/__NR3264_sendfile/71/;'
304 sed_filter+='s/__NR3264_fstatat/79/;'
305 sed_filter+='s/__NR3264_fstatfs/44/;'
306 sed_filter+='s/__NR3264_fcntl/25/;'
307 sed_filter+='s/__NR3264_fadvise64/223/;'
308 sed_filter+='s/__NR3264_mmap/222/;'
309 sed_filter+='s/__NR3264_fstat/80/;'
310 sed_filter+='s/__NR3264_lstat/1039/;'
311 sed_filter+='s/__NR3264_stat/1038/;'
312
313 gcc -E -dM -I$1/include/uapi \
314 -D__BITS_PER_LONG=64 -D__ARCH_WANT_RENAMEAT \
315 -D__ARCH_WANT_NEW_STAT \
316 $1/arch/arm64/include/uapi/asm/unistd.h | \
317 grep "^#define __NR_" | \
318 sed '/__NR_syscalls/d' | \
319 sed '/__NR_arch_specific_syscall/d' | \
320 sed 's/#define[ \t]\+__NR_\([^ \t]\+\)[ \t]\+\(.*\)/\1,\2/' | \
321 sed $sed_filter | sort
322 }
323
324 #
325 # Dump the aarch64 library syscall table
326 #
327 # Dump the library's syscall table to stdout.
328 #
329 function dump_lib_aarch64() {
330 dump_lib_arch aarch64 | mangle_lib_syscall aarch64
331 }
332
333 #
334 # Dump the loongarch64 syscall table
335 #
336 # Arguments:
337 # 1 path to the kernel source
338 #
339 # Dump the architecture's syscall table to stdout.
340 #
341 function dump_sys_loongarch64() {
342 if [[ -e $1/arch/loongarch/kernel/Makefile.syscalls ]]; then
343 dump_from_syscall_tbl "$1/scripts/syscall.tbl" 64
344 return
345 fi
346
347 local sed_filter=""
348
349 sed_filter+='s/__NR3264_fadvise64/223/;'
350 sed_filter+='s/__NR3264_fcntl/25/;'
351 sed_filter+='s/__NR3264_fstatfs/44/;'
352 sed_filter+='s/__NR3264_ftruncate/46/;'
353 sed_filter+='s/__NR3264_lseek/62/;'
354 sed_filter+='s/__NR3264_mmap/222/;'
355 sed_filter+='s/__NR3264_sendfile/71/;'
356 sed_filter+='s/__NR3264_statfs/43/;'
357 sed_filter+='s/__NR3264_truncate/45/;'
358
359 gcc -E -dM -I$1/include/uapi \
360 -D__BITS_PER_LONG=64 \
361 -D__ARCH_WANT_SYS_CLONE \
362 -D__ARCH_WANT_SYS_CLONE3 \
363 $1/arch/loongarch/include/uapi/asm/unistd.h | \
364 grep "^#define __NR_" | \
365 sed '/__NR_syscalls/d' | \
366 sed '/__NR_arch_specific_syscall/d' | \
367 sed 's/#define[ \t]\+__NR_\([^ \t]\+\)[ \t]\+\(.*\)/\1,\2/' | \
368 sed $sed_filter | sort
369 }
370
371 #
372 # Dump the loongarch64 library syscall table
373 #
374 # Dump the library's syscall table to stdout.
375 #
376 function dump_lib_loongarch64() {
377 dump_lib_arch loongarch64 | mangle_lib_syscall loongarch64
378 }
379
380 # Dump the m68k system syscall table
381 #
382 # Arguments:
383 # 1 path to the kernel source
384 #
385 # Dump the architecture's syscall table to stdout.
386 #
387 function dump_sys_m68k() {
388 dump_from_syscall_tbl "$1/arch/m68k/kernel/syscalls/syscall.tbl"
389 }
390
391 #
392 # Dump the m68k library syscall table
393 #
394 # Dump the library's syscall table to stdout.
395 #
396 function dump_lib_m68k() {
397 dump_lib_arch m68k | mangle_lib_syscall m68k
398 }
399
400 #
401 # Dump the mips system syscall table
402 #
403 # Arguments:
404 # 1 path to the kernel source
405 #
406 # Dump the architecture's syscall table to stdout.
407 #
408 function dump_sys_mips() {
409 dump_from_syscall_tbl "$1/arch/mips/kernel/syscalls/syscall_o32.tbl" o32
410 }
411
412 #
413 # Dump the mips library syscall table
414 #
415 # Dump the library's syscall table to stdout.
416 #
417 function dump_lib_mips() {
418 dump_lib_arch mips | mangle_lib_syscall mips
419 }
420
421 #
422 # Dump the mips64 system syscall table
423 #
424 # Arguments:
425 # 1 path to the kernel source
426 #
427 # Dump the architecture's syscall table to stdout.
428 #
429 function dump_sys_mips64() {
430 dump_from_syscall_tbl "$1/arch/mips/kernel/syscalls/syscall_n64.tbl" n64
431 }
432
433 #
434 # Dump the mips64 library syscall table
435 #
436 # Dump the library's syscall table to stdout.
437 #
438 function dump_lib_mips64() {
439 dump_lib_arch mips64 | mangle_lib_syscall mips64
440 }
441
442 #
443 # Dump the mips64n32 system syscall table
444 #
445 # Arguments:
446 # 1 path to the kernel source
447 #
448 # Dump the architecture's syscall table to stdout.
449 #
450 function dump_sys_mips64n32() {
451 dump_from_syscall_tbl "$1/arch/mips/kernel/syscalls/syscall_n32.tbl" n32
452 }
453
454 #
455 # Dump the mips64n32 library syscall table
456 #
457 # Dump the library's syscall table to stdout.
458 #
459 function dump_lib_mips64n32() {
460 dump_lib_arch mips64n32 | mangle_lib_syscall mips64n32
461 }
462
463 #
464 # Dump the parisc system syscall table
465 #
466 # Arguments:
467 # 1 path to the kernel source
468 #
469 # Dump the architecture's syscall table to stdout.
470 #
471 function dump_sys_parisc() {
472 dump_from_syscall_tbl "$1/arch/parisc/kernel/syscalls/syscall.tbl" 32
473 }
474
475 #
476 # Dump the parisc library syscall table
477 #
478 # Dump the library's syscall table to stdout.
479 #
480 function dump_lib_parisc() {
481 dump_lib_arch parisc | mangle_lib_syscall parisc
482 }
483
484 #
485 # Dump the parisc64 system syscall table
486 #
487 # Arguments:
488 # 1 path to the kernel source
489 #
490 # Dump the architecture's syscall table to stdout.
491 #
492 function dump_sys_parisc64() {
493 dump_from_syscall_tbl "$1/arch/parisc/kernel/syscalls/syscall.tbl" 64
494 }
495
496 #
497 # Dump the parisc64 library syscall table
498 #
499 # Dump the library's syscall table to stdout.
500 #
501 function dump_lib_parisc64() {
502 dump_lib_arch parisc64 | mangle_lib_syscall parisc64
503 }
504
505 #
506 # Dump the ppc system syscall table
507 #
508 # Arguments:
509 # 1 path to the kernel source
510 #
511 # Dump the architecture's syscall table to stdout.
512 #
513 function dump_sys_ppc() {
514 dump_from_syscall_tbl "$1/arch/powerpc/kernel/syscalls/syscall.tbl" \
515 nospu 32
516 }
517
518 #
519 # Dump the ppc library syscall table
520 #
521 # Dump the library's syscall table to stdout.
522 #
523 function dump_lib_ppc() {
524 dump_lib_arch ppc | mangle_lib_syscall ppc
525 }
526
527 #
528 # Dump the ppc64 system syscall table
529 #
530 # Arguments:
531 # 1 path to the kernel source
532 #
533 # Dump the architecture's syscall table to stdout.
534 #
535 function dump_sys_ppc64() {
536 dump_from_syscall_tbl "$1/arch/powerpc/kernel/syscalls/syscall.tbl" \
537 nospu 64
538 }
539
540 #
541 # Dump the ppc64 library syscall table
542 #
543 # Dump the library's syscall table to stdout.
544 #
545 function dump_lib_ppc64() {
546 dump_lib_arch ppc64 | mangle_lib_syscall ppc64
547 }
548
549 #
550 # Dump the riscv64 system syscall table
551 #
552 # Arguments:
553 # 1 path to the kernel source
554 #
555 # Dump the architecture's syscall table to stdout.
556 #
557 function dump_sys_riscv64() {
558 if [[ -e $1/arch/riscv/kernel/Makefile.syscalls ]]; then
559 dump_from_syscall_tbl "$1/scripts/syscall.tbl" \
560 64 riscv rlimit memfd_secret
561 return
562 fi
563
564 local sed_filter=""
565
566 sed_filter+='s/__NR3264_fadvise64/223/;'
567 sed_filter+='s/__NR3264_fcntl/25/;'
568 sed_filter+='s/__NR3264_fstatat/79/;'
569 sed_filter+='s/__NR3264_fstatfs/44/;'
570 sed_filter+='s/__NR3264_ftruncate/46/;'
571 sed_filter+='s/__NR3264_lseek/62/;'
572 sed_filter+='s/__NR3264_mmap/222/;'
573 sed_filter+='s/__NR3264_sendfile/71/;'
574 sed_filter+='s/__NR3264_statfs/43/;'
575 sed_filter+='s/__NR3264_truncate/45/;'
576 sed_filter+='s/__NR3264_fstat/80/;'
577
578 gcc -E -dM -I$1/include/uapi \
579 -D__BITS_PER_LONG=64 -D__ARCH_WANT_NEW_STAT \
580 $1/arch/riscv/include/uapi/asm/unistd.h | \
581 grep "^#define __NR_" | \
582 sed '/__NR_syscalls/d' | \
583 sed 's/(__NR_arch_specific_syscall + 15)/259/' | \
584 sed '/__NR_arch_specific_syscall/d' | \
585 sed 's/#define[ \t]\+__NR_\([^ \t]\+\)[ \t]\+\(.*\)/\1,\2/' | \
586 sed $sed_filter | sort
587 }
588
589 #
590 # Dump the riscv64 library syscall table
591 #
592 # Dump the library's syscall table to stdout.
593 #
594 function dump_lib_riscv64() {
595 dump_lib_arch riscv64 | mangle_lib_syscall riscv64
596 }
597
598 #
599 # Dump the s390 system syscall table
600 #
601 # Arguments:
602 # 1 path to the kernel source
603 #
604 # Dump the architecture's syscall table to stdout.
605 #
606 function dump_sys_s390() {
607 # s390 removed in Linux v6.19, use historical data
608 cat ./syscalls.s390.csv
609 }
610
611 #
612 # Dump the s390 library syscall table
613 #
614 # Dump the library's syscall table to stdout.
615 #
616 function dump_lib_s390() {
617 dump_lib_arch s390 | mangle_lib_syscall s390
618 }
619
620 #
621 # Dump the s390x system syscall table
622 #
623 # Arguments:
624 # 1 path to the kernel source
625 #
626 # Dump the architecture's syscall table to stdout.
627 #
628 function dump_sys_s390x() {
629 dump_from_syscall_tbl "$1/arch/s390/kernel/syscalls/syscall.tbl" 64
630 }
631
632 #
633 # Dump the s390x library syscall table
634 #
635 # Dump the library's syscall table to stdout.
636 #
637 function dump_lib_s390x() {
638 dump_lib_arch s390x | mangle_lib_syscall s390x
639 }
640
641 #
642 # Dump the sh system syscall table
643 #
644 # Arguments:
645 # 1 path to the kernel source
646 #
647 # Dump the architecture's syscall table to stdout.
648 #
649 function dump_sys_sh() {
650 dump_from_syscall_tbl "$1/arch/sh/kernel/syscalls/syscall.tbl"
651 }
652
653 #
654 # Dump the sh library syscall table
655 #
656 # Dump the library's syscall table to stdout.
657 #
658 function dump_lib_sh() {
659 dump_lib_arch sh | mangle_lib_syscall sh
660 }
661
662 #
663 # Dump the system syscall table
664 #
665 # Arguments:
666 # 1 architecture
667 # 2 path to the kernel source
668 #
669 # Dump the system's syscall table to stdout using the given architecture.
670 #
671 function dump_sys() {
672 case $1 in
673 x86)
674 dump_sys_x86 "$2"
675 ;;
676 x86_64)
677 dump_sys_x86_64 "$2"
678 ;;
679 x32)
680 dump_sys_x32 "$2"
681 ;;
682 arm)
683 dump_sys_arm "$2"
684 ;;
685 aarch64)
686 dump_sys_aarch64 "$2"
687 ;;
688 loongarch64)
689 dump_sys_loongarch64 "$2"
690 ;;
691 m68k)
692 dump_sys_m68k "$2"
693 ;;
694 mips)
695 dump_sys_mips "$2"
696 ;;
697 mips64)
698 dump_sys_mips64 "$2"
699 ;;
700 mips64n32)
701 dump_sys_mips64n32 "$2"
702 ;;
703 parisc)
704 dump_sys_parisc "$2"
705 ;;
706 parisc64)
707 dump_sys_parisc64 "$2"
708 ;;
709 ppc)
710 dump_sys_ppc "$2"
711 ;;
712 ppc64)
713 dump_sys_ppc64 "$2"
714 ;;
715 riscv64)
716 dump_sys_riscv64 "$2"
717 ;;
718 s390)
719 dump_sys_s390 "$2"
720 ;;
721 s390x)
722 dump_sys_s390x "$2"
723 ;;
724 sh)
725 dump_sys_sh "$2"
726 ;;
727 *)
728 echo ""
729 return 1
730 ;;
731 esac
732
733 return 0
734 }
735
736 #
737 # Dump the library syscall table
738 #
739 # Arguments:
740 # 1 architecture
741 #
742 # Dump the library's syscall table to stdout using the given architecture.
743 #
744 function dump_lib() {
745 case $1 in
746 x86)
747 dump_lib_x86
748 ;;
749 x86_64)
750 dump_lib_x86_64
751 ;;
752 x32)
753 dump_lib_x32
754 ;;
755 arm)
756 dump_lib_arm
757 ;;
758 aarch64)
759 dump_lib_aarch64
760 ;;
761 loongarch64)
762 dump_lib_loongarch64
763 ;;
764 m68k)
765 dump_lib_m68k
766 ;;
767 mips)
768 dump_lib_mips
769 ;;
770 mips64)
771 dump_lib_mips64
772 ;;
773 mips64n32)
774 dump_lib_mips64n32
775 ;;
776 parisc)
777 dump_lib_parisc
778 ;;
779 parisc64)
780 dump_lib_parisc64
781 ;;
782 ppc)
783 dump_lib_ppc
784 ;;
785 ppc64)
786 dump_lib_ppc64
787 ;;
788 riscv64)
789 dump_lib_riscv64
790 ;;
791 s390)
792 dump_lib_s390
793 ;;
794 s390x)
795 dump_lib_s390x
796 ;;
797 sh)
798 dump_lib_sh
799 ;;
800 *)
801 echo ""
802 return 1
803 ;;
804 esac
805
806 return 0
807 }
808
809 #
810 # Generate the syscall csv file
811 #
812 # Arguments:
813 # 1 path to the kernel source
814 # 2 "sys" or "lib" depending on the syscall list to use
815 # 3 csv file to use as input for saved fields
816 #
817 # Generate a syscall csv file from the given kernel sources.
818 #
819 function gen_csv() {
820
821 # sanity checks
822 [[ -z $1 ]] && return
823 [[ $2 != "sys" && $2 != "lib" ]] && return
824
825 # abi list
826 # NOTE: the ordering here is dependent on the layout of the
827 # arch_syscall_table struct in syscalls.h - BEWARE!
828 abi_list=""
829 abi_list+=" x86 x86_64 x32"
830 abi_list+=" arm aarch64"
831 abi_list+=" loongarch64"
832 abi_list+=" m68k"
833 abi_list+=" mips mips64 mips64n32"
834 abi_list+=" parisc parisc64"
835 abi_list+=" ppc ppc64"
836 abi_list+=" riscv64"
837 abi_list+=" s390 s390x"
838 abi_list+=" sh"
839
840 # read the csv to get the existing data
841 local -A csv
842 local csv_has_kver=0
843 local csv_input="$3"
844 [[ ! -e "$3" ]] && csv_input=/dev/null
845 grep -q "KV_" "$csv_input" && csv_has_kver=1
846 while read line; do
847 sc=$(echo $line | cut -d, -f 1)
848 local field=2
849 for abi in $abi_list; do
850 csv[$sc,$abi]=$(echo $line | cut -d, -f $field)
851 ((field++))
852 if [[ $csv_has_kver -eq 1 ]]; then
853 csv[$sc,${abi}_KVER]=$(echo $line | \
854 cut -d, -f $field)
855 ((field++))
856 fi
857 done
858 done < <(sed 's/#.*//;/^[ \t]*$/d' "$csv_input")
859
860 # get the full syscall list
861 for abi in $abi_list; do
862 eval output_$abi=$(mktemp -t syscall_validate_XXXXXX)
863 dump_$2_$abi "$1" > $(eval echo $`eval echo output_$abi`)
864 done
865 sc_list=$( (for abi in $abi_list; do
866 cat $(eval echo $`eval echo output_$abi`);
867 done) | grep -v '^#' | awk -F "," '{ print $1 }' | sort -u)
868
869 # redirect the subshell to the csv file
870 (
871 # output a simple header
872 printf "#syscall (v%s %s)" \
873 "$(kernel_version "$1")" "$(TZ=UTC date "+%Y-%m-%d")"
874 for abi in $abi_list; do
875 printf ",%s,%s_kver" $abi $abi
876 done
877 printf "\n"
878
879 # output the syscall csv details
880 for sc in $sc_list; do
881 printf "%s" $sc
882 for abi in $abi_list; do
883 num=$(grep "^$sc," \
884 $(eval echo $`eval echo output_$abi`) | \
885 awk -F "," '{ print $2 }' )
886 kver=${csv[$sc,${abi}_KVER]}
887 [[ -z $num ]] && num="PNR"
888 [[ -z $kver ]] && kver="KV_UNDEF"
889 printf ",%s,%s" $num $kver
890 done
891 printf "\n"
892 done
893 ) > "$3"
894
895 # cleanup
896 for abi in $abi_list; do
897 rm -f $(eval echo $`eval echo output_$abi`)
898 done
899 }
900
901 ####
902 # main
903
904 verify_deps diff
905 verify_deps gcc
906 verify_deps grep
907 verify_deps mktemp
908 verify_deps sed
909
910 opt_arches=""
911 opt_csv=""
912 opt_sys=0
913 opt_lib=0
914
915 while getopts "a:c:slh" opt; do
916 case $opt in
917 a)
918 opt_arches+="$OPTARG "
919 ;;
920 c)
921 opt_csv="$OPTARG"
922 ;;
923 s)
924 opt_sys=1
925 opt_lib=0
926 ;;
927 l)
928 opt_sys=0
929 opt_lib=1
930 ;;
931 h|*)
932 usage
933 exit 1
934 ;;
935 esac
936 done
937 shift $(($OPTIND - 1))
938
939 # defaults
940 if [[ $opt_arches == "" ]]; then
941 opt_arches=" \
942 x86 x86_64 x32 \
943 arm aarch64 \
944 loongarch64 \
945 m68k \
946 mips mips64 mips64n32 \
947 parisc parisc64 \
948 ppc ppc64 \
949 s390 s390x \
950 sh"
951 fi
952
953 # sanity checks
954 kernel_dir="$1"
955 if [[ -z $kernel_dir ]]; then
956 usage
957 exit 1
958 fi
959 if [[ ! -d $kernel_dir ]]; then
960 echo "error: \"$1\" is not a valid directory"
961 exit 1
962 fi
963
964 if [[ ! -x "$LIB_SYS_DUMP" ]]; then
965 echo "error: \"$LIB_SYS_DUMP\" is not present"
966 exit 1
967 fi
968
969 # generate some temp files
970 tmp_lib=$(mktemp -t syscall_validate_XXXXXX)
971 tmp_sys=$(mktemp -t syscall_validate_XXXXXX)
972
973 if [[ -n $opt_csv ]]; then
974 # generate the syscall csv file
975 if [[ $opt_lib -eq 1 ]]; then
976 gen_csv $kernel_dir "lib" $opt_csv
977 else
978 gen_csv $kernel_dir "sys" $opt_csv
979 fi
980 else
981 # loop through the architectures and compare
982 for i in $opt_arches; do
983 # dump the syscall tables
984 dump_lib $i > $tmp_lib
985 if [[ $? -ne 0 ]]; then
986 echo "error: unknown arch $i"
987 exit 1
988 fi
989 dump_sys $i "$kernel_dir" > $tmp_sys
990 if [[ $? -ne 0 ]]; then
991 echo "error: unknown arch $i"
992 exit 1
993 fi
994
995 if [[ $opt_lib -eq 1 ]]; then
996 cat $tmp_lib
997 elif [[ $opt_sys -eq 1 ]]; then
998 cat $tmp_sys
999 else
1000 # compare the lib and sys output
1001 diff -u --label="$i [library]" $tmp_lib \
1002 --label "$i [system]" $tmp_sys
1003 fi
1004 done
1005 fi
1006
1007 # cleanup and exit
1008 rm -f $tmp_lib $tmp_sys
1009
1010 exit 0