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