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