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