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