rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/runtime/sys_darwin_arm.s (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // System calls and other sys.stuff for ARM, Darwin 6 // See http://fxr.watson.org/fxr/source/bsd/kern/syscalls.c?v=xnu-1228 7 // or /usr/include/sys/syscall.h (on a Mac) for system call numbers. 8 9 #include "go_asm.h" 10 #include "go_tls.h" 11 #include "textflag.h" 12 13 // Copied from /usr/include/sys/syscall.h 14 #define SYS_exit 1 15 #define SYS_read 3 16 #define SYS_write 4 17 #define SYS_open 5 18 #define SYS_close 6 19 #define SYS_mmap 197 20 #define SYS_munmap 73 21 #define SYS_madvise 75 22 #define SYS_mincore 78 23 #define SYS_gettimeofday 116 24 #define SYS_kill 37 25 #define SYS_getpid 20 26 #define SYS___pthread_kill 328 27 #define SYS_setitimer 83 28 #define SYS___sysctl 202 29 #define SYS_sigprocmask 48 30 #define SYS_sigaction 46 31 #define SYS_sigreturn 184 32 #define SYS_select 93 33 #define SYS_bsdthread_register 366 34 #define SYS_bsdthread_create 360 35 #define SYS_bsdthread_terminate 361 36 #define SYS_kqueue 362 37 #define SYS_kevent 363 38 #define SYS_fcntl 92 39 40 TEXT notok<>(SB),NOSPLIT,$0 41 MOVW $0, R8 42 MOVW R8, (R8) 43 B 0(PC) 44 45 TEXT runtime·open(SB),NOSPLIT,$0 46 MOVW name+0(FP), R0 47 MOVW mode+4(FP), R1 48 MOVW perm+8(FP), R2 49 MOVW $SYS_open, R12 50 SWI $0x80 51 MOVW.CS $-1, R0 52 MOVW R0, ret+12(FP) 53 RET 54 55 TEXT runtime·closefd(SB),NOSPLIT,$0 56 MOVW fd+0(FP), R0 57 MOVW $SYS_close, R12 58 SWI $0x80 59 MOVW.CS $-1, R0 60 MOVW R0, ret+4(FP) 61 RET 62 63 TEXT runtime·write(SB),NOSPLIT,$0 64 MOVW fd+0(FP), R0 65 MOVW p+4(FP), R1 66 MOVW n+8(FP), R2 67 MOVW $SYS_write, R12 68 SWI $0x80 69 MOVW.CS $-1, R0 70 MOVW R0, ret+12(FP) 71 RET 72 73 TEXT runtime·read(SB),NOSPLIT,$0 74 MOVW fd+0(FP), R0 75 MOVW p+4(FP), R1 76 MOVW n+8(FP), R2 77 MOVW $SYS_read, R12 78 SWI $0x80 79 MOVW.CS $-1, R0 80 MOVW R0, ret+12(FP) 81 RET 82 83 TEXT runtime·exit(SB),NOSPLIT,$-4 84 MOVW code+0(FP), R0 85 MOVW $SYS_exit, R12 86 SWI $0x80 87 MOVW $1234, R0 88 MOVW $1002, R1 89 MOVW R0, (R1) // fail hard 90 91 // Exit this OS thread (like pthread_exit, which eventually 92 // calls __bsdthread_terminate). 93 TEXT runtime·exit1(SB),NOSPLIT,$0 94 MOVW $SYS_bsdthread_terminate, R12 95 SWI $0x80 96 MOVW $1234, R0 97 MOVW $1003, R1 98 MOVW R0, (R1) // fail hard 99 100 TEXT runtime·raise(SB),NOSPLIT,$24 101 MOVW $SYS_getpid, R12 102 SWI $0x80 103 // arg 1 pid already in R0 from getpid 104 MOVW unnamed+0(FP), R1 // arg 2 - signal 105 MOVW $1, R2 // arg 3 - posix 106 MOVW $SYS_kill, R12 107 SWI $0x80 108 RET 109 110 TEXT runtime·mmap(SB),NOSPLIT,$0 111 MOVW addr+0(FP), R0 112 MOVW n+4(FP), R1 113 MOVW prot+8(FP), R2 114 MOVW flags+12(FP), R3 115 MOVW fd+16(FP), R4 116 MOVW off+20(FP), R5 117 MOVW $0, R6 // off_t is uint64_t 118 MOVW $SYS_mmap, R12 119 SWI $0x80 120 MOVW R0, ret+24(FP) 121 RET 122 123 TEXT runtime·munmap(SB),NOSPLIT,$0 124 MOVW addr+0(FP), R0 125 MOVW n+4(FP), R1 126 MOVW $SYS_munmap, R12 127 SWI $0x80 128 BL.CS notok<>(SB) 129 RET 130 131 TEXT runtime·madvise(SB),NOSPLIT,$0 132 MOVW addr+0(FP), R0 133 MOVW n+4(FP), R1 134 MOVW flags+8(FP), R2 135 MOVW $SYS_madvise, R12 136 SWI $0x80 137 BL.CS notok<>(SB) 138 RET 139 140 TEXT runtime·setitimer(SB),NOSPLIT,$0 141 MOVW mode+0(FP), R0 142 MOVW new+4(FP), R1 143 MOVW old+8(FP), R2 144 MOVW $SYS_setitimer, R12 145 SWI $0x80 146 RET 147 148 TEXT runtime·mincore(SB),NOSPLIT,$0 149 MOVW addr+0(FP), R0 150 MOVW n+4(FP), R1 151 MOVW dst+8(FP), R2 152 MOVW $SYS_mincore, R12 153 SWI $0x80 154 MOVW R0, ret+12(FP) 155 RET 156 157 TEXT time·now(SB), 7, $32 158 MOVW $8(R13), R0 // timeval 159 MOVW $0, R1 // zone 160 MOVW $SYS_gettimeofday, R12 161 SWI $0x80 // Note: R0 is tv_sec, R1 is tv_usec 162 163 MOVW R1, R2 // usec 164 165 MOVW R0, sec+0(FP) 166 MOVW $0, R1 167 MOVW R1, loc+4(FP) 168 MOVW $1000, R3 169 MUL R3, R2 170 MOVW R2, nsec+8(FP) 171 RET 172 173 TEXT runtime·nanotime(SB),NOSPLIT,$32 174 MOVW $8(R13), R0 // timeval 175 MOVW $0, R1 // zone 176 MOVW $SYS_gettimeofday, R12 177 SWI $0x80 // Note: R0 is tv_sec, R1 is tv_usec 178 179 MOVW R1, R2 180 MOVW $1000000000, R3 181 MULLU R0, R3, (R1, R0) 182 MOVW $1000, R3 183 MOVW $0, R4 184 MUL R3, R2 185 ADD.S R2, R0 186 ADC R4, R1 187 188 MOVW R0, ret_lo+0(FP) 189 MOVW R1, ret_hi+4(FP) 190 RET 191 192 // Sigtramp's job is to call the actual signal handler. 193 // It is called with the following arguments on the stack: 194 // LR "return address" - ignored 195 // R0 actual handler 196 // R1 siginfo style - ignored 197 // R2 signal number 198 // R3 siginfo 199 // -4(FP) context, beware that 0(FP) is the saved LR 200 TEXT runtime·sigtramp(SB),NOSPLIT,$0 201 // this might be called in external code context, 202 // where g is not set. 203 // first save R0, because runtime·load_g will clobber it 204 MOVM.DB.W [R0], (R13) 205 MOVB runtime·iscgo(SB), R0 206 CMP $0, R0 207 BL.NE runtime·load_g(SB) 208 209 CMP $0, g 210 BNE cont 211 // fake function call stack frame for badsignal 212 // we only need to pass R2 (signal number), but 213 // badsignal will expect R2 at 4(R13), so we also 214 // push R1 onto stack. turns out we do need R1 215 // to do sigreturn. 216 MOVM.DB.W [R1,R2], (R13) 217 MOVW $runtime·badsignal(SB), R11 218 BL (R11) 219 MOVM.IA.W [R1], (R13) // saved infostype 220 ADD $(4+4), R13 // +4: also need to remove the pushed R0. 221 MOVW ucontext-4(FP), R0 // load ucontext 222 B ret 223 224 cont: 225 // Restore R0 226 MOVM.IA.W (R13), [R0] 227 228 // NOTE: some Darwin/ARM kernels always use the main stack to run the 229 // signal handler. We need to switch to gsignal ourselves. 230 MOVW g_m(g), R11 231 MOVW m_gsignal(R11), R5 232 MOVW (g_stack+stack_hi)(R5), R6 233 SUB $28, R6 234 235 // copy arguments for call to sighandler 236 MOVW R2, 4(R6) // signal num 237 MOVW R3, 8(R6) // signal info 238 MOVW g, 16(R6) // old_g 239 MOVW context-4(FP), R4 240 MOVW R4, 12(R6) // context 241 242 // Backup ucontext and infostyle 243 MOVW R4, 20(R6) 244 MOVW R1, 24(R6) 245 246 // switch stack and g 247 MOVW R6, R13 // sigtramp can not re-entrant, so no need to back up R13. 248 MOVW R5, g 249 250 BL (R0) 251 252 // call sigreturn 253 MOVW 20(R13), R0 // saved ucontext 254 MOVW 24(R13), R1 // saved infostyle 255 ret: 256 MOVW $SYS_sigreturn, R12 // sigreturn(ucontext, infostyle) 257 SWI $0x80 258 259 // if sigreturn fails, we can do nothing but exit 260 B runtime·exit(SB) 261 262 TEXT runtime·sigprocmask(SB),NOSPLIT,$0 263 MOVW sig+0(FP), R0 264 MOVW new+4(FP), R1 265 MOVW old+8(FP), R2 266 MOVW $SYS_sigprocmask, R12 267 SWI $0x80 268 BL.CS notok<>(SB) 269 RET 270 271 TEXT runtime·sigaction(SB),NOSPLIT,$0 272 MOVW mode+0(FP), R0 273 MOVW new+4(FP), R1 274 MOVW old+8(FP), R2 275 MOVW $SYS_sigaction, R12 276 SWI $0x80 277 RET 278 279 TEXT runtime·usleep(SB),NOSPLIT,$12 280 MOVW usec+0(FP), R0 281 MOVW R0, R1 282 MOVW $1000000, R2 283 DIV R2, R0 284 MOD R2, R1 285 MOVW R0, a-12(SP) 286 MOVW R1, b-8(SP) 287 288 // select(0, 0, 0, 0, &tv) 289 MOVW $0, R0 290 MOVW $0, R1 291 MOVW $0, R2 292 MOVW $0, R3 293 MOVW $a-12(SP), R4 294 MOVW $SYS_select, R12 295 SWI $0x80 296 RET 297 298 TEXT runtime·cas(SB),NOSPLIT,$0 299 B runtime·armcas(SB) 300 301 TEXT runtime·casp1(SB),NOSPLIT,$0 302 B runtime·cas(SB) 303 304 TEXT runtime·sysctl(SB),NOSPLIT,$0 305 MOVW mib+0(FP), R0 306 MOVW miblen+4(FP), R1 307 MOVW out+8(FP), R2 308 MOVW size+12(FP), R3 309 MOVW dst+16(FP), R4 310 MOVW ndst+20(FP), R5 311 MOVW $SYS___sysctl, R12 // syscall entry 312 SWI $0x80 313 BCC sysctl_ret 314 RSB $0, R0, R0 315 MOVW R0, ret+24(FP) 316 RET 317 sysctl_ret: 318 MOVW $0, R0 319 MOVW R0, ret+24(FP) 320 RET 321 322 // Thread related functions 323 // func bsdthread_create(stk, arg unsafe.Pointer, fn uintptr) int32 324 TEXT runtime·bsdthread_create(SB),NOSPLIT,$0 325 // Set up arguments to bsdthread_create system call. 326 // The ones in quotes pass through to the thread callback 327 // uninterpreted, so we can put whatever we want there. 328 MOVW fn+8(FP), R0 // "func" 329 MOVW arg+4(FP), R1 // "arg" 330 MOVW stk+0(FP), R2 // stack 331 MOVW $0x01000000, R4 // flags = PTHREAD_START_CUSTOM 332 MOVW $0, R5 // paranoia 333 MOVW $SYS_bsdthread_create, R12 334 SWI $0x80 335 BCC create_ret 336 RSB $0, R0, R0 337 MOVW R0, ret+12(FP) 338 RET 339 create_ret: 340 MOVW $0, R0 341 MOVW R0, ret+12(FP) 342 RET 343 344 // The thread that bsdthread_create creates starts executing here, 345 // because we registered this function using bsdthread_register 346 // at startup. 347 // R0 = "pthread" 348 // R1 = mach thread port 349 // R2 = "func" (= fn) 350 // R3 = "arg" (= m) 351 // R4 = stack 352 // R5 = flags (= 0) 353 // XXX: how to deal with R4/SP? ref: Libc-594.9.1/arm/pthreads/thread_start.s 354 TEXT runtime·bsdthread_start(SB),NOSPLIT,$0 355 MOVW R1, m_procid(R3) // thread port is m->procid 356 MOVW m_g0(R3), g 357 MOVW R3, g_m(g) 358 // ARM don't have runtime·stackcheck(SB) 359 // disable runfast mode of vfp 360 EOR R12, R12 361 WORD $0xeee1ca10 // fmxr fpscr, ip 362 BL (R2) // fn 363 BL runtime·exit1(SB) 364 RET 365 366 // int32 bsdthread_register(void) 367 // registers callbacks for threadstart (see bsdthread_create above 368 // and wqthread and pthsize (not used). returns 0 on success. 369 TEXT runtime·bsdthread_register(SB),NOSPLIT,$0 370 MOVW $runtime·bsdthread_start(SB), R0 // threadstart 371 MOVW $0, R1 // wqthread, not used by us 372 MOVW $0, R2 // pthsize, not used by us 373 MOVW $0, R3 // dummy_value [sic] 374 MOVW $0, R4 // targetconc_ptr 375 MOVW $0, R5 // dispatchqueue_offset 376 MOVW $SYS_bsdthread_register, R12 // bsdthread_register 377 SWI $0x80 378 MOVW R0, ret+0(FP) 379 RET 380 381 // uint32 mach_msg_trap(void*, uint32, uint32, uint32, uint32, uint32, uint32) 382 TEXT runtime·mach_msg_trap(SB),NOSPLIT,$0 383 MOVW h+0(FP), R0 384 MOVW op+4(FP), R1 385 MOVW send_size+8(FP), R2 386 MOVW rcv_size+12(FP), R3 387 MOVW rcv_name+16(FP), R4 388 MOVW timeout+20(FP), R5 389 MOVW notify+24(FP), R6 390 MVN $30, R12 391 SWI $0x80 392 MOVW R0, ret+28(FP) 393 RET 394 395 TEXT runtime·mach_task_self(SB),NOSPLIT,$0 396 MVN $27, R12 // task_self_trap 397 SWI $0x80 398 MOVW R0, ret+0(FP) 399 RET 400 401 TEXT runtime·mach_thread_self(SB),NOSPLIT,$0 402 MVN $26, R12 // thread_self_trap 403 SWI $0x80 404 MOVW R0, ret+0(FP) 405 RET 406 407 TEXT runtime·mach_reply_port(SB),NOSPLIT,$0 408 MVN $25, R12 // mach_reply_port 409 SWI $0x80 410 MOVW R0, ret+0(FP) 411 RET 412 413 // Mach provides trap versions of the semaphore ops, 414 // instead of requiring the use of RPC. 415 416 // uint32 mach_semaphore_wait(uint32) 417 TEXT runtime·mach_semaphore_wait(SB),NOSPLIT,$0 418 MOVW sema+0(FP), R0 419 MVN $35, R12 // semaphore_wait_trap 420 SWI $0x80 421 MOVW R0, ret+4(FP) 422 RET 423 424 // uint32 mach_semaphore_timedwait(uint32, uint32, uint32) 425 TEXT runtime·mach_semaphore_timedwait(SB),NOSPLIT,$0 426 MOVW sema+0(FP), R0 427 MOVW sec+4(FP), R1 428 MOVW nsec+8(FP), R2 429 MVN $37, R12 // semaphore_timedwait_trap 430 SWI $0x80 431 MOVW R0, ret+12(FP) 432 RET 433 434 // uint32 mach_semaphore_signal(uint32) 435 TEXT runtime·mach_semaphore_signal(SB),NOSPLIT,$0 436 MOVW sema+0(FP), R0 437 MVN $32, R12 // semaphore_signal_trap 438 SWI $0x80 439 MOVW R0, ret+4(FP) 440 RET 441 442 // uint32 mach_semaphore_signal_all(uint32) 443 TEXT runtime·mach_semaphore_signal_all(SB),NOSPLIT,$0 444 MOVW sema+0(FP), R0 445 MVN $33, R12 // semaphore_signal_all_trap 446 SWI $0x80 447 MOVW R0, ret+4(FP) 448 RET 449 450 // int32 runtime·kqueue(void) 451 TEXT runtime·kqueue(SB),NOSPLIT,$0 452 MOVW $SYS_kqueue, R12 453 SWI $0x80 454 RSB.CS $0, R0, R0 455 MOVW R0, ret+0(FP) 456 RET 457 458 // int32 runtime·kevent(int kq, Kevent *changelist, int nchanges, Kevent *eventlist, int events, Timespec *timeout) 459 TEXT runtime·kevent(SB),NOSPLIT,$0 460 MOVW $SYS_kevent, R12 461 MOVW kq+0(FP), R0 462 MOVW ch+4(FP), R1 463 MOVW nch+8(FP), R2 464 MOVW ev+12(FP), R3 465 MOVW nev+16(FP), R4 466 MOVW ts+20(FP), R5 467 SWI $0x80 468 RSB.CS $0, R0, R0 469 MOVW R0, ret+24(FP) 470 RET 471 472 // int32 runtime·closeonexec(int32 fd) 473 TEXT runtime·closeonexec(SB),NOSPLIT,$0 474 MOVW $SYS_fcntl, R12 475 MOVW fd+0(FP), R0 476 MOVW $2, R1 // F_SETFD 477 MOVW $1, R2 // FD_CLOEXEC 478 SWI $0x80 479 RET 480 481 // sigaltstack on some darwin/arm version is buggy and will always 482 // run the signal handler on the main stack, so our sigtramp has 483 // to do the stack switch ourselves. 484 TEXT runtime·sigaltstack(SB),NOSPLIT,$0 485 RET