The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 2 | |
| 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 Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 6 | |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 7 | import commands |
| 8 | import filecmp |
| 9 | import glob |
| 10 | import os.path |
| 11 | import re |
| 12 | import shutil |
| 13 | import stat |
| 14 | import sys |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 15 | |
| 16 | from bionic_utils import * |
| 17 | |
Elliott Hughes | 18bc975 | 2013-06-17 10:26:10 -0700 | [diff] [blame] | 18 | bionic_libc_root = os.environ["ANDROID_BUILD_TOP"] + "/bionic/libc/" |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | |
| 20 | # temp directory where we store all intermediate files |
| 21 | bionic_temp = "/tmp/bionic_gensyscalls/" |
| 22 | |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 23 | warning = "Generated by gensyscalls.py. Do not edit." |
| 24 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 25 | DRY_RUN = False |
| 26 | |
| 27 | def make_dir(path): |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 28 | path = os.path.abspath(path) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | if not os.path.exists(path): |
| 30 | parent = os.path.dirname(path) |
| 31 | if parent: |
| 32 | make_dir(parent) |
| 33 | os.mkdir(path) |
| 34 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 35 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 36 | def create_file(relpath): |
| 37 | dir = os.path.dirname(bionic_temp + relpath) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | make_dir(dir) |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 39 | return open(bionic_temp + relpath, "w") |
| 40 | |
| 41 | |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 42 | syscall_stub_header = "/* " + warning + " */\n" + \ |
| 43 | """ |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 44 | #include <asm/unistd.h> |
| 45 | #include <linux/err.h> |
| 46 | #include <machine/asm.h> |
| 47 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 48 | ENTRY(%(func)s) |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 49 | """ |
| 50 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 51 | |
H.J. Lu | 6fe4e87 | 2013-10-04 10:03:17 -0700 | [diff] [blame] | 52 | function_alias = """ |
| 53 | .globl _C_LABEL(%(alias)s) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 54 | .equ _C_LABEL(%(alias)s), _C_LABEL(%(func)s) |
H.J. Lu | 6fe4e87 | 2013-10-04 10:03:17 -0700 | [diff] [blame] | 55 | """ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 57 | |
| 58 | # |
| 59 | # ARM assembler templates for each syscall stub |
| 60 | # |
| 61 | |
| 62 | arm_eabi_call_default = syscall_stub_header + """\ |
| 63 | mov ip, r7 |
| 64 | ldr r7, =%(__NR_name)s |
| 65 | swi #0 |
| 66 | mov r7, ip |
| 67 | cmn r0, #(MAX_ERRNO + 1) |
| 68 | bxls lr |
| 69 | neg r0, r0 |
| 70 | b __set_errno |
| 71 | END(%(func)s) |
| 72 | """ |
| 73 | |
| 74 | arm_eabi_call_long = syscall_stub_header + """\ |
| 75 | mov ip, sp |
| 76 | .save {r4, r5, r6, r7} |
| 77 | stmfd sp!, {r4, r5, r6, r7} |
| 78 | ldmfd ip, {r4, r5, r6} |
| 79 | ldr r7, =%(__NR_name)s |
| 80 | swi #0 |
| 81 | ldmfd sp!, {r4, r5, r6, r7} |
| 82 | cmn r0, #(MAX_ERRNO + 1) |
| 83 | bxls lr |
| 84 | neg r0, r0 |
| 85 | b __set_errno |
| 86 | END(%(func)s) |
| 87 | """ |
| 88 | |
| 89 | |
| 90 | # |
| 91 | # MIPS assembler templates for each syscall stub |
| 92 | # |
| 93 | |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 94 | mips_call = "/* " + warning + " */\n" + \ |
| 95 | """ |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 96 | #include <asm/unistd.h> |
| 97 | .text |
| 98 | .globl %(func)s |
| 99 | .align 4 |
| 100 | .ent %(func)s |
| 101 | |
| 102 | %(func)s: |
| 103 | .set noreorder |
| 104 | .cpload $t9 |
| 105 | li $v0, %(__NR_name)s |
| 106 | syscall |
| 107 | bnez $a3, 1f |
| 108 | move $a0, $v0 |
| 109 | j $ra |
| 110 | nop |
| 111 | 1: |
| 112 | la $t9,__set_errno |
| 113 | j $t9 |
| 114 | nop |
| 115 | .set reorder |
| 116 | .end %(func)s |
| 117 | """ |
| 118 | |
| 119 | |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 120 | # |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 121 | # x86 assembler templates for each syscall stub |
| 122 | # |
| 123 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | x86_registers = [ "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp" ] |
| 125 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 126 | x86_call = """\ |
| 127 | movl $%(__NR_name)s, %%eax |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 128 | int $0x80 |
Elliott Hughes | 9aceab5 | 2013-03-12 14:57:30 -0700 | [diff] [blame] | 129 | cmpl $-MAX_ERRNO, %%eax |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 130 | jb 1f |
| 131 | negl %%eax |
| 132 | pushl %%eax |
| 133 | call __set_errno |
| 134 | addl $4, %%esp |
| 135 | orl $-1, %%eax |
| 136 | 1: |
| 137 | """ |
| 138 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 139 | x86_return = """\ |
| 140 | ret |
| 141 | END(%(func)s) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 142 | """ |
| 143 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 144 | |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 145 | # |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 146 | # x86_64 assembler templates for each syscall stub |
| 147 | # |
| 148 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 149 | x86_64_call = """\ |
| 150 | movl $%(__NR_name)s, %%eax |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 151 | syscall |
| 152 | cmpq $-MAX_ERRNO, %%rax |
| 153 | jb 1f |
| 154 | negl %%eax |
| 155 | movl %%eax, %%edi |
| 156 | call __set_errno |
| 157 | orq $-1, %%rax |
| 158 | 1: |
| 159 | ret |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 160 | END(%(func)s) |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 161 | """ |
| 162 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 163 | |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 164 | def param_uses_64bits(param): |
| 165 | """Returns True iff a syscall parameter description corresponds |
| 166 | to a 64-bit type.""" |
| 167 | param = param.strip() |
| 168 | # First, check that the param type begins with one of the known |
| 169 | # 64-bit types. |
| 170 | if not ( \ |
| 171 | param.startswith("int64_t") or param.startswith("uint64_t") or \ |
| 172 | param.startswith("loff_t") or param.startswith("off64_t") or \ |
| 173 | param.startswith("long long") or param.startswith("unsigned long long") or |
| 174 | param.startswith("signed long long") ): |
| 175 | return False |
| 176 | |
| 177 | # Second, check that there is no pointer type here |
| 178 | if param.find("*") >= 0: |
| 179 | return False |
| 180 | |
| 181 | # Ok |
| 182 | return True |
| 183 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 184 | |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 185 | def count_arm_param_registers(params): |
| 186 | """This function is used to count the number of register used |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 187 | to pass parameters when invoking an ARM system call. |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 188 | This is because the ARM EABI mandates that 64-bit quantities |
| 189 | must be passed in an even+odd register pair. So, for example, |
| 190 | something like: |
| 191 | |
| 192 | foo(int fd, off64_t pos) |
| 193 | |
| 194 | would actually need 4 registers: |
| 195 | r0 -> int |
| 196 | r1 -> unused |
| 197 | r2-r3 -> pos |
| 198 | """ |
| 199 | count = 0 |
| 200 | for param in params: |
| 201 | if param_uses_64bits(param): |
| 202 | if (count & 1) != 0: |
| 203 | count += 1 |
| 204 | count += 2 |
| 205 | else: |
| 206 | count += 1 |
| 207 | return count |
| 208 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 209 | |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 210 | def count_generic_param_registers(params): |
| 211 | count = 0 |
| 212 | for param in params: |
| 213 | if param_uses_64bits(param): |
| 214 | count += 2 |
| 215 | else: |
| 216 | count += 1 |
| 217 | return count |
| 218 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 219 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 220 | def count_generic_param_registers64(params): |
| 221 | count = 0 |
| 222 | for param in params: |
| 223 | count += 1 |
| 224 | return count |
| 225 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 226 | |
Elliott Hughes | cda6209 | 2013-03-22 13:50:44 -0700 | [diff] [blame] | 227 | # This lets us support regular system calls like __NR_write and also weird |
| 228 | # ones like __ARM_NR_cacheflush, where the NR doesn't come at the start. |
| 229 | def make__NR_name(name): |
| 230 | if name.startswith("__"): |
| 231 | return name |
| 232 | else: |
| 233 | return "__NR_%s" % (name) |
| 234 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 235 | |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame^] | 236 | def add_footer(pointer_length, stub, syscall): |
| 237 | # Add any aliases for this syscall. |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 238 | aliases = syscall["aliases"] |
| 239 | for alias in aliases: |
| 240 | stub += function_alias % { "func" : syscall["func"], "alias" : alias } |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame^] | 241 | |
| 242 | # Use hidden visibility for any functions beginning with underscores. |
| 243 | # TODO: clean up single-underscore names too. |
| 244 | if pointer_length == 64 and syscall["func"].startswith("__"): |
| 245 | stub += '.hidden _C_LABEL(' + syscall["func"] + ')\n' |
| 246 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 247 | return stub |
| 248 | |
| 249 | |
| 250 | def arm_eabi_genstub(syscall): |
| 251 | num_regs = count_arm_param_registers(syscall["params"]) |
| 252 | if num_regs > 4: |
| 253 | return arm_eabi_call_long % syscall |
| 254 | return arm_eabi_call_default % syscall |
| 255 | |
| 256 | |
| 257 | def mips_genstub(syscall): |
| 258 | return mips_call % syscall |
| 259 | |
| 260 | |
| 261 | def x86_genstub(syscall): |
| 262 | result = syscall_stub_header % syscall |
| 263 | stack_bias = 4 |
| 264 | |
| 265 | numparams = count_generic_param_registers(syscall["params"]) |
| 266 | for r in range(numparams): |
| 267 | result += " pushl " + x86_registers[r] + "\n" |
| 268 | stack_bias += 4 |
| 269 | |
| 270 | for r in range(numparams): |
| 271 | result += " mov %d(%%esp), %s" % (stack_bias+r*4, x86_registers[r]) + "\n" |
| 272 | |
| 273 | result += x86_call % syscall |
| 274 | |
| 275 | for r in range(numparams): |
| 276 | result += " popl " + x86_registers[numparams-r-1] + "\n" |
| 277 | |
| 278 | result += x86_return % syscall |
| 279 | return result |
| 280 | |
| 281 | def x86_genstub_socketcall(syscall): |
| 282 | # %ebx <--- Argument 1 - The call id of the needed vectored |
| 283 | # syscall (socket, bind, recv, etc) |
| 284 | # %ecx <--- Argument 2 - Pointer to the rest of the arguments |
| 285 | # from the original function called (socket()) |
| 286 | |
| 287 | result = syscall_stub_header % syscall |
| 288 | stack_bias = 4 |
| 289 | |
| 290 | # save the regs we need |
| 291 | result += " pushl %ebx" + "\n" |
| 292 | stack_bias += 4 |
| 293 | result += " pushl %ecx" + "\n" |
| 294 | stack_bias += 4 |
| 295 | |
| 296 | # set the call id (%ebx) |
| 297 | result += " mov $%d, %%ebx" % syscall["socketcall_id"] + "\n" |
| 298 | |
| 299 | # set the pointer to the rest of the args into %ecx |
| 300 | result += " mov %esp, %ecx" + "\n" |
| 301 | result += " addl $%d, %%ecx" % (stack_bias) + "\n" |
| 302 | |
| 303 | # now do the syscall code itself |
| 304 | result += x86_call % syscall |
| 305 | |
| 306 | # now restore the saved regs |
| 307 | result += " popl %ecx" + "\n" |
| 308 | result += " popl %ebx" + "\n" |
| 309 | |
| 310 | # epilog |
| 311 | result += x86_return % syscall |
| 312 | return result |
| 313 | |
| 314 | |
| 315 | def x86_64_genstub(syscall): |
| 316 | result = syscall_stub_header % syscall |
| 317 | num_regs = count_generic_param_registers64(syscall["params"]) |
| 318 | if (num_regs > 3): |
| 319 | # rcx is used as 4th argument. Kernel wants it at r10. |
| 320 | result += " movq %rcx, %r10\n" |
| 321 | |
| 322 | result += x86_64_call % syscall |
| 323 | return result |
| 324 | |
| 325 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 326 | class State: |
| 327 | def __init__(self): |
| 328 | self.old_stubs = [] |
| 329 | self.new_stubs = [] |
| 330 | self.other_files = [] |
| 331 | self.syscalls = [] |
| 332 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 333 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 334 | def process_file(self, input): |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 335 | parser = SysCallsTxtParser() |
| 336 | parser.parse_file(input) |
| 337 | self.syscalls = parser.syscalls |
| 338 | parser = None |
| 339 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 340 | for syscall in self.syscalls: |
| 341 | syscall["__NR_name"] = make__NR_name(syscall["name"]) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 342 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 343 | if syscall.has_key("arm"): |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame^] | 344 | syscall["asm-arm"] = add_footer(32, arm_eabi_genstub(syscall), syscall) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 345 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 346 | if syscall.has_key("x86"): |
| 347 | if syscall["socketcall_id"] >= 0: |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame^] | 348 | syscall["asm-x86"] = add_footer(32, x86_genstub_socketcall(syscall), syscall) |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 349 | else: |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame^] | 350 | syscall["asm-x86"] = add_footer(32, x86_genstub(syscall), syscall) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 351 | elif syscall["socketcall_id"] >= 0: |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 352 | E("socketcall_id for dispatch syscalls is only supported for x86 in '%s'" % t) |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 353 | return |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 354 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 355 | if syscall.has_key("mips"): |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame^] | 356 | syscall["asm-mips"] = add_footer(32, mips_genstub(syscall), syscall) |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 357 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 358 | if syscall.has_key("x86_64"): |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame^] | 359 | syscall["asm-x86_64"] = add_footer(64, x86_64_genstub(syscall), syscall) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 360 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 361 | |
Elliott Hughes | 1b91c6c | 2013-03-22 18:56:24 -0700 | [diff] [blame] | 362 | # Scan a Linux kernel asm/unistd.h file containing __NR_* constants |
| 363 | # and write out equivalent SYS_* constants for glibc source compatibility. |
Elliott Hughes | 5c2772f | 2013-03-21 22:15:06 -0700 | [diff] [blame] | 364 | def scan_linux_unistd_h(self, fp, path): |
| 365 | pattern = re.compile(r'^#define __NR_([a-z]\S+) .*') |
| 366 | syscalls = set() # MIPS defines everything three times; work around that. |
| 367 | for line in open(path): |
| 368 | m = re.search(pattern, line) |
| 369 | if m: |
| 370 | syscalls.add(m.group(1)) |
| 371 | for syscall in sorted(syscalls): |
Elliott Hughes | cda6209 | 2013-03-22 13:50:44 -0700 | [diff] [blame] | 372 | fp.write("#define SYS_%s %s\n" % (syscall, make__NR_name(syscall))) |
Elliott Hughes | 8ecf225 | 2013-03-21 18:06:55 -0700 | [diff] [blame] | 373 | |
| 374 | |
Elliott Hughes | 1b91c6c | 2013-03-22 18:56:24 -0700 | [diff] [blame] | 375 | def gen_glibc_syscalls_h(self): |
Elliott Hughes | cda6209 | 2013-03-22 13:50:44 -0700 | [diff] [blame] | 376 | # TODO: generate a separate file for each architecture, like glibc's bits/syscall.h. |
Elliott Hughes | 9724ce3 | 2013-03-21 19:43:54 -0700 | [diff] [blame] | 377 | glibc_syscalls_h_path = "include/sys/glibc-syscalls.h" |
Elliott Hughes | 1b91c6c | 2013-03-22 18:56:24 -0700 | [diff] [blame] | 378 | D("generating " + glibc_syscalls_h_path) |
Elliott Hughes | 9724ce3 | 2013-03-21 19:43:54 -0700 | [diff] [blame] | 379 | glibc_fp = create_file(glibc_syscalls_h_path) |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 380 | glibc_fp.write("/* %s */\n" % warning) |
Elliott Hughes | 9724ce3 | 2013-03-21 19:43:54 -0700 | [diff] [blame] | 381 | glibc_fp.write("#ifndef _BIONIC_GLIBC_SYSCALLS_H_\n") |
| 382 | glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n") |
| 383 | |
Elliott Hughes | 5c2772f | 2013-03-21 22:15:06 -0700 | [diff] [blame] | 384 | glibc_fp.write("#if defined(__arm__)\n") |
Elliott Hughes | 18bc975 | 2013-06-17 10:26:10 -0700 | [diff] [blame] | 385 | self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/arch-arm/asm/unistd.h") |
Elliott Hughes | 5c2772f | 2013-03-21 22:15:06 -0700 | [diff] [blame] | 386 | glibc_fp.write("#elif defined(__mips__)\n") |
Elliott Hughes | 18bc975 | 2013-06-17 10:26:10 -0700 | [diff] [blame] | 387 | self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/arch-mips/asm/unistd.h") |
Elliott Hughes | 5c2772f | 2013-03-21 22:15:06 -0700 | [diff] [blame] | 388 | glibc_fp.write("#elif defined(__i386__)\n") |
Elliott Hughes | 18bc975 | 2013-06-17 10:26:10 -0700 | [diff] [blame] | 389 | self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/arch-x86/asm/unistd_32.h") |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 390 | glibc_fp.write("#elif defined(__x86_64__)\n") |
| 391 | self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/arch-x86/asm/unistd_64.h") |
Elliott Hughes | 5c2772f | 2013-03-21 22:15:06 -0700 | [diff] [blame] | 392 | glibc_fp.write("#endif\n") |
| 393 | |
| 394 | glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n") |
| 395 | glibc_fp.close() |
| 396 | self.other_files.append(glibc_syscalls_h_path) |
| 397 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 398 | |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 399 | # Write the contents of syscalls.mk. |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 400 | def gen_arch_syscalls_mk(self, arch): |
| 401 | path = "arch-%s/syscalls.mk" % arch |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 402 | D("generating " + path) |
| 403 | fp = create_file(path) |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 404 | fp.write("# %s\n" % warning) |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 405 | fp.write("syscall_src :=\n") |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 406 | for syscall in sorted(self.syscalls, key=lambda syscall: syscall["func"]): |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 407 | if syscall.has_key("asm-%s" % arch): |
| 408 | fp.write("syscall_src += arch-%s/syscalls/%s.S\n" % (arch, syscall["func"])) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 409 | fp.close() |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 410 | self.other_files.append(path) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 411 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 412 | |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 413 | # Write each syscall stub. |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 414 | def gen_syscall_stubs(self): |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 415 | for syscall in self.syscalls: |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 416 | for arch in all_arches: |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 417 | if syscall.has_key("asm-%s" % arch): |
| 418 | filename = "arch-%s/syscalls/%s.S" % (arch, syscall["func"]) |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 419 | D2(">>> generating " + filename) |
| 420 | fp = create_file(filename) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 421 | fp.write(syscall["asm-%s" % arch]) |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 422 | fp.close() |
| 423 | self.new_stubs.append(filename) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 424 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 425 | |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 426 | def regenerate(self): |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 427 | D("scanning for existing architecture-specific stub files...") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 428 | |
Elliott Hughes | 18bc975 | 2013-06-17 10:26:10 -0700 | [diff] [blame] | 429 | bionic_libc_root_len = len(bionic_libc_root) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 430 | |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 431 | for arch in all_arches: |
Elliott Hughes | 18bc975 | 2013-06-17 10:26:10 -0700 | [diff] [blame] | 432 | arch_path = bionic_libc_root + "arch-" + arch |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 433 | D("scanning " + arch_path) |
| 434 | files = glob.glob(arch_path + "/syscalls/*.S") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 435 | for f in files: |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 436 | self.old_stubs.append(f[bionic_libc_root_len:]) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 437 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 438 | D("found %d stub files" % len(self.old_stubs)) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 439 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 440 | if not os.path.exists(bionic_temp): |
| 441 | D("creating %s..." % bionic_temp) |
| 442 | make_dir(bionic_temp) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 443 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 444 | D("re-generating stubs and support files...") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 445 | |
Elliott Hughes | 1b91c6c | 2013-03-22 18:56:24 -0700 | [diff] [blame] | 446 | self.gen_glibc_syscalls_h() |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 447 | for arch in all_arches: |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 448 | self.gen_arch_syscalls_mk(arch) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 449 | self.gen_syscall_stubs() |
| 450 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 451 | D("comparing files...") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 452 | adds = [] |
| 453 | edits = [] |
| 454 | |
| 455 | for stub in self.new_stubs + self.other_files: |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 456 | if not os.path.exists(bionic_libc_root + stub): |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 457 | # new file, git add it |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 458 | D("new file: " + stub) |
| 459 | adds.append(bionic_libc_root + stub) |
| 460 | shutil.copyfile(bionic_temp + stub, bionic_libc_root + stub) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 461 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 462 | elif not filecmp.cmp(bionic_temp + stub, bionic_libc_root + stub): |
| 463 | D("changed file: " + stub) |
| 464 | edits.append(stub) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 465 | |
| 466 | deletes = [] |
| 467 | for stub in self.old_stubs: |
| 468 | if not stub in self.new_stubs: |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 469 | D("deleted file: " + stub) |
| 470 | deletes.append(bionic_libc_root + stub) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 471 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 472 | if not DRY_RUN: |
| 473 | if adds: |
| 474 | commands.getoutput("git add " + " ".join(adds)) |
| 475 | if deletes: |
| 476 | commands.getoutput("git rm " + " ".join(deletes)) |
| 477 | if edits: |
| 478 | for file in edits: |
| 479 | shutil.copyfile(bionic_temp + file, bionic_libc_root + file) |
| 480 | commands.getoutput("git add " + " ".join((bionic_libc_root + file) for file in edits)) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 481 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 482 | commands.getoutput("git add %s%s" % (bionic_libc_root,"SYSCALLS.TXT")) |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 483 | |
| 484 | if (not adds) and (not deletes) and (not edits): |
| 485 | D("no changes detected!") |
| 486 | else: |
| 487 | D("ready to go!!") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 488 | |
| 489 | D_setlevel(1) |
| 490 | |
| 491 | state = State() |
Elliott Hughes | 18bc975 | 2013-06-17 10:26:10 -0700 | [diff] [blame] | 492 | state.process_file(bionic_libc_root+"SYSCALLS.TXT") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 493 | state.regenerate() |