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