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