rsc.io/go@v0.0.0-20150416155037-e040fd465409/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 nanotime<>(SB), NOSPLIT, $32
   121  	MOVQ	$0x7fffffe00000, BP	/* comm page base */
   122  	// Loop trying to take a consistent snapshot
   123  	// of the time parameters.
   124  timeloop:
   125  	MOVL	gtod_generation(BP), R8
   126  	TESTL	R8, R8
   127  	JZ	systime
   128  	MOVL	nt_generation(BP), R9
   129  	TESTL	R9, R9
   130  	JZ	timeloop
   131  	RDTSC
   132  	MOVQ	nt_tsc_base(BP), R10
   133  	MOVL	nt_scale(BP), R11
   134  	MOVQ	nt_ns_base(BP), R12
   135  	CMPL	nt_generation(BP), R9
   136  	JNE	timeloop
   137  	MOVQ	gtod_ns_base(BP), R13
   138  	MOVQ	gtod_sec_base(BP), R14
   139  	CMPL	gtod_generation(BP), R8
   140  	JNE	timeloop
   141  
   142  	// Gathered all the data we need. Compute time.
   143  	//	((tsc - nt_tsc_base) * nt_scale) >> 32 + nt_ns_base - gtod_ns_base + gtod_sec_base*1e9
   144  	// The multiply and shift extracts the top 64 bits of the 96-bit product.
   145  	SHLQ	$32, DX
   146  	ADDQ	DX, AX
   147  	SUBQ	R10, AX
   148  	MULQ	R11
   149  	SHRQ	$32, AX:DX
   150  	ADDQ	R12, AX
   151  	SUBQ	R13, AX
   152  	IMULQ	$1000000000, R14
   153  	ADDQ	R14, AX
   154  	RET
   155  
   156  systime:
   157  	// Fall back to system call (usually first call in this thread).
   158  	MOVQ	SP, DI	// must be non-nil, unused
   159  	MOVQ	$0, SI
   160  	MOVL	$(0x2000000+116), AX
   161  	SYSCALL
   162  	// sec is in AX, usec in DX
   163  	// return nsec in AX
   164  	IMULQ	$1000000000, AX
   165  	IMULQ	$1000, DX
   166  	ADDQ	DX, AX
   167  	RET
   168  
   169  TEXT runtime·nanotime(SB),NOSPLIT,$0-8
   170  	CALL	nanotime<>(SB)
   171  	MOVQ	AX, ret+0(FP)
   172  	RET
   173  
   174  // func now() (sec int64, nsec int32)
   175  TEXT time·now(SB),NOSPLIT,$0-12
   176  	CALL	nanotime<>(SB)
   177  
   178  	// generated code for
   179  	//	func f(x uint64) (uint64, uint64) { return x/1000000000, x%100000000 }
   180  	// adapted to reduce duplication
   181  	MOVQ	AX, CX
   182  	MOVQ	$1360296554856532783, AX
   183  	MULQ	CX
   184  	ADDQ	CX, DX
   185  	RCRQ	$1, DX
   186  	SHRQ	$29, DX
   187  	MOVQ	DX, sec+0(FP)
   188  	IMULQ	$1000000000, DX
   189  	SUBQ	DX, CX
   190  	MOVL	CX, nsec+8(FP)
   191  	RET
   192  
   193  TEXT runtime·sigprocmask(SB),NOSPLIT,$0
   194  	MOVL	sig+0(FP), DI
   195  	MOVQ	new+8(FP), SI
   196  	MOVQ	old+16(FP), DX
   197  	MOVL	$(0x2000000+329), AX  // pthread_sigmask (on OS X, sigprocmask==entire process)
   198  	SYSCALL
   199  	JCC	2(PC)
   200  	MOVL	$0xf1, 0xf1  // crash
   201  	RET
   202  
   203  TEXT runtime·sigaction(SB),NOSPLIT,$0
   204  	MOVL	mode+0(FP), DI		// arg 1 sig
   205  	MOVQ	new+8(FP), SI		// arg 2 act
   206  	MOVQ	old+16(FP), DX		// arg 3 oact
   207  	MOVQ	old+16(FP), CX		// arg 3 oact
   208  	MOVQ	old+16(FP), R10		// arg 3 oact
   209  	MOVL	$(0x2000000+46), AX	// syscall entry
   210  	SYSCALL
   211  	JCC	2(PC)
   212  	MOVL	$0xf1, 0xf1  // crash
   213  	RET
   214  
   215  TEXT runtime·sigtramp(SB),NOSPLIT,$64
   216  	get_tls(BX)
   217  
   218  	MOVQ	R8, 32(SP)	// save ucontext
   219  	MOVQ	SI, 40(SP)	// save infostyle
   220  
   221  	// check that g exists
   222  	MOVQ	g(BX), R10
   223  	CMPQ	R10, $0
   224  	JNE	5(PC)
   225  	MOVL	DX, 0(SP)
   226  	MOVQ	$runtime·badsignal(SB), AX
   227  	CALL	AX
   228  	JMP 	ret
   229  
   230  	// save g
   231  	MOVQ	R10, 48(SP)
   232  
   233  	// g = m->gsignal
   234  	MOVQ	g_m(R10), BP
   235  	MOVQ	m_gsignal(BP), BP
   236  	MOVQ	BP, g(BX)
   237  
   238  	MOVL	DX, 0(SP)
   239  	MOVQ	CX, 8(SP)
   240  	MOVQ	R8, 16(SP)
   241  	MOVQ	R10, 24(SP)
   242  
   243  	CALL	DI
   244  
   245  	// restore g
   246  	get_tls(BX)
   247  	MOVQ	48(SP), R10
   248  	MOVQ	R10, g(BX)
   249  
   250  ret:
   251  	// call sigreturn
   252  	MOVL	$(0x2000000+184), AX	// sigreturn(ucontext, infostyle)
   253  	MOVQ	32(SP), DI	// saved ucontext
   254  	MOVQ	40(SP), SI	// saved infostyle
   255  	SYSCALL
   256  	INT $3	// not reached
   257  
   258  TEXT runtime·mmap(SB),NOSPLIT,$0
   259  	MOVQ	addr+0(FP), DI		// arg 1 addr
   260  	MOVQ	n+8(FP), SI		// arg 2 len
   261  	MOVL	prot+16(FP), DX		// arg 3 prot
   262  	MOVL	flags+20(FP), R10		// arg 4 flags
   263  	MOVL	fd+24(FP), R8		// arg 5 fid
   264  	MOVL	off+28(FP), R9		// arg 6 offset
   265  	MOVL	$(0x2000000+197), AX	// syscall entry
   266  	SYSCALL
   267  	MOVQ	AX, ret+32(FP)
   268  	RET
   269  
   270  TEXT runtime·munmap(SB),NOSPLIT,$0
   271  	MOVQ	addr+0(FP), DI		// arg 1 addr
   272  	MOVQ	n+8(FP), SI		// arg 2 len
   273  	MOVL	$(0x2000000+73), AX	// syscall entry
   274  	SYSCALL
   275  	JCC	2(PC)
   276  	MOVL	$0xf1, 0xf1  // crash
   277  	RET
   278  
   279  TEXT runtime·sigaltstack(SB),NOSPLIT,$0
   280  	MOVQ	new+8(SP), DI
   281  	MOVQ	old+16(SP), SI
   282  	MOVQ	$(0x2000000+53), AX
   283  	SYSCALL
   284  	JCC	2(PC)
   285  	MOVL	$0xf1, 0xf1  // crash
   286  	RET
   287  
   288  TEXT runtime·usleep(SB),NOSPLIT,$16
   289  	MOVL	$0, DX
   290  	MOVL	usec+0(FP), AX
   291  	MOVL	$1000000, CX
   292  	DIVL	CX
   293  	MOVQ	AX, 0(SP)  // sec
   294  	MOVL	DX, 8(SP)  // usec
   295  
   296  	// select(0, 0, 0, 0, &tv)
   297  	MOVL	$0, DI
   298  	MOVL	$0, SI
   299  	MOVL	$0, DX
   300  	MOVL	$0, R10
   301  	MOVQ	SP, R8
   302  	MOVL	$(0x2000000+93), AX
   303  	SYSCALL
   304  	RET
   305  
   306  // func bsdthread_create(stk, arg unsafe.Pointer, fn uintptr) int32
   307  TEXT runtime·bsdthread_create(SB),NOSPLIT,$0
   308  	// Set up arguments to bsdthread_create system call.
   309  	// The ones in quotes pass through to the thread callback
   310  	// uninterpreted, so we can put whatever we want there.
   311  	MOVQ	fn+24(SP),   DI
   312  	MOVQ	arg+16(SP),  SI
   313  	MOVQ	stk+8(SP),   DX
   314  	MOVQ	$0x01000000, R8  // flags = PTHREAD_START_CUSTOM
   315  	MOVQ	$0,          R9  // paranoia
   316  	MOVQ	$0,          R10 // paranoia, "pthread"
   317  	MOVQ	$(0x2000000+360), AX	// bsdthread_create
   318  	SYSCALL
   319  	JCC 4(PC)
   320  	NEGQ	AX
   321  	MOVL	AX, ret+32(FP)
   322  	RET
   323  	MOVL	$0, AX
   324  	MOVL	AX, ret+32(FP)
   325  	RET
   326  
   327  // The thread that bsdthread_create creates starts executing here,
   328  // because we registered this function using bsdthread_register
   329  // at startup.
   330  //	DI = "pthread"
   331  //	SI = mach thread port
   332  //	DX = "func" (= fn)
   333  //	CX = "arg" (= m)
   334  //	R8 = stack
   335  //	R9 = flags (= 0)
   336  //	SP = stack - C_64_REDZONE_LEN (= stack - 128)
   337  TEXT runtime·bsdthread_start(SB),NOSPLIT,$0
   338  	MOVQ	R8, SP		// empirically, SP is very wrong but R8 is right
   339  
   340  	PUSHQ	DX
   341  	PUSHQ	CX
   342  	PUSHQ	SI
   343  
   344  	// set up thread local storage pointing at m->tls.
   345  	LEAQ	m_tls(CX), DI
   346  	CALL	runtime·settls(SB)
   347  
   348  	POPQ	SI
   349  	POPQ	CX
   350  	POPQ	DX
   351  
   352  	get_tls(BX)
   353  	MOVQ	SI, m_procid(CX)	// thread port is m->procid
   354  	MOVQ	m_g0(CX), AX
   355  	MOVQ	AX, g(BX)
   356  	MOVQ	CX, g_m(AX)
   357  	CALL	runtime·stackcheck(SB)	// smashes AX, CX
   358  	CALL	DX	// fn
   359  	CALL	runtime·exit1(SB)
   360  	RET
   361  
   362  // func bsdthread_register() int32
   363  // registers callbacks for threadstart (see bsdthread_create above
   364  // and wqthread and pthsize (not used).  returns 0 on success.
   365  TEXT runtime·bsdthread_register(SB),NOSPLIT,$0
   366  	MOVQ	$runtime·bsdthread_start(SB), DI	// threadstart
   367  	MOVQ	$0, SI	// wqthread, not used by us
   368  	MOVQ	$0, DX	// pthsize, not used by us
   369  	MOVQ	$0, R10	// dummy_value [sic]
   370  	MOVQ	$0, R8	// targetconc_ptr
   371  	MOVQ	$0, R9	// dispatchqueue_offset
   372  	MOVQ	$(0x2000000+366), AX	// bsdthread_register
   373  	SYSCALL
   374  	JCC 4(PC)
   375  	NEGQ	AX
   376  	MOVL	AX, ret+0(FP)
   377  	RET
   378  	MOVL	$0, AX
   379  	MOVL	AX, ret+0(FP)
   380  	RET
   381  
   382  // Mach system calls use 0x1000000 instead of the BSD's 0x2000000.
   383  
   384  // func mach_msg_trap(h unsafe.Pointer, op int32, send_size, rcv_size, rcv_name, timeout, notify uint32) int32
   385  TEXT runtime·mach_msg_trap(SB),NOSPLIT,$0
   386  	MOVQ	h+0(FP), DI
   387  	MOVL	op+8(FP), SI
   388  	MOVL	send_size+12(FP), DX
   389  	MOVL	rcv_size+16(FP), R10
   390  	MOVL	rcv_name+20(FP), R8
   391  	MOVL	timeout+24(FP), R9
   392  	MOVL	notify+28(FP), R11
   393  	PUSHQ	R11	// seventh arg, on stack
   394  	MOVL	$(0x1000000+31), AX	// mach_msg_trap
   395  	SYSCALL
   396  	POPQ	R11
   397  	MOVL	AX, ret+32(FP)
   398  	RET
   399  
   400  TEXT runtime·mach_task_self(SB),NOSPLIT,$0
   401  	MOVL	$(0x1000000+28), AX	// task_self_trap
   402  	SYSCALL
   403  	MOVL	AX, ret+0(FP)
   404  	RET
   405  
   406  TEXT runtime·mach_thread_self(SB),NOSPLIT,$0
   407  	MOVL	$(0x1000000+27), AX	// thread_self_trap
   408  	SYSCALL
   409  	MOVL	AX, ret+0(FP)
   410  	RET
   411  
   412  TEXT runtime·mach_reply_port(SB),NOSPLIT,$0
   413  	MOVL	$(0x1000000+26), AX	// mach_reply_port
   414  	SYSCALL
   415  	MOVL	AX, ret+0(FP)
   416  	RET
   417  
   418  // Mach provides trap versions of the semaphore ops,
   419  // instead of requiring the use of RPC.
   420  
   421  // func mach_semaphore_wait(sema uint32) int32
   422  TEXT runtime·mach_semaphore_wait(SB),NOSPLIT,$0
   423  	MOVL	sema+0(FP), DI
   424  	MOVL	$(0x1000000+36), AX	// semaphore_wait_trap
   425  	SYSCALL
   426  	MOVL	AX, ret+8(FP)
   427  	RET
   428  
   429  // func mach_semaphore_timedwait(sema, sec, nsec uint32) int32
   430  TEXT runtime·mach_semaphore_timedwait(SB),NOSPLIT,$0
   431  	MOVL	sema+0(FP), DI
   432  	MOVL	sec+4(FP), SI
   433  	MOVL	nsec+8(FP), DX
   434  	MOVL	$(0x1000000+38), AX	// semaphore_timedwait_trap
   435  	SYSCALL
   436  	MOVL	AX, ret+16(FP)
   437  	RET
   438  
   439  // func mach_semaphore_signal(sema uint32) int32
   440  TEXT runtime·mach_semaphore_signal(SB),NOSPLIT,$0
   441  	MOVL	sema+0(FP), DI
   442  	MOVL	$(0x1000000+33), AX	// semaphore_signal_trap
   443  	SYSCALL
   444  	MOVL	AX, ret+8(FP)
   445  	RET
   446  
   447  // func mach_semaphore_signal_all(sema uint32) int32
   448  TEXT runtime·mach_semaphore_signal_all(SB),NOSPLIT,$0
   449  	MOVL	sema+0(FP), DI
   450  	MOVL	$(0x1000000+34), AX	// semaphore_signal_all_trap
   451  	SYSCALL
   452  	MOVL	AX, ret+8(FP)
   453  	RET
   454  
   455  // set tls base to DI
   456  TEXT runtime·settls(SB),NOSPLIT,$32
   457  	/*
   458  	* Same as in sys_darwin_386.s:/ugliness, different constant.
   459  	* See cgo/gcc_darwin_amd64.c for the derivation
   460  	* of the constant.
   461  	*/
   462  	SUBQ $0x8a0, DI
   463  
   464  	MOVL	$(0x3000000+3), AX	// thread_fast_set_cthread_self - machdep call #3
   465  	SYSCALL
   466  	RET
   467  
   468  TEXT runtime·sysctl(SB),NOSPLIT,$0
   469  	MOVQ	mib+0(FP), DI
   470  	MOVL	miblen+8(FP), SI
   471  	MOVQ	out+16(FP), DX
   472  	MOVQ	size+24(FP), R10
   473  	MOVQ	dst+32(FP), R8
   474  	MOVQ	ndst+40(FP), R9
   475  	MOVL	$(0x2000000+202), AX	// syscall entry
   476  	SYSCALL
   477  	JCC 4(PC)
   478  	NEGQ	AX
   479  	MOVL	AX, ret+48(FP)
   480  	RET
   481  	MOVL	$0, AX
   482  	MOVL	AX, ret+48(FP)
   483  	RET
   484  
   485  // func kqueue() int32
   486  TEXT runtime·kqueue(SB),NOSPLIT,$0
   487  	MOVQ    $0, DI
   488  	MOVQ    $0, SI
   489  	MOVQ    $0, DX
   490  	MOVL	$(0x2000000+362), AX
   491  	SYSCALL
   492  	JCC	2(PC)
   493  	NEGQ	AX
   494  	MOVL	AX, ret+0(FP)
   495  	RET
   496  
   497  // func kevent(kq int32, ch *keventt, nch int32, ev *keventt, nev int32, ts *timespec) int32
   498  TEXT runtime·kevent(SB),NOSPLIT,$0
   499  	MOVL    kq+0(FP), DI
   500  	MOVQ    ch+8(FP), SI
   501  	MOVL    nch+16(FP), DX
   502  	MOVQ    ev+24(FP), R10
   503  	MOVL    nev+32(FP), R8
   504  	MOVQ    ts+40(FP), R9
   505  	MOVL	$(0x2000000+363), AX
   506  	SYSCALL
   507  	JCC	2(PC)
   508  	NEGQ	AX
   509  	MOVL	AX, ret+48(FP)
   510  	RET
   511  
   512  // func closeonexec(fd int32)
   513  TEXT runtime·closeonexec(SB),NOSPLIT,$0
   514  	MOVL    fd+0(FP), DI  // fd
   515  	MOVQ    $2, SI  // F_SETFD
   516  	MOVQ    $1, DX  // FD_CLOEXEC
   517  	MOVL	$(0x2000000+92), AX  // fcntl
   518  	SYSCALL
   519  	RET