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 | |
Shin-ichiro KAWASAKI | ce0595d | 2009-09-01 19:03:06 +0900 | [diff] [blame] | 367 | def superh_genstub(self, fname, flags, idname): |
| 368 | numargs = int(flags) |
| 369 | t = { "fname" : fname, |
| 370 | "idname" : idname, |
| 371 | "numargs" : numargs } |
| 372 | superh_call = superh_header |
| 373 | if flags: |
| 374 | if numargs == 5: |
| 375 | superh_call += superh_5args_header |
| 376 | if numargs == 6: |
| 377 | superh_call += superh_6args_header |
| 378 | if numargs == 7: |
| 379 | superh_call += superh_7args_header |
| 380 | superh_call += superh_call_default |
| 381 | return superh_call % t |
| 382 | |
| 383 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 384 | def process_file(self,input): |
| 385 | parser = SysCallsTxtParser() |
| 386 | parser.parse_file(input) |
| 387 | self.syscalls = parser.syscalls |
| 388 | parser = None |
| 389 | |
| 390 | for t in self.syscalls: |
| 391 | syscall_func = t["func"] |
| 392 | syscall_params = t["params"] |
| 393 | syscall_name = t["name"] |
| 394 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 395 | if t["common"] >= 0 or t["armid"] >= 0: |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 396 | num_regs = count_arm_param_registers(syscall_params) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 397 | if gen_thumb_stubs: |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 398 | 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] | 399 | else: |
| 400 | if gen_eabi_stubs: |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 401 | 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] | 402 | else: |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 403 | 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] | 404 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 405 | if t["common"] >= 0 or t["x86id"] >= 0: |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 406 | num_regs = count_generic_param_registers(syscall_params) |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 407 | if t["cid"] >= 0: |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 408 | 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] | 409 | else: |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 410 | 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] | 411 | elif t["cid"] >= 0: |
| 412 | E("cid for dispatch syscalls is only supported for x86 in " |
| 413 | "'%s'" % syscall_name) |
| 414 | return |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 415 | if t["common"] >= 0 or t["mipsid"] >= 0: |
| 416 | 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] | 417 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 418 | |
| 419 | def gen_NR_syscall(self,fp,name,id): |
| 420 | fp.write( "#define __NR_%-25s (__NR_SYSCALL_BASE + %d)\n" % (name,id) ) |
| 421 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 422 | # now dump the content of linux-syscalls.h |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 423 | def gen_linux_syscalls_h(self): |
| 424 | path = "include/sys/linux-syscalls.h" |
| 425 | D( "generating "+path ) |
| 426 | fp = create_file( path ) |
| 427 | fp.write( "/* auto-generated by gensyscalls.py, do not touch */\n" ) |
| 428 | fp.write( "#ifndef _BIONIC_LINUX_SYSCALLS_H_\n\n" ) |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 429 | 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] | 430 | fp.write( "#if defined __arm__ && !defined __ARM_EABI__ && !defined __thumb__\n" ) |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 431 | fp.write( " # define __NR_SYSCALL_BASE 0x900000\n" ) |
| 432 | fp.write( "#elif defined(__mips__)\n" ) |
| 433 | fp.write( " # define __NR_SYSCALL_BASE 4000\n" ) |
| 434 | fp.write( "#else\n" ) |
| 435 | fp.write( " # define __NR_SYSCALL_BASE 0\n" ) |
| 436 | fp.write( "#endif\n\n" ) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 437 | |
| 438 | # first, all common syscalls |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 439 | for sc in sorted(self.syscalls,key=lambda x:x["common"]): |
| 440 | sc_id = sc["common"] |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 441 | sc_name = sc["name"] |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 442 | if sc_id >= 0: |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 443 | self.gen_NR_syscall( fp, sc_name, sc_id ) |
| 444 | |
| 445 | # now, all arm-specific syscalls |
| 446 | fp.write( "\n#ifdef __arm__\n" ); |
| 447 | for sc in self.syscalls: |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 448 | sc_id = sc["armid"] |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 449 | sc_name = sc["name"] |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 450 | if sc_id >= 0: |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 451 | self.gen_NR_syscall( fp, sc_name, sc_id ) |
| 452 | fp.write( "#endif\n" ); |
| 453 | |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 454 | gen_syscalls = {} |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 455 | # finally, all i386-specific syscalls |
| 456 | fp.write( "\n#ifdef __i386__\n" ); |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 457 | for sc in sorted(self.syscalls,key=lambda x:x["x86id"]): |
| 458 | sc_id = sc["x86id"] |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 459 | sc_name = sc["name"] |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 460 | if sc_id >= 0 and sc_name not in gen_syscalls: |
| 461 | self.gen_NR_syscall( fp, sc_name, sc_id ) |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 462 | gen_syscalls[sc_name] = True |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 463 | fp.write( "#endif\n" ); |
| 464 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 465 | # all mips-specific syscalls |
| 466 | fp.write( "\n#ifdef __mips__\n" ); |
| 467 | for sc in sorted(self.syscalls,key=lambda x:x["mipsid"]): |
| 468 | sc_id = sc["mipsid"] |
| 469 | if sc_id >= 0: |
| 470 | self.gen_NR_syscall( fp, sc["name"], sc_id ) |
| 471 | fp.write( "#endif\n" ); |
| 472 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 473 | fp.write( "\n#endif\n" ) |
| 474 | fp.write( "\n#endif /* _BIONIC_LINUX_SYSCALLS_H_ */\n" ); |
| 475 | fp.close() |
| 476 | self.other_files.append( path ) |
| 477 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 478 | # now dump the content of linux-syscalls.h |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 479 | def gen_linux_unistd_h(self): |
| 480 | path = "include/sys/linux-unistd.h" |
| 481 | D( "generating "+path ) |
| 482 | fp = create_file( path ) |
| 483 | fp.write( "/* auto-generated by gensyscalls.py, do not touch */\n" ) |
| 484 | fp.write( "#ifndef _BIONIC_LINUX_UNISTD_H_\n\n" ); |
| 485 | fp.write( "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n" ) |
| 486 | |
| 487 | for sc in self.syscalls: |
| 488 | fp.write( sc["decl"]+"\n" ) |
| 489 | |
| 490 | fp.write( "#ifdef __cplusplus\n}\n#endif\n" ) |
| 491 | fp.write( "\n#endif /* _BIONIC_LINUX_UNISTD_H_ */\n" ); |
| 492 | fp.close() |
| 493 | self.other_files.append( path ) |
| 494 | |
| 495 | # now dump the contents of syscalls.mk |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 496 | def gen_arch_syscalls_mk(self, arch): |
| 497 | path = "arch-%s/syscalls.mk" % arch |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 498 | D( "generating "+path ) |
| 499 | fp = create_file( path ) |
| 500 | fp.write( "# auto-generated by gensyscalls.py, do not touch\n" ) |
| 501 | fp.write( "syscall_src := \n" ) |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 502 | arch_test = { |
| 503 | "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] | 504 | "x86": lambda x: x.has_key("asm-x86"), |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 505 | "mips": lambda x: x.has_key("asm-mips") |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 506 | } |
| 507 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 508 | for sc in self.syscalls: |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 509 | if arch_test[arch](sc): |
| 510 | fp.write("syscall_src += arch-%s/syscalls/%s.S\n" % |
| 511 | (arch, sc["func"])) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 512 | fp.close() |
| 513 | self.other_files.append( path ) |
| 514 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 515 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 516 | # now generate each syscall stub |
| 517 | def gen_syscall_stubs(self): |
| 518 | for sc in self.syscalls: |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 519 | 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] | 520 | fname = "arch-arm/syscalls/%s.S" % sc["func"] |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 521 | D2( ">>> generating "+fname ) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 522 | fp = create_file( fname ) |
| 523 | fp.write(sc["asm-arm"]) |
| 524 | fp.close() |
| 525 | self.new_stubs.append( fname ) |
| 526 | |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 527 | 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] | 528 | fname = "arch-arm/syscalls/%s.S" % sc["func"] |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 529 | D2( ">>> generating "+fname ) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 530 | fp = create_file( fname ) |
| 531 | fp.write(sc["asm-thumb"]) |
| 532 | fp.close() |
| 533 | self.new_stubs.append( fname ) |
| 534 | |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 535 | 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] | 536 | fname = "arch-x86/syscalls/%s.S" % sc["func"] |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 537 | D2( ">>> generating "+fname ) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 538 | fp = create_file( fname ) |
| 539 | fp.write(sc["asm-x86"]) |
| 540 | fp.close() |
| 541 | self.new_stubs.append( fname ) |
| 542 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 543 | if sc.has_key("asm-mips") and 'mips' in all_archs: |
| 544 | fname = "arch-mips/syscalls/%s.S" % sc["func"] |
| 545 | D2( ">>> generating "+fname ) |
| 546 | fp = create_file( fname ) |
| 547 | fp.write(sc["asm-mips"]) |
| 548 | fp.close() |
| 549 | self.new_stubs.append( fname ) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 550 | |
| 551 | def regenerate(self): |
| 552 | D( "scanning for existing architecture-specific stub files" ) |
| 553 | |
| 554 | bionic_root_len = len(bionic_root) |
| 555 | |
| 556 | for arch in all_archs: |
| 557 | arch_path = bionic_root + "arch-" + arch |
| 558 | D( "scanning " + arch_path ) |
| 559 | files = glob.glob( arch_path + "/syscalls/*.S" ) |
| 560 | for f in files: |
| 561 | self.old_stubs.append( f[bionic_root_len:] ) |
| 562 | |
| 563 | D( "found %d stub files" % len(self.old_stubs) ) |
| 564 | |
| 565 | if not os.path.exists( bionic_temp ): |
| 566 | D( "creating %s" % bionic_temp ) |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame^] | 567 | make_dir( bionic_temp ) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 568 | |
| 569 | # D( "p4 editing source files" ) |
| 570 | # for arch in all_archs: |
| 571 | # commands.getoutput( "p4 edit " + arch + "/syscalls/*.S " ) |
| 572 | # commands.getoutput( "p4 edit " + arch + "/syscalls.mk" ) |
| 573 | # commands.getoutput( "p4 edit " + bionic_root + "include/sys/linux-syscalls.h" ) |
| 574 | |
| 575 | D( "re-generating stubs and support files" ) |
| 576 | |
| 577 | self.gen_linux_syscalls_h() |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 578 | for arch in all_archs: |
| 579 | self.gen_arch_syscalls_mk(arch) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 580 | self.gen_linux_unistd_h() |
| 581 | self.gen_syscall_stubs() |
| 582 | |
| 583 | D( "comparing files" ) |
| 584 | adds = [] |
| 585 | edits = [] |
| 586 | |
| 587 | for stub in self.new_stubs + self.other_files: |
| 588 | if not os.path.exists( bionic_root + stub ): |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 589 | # new file, git add it |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 590 | D( "new file: " + stub) |
| 591 | adds.append( bionic_root + stub ) |
| 592 | shutil.copyfile( bionic_temp + stub, bionic_root + stub ) |
| 593 | |
| 594 | elif not filecmp.cmp( bionic_temp + stub, bionic_root + stub ): |
| 595 | D( "changed file: " + stub) |
| 596 | edits.append( stub ) |
| 597 | |
| 598 | deletes = [] |
| 599 | for stub in self.old_stubs: |
| 600 | if not stub in self.new_stubs: |
| 601 | D( "deleted file: " + stub) |
| 602 | deletes.append( bionic_root + stub ) |
| 603 | |
| 604 | |
| 605 | if adds: |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 606 | commands.getoutput("git add " + " ".join(adds)) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 607 | if deletes: |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 608 | commands.getoutput("git rm " + " ".join(deletes)) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 609 | if edits: |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 610 | for file in edits: |
| 611 | shutil.copyfile( bionic_temp + file, bionic_root + file ) |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 612 | commands.getoutput("git add " + |
| 613 | " ".join((bionic_root + file) for file in edits)) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 614 | |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 615 | commands.getoutput("git add %s%s" % (bionic_root,"SYSCALLS.TXT")) |
| 616 | |
| 617 | if (not adds) and (not deletes) and (not edits): |
| 618 | D("no changes detected!") |
| 619 | else: |
| 620 | D("ready to go!!") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 621 | |
| 622 | D_setlevel(1) |
| 623 | |
| 624 | state = State() |
| 625 | state.process_file(bionic_root+"SYSCALLS.TXT") |
| 626 | state.regenerate() |