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