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