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

View as plain text