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_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,$16-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	// Save the old values on stack and restore them on exit,
   225	// so this function is reentrant.
   226	MOVQ	m_vdsoPC(BX), CX
   227	MOVQ	m_vdsoSP(BX), DX
   228	MOVQ	CX, 0(SP)
   229	MOVQ	DX, 8(SP)
   230
   231	LEAQ	sec+0(FP), DX
   232	MOVQ	-8(DX), CX
   233	MOVQ	CX, m_vdsoPC(BX)
   234	MOVQ	DX, m_vdsoSP(BX)
   235
   236	CMPQ	AX, m_curg(BX)	// Only switch if on curg.
   237	JNE	noswitch
   238
   239	MOVQ	m_g0(BX), DX
   240	MOVQ	(g_sched+gobuf_sp)(DX), SP	// Set SP to g0 stack
   241
   242noswitch:
   243	SUBQ	$16, SP		// Space for results
   244	ANDQ	$~15, SP	// Align for C code
   245
   246	MOVQ	runtime·vdsoClockgettimeSym(SB), AX
   247	CMPQ	AX, $0
   248	JEQ	fallback
   249	MOVL	$0, DI // CLOCK_REALTIME
   250	LEAQ	0(SP), SI
   251	CALL	AX
   252	MOVQ	0(SP), AX	// sec
   253	MOVQ	8(SP), DX	// nsec
   254ret:
   255	MOVQ	BP, SP		// Restore real SP
   256	// Restore vdsoPC, vdsoSP
   257	// We don't worry about being signaled between the two stores.
   258	// If we are not in a signal handler, we'll restore vdsoSP to 0,
   259	// and no one will care about vdsoPC. If we are in a signal handler,
   260	// we cannot receive another signal.
   261	MOVQ	8(SP), CX
   262	MOVQ	CX, m_vdsoSP(BX)
   263	MOVQ	0(SP), CX
   264	MOVQ	CX, m_vdsoPC(BX)
   265	MOVQ	AX, sec+0(FP)
   266	MOVL	DX, nsec+8(FP)
   267	RET
   268fallback:
   269	LEAQ	0(SP), DI
   270	MOVQ	$0, SI
   271	MOVQ	runtime·vdsoGettimeofdaySym(SB), AX
   272	CALL	AX
   273	MOVQ	0(SP), AX	// sec
   274	MOVL	8(SP), DX	// usec
   275	IMULQ	$1000, DX
   276	JMP ret
   277
   278// func nanotime1() int64
   279TEXT runtime·nanotime1(SB),NOSPLIT,$16-8
   280	// Switch to g0 stack. See comment above in runtime·walltime.
   281
   282	MOVQ	SP, BP	// Save old SP; BP unchanged by C code.
   283
   284	get_tls(CX)
   285	MOVQ	g(CX), AX
   286	MOVQ	g_m(AX), BX // BX unchanged by C code.
   287
   288	// Set vdsoPC and vdsoSP for SIGPROF traceback.
   289	// Save the old values on stack and restore them on exit,
   290	// so this function is reentrant.
   291	MOVQ	m_vdsoPC(BX), CX
   292	MOVQ	m_vdsoSP(BX), DX
   293	MOVQ	CX, 0(SP)
   294	MOVQ	DX, 8(SP)
   295
   296	LEAQ	ret+0(FP), DX
   297	MOVQ	-8(DX), CX
   298	MOVQ	CX, m_vdsoPC(BX)
   299	MOVQ	DX, m_vdsoSP(BX)
   300
   301	CMPQ	AX, m_curg(BX)	// Only switch if on curg.
   302	JNE	noswitch
   303
   304	MOVQ	m_g0(BX), DX
   305	MOVQ	(g_sched+gobuf_sp)(DX), SP	// Set SP to g0 stack
   306
   307noswitch:
   308	SUBQ	$16, SP		// Space for results
   309	ANDQ	$~15, SP	// Align for C code
   310
   311	MOVQ	runtime·vdsoClockgettimeSym(SB), AX
   312	CMPQ	AX, $0
   313	JEQ	fallback
   314	MOVL	$1, DI // CLOCK_MONOTONIC
   315	LEAQ	0(SP), SI
   316	CALL	AX
   317	MOVQ	0(SP), AX	// sec
   318	MOVQ	8(SP), DX	// nsec
   319ret:
   320	MOVQ	BP, SP		// Restore real SP
   321	// Restore vdsoPC, vdsoSP
   322	// We don't worry about being signaled between the two stores.
   323	// If we are not in a signal handler, we'll restore vdsoSP to 0,
   324	// and no one will care about vdsoPC. If we are in a signal handler,
   325	// we cannot receive another signal.
   326	MOVQ	8(SP), CX
   327	MOVQ	CX, m_vdsoSP(BX)
   328	MOVQ	0(SP), CX
   329	MOVQ	CX, m_vdsoPC(BX)
   330	// sec is in AX, nsec in DX
   331	// return nsec in AX
   332	IMULQ	$1000000000, AX
   333	ADDQ	DX, AX
   334	MOVQ	AX, ret+0(FP)
   335	RET
   336fallback:
   337	LEAQ	0(SP), DI
   338	MOVQ	$0, SI
   339	MOVQ	runtime·vdsoGettimeofdaySym(SB), AX
   340	CALL	AX
   341	MOVQ	0(SP), AX	// sec
   342	MOVL	8(SP), DX	// usec
   343	IMULQ	$1000, DX
   344	JMP	ret
   345
   346TEXT runtime·rtsigprocmask(SB),NOSPLIT,$0-28
   347	MOVL	how+0(FP), DI
   348	MOVQ	new+8(FP), SI
   349	MOVQ	old+16(FP), DX
   350	MOVL	size+24(FP), R10
   351	MOVL	$SYS_rt_sigprocmask, AX
   352	SYSCALL
   353	CMPQ	AX, $0xfffffffffffff001
   354	JLS	2(PC)
   355	MOVL	$0xf1, 0xf1  // crash
   356	RET
   357
   358TEXT runtime·rt_sigaction(SB),NOSPLIT,$0-36
   359	MOVQ	sig+0(FP), DI
   360	MOVQ	new+8(FP), SI
   361	MOVQ	old+16(FP), DX
   362	MOVQ	size+24(FP), R10
   363	MOVL	$SYS_rt_sigaction, AX
   364	SYSCALL
   365	MOVL	AX, ret+32(FP)
   366	RET
   367
   368// Call the function stored in _cgo_sigaction using the GCC calling convention.
   369TEXT runtime·callCgoSigaction(SB),NOSPLIT,$16
   370	MOVQ	sig+0(FP), DI
   371	MOVQ	new+8(FP), SI
   372	MOVQ	old+16(FP), DX
   373	MOVQ	_cgo_sigaction(SB), AX
   374	MOVQ	SP, BX	// callee-saved
   375	ANDQ	$~15, SP	// alignment as per amd64 psABI
   376	CALL	AX
   377	MOVQ	BX, SP
   378	MOVL	AX, ret+24(FP)
   379	RET
   380
   381TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
   382	MOVQ	fn+0(FP),    AX
   383	MOVL	sig+8(FP),   DI
   384	MOVQ	info+16(FP), SI
   385	MOVQ	ctx+24(FP),  DX
   386	PUSHQ	BP
   387	MOVQ	SP, BP
   388	ANDQ	$~15, SP     // alignment for x86_64 ABI
   389	CALL	AX
   390	MOVQ	BP, SP
   391	POPQ	BP
   392	RET
   393
   394TEXT runtime·sigtramp(SB),NOSPLIT,$72
   395	// Save callee-saved C registers, since the caller may be a C signal handler.
   396	MOVQ	BX,  bx-8(SP)
   397	MOVQ	BP,  bp-16(SP)  // save in case GOEXPERIMENT=noframepointer is set
   398	MOVQ	R12, r12-24(SP)
   399	MOVQ	R13, r13-32(SP)
   400	MOVQ	R14, r14-40(SP)
   401	MOVQ	R15, r15-48(SP)
   402	// We don't save mxcsr or the x87 control word because sigtrampgo doesn't
   403	// modify them.
   404
   405	MOVQ	DX, ctx-56(SP)
   406	MOVQ	SI, info-64(SP)
   407	MOVQ	DI, signum-72(SP)
   408	MOVQ	$runtime·sigtrampgo(SB), AX
   409	CALL AX
   410
   411	MOVQ	r15-48(SP), R15
   412	MOVQ	r14-40(SP), R14
   413	MOVQ	r13-32(SP), R13
   414	MOVQ	r12-24(SP), R12
   415	MOVQ	bp-16(SP),  BP
   416	MOVQ	bx-8(SP),   BX
   417	RET
   418
   419// Used instead of sigtramp in programs that use cgo.
   420// Arguments from kernel are in DI, SI, DX.
   421TEXT runtime·cgoSigtramp(SB),NOSPLIT,$0
   422	// If no traceback function, do usual sigtramp.
   423	MOVQ	runtime·cgoTraceback(SB), AX
   424	TESTQ	AX, AX
   425	JZ	sigtramp
   426
   427	// If no traceback support function, which means that
   428	// runtime/cgo was not linked in, do usual sigtramp.
   429	MOVQ	_cgo_callers(SB), AX
   430	TESTQ	AX, AX
   431	JZ	sigtramp
   432
   433	// Figure out if we are currently in a cgo call.
   434	// If not, just do usual sigtramp.
   435	get_tls(CX)
   436	MOVQ	g(CX),AX
   437	TESTQ	AX, AX
   438	JZ	sigtrampnog     // g == nil
   439	MOVQ	g_m(AX), AX
   440	TESTQ	AX, AX
   441	JZ	sigtramp        // g.m == nil
   442	MOVL	m_ncgo(AX), CX
   443	TESTL	CX, CX
   444	JZ	sigtramp        // g.m.ncgo == 0
   445	MOVQ	m_curg(AX), CX
   446	TESTQ	CX, CX
   447	JZ	sigtramp        // g.m.curg == nil
   448	MOVQ	g_syscallsp(CX), CX
   449	TESTQ	CX, CX
   450	JZ	sigtramp        // g.m.curg.syscallsp == 0
   451	MOVQ	m_cgoCallers(AX), R8
   452	TESTQ	R8, R8
   453	JZ	sigtramp        // g.m.cgoCallers == nil
   454	MOVL	m_cgoCallersUse(AX), CX
   455	TESTL	CX, CX
   456	JNZ	sigtramp	// g.m.cgoCallersUse != 0
   457
   458	// Jump to a function in runtime/cgo.
   459	// That function, written in C, will call the user's traceback
   460	// function with proper unwind info, and will then call back here.
   461	// The first three arguments, and the fifth, are already in registers.
   462	// Set the two remaining arguments now.
   463	MOVQ	runtime·cgoTraceback(SB), CX
   464	MOVQ	$runtime·sigtramp(SB), R9
   465	MOVQ	_cgo_callers(SB), AX
   466	JMP	AX
   467
   468sigtramp:
   469	JMP	runtime·sigtramp(SB)
   470
   471sigtrampnog:
   472	// Signal arrived on a non-Go thread. If this is SIGPROF, get a
   473	// stack trace.
   474	CMPL	DI, $27 // 27 == SIGPROF
   475	JNZ	sigtramp
   476
   477	// Lock sigprofCallersUse.
   478	MOVL	$0, AX
   479	MOVL	$1, CX
   480	MOVQ	$runtime·sigprofCallersUse(SB), R11
   481	LOCK
   482	CMPXCHGL	CX, 0(R11)
   483	JNZ	sigtramp  // Skip stack trace if already locked.
   484
   485	// Jump to the traceback function in runtime/cgo.
   486	// It will call back to sigprofNonGo, which will ignore the
   487	// arguments passed in registers.
   488	// First three arguments to traceback function are in registers already.
   489	MOVQ	runtime·cgoTraceback(SB), CX
   490	MOVQ	$runtime·sigprofCallers(SB), R8
   491	MOVQ	$runtime·sigprofNonGo(SB), R9
   492	MOVQ	_cgo_callers(SB), AX
   493	JMP	AX
   494
   495// For cgo unwinding to work, this function must look precisely like
   496// the one in glibc.  The glibc source code is:
   497// https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/sigaction.c
   498// The code that cares about the precise instructions used is:
   499// https://gcc.gnu.org/viewcvs/gcc/trunk/libgcc/config/i386/linux-unwind.h?revision=219188&view=markup
   500TEXT runtime·sigreturn(SB),NOSPLIT,$0
   501	MOVQ	$SYS_rt_sigreturn, AX
   502	SYSCALL
   503	INT $3	// not reached
   504
   505TEXT runtime·sysMmap(SB),NOSPLIT,$0
   506	MOVQ	addr+0(FP), DI
   507	MOVQ	n+8(FP), SI
   508	MOVL	prot+16(FP), DX
   509	MOVL	flags+20(FP), R10
   510	MOVL	fd+24(FP), R8
   511	MOVL	off+28(FP), R9
   512
   513	MOVL	$SYS_mmap, AX
   514	SYSCALL
   515	CMPQ	AX, $0xfffffffffffff001
   516	JLS	ok
   517	NOTQ	AX
   518	INCQ	AX
   519	MOVQ	$0, p+32(FP)
   520	MOVQ	AX, err+40(FP)
   521	RET
   522ok:
   523	MOVQ	AX, p+32(FP)
   524	MOVQ	$0, err+40(FP)
   525	RET
   526
   527// Call the function stored in _cgo_mmap using the GCC calling convention.
   528// This must be called on the system stack.
   529TEXT runtime·callCgoMmap(SB),NOSPLIT,$16
   530	MOVQ	addr+0(FP), DI
   531	MOVQ	n+8(FP), SI
   532	MOVL	prot+16(FP), DX
   533	MOVL	flags+20(FP), CX
   534	MOVL	fd+24(FP), R8
   535	MOVL	off+28(FP), R9
   536	MOVQ	_cgo_mmap(SB), AX
   537	MOVQ	SP, BX
   538	ANDQ	$~15, SP	// alignment as per amd64 psABI
   539	MOVQ	BX, 0(SP)
   540	CALL	AX
   541	MOVQ	0(SP), SP
   542	MOVQ	AX, ret+32(FP)
   543	RET
   544
   545TEXT runtime·sysMunmap(SB),NOSPLIT,$0
   546	MOVQ	addr+0(FP), DI
   547	MOVQ	n+8(FP), SI
   548	MOVQ	$SYS_munmap, AX
   549	SYSCALL
   550	CMPQ	AX, $0xfffffffffffff001
   551	JLS	2(PC)
   552	MOVL	$0xf1, 0xf1  // crash
   553	RET
   554
   555// Call the function stored in _cgo_munmap using the GCC calling convention.
   556// This must be called on the system stack.
   557TEXT runtime·callCgoMunmap(SB),NOSPLIT,$16-16
   558	MOVQ	addr+0(FP), DI
   559	MOVQ	n+8(FP), SI
   560	MOVQ	_cgo_munmap(SB), AX
   561	MOVQ	SP, BX
   562	ANDQ	$~15, SP	// alignment as per amd64 psABI
   563	MOVQ	BX, 0(SP)
   564	CALL	AX
   565	MOVQ	0(SP), SP
   566	RET
   567
   568TEXT runtime·madvise(SB),NOSPLIT,$0
   569	MOVQ	addr+0(FP), DI
   570	MOVQ	n+8(FP), SI
   571	MOVL	flags+16(FP), DX
   572	MOVQ	$SYS_madvise, AX
   573	SYSCALL
   574	MOVL	AX, ret+24(FP)
   575	RET
   576
   577// int64 futex(int32 *uaddr, int32 op, int32 val,
   578//	struct timespec *timeout, int32 *uaddr2, int32 val2);
   579TEXT runtime·futex(SB),NOSPLIT,$0
   580	MOVQ	addr+0(FP), DI
   581	MOVL	op+8(FP), SI
   582	MOVL	val+12(FP), DX
   583	MOVQ	ts+16(FP), R10
   584	MOVQ	addr2+24(FP), R8
   585	MOVL	val3+32(FP), R9
   586	MOVL	$SYS_futex, AX
   587	SYSCALL
   588	MOVL	AX, ret+40(FP)
   589	RET
   590
   591// int32 clone(int32 flags, void *stk, M *mp, G *gp, void (*fn)(void));
   592TEXT runtime·clone(SB),NOSPLIT,$0
   593	MOVL	flags+0(FP), DI
   594	MOVQ	stk+8(FP), SI
   595	MOVQ	$0, DX
   596	MOVQ	$0, R10
   597
   598	// Copy mp, gp, fn off parent stack for use by child.
   599	// Careful: Linux system call clobbers CX and R11.
   600	MOVQ	mp+16(FP), R8
   601	MOVQ	gp+24(FP), R9
   602	MOVQ	fn+32(FP), R12
   603
   604	MOVL	$SYS_clone, AX
   605	SYSCALL
   606
   607	// In parent, return.
   608	CMPQ	AX, $0
   609	JEQ	3(PC)
   610	MOVL	AX, ret+40(FP)
   611	RET
   612
   613	// In child, on new stack.
   614	MOVQ	SI, SP
   615
   616	// If g or m are nil, skip Go-related setup.
   617	CMPQ	R8, $0    // m
   618	JEQ	nog
   619	CMPQ	R9, $0    // g
   620	JEQ	nog
   621
   622	// Initialize m->procid to Linux tid
   623	MOVL	$SYS_gettid, AX
   624	SYSCALL
   625	MOVQ	AX, m_procid(R8)
   626
   627	// Set FS to point at m->tls.
   628	LEAQ	m_tls(R8), DI
   629	CALL	runtime·settls(SB)
   630
   631	// In child, set up new stack
   632	get_tls(CX)
   633	MOVQ	R8, g_m(R9)
   634	MOVQ	R9, g(CX)
   635	CALL	runtime·stackcheck(SB)
   636
   637nog:
   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
   647TEXT 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
   658TEXT 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
   674TEXT runtime·osyield(SB),NOSPLIT,$0
   675	MOVL	$SYS_sched_yield, AX
   676	SYSCALL
   677	RET
   678
   679TEXT 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);
   689TEXT 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);
   697TEXT 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
   705TEXT 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);
   716TEXT 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);
   729TEXT 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)
   738TEXT 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)
   753TEXT 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)
   765TEXT 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)
   775TEXT 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
   785TEXT 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
   793// func uname(utsname *new_utsname) int
   794TEXT ·uname(SB),NOSPLIT,$0-16
   795	MOVQ    utsname+0(FP), DI
   796	MOVL    $SYS_uname, AX
   797	SYSCALL
   798	MOVQ	AX, ret+8(FP)
   799	RET
   800
   801// func mlock(addr, len uintptr) int
   802TEXT ·mlock(SB),NOSPLIT,$0-24
   803	MOVQ    addr+0(FP), DI
   804	MOVQ    len+8(FP), SI
   805	MOVL    $SYS_mlock, AX
   806	SYSCALL
   807	MOVQ	AX, ret+16(FP)
   808	RET

View as plain text