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