Black Lives Matter. Support the Equal Justice Initiative.

Text file src/runtime/sys_linux_amd64.s

Documentation: runtime

     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, Linux
     7  //
     8  
     9  #include "go_asm.h"
    10  #include "go_tls.h"
    11  #include "textflag.h"
    12  
    13  #define AT_FDCWD -100
    14  
    15  #define SYS_read		0
    16  #define SYS_write		1
    17  #define SYS_close		3
    18  #define SYS_mmap		9
    19  #define SYS_munmap		11
    20  #define SYS_brk 		12
    21  #define SYS_rt_sigaction	13
    22  #define SYS_rt_sigprocmask	14
    23  #define SYS_rt_sigreturn	15
    24  #define SYS_pipe		22
    25  #define SYS_sched_yield 	24
    26  #define SYS_mincore		27
    27  #define SYS_madvise		28
    28  #define SYS_nanosleep		35
    29  #define SYS_setittimer		38
    30  #define SYS_getpid		39
    31  #define SYS_socket		41
    32  #define SYS_connect		42
    33  #define SYS_clone		56
    34  #define SYS_exit		60
    35  #define SYS_kill		62
    36  #define SYS_fcntl		72
    37  #define SYS_sigaltstack 	131
    38  #define SYS_arch_prctl		158
    39  #define SYS_gettid		186
    40  #define SYS_futex		202
    41  #define SYS_sched_getaffinity	204
    42  #define SYS_epoll_create	213
    43  #define SYS_clock_gettime	228
    44  #define SYS_exit_group		231
    45  #define SYS_epoll_ctl		233
    46  #define SYS_tgkill		234
    47  #define SYS_openat		257
    48  #define SYS_faccessat		269
    49  #define SYS_epoll_pwait		281
    50  #define SYS_epoll_create1	291
    51  #define SYS_pipe2		293
    52  
    53  TEXT runtime·exit(SB),NOSPLIT,$0-4
    54  	MOVL	code+0(FP), DI
    55  	MOVL	$SYS_exit_group, AX
    56  	SYSCALL
    57  	RET
    58  
    59  // func exitThread(wait *uint32)
    60  TEXT runtime·exitThread(SB),NOSPLIT,$0-8
    61  	MOVQ	wait+0(FP), AX
    62  	// We're done using the stack.
    63  	MOVL	$0, (AX)
    64  	MOVL	$0, DI	// exit code
    65  	MOVL	$SYS_exit, AX
    66  	SYSCALL
    67  	// We may not even have a stack any more.
    68  	INT	$3
    69  	JMP	0(PC)
    70  
    71  TEXT runtime·open(SB),NOSPLIT,$0-20
    72  	// This uses openat instead of open, because Android O blocks open.
    73  	MOVL	$AT_FDCWD, DI // AT_FDCWD, so this acts like open
    74  	MOVQ	name+0(FP), SI
    75  	MOVL	mode+8(FP), DX
    76  	MOVL	perm+12(FP), R10
    77  	MOVL	$SYS_openat, AX
    78  	SYSCALL
    79  	CMPQ	AX, $0xfffffffffffff001
    80  	JLS	2(PC)
    81  	MOVL	$-1, AX
    82  	MOVL	AX, ret+16(FP)
    83  	RET
    84  
    85  TEXT runtime·closefd(SB),NOSPLIT,$0-12
    86  	MOVL	fd+0(FP), DI
    87  	MOVL	$SYS_close, AX
    88  	SYSCALL
    89  	CMPQ	AX, $0xfffffffffffff001
    90  	JLS	2(PC)
    91  	MOVL	$-1, AX
    92  	MOVL	AX, ret+8(FP)
    93  	RET
    94  
    95  TEXT runtime·write1(SB),NOSPLIT,$0-28
    96  	MOVQ	fd+0(FP), DI
    97  	MOVQ	p+8(FP), SI
    98  	MOVL	n+16(FP), DX
    99  	MOVL	$SYS_write, AX
   100  	SYSCALL
   101  	MOVL	AX, ret+24(FP)
   102  	RET
   103  
   104  TEXT runtime·read(SB),NOSPLIT,$0-28
   105  	MOVL	fd+0(FP), DI
   106  	MOVQ	p+8(FP), SI
   107  	MOVL	n+16(FP), DX
   108  	MOVL	$SYS_read, AX
   109  	SYSCALL
   110  	MOVL	AX, ret+24(FP)
   111  	RET
   112  
   113  // func pipe() (r, w int32, errno int32)
   114  TEXT runtime·pipe(SB),NOSPLIT,$0-12
   115  	LEAQ	r+0(FP), DI
   116  	MOVL	$SYS_pipe, AX
   117  	SYSCALL
   118  	MOVL	AX, errno+8(FP)
   119  	RET
   120  
   121  // func pipe2(flags int32) (r, w int32, errno int32)
   122  TEXT runtime·pipe2(SB),NOSPLIT,$0-20
   123  	LEAQ	r+8(FP), DI
   124  	MOVL	flags+0(FP), SI
   125  	MOVL	$SYS_pipe2, AX
   126  	SYSCALL
   127  	MOVL	AX, errno+16(FP)
   128  	RET
   129  
   130  TEXT runtime·usleep(SB),NOSPLIT,$16
   131  	MOVL	$0, DX
   132  	MOVL	usec+0(FP), AX
   133  	MOVL	$1000000, CX
   134  	DIVL	CX
   135  	MOVQ	AX, 0(SP)
   136  	MOVL	$1000, AX	// usec to nsec
   137  	MULL	DX
   138  	MOVQ	AX, 8(SP)
   139  
   140  	// nanosleep(&ts, 0)
   141  	MOVQ	SP, DI
   142  	MOVL	$0, SI
   143  	MOVL	$SYS_nanosleep, AX
   144  	SYSCALL
   145  	RET
   146  
   147  TEXT runtime·gettid(SB),NOSPLIT,$0-4
   148  	MOVL	$SYS_gettid, AX
   149  	SYSCALL
   150  	MOVL	AX, ret+0(FP)
   151  	RET
   152  
   153  TEXT runtime·raise(SB),NOSPLIT,$0
   154  	MOVL	$SYS_getpid, AX
   155  	SYSCALL
   156  	MOVL	AX, R12
   157  	MOVL	$SYS_gettid, AX
   158  	SYSCALL
   159  	MOVL	AX, SI	// arg 2 tid
   160  	MOVL	R12, DI	// arg 1 pid
   161  	MOVL	sig+0(FP), DX	// arg 3
   162  	MOVL	$SYS_tgkill, AX
   163  	SYSCALL
   164  	RET
   165  
   166  TEXT runtime·raiseproc(SB),NOSPLIT,$0
   167  	MOVL	$SYS_getpid, AX
   168  	SYSCALL
   169  	MOVL	AX, DI	// arg 1 pid
   170  	MOVL	sig+0(FP), SI	// arg 2
   171  	MOVL	$SYS_kill, AX
   172  	SYSCALL
   173  	RET
   174  
   175  TEXT ·getpid(SB),NOSPLIT,$0-8
   176  	MOVL	$SYS_getpid, AX
   177  	SYSCALL
   178  	MOVQ	AX, ret+0(FP)
   179  	RET
   180  
   181  TEXT ·tgkill(SB),NOSPLIT,$0
   182  	MOVQ	tgid+0(FP), DI
   183  	MOVQ	tid+8(FP), SI
   184  	MOVQ	sig+16(FP), DX
   185  	MOVL	$SYS_tgkill, AX
   186  	SYSCALL
   187  	RET
   188  
   189  TEXT runtime·setitimer(SB),NOSPLIT,$0-24
   190  	MOVL	mode+0(FP), DI
   191  	MOVQ	new+8(FP), SI
   192  	MOVQ	old+16(FP), DX
   193  	MOVL	$SYS_setittimer, AX
   194  	SYSCALL
   195  	RET
   196  
   197  TEXT runtime·mincore(SB),NOSPLIT,$0-28
   198  	MOVQ	addr+0(FP), DI
   199  	MOVQ	n+8(FP), SI
   200  	MOVQ	dst+16(FP), DX
   201  	MOVL	$SYS_mincore, AX
   202  	SYSCALL
   203  	MOVL	AX, ret+24(FP)
   204  	RET
   205  
   206  // func walltime1() (sec int64, nsec int32)
   207  // non-zero frame-size means bp is saved and restored
   208  TEXT runtime·walltime1(SB),NOSPLIT,$16-12
   209  	// We don't know how much stack space the VDSO code will need,
   210  	// so switch to g0.
   211  	// In particular, a kernel configured with CONFIG_OPTIMIZE_INLINING=n
   212  	// and hardening can use a full page of stack space in gettime_sym
   213  	// due to stack probes inserted to avoid stack/heap collisions.
   214  	// See issue #20427.
   215  
   216  	MOVQ	SP, R12	// Save old SP; R12 unchanged by C code.
   217  
   218  	get_tls(CX)
   219  	MOVQ	g(CX), AX
   220  	MOVQ	g_m(AX), BX // BX unchanged by C code.
   221  
   222  	// Set vdsoPC and vdsoSP for SIGPROF traceback.
   223  	// Save the old values on stack and restore them on exit,
   224  	// so this function is reentrant.
   225  	MOVQ	m_vdsoPC(BX), CX
   226  	MOVQ	m_vdsoSP(BX), DX
   227  	MOVQ	CX, 0(SP)
   228  	MOVQ	DX, 8(SP)
   229  
   230  	LEAQ	sec+0(FP), DX
   231  	MOVQ	-8(DX), CX
   232  	MOVQ	CX, m_vdsoPC(BX)
   233  	MOVQ	DX, m_vdsoSP(BX)
   234  
   235  	CMPQ	AX, m_curg(BX)	// Only switch if on curg.
   236  	JNE	noswitch
   237  
   238  	MOVQ	m_g0(BX), DX
   239  	MOVQ	(g_sched+gobuf_sp)(DX), SP	// Set SP to g0 stack
   240  
   241  noswitch:
   242  	SUBQ	$16, SP		// Space for results
   243  	ANDQ	$~15, SP	// Align for C code
   244  
   245  	MOVL	$0, DI // CLOCK_REALTIME
   246  	LEAQ	0(SP), SI
   247  	MOVQ	runtime·vdsoClockgettimeSym(SB), AX
   248  	CMPQ	AX, $0
   249  	JEQ	fallback
   250  	CALL	AX
   251  ret:
   252  	MOVQ	0(SP), AX	// sec
   253  	MOVQ	8(SP), DX	// nsec
   254  	MOVQ	R12, SP		// Restore real SP
   255  	// Restore vdsoPC, vdsoSP
   256  	// We don't worry about being signaled between the two stores.
   257  	// If we are not in a signal handler, we'll restore vdsoSP to 0,
   258  	// and no one will care about vdsoPC. If we are in a signal handler,
   259  	// we cannot receive another signal.
   260  	MOVQ	8(SP), CX
   261  	MOVQ	CX, m_vdsoSP(BX)
   262  	MOVQ	0(SP), CX
   263  	MOVQ	CX, m_vdsoPC(BX)
   264  	MOVQ	AX, sec+0(FP)
   265  	MOVL	DX, nsec+8(FP)
   266  	RET
   267  fallback:
   268  	MOVQ	$SYS_clock_gettime, AX
   269  	SYSCALL
   270  	JMP ret
   271  
   272  // func nanotime1() int64
   273  TEXT runtime·nanotime1(SB),NOSPLIT,$16-8
   274  	// Switch to g0 stack. See comment above in runtime·walltime.
   275  
   276  	MOVQ	SP, R12	// Save old SP; R12 unchanged by C code.
   277  
   278  	get_tls(CX)
   279  	MOVQ	g(CX), AX
   280  	MOVQ	g_m(AX), BX // BX unchanged by C code.
   281  
   282  	// Set vdsoPC and vdsoSP for SIGPROF traceback.
   283  	// Save the old values on stack and restore them on exit,
   284  	// so this function is reentrant.
   285  	MOVQ	m_vdsoPC(BX), CX
   286  	MOVQ	m_vdsoSP(BX), DX
   287  	MOVQ	CX, 0(SP)
   288  	MOVQ	DX, 8(SP)
   289  
   290  	LEAQ	ret+0(FP), DX
   291  	MOVQ	-8(DX), CX
   292  	MOVQ	CX, m_vdsoPC(BX)
   293  	MOVQ	DX, m_vdsoSP(BX)
   294  
   295  	CMPQ	AX, m_curg(BX)	// Only switch if on curg.
   296  	JNE	noswitch
   297  
   298  	MOVQ	m_g0(BX), DX
   299  	MOVQ	(g_sched+gobuf_sp)(DX), SP	// Set SP to g0 stack
   300  
   301  noswitch:
   302  	SUBQ	$16, SP		// Space for results
   303  	ANDQ	$~15, SP	// Align for C code
   304  
   305  	MOVL	$1, DI // CLOCK_MONOTONIC
   306  	LEAQ	0(SP), SI
   307  	MOVQ	runtime·vdsoClockgettimeSym(SB), AX
   308  	CMPQ	AX, $0
   309  	JEQ	fallback
   310  	CALL	AX
   311  ret:
   312  	MOVQ	0(SP), AX	// sec
   313  	MOVQ	8(SP), DX	// nsec
   314  	MOVQ	R12, SP		// Restore real SP
   315  	// Restore vdsoPC, vdsoSP
   316  	// We don't worry about being signaled between the two stores.
   317  	// If we are not in a signal handler, we'll restore vdsoSP to 0,
   318  	// and no one will care about vdsoPC. If we are in a signal handler,
   319  	// we cannot receive another signal.
   320  	MOVQ	8(SP), CX
   321  	MOVQ	CX, m_vdsoSP(BX)
   322  	MOVQ	0(SP), CX
   323  	MOVQ	CX, m_vdsoPC(BX)
   324  	// sec is in AX, nsec in DX
   325  	// return nsec in AX
   326  	IMULQ	$1000000000, AX
   327  	ADDQ	DX, AX
   328  	MOVQ	AX, ret+0(FP)
   329  	RET
   330  fallback:
   331  	MOVQ	$SYS_clock_gettime, AX
   332  	SYSCALL
   333  	JMP	ret
   334  
   335  TEXT runtime·rtsigprocmask(SB),NOSPLIT,$0-28
   336  	MOVL	how+0(FP), DI
   337  	MOVQ	new+8(FP), SI
   338  	MOVQ	old+16(FP), DX
   339  	MOVL	size+24(FP), R10
   340  	MOVL	$SYS_rt_sigprocmask, AX
   341  	SYSCALL
   342  	CMPQ	AX, $0xfffffffffffff001
   343  	JLS	2(PC)
   344  	MOVL	$0xf1, 0xf1  // crash
   345  	RET
   346  
   347  TEXT runtime·rt_sigaction(SB),NOSPLIT,$0-36
   348  	MOVQ	sig+0(FP), DI
   349  	MOVQ	new+8(FP), SI
   350  	MOVQ	old+16(FP), DX
   351  	MOVQ	size+24(FP), R10
   352  	MOVL	$SYS_rt_sigaction, AX
   353  	SYSCALL
   354  	MOVL	AX, ret+32(FP)
   355  	RET
   356  
   357  // Call the function stored in _cgo_sigaction using the GCC calling convention.
   358  TEXT runtime·callCgoSigaction(SB),NOSPLIT,$16
   359  	MOVQ	sig+0(FP), DI
   360  	MOVQ	new+8(FP), SI
   361  	MOVQ	old+16(FP), DX
   362  	MOVQ	_cgo_sigaction(SB), AX
   363  	MOVQ	SP, BX	// callee-saved
   364  	ANDQ	$~15, SP	// alignment as per amd64 psABI
   365  	CALL	AX
   366  	MOVQ	BX, SP
   367  	MOVL	AX, ret+24(FP)
   368  	RET
   369  
   370  TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
   371  	MOVQ	fn+0(FP),    AX
   372  	MOVL	sig+8(FP),   DI
   373  	MOVQ	info+16(FP), SI
   374  	MOVQ	ctx+24(FP),  DX
   375  	PUSHQ	BP
   376  	MOVQ	SP, BP
   377  	ANDQ	$~15, SP     // alignment for x86_64 ABI
   378  	CALL	AX
   379  	MOVQ	BP, SP
   380  	POPQ	BP
   381  	RET
   382  
   383  // Defined as ABIInternal since it does not use the stack-based Go ABI.
   384  TEXT runtime·sigtramp<ABIInternal>(SB),NOSPLIT,$72
   385  	// Save callee-saved C registers, since the caller may be a C signal handler.
   386  	MOVQ	BX,  bx-8(SP)
   387  	MOVQ	BP,  bp-16(SP)  // save in case GOEXPERIMENT=noframepointer is set
   388  	MOVQ	R12, r12-24(SP)
   389  	MOVQ	R13, r13-32(SP)
   390  	MOVQ	R14, r14-40(SP)
   391  	MOVQ	R15, r15-48(SP)
   392  	// We don't save mxcsr or the x87 control word because sigtrampgo doesn't
   393  	// modify them.
   394  
   395  	MOVQ	DX, ctx-56(SP)
   396  	MOVQ	SI, info-64(SP)
   397  	MOVQ	DI, signum-72(SP)
   398  	MOVQ	$runtime·sigtrampgo(SB), AX
   399  	CALL AX
   400  
   401  	MOVQ	r15-48(SP), R15
   402  	MOVQ	r14-40(SP), R14
   403  	MOVQ	r13-32(SP), R13
   404  	MOVQ	r12-24(SP), R12
   405  	MOVQ	bp-16(SP),  BP
   406  	MOVQ	bx-8(SP),   BX
   407  	RET
   408  
   409  // Used instead of sigtramp in programs that use cgo.
   410  // Arguments from kernel are in DI, SI, DX.
   411  // Defined as ABIInternal since it does not use the stack-based Go ABI.
   412  TEXT runtime·cgoSigtramp<ABIInternal>(SB),NOSPLIT,$0
   413  	// If no traceback function, do usual sigtramp.
   414  	MOVQ	runtime·cgoTraceback(SB), AX
   415  	TESTQ	AX, AX
   416  	JZ	sigtramp
   417  
   418  	// If no traceback support function, which means that
   419  	// runtime/cgo was not linked in, do usual sigtramp.
   420  	MOVQ	_cgo_callers(SB), AX
   421  	TESTQ	AX, AX
   422  	JZ	sigtramp
   423  
   424  	// Figure out if we are currently in a cgo call.
   425  	// If not, just do usual sigtramp.
   426  	get_tls(CX)
   427  	MOVQ	g(CX),AX
   428  	TESTQ	AX, AX
   429  	JZ	sigtrampnog     // g == nil
   430  	MOVQ	g_m(AX), AX
   431  	TESTQ	AX, AX
   432  	JZ	sigtramp        // g.m == nil
   433  	MOVL	m_ncgo(AX), CX
   434  	TESTL	CX, CX
   435  	JZ	sigtramp        // g.m.ncgo == 0
   436  	MOVQ	m_curg(AX), CX
   437  	TESTQ	CX, CX
   438  	JZ	sigtramp        // g.m.curg == nil
   439  	MOVQ	g_syscallsp(CX), CX
   440  	TESTQ	CX, CX
   441  	JZ	sigtramp        // g.m.curg.syscallsp == 0
   442  	MOVQ	m_cgoCallers(AX), R8
   443  	TESTQ	R8, R8
   444  	JZ	sigtramp        // g.m.cgoCallers == nil
   445  	MOVL	m_cgoCallersUse(AX), CX
   446  	TESTL	CX, CX
   447  	JNZ	sigtramp	// g.m.cgoCallersUse != 0
   448  
   449  	// Jump to a function in runtime/cgo.
   450  	// That function, written in C, will call the user's traceback
   451  	// function with proper unwind info, and will then call back here.
   452  	// The first three arguments, and the fifth, are already in registers.
   453  	// Set the two remaining arguments now.
   454  	MOVQ	runtime·cgoTraceback(SB), CX
   455  	MOVQ	$runtime·sigtramp<ABIInternal>(SB), R9
   456  	MOVQ	_cgo_callers(SB), AX
   457  	JMP	AX
   458  
   459  sigtramp:
   460  	JMP	runtime·sigtramp<ABIInternal>(SB)
   461  
   462  sigtrampnog:
   463  	// Signal arrived on a non-Go thread. If this is SIGPROF, get a
   464  	// stack trace.
   465  	CMPL	DI, $27 // 27 == SIGPROF
   466  	JNZ	sigtramp
   467  
   468  	// Lock sigprofCallersUse.
   469  	MOVL	$0, AX
   470  	MOVL	$1, CX
   471  	MOVQ	$runtime·sigprofCallersUse(SB), R11
   472  	LOCK
   473  	CMPXCHGL	CX, 0(R11)
   474  	JNZ	sigtramp  // Skip stack trace if already locked.
   475  
   476  	// Jump to the traceback function in runtime/cgo.
   477  	// It will call back to sigprofNonGo, which will ignore the
   478  	// arguments passed in registers.
   479  	// First three arguments to traceback function are in registers already.
   480  	MOVQ	runtime·cgoTraceback(SB), CX
   481  	MOVQ	$runtime·sigprofCallers(SB), R8
   482  	MOVQ	$runtime·sigprofNonGo(SB), R9
   483  	MOVQ	_cgo_callers(SB), AX
   484  	JMP	AX
   485  
   486  // For cgo unwinding to work, this function must look precisely like
   487  // the one in glibc.  The glibc source code is:
   488  // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/sigaction.c
   489  // The code that cares about the precise instructions used is:
   490  // https://gcc.gnu.org/viewcvs/gcc/trunk/libgcc/config/i386/linux-unwind.h?revision=219188&view=markup
   491  // Defined as ABIInternal since it does not use the stack-based Go ABI.
   492  TEXT runtime·sigreturn<ABIInternal>(SB),NOSPLIT,$0
   493  	MOVQ	$SYS_rt_sigreturn, AX
   494  	SYSCALL
   495  	INT $3	// not reached
   496  
   497  TEXT runtime·sysMmap(SB),NOSPLIT,$0
   498  	MOVQ	addr+0(FP), DI
   499  	MOVQ	n+8(FP), SI
   500  	MOVL	prot+16(FP), DX
   501  	MOVL	flags+20(FP), R10
   502  	MOVL	fd+24(FP), R8
   503  	MOVL	off+28(FP), R9
   504  
   505  	MOVL	$SYS_mmap, AX
   506  	SYSCALL
   507  	CMPQ	AX, $0xfffffffffffff001
   508  	JLS	ok
   509  	NOTQ	AX
   510  	INCQ	AX
   511  	MOVQ	$0, p+32(FP)
   512  	MOVQ	AX, err+40(FP)
   513  	RET
   514  ok:
   515  	MOVQ	AX, p+32(FP)
   516  	MOVQ	$0, err+40(FP)
   517  	RET
   518  
   519  // Call the function stored in _cgo_mmap using the GCC calling convention.
   520  // This must be called on the system stack.
   521  TEXT runtime·callCgoMmap(SB),NOSPLIT,$16
   522  	MOVQ	addr+0(FP), DI
   523  	MOVQ	n+8(FP), SI
   524  	MOVL	prot+16(FP), DX
   525  	MOVL	flags+20(FP), CX
   526  	MOVL	fd+24(FP), R8
   527  	MOVL	off+28(FP), R9
   528  	MOVQ	_cgo_mmap(SB), AX
   529  	MOVQ	SP, BX
   530  	ANDQ	$~15, SP	// alignment as per amd64 psABI
   531  	MOVQ	BX, 0(SP)
   532  	CALL	AX
   533  	MOVQ	0(SP), SP
   534  	MOVQ	AX, ret+32(FP)
   535  	RET
   536  
   537  TEXT runtime·sysMunmap(SB),NOSPLIT,$0
   538  	MOVQ	addr+0(FP), DI
   539  	MOVQ	n+8(FP), SI
   540  	MOVQ	$SYS_munmap, AX
   541  	SYSCALL
   542  	CMPQ	AX, $0xfffffffffffff001
   543  	JLS	2(PC)
   544  	MOVL	$0xf1, 0xf1  // crash
   545  	RET
   546  
   547  // Call the function stored in _cgo_munmap using the GCC calling convention.
   548  // This must be called on the system stack.
   549  TEXT runtime·callCgoMunmap(SB),NOSPLIT,$16-16
   550  	MOVQ	addr+0(FP), DI
   551  	MOVQ	n+8(FP), SI
   552  	MOVQ	_cgo_munmap(SB), AX
   553  	MOVQ	SP, BX
   554  	ANDQ	$~15, SP	// alignment as per amd64 psABI
   555  	MOVQ	BX, 0(SP)
   556  	CALL	AX
   557  	MOVQ	0(SP), SP
   558  	RET
   559  
   560  TEXT runtime·madvise(SB),NOSPLIT,$0
   561  	MOVQ	addr+0(FP), DI
   562  	MOVQ	n+8(FP), SI
   563  	MOVL	flags+16(FP), DX
   564  	MOVQ	$SYS_madvise, AX
   565  	SYSCALL
   566  	MOVL	AX, ret+24(FP)
   567  	RET
   568  
   569  // int64 futex(int32 *uaddr, int32 op, int32 val,
   570  //	struct timespec *timeout, int32 *uaddr2, int32 val2);
   571  TEXT runtime·futex(SB),NOSPLIT,$0
   572  	MOVQ	addr+0(FP), DI
   573  	MOVL	op+8(FP), SI
   574  	MOVL	val+12(FP), DX
   575  	MOVQ	ts+16(FP), R10
   576  	MOVQ	addr2+24(FP), R8
   577  	MOVL	val3+32(FP), R9
   578  	MOVL	$SYS_futex, AX
   579  	SYSCALL
   580  	MOVL	AX, ret+40(FP)
   581  	RET
   582  
   583  // int32 clone(int32 flags, void *stk, M *mp, G *gp, void (*fn)(void));
   584  TEXT runtime·clone(SB),NOSPLIT,$0
   585  	MOVL	flags+0(FP), DI
   586  	MOVQ	stk+8(FP), SI
   587  	MOVQ	$0, DX
   588  	MOVQ	$0, R10
   589  	MOVQ    $0, R8
   590  	// Copy mp, gp, fn off parent stack for use by child.
   591  	// Careful: Linux system call clobbers CX and R11.
   592  	MOVQ	mp+16(FP), R13
   593  	MOVQ	gp+24(FP), R9
   594  	MOVQ	fn+32(FP), R12
   595  	CMPQ	R13, $0    // m
   596  	JEQ	nog1
   597  	CMPQ	R9, $0    // g
   598  	JEQ	nog1
   599  	LEAQ	m_tls(R13), R8
   600  #ifdef GOOS_android
   601  	// Android stores the TLS offset in runtime·tls_g.
   602  	SUBQ	runtime·tls_g(SB), R8
   603  #else
   604  	ADDQ	$8, R8	// ELF wants to use -8(FS)
   605  #endif
   606  	ORQ 	$0x00080000, DI //add flag CLONE_SETTLS(0x00080000) to call clone
   607  nog1:
   608  	MOVL	$SYS_clone, AX
   609  	SYSCALL
   610  
   611  	// In parent, return.
   612  	CMPQ	AX, $0
   613  	JEQ	3(PC)
   614  	MOVL	AX, ret+40(FP)
   615  	RET
   616  
   617  	// In child, on new stack.
   618  	MOVQ	SI, SP
   619  
   620  	// If g or m are nil, skip Go-related setup.
   621  	CMPQ	R13, $0    // m
   622  	JEQ	nog2
   623  	CMPQ	R9, $0    // g
   624  	JEQ	nog2
   625  
   626  	// Initialize m->procid to Linux tid
   627  	MOVL	$SYS_gettid, AX
   628  	SYSCALL
   629  	MOVQ	AX, m_procid(R13)
   630  
   631  	// In child, set up new stack
   632  	get_tls(CX)
   633  	MOVQ	R13, g_m(R9)
   634  	MOVQ	R9, g(CX)
   635  	CALL	runtime·stackcheck(SB)
   636  
   637  nog2:
   638  	// Call fn
   639  	CALL	R12
   640  
   641  	// It shouldn't return. If it does, exit that thread.
   642  	MOVL	$111, DI
   643  	MOVL	$SYS_exit, AX
   644  	SYSCALL
   645  	JMP	-3(PC)	// keep exiting
   646  
   647  TEXT runtime·sigaltstack(SB),NOSPLIT,$-8
   648  	MOVQ	new+0(FP), DI
   649  	MOVQ	old+8(FP), SI
   650  	MOVQ	$SYS_sigaltstack, AX
   651  	SYSCALL
   652  	CMPQ	AX, $0xfffffffffffff001
   653  	JLS	2(PC)
   654  	MOVL	$0xf1, 0xf1  // crash
   655  	RET
   656  
   657  // set tls base to DI
   658  TEXT runtime·settls(SB),NOSPLIT,$32
   659  #ifdef GOOS_android
   660  	// Android stores the TLS offset in runtime·tls_g.
   661  	SUBQ	runtime·tls_g(SB), DI
   662  #else
   663  	ADDQ	$8, DI	// ELF wants to use -8(FS)
   664  #endif
   665  	MOVQ	DI, SI
   666  	MOVQ	$0x1002, DI	// ARCH_SET_FS
   667  	MOVQ	$SYS_arch_prctl, AX
   668  	SYSCALL
   669  	CMPQ	AX, $0xfffffffffffff001
   670  	JLS	2(PC)
   671  	MOVL	$0xf1, 0xf1  // crash
   672  	RET
   673  
   674  TEXT runtime·osyield(SB),NOSPLIT,$0
   675  	MOVL	$SYS_sched_yield, AX
   676  	SYSCALL
   677  	RET
   678  
   679  TEXT runtime·sched_getaffinity(SB),NOSPLIT,$0
   680  	MOVQ	pid+0(FP), DI
   681  	MOVQ	len+8(FP), SI
   682  	MOVQ	buf+16(FP), DX
   683  	MOVL	$SYS_sched_getaffinity, AX
   684  	SYSCALL
   685  	MOVL	AX, ret+24(FP)
   686  	RET
   687  
   688  // int32 runtime·epollcreate(int32 size);
   689  TEXT runtime·epollcreate(SB),NOSPLIT,$0
   690  	MOVL    size+0(FP), DI
   691  	MOVL    $SYS_epoll_create, AX
   692  	SYSCALL
   693  	MOVL	AX, ret+8(FP)
   694  	RET
   695  
   696  // int32 runtime·epollcreate1(int32 flags);
   697  TEXT runtime·epollcreate1(SB),NOSPLIT,$0
   698  	MOVL	flags+0(FP), DI
   699  	MOVL	$SYS_epoll_create1, AX
   700  	SYSCALL
   701  	MOVL	AX, ret+8(FP)
   702  	RET
   703  
   704  // func epollctl(epfd, op, fd int32, ev *epollEvent) int
   705  TEXT runtime·epollctl(SB),NOSPLIT,$0
   706  	MOVL	epfd+0(FP), DI
   707  	MOVL	op+4(FP), SI
   708  	MOVL	fd+8(FP), DX
   709  	MOVQ	ev+16(FP), R10
   710  	MOVL	$SYS_epoll_ctl, AX
   711  	SYSCALL
   712  	MOVL	AX, ret+24(FP)
   713  	RET
   714  
   715  // int32 runtime·epollwait(int32 epfd, EpollEvent *ev, int32 nev, int32 timeout);
   716  TEXT runtime·epollwait(SB),NOSPLIT,$0
   717  	// This uses pwait instead of wait, because Android O blocks wait.
   718  	MOVL	epfd+0(FP), DI
   719  	MOVQ	ev+8(FP), SI
   720  	MOVL	nev+16(FP), DX
   721  	MOVL	timeout+20(FP), R10
   722  	MOVQ	$0, R8
   723  	MOVL	$SYS_epoll_pwait, AX
   724  	SYSCALL
   725  	MOVL	AX, ret+24(FP)
   726  	RET
   727  
   728  // void runtime·closeonexec(int32 fd);
   729  TEXT runtime·closeonexec(SB),NOSPLIT,$0
   730  	MOVL    fd+0(FP), DI  // fd
   731  	MOVQ    $2, SI  // F_SETFD
   732  	MOVQ    $1, DX  // FD_CLOEXEC
   733  	MOVL	$SYS_fcntl, AX
   734  	SYSCALL
   735  	RET
   736  
   737  // func runtime·setNonblock(int32 fd)
   738  TEXT runtime·setNonblock(SB),NOSPLIT,$0-4
   739  	MOVL    fd+0(FP), DI  // fd
   740  	MOVQ    $3, SI  // F_GETFL
   741  	MOVQ    $0, DX
   742  	MOVL	$SYS_fcntl, AX
   743  	SYSCALL
   744  	MOVL	fd+0(FP), DI // fd
   745  	MOVQ	$4, SI // F_SETFL
   746  	MOVQ	$0x800, DX // O_NONBLOCK
   747  	ORL	AX, DX
   748  	MOVL	$SYS_fcntl, AX
   749  	SYSCALL
   750  	RET
   751  
   752  // int access(const char *name, int mode)
   753  TEXT runtime·access(SB),NOSPLIT,$0
   754  	// This uses faccessat instead of access, because Android O blocks access.
   755  	MOVL	$AT_FDCWD, DI // AT_FDCWD, so this acts like access
   756  	MOVQ	name+0(FP), SI
   757  	MOVL	mode+8(FP), DX
   758  	MOVL	$0, R10
   759  	MOVL	$SYS_faccessat, AX
   760  	SYSCALL
   761  	MOVL	AX, ret+16(FP)
   762  	RET
   763  
   764  // int connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
   765  TEXT runtime·connect(SB),NOSPLIT,$0-28
   766  	MOVL	fd+0(FP), DI
   767  	MOVQ	addr+8(FP), SI
   768  	MOVL	len+16(FP), DX
   769  	MOVL	$SYS_connect, AX
   770  	SYSCALL
   771  	MOVL	AX, ret+24(FP)
   772  	RET
   773  
   774  // int socket(int domain, int type, int protocol)
   775  TEXT runtime·socket(SB),NOSPLIT,$0-20
   776  	MOVL	domain+0(FP), DI
   777  	MOVL	typ+4(FP), SI
   778  	MOVL	prot+8(FP), DX
   779  	MOVL	$SYS_socket, AX
   780  	SYSCALL
   781  	MOVL	AX, ret+16(FP)
   782  	RET
   783  
   784  // func sbrk0() uintptr
   785  TEXT runtime·sbrk0(SB),NOSPLIT,$0-8
   786  	// Implemented as brk(NULL).
   787  	MOVQ	$0, DI
   788  	MOVL	$SYS_brk, AX
   789  	SYSCALL
   790  	MOVQ	AX, ret+0(FP)
   791  	RET
   792  

View as plain text