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