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