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