Stephen Crane | 77bb564 | 2017-08-31 15:08:26 -0700 | [diff] [blame] | 1 | #!/usr/bin/env 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 |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 11 | import logging |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 12 | import os.path |
| 13 | import re |
| 14 | import shutil |
| 15 | import stat |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 16 | import string |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 17 | import sys |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 18 | import tempfile |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 20 | |
| 21 | all_arches = [ "arm", "arm64", "mips", "mips64", "x86", "x86_64" ] |
| 22 | |
Elliott Hughes | d67b037 | 2019-04-15 14:18:26 -0700 | [diff] [blame^] | 23 | bionic_libc = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | d67b037 | 2019-04-15 14:18:26 -0700 | [diff] [blame^] | 25 | syscall_stub_header = \ |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 26 | """ |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 27 | ENTRY(%(func)s) |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 28 | """ |
| 29 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 31 | # |
| 32 | # ARM assembler templates for each syscall stub |
| 33 | # |
| 34 | |
| 35 | arm_eabi_call_default = syscall_stub_header + """\ |
| 36 | mov ip, r7 |
Christopher Ferris | f5a9123 | 2016-04-27 18:31:02 -0700 | [diff] [blame] | 37 | .cfi_register r7, ip |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 38 | ldr r7, =%(__NR_name)s |
| 39 | swi #0 |
| 40 | mov r7, ip |
Christopher Ferris | f5a9123 | 2016-04-27 18:31:02 -0700 | [diff] [blame] | 41 | .cfi_restore r7 |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 42 | cmn r0, #(MAX_ERRNO + 1) |
| 43 | bxls lr |
| 44 | neg r0, r0 |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 45 | b __set_errno_internal |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 46 | END(%(func)s) |
| 47 | """ |
| 48 | |
| 49 | arm_eabi_call_long = syscall_stub_header + """\ |
| 50 | mov ip, sp |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 51 | stmfd sp!, {r4, r5, r6, r7} |
Christopher Ferris | ed45970 | 2013-12-02 17:44:53 -0800 | [diff] [blame] | 52 | .cfi_def_cfa_offset 16 |
| 53 | .cfi_rel_offset r4, 0 |
| 54 | .cfi_rel_offset r5, 4 |
| 55 | .cfi_rel_offset r6, 8 |
| 56 | .cfi_rel_offset r7, 12 |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 57 | ldmfd ip, {r4, r5, r6} |
| 58 | ldr r7, =%(__NR_name)s |
| 59 | swi #0 |
| 60 | ldmfd sp!, {r4, r5, r6, r7} |
Christopher Ferris | ed45970 | 2013-12-02 17:44:53 -0800 | [diff] [blame] | 61 | .cfi_def_cfa_offset 0 |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 62 | cmn r0, #(MAX_ERRNO + 1) |
| 63 | bxls lr |
| 64 | neg r0, r0 |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 65 | b __set_errno_internal |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 66 | END(%(func)s) |
| 67 | """ |
| 68 | |
| 69 | |
| 70 | # |
Elliott Hughes | d67b037 | 2019-04-15 14:18:26 -0700 | [diff] [blame^] | 71 | # Arm64 assembler template for each syscall stub |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 72 | # |
| 73 | |
| 74 | arm64_call = syscall_stub_header + """\ |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 75 | mov x8, %(__NR_name)s |
| 76 | svc #0 |
| 77 | |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 78 | cmn x0, #(MAX_ERRNO + 1) |
| 79 | cneg x0, x0, hi |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 80 | b.hi __set_errno_internal |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 81 | |
| 82 | ret |
| 83 | END(%(func)s) |
| 84 | """ |
| 85 | |
| 86 | |
| 87 | # |
Elliott Hughes | d67b037 | 2019-04-15 14:18:26 -0700 | [diff] [blame^] | 88 | # MIPS assembler template for each syscall stub |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 89 | # |
| 90 | |
Elliott Hughes | 9abbbdc | 2014-02-19 14:54:31 -0800 | [diff] [blame] | 91 | mips_call = syscall_stub_header + """\ |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 92 | .set noreorder |
Elliott Hughes | d4ca231 | 2017-10-11 22:27:45 -0700 | [diff] [blame] | 93 | .cpload $t9 |
| 94 | li $v0, %(__NR_name)s |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 95 | syscall |
Elliott Hughes | d4ca231 | 2017-10-11 22:27:45 -0700 | [diff] [blame] | 96 | bnez $a3, 1f |
| 97 | move $a0, $v0 |
| 98 | j $ra |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 99 | nop |
| 100 | 1: |
Elliott Hughes | d4ca231 | 2017-10-11 22:27:45 -0700 | [diff] [blame] | 101 | la $t9,__set_errno_internal |
| 102 | j $t9 |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 103 | nop |
| 104 | .set reorder |
Elliott Hughes | 9abbbdc | 2014-02-19 14:54:31 -0800 | [diff] [blame] | 105 | END(%(func)s) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 106 | """ |
| 107 | |
| 108 | |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 109 | # |
Elliott Hughes | d67b037 | 2019-04-15 14:18:26 -0700 | [diff] [blame^] | 110 | # MIPS64 assembler template for each syscall stub |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 111 | # |
| 112 | |
Elliott Hughes | 9abbbdc | 2014-02-19 14:54:31 -0800 | [diff] [blame] | 113 | mips64_call = syscall_stub_header + """\ |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 114 | .set push |
| 115 | .set noreorder |
Elliott Hughes | ab413c5 | 2017-10-13 13:22:24 -0700 | [diff] [blame] | 116 | li $v0, %(__NR_name)s |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 117 | syscall |
Elliott Hughes | ab413c5 | 2017-10-13 13:22:24 -0700 | [diff] [blame] | 118 | bnez $a3, 1f |
| 119 | move $a0, $v0 |
| 120 | j $ra |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 121 | nop |
| 122 | 1: |
Elliott Hughes | ab413c5 | 2017-10-13 13:22:24 -0700 | [diff] [blame] | 123 | move $t0, $ra |
| 124 | bal 2f |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 125 | nop |
| 126 | 2: |
Elliott Hughes | ab413c5 | 2017-10-13 13:22:24 -0700 | [diff] [blame] | 127 | .cpsetup $ra, $t1, 2b |
| 128 | LA $t9, __set_errno_internal |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 129 | .cpreturn |
Elliott Hughes | ab413c5 | 2017-10-13 13:22:24 -0700 | [diff] [blame] | 130 | j $t9 |
| 131 | move $ra, $t0 |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 132 | .set pop |
Elliott Hughes | 9abbbdc | 2014-02-19 14:54:31 -0800 | [diff] [blame] | 133 | END(%(func)s) |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 134 | """ |
| 135 | |
| 136 | |
| 137 | # |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | # x86 assembler templates for each syscall stub |
| 139 | # |
| 140 | |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 141 | x86_registers = [ "ebx", "ecx", "edx", "esi", "edi", "ebp" ] |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 142 | |
Mingwei Shi | be91052 | 2015-11-12 07:02:14 +0000 | [diff] [blame] | 143 | x86_call_prepare = """\ |
| 144 | |
| 145 | call __kernel_syscall |
| 146 | pushl %eax |
| 147 | .cfi_adjust_cfa_offset 4 |
| 148 | .cfi_rel_offset eax, 0 |
| 149 | |
| 150 | """ |
| 151 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 152 | x86_call = """\ |
| 153 | movl $%(__NR_name)s, %%eax |
Mingwei Shi | be91052 | 2015-11-12 07:02:14 +0000 | [diff] [blame] | 154 | call *(%%esp) |
| 155 | addl $4, %%esp |
| 156 | |
Elliott Hughes | 9aceab5 | 2013-03-12 14:57:30 -0700 | [diff] [blame] | 157 | cmpl $-MAX_ERRNO, %%eax |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 158 | jb 1f |
| 159 | negl %%eax |
| 160 | pushl %%eax |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 161 | call __set_errno_internal |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | addl $4, %%esp |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 163 | 1: |
| 164 | """ |
| 165 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 166 | x86_return = """\ |
| 167 | ret |
| 168 | END(%(func)s) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 169 | """ |
| 170 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 171 | |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 172 | # |
Elliott Hughes | d67b037 | 2019-04-15 14:18:26 -0700 | [diff] [blame^] | 173 | # x86_64 assembler template for each syscall stub |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 174 | # |
| 175 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 176 | x86_64_call = """\ |
| 177 | movl $%(__NR_name)s, %%eax |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 178 | syscall |
| 179 | cmpq $-MAX_ERRNO, %%rax |
| 180 | jb 1f |
| 181 | negl %%eax |
| 182 | movl %%eax, %%edi |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 183 | call __set_errno_internal |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 184 | 1: |
| 185 | ret |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 186 | END(%(func)s) |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 187 | """ |
| 188 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 189 | |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 190 | def param_uses_64bits(param): |
| 191 | """Returns True iff a syscall parameter description corresponds |
| 192 | to a 64-bit type.""" |
| 193 | param = param.strip() |
| 194 | # First, check that the param type begins with one of the known |
| 195 | # 64-bit types. |
| 196 | if not ( \ |
| 197 | param.startswith("int64_t") or param.startswith("uint64_t") or \ |
| 198 | param.startswith("loff_t") or param.startswith("off64_t") or \ |
| 199 | param.startswith("long long") or param.startswith("unsigned long long") or |
| 200 | param.startswith("signed long long") ): |
| 201 | return False |
| 202 | |
| 203 | # Second, check that there is no pointer type here |
| 204 | if param.find("*") >= 0: |
| 205 | return False |
| 206 | |
| 207 | # Ok |
| 208 | return True |
| 209 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 210 | |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 211 | def count_arm_param_registers(params): |
| 212 | """This function is used to count the number of register used |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 213 | to pass parameters when invoking an ARM system call. |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 214 | This is because the ARM EABI mandates that 64-bit quantities |
| 215 | must be passed in an even+odd register pair. So, for example, |
| 216 | something like: |
| 217 | |
| 218 | foo(int fd, off64_t pos) |
| 219 | |
| 220 | would actually need 4 registers: |
| 221 | r0 -> int |
| 222 | r1 -> unused |
| 223 | r2-r3 -> pos |
| 224 | """ |
| 225 | count = 0 |
| 226 | for param in params: |
| 227 | if param_uses_64bits(param): |
| 228 | if (count & 1) != 0: |
| 229 | count += 1 |
| 230 | count += 2 |
| 231 | else: |
| 232 | count += 1 |
| 233 | return count |
| 234 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 235 | |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 236 | def count_generic_param_registers(params): |
| 237 | count = 0 |
| 238 | for param in params: |
| 239 | if param_uses_64bits(param): |
| 240 | count += 2 |
| 241 | else: |
| 242 | count += 1 |
| 243 | return count |
| 244 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 245 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 246 | def count_generic_param_registers64(params): |
| 247 | count = 0 |
| 248 | for param in params: |
| 249 | count += 1 |
| 250 | return count |
| 251 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 252 | |
Elliott Hughes | cda6209 | 2013-03-22 13:50:44 -0700 | [diff] [blame] | 253 | # This lets us support regular system calls like __NR_write and also weird |
| 254 | # ones like __ARM_NR_cacheflush, where the NR doesn't come at the start. |
| 255 | def make__NR_name(name): |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 256 | if name.startswith("__ARM_NR_"): |
Elliott Hughes | cda6209 | 2013-03-22 13:50:44 -0700 | [diff] [blame] | 257 | return name |
| 258 | else: |
| 259 | return "__NR_%s" % (name) |
| 260 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 261 | |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 262 | def add_footer(pointer_length, stub, syscall): |
| 263 | # Add any aliases for this syscall. |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 264 | aliases = syscall["aliases"] |
| 265 | for alias in aliases: |
Christopher Ferris | fa5faa0 | 2015-03-24 16:50:46 -0700 | [diff] [blame] | 266 | stub += "\nALIAS_SYMBOL(%s, %s)\n" % (alias, syscall["func"]) |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 267 | |
Nick Kralevich | 00490ae | 2015-02-03 11:27:25 -0800 | [diff] [blame] | 268 | # Use hidden visibility on LP64 for any functions beginning with underscores. |
| 269 | # Force hidden visibility for any functions which begin with 3 underscores |
| 270 | if (pointer_length == 64 and syscall["func"].startswith("__")) or syscall["func"].startswith("___"): |
Elliott Hughes | d465eb4 | 2014-02-19 18:59:19 -0800 | [diff] [blame] | 271 | stub += '.hidden ' + syscall["func"] + '\n' |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 272 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 273 | return stub |
| 274 | |
| 275 | |
| 276 | def arm_eabi_genstub(syscall): |
| 277 | num_regs = count_arm_param_registers(syscall["params"]) |
| 278 | if num_regs > 4: |
| 279 | return arm_eabi_call_long % syscall |
| 280 | return arm_eabi_call_default % syscall |
| 281 | |
| 282 | |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 283 | def arm64_genstub(syscall): |
| 284 | return arm64_call % syscall |
| 285 | |
| 286 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 287 | def mips_genstub(syscall): |
| 288 | return mips_call % syscall |
| 289 | |
| 290 | |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 291 | def mips64_genstub(syscall): |
| 292 | return mips64_call % syscall |
| 293 | |
| 294 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 295 | def x86_genstub(syscall): |
| 296 | result = syscall_stub_header % syscall |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 297 | |
| 298 | numparams = count_generic_param_registers(syscall["params"]) |
Mingwei Shi | be91052 | 2015-11-12 07:02:14 +0000 | [diff] [blame] | 299 | stack_bias = numparams*4 + 8 |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 300 | offset = 0 |
| 301 | mov_result = "" |
Christopher Ferris | 15b91e9 | 2014-05-29 18:17:09 -0700 | [diff] [blame] | 302 | first_push = True |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 303 | for register in x86_registers[:numparams]: |
| 304 | result += " pushl %%%s\n" % register |
Christopher Ferris | 15b91e9 | 2014-05-29 18:17:09 -0700 | [diff] [blame] | 305 | if first_push: |
| 306 | result += " .cfi_def_cfa_offset 8\n" |
| 307 | result += " .cfi_rel_offset %s, 0\n" % register |
| 308 | first_push = False |
| 309 | else: |
| 310 | result += " .cfi_adjust_cfa_offset 4\n" |
| 311 | result += " .cfi_rel_offset %s, 0\n" % register |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 312 | mov_result += " mov %d(%%esp), %%%s\n" % (stack_bias+offset, register) |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 313 | offset += 4 |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 314 | |
Mingwei Shi | be91052 | 2015-11-12 07:02:14 +0000 | [diff] [blame] | 315 | result += x86_call_prepare |
Christopher Ferris | 15b91e9 | 2014-05-29 18:17:09 -0700 | [diff] [blame] | 316 | result += mov_result |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 317 | result += x86_call % syscall |
| 318 | |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 319 | for register in reversed(x86_registers[:numparams]): |
| 320 | result += " popl %%%s\n" % register |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 321 | |
| 322 | result += x86_return % syscall |
| 323 | return result |
| 324 | |
Serban Constantinescu | feaa89a | 2013-10-07 16:49:09 +0100 | [diff] [blame] | 325 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 326 | def x86_genstub_socketcall(syscall): |
| 327 | # %ebx <--- Argument 1 - The call id of the needed vectored |
| 328 | # syscall (socket, bind, recv, etc) |
| 329 | # %ecx <--- Argument 2 - Pointer to the rest of the arguments |
| 330 | # from the original function called (socket()) |
| 331 | |
| 332 | result = syscall_stub_header % syscall |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 333 | |
| 334 | # save the regs we need |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 335 | result += " pushl %ebx\n" |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 336 | result += " .cfi_def_cfa_offset 8\n" |
| 337 | result += " .cfi_rel_offset ebx, 0\n" |
Christopher Ferris | 15b91e9 | 2014-05-29 18:17:09 -0700 | [diff] [blame] | 338 | result += " pushl %ecx\n" |
| 339 | result += " .cfi_adjust_cfa_offset 4\n" |
| 340 | result += " .cfi_rel_offset ecx, 0\n" |
Mingwei Shi | be91052 | 2015-11-12 07:02:14 +0000 | [diff] [blame] | 341 | stack_bias = 16 |
| 342 | |
| 343 | result += x86_call_prepare |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 344 | |
| 345 | # set the call id (%ebx) |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 346 | result += " mov $%d, %%ebx\n" % syscall["socketcall_id"] |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 347 | |
| 348 | # set the pointer to the rest of the args into %ecx |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 349 | result += " mov %esp, %ecx\n" |
| 350 | result += " addl $%d, %%ecx\n" % (stack_bias) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 351 | |
| 352 | # now do the syscall code itself |
| 353 | result += x86_call % syscall |
| 354 | |
| 355 | # now restore the saved regs |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 356 | result += " popl %ecx\n" |
| 357 | result += " popl %ebx\n" |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 358 | |
| 359 | # epilog |
| 360 | result += x86_return % syscall |
| 361 | return result |
| 362 | |
| 363 | |
| 364 | def x86_64_genstub(syscall): |
| 365 | result = syscall_stub_header % syscall |
| 366 | num_regs = count_generic_param_registers64(syscall["params"]) |
| 367 | if (num_regs > 3): |
| 368 | # rcx is used as 4th argument. Kernel wants it at r10. |
| 369 | result += " movq %rcx, %r10\n" |
| 370 | |
| 371 | result += x86_64_call % syscall |
| 372 | return result |
| 373 | |
| 374 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 375 | class SysCallsTxtParser: |
| 376 | def __init__(self): |
| 377 | self.syscalls = [] |
| 378 | self.lineno = 0 |
| 379 | |
| 380 | def E(self, msg): |
| 381 | print "%d: %s" % (self.lineno, msg) |
| 382 | |
| 383 | def parse_line(self, line): |
| 384 | """ parse a syscall spec line. |
| 385 | |
| 386 | line processing, format is |
| 387 | return type func_name[|alias_list][:syscall_name[:socketcall_id]] ( [paramlist] ) architecture_list |
| 388 | """ |
| 389 | pos_lparen = line.find('(') |
| 390 | E = self.E |
| 391 | if pos_lparen < 0: |
| 392 | E("missing left parenthesis in '%s'" % line) |
| 393 | return |
| 394 | |
| 395 | pos_rparen = line.rfind(')') |
| 396 | if pos_rparen < 0 or pos_rparen <= pos_lparen: |
| 397 | E("missing or misplaced right parenthesis in '%s'" % line) |
| 398 | return |
| 399 | |
| 400 | return_type = line[:pos_lparen].strip().split() |
| 401 | if len(return_type) < 2: |
| 402 | E("missing return type in '%s'" % line) |
| 403 | return |
| 404 | |
| 405 | syscall_func = return_type[-1] |
| 406 | return_type = string.join(return_type[:-1],' ') |
| 407 | socketcall_id = -1 |
| 408 | |
| 409 | pos_colon = syscall_func.find(':') |
| 410 | if pos_colon < 0: |
| 411 | syscall_name = syscall_func |
| 412 | else: |
| 413 | if pos_colon == 0 or pos_colon+1 >= len(syscall_func): |
| 414 | E("misplaced colon in '%s'" % line) |
| 415 | return |
| 416 | |
| 417 | # now find if there is a socketcall_id for a dispatch-type syscall |
| 418 | # after the optional 2nd colon |
| 419 | pos_colon2 = syscall_func.find(':', pos_colon + 1) |
| 420 | if pos_colon2 < 0: |
| 421 | syscall_name = syscall_func[pos_colon+1:] |
| 422 | syscall_func = syscall_func[:pos_colon] |
| 423 | else: |
| 424 | if pos_colon2+1 >= len(syscall_func): |
| 425 | E("misplaced colon2 in '%s'" % line) |
| 426 | return |
| 427 | syscall_name = syscall_func[(pos_colon+1):pos_colon2] |
| 428 | socketcall_id = int(syscall_func[pos_colon2+1:]) |
| 429 | syscall_func = syscall_func[:pos_colon] |
| 430 | |
| 431 | alias_delim = syscall_func.find('|') |
| 432 | if alias_delim > 0: |
| 433 | alias_list = syscall_func[alias_delim+1:].strip() |
| 434 | syscall_func = syscall_func[:alias_delim] |
| 435 | alias_delim = syscall_name.find('|') |
| 436 | if alias_delim > 0: |
| 437 | syscall_name = syscall_name[:alias_delim] |
| 438 | syscall_aliases = string.split(alias_list, ',') |
| 439 | else: |
| 440 | syscall_aliases = [] |
| 441 | |
| 442 | if pos_rparen > pos_lparen+1: |
| 443 | syscall_params = line[pos_lparen+1:pos_rparen].split(',') |
| 444 | params = string.join(syscall_params,',') |
| 445 | else: |
| 446 | syscall_params = [] |
| 447 | params = "void" |
| 448 | |
| 449 | t = { |
| 450 | "name" : syscall_name, |
| 451 | "func" : syscall_func, |
| 452 | "aliases" : syscall_aliases, |
| 453 | "params" : syscall_params, |
| 454 | "decl" : "%-15s %s (%s);" % (return_type, syscall_func, params), |
| 455 | "socketcall_id" : socketcall_id |
| 456 | } |
| 457 | |
| 458 | # Parse the architecture list. |
| 459 | arch_list = line[pos_rparen+1:].strip() |
| 460 | if arch_list == "all": |
| 461 | for arch in all_arches: |
| 462 | t[arch] = True |
Elliott Hughes | 8251d44 | 2018-11-09 13:55:21 -0800 | [diff] [blame] | 463 | elif arch_list == "lp32": |
| 464 | for arch in all_arches: |
| 465 | if "64" not in arch: |
| 466 | t[arch] = True |
| 467 | elif arch_list == "lp64": |
| 468 | for arch in all_arches: |
| 469 | if "64" in arch: |
| 470 | t[arch] = True |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 471 | else: |
| 472 | for arch in string.split(arch_list, ','): |
| 473 | if arch in all_arches: |
| 474 | t[arch] = True |
| 475 | else: |
| 476 | E("invalid syscall architecture '%s' in '%s'" % (arch, line)) |
| 477 | return |
| 478 | |
| 479 | self.syscalls.append(t) |
| 480 | |
| 481 | logging.debug(t) |
| 482 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 483 | def parse_open_file(self, fp): |
| 484 | for line in fp: |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 485 | self.lineno += 1 |
| 486 | line = line.strip() |
| 487 | if not line: continue |
| 488 | if line[0] == '#': continue |
| 489 | self.parse_line(line) |
| 490 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 491 | def parse_file(self, file_path): |
| 492 | logging.debug("parse_file: %s" % file_path) |
| 493 | with open(file_path) as fp: |
Alessio Balsini | 93d4f8b | 2017-04-11 18:27:29 +0200 | [diff] [blame] | 494 | self.parse_open_file(fp) |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 495 | |
| 496 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 497 | class State: |
| 498 | def __init__(self): |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 499 | self.syscalls = [] |
| 500 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 501 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 502 | def process_file(self, input): |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 503 | parser = SysCallsTxtParser() |
| 504 | parser.parse_file(input) |
| 505 | self.syscalls = parser.syscalls |
| 506 | parser = None |
| 507 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 508 | for syscall in self.syscalls: |
| 509 | syscall["__NR_name"] = make__NR_name(syscall["name"]) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 510 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 511 | if syscall.has_key("arm"): |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 512 | 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] | 513 | |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 514 | if syscall.has_key("arm64"): |
| 515 | syscall["asm-arm64"] = add_footer(64, arm64_genstub(syscall), syscall) |
| 516 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 517 | if syscall.has_key("x86"): |
| 518 | if syscall["socketcall_id"] >= 0: |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 519 | 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] | 520 | else: |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 521 | syscall["asm-x86"] = add_footer(32, x86_genstub(syscall), syscall) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 522 | elif syscall["socketcall_id"] >= 0: |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 523 | 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] | 524 | return |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 525 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 526 | if syscall.has_key("mips"): |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 527 | 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] | 528 | |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 529 | if syscall.has_key("mips64"): |
| 530 | syscall["asm-mips64"] = add_footer(64, mips64_genstub(syscall), syscall) |
| 531 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 532 | if syscall.has_key("x86_64"): |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 533 | syscall["asm-x86_64"] = add_footer(64, x86_64_genstub(syscall), syscall) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 534 | |
Elliott Hughes | d2f725e | 2016-07-15 15:47:47 -0700 | [diff] [blame] | 535 | |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 536 | def regenerate(self): |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 537 | for arch in all_arches: |
Elliott Hughes | d67b037 | 2019-04-15 14:18:26 -0700 | [diff] [blame^] | 538 | filename = '%s/arch-%s/syscalls.S' % (bionic_libc, arch) |
| 539 | fp = open(filename, 'w') |
| 540 | fp.write("/* Generated by gensyscalls.py. Do not edit. */\n") |
| 541 | fp.write("#include <private/bionic_asm.h>\n") |
| 542 | for syscall in self.syscalls: |
| 543 | if syscall.has_key("asm-%s" % arch): |
| 544 | fp.write(syscall["asm-%s" % arch]) |
| 545 | fp.close() |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 546 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 547 | logging.basicConfig(level=logging.INFO) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 548 | |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 549 | if __name__ == "__main__": |
| 550 | state = State() |
Elliott Hughes | d67b037 | 2019-04-15 14:18:26 -0700 | [diff] [blame^] | 551 | state.process_file(os.path.join(bionic_libc, "SYSCALLS.TXT")) |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 552 | state.regenerate() |