blob: 4e240777ab31beda14633fd17ee598d72bbe8f9c [file] [log] [blame]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001#!/usr/bin/python
Pavel Chupinf12a18b2012-12-12 13:11:48 +04002
3# This tool is used to generate the assembler system call stubs,
4# the header files listing all available system calls, and the
5# makefiles used to build all the stubs.
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07006
Christopher Ferris01bd32e2014-08-05 12:19:27 -07007import atexit
Elliott Hughes103ccde2013-10-16 14:27:59 -07008import commands
9import filecmp
10import glob
Elliott Hughesdc1fb702014-08-20 11:16:11 -070011import logging
Elliott Hughes103ccde2013-10-16 14:27:59 -070012import os.path
13import re
14import shutil
15import stat
Elliott Hughesdc1fb702014-08-20 11:16:11 -070016import string
Elliott Hughes103ccde2013-10-16 14:27:59 -070017import sys
Christopher Ferris01bd32e2014-08-05 12:19:27 -070018import tempfile
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070019
Elliott Hughesdc1fb702014-08-20 11:16:11 -070020
21all_arches = [ "arm", "arm64", "mips", "mips64", "x86", "x86_64" ]
22
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070023
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070024# temp directory where we store all intermediate files
Christopher Ferris01bd32e2014-08-05 12:19:27 -070025bionic_temp = tempfile.mkdtemp(prefix="bionic_gensyscalls");
26# Make sure the directory is deleted when the script exits.
27atexit.register(shutil.rmtree, bionic_temp)
28
29bionic_libc_root = os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070030
Elliott Hughes103ccde2013-10-16 14:27:59 -070031warning = "Generated by gensyscalls.py. Do not edit."
32
Pavel Chupinf12a18b2012-12-12 13:11:48 +040033DRY_RUN = False
34
35def make_dir(path):
Raghu Gandham1fa0d842012-01-27 17:51:42 -080036 path = os.path.abspath(path)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070037 if not os.path.exists(path):
38 parent = os.path.dirname(path)
39 if parent:
40 make_dir(parent)
41 os.mkdir(path)
42
Elliott Hughes0437f3f2013-10-07 23:53:13 -070043
Pavel Chupinf12a18b2012-12-12 13:11:48 +040044def create_file(relpath):
Christopher Ferris01bd32e2014-08-05 12:19:27 -070045 full_path = os.path.join(bionic_temp, relpath)
46 dir = os.path.dirname(full_path)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070047 make_dir(dir)
Christopher Ferris01bd32e2014-08-05 12:19:27 -070048 return open(full_path, "w")
Pavel Chupinf12a18b2012-12-12 13:11:48 +040049
50
Elliott Hughes103ccde2013-10-16 14:27:59 -070051syscall_stub_header = "/* " + warning + " */\n" + \
52"""
Elliott Hughesed744842013-11-07 10:31:05 -080053#include <private/bionic_asm.h>
Pavel Chupinf12a18b2012-12-12 13:11:48 +040054
Elliott Hughes0437f3f2013-10-07 23:53:13 -070055ENTRY(%(func)s)
Pavel Chupinf12a18b2012-12-12 13:11:48 +040056"""
57
Elliott Hughes0437f3f2013-10-07 23:53:13 -070058
H.J. Lu6fe4e872013-10-04 10:03:17 -070059function_alias = """
Elliott Hughes986f9062014-02-18 16:42:36 -080060 .globl %(alias)s
61 .equ %(alias)s, %(func)s
H.J. Lu6fe4e872013-10-04 10:03:17 -070062"""
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070063
Elliott Hughes0437f3f2013-10-07 23:53:13 -070064
65#
66# ARM assembler templates for each syscall stub
67#
68
69arm_eabi_call_default = syscall_stub_header + """\
70 mov ip, r7
71 ldr r7, =%(__NR_name)s
72 swi #0
73 mov r7, ip
74 cmn r0, #(MAX_ERRNO + 1)
75 bxls lr
76 neg r0, r0
Elliott Hughes011e1112014-09-08 15:25:01 -070077 b __set_errno_internal
Elliott Hughes0437f3f2013-10-07 23:53:13 -070078END(%(func)s)
79"""
80
81arm_eabi_call_long = syscall_stub_header + """\
82 mov ip, sp
Elliott Hughes0437f3f2013-10-07 23:53:13 -070083 stmfd sp!, {r4, r5, r6, r7}
Christopher Ferrised459702013-12-02 17:44:53 -080084 .cfi_def_cfa_offset 16
85 .cfi_rel_offset r4, 0
86 .cfi_rel_offset r5, 4
87 .cfi_rel_offset r6, 8
88 .cfi_rel_offset r7, 12
Elliott Hughes0437f3f2013-10-07 23:53:13 -070089 ldmfd ip, {r4, r5, r6}
90 ldr r7, =%(__NR_name)s
91 swi #0
92 ldmfd sp!, {r4, r5, r6, r7}
Christopher Ferrised459702013-12-02 17:44:53 -080093 .cfi_def_cfa_offset 0
Elliott Hughes0437f3f2013-10-07 23:53:13 -070094 cmn r0, #(MAX_ERRNO + 1)
95 bxls lr
96 neg r0, r0
Elliott Hughes011e1112014-09-08 15:25:01 -070097 b __set_errno_internal
Elliott Hughes0437f3f2013-10-07 23:53:13 -070098END(%(func)s)
99"""
100
101
102#
Colin Crossd1973ca2014-01-21 19:50:58 -0800103# Arm64 assembler templates for each syscall stub
104#
105
106arm64_call = syscall_stub_header + """\
Colin Crossd1973ca2014-01-21 19:50:58 -0800107 mov x8, %(__NR_name)s
108 svc #0
109
Colin Crossd1973ca2014-01-21 19:50:58 -0800110 cmn x0, #(MAX_ERRNO + 1)
111 cneg x0, x0, hi
Elliott Hughes011e1112014-09-08 15:25:01 -0700112 b.hi __set_errno_internal
Colin Crossd1973ca2014-01-21 19:50:58 -0800113
114 ret
115END(%(func)s)
116"""
117
118
119#
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700120# MIPS assembler templates for each syscall stub
121#
122
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800123mips_call = syscall_stub_header + """\
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700124 .set noreorder
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800125 .cpload t9
126 li v0, %(__NR_name)s
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700127 syscall
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800128 bnez a3, 1f
129 move a0, v0
130 j ra
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700131 nop
1321:
Elliott Hughes011e1112014-09-08 15:25:01 -0700133 la t9,__set_errno_internal
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800134 j t9
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700135 nop
136 .set reorder
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800137END(%(func)s)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700138"""
139
140
Elliott Hughescd6780b2013-02-07 14:07:00 -0800141#
Chris Dearman50432122014-02-05 16:59:23 -0800142# MIPS64 assembler templates for each syscall stub
143#
144
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800145mips64_call = syscall_stub_header + """\
Chris Dearman50432122014-02-05 16:59:23 -0800146 .set push
147 .set noreorder
148 li v0, %(__NR_name)s
149 syscall
150 bnez a3, 1f
151 move a0, v0
152 j ra
153 nop
1541:
155 move t0, ra
156 bal 2f
157 nop
1582:
159 .cpsetup ra, t1, 2b
Elliott Hughes011e1112014-09-08 15:25:01 -0700160 LA t9,__set_errno_internal
Chris Dearman50432122014-02-05 16:59:23 -0800161 .cpreturn
162 j t9
163 move ra, t0
164 .set pop
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800165END(%(func)s)
Chris Dearman50432122014-02-05 16:59:23 -0800166"""
167
168
169#
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700170# x86 assembler templates for each syscall stub
171#
172
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800173x86_registers = [ "ebx", "ecx", "edx", "esi", "edi", "ebp" ]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700174
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700175x86_call = """\
176 movl $%(__NR_name)s, %%eax
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700177 int $0x80
Elliott Hughes9aceab52013-03-12 14:57:30 -0700178 cmpl $-MAX_ERRNO, %%eax
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700179 jb 1f
180 negl %%eax
181 pushl %%eax
Elliott Hughes011e1112014-09-08 15:25:01 -0700182 call __set_errno_internal
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700183 addl $4, %%esp
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001841:
185"""
186
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700187x86_return = """\
188 ret
189END(%(func)s)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700190"""
191
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700192
Elliott Hughescd6780b2013-02-07 14:07:00 -0800193#
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400194# x86_64 assembler templates for each syscall stub
195#
196
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700197x86_64_call = """\
198 movl $%(__NR_name)s, %%eax
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400199 syscall
200 cmpq $-MAX_ERRNO, %%rax
201 jb 1f
202 negl %%eax
203 movl %%eax, %%edi
Elliott Hughes011e1112014-09-08 15:25:01 -0700204 call __set_errno_internal
Pavel Chupinf12a18b2012-12-12 13:11:48 +04002051:
206 ret
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700207END(%(func)s)
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400208"""
209
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800210
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100211def param_uses_64bits(param):
212 """Returns True iff a syscall parameter description corresponds
213 to a 64-bit type."""
214 param = param.strip()
215 # First, check that the param type begins with one of the known
216 # 64-bit types.
217 if not ( \
218 param.startswith("int64_t") or param.startswith("uint64_t") or \
219 param.startswith("loff_t") or param.startswith("off64_t") or \
220 param.startswith("long long") or param.startswith("unsigned long long") or
221 param.startswith("signed long long") ):
222 return False
223
224 # Second, check that there is no pointer type here
225 if param.find("*") >= 0:
226 return False
227
228 # Ok
229 return True
230
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700231
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100232def count_arm_param_registers(params):
233 """This function is used to count the number of register used
Elliott Hughescd6780b2013-02-07 14:07:00 -0800234 to pass parameters when invoking an ARM system call.
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100235 This is because the ARM EABI mandates that 64-bit quantities
236 must be passed in an even+odd register pair. So, for example,
237 something like:
238
239 foo(int fd, off64_t pos)
240
241 would actually need 4 registers:
242 r0 -> int
243 r1 -> unused
244 r2-r3 -> pos
245 """
246 count = 0
247 for param in params:
248 if param_uses_64bits(param):
249 if (count & 1) != 0:
250 count += 1
251 count += 2
252 else:
253 count += 1
254 return count
255
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700256
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100257def count_generic_param_registers(params):
258 count = 0
259 for param in params:
260 if param_uses_64bits(param):
261 count += 2
262 else:
263 count += 1
264 return count
265
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700266
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400267def count_generic_param_registers64(params):
268 count = 0
269 for param in params:
270 count += 1
271 return count
272
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700273
Elliott Hughescda62092013-03-22 13:50:44 -0700274# This lets us support regular system calls like __NR_write and also weird
275# ones like __ARM_NR_cacheflush, where the NR doesn't come at the start.
276def make__NR_name(name):
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700277 if name.startswith("__ARM_NR_"):
Elliott Hughescda62092013-03-22 13:50:44 -0700278 return name
279 else:
280 return "__NR_%s" % (name)
281
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700282
Elliott Hughesfff6e272013-10-24 17:03:20 -0700283def add_footer(pointer_length, stub, syscall):
284 # Add any aliases for this syscall.
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700285 aliases = syscall["aliases"]
286 for alias in aliases:
287 stub += function_alias % { "func" : syscall["func"], "alias" : alias }
Elliott Hughesfff6e272013-10-24 17:03:20 -0700288
289 # Use hidden visibility for any functions beginning with underscores.
Elliott Hughesfff6e272013-10-24 17:03:20 -0700290 if pointer_length == 64 and syscall["func"].startswith("__"):
Elliott Hughesd465eb42014-02-19 18:59:19 -0800291 stub += '.hidden ' + syscall["func"] + '\n'
Elliott Hughesfff6e272013-10-24 17:03:20 -0700292
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700293 return stub
294
295
296def arm_eabi_genstub(syscall):
297 num_regs = count_arm_param_registers(syscall["params"])
298 if num_regs > 4:
299 return arm_eabi_call_long % syscall
300 return arm_eabi_call_default % syscall
301
302
Colin Crossd1973ca2014-01-21 19:50:58 -0800303def arm64_genstub(syscall):
304 return arm64_call % syscall
305
306
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700307def mips_genstub(syscall):
308 return mips_call % syscall
309
310
Chris Dearman50432122014-02-05 16:59:23 -0800311def mips64_genstub(syscall):
312 return mips64_call % syscall
313
314
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700315def x86_genstub(syscall):
316 result = syscall_stub_header % syscall
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700317
318 numparams = count_generic_param_registers(syscall["params"])
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800319 stack_bias = numparams*4 + 4
320 offset = 0
321 mov_result = ""
Christopher Ferris15b91e92014-05-29 18:17:09 -0700322 first_push = True
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800323 for register in x86_registers[:numparams]:
324 result += " pushl %%%s\n" % register
Christopher Ferris15b91e92014-05-29 18:17:09 -0700325 if first_push:
326 result += " .cfi_def_cfa_offset 8\n"
327 result += " .cfi_rel_offset %s, 0\n" % register
328 first_push = False
329 else:
330 result += " .cfi_adjust_cfa_offset 4\n"
331 result += " .cfi_rel_offset %s, 0\n" % register
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800332 mov_result += " mov %d(%%esp), %%%s\n" % (stack_bias+offset, register)
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800333 offset += 4
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700334
Christopher Ferris15b91e92014-05-29 18:17:09 -0700335 result += mov_result
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700336 result += x86_call % syscall
337
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800338 for register in reversed(x86_registers[:numparams]):
339 result += " popl %%%s\n" % register
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700340
341 result += x86_return % syscall
342 return result
343
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100344
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700345def x86_genstub_socketcall(syscall):
346 # %ebx <--- Argument 1 - The call id of the needed vectored
347 # syscall (socket, bind, recv, etc)
348 # %ecx <--- Argument 2 - Pointer to the rest of the arguments
349 # from the original function called (socket())
350
351 result = syscall_stub_header % syscall
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700352
353 # save the regs we need
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800354 result += " pushl %ebx\n"
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800355 result += " .cfi_def_cfa_offset 8\n"
356 result += " .cfi_rel_offset ebx, 0\n"
Christopher Ferris15b91e92014-05-29 18:17:09 -0700357 result += " pushl %ecx\n"
358 result += " .cfi_adjust_cfa_offset 4\n"
359 result += " .cfi_rel_offset ecx, 0\n"
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800360 stack_bias = 12
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700361
362 # set the call id (%ebx)
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800363 result += " mov $%d, %%ebx\n" % syscall["socketcall_id"]
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700364
365 # set the pointer to the rest of the args into %ecx
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800366 result += " mov %esp, %ecx\n"
367 result += " addl $%d, %%ecx\n" % (stack_bias)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700368
369 # now do the syscall code itself
370 result += x86_call % syscall
371
372 # now restore the saved regs
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800373 result += " popl %ecx\n"
374 result += " popl %ebx\n"
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700375
376 # epilog
377 result += x86_return % syscall
378 return result
379
380
381def x86_64_genstub(syscall):
382 result = syscall_stub_header % syscall
383 num_regs = count_generic_param_registers64(syscall["params"])
384 if (num_regs > 3):
385 # rcx is used as 4th argument. Kernel wants it at r10.
386 result += " movq %rcx, %r10\n"
387
388 result += x86_64_call % syscall
389 return result
390
391
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700392class SysCallsTxtParser:
393 def __init__(self):
394 self.syscalls = []
395 self.lineno = 0
396
397 def E(self, msg):
398 print "%d: %s" % (self.lineno, msg)
399
400 def parse_line(self, line):
401 """ parse a syscall spec line.
402
403 line processing, format is
404 return type func_name[|alias_list][:syscall_name[:socketcall_id]] ( [paramlist] ) architecture_list
405 """
406 pos_lparen = line.find('(')
407 E = self.E
408 if pos_lparen < 0:
409 E("missing left parenthesis in '%s'" % line)
410 return
411
412 pos_rparen = line.rfind(')')
413 if pos_rparen < 0 or pos_rparen <= pos_lparen:
414 E("missing or misplaced right parenthesis in '%s'" % line)
415 return
416
417 return_type = line[:pos_lparen].strip().split()
418 if len(return_type) < 2:
419 E("missing return type in '%s'" % line)
420 return
421
422 syscall_func = return_type[-1]
423 return_type = string.join(return_type[:-1],' ')
424 socketcall_id = -1
425
426 pos_colon = syscall_func.find(':')
427 if pos_colon < 0:
428 syscall_name = syscall_func
429 else:
430 if pos_colon == 0 or pos_colon+1 >= len(syscall_func):
431 E("misplaced colon in '%s'" % line)
432 return
433
434 # now find if there is a socketcall_id for a dispatch-type syscall
435 # after the optional 2nd colon
436 pos_colon2 = syscall_func.find(':', pos_colon + 1)
437 if pos_colon2 < 0:
438 syscall_name = syscall_func[pos_colon+1:]
439 syscall_func = syscall_func[:pos_colon]
440 else:
441 if pos_colon2+1 >= len(syscall_func):
442 E("misplaced colon2 in '%s'" % line)
443 return
444 syscall_name = syscall_func[(pos_colon+1):pos_colon2]
445 socketcall_id = int(syscall_func[pos_colon2+1:])
446 syscall_func = syscall_func[:pos_colon]
447
448 alias_delim = syscall_func.find('|')
449 if alias_delim > 0:
450 alias_list = syscall_func[alias_delim+1:].strip()
451 syscall_func = syscall_func[:alias_delim]
452 alias_delim = syscall_name.find('|')
453 if alias_delim > 0:
454 syscall_name = syscall_name[:alias_delim]
455 syscall_aliases = string.split(alias_list, ',')
456 else:
457 syscall_aliases = []
458
459 if pos_rparen > pos_lparen+1:
460 syscall_params = line[pos_lparen+1:pos_rparen].split(',')
461 params = string.join(syscall_params,',')
462 else:
463 syscall_params = []
464 params = "void"
465
466 t = {
467 "name" : syscall_name,
468 "func" : syscall_func,
469 "aliases" : syscall_aliases,
470 "params" : syscall_params,
471 "decl" : "%-15s %s (%s);" % (return_type, syscall_func, params),
472 "socketcall_id" : socketcall_id
473 }
474
475 # Parse the architecture list.
476 arch_list = line[pos_rparen+1:].strip()
477 if arch_list == "all":
478 for arch in all_arches:
479 t[arch] = True
480 else:
481 for arch in string.split(arch_list, ','):
482 if arch in all_arches:
483 t[arch] = True
484 else:
485 E("invalid syscall architecture '%s' in '%s'" % (arch, line))
486 return
487
488 self.syscalls.append(t)
489
490 logging.debug(t)
491
492
493 def parse_file(self, file_path):
494 logging.debug("parse_file: %s" % file_path)
495 fp = open(file_path)
496 for line in fp.xreadlines():
497 self.lineno += 1
498 line = line.strip()
499 if not line: continue
500 if line[0] == '#': continue
501 self.parse_line(line)
502
503 fp.close()
504
505
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700506class State:
507 def __init__(self):
508 self.old_stubs = []
509 self.new_stubs = []
510 self.other_files = []
511 self.syscalls = []
512
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400513
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700514 def process_file(self, input):
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700515 parser = SysCallsTxtParser()
516 parser.parse_file(input)
517 self.syscalls = parser.syscalls
518 parser = None
519
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700520 for syscall in self.syscalls:
521 syscall["__NR_name"] = make__NR_name(syscall["name"])
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700522
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700523 if syscall.has_key("arm"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700524 syscall["asm-arm"] = add_footer(32, arm_eabi_genstub(syscall), syscall)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700525
Colin Crossd1973ca2014-01-21 19:50:58 -0800526 if syscall.has_key("arm64"):
527 syscall["asm-arm64"] = add_footer(64, arm64_genstub(syscall), syscall)
528
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700529 if syscall.has_key("x86"):
530 if syscall["socketcall_id"] >= 0:
Elliott Hughesfff6e272013-10-24 17:03:20 -0700531 syscall["asm-x86"] = add_footer(32, x86_genstub_socketcall(syscall), syscall)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800532 else:
Elliott Hughesfff6e272013-10-24 17:03:20 -0700533 syscall["asm-x86"] = add_footer(32, x86_genstub(syscall), syscall)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700534 elif syscall["socketcall_id"] >= 0:
Elliott Hughesd6121652013-09-25 22:43:36 -0700535 E("socketcall_id for dispatch syscalls is only supported for x86 in '%s'" % t)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800536 return
Elliott Hughescd6780b2013-02-07 14:07:00 -0800537
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700538 if syscall.has_key("mips"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700539 syscall["asm-mips"] = add_footer(32, mips_genstub(syscall), syscall)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800540
Chris Dearman50432122014-02-05 16:59:23 -0800541 if syscall.has_key("mips64"):
542 syscall["asm-mips64"] = add_footer(64, mips64_genstub(syscall), syscall)
543
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700544 if syscall.has_key("x86_64"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700545 syscall["asm-x86_64"] = add_footer(64, x86_64_genstub(syscall), syscall)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700546
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700547 # Scan a Linux kernel asm/unistd.h file containing __NR_* constants
548 # and write out equivalent SYS_* constants for glibc source compatibility.
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700549 def scan_linux_unistd_h(self, fp, path):
550 pattern = re.compile(r'^#define __NR_([a-z]\S+) .*')
551 syscalls = set() # MIPS defines everything three times; work around that.
552 for line in open(path):
553 m = re.search(pattern, line)
554 if m:
555 syscalls.add(m.group(1))
556 for syscall in sorted(syscalls):
Elliott Hughescda62092013-03-22 13:50:44 -0700557 fp.write("#define SYS_%s %s\n" % (syscall, make__NR_name(syscall)))
Elliott Hughes8ecf2252013-03-21 18:06:55 -0700558
559
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700560 def gen_glibc_syscalls_h(self):
Elliott Hughescda62092013-03-22 13:50:44 -0700561 # TODO: generate a separate file for each architecture, like glibc's bits/syscall.h.
Elliott Hughes9724ce32013-03-21 19:43:54 -0700562 glibc_syscalls_h_path = "include/sys/glibc-syscalls.h"
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700563 logging.info("generating " + glibc_syscalls_h_path)
Elliott Hughes9724ce32013-03-21 19:43:54 -0700564 glibc_fp = create_file(glibc_syscalls_h_path)
Elliott Hughes103ccde2013-10-16 14:27:59 -0700565 glibc_fp.write("/* %s */\n" % warning)
Elliott Hughes9724ce32013-03-21 19:43:54 -0700566 glibc_fp.write("#ifndef _BIONIC_GLIBC_SYSCALLS_H_\n")
567 glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n")
568
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100569 glibc_fp.write("#if defined(__aarch64__)\n")
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700570 self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-generic/unistd.h"))
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100571 glibc_fp.write("#elif defined(__arm__)\n")
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700572 self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-arm/asm/unistd.h"))
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700573 glibc_fp.write("#elif defined(__mips__)\n")
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700574 self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-mips/asm/unistd.h"))
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700575 glibc_fp.write("#elif defined(__i386__)\n")
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700576 self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-x86/asm/unistd_32.h"))
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400577 glibc_fp.write("#elif defined(__x86_64__)\n")
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700578 self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-x86/asm/unistd_64.h"))
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700579 glibc_fp.write("#endif\n")
580
581 glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n")
582 glibc_fp.close()
583 self.other_files.append(glibc_syscalls_h_path)
584
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700585
Elliott Hughesd6121652013-09-25 22:43:36 -0700586 # Write each syscall stub.
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700587 def gen_syscall_stubs(self):
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700588 for syscall in self.syscalls:
Elliott Hughesd6121652013-09-25 22:43:36 -0700589 for arch in all_arches:
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700590 if syscall.has_key("asm-%s" % arch):
591 filename = "arch-%s/syscalls/%s.S" % (arch, syscall["func"])
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700592 logging.info(">>> generating " + filename)
Elliott Hughesd6121652013-09-25 22:43:36 -0700593 fp = create_file(filename)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700594 fp.write(syscall["asm-%s" % arch])
Elliott Hughesd6121652013-09-25 22:43:36 -0700595 fp.close()
596 self.new_stubs.append(filename)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700597
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700598
Elliott Hughesd6121652013-09-25 22:43:36 -0700599 def regenerate(self):
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700600 logging.info("scanning for existing architecture-specific stub files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700601
Elliott Hughesd6121652013-09-25 22:43:36 -0700602 for arch in all_arches:
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700603 arch_dir = "arch-" + arch
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700604 logging.info("scanning " + os.path.join(bionic_libc_root, arch_dir))
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700605 rel_path = os.path.join(arch_dir, "syscalls")
606 for file in os.listdir(os.path.join(bionic_libc_root, rel_path)):
607 if file.endswith(".S"):
608 self.old_stubs.append(os.path.join(rel_path, file))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700609
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700610 logging.info("found %d stub files" % len(self.old_stubs))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700611
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400612 if not os.path.exists(bionic_temp):
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700613 logging.info("creating %s..." % bionic_temp)
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400614 make_dir(bionic_temp)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700615
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700616 logging.info("re-generating stubs and support files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700617
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700618 self.gen_glibc_syscalls_h()
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700619 self.gen_syscall_stubs()
620
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700621 logging.info("comparing files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700622 adds = []
623 edits = []
624
625 for stub in self.new_stubs + self.other_files:
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700626 tmp_file = os.path.join(bionic_temp, stub)
627 libc_file = os.path.join(bionic_libc_root, stub)
628 if not os.path.exists(libc_file):
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200629 # new file, git add it
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700630 logging.info("new file: " + stub)
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700631 adds.append(libc_file)
632 shutil.copyfile(tmp_file, libc_file)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700633
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700634 elif not filecmp.cmp(tmp_file, libc_file):
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700635 logging.info("changed file: " + stub)
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400636 edits.append(stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700637
638 deletes = []
639 for stub in self.old_stubs:
640 if not stub in self.new_stubs:
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700641 logging.info("deleted file: " + stub)
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700642 deletes.append(os.path.join(bionic_libc_root, stub))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700643
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400644 if not DRY_RUN:
645 if adds:
646 commands.getoutput("git add " + " ".join(adds))
647 if deletes:
648 commands.getoutput("git rm " + " ".join(deletes))
649 if edits:
650 for file in edits:
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700651 shutil.copyfile(os.path.join(bionic_temp, file),
652 os.path.join(bionic_libc_root, file))
653 commands.getoutput("git add " + " ".join((os.path.join(bionic_libc_root, file)) for file in edits))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700654
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700655 commands.getoutput("git add %s" % (os.path.join(bionic_libc_root, "SYSCALLS.TXT")))
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200656
657 if (not adds) and (not deletes) and (not edits):
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700658 logging.info("no changes detected!")
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200659 else:
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700660 logging.info("ready to go!!")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700661
Elliott Hughesdc1fb702014-08-20 11:16:11 -0700662logging.basicConfig(level=logging.INFO)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700663
664state = State()
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700665state.process_file(os.path.join(bionic_libc_root, "SYSCALLS.TXT"))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700666state.regenerate()