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