1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
#!/usr/bin/python
# This tool is used to generate the assembler system call stubs,
# the header files listing all available system calls, and the
# makefiles used to build all the stubs.
import atexit
import commands
import filecmp
import glob
import logging
import os.path
import re
import shutil
import stat
import string
import sys
import tempfile
all_arches = [ "arm", "arm64", "mips", "mips64", "x86", "x86_64" ]
# temp directory where we store all intermediate files
bionic_temp = tempfile.mkdtemp(prefix="bionic_gensyscalls");
# Make sure the directory is deleted when the script exits.
atexit.register(shutil.rmtree, bionic_temp)
bionic_libc_root = os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc")
warning = "Generated by gensyscalls.py. Do not edit."
DRY_RUN = False
def make_dir(path):
path = os.path.abspath(path)
if not os.path.exists(path):
parent = os.path.dirname(path)
if parent:
make_dir(parent)
os.mkdir(path)
def create_file(relpath):
full_path = os.path.join(bionic_temp, relpath)
dir = os.path.dirname(full_path)
make_dir(dir)
return open(full_path, "w")
syscall_stub_header = "/* " + warning + " */\n" + \
"""
#include <private/bionic_asm.h>
ENTRY(%(func)s)
"""
#
# ARM assembler templates for each syscall stub
#
arm_eabi_call_default = syscall_stub_header + """\
mov ip, r7
ldr r7, =%(__NR_name)s
swi #0
mov r7, ip
cmn r0, #(MAX_ERRNO + 1)
bxls lr
neg r0, r0
b __set_errno_internal
END(%(func)s)
"""
arm_eabi_call_long = syscall_stub_header + """\
mov ip, sp
stmfd sp!, {r4, r5, r6, r7}
.cfi_def_cfa_offset 16
.cfi_rel_offset r4, 0
.cfi_rel_offset r5, 4
.cfi_rel_offset r6, 8
.cfi_rel_offset r7, 12
ldmfd ip, {r4, r5, r6}
ldr r7, =%(__NR_name)s
swi #0
ldmfd sp!, {r4, r5, r6, r7}
.cfi_def_cfa_offset 0
cmn r0, #(MAX_ERRNO + 1)
bxls lr
neg r0, r0
b __set_errno_internal
END(%(func)s)
"""
#
# Arm64 assembler templates for each syscall stub
#
arm64_call = syscall_stub_header + """\
mov x8, %(__NR_name)s
svc #0
cmn x0, #(MAX_ERRNO + 1)
cneg x0, x0, hi
b.hi __set_errno_internal
ret
END(%(func)s)
"""
#
# MIPS assembler templates for each syscall stub
#
mips_call = syscall_stub_header + """\
.set noreorder
.cpload t9
li v0, %(__NR_name)s
syscall
bnez a3, 1f
move a0, v0
j ra
nop
1:
la t9,__set_errno_internal
j t9
nop
.set reorder
END(%(func)s)
"""
#
# MIPS64 assembler templates for each syscall stub
#
mips64_call = syscall_stub_header + """\
.set push
.set noreorder
li v0, %(__NR_name)s
syscall
bnez a3, 1f
move a0, v0
j ra
nop
1:
move t0, ra
bal 2f
nop
2:
.cpsetup ra, t1, 2b
LA t9,__set_errno_internal
.cpreturn
j t9
move ra, t0
.set pop
END(%(func)s)
"""
#
# x86 assembler templates for each syscall stub
#
x86_registers = [ "ebx", "ecx", "edx", "esi", "edi", "ebp" ]
x86_call = """\
movl $%(__NR_name)s, %%eax
int $0x80
cmpl $-MAX_ERRNO, %%eax
jb 1f
negl %%eax
pushl %%eax
call __set_errno_internal
addl $4, %%esp
1:
"""
x86_return = """\
ret
END(%(func)s)
"""
#
# x86_64 assembler templates for each syscall stub
#
x86_64_call = """\
movl $%(__NR_name)s, %%eax
syscall
cmpq $-MAX_ERRNO, %%rax
jb 1f
negl %%eax
movl %%eax, %%edi
call __set_errno_internal
1:
ret
END(%(func)s)
"""
def param_uses_64bits(param):
"""Returns True iff a syscall parameter description corresponds
to a 64-bit type."""
param = param.strip()
# First, check that the param type begins with one of the known
# 64-bit types.
if not ( \
param.startswith("int64_t") or param.startswith("uint64_t") or \
param.startswith("loff_t") or param.startswith("off64_t") or \
param.startswith("long long") or param.startswith("unsigned long long") or
param.startswith("signed long long") ):
return False
# Second, check that there is no pointer type here
if param.find("*") >= 0:
return False
# Ok
return True
def count_arm_param_registers(params):
"""This function is used to count the number of register used
to pass parameters when invoking an ARM system call.
This is because the ARM EABI mandates that 64-bit quantities
must be passed in an even+odd register pair. So, for example,
something like:
foo(int fd, off64_t pos)
would actually need 4 registers:
r0 -> int
r1 -> unused
r2-r3 -> pos
"""
count = 0
for param in params:
if param_uses_64bits(param):
if (count & 1) != 0:
count += 1
count += 2
else:
count += 1
return count
def count_generic_param_registers(params):
count = 0
for param in params:
if param_uses_64bits(param):
count += 2
else:
count += 1
return count
def count_generic_param_registers64(params):
count = 0
for param in params:
count += 1
return count
# This lets us support regular system calls like __NR_write and also weird
# ones like __ARM_NR_cacheflush, where the NR doesn't come at the start.
def make__NR_name(name):
if name.startswith("__ARM_NR_"):
return name
else:
return "__NR_%s" % (name)
def add_footer(pointer_length, stub, syscall):
# Add any aliases for this syscall.
aliases = syscall["aliases"]
for alias in aliases:
stub += "\nALIAS_SYMBOL(%s, %s)\n" % (alias, syscall["func"])
# Use hidden visibility on LP64 for any functions beginning with underscores.
# Force hidden visibility for any functions which begin with 3 underscores
if (pointer_length == 64 and syscall["func"].startswith("__")) or syscall["func"].startswith("___"):
stub += '.hidden ' + syscall["func"] + '\n'
return stub
def arm_eabi_genstub(syscall):
num_regs = count_arm_param_registers(syscall["params"])
if num_regs > 4:
return arm_eabi_call_long % syscall
return arm_eabi_call_default % syscall
def arm64_genstub(syscall):
return arm64_call % syscall
def mips_genstub(syscall):
return mips_call % syscall
def mips64_genstub(syscall):
return mips64_call % syscall
def x86_genstub(syscall):
result = syscall_stub_header % syscall
numparams = count_generic_param_registers(syscall["params"])
stack_bias = numparams*4 + 4
offset = 0
mov_result = ""
first_push = True
for register in x86_registers[:numparams]:
result += " pushl %%%s\n" % register
if first_push:
result += " .cfi_def_cfa_offset 8\n"
result += " .cfi_rel_offset %s, 0\n" % register
first_push = False
else:
result += " .cfi_adjust_cfa_offset 4\n"
result += " .cfi_rel_offset %s, 0\n" % register
mov_result += " mov %d(%%esp), %%%s\n" % (stack_bias+offset, register)
offset += 4
result += mov_result
result += x86_call % syscall
for register in reversed(x86_registers[:numparams]):
result += " popl %%%s\n" % register
result += x86_return % syscall
return result
def x86_genstub_socketcall(syscall):
# %ebx <--- Argument 1 - The call id of the needed vectored
# syscall (socket, bind, recv, etc)
# %ecx <--- Argument 2 - Pointer to the rest of the arguments
# from the original function called (socket())
result = syscall_stub_header % syscall
# save the regs we need
result += " pushl %ebx\n"
result += " .cfi_def_cfa_offset 8\n"
result += " .cfi_rel_offset ebx, 0\n"
result += " pushl %ecx\n"
result += " .cfi_adjust_cfa_offset 4\n"
result += " .cfi_rel_offset ecx, 0\n"
stack_bias = 12
# set the call id (%ebx)
result += " mov $%d, %%ebx\n" % syscall["socketcall_id"]
# set the pointer to the rest of the args into %ecx
result += " mov %esp, %ecx\n"
result += " addl $%d, %%ecx\n" % (stack_bias)
# now do the syscall code itself
result += x86_call % syscall
# now restore the saved regs
result += " popl %ecx\n"
result += " popl %ebx\n"
# epilog
result += x86_return % syscall
return result
def x86_64_genstub(syscall):
result = syscall_stub_header % syscall
num_regs = count_generic_param_registers64(syscall["params"])
if (num_regs > 3):
# rcx is used as 4th argument. Kernel wants it at r10.
result += " movq %rcx, %r10\n"
result += x86_64_call % syscall
return result
class SysCallsTxtParser:
def __init__(self):
self.syscalls = []
self.lineno = 0
def E(self, msg):
print "%d: %s" % (self.lineno, msg)
def parse_line(self, line):
""" parse a syscall spec line.
line processing, format is
return type func_name[|alias_list][:syscall_name[:socketcall_id]] ( [paramlist] ) architecture_list
"""
pos_lparen = line.find('(')
E = self.E
if pos_lparen < 0:
E("missing left parenthesis in '%s'" % line)
return
pos_rparen = line.rfind(')')
if pos_rparen < 0 or pos_rparen <= pos_lparen:
E("missing or misplaced right parenthesis in '%s'" % line)
return
return_type = line[:pos_lparen].strip().split()
if len(return_type) < 2:
E("missing return type in '%s'" % line)
return
syscall_func = return_type[-1]
return_type = string.join(return_type[:-1],' ')
socketcall_id = -1
pos_colon = syscall_func.find(':')
if pos_colon < 0:
syscall_name = syscall_func
else:
if pos_colon == 0 or pos_colon+1 >= len(syscall_func):
E("misplaced colon in '%s'" % line)
return
# now find if there is a socketcall_id for a dispatch-type syscall
# after the optional 2nd colon
pos_colon2 = syscall_func.find(':', pos_colon + 1)
if pos_colon2 < 0:
syscall_name = syscall_func[pos_colon+1:]
syscall_func = syscall_func[:pos_colon]
else:
if pos_colon2+1 >= len(syscall_func):
E("misplaced colon2 in '%s'" % line)
return
syscall_name = syscall_func[(pos_colon+1):pos_colon2]
socketcall_id = int(syscall_func[pos_colon2+1:])
syscall_func = syscall_func[:pos_colon]
alias_delim = syscall_func.find('|')
if alias_delim > 0:
alias_list = syscall_func[alias_delim+1:].strip()
syscall_func = syscall_func[:alias_delim]
alias_delim = syscall_name.find('|')
if alias_delim > 0:
syscall_name = syscall_name[:alias_delim]
syscall_aliases = string.split(alias_list, ',')
else:
syscall_aliases = []
if pos_rparen > pos_lparen+1:
syscall_params = line[pos_lparen+1:pos_rparen].split(',')
params = string.join(syscall_params,',')
else:
syscall_params = []
params = "void"
t = {
"name" : syscall_name,
"func" : syscall_func,
"aliases" : syscall_aliases,
"params" : syscall_params,
"decl" : "%-15s %s (%s);" % (return_type, syscall_func, params),
"socketcall_id" : socketcall_id
}
# Parse the architecture list.
arch_list = line[pos_rparen+1:].strip()
if arch_list == "all":
for arch in all_arches:
t[arch] = True
else:
for arch in string.split(arch_list, ','):
if arch in all_arches:
t[arch] = True
else:
E("invalid syscall architecture '%s' in '%s'" % (arch, line))
return
self.syscalls.append(t)
logging.debug(t)
def parse_file(self, file_path):
logging.debug("parse_file: %s" % file_path)
fp = open(file_path)
for line in fp.xreadlines():
self.lineno += 1
line = line.strip()
if not line: continue
if line[0] == '#': continue
self.parse_line(line)
fp.close()
class State:
def __init__(self):
self.old_stubs = []
self.new_stubs = []
self.other_files = []
self.syscalls = []
def process_file(self, input):
parser = SysCallsTxtParser()
parser.parse_file(input)
self.syscalls = parser.syscalls
parser = None
for syscall in self.syscalls:
syscall["__NR_name"] = make__NR_name(syscall["name"])
if syscall.has_key("arm"):
syscall["asm-arm"] = add_footer(32, arm_eabi_genstub(syscall), syscall)
if syscall.has_key("arm64"):
syscall["asm-arm64"] = add_footer(64, arm64_genstub(syscall), syscall)
if syscall.has_key("x86"):
if syscall["socketcall_id"] >= 0:
syscall["asm-x86"] = add_footer(32, x86_genstub_socketcall(syscall), syscall)
else:
syscall["asm-x86"] = add_footer(32, x86_genstub(syscall), syscall)
elif syscall["socketcall_id"] >= 0:
E("socketcall_id for dispatch syscalls is only supported for x86 in '%s'" % t)
return
if syscall.has_key("mips"):
syscall["asm-mips"] = add_footer(32, mips_genstub(syscall), syscall)
if syscall.has_key("mips64"):
syscall["asm-mips64"] = add_footer(64, mips64_genstub(syscall), syscall)
if syscall.has_key("x86_64"):
syscall["asm-x86_64"] = add_footer(64, x86_64_genstub(syscall), syscall)
# Scan a Linux kernel asm/unistd.h file containing __NR_* constants
# and write out equivalent SYS_* constants for glibc source compatibility.
def scan_linux_unistd_h(self, fp, path):
pattern = re.compile(r'^#define __NR_([a-z]\S+) .*')
syscalls = set() # MIPS defines everything three times; work around that.
for line in open(path):
m = re.search(pattern, line)
if m:
syscalls.add(m.group(1))
for syscall in sorted(syscalls):
fp.write("#define SYS_%s %s\n" % (syscall, make__NR_name(syscall)))
def gen_glibc_syscalls_h(self):
# TODO: generate a separate file for each architecture, like glibc's bits/syscall.h.
glibc_syscalls_h_path = "include/sys/glibc-syscalls.h"
logging.info("generating " + glibc_syscalls_h_path)
glibc_fp = create_file(glibc_syscalls_h_path)
glibc_fp.write("/* %s */\n" % warning)
glibc_fp.write("#ifndef _BIONIC_GLIBC_SYSCALLS_H_\n")
glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n")
glibc_fp.write("#if defined(__aarch64__)\n")
self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-generic/unistd.h"))
glibc_fp.write("#elif defined(__arm__)\n")
self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-arm/asm/unistd.h"))
glibc_fp.write("#elif defined(__mips__)\n")
self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-mips/asm/unistd.h"))
glibc_fp.write("#elif defined(__i386__)\n")
self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-x86/asm/unistd_32.h"))
glibc_fp.write("#elif defined(__x86_64__)\n")
self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-x86/asm/unistd_64.h"))
glibc_fp.write("#endif\n")
glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n")
glibc_fp.close()
self.other_files.append(glibc_syscalls_h_path)
# Write each syscall stub.
def gen_syscall_stubs(self):
for syscall in self.syscalls:
for arch in all_arches:
if syscall.has_key("asm-%s" % arch):
filename = "arch-%s/syscalls/%s.S" % (arch, syscall["func"])
logging.info(">>> generating " + filename)
fp = create_file(filename)
fp.write(syscall["asm-%s" % arch])
fp.close()
self.new_stubs.append(filename)
def regenerate(self):
logging.info("scanning for existing architecture-specific stub files...")
for arch in all_arches:
arch_dir = "arch-" + arch
logging.info("scanning " + os.path.join(bionic_libc_root, arch_dir))
rel_path = os.path.join(arch_dir, "syscalls")
for file in os.listdir(os.path.join(bionic_libc_root, rel_path)):
if file.endswith(".S"):
self.old_stubs.append(os.path.join(rel_path, file))
logging.info("found %d stub files" % len(self.old_stubs))
if not os.path.exists(bionic_temp):
logging.info("creating %s..." % bionic_temp)
make_dir(bionic_temp)
logging.info("re-generating stubs and support files...")
self.gen_glibc_syscalls_h()
self.gen_syscall_stubs()
logging.info("comparing files...")
adds = []
edits = []
for stub in self.new_stubs + self.other_files:
tmp_file = os.path.join(bionic_temp, stub)
libc_file = os.path.join(bionic_libc_root, stub)
if not os.path.exists(libc_file):
# new file, git add it
logging.info("new file: " + stub)
adds.append(libc_file)
shutil.copyfile(tmp_file, libc_file)
elif not filecmp.cmp(tmp_file, libc_file):
logging.info("changed file: " + stub)
edits.append(stub)
deletes = []
for stub in self.old_stubs:
if not stub in self.new_stubs:
logging.info("deleted file: " + stub)
deletes.append(os.path.join(bionic_libc_root, stub))
if not DRY_RUN:
if adds:
commands.getoutput("git add " + " ".join(adds))
if deletes:
commands.getoutput("git rm " + " ".join(deletes))
if edits:
for file in edits:
shutil.copyfile(os.path.join(bionic_temp, file),
os.path.join(bionic_libc_root, file))
commands.getoutput("git add " + " ".join((os.path.join(bionic_libc_root, file)) for file in edits))
commands.getoutput("git add %s" % (os.path.join(bionic_libc_root, "SYSCALLS.TXT")))
if (not adds) and (not deletes) and (not edits):
logging.info("no changes detected!")
else:
logging.info("ready to go!!")
logging.basicConfig(level=logging.INFO)
state = State()
state.process_file(os.path.join(bionic_libc_root, "SYSCALLS.TXT"))
state.regenerate()
|