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