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