Text file src/runtime/sys_linux_arm64.s

     1  // Copyright 2014 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 arm64, Linux
     7  //
     8  
     9  #include "go_asm.h"
    10  #include "go_tls.h"
    11  #include "textflag.h"
    12  #include "cgo/abi_arm64.h"
    13  
    14  #define AT_FDCWD -100
    15  
    16  #define CLOCK_REALTIME 0
    17  #define CLOCK_MONOTONIC 1
    18  
    19  #define SYS_exit		93
    20  #define SYS_read		63
    21  #define SYS_write		64
    22  #define SYS_openat		56
    23  #define SYS_close		57
    24  #define SYS_pipe2		59
    25  #define SYS_fcntl		25
    26  #define SYS_nanosleep		101
    27  #define SYS_mmap		222
    28  #define SYS_munmap		215
    29  #define SYS_setitimer		103
    30  #define SYS_clone		220
    31  #define SYS_sched_yield		124
    32  #define SYS_rt_sigreturn	139
    33  #define SYS_rt_sigaction	134
    34  #define SYS_rt_sigprocmask	135
    35  #define SYS_sigaltstack		132
    36  #define SYS_madvise		233
    37  #define SYS_mincore		232
    38  #define SYS_getpid		172
    39  #define SYS_gettid		178
    40  #define SYS_kill		129
    41  #define SYS_tgkill		131
    42  #define SYS_futex		98
    43  #define SYS_sched_getaffinity	123
    44  #define SYS_exit_group		94
    45  #define SYS_epoll_create1	20
    46  #define SYS_epoll_ctl		21
    47  #define SYS_epoll_pwait		22
    48  #define SYS_clock_gettime	113
    49  #define SYS_faccessat		48
    50  #define SYS_socket		198
    51  #define SYS_connect		203
    52  #define SYS_brk			214
    53  #define SYS_timer_create	107
    54  #define SYS_timer_settime	110
    55  #define SYS_timer_delete	111
    56  
    57  TEXT runtime·exit(SB),NOSPLIT|NOFRAME,$0-4
    58  	MOVW	code+0(FP), R0
    59  	MOVD	$SYS_exit_group, R8
    60  	SVC
    61  	RET
    62  
    63  // func exitThread(wait *uint32)
    64  TEXT runtime·exitThread(SB),NOSPLIT|NOFRAME,$0-8
    65  	MOVD	wait+0(FP), R0
    66  	// We're done using the stack.
    67  	MOVW	$0, R1
    68  	STLRW	R1, (R0)
    69  	MOVW	$0, R0	// exit code
    70  	MOVD	$SYS_exit, R8
    71  	SVC
    72  	JMP	0(PC)
    73  
    74  TEXT runtime·open(SB),NOSPLIT|NOFRAME,$0-20
    75  	MOVD	$AT_FDCWD, R0
    76  	MOVD	name+0(FP), R1
    77  	MOVW	mode+8(FP), R2
    78  	MOVW	perm+12(FP), R3
    79  	MOVD	$SYS_openat, R8
    80  	SVC
    81  	CMN	$4095, R0
    82  	BCC	done
    83  	MOVW	$-1, R0
    84  done:
    85  	MOVW	R0, ret+16(FP)
    86  	RET
    87  
    88  TEXT runtime·closefd(SB),NOSPLIT|NOFRAME,$0-12
    89  	MOVW	fd+0(FP), R0
    90  	MOVD	$SYS_close, R8
    91  	SVC
    92  	CMN	$4095, R0
    93  	BCC	done
    94  	MOVW	$-1, R0
    95  done:
    96  	MOVW	R0, ret+8(FP)
    97  	RET
    98  
    99  TEXT runtime·write1(SB),NOSPLIT|NOFRAME,$0-28
   100  	MOVD	fd+0(FP), R0
   101  	MOVD	p+8(FP), R1
   102  	MOVW	n+16(FP), R2
   103  	MOVD	$SYS_write, R8
   104  	SVC
   105  	MOVW	R0, ret+24(FP)
   106  	RET
   107  
   108  TEXT runtime·read(SB),NOSPLIT|NOFRAME,$0-28
   109  	MOVW	fd+0(FP), R0
   110  	MOVD	p+8(FP), R1
   111  	MOVW	n+16(FP), R2
   112  	MOVD	$SYS_read, R8
   113  	SVC
   114  	MOVW	R0, ret+24(FP)
   115  	RET
   116  
   117  // func pipe2(flags int32) (r, w int32, errno int32)
   118  TEXT runtime·pipe2(SB),NOSPLIT|NOFRAME,$0-20
   119  	MOVD	$r+8(FP), R0
   120  	MOVW	flags+0(FP), R1
   121  	MOVW	$SYS_pipe2, R8
   122  	SVC
   123  	MOVW	R0, errno+16(FP)
   124  	RET
   125  
   126  TEXT runtime·usleep(SB),NOSPLIT,$24-4
   127  	MOVWU	usec+0(FP), R3
   128  	MOVD	R3, R5
   129  	MOVW	$1000000, R4
   130  	UDIV	R4, R3
   131  	MOVD	R3, 8(RSP)
   132  	MUL	R3, R4
   133  	SUB	R4, R5
   134  	MOVW	$1000, R4
   135  	MUL	R4, R5
   136  	MOVD	R5, 16(RSP)
   137  
   138  	// nanosleep(&ts, 0)
   139  	ADD	$8, RSP, R0
   140  	MOVD	$0, R1
   141  	MOVD	$SYS_nanosleep, R8
   142  	SVC
   143  	RET
   144  
   145  TEXT runtime·gettid(SB),NOSPLIT,$0-4
   146  	MOVD	$SYS_gettid, R8
   147  	SVC
   148  	MOVW	R0, ret+0(FP)
   149  	RET
   150  
   151  TEXT runtime·raise(SB),NOSPLIT|NOFRAME,$0
   152  	MOVD	$SYS_getpid, R8
   153  	SVC
   154  	MOVW	R0, R19
   155  	MOVD	$SYS_gettid, R8
   156  	SVC
   157  	MOVW	R0, R1	// arg 2 tid
   158  	MOVW	R19, R0	// arg 1 pid
   159  	MOVW	sig+0(FP), R2	// arg 3
   160  	MOVD	$SYS_tgkill, R8
   161  	SVC
   162  	RET
   163  
   164  TEXT runtime·raiseproc(SB),NOSPLIT|NOFRAME,$0
   165  	MOVD	$SYS_getpid, R8
   166  	SVC
   167  	MOVW	R0, R0		// arg 1 pid
   168  	MOVW	sig+0(FP), R1	// arg 2
   169  	MOVD	$SYS_kill, R8
   170  	SVC
   171  	RET
   172  
   173  TEXT ·getpid(SB),NOSPLIT|NOFRAME,$0-8
   174  	MOVD	$SYS_getpid, R8
   175  	SVC
   176  	MOVD	R0, ret+0(FP)
   177  	RET
   178  
   179  TEXT ·tgkill(SB),NOSPLIT,$0-24
   180  	MOVD	tgid+0(FP), R0
   181  	MOVD	tid+8(FP), R1
   182  	MOVD	sig+16(FP), R2
   183  	MOVD	$SYS_tgkill, R8
   184  	SVC
   185  	RET
   186  
   187  TEXT runtime·setitimer(SB),NOSPLIT|NOFRAME,$0-24
   188  	MOVW	mode+0(FP), R0
   189  	MOVD	new+8(FP), R1
   190  	MOVD	old+16(FP), R2
   191  	MOVD	$SYS_setitimer, R8
   192  	SVC
   193  	RET
   194  
   195  TEXT runtime·timer_create(SB),NOSPLIT,$0-28
   196  	MOVW	clockid+0(FP), R0
   197  	MOVD	sevp+8(FP), R1
   198  	MOVD	timerid+16(FP), R2
   199  	MOVD	$SYS_timer_create, R8
   200  	SVC
   201  	MOVW	R0, ret+24(FP)
   202  	RET
   203  
   204  TEXT runtime·timer_settime(SB),NOSPLIT,$0-28
   205  	MOVW	timerid+0(FP), R0
   206  	MOVW	flags+4(FP), R1
   207  	MOVD	new+8(FP), R2
   208  	MOVD	old+16(FP), R3
   209  	MOVD	$SYS_timer_settime, R8
   210  	SVC
   211  	MOVW	R0, ret+24(FP)
   212  	RET
   213  
   214  TEXT runtime·timer_delete(SB),NOSPLIT,$0-12
   215  	MOVW	timerid+0(FP), R0
   216  	MOVD	$SYS_timer_delete, R8
   217  	SVC
   218  	MOVW	R0, ret+8(FP)
   219  	RET
   220  
   221  TEXT runtime·mincore(SB),NOSPLIT|NOFRAME,$0-28
   222  	MOVD	addr+0(FP), R0
   223  	MOVD	n+8(FP), R1
   224  	MOVD	dst+16(FP), R2
   225  	MOVD	$SYS_mincore, R8
   226  	SVC
   227  	MOVW	R0, ret+24(FP)
   228  	RET
   229  
   230  // func walltime() (sec int64, nsec int32)
   231  TEXT runtime·walltime(SB),NOSPLIT,$24-12
   232  	MOVD	RSP, R20	// R20 is unchanged by C code
   233  	MOVD	RSP, R1
   234  
   235  	MOVD	g_m(g), R21	// R21 = m
   236  
   237  	// Set vdsoPC and vdsoSP for SIGPROF traceback.
   238  	// Save the old values on stack and restore them on exit,
   239  	// so this function is reentrant.
   240  	MOVD	m_vdsoPC(R21), R2
   241  	MOVD	m_vdsoSP(R21), R3
   242  	MOVD	R2, 8(RSP)
   243  	MOVD	R3, 16(RSP)
   244  
   245  	MOVD	$ret-8(FP), R2 // caller's SP
   246  	MOVD	LR, m_vdsoPC(R21)
   247  	MOVD	R2, m_vdsoSP(R21)
   248  
   249  	MOVD	m_curg(R21), R0
   250  	CMP	g, R0
   251  	BNE	noswitch
   252  
   253  	MOVD	m_g0(R21), R3
   254  	MOVD	(g_sched+gobuf_sp)(R3), R1	// Set RSP to g0 stack
   255  
   256  noswitch:
   257  	SUB	$16, R1
   258  	BIC	$15, R1	// Align for C code
   259  	MOVD	R1, RSP
   260  
   261  	MOVW	$CLOCK_REALTIME, R0
   262  	MOVD	runtime·vdsoClockgettimeSym(SB), R2
   263  	CBZ	R2, fallback
   264  
   265  	// Store g on gsignal's stack, so if we receive a signal
   266  	// during VDSO code we can find the g.
   267  	// If we don't have a signal stack, we won't receive signal,
   268  	// so don't bother saving g.
   269  	// When using cgo, we already saved g on TLS, also don't save
   270  	// g here.
   271  	// Also don't save g if we are already on the signal stack.
   272  	// We won't get a nested signal.
   273  	MOVBU	runtime·iscgo(SB), R22
   274  	CBNZ	R22, nosaveg
   275  	MOVD	m_gsignal(R21), R22          // g.m.gsignal
   276  	CBZ	R22, nosaveg
   277  	CMP	g, R22
   278  	BEQ	nosaveg
   279  	MOVD	(g_stack+stack_lo)(R22), R22 // g.m.gsignal.stack.lo
   280  	MOVD	g, (R22)
   281  
   282  	BL	(R2)
   283  
   284  	MOVD	ZR, (R22)  // clear g slot, R22 is unchanged by C code
   285  
   286  	B	finish
   287  
   288  nosaveg:
   289  	BL	(R2)
   290  	B	finish
   291  
   292  fallback:
   293  	MOVD	$SYS_clock_gettime, R8
   294  	SVC
   295  
   296  finish:
   297  	MOVD	0(RSP), R3	// sec
   298  	MOVD	8(RSP), R5	// nsec
   299  
   300  	MOVD	R20, RSP	// restore SP
   301  	// Restore vdsoPC, vdsoSP
   302  	// We don't worry about being signaled between the two stores.
   303  	// If we are not in a signal handler, we'll restore vdsoSP to 0,
   304  	// and no one will care about vdsoPC. If we are in a signal handler,
   305  	// we cannot receive another signal.
   306  	MOVD	16(RSP), R1
   307  	MOVD	R1, m_vdsoSP(R21)
   308  	MOVD	8(RSP), R1
   309  	MOVD	R1, m_vdsoPC(R21)
   310  
   311  	MOVD	R3, sec+0(FP)
   312  	MOVW	R5, nsec+8(FP)
   313  	RET
   314  
   315  TEXT runtime·nanotime1(SB),NOSPLIT,$24-8
   316  	MOVD	RSP, R20	// R20 is unchanged by C code
   317  	MOVD	RSP, R1
   318  
   319  	MOVD	g_m(g), R21	// R21 = m
   320  
   321  	// Set vdsoPC and vdsoSP for SIGPROF traceback.
   322  	// Save the old values on stack and restore them on exit,
   323  	// so this function is reentrant.
   324  	MOVD	m_vdsoPC(R21), R2
   325  	MOVD	m_vdsoSP(R21), R3
   326  	MOVD	R2, 8(RSP)
   327  	MOVD	R3, 16(RSP)
   328  
   329  	MOVD	$ret-8(FP), R2 // caller's SP
   330  	MOVD	LR, m_vdsoPC(R21)
   331  	MOVD	R2, m_vdsoSP(R21)
   332  
   333  	MOVD	m_curg(R21), R0
   334  	CMP	g, R0
   335  	BNE	noswitch
   336  
   337  	MOVD	m_g0(R21), R3
   338  	MOVD	(g_sched+gobuf_sp)(R3), R1	// Set RSP to g0 stack
   339  
   340  noswitch:
   341  	SUB	$32, R1
   342  	BIC	$15, R1
   343  	MOVD	R1, RSP
   344  
   345  	MOVW	$CLOCK_MONOTONIC, R0
   346  	MOVD	runtime·vdsoClockgettimeSym(SB), R2
   347  	CBZ	R2, fallback
   348  
   349  	// Store g on gsignal's stack, so if we receive a signal
   350  	// during VDSO code we can find the g.
   351  	// If we don't have a signal stack, we won't receive signal,
   352  	// so don't bother saving g.
   353  	// When using cgo, we already saved g on TLS, also don't save
   354  	// g here.
   355  	// Also don't save g if we are already on the signal stack.
   356  	// We won't get a nested signal.
   357  	MOVBU	runtime·iscgo(SB), R22
   358  	CBNZ	R22, nosaveg
   359  	MOVD	m_gsignal(R21), R22          // g.m.gsignal
   360  	CBZ	R22, nosaveg
   361  	CMP	g, R22
   362  	BEQ	nosaveg
   363  	MOVD	(g_stack+stack_lo)(R22), R22 // g.m.gsignal.stack.lo
   364  	MOVD	g, (R22)
   365  
   366  	BL	(R2)
   367  
   368  	MOVD	ZR, (R22)  // clear g slot, R22 is unchanged by C code
   369  
   370  	B	finish
   371  
   372  nosaveg:
   373  	BL	(R2)
   374  	B	finish
   375  
   376  fallback:
   377  	MOVD	$SYS_clock_gettime, R8
   378  	SVC
   379  
   380  finish:
   381  	MOVD	0(RSP), R3	// sec
   382  	MOVD	8(RSP), R5	// nsec
   383  
   384  	MOVD	R20, RSP	// restore SP
   385  	// Restore vdsoPC, vdsoSP
   386  	// We don't worry about being signaled between the two stores.
   387  	// If we are not in a signal handler, we'll restore vdsoSP to 0,
   388  	// and no one will care about vdsoPC. If we are in a signal handler,
   389  	// we cannot receive another signal.
   390  	MOVD	16(RSP), R1
   391  	MOVD	R1, m_vdsoSP(R21)
   392  	MOVD	8(RSP), R1
   393  	MOVD	R1, m_vdsoPC(R21)
   394  
   395  	// sec is in R3, nsec in R5
   396  	// return nsec in R3
   397  	MOVD	$1000000000, R4
   398  	MUL	R4, R3
   399  	ADD	R5, R3
   400  	MOVD	R3, ret+0(FP)
   401  	RET
   402  
   403  TEXT runtime·rtsigprocmask(SB),NOSPLIT|NOFRAME,$0-28
   404  	MOVW	how+0(FP), R0
   405  	MOVD	new+8(FP), R1
   406  	MOVD	old+16(FP), R2
   407  	MOVW	size+24(FP), R3
   408  	MOVD	$SYS_rt_sigprocmask, R8
   409  	SVC
   410  	CMN	$4095, R0
   411  	BCC	done
   412  	MOVD	$0, R0
   413  	MOVD	R0, (R0)	// crash
   414  done:
   415  	RET
   416  
   417  TEXT runtime·rt_sigaction(SB),NOSPLIT|NOFRAME,$0-36
   418  	MOVD	sig+0(FP), R0
   419  	MOVD	new+8(FP), R1
   420  	MOVD	old+16(FP), R2
   421  	MOVD	size+24(FP), R3
   422  	MOVD	$SYS_rt_sigaction, R8
   423  	SVC
   424  	MOVW	R0, ret+32(FP)
   425  	RET
   426  
   427  // Call the function stored in _cgo_sigaction using the GCC calling convention.
   428  TEXT runtime·callCgoSigaction(SB),NOSPLIT,$0
   429  	MOVD	sig+0(FP), R0
   430  	MOVD	new+8(FP), R1
   431  	MOVD	old+16(FP), R2
   432  	MOVD	 _cgo_sigaction(SB), R3
   433  	SUB	$16, RSP		// reserve 16 bytes for sp-8 where fp may be saved.
   434  	BL	R3
   435  	ADD	$16, RSP
   436  	MOVW	R0, ret+24(FP)
   437  	RET
   438  
   439  TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
   440  	MOVW	sig+8(FP), R0
   441  	MOVD	info+16(FP), R1
   442  	MOVD	ctx+24(FP), R2
   443  	MOVD	fn+0(FP), R11
   444  	BL	(R11)
   445  	RET
   446  
   447  // Called from c-abi, R0: sig, R1: info, R2: cxt
   448  TEXT runtime·sigtramp(SB),NOSPLIT|TOPFRAME,$176
   449  	// Save callee-save registers in the case of signal forwarding.
   450  	// Please refer to https://golang.org/issue/31827 .
   451  	SAVE_R19_TO_R28(8*4)
   452  	SAVE_F8_TO_F15(8*14)
   453  
   454  	// this might be called in external code context,
   455  	// where g is not set.
   456  	// first save R0, because runtime·load_g will clobber it
   457  	MOVW	R0, 8(RSP)
   458  	MOVBU	runtime·iscgo(SB), R0
   459  	CBZ	R0, 2(PC)
   460  	BL	runtime·load_g(SB)
   461  
   462  #ifdef GOEXPERIMENT_regabiargs
   463  	// Restore signum to R0.
   464  	MOVW	8(RSP), R0
   465  	// R1 and R2 already contain info and ctx, respectively.
   466  #else
   467  	MOVD	R1, 16(RSP)
   468  	MOVD	R2, 24(RSP)
   469  #endif
   470  	MOVD	$runtime·sigtrampgo<ABIInternal>(SB), R3
   471  	BL	(R3)
   472  
   473  	// Restore callee-save registers.
   474  	RESTORE_R19_TO_R28(8*4)
   475  	RESTORE_F8_TO_F15(8*14)
   476  
   477  	RET
   478  
   479  // Called from c-abi, R0: sig, R1: info, R2: cxt
   480  TEXT runtime·sigprofNonGoWrapper<>(SB),NOSPLIT,$176
   481  	// Save callee-save registers because it's a callback from c code.
   482  	SAVE_R19_TO_R28(8*4)
   483  	SAVE_F8_TO_F15(8*14)
   484  
   485  #ifdef GOEXPERIMENT_regabiargs
   486  	// R0, R1 and R2 already contain sig, info and ctx, respectively.
   487  #else
   488  	MOVW	R0, 8(RSP)	// sig
   489  	MOVD	R1, 16(RSP)	// info
   490  	MOVD	R2, 24(RSP)	// ctx
   491  #endif
   492  	CALL	runtime·sigprofNonGo<ABIInternal>(SB)
   493  
   494  	// Restore callee-save registers.
   495  	RESTORE_R19_TO_R28(8*4)
   496  	RESTORE_F8_TO_F15(8*14)
   497  	RET
   498  
   499  // Called from c-abi, R0: sig, R1: info, R2: cxt
   500  TEXT runtime·cgoSigtramp(SB),NOSPLIT|NOFRAME,$0
   501  	// The stack unwinder, presumably written in C, may not be able to
   502  	// handle Go frame correctly. So, this function is NOFRAME, and we
   503  	// save/restore LR manually.
   504  	MOVD	LR, R10
   505  	// Save R27, g because they will be clobbered,
   506  	// we need to restore them before jump to sigtramp.
   507  	MOVD	R27, R11
   508  	MOVD	g, R12
   509  
   510  	// If no traceback function, do usual sigtramp.
   511  	MOVD	runtime·cgoTraceback(SB), R6
   512  	CBZ	R6, sigtramp
   513  
   514  	// If no traceback support function, which means that
   515  	// runtime/cgo was not linked in, do usual sigtramp.
   516  	MOVD	_cgo_callers(SB), R7
   517  	CBZ	R7, sigtramp
   518  
   519  	// Figure out if we are currently in a cgo call.
   520  	// If not, just do usual sigtramp.
   521  	// first save R0, because runtime·load_g will clobber it.
   522  	MOVD	R0, R8
   523  	// Set up g register.
   524  	CALL	runtime·load_g(SB)
   525  	MOVD	R8, R0
   526  
   527  	CBZ	g, sigtrampnog // g == nil
   528  	MOVD	g_m(g), R6
   529  	CBZ	R6, sigtramp    // g.m == nil
   530  	MOVW	m_ncgo(R6), R7
   531  	CBZW	R7, sigtramp    // g.m.ncgo = 0
   532  	MOVD	m_curg(R6), R8
   533  	CBZ	R8, sigtramp    // g.m.curg == nil
   534  	MOVD	g_syscallsp(R8), R7
   535  	CBZ	R7,	sigtramp    // g.m.curg.syscallsp == 0
   536  	MOVD	m_cgoCallers(R6), R4 // R4 is the fifth arg in C calling convention.
   537  	CBZ	R4,	sigtramp    // g.m.cgoCallers == nil
   538  	MOVW	m_cgoCallersUse(R6), R8
   539  	CBNZW	R8, sigtramp    // g.m.cgoCallersUse != 0
   540  
   541  	// Jump to a function in runtime/cgo.
   542  	// That function, written in C, will call the user's traceback
   543  	// function with proper unwind info, and will then call back here.
   544  	// The first three arguments, and the fifth, are already in registers.
   545  	// Set the two remaining arguments now.
   546  	MOVD	runtime·cgoTraceback(SB), R3
   547  	MOVD	$runtime·sigtramp(SB), R5
   548  	MOVD	_cgo_callers(SB), R13
   549  	MOVD	R10, LR // restore
   550  	MOVD	R11, R27
   551  	MOVD	R12, g
   552  	B	(R13)
   553  
   554  sigtramp:
   555  	MOVD	R10, LR // restore
   556  	MOVD	R11, R27
   557  	MOVD	R12, g
   558  	B	runtime·sigtramp(SB)
   559  
   560  sigtrampnog:
   561  	// Signal arrived on a non-Go thread. If this is SIGPROF, get a
   562  	// stack trace.
   563  	CMPW	$27, R0 // 27 == SIGPROF
   564  	BNE	sigtramp
   565  
   566  	// Lock sigprofCallersUse (cas from 0 to 1).
   567  	MOVW	$1, R7
   568  	MOVD	$runtime·sigprofCallersUse(SB), R8
   569  load_store_loop:
   570  	LDAXRW	(R8), R9
   571  	CBNZW	R9, sigtramp // Skip stack trace if already locked.
   572  	STLXRW	R7, (R8), R9
   573  	CBNZ	R9, load_store_loop
   574  
   575  	// Jump to the traceback function in runtime/cgo.
   576  	// It will call back to sigprofNonGo, which will ignore the
   577  	// arguments passed in registers.
   578  	// First three arguments to traceback function are in registers already.
   579  	MOVD	runtime·cgoTraceback(SB), R3
   580  	MOVD	$runtime·sigprofCallers(SB), R4
   581  	MOVD	$runtime·sigprofNonGoWrapper<>(SB), R5
   582  	MOVD	_cgo_callers(SB), R13
   583  	MOVD	R10, LR // restore
   584  	MOVD	R11, R27
   585  	MOVD	R12, g
   586  	B	(R13)
   587  
   588  TEXT runtime·sysMmap(SB),NOSPLIT|NOFRAME,$0
   589  	MOVD	addr+0(FP), R0
   590  	MOVD	n+8(FP), R1
   591  	MOVW	prot+16(FP), R2
   592  	MOVW	flags+20(FP), R3
   593  	MOVW	fd+24(FP), R4
   594  	MOVW	off+28(FP), R5
   595  
   596  	MOVD	$SYS_mmap, R8
   597  	SVC
   598  	CMN	$4095, R0
   599  	BCC	ok
   600  	NEG	R0,R0
   601  	MOVD	$0, p+32(FP)
   602  	MOVD	R0, err+40(FP)
   603  	RET
   604  ok:
   605  	MOVD	R0, p+32(FP)
   606  	MOVD	$0, err+40(FP)
   607  	RET
   608  
   609  // Call the function stored in _cgo_mmap using the GCC calling convention.
   610  // This must be called on the system stack.
   611  TEXT runtime·callCgoMmap(SB),NOSPLIT,$0
   612  	MOVD	addr+0(FP), R0
   613  	MOVD	n+8(FP), R1
   614  	MOVW	prot+16(FP), R2
   615  	MOVW	flags+20(FP), R3
   616  	MOVW	fd+24(FP), R4
   617  	MOVW	off+28(FP), R5
   618  	MOVD	_cgo_mmap(SB), R9
   619  	SUB	$16, RSP		// reserve 16 bytes for sp-8 where fp may be saved.
   620  	BL	R9
   621  	ADD	$16, RSP
   622  	MOVD	R0, ret+32(FP)
   623  	RET
   624  
   625  TEXT runtime·sysMunmap(SB),NOSPLIT|NOFRAME,$0
   626  	MOVD	addr+0(FP), R0
   627  	MOVD	n+8(FP), R1
   628  	MOVD	$SYS_munmap, R8
   629  	SVC
   630  	CMN	$4095, R0
   631  	BCC	cool
   632  	MOVD	R0, 0xf0(R0)
   633  cool:
   634  	RET
   635  
   636  // Call the function stored in _cgo_munmap using the GCC calling convention.
   637  // This must be called on the system stack.
   638  TEXT runtime·callCgoMunmap(SB),NOSPLIT,$0
   639  	MOVD	addr+0(FP), R0
   640  	MOVD	n+8(FP), R1
   641  	MOVD	_cgo_munmap(SB), R9
   642  	SUB	$16, RSP		// reserve 16 bytes for sp-8 where fp may be saved.
   643  	BL	R9
   644  	ADD	$16, RSP
   645  	RET
   646  
   647  TEXT runtime·madvise(SB),NOSPLIT|NOFRAME,$0
   648  	MOVD	addr+0(FP), R0
   649  	MOVD	n+8(FP), R1
   650  	MOVW	flags+16(FP), R2
   651  	MOVD	$SYS_madvise, R8
   652  	SVC
   653  	MOVW	R0, ret+24(FP)
   654  	RET
   655  
   656  // int64 futex(int32 *uaddr, int32 op, int32 val,
   657  //	struct timespec *timeout, int32 *uaddr2, int32 val2);
   658  TEXT runtime·futex(SB),NOSPLIT|NOFRAME,$0
   659  	MOVD	addr+0(FP), R0
   660  	MOVW	op+8(FP), R1
   661  	MOVW	val+12(FP), R2
   662  	MOVD	ts+16(FP), R3
   663  	MOVD	addr2+24(FP), R4
   664  	MOVW	val3+32(FP), R5
   665  	MOVD	$SYS_futex, R8
   666  	SVC
   667  	MOVW	R0, ret+40(FP)
   668  	RET
   669  
   670  // int64 clone(int32 flags, void *stk, M *mp, G *gp, void (*fn)(void));
   671  TEXT runtime·clone(SB),NOSPLIT|NOFRAME,$0
   672  	MOVW	flags+0(FP), R0
   673  	MOVD	stk+8(FP), R1
   674  
   675  	// Copy mp, gp, fn off parent stack for use by child.
   676  	MOVD	mp+16(FP), R10
   677  	MOVD	gp+24(FP), R11
   678  	MOVD	fn+32(FP), R12
   679  
   680  	MOVD	R10, -8(R1)
   681  	MOVD	R11, -16(R1)
   682  	MOVD	R12, -24(R1)
   683  	MOVD	$1234, R10
   684  	MOVD	R10, -32(R1)
   685  
   686  	MOVD	$SYS_clone, R8
   687  	SVC
   688  
   689  	// In parent, return.
   690  	CMP	ZR, R0
   691  	BEQ	child
   692  	MOVW	R0, ret+40(FP)
   693  	RET
   694  child:
   695  
   696  	// In child, on new stack.
   697  	MOVD	-32(RSP), R10
   698  	MOVD	$1234, R0
   699  	CMP	R0, R10
   700  	BEQ	good
   701  	MOVD	$0, R0
   702  	MOVD	R0, (R0)	// crash
   703  
   704  good:
   705  	// Initialize m->procid to Linux tid
   706  	MOVD	$SYS_gettid, R8
   707  	SVC
   708  
   709  	MOVD	-24(RSP), R12     // fn
   710  	MOVD	-16(RSP), R11     // g
   711  	MOVD	-8(RSP), R10      // m
   712  
   713  	CMP	$0, R10
   714  	BEQ	nog
   715  	CMP	$0, R11
   716  	BEQ	nog
   717  
   718  	MOVD	R0, m_procid(R10)
   719  
   720  	// TODO: setup TLS.
   721  
   722  	// In child, set up new stack
   723  	MOVD	R10, g_m(R11)
   724  	MOVD	R11, g
   725  	//CALL	runtime·stackcheck(SB)
   726  
   727  nog:
   728  	// Call fn
   729  	MOVD	R12, R0
   730  	BL	(R0)
   731  
   732  	// It shouldn't return.	 If it does, exit that thread.
   733  	MOVW	$111, R0
   734  again:
   735  	MOVD	$SYS_exit, R8
   736  	SVC
   737  	B	again	// keep exiting
   738  
   739  TEXT runtime·sigaltstack(SB),NOSPLIT|NOFRAME,$0
   740  	MOVD	new+0(FP), R0
   741  	MOVD	old+8(FP), R1
   742  	MOVD	$SYS_sigaltstack, R8
   743  	SVC
   744  	CMN	$4095, R0
   745  	BCC	ok
   746  	MOVD	$0, R0
   747  	MOVD	R0, (R0)	// crash
   748  ok:
   749  	RET
   750  
   751  TEXT runtime·osyield(SB),NOSPLIT|NOFRAME,$0
   752  	MOVD	$SYS_sched_yield, R8
   753  	SVC
   754  	RET
   755  
   756  TEXT runtime·sched_getaffinity(SB),NOSPLIT|NOFRAME,$0
   757  	MOVD	pid+0(FP), R0
   758  	MOVD	len+8(FP), R1
   759  	MOVD	buf+16(FP), R2
   760  	MOVD	$SYS_sched_getaffinity, R8
   761  	SVC
   762  	MOVW	R0, ret+24(FP)
   763  	RET
   764  
   765  // int32 runtime·epollcreate(int32 size);
   766  TEXT runtime·epollcreate(SB),NOSPLIT|NOFRAME,$0
   767  	MOVW	$0, R0
   768  	MOVD	$SYS_epoll_create1, R8
   769  	SVC
   770  	MOVW	R0, ret+8(FP)
   771  	RET
   772  
   773  // int32 runtime·epollcreate1(int32 flags);
   774  TEXT runtime·epollcreate1(SB),NOSPLIT|NOFRAME,$0
   775  	MOVW	flags+0(FP), R0
   776  	MOVD	$SYS_epoll_create1, R8
   777  	SVC
   778  	MOVW	R0, ret+8(FP)
   779  	RET
   780  
   781  // func epollctl(epfd, op, fd int32, ev *epollEvent) int
   782  TEXT runtime·epollctl(SB),NOSPLIT|NOFRAME,$0
   783  	MOVW	epfd+0(FP), R0
   784  	MOVW	op+4(FP), R1
   785  	MOVW	fd+8(FP), R2
   786  	MOVD	ev+16(FP), R3
   787  	MOVD	$SYS_epoll_ctl, R8
   788  	SVC
   789  	MOVW	R0, ret+24(FP)
   790  	RET
   791  
   792  // int32 runtime·epollwait(int32 epfd, EpollEvent *ev, int32 nev, int32 timeout);
   793  TEXT runtime·epollwait(SB),NOSPLIT|NOFRAME,$0
   794  	MOVW	epfd+0(FP), R0
   795  	MOVD	ev+8(FP), R1
   796  	MOVW	nev+16(FP), R2
   797  	MOVW	timeout+20(FP), R3
   798  	MOVD	$0, R4
   799  	MOVD	$SYS_epoll_pwait, R8
   800  	SVC
   801  	MOVW	R0, ret+24(FP)
   802  	RET
   803  
   804  // void runtime·closeonexec(int32 fd);
   805  TEXT runtime·closeonexec(SB),NOSPLIT|NOFRAME,$0
   806  	MOVW	fd+0(FP), R0  // fd
   807  	MOVD	$2, R1	// F_SETFD
   808  	MOVD	$1, R2	// FD_CLOEXEC
   809  	MOVD	$SYS_fcntl, R8
   810  	SVC
   811  	RET
   812  
   813  // int access(const char *name, int mode)
   814  TEXT runtime·access(SB),NOSPLIT,$0-20
   815  	MOVD	$AT_FDCWD, R0
   816  	MOVD	name+0(FP), R1
   817  	MOVW	mode+8(FP), R2
   818  	MOVD	$SYS_faccessat, R8
   819  	SVC
   820  	MOVW	R0, ret+16(FP)
   821  	RET
   822  
   823  // int connect(int fd, const struct sockaddr *addr, socklen_t len)
   824  TEXT runtime·connect(SB),NOSPLIT,$0-28
   825  	MOVW	fd+0(FP), R0
   826  	MOVD	addr+8(FP), R1
   827  	MOVW	len+16(FP), R2
   828  	MOVD	$SYS_connect, R8
   829  	SVC
   830  	MOVW	R0, ret+24(FP)
   831  	RET
   832  
   833  // int socket(int domain, int typ, int prot)
   834  TEXT runtime·socket(SB),NOSPLIT,$0-20
   835  	MOVW	domain+0(FP), R0
   836  	MOVW	typ+4(FP), R1
   837  	MOVW	prot+8(FP), R2
   838  	MOVD	$SYS_socket, R8
   839  	SVC
   840  	MOVW	R0, ret+16(FP)
   841  	RET
   842  
   843  // func sbrk0() uintptr
   844  TEXT runtime·sbrk0(SB),NOSPLIT,$0-8
   845  	// Implemented as brk(NULL).
   846  	MOVD	$0, R0
   847  	MOVD	$SYS_brk, R8
   848  	SVC
   849  	MOVD	R0, ret+0(FP)
   850  	RET
   851  
   852  TEXT runtime·sigreturn(SB),NOSPLIT,$0-0
   853  	RET
   854  

View as plain text