cavatools: initialize repository
[cavatools.git] / caveat / make_ecall_tbl.py
1 #
2 # Copyright (c) 2020 Peter Hsu. All Rights Reserved. See LICENCE file for details.
3 #
4
5 import re
6 import os
7
8 PKpattern = re.compile(r'#define\s+SYS_(\S+)\s+(\d+)')
9 RVpattern = re.compile(r'#define\s+TARGET_NR_(\S+)\s+(\d+)')
10
11 # Algorith is we make table of RISC-V system call names and record
12 # their numbers, create a C file of names, include the host x86
13 # 'asm/unistd_64.h' file to get the correct mapping.
14
15 ecall = {}
16 enames = {}
17 highest = -1
18 rv = open('../include/pk-syscall.h', 'r')
19 for line in rv:
20 m = PKpattern.match(line)
21 if m:
22 name, num = m.groups()
23 num = int(num)
24 ecall[num] = name
25 enames[name] = num
26 highest = max(num, highest)
27 rv.close()
28
29 rv = open('../include/syscall64_nr.h', 'r')
30 for line in rv:
31 m = RVpattern.match(line)
32 if m:
33 name, num = m.groups()
34 num = int(num)
35 if num in ecall and name != ecall[num]:
36 print('libc {:s} override pk {:s} ecall'.format(name, ecall[num]))
37 ecall[num] = name
38 enames[name] = num
39 highest = max(num, highest)
40
41 en = open('ecall_nums.h', 'w')
42
43 for name in sorted(enames.keys()):
44 en.write('#ifndef __NR_{:s}\n'.format(name))
45 en.write('#define __NR_{:s} -2\n'.format(name))
46 en.write('#endif\n')
47
48 en.write("""\n
49 static const struct {
50 int sysnum;
51 const char*name;
52 } rv_to_host[] = {
53 """)
54
55 for n in range(0, highest+1):
56 if n in ecall:
57 name = ecall[n]
58 en.write(' /* {:5d} */ {{ __NR_{:s}, "{:s}" }},\n'.format(n, name, name))
59 else:
60 en.write(' /* {:5d} */ {{ -1, 0 }},\n'.format(n))
61
62 en.write('};\n\n')
63 en.write('const int rv_syscall_entries = {:d};\n\n'.format(highest+1))
64 en.close()