blob: 42f2c916bc05dcb135f2d3a3bcfe07d0ffe92759 [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
11import os.path
12import re
13import shutil
14import stat
15import sys
Christopher Ferris01bd32e2014-08-05 12:19:27 -070016import tempfile
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070017
18from bionic_utils import *
19
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070020# temp directory where we store all intermediate files
Christopher Ferris01bd32e2014-08-05 12:19:27 -070021bionic_temp = tempfile.mkdtemp(prefix="bionic_gensyscalls");
22# Make sure the directory is deleted when the script exits.
23atexit.register(shutil.rmtree, bionic_temp)
24
25bionic_libc_root = os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070026
Elliott Hughes103ccde2013-10-16 14:27:59 -070027warning = "Generated by gensyscalls.py. Do not edit."
28
Pavel Chupinf12a18b2012-12-12 13:11:48 +040029DRY_RUN = False
30
31def make_dir(path):
Raghu Gandham1fa0d842012-01-27 17:51:42 -080032 path = os.path.abspath(path)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070033 if not os.path.exists(path):
34 parent = os.path.dirname(path)
35 if parent:
36 make_dir(parent)
37 os.mkdir(path)
38
Elliott Hughes0437f3f2013-10-07 23:53:13 -070039
Pavel Chupinf12a18b2012-12-12 13:11:48 +040040def create_file(relpath):
Christopher Ferris01bd32e2014-08-05 12:19:27 -070041 full_path = os.path.join(bionic_temp, relpath)
42 dir = os.path.dirname(full_path)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070043 make_dir(dir)
Christopher Ferris01bd32e2014-08-05 12:19:27 -070044 return open(full_path, "w")
Pavel Chupinf12a18b2012-12-12 13:11:48 +040045
46
Elliott Hughes103ccde2013-10-16 14:27:59 -070047syscall_stub_header = "/* " + warning + " */\n" + \
48"""
Elliott Hughesed744842013-11-07 10:31:05 -080049#include <private/bionic_asm.h>
Pavel Chupinf12a18b2012-12-12 13:11:48 +040050
Elliott Hughes0437f3f2013-10-07 23:53:13 -070051ENTRY(%(func)s)
Pavel Chupinf12a18b2012-12-12 13:11:48 +040052"""
53
Elliott Hughes0437f3f2013-10-07 23:53:13 -070054
H.J. Lu6fe4e872013-10-04 10:03:17 -070055function_alias = """
Elliott Hughes986f9062014-02-18 16:42:36 -080056 .globl %(alias)s
57 .equ %(alias)s, %(func)s
H.J. Lu6fe4e872013-10-04 10:03:17 -070058"""
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070059
Elliott Hughes0437f3f2013-10-07 23:53:13 -070060
61#
62# ARM assembler templates for each syscall stub
63#
64
65arm_eabi_call_default = syscall_stub_header + """\
66 mov ip, r7
67 ldr r7, =%(__NR_name)s
68 swi #0
69 mov r7, ip
70 cmn r0, #(MAX_ERRNO + 1)
71 bxls lr
72 neg r0, r0
73 b __set_errno
74END(%(func)s)
75"""
76
77arm_eabi_call_long = syscall_stub_header + """\
78 mov ip, sp
Elliott Hughes0437f3f2013-10-07 23:53:13 -070079 stmfd sp!, {r4, r5, r6, r7}
Christopher Ferrised459702013-12-02 17:44:53 -080080 .cfi_def_cfa_offset 16
81 .cfi_rel_offset r4, 0
82 .cfi_rel_offset r5, 4
83 .cfi_rel_offset r6, 8
84 .cfi_rel_offset r7, 12
Elliott Hughes0437f3f2013-10-07 23:53:13 -070085 ldmfd ip, {r4, r5, r6}
86 ldr r7, =%(__NR_name)s
87 swi #0
88 ldmfd sp!, {r4, r5, r6, r7}
Christopher Ferrised459702013-12-02 17:44:53 -080089 .cfi_def_cfa_offset 0
Elliott Hughes0437f3f2013-10-07 23:53:13 -070090 cmn r0, #(MAX_ERRNO + 1)
91 bxls lr
92 neg r0, r0
93 b __set_errno
94END(%(func)s)
95"""
96
97
98#
Colin Crossd1973ca2014-01-21 19:50:58 -080099# Arm64 assembler templates for each syscall stub
100#
101
102arm64_call = syscall_stub_header + """\
Colin Crossd1973ca2014-01-21 19:50:58 -0800103 mov x8, %(__NR_name)s
104 svc #0
105
Colin Crossd1973ca2014-01-21 19:50:58 -0800106 cmn x0, #(MAX_ERRNO + 1)
107 cneg x0, x0, hi
108 b.hi __set_errno
109
110 ret
111END(%(func)s)
112"""
113
114
115#
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700116# MIPS assembler templates for each syscall stub
117#
118
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800119mips_call = syscall_stub_header + """\
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700120 .set noreorder
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800121 .cpload t9
122 li v0, %(__NR_name)s
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700123 syscall
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800124 bnez a3, 1f
125 move a0, v0
126 j ra
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700127 nop
1281:
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800129 la t9,__set_errno
130 j t9
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700131 nop
132 .set reorder
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800133END(%(func)s)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700134"""
135
136
Elliott Hughescd6780b2013-02-07 14:07:00 -0800137#
Chris Dearman50432122014-02-05 16:59:23 -0800138# MIPS64 assembler templates for each syscall stub
139#
140
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800141mips64_call = syscall_stub_header + """\
Chris Dearman50432122014-02-05 16:59:23 -0800142 .set push
143 .set noreorder
144 li v0, %(__NR_name)s
145 syscall
146 bnez a3, 1f
147 move a0, v0
148 j ra
149 nop
1501:
151 move t0, ra
152 bal 2f
153 nop
1542:
155 .cpsetup ra, t1, 2b
156 LA t9,__set_errno
157 .cpreturn
158 j t9
159 move ra, t0
160 .set pop
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800161END(%(func)s)
Chris Dearman50432122014-02-05 16:59:23 -0800162"""
163
164
165#
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700166# x86 assembler templates for each syscall stub
167#
168
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800169x86_registers = [ "ebx", "ecx", "edx", "esi", "edi", "ebp" ]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700170
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700171x86_call = """\
172 movl $%(__NR_name)s, %%eax
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700173 int $0x80
Elliott Hughes9aceab52013-03-12 14:57:30 -0700174 cmpl $-MAX_ERRNO, %%eax
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700175 jb 1f
176 negl %%eax
177 pushl %%eax
Dan Albert3726f9c2014-08-08 15:15:29 -0700178 call __set_errno
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700179 addl $4, %%esp
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001801:
181"""
182
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700183x86_return = """\
184 ret
185END(%(func)s)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700186"""
187
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700188
Elliott Hughescd6780b2013-02-07 14:07:00 -0800189#
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400190# x86_64 assembler templates for each syscall stub
191#
192
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700193x86_64_call = """\
194 movl $%(__NR_name)s, %%eax
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400195 syscall
196 cmpq $-MAX_ERRNO, %%rax
197 jb 1f
198 negl %%eax
199 movl %%eax, %%edi
Dan Albert3726f9c2014-08-08 15:15:29 -0700200 call __set_errno
Pavel Chupinf12a18b2012-12-12 13:11:48 +04002011:
202 ret
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700203END(%(func)s)
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400204"""
205
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800206
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100207def param_uses_64bits(param):
208 """Returns True iff a syscall parameter description corresponds
209 to a 64-bit type."""
210 param = param.strip()
211 # First, check that the param type begins with one of the known
212 # 64-bit types.
213 if not ( \
214 param.startswith("int64_t") or param.startswith("uint64_t") or \
215 param.startswith("loff_t") or param.startswith("off64_t") or \
216 param.startswith("long long") or param.startswith("unsigned long long") or
217 param.startswith("signed long long") ):
218 return False
219
220 # Second, check that there is no pointer type here
221 if param.find("*") >= 0:
222 return False
223
224 # Ok
225 return True
226
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700227
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100228def count_arm_param_registers(params):
229 """This function is used to count the number of register used
Elliott Hughescd6780b2013-02-07 14:07:00 -0800230 to pass parameters when invoking an ARM system call.
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100231 This is because the ARM EABI mandates that 64-bit quantities
232 must be passed in an even+odd register pair. So, for example,
233 something like:
234
235 foo(int fd, off64_t pos)
236
237 would actually need 4 registers:
238 r0 -> int
239 r1 -> unused
240 r2-r3 -> pos
241 """
242 count = 0
243 for param in params:
244 if param_uses_64bits(param):
245 if (count & 1) != 0:
246 count += 1
247 count += 2
248 else:
249 count += 1
250 return count
251
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700252
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100253def count_generic_param_registers(params):
254 count = 0
255 for param in params:
256 if param_uses_64bits(param):
257 count += 2
258 else:
259 count += 1
260 return count
261
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700262
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400263def count_generic_param_registers64(params):
264 count = 0
265 for param in params:
266 count += 1
267 return count
268
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700269
Elliott Hughescda62092013-03-22 13:50:44 -0700270# This lets us support regular system calls like __NR_write and also weird
271# ones like __ARM_NR_cacheflush, where the NR doesn't come at the start.
272def make__NR_name(name):
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700273 if name.startswith("__ARM_NR_"):
Elliott Hughescda62092013-03-22 13:50:44 -0700274 return name
275 else:
276 return "__NR_%s" % (name)
277
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700278
Elliott Hughesfff6e272013-10-24 17:03:20 -0700279def add_footer(pointer_length, stub, syscall):
280 # Add any aliases for this syscall.
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700281 aliases = syscall["aliases"]
282 for alias in aliases:
283 stub += function_alias % { "func" : syscall["func"], "alias" : alias }
Elliott Hughesfff6e272013-10-24 17:03:20 -0700284
285 # Use hidden visibility for any functions beginning with underscores.
Elliott Hughesfff6e272013-10-24 17:03:20 -0700286 if pointer_length == 64 and syscall["func"].startswith("__"):
Elliott Hughesd465eb42014-02-19 18:59:19 -0800287 stub += '.hidden ' + syscall["func"] + '\n'
Elliott Hughesfff6e272013-10-24 17:03:20 -0700288
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700289 return stub
290
291
292def arm_eabi_genstub(syscall):
293 num_regs = count_arm_param_registers(syscall["params"])
294 if num_regs > 4:
295 return arm_eabi_call_long % syscall
296 return arm_eabi_call_default % syscall
297
298
Colin Crossd1973ca2014-01-21 19:50:58 -0800299def arm64_genstub(syscall):
300 return arm64_call % syscall
301
302
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700303def mips_genstub(syscall):
304 return mips_call % syscall
305
306
Chris Dearman50432122014-02-05 16:59:23 -0800307def mips64_genstub(syscall):
308 return mips64_call % syscall
309
310
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700311def x86_genstub(syscall):
312 result = syscall_stub_header % syscall
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700313
314 numparams = count_generic_param_registers(syscall["params"])
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800315 stack_bias = numparams*4 + 4
316 offset = 0
317 mov_result = ""
Christopher Ferris15b91e92014-05-29 18:17:09 -0700318 first_push = True
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800319 for register in x86_registers[:numparams]:
320 result += " pushl %%%s\n" % register
Christopher Ferris15b91e92014-05-29 18:17:09 -0700321 if first_push:
322 result += " .cfi_def_cfa_offset 8\n"
323 result += " .cfi_rel_offset %s, 0\n" % register
324 first_push = False
325 else:
326 result += " .cfi_adjust_cfa_offset 4\n"
327 result += " .cfi_rel_offset %s, 0\n" % register
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800328 mov_result += " mov %d(%%esp), %%%s\n" % (stack_bias+offset, register)
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800329 offset += 4
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700330
Christopher Ferris15b91e92014-05-29 18:17:09 -0700331 result += mov_result
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700332 result += x86_call % syscall
333
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800334 for register in reversed(x86_registers[:numparams]):
335 result += " popl %%%s\n" % register
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700336
337 result += x86_return % syscall
338 return result
339
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100340
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700341def x86_genstub_socketcall(syscall):
342 # %ebx <--- Argument 1 - The call id of the needed vectored
343 # syscall (socket, bind, recv, etc)
344 # %ecx <--- Argument 2 - Pointer to the rest of the arguments
345 # from the original function called (socket())
346
347 result = syscall_stub_header % syscall
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700348
349 # save the regs we need
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800350 result += " pushl %ebx\n"
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800351 result += " .cfi_def_cfa_offset 8\n"
352 result += " .cfi_rel_offset ebx, 0\n"
Christopher Ferris15b91e92014-05-29 18:17:09 -0700353 result += " pushl %ecx\n"
354 result += " .cfi_adjust_cfa_offset 4\n"
355 result += " .cfi_rel_offset ecx, 0\n"
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800356 stack_bias = 12
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700357
358 # set the call id (%ebx)
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800359 result += " mov $%d, %%ebx\n" % syscall["socketcall_id"]
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700360
361 # set the pointer to the rest of the args into %ecx
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800362 result += " mov %esp, %ecx\n"
363 result += " addl $%d, %%ecx\n" % (stack_bias)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700364
365 # now do the syscall code itself
366 result += x86_call % syscall
367
368 # now restore the saved regs
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800369 result += " popl %ecx\n"
370 result += " popl %ebx\n"
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700371
372 # epilog
373 result += x86_return % syscall
374 return result
375
376
377def x86_64_genstub(syscall):
378 result = syscall_stub_header % syscall
379 num_regs = count_generic_param_registers64(syscall["params"])
380 if (num_regs > 3):
381 # rcx is used as 4th argument. Kernel wants it at r10.
382 result += " movq %rcx, %r10\n"
383
384 result += x86_64_call % syscall
385 return result
386
387
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700388class State:
389 def __init__(self):
390 self.old_stubs = []
391 self.new_stubs = []
392 self.other_files = []
393 self.syscalls = []
394
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400395
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700396 def process_file(self, input):
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700397 parser = SysCallsTxtParser()
398 parser.parse_file(input)
399 self.syscalls = parser.syscalls
400 parser = None
401
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700402 for syscall in self.syscalls:
403 syscall["__NR_name"] = make__NR_name(syscall["name"])
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700404
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700405 if syscall.has_key("arm"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700406 syscall["asm-arm"] = add_footer(32, arm_eabi_genstub(syscall), syscall)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700407
Colin Crossd1973ca2014-01-21 19:50:58 -0800408 if syscall.has_key("arm64"):
409 syscall["asm-arm64"] = add_footer(64, arm64_genstub(syscall), syscall)
410
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700411 if syscall.has_key("x86"):
412 if syscall["socketcall_id"] >= 0:
Elliott Hughesfff6e272013-10-24 17:03:20 -0700413 syscall["asm-x86"] = add_footer(32, x86_genstub_socketcall(syscall), syscall)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800414 else:
Elliott Hughesfff6e272013-10-24 17:03:20 -0700415 syscall["asm-x86"] = add_footer(32, x86_genstub(syscall), syscall)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700416 elif syscall["socketcall_id"] >= 0:
Elliott Hughesd6121652013-09-25 22:43:36 -0700417 E("socketcall_id for dispatch syscalls is only supported for x86 in '%s'" % t)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800418 return
Elliott Hughescd6780b2013-02-07 14:07:00 -0800419
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700420 if syscall.has_key("mips"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700421 syscall["asm-mips"] = add_footer(32, mips_genstub(syscall), syscall)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800422
Chris Dearman50432122014-02-05 16:59:23 -0800423 if syscall.has_key("mips64"):
424 syscall["asm-mips64"] = add_footer(64, mips64_genstub(syscall), syscall)
425
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700426 if syscall.has_key("x86_64"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700427 syscall["asm-x86_64"] = add_footer(64, x86_64_genstub(syscall), syscall)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700428
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700429 # Scan a Linux kernel asm/unistd.h file containing __NR_* constants
430 # and write out equivalent SYS_* constants for glibc source compatibility.
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700431 def scan_linux_unistd_h(self, fp, path):
432 pattern = re.compile(r'^#define __NR_([a-z]\S+) .*')
433 syscalls = set() # MIPS defines everything three times; work around that.
434 for line in open(path):
435 m = re.search(pattern, line)
436 if m:
437 syscalls.add(m.group(1))
438 for syscall in sorted(syscalls):
Elliott Hughescda62092013-03-22 13:50:44 -0700439 fp.write("#define SYS_%s %s\n" % (syscall, make__NR_name(syscall)))
Elliott Hughes8ecf2252013-03-21 18:06:55 -0700440
441
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700442 def gen_glibc_syscalls_h(self):
Elliott Hughescda62092013-03-22 13:50:44 -0700443 # TODO: generate a separate file for each architecture, like glibc's bits/syscall.h.
Elliott Hughes9724ce32013-03-21 19:43:54 -0700444 glibc_syscalls_h_path = "include/sys/glibc-syscalls.h"
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700445 D("generating " + glibc_syscalls_h_path)
Elliott Hughes9724ce32013-03-21 19:43:54 -0700446 glibc_fp = create_file(glibc_syscalls_h_path)
Elliott Hughes103ccde2013-10-16 14:27:59 -0700447 glibc_fp.write("/* %s */\n" % warning)
Elliott Hughes9724ce32013-03-21 19:43:54 -0700448 glibc_fp.write("#ifndef _BIONIC_GLIBC_SYSCALLS_H_\n")
449 glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n")
450
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100451 glibc_fp.write("#if defined(__aarch64__)\n")
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700452 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 +0100453 glibc_fp.write("#elif defined(__arm__)\n")
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700454 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 -0700455 glibc_fp.write("#elif defined(__mips__)\n")
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700456 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 -0700457 glibc_fp.write("#elif defined(__i386__)\n")
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700458 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 +0400459 glibc_fp.write("#elif defined(__x86_64__)\n")
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700460 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 -0700461 glibc_fp.write("#endif\n")
462
463 glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n")
464 glibc_fp.close()
465 self.other_files.append(glibc_syscalls_h_path)
466
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700467
Elliott Hughesd6121652013-09-25 22:43:36 -0700468 # Write each syscall stub.
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700469 def gen_syscall_stubs(self):
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700470 for syscall in self.syscalls:
Elliott Hughesd6121652013-09-25 22:43:36 -0700471 for arch in all_arches:
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700472 if syscall.has_key("asm-%s" % arch):
473 filename = "arch-%s/syscalls/%s.S" % (arch, syscall["func"])
Elliott Hughesd6121652013-09-25 22:43:36 -0700474 D2(">>> generating " + filename)
475 fp = create_file(filename)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700476 fp.write(syscall["asm-%s" % arch])
Elliott Hughesd6121652013-09-25 22:43:36 -0700477 fp.close()
478 self.new_stubs.append(filename)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700479
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700480
Elliott Hughesd6121652013-09-25 22:43:36 -0700481 def regenerate(self):
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400482 D("scanning for existing architecture-specific stub files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700483
Elliott Hughesd6121652013-09-25 22:43:36 -0700484 for arch in all_arches:
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700485 arch_dir = "arch-" + arch
486 D("scanning " + os.path.join(bionic_libc_root, arch_dir))
487 rel_path = os.path.join(arch_dir, "syscalls")
488 for file in os.listdir(os.path.join(bionic_libc_root, rel_path)):
489 if file.endswith(".S"):
490 self.old_stubs.append(os.path.join(rel_path, file))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700491
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400492 D("found %d stub files" % len(self.old_stubs))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700493
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400494 if not os.path.exists(bionic_temp):
495 D("creating %s..." % bionic_temp)
496 make_dir(bionic_temp)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700497
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400498 D("re-generating stubs and support files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700499
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700500 self.gen_glibc_syscalls_h()
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700501 self.gen_syscall_stubs()
502
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400503 D("comparing files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700504 adds = []
505 edits = []
506
507 for stub in self.new_stubs + self.other_files:
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700508 tmp_file = os.path.join(bionic_temp, stub)
509 libc_file = os.path.join(bionic_libc_root, stub)
510 if not os.path.exists(libc_file):
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200511 # new file, git add it
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400512 D("new file: " + stub)
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700513 adds.append(libc_file)
514 shutil.copyfile(tmp_file, libc_file)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700515
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700516 elif not filecmp.cmp(tmp_file, libc_file):
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400517 D("changed file: " + stub)
518 edits.append(stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700519
520 deletes = []
521 for stub in self.old_stubs:
522 if not stub in self.new_stubs:
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400523 D("deleted file: " + stub)
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700524 deletes.append(os.path.join(bionic_libc_root, stub))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700525
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400526 if not DRY_RUN:
527 if adds:
528 commands.getoutput("git add " + " ".join(adds))
529 if deletes:
530 commands.getoutput("git rm " + " ".join(deletes))
531 if edits:
532 for file in edits:
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700533 shutil.copyfile(os.path.join(bionic_temp, file),
534 os.path.join(bionic_libc_root, file))
535 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 -0700536
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700537 commands.getoutput("git add %s" % (os.path.join(bionic_libc_root, "SYSCALLS.TXT")))
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200538
539 if (not adds) and (not deletes) and (not edits):
540 D("no changes detected!")
541 else:
542 D("ready to go!!")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700543
544D_setlevel(1)
545
546state = State()
Christopher Ferris01bd32e2014-08-05 12:19:27 -0700547state.process_file(os.path.join(bionic_libc_root, "SYSCALLS.TXT"))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700548state.regenerate()