github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/runtime/sys_darwin_amd64.s (about) 1 // Copyright 2009 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 // 6 // System calls and other sys.stuff for AMD64, Darwin 7 // See http://fxr.watson.org/fxr/source/bsd/kern/syscalls.c?v=xnu-1228 8 // or /usr/include/sys/syscall.h (on a Mac) for system call numbers. 9 // 10 // The low 24 bits are the system call number. 11 // The high 8 bits specify the kind of system call: 1=Mach, 2=BSD, 3=Machine-Dependent. 12 // 13 14 #include "go_asm.h" 15 #include "go_tls.h" 16 #include "textflag.h" 17 18 // Exit the entire program (like C exit) 19 TEXT runtime·exit(SB),NOSPLIT,$0 20 MOVL code+0(FP), DI // arg 1 exit status 21 MOVL $(0x2000000+1), AX // syscall entry 22 SYSCALL 23 MOVL $0xf1, 0xf1 // crash 24 RET 25 26 // Exit this OS thread (like pthread_exit, which eventually 27 // calls __bsdthread_terminate). 28 TEXT runtime·exit1(SB),NOSPLIT,$0 29 MOVL code+0(FP), DI // arg 1 exit status 30 MOVL $(0x2000000+361), AX // syscall entry 31 SYSCALL 32 MOVL $0xf1, 0xf1 // crash 33 RET 34 35 TEXT runtime·open(SB),NOSPLIT,$0 36 MOVQ name+0(FP), DI // arg 1 pathname 37 MOVL mode+8(FP), SI // arg 2 flags 38 MOVL perm+12(FP), DX // arg 3 mode 39 MOVL $(0x2000000+5), AX // syscall entry 40 SYSCALL 41 JCC 2(PC) 42 MOVL $-1, AX 43 MOVL AX, ret+16(FP) 44 RET 45 46 TEXT runtime·closefd(SB),NOSPLIT,$0 47 MOVL fd+0(FP), DI // arg 1 fd 48 MOVL $(0x2000000+6), AX // syscall entry 49 SYSCALL 50 JCC 2(PC) 51 MOVL $-1, AX 52 MOVL AX, ret+8(FP) 53 RET 54 55 TEXT runtime·read(SB),NOSPLIT,$0 56 MOVL fd+0(FP), DI // arg 1 fd 57 MOVQ p+8(FP), SI // arg 2 buf 58 MOVL n+16(FP), DX // arg 3 count 59 MOVL $(0x2000000+3), AX // syscall entry 60 SYSCALL 61 JCC 2(PC) 62 MOVL $-1, AX 63 MOVL AX, ret+24(FP) 64 RET 65 66 TEXT runtime·write(SB),NOSPLIT,$0 67 MOVQ fd+0(FP), DI // arg 1 fd 68 MOVQ p+8(FP), SI // arg 2 buf 69 MOVL n+16(FP), DX // arg 3 count 70 MOVL $(0x2000000+4), AX // syscall entry 71 SYSCALL 72 JCC 2(PC) 73 MOVL $-1, AX 74 MOVL AX, ret+24(FP) 75 RET 76 77 TEXT runtime·raise(SB),NOSPLIT,$0 78 // Ideally we'd send the signal to the current thread, 79 // not the whole process, but that's too hard on OS X. 80 JMP runtime·raiseproc(SB) 81 82 TEXT runtime·raiseproc(SB),NOSPLIT,$24 83 MOVL $(0x2000000+20), AX // getpid 84 SYSCALL 85 MOVQ AX, DI // arg 1 - pid 86 MOVL sig+0(FP), SI // arg 2 - signal 87 MOVL $1, DX // arg 3 - posix 88 MOVL $(0x2000000+37), AX // kill 89 SYSCALL 90 RET 91 92 TEXT runtime·setitimer(SB), NOSPLIT, $0 93 MOVL mode+0(FP), DI 94 MOVQ new+8(FP), SI 95 MOVQ old+16(FP), DX 96 MOVL $(0x2000000+83), AX // syscall entry 97 SYSCALL 98 RET 99 100 TEXT runtime·madvise(SB), NOSPLIT, $0 101 MOVQ addr+0(FP), DI // arg 1 addr 102 MOVQ n+8(FP), SI // arg 2 len 103 MOVL flags+16(FP), DX // arg 3 advice 104 MOVL $(0x2000000+75), AX // syscall entry madvise 105 SYSCALL 106 // ignore failure - maybe pages are locked 107 RET 108 109 // OS X comm page time offsets 110 // http://www.opensource.apple.com/source/xnu/xnu-1699.26.8/osfmk/i386/cpu_capabilities.h 111 #define nt_tsc_base 0x50 112 #define nt_scale 0x58 113 #define nt_shift 0x5c 114 #define nt_ns_base 0x60 115 #define nt_generation 0x68 116 #define gtod_generation 0x6c 117 #define gtod_ns_base 0x70 118 #define gtod_sec_base 0x78 119 120 TEXT monotonictime<>(SB), NOSPLIT, $32 121 MOVQ $0x7fffffe00000, SI // comm page base 122 123 timeloop: 124 MOVL nt_generation(SI), R8 125 TESTL R8, R8 126 JZ timeloop 127 RDTSC 128 SHLQ $32, DX 129 ORQ DX, AX 130 MOVL nt_shift(SI), CX 131 SUBQ nt_tsc_base(SI), AX 132 SHLQ CX, AX 133 MOVL nt_scale(SI), CX 134 MULQ CX 135 SHRQ $32, AX:DX 136 ADDQ nt_ns_base(SI), AX 137 CMPL nt_generation(SI), R8 138 JNE timeloop 139 RET 140 141 TEXT nanotime<>(SB), NOSPLIT, $32 142 MOVQ $0x7fffffe00000, BP /* comm page base */ 143 // Loop trying to take a consistent snapshot 144 // of the time parameters. 145 timeloop: 146 MOVL gtod_generation(BP), R8 147 TESTL R8, R8 148 JZ systime 149 MOVL nt_generation(BP), R9 150 TESTL R9, R9 151 JZ timeloop 152 RDTSC 153 MOVQ nt_tsc_base(BP), R10 154 MOVL nt_scale(BP), R11 155 MOVQ nt_ns_base(BP), R12 156 CMPL nt_generation(BP), R9 157 JNE timeloop 158 MOVQ gtod_ns_base(BP), R13 159 MOVQ gtod_sec_base(BP), R14 160 CMPL gtod_generation(BP), R8 161 JNE timeloop 162 163 // Gathered all the data we need. Compute time. 164 // ((tsc - nt_tsc_base) * nt_scale) >> 32 + nt_ns_base - gtod_ns_base + gtod_sec_base*1e9 165 // The multiply and shift extracts the top 64 bits of the 96-bit product. 166 SHLQ $32, DX 167 ADDQ DX, AX 168 SUBQ R10, AX 169 MULQ R11 170 SHRQ $32, AX:DX 171 ADDQ R12, AX 172 SUBQ R13, AX 173 IMULQ $1000000000, R14 174 ADDQ R14, AX 175 RET 176 177 systime: 178 // Fall back to system call (usually first call in this thread). 179 MOVQ SP, DI 180 MOVQ $0, SI 181 MOVQ $0, DX // required as of Sierra; Issue 16570 182 MOVL $(0x2000000+116), AX // gettimeofday 183 SYSCALL 184 CMPQ AX, $0 185 JNE inreg 186 MOVQ 0(SP), AX 187 MOVL 8(SP), DX 188 inreg: 189 // sec is in AX, usec in DX 190 // return nsec in AX 191 IMULQ $1000000000, AX 192 IMULQ $1000, DX 193 ADDQ DX, AX 194 RET 195 196 TEXT runtime·nanotime(SB),NOSPLIT,$0-8 197 CALL monotonictime<>(SB) 198 MOVQ AX, ret+0(FP) 199 RET 200 201 // func walltime() (sec int64, nsec int32) 202 TEXT runtime·walltime(SB),NOSPLIT,$0-12 203 CALL nanotime<>(SB) 204 205 // generated code for 206 // func f(x uint64) (uint64, uint64) { return x/1000000000, x%100000000 } 207 // adapted to reduce duplication 208 MOVQ AX, CX 209 MOVQ $1360296554856532783, AX 210 MULQ CX 211 ADDQ CX, DX 212 RCRQ $1, DX 213 SHRQ $29, DX 214 MOVQ DX, sec+0(FP) 215 IMULQ $1000000000, DX 216 SUBQ DX, CX 217 MOVL CX, nsec+8(FP) 218 RET 219 220 TEXT runtime·sigprocmask(SB),NOSPLIT,$0 221 MOVL how+0(FP), DI 222 MOVQ new+8(FP), SI 223 MOVQ old+16(FP), DX 224 MOVL $(0x2000000+329), AX // pthread_sigmask (on OS X, sigprocmask==entire process) 225 SYSCALL 226 JCC 2(PC) 227 MOVL $0xf1, 0xf1 // crash 228 RET 229 230 TEXT runtime·sigaction(SB),NOSPLIT,$0-24 231 MOVL mode+0(FP), DI // arg 1 sig 232 MOVQ new+8(FP), SI // arg 2 act 233 MOVQ old+16(FP), DX // arg 3 oact 234 MOVQ old+16(FP), CX // arg 3 oact 235 MOVQ old+16(FP), R10 // arg 3 oact 236 MOVL $(0x2000000+46), AX // syscall entry 237 SYSCALL 238 JCC 2(PC) 239 MOVL $0xf1, 0xf1 // crash 240 RET 241 242 TEXT runtime·sigfwd(SB),NOSPLIT,$0-32 243 MOVQ fn+0(FP), AX 244 MOVL sig+8(FP), DI 245 MOVQ info+16(FP), SI 246 MOVQ ctx+24(FP), DX 247 PUSHQ BP 248 MOVQ SP, BP 249 ANDQ $~15, SP // alignment for x86_64 ABI 250 CALL AX 251 MOVQ BP, SP 252 POPQ BP 253 RET 254 255 TEXT runtime·sigtramp(SB),NOSPLIT,$40 256 MOVL SI, 24(SP) // save infostyle for sigreturn below 257 MOVQ R8, 32(SP) // save ctx 258 MOVL DX, 0(SP) // sig 259 MOVQ CX, 8(SP) // info 260 MOVQ R8, 16(SP) // ctx 261 MOVQ $runtime·sigtrampgo(SB), AX 262 CALL AX 263 MOVQ 32(SP), DI // ctx 264 MOVL 24(SP), SI // infostyle 265 MOVL $(0x2000000+184), AX 266 SYSCALL 267 INT $3 // not reached 268 269 TEXT runtime·mmap(SB),NOSPLIT,$0 270 MOVQ addr+0(FP), DI // arg 1 addr 271 MOVQ n+8(FP), SI // arg 2 len 272 MOVL prot+16(FP), DX // arg 3 prot 273 MOVL flags+20(FP), R10 // arg 4 flags 274 MOVL fd+24(FP), R8 // arg 5 fid 275 MOVL off+28(FP), R9 // arg 6 offset 276 MOVL $(0x2000000+197), AX // syscall entry 277 SYSCALL 278 MOVQ AX, ret+32(FP) 279 RET 280 281 TEXT runtime·munmap(SB),NOSPLIT,$0 282 MOVQ addr+0(FP), DI // arg 1 addr 283 MOVQ n+8(FP), SI // arg 2 len 284 MOVL $(0x2000000+73), AX // syscall entry 285 SYSCALL 286 JCC 2(PC) 287 MOVL $0xf1, 0xf1 // crash 288 RET 289 290 TEXT runtime·sigaltstack(SB),NOSPLIT,$0 291 MOVQ new+0(FP), DI 292 MOVQ old+8(FP), SI 293 MOVQ $(0x2000000+53), AX 294 SYSCALL 295 JCC 2(PC) 296 MOVL $0xf1, 0xf1 // crash 297 RET 298 299 TEXT runtime·usleep(SB),NOSPLIT,$16 300 MOVL $0, DX 301 MOVL usec+0(FP), AX 302 MOVL $1000000, CX 303 DIVL CX 304 MOVQ AX, 0(SP) // sec 305 MOVL DX, 8(SP) // usec 306 307 // select(0, 0, 0, 0, &tv) 308 MOVL $0, DI 309 MOVL $0, SI 310 MOVL $0, DX 311 MOVL $0, R10 312 MOVQ SP, R8 313 MOVL $(0x2000000+93), AX 314 SYSCALL 315 RET 316 317 // func bsdthread_create(stk, arg unsafe.Pointer, fn uintptr) int32 318 TEXT runtime·bsdthread_create(SB),NOSPLIT,$0 319 // Set up arguments to bsdthread_create system call. 320 // The ones in quotes pass through to the thread callback 321 // uninterpreted, so we can put whatever we want there. 322 MOVQ fn+16(FP), DI 323 MOVQ arg+8(FP), SI 324 MOVQ stk+0(FP), DX 325 MOVQ $0x01000000, R8 // flags = PTHREAD_START_CUSTOM 326 MOVQ $0, R9 // paranoia 327 MOVQ $0, R10 // paranoia, "pthread" 328 MOVQ $(0x2000000+360), AX // bsdthread_create 329 SYSCALL 330 JCC 4(PC) 331 NEGQ AX 332 MOVL AX, ret+24(FP) 333 RET 334 MOVL $0, AX 335 MOVL AX, ret+24(FP) 336 RET 337 338 // The thread that bsdthread_create creates starts executing here, 339 // because we registered this function using bsdthread_register 340 // at startup. 341 // DI = "pthread" 342 // SI = mach thread port 343 // DX = "func" (= fn) 344 // CX = "arg" (= m) 345 // R8 = stack 346 // R9 = flags (= 0) 347 // SP = stack - C_64_REDZONE_LEN (= stack - 128) 348 TEXT runtime·bsdthread_start(SB),NOSPLIT,$0 349 MOVQ R8, SP // empirically, SP is very wrong but R8 is right 350 351 PUSHQ DX 352 PUSHQ CX 353 PUSHQ SI 354 355 // set up thread local storage pointing at m->tls. 356 LEAQ m_tls(CX), DI 357 CALL runtime·settls(SB) 358 359 POPQ SI 360 POPQ CX 361 POPQ DX 362 363 get_tls(BX) 364 MOVQ SI, m_procid(CX) // thread port is m->procid 365 MOVQ m_g0(CX), AX 366 MOVQ AX, g(BX) 367 MOVQ CX, g_m(AX) 368 CALL runtime·stackcheck(SB) // smashes AX, CX 369 CALL DX // fn 370 CALL runtime·exit1(SB) 371 RET 372 373 // func bsdthread_register() int32 374 // registers callbacks for threadstart (see bsdthread_create above 375 // and wqthread and pthsize (not used). returns 0 on success. 376 TEXT runtime·bsdthread_register(SB),NOSPLIT,$0 377 MOVQ $runtime·bsdthread_start(SB), DI // threadstart 378 MOVQ $0, SI // wqthread, not used by us 379 MOVQ $0, DX // pthsize, not used by us 380 MOVQ $0, R10 // dummy_value [sic] 381 MOVQ $0, R8 // targetconc_ptr 382 MOVQ $0, R9 // dispatchqueue_offset 383 MOVQ $(0x2000000+366), AX // bsdthread_register 384 SYSCALL 385 JCC 4(PC) 386 NEGQ AX 387 MOVL AX, ret+0(FP) 388 RET 389 MOVL $0, AX 390 MOVL AX, ret+0(FP) 391 RET 392 393 // Mach system calls use 0x1000000 instead of the BSD's 0x2000000. 394 395 // func mach_msg_trap(h unsafe.Pointer, op int32, send_size, rcv_size, rcv_name, timeout, notify uint32) int32 396 TEXT runtime·mach_msg_trap(SB),NOSPLIT,$0 397 MOVQ h+0(FP), DI 398 MOVL op+8(FP), SI 399 MOVL send_size+12(FP), DX 400 MOVL rcv_size+16(FP), R10 401 MOVL rcv_name+20(FP), R8 402 MOVL timeout+24(FP), R9 403 MOVL notify+28(FP), R11 404 PUSHQ R11 // seventh arg, on stack 405 MOVL $(0x1000000+31), AX // mach_msg_trap 406 SYSCALL 407 POPQ R11 408 MOVL AX, ret+32(FP) 409 RET 410 411 TEXT runtime·mach_task_self(SB),NOSPLIT,$0 412 MOVL $(0x1000000+28), AX // task_self_trap 413 SYSCALL 414 MOVL AX, ret+0(FP) 415 RET 416 417 TEXT runtime·mach_thread_self(SB),NOSPLIT,$0 418 MOVL $(0x1000000+27), AX // thread_self_trap 419 SYSCALL 420 MOVL AX, ret+0(FP) 421 RET 422 423 TEXT runtime·mach_reply_port(SB),NOSPLIT,$0 424 MOVL $(0x1000000+26), AX // mach_reply_port 425 SYSCALL 426 MOVL AX, ret+0(FP) 427 RET 428 429 // Mach provides trap versions of the semaphore ops, 430 // instead of requiring the use of RPC. 431 432 // func mach_semaphore_wait(sema uint32) int32 433 TEXT runtime·mach_semaphore_wait(SB),NOSPLIT,$0 434 MOVL sema+0(FP), DI 435 MOVL $(0x1000000+36), AX // semaphore_wait_trap 436 SYSCALL 437 MOVL AX, ret+8(FP) 438 RET 439 440 // func mach_semaphore_timedwait(sema, sec, nsec uint32) int32 441 TEXT runtime·mach_semaphore_timedwait(SB),NOSPLIT,$0 442 MOVL sema+0(FP), DI 443 MOVL sec+4(FP), SI 444 MOVL nsec+8(FP), DX 445 MOVL $(0x1000000+38), AX // semaphore_timedwait_trap 446 SYSCALL 447 MOVL AX, ret+16(FP) 448 RET 449 450 // func mach_semaphore_signal(sema uint32) int32 451 TEXT runtime·mach_semaphore_signal(SB),NOSPLIT,$0 452 MOVL sema+0(FP), DI 453 MOVL $(0x1000000+33), AX // semaphore_signal_trap 454 SYSCALL 455 MOVL AX, ret+8(FP) 456 RET 457 458 // func mach_semaphore_signal_all(sema uint32) int32 459 TEXT runtime·mach_semaphore_signal_all(SB),NOSPLIT,$0 460 MOVL sema+0(FP), DI 461 MOVL $(0x1000000+34), AX // semaphore_signal_all_trap 462 SYSCALL 463 MOVL AX, ret+8(FP) 464 RET 465 466 // set tls base to DI 467 TEXT runtime·settls(SB),NOSPLIT,$32 468 /* 469 * Same as in sys_darwin_386.s:/ugliness, different constant. 470 * See cgo/gcc_darwin_amd64.c for the derivation 471 * of the constant. 472 */ 473 SUBQ $0x8a0, DI 474 475 MOVL $(0x3000000+3), AX // thread_fast_set_cthread_self - machdep call #3 476 SYSCALL 477 RET 478 479 TEXT runtime·sysctl(SB),NOSPLIT,$0 480 MOVQ mib+0(FP), DI 481 MOVL miblen+8(FP), SI 482 MOVQ out+16(FP), DX 483 MOVQ size+24(FP), R10 484 MOVQ dst+32(FP), R8 485 MOVQ ndst+40(FP), R9 486 MOVL $(0x2000000+202), AX // syscall entry 487 SYSCALL 488 JCC 4(PC) 489 NEGQ AX 490 MOVL AX, ret+48(FP) 491 RET 492 MOVL $0, AX 493 MOVL AX, ret+48(FP) 494 RET 495 496 // func kqueue() int32 497 TEXT runtime·kqueue(SB),NOSPLIT,$0 498 MOVQ $0, DI 499 MOVQ $0, SI 500 MOVQ $0, DX 501 MOVL $(0x2000000+362), AX 502 SYSCALL 503 JCC 2(PC) 504 NEGQ AX 505 MOVL AX, ret+0(FP) 506 RET 507 508 // func kevent(kq int32, ch *keventt, nch int32, ev *keventt, nev int32, ts *timespec) int32 509 TEXT runtime·kevent(SB),NOSPLIT,$0 510 MOVL kq+0(FP), DI 511 MOVQ ch+8(FP), SI 512 MOVL nch+16(FP), DX 513 MOVQ ev+24(FP), R10 514 MOVL nev+32(FP), R8 515 MOVQ ts+40(FP), R9 516 MOVL $(0x2000000+363), AX 517 SYSCALL 518 JCC 2(PC) 519 NEGQ AX 520 MOVL AX, ret+48(FP) 521 RET 522 523 // func closeonexec(fd int32) 524 TEXT runtime·closeonexec(SB),NOSPLIT,$0 525 MOVL fd+0(FP), DI // fd 526 MOVQ $2, SI // F_SETFD 527 MOVQ $1, DX // FD_CLOEXEC 528 MOVL $(0x2000000+92), AX // fcntl 529 SYSCALL 530 RET