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