Text file src/runtime/sys_linux_amd64.s

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

View as plain text