Text file src/runtime/sys_linux_loong64.s

     1  // Copyright 2022 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 loong64, Linux
     7  //
     8  
     9  #include "go_asm.h"
    10  #include "go_tls.h"
    11  #include "textflag.h"
    12  #include "cgo/abi_loong64.h"
    13  
    14  #define AT_FDCWD	-100
    15  #define CLOCK_REALTIME	0
    16  #define CLOCK_MONOTONIC	1
    17  
    18  #define SYS_exit		93
    19  #define SYS_read		63
    20  #define SYS_write		64
    21  #define SYS_close		57
    22  #define SYS_getpid		172
    23  #define SYS_kill		129
    24  #define SYS_mmap		222
    25  #define SYS_munmap		215
    26  #define SYS_setitimer		103
    27  #define SYS_clone		220
    28  #define SYS_nanosleep		101
    29  #define SYS_sched_yield		124
    30  #define SYS_rt_sigreturn	139
    31  #define SYS_rt_sigaction	134
    32  #define SYS_rt_sigprocmask	135
    33  #define SYS_sigaltstack		132
    34  #define SYS_madvise		233
    35  #define SYS_mincore		232
    36  #define SYS_gettid		178
    37  #define SYS_futex		98
    38  #define SYS_sched_getaffinity	123
    39  #define SYS_exit_group		94
    40  #define SYS_tgkill		131
    41  #define SYS_openat		56
    42  #define SYS_clock_gettime	113
    43  #define SYS_brk			214
    44  #define SYS_pipe2		59
    45  #define SYS_timer_create	107
    46  #define SYS_timer_settime	110
    47  #define SYS_timer_delete	111
    48  
    49  // func exit(code int32)
    50  TEXT runtime·exit<ABIInternal>(SB),NOSPLIT,$0
    51  	MOVV	$SYS_exit_group, R11
    52  	SYSCALL
    53  	RET
    54  
    55  // func exitThread(wait *atomic.Uint32)
    56  TEXT runtime·exitThread<ABIInternal>(SB),NOSPLIT|NOFRAME,$0
    57  	// We're done using the stack.
    58  	DBAR	$0x12	// StoreRelease barrier
    59  	MOVW	R0, (R4)
    60  	MOVW	$0, R4	// exit code
    61  	MOVV	$SYS_exit, R11
    62  	SYSCALL
    63  	JMP	0(PC)
    64  
    65  // func open(name *byte, mode, perm int32) int32
    66  TEXT runtime·open<ABIInternal>(SB),NOSPLIT,$0
    67  	// before:
    68  	//    R4:  name, R5: mode, R6: perm
    69  	// after:
    70  	//    R4: AT_FDCWD, R5: name, R6: mode, R7: perm
    71  	MOVW	R6, R7
    72  	MOVW	R5, R6
    73  	MOVV	R4, R5
    74  	MOVW	$AT_FDCWD, R4 // AT_FDCWD, so this acts like open
    75  
    76  	MOVV	$SYS_openat, R11
    77  	SYSCALL
    78  	MOVW	$-4096, R5
    79  	BGEU	R5, R4, 2(PC)
    80  	MOVW	$-1, R4
    81  	RET
    82  
    83  // func closefd(fd int32) int32
    84  TEXT runtime·closefd<ABIInternal>(SB),NOSPLIT,$0
    85  	MOVV	$SYS_close, R11
    86  	SYSCALL
    87  	MOVW	$-4096, R5
    88  	BGEU	R5, R4, 2(PC)
    89  	MOVW	$-1, R4
    90  	RET
    91  
    92  // func write1(fd uintptr, p unsafe.Pointer, n int32) int32
    93  TEXT runtime·write1<ABIInternal>(SB),NOSPLIT,$0
    94  	MOVV	$SYS_write, R11
    95  	SYSCALL
    96  	RET
    97  
    98  // func read(fd int32, p unsafe.Pointer, n int32) int32
    99  TEXT runtime·read<ABIInternal>(SB),NOSPLIT,$0
   100  	MOVV	$SYS_read, R11
   101  	SYSCALL
   102  	RET
   103  
   104  // func pipe2(flags int32) (r, w int32, errno int32)
   105  TEXT runtime·pipe2(SB),NOSPLIT|NOFRAME,$0-20
   106  	MOVV	$r+8(FP), R4
   107  	MOVW	flags+0(FP), R5
   108  	MOVV	$SYS_pipe2, R11
   109  	SYSCALL
   110  	MOVW	R4, errno+16(FP)
   111  	RET
   112  
   113  // func usleep(usec uint32)
   114  TEXT runtime·usleep<ABIInternal>(SB),NOSPLIT,$16
   115  	MOVV	$1000, R6
   116  	MULVU	R6, R4, R4
   117  	MOVV	$1000000000, R6
   118  
   119  	DIVVU	R6, R4, R5	// ts->tv_sec
   120  	REMVU	R6, R4, R8	// ts->tv_nsec
   121  	MOVV	R5, 8(R3)
   122  	MOVV	R8, 16(R3)
   123  
   124  	// nanosleep(&ts, 0)
   125  	ADDV	$8, R3, R4
   126  	MOVV	R0, R5
   127  	MOVV	$SYS_nanosleep, R11
   128  	SYSCALL
   129  	RET
   130  
   131  // func gettid() uint32
   132  TEXT runtime·gettid<ABIInternal>(SB),NOSPLIT,$0
   133  	MOVV	$SYS_gettid, R11
   134  	SYSCALL
   135  	RET
   136  
   137  // func raise(sig uint32)
   138  TEXT runtime·raise<ABIInternal>(SB),NOSPLIT,$0
   139  	MOVW	R4, R24 // backup sig
   140  	MOVV	$SYS_getpid, R11
   141  	SYSCALL
   142  	MOVW	R4, R23
   143  	MOVV	$SYS_gettid, R11
   144  	SYSCALL
   145  	MOVW	R4, R5	// arg 2 tid
   146  	MOVW	R23, R4	// arg 1 pid
   147  	MOVW	R24, R6	// arg 3
   148  	MOVV	$SYS_tgkill, R11
   149  	SYSCALL
   150  	RET
   151  
   152  // func raiseproc(sig uint32)
   153  TEXT runtime·raiseproc<ABIInternal>(SB),NOSPLIT,$0
   154  	MOVW	R4, R24 // backup sig
   155  	MOVV	$SYS_getpid, R11
   156  	SYSCALL
   157  	//MOVW	R4, R4	// arg 1 pid
   158  	MOVW	R24, R5	// arg 2
   159  	MOVV	$SYS_kill, R11
   160  	SYSCALL
   161  	RET
   162  
   163  // func getpid() int
   164  TEXT ·getpid<ABIInternal>(SB),NOSPLIT,$0
   165  	MOVV	$SYS_getpid, R11
   166  	SYSCALL
   167  	RET
   168  
   169  // func tgkill(tgid, tid, sig int)
   170  TEXT ·tgkill<ABIInternal>(SB),NOSPLIT,$0
   171  	MOVV	$SYS_tgkill, R11
   172  	SYSCALL
   173  	RET
   174  
   175  // func setitimer(mode int32, new, old *itimerval)
   176  TEXT runtime·setitimer<ABIInternal>(SB),NOSPLIT,$0
   177  	MOVV	$SYS_setitimer, R11
   178  	SYSCALL
   179  	RET
   180  
   181  // func timer_create(clockid int32, sevp *sigevent, timerid *int32) int32
   182  TEXT runtime·timer_create<ABIInternal>(SB),NOSPLIT,$0
   183  	MOVV	$SYS_timer_create, R11
   184  	SYSCALL
   185  	RET
   186  
   187  // func timer_settime(timerid int32, flags int32, new, old *itimerspec) int32
   188  TEXT runtime·timer_settime<ABIInternal>(SB),NOSPLIT,$0
   189  	MOVV	$SYS_timer_settime, R11
   190  	SYSCALL
   191  	RET
   192  
   193  // func timer_delete(timerid int32) int32
   194  TEXT runtime·timer_delete<ABIInternal>(SB),NOSPLIT,$0
   195  	MOVV	$SYS_timer_delete, R11
   196  	SYSCALL
   197  	RET
   198  
   199  // func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
   200  TEXT runtime·mincore<ABIInternal>(SB),NOSPLIT,$0
   201  	MOVV	$SYS_mincore, R11
   202  	SYSCALL
   203  	RET
   204  
   205  // func walltime() (sec int64, nsec int32)
   206  TEXT runtime·walltime<ABIInternal>(SB),NOSPLIT,$24
   207  #ifdef GOEXPERIMENT_runtimesecret
   208  	MOVW	g_secret(g), R23
   209  	BEQ	R23, nosecret
   210  	JAL	·secretEraseRegisters(SB)
   211  nosecret:
   212  #endif
   213  	MOVV	R3, R23	// R23 is unchanged by C code
   214  
   215  	MOVV	g_m(g), R24	// R24 = m
   216  
   217  	// Set vdsoPC and vdsoSP for SIGPROF traceback.
   218  	// Save the old values on stack and restore them on exit,
   219  	// so this function is reentrant.
   220  	MOVV	m_vdsoPC(R24), R11
   221  	MOVV	m_vdsoSP(R24), R7
   222  	MOVV	R11, 8(R3)
   223  	MOVV	R7, 16(R3)
   224  
   225  	MOVV    $ret-8(FP), R11 // caller's SP
   226  	MOVV	R1, m_vdsoPC(R24)
   227  	MOVV	R11, m_vdsoSP(R24)
   228  
   229  	MOVV	m_curg(R24), R4
   230  	BNE	R4, g, noswitch
   231  
   232  	MOVV	m_g0(R24), R4
   233  	MOVV	(g_sched+gobuf_sp)(R4), R3	// Set SP to g0 stack
   234  
   235  noswitch:
   236  	SUBV	$16, R3
   237  	AND	$~15, R3	// Align for C code
   238  
   239  	MOVW	$CLOCK_REALTIME, R4
   240  	MOVV	$0(R3), R5
   241  
   242  	MOVV	runtime·vdsoClockgettimeSym(SB), R20
   243  	BEQ	R20, fallback
   244  
   245  	// Store g on gsignal's stack, see sys_linux_arm64.s for detail
   246  	MOVBU	runtime·iscgo(SB), R25
   247  	BNE	R25, nosaveg
   248  
   249  	MOVV	m_gsignal(R24), R25	// g.m.gsignal
   250  	BEQ	R25, nosaveg
   251  	BEQ	g, R25, nosaveg
   252  
   253  	MOVV	(g_stack+stack_lo)(R25), R25	// g.m.gsignal.stack.lo
   254  	MOVV	g, (R25)
   255  
   256  	JAL	(R20)
   257  
   258  	MOVV	R0, (R25)
   259  	JMP	finish
   260  
   261  nosaveg:
   262  	JAL	(R20)
   263  
   264  finish:
   265  	MOVV	0(R3), R4	// sec
   266  	MOVV	8(R3), R5	// nsec
   267  
   268  	MOVV	R23, R3	// restore SP
   269  	// Restore vdsoPC, vdsoSP
   270  	// We don't worry about being signaled between the two stores.
   271  	// If we are not in a signal handler, we'll restore vdsoSP to 0,
   272  	// and no one will care about vdsoPC. If we are in a signal handler,
   273  	// we cannot receive another signal.
   274  	MOVV	16(R3), R25
   275  	MOVV	R25, m_vdsoSP(R24)
   276  	MOVV	8(R3), R25
   277  	MOVV	R25, m_vdsoPC(R24)
   278  
   279  	RET
   280  
   281  fallback:
   282  	MOVV	$SYS_clock_gettime, R11
   283  	SYSCALL
   284  	JMP finish
   285  
   286  // func nanotime1() int64
   287  TEXT runtime·nanotime1<ABIInternal>(SB),NOSPLIT,$24
   288  #ifdef GOEXPERIMENT_runtimesecret
   289  	MOVW	g_secret(g), R23
   290  	BEQ	R23, nosecret
   291  	JAL	·secretEraseRegisters(SB)
   292  nosecret:
   293  #endif
   294  	MOVV	R3, R23	// R23 is unchanged by C code
   295  
   296  	MOVV	g_m(g), R24	// R24 = m
   297  
   298  	// Set vdsoPC and vdsoSP for SIGPROF traceback.
   299  	// Save the old values on stack and restore them on exit,
   300  	// so this function is reentrant.
   301  	MOVV	m_vdsoPC(R24), R11
   302  	MOVV	m_vdsoSP(R24), R7
   303  	MOVV	R11, 8(R3)
   304  	MOVV	R7, 16(R3)
   305  
   306  	MOVV    $ret-8(FP), R11 // caller's SP
   307  	MOVV	R1, m_vdsoPC(R24)
   308  	MOVV	R11, m_vdsoSP(R24)
   309  
   310  	MOVV	m_curg(R24), R4
   311  	BNE	R4, g, noswitch
   312  
   313  	MOVV	m_g0(R24), R4
   314  	MOVV	(g_sched+gobuf_sp)(R4), R3	// Set SP to g0 stack
   315  
   316  noswitch:
   317  	SUBV	$16, R3
   318  	AND	$~15, R3	// Align for C code
   319  
   320  	MOVW	$CLOCK_MONOTONIC, R4
   321  	MOVV	$0(R3), R5
   322  
   323  	MOVV	runtime·vdsoClockgettimeSym(SB), R20
   324  	BEQ	R20, fallback
   325  
   326  	// Store g on gsignal's stack, see sys_linux_arm64.s for detail
   327  	MOVBU	runtime·iscgo(SB), R25
   328  	BNE	R25, nosaveg
   329  
   330  	MOVV	m_gsignal(R24), R25	// g.m.gsignal
   331  	BEQ	R25, nosaveg
   332  	BEQ	g, R25, nosaveg
   333  
   334  	MOVV	(g_stack+stack_lo)(R25), R25	// g.m.gsignal.stack.lo
   335  	MOVV	g, (R25)
   336  
   337  	JAL	(R20)
   338  
   339  	MOVV	R0, (R25)
   340  	JMP	finish
   341  
   342  nosaveg:
   343  	JAL	(R20)
   344  
   345  finish:
   346  	MOVV	0(R3), R7	// sec
   347  	MOVV	8(R3), R5	// nsec
   348  
   349  	MOVV	R23, R3	// restore SP
   350  	// Restore vdsoPC, vdsoSP
   351  	// We don't worry about being signaled between the two stores.
   352  	// If we are not in a signal handler, we'll restore vdsoSP to 0,
   353  	// and no one will care about vdsoPC. If we are in a signal handler,
   354  	// we cannot receive another signal.
   355  	MOVV	16(R3), R25
   356  	MOVV	R25, m_vdsoSP(R24)
   357  	MOVV	8(R3), R25
   358  	MOVV	R25, m_vdsoPC(R24)
   359  
   360  	// sec is in R7, nsec in R5
   361  	// return nsec in R7
   362  	MOVV	$1000000000, R4
   363  	MULVU	R4, R7, R7
   364  	ADDVU	R5, R7, R4
   365  	RET
   366  
   367  fallback:
   368  	MOVV	$SYS_clock_gettime, R11
   369  	SYSCALL
   370  	JMP	finish
   371  
   372  // func rtsigprocmask(how int32, new, old *sigset, size int32)
   373  TEXT runtime·rtsigprocmask<ABIInternal>(SB),NOSPLIT,$0
   374  	MOVV	$SYS_rt_sigprocmask, R11
   375  	SYSCALL
   376  	MOVW	$-4096, R5
   377  	BGEU	R5, R4, 2(PC)
   378  	MOVV	R0, 0xf1(R0)	// crash
   379  	RET
   380  
   381  // func rt_sigaction(sig uintptr, new, old *sigactiont, size uintptr) int32
   382  TEXT runtime·rt_sigaction<ABIInternal>(SB),NOSPLIT,$0
   383  	MOVV	$SYS_rt_sigaction, R11
   384  	SYSCALL
   385  	RET
   386  
   387  // Call the function stored in _cgo_sigaction using the GCC calling convention.
   388  TEXT runtime·callCgoSigaction<ABIInternal>(SB),NOSPLIT,$0
   389  	// R4: sig, R5: new, R6: old
   390  	MOVV    _cgo_sigaction(SB), R7
   391  	SUBV    $16, R3 // reserve 16 bytes for sp-8 where fp may be saved.
   392  	JAL     (R7)
   393  	ADDV    $16, R3
   394  	MOVW    R4, R4
   395  	RET
   396  
   397  // func sigfwd(fn uintptr, sig uint32, info *siginfo, ctx unsafe.Pointer)
   398  TEXT runtime·sigfwd<ABIInternal>(SB),NOSPLIT,$0
   399  	// before:
   400  	//    R4:  fn, R5: sig, R6: info, R7: ctx
   401  	// after:
   402  	//    R20: fn, R4: sig, R5: info, R6: ctx
   403  	MOVV	R4, R20
   404  	MOVV	R5, R4
   405  	MOVV	R6, R5
   406  	MOVV	R7, R6
   407  	JAL	(R20)
   408  	RET
   409  
   410  // Called from c-abi, R4: sig, R5: info, R6: cxt
   411  // func sigtramp(signo, ureg, ctxt unsafe.Pointer)
   412  TEXT runtime·sigtramp(SB),NOSPLIT|TOPFRAME,$168
   413  	// Save callee-save registers in the case of signal forwarding.
   414  	// Please refer to https://golang.org/issue/31827 .
   415  	SAVE_R22_TO_R31((4*8))
   416  	SAVE_F24_TO_F31((14*8))
   417  
   418  	// this might be called in external code context,
   419  	// where g is not set.
   420  	MOVB	runtime·iscgo(SB), R7
   421  	BEQ	R7, 2(PC)
   422  	JAL	runtime·load_g(SB)
   423  
   424  	// R5 and R6 already contain info and ctx, respectively.
   425  	MOVV	$runtime·sigtrampgo<ABIInternal>(SB), R7
   426  	JAL	(R7)
   427  
   428  	// Restore callee-save registers.
   429  	RESTORE_R22_TO_R31((4*8))
   430  	RESTORE_F24_TO_F31((14*8))
   431  
   432  	RET
   433  
   434  // Called from c-abi, R4: sig, R5: info, R6: cxt
   435  TEXT runtime·sigprofNonGoWrapper<>(SB),NOSPLIT,$168
   436  	// Save callee-save registers because it's a callback from c code.
   437  	SAVE_R22_TO_R31((4*8))
   438  	SAVE_F24_TO_F31((14*8))
   439  
   440  	// R4, R5 and R6 already contain sig, info and ctx, respectively.
   441  	CALL	runtime·sigprofNonGo<ABIInternal>(SB)
   442  
   443  	// Restore callee-save registers.
   444  	RESTORE_R22_TO_R31((4*8))
   445  	RESTORE_F24_TO_F31((14*8))
   446  	RET
   447  
   448  // Called from c-abi, R4: sig, R5: info, R6: cxt
   449  TEXT runtime·cgoSigtramp(SB),NOSPLIT|NOFRAME,$0
   450  	// The stack unwinder, presumably written in C, may not be able to
   451  	// handle Go frame correctly. So, this function is NOFRAME, and we
   452  	// save/restore LR manually.
   453  	MOVV	R1, R12
   454  	// Save R30, g because they will be clobbered,
   455  	// we need to restore them before jump to sigtramp.
   456  	MOVV	R30, R13
   457  	MOVV	g, R14
   458  
   459  	// If no traceback function, do usual sigtramp.
   460  	MOVV	runtime·cgoTraceback(SB), R15
   461  	BEQ	R15, sigtramp
   462  
   463  	// If no traceback support function, which means that
   464  	// runtime/cgo was not linked in, do usual sigtramp.
   465  	MOVV	_cgo_callers(SB), R15
   466  	BEQ	R15, sigtramp
   467  
   468  	// Figure out if we are currently in a cgo call.
   469  	// If not, just do usual sigtramp.
   470  	CALL	runtime·load_g(SB)
   471  	BEQ	g, sigtrampnog // g == nil
   472  
   473  	MOVV	g_m(g), R15
   474  	BEQ	R15, sigtramp    // g.m == nil
   475  	MOVW	m_ncgo(R15), R16
   476  	BEQ	R16, sigtramp    // g.m.ncgo = 0
   477  	MOVV	m_curg(R15), R16
   478  	BEQ	R16, sigtramp    // g.m.curg == nil
   479  	MOVV	g_syscallsp(R16), R17
   480  	BEQ     R17, sigtramp    // g.m.curg.syscallsp == 0
   481  	MOVV	m_cgoCallers(R15), R8 // R8 is the fifth arg in C calling convention.
   482  	BEQ	R8, sigtramp    // g.m.cgoCallers == nil
   483  	MOVW	m_cgoCallersUse(R15), R16
   484  	BNE	R16, sigtramp    // g.m.cgoCallersUse != 0
   485  
   486  	// Jump to a function in runtime/cgo.
   487  	// That function, written in C, will call the user's traceback
   488  	// function with proper unwind info, and will then call back here.
   489  	// The first three arguments, and the fifth, are already in registers.
   490  	// Set the two remaining arguments now.
   491  	MOVV	runtime·cgoTraceback(SB), R7
   492  	MOVV	$runtime·sigtramp(SB), R9
   493  	MOVV	_cgo_callers(SB), R15
   494  	MOVV	R12, R1 // restore
   495  	MOVV	R13, R30
   496  	MOVV	R14, g
   497  	JMP	(R15)
   498  
   499  sigtramp:
   500  	MOVV	R12, R1 // restore
   501  	MOVV	R13, R30
   502  	MOVV	R14, g
   503  	JMP	runtime·sigtramp(SB)
   504  
   505  sigtrampnog:
   506  	// Signal arrived on a non-Go thread. If this is SIGPROF, get a
   507  	// stack trace.
   508  	MOVW    $27, R15 // 27 == SIGPROF
   509  	BNE     R4, R15, sigtramp
   510  
   511  	MOVV    $runtime·sigprofCallersUse(SB), R16
   512  	DBAR	$0x14
   513  cas_again:
   514  	MOVV    $1, R15
   515  	LL	(R16), R17
   516  	BNE	R17, fail
   517  	SC	R15, (R16)
   518  	BEQ	R15, cas_again
   519  	DBAR    $0x14
   520  
   521  	// Jump to the traceback function in runtime/cgo.
   522  	// It will call back to sigprofNonGo, which will ignore the
   523  	// arguments passed in registers.
   524  	// First three arguments to traceback function are in registers already.
   525  	MOVV	runtime·cgoTraceback(SB), R7
   526  	MOVV	$runtime·sigprofCallers(SB), R8
   527  	MOVV	$runtime·sigprofNonGoWrapper<>(SB), R9
   528  	MOVV	_cgo_callers(SB), R15
   529  	MOVV	R12, R1 // restore
   530  	MOVV	R13, R30
   531  	MOVV	R14, g
   532  	JMP	(R15)
   533  
   534  fail:
   535  	DBAR    $0x14
   536  	JMP     sigtramp
   537  
   538  // func sysMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (p unsafe.Pointer, err int)
   539  TEXT runtime·sysMmap<ABIInternal>(SB),NOSPLIT,$0
   540  	MOVV	$SYS_mmap, R11
   541  	SYSCALL
   542  	MOVW	$-4096, R5
   543  	BGEU	R5, R4, ok
   544  	SUBVU	R4, R0, R5
   545  	MOVV	$0, R4
   546  	RET
   547  ok:
   548  	MOVV	$0, R5
   549  	RET
   550  
   551  // Call the function stored in _cgo_mmap using the GCC calling convention.
   552  // This must be called on the system stack.
   553  // func callCgoMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) uintptr
   554  TEXT runtime·callCgoMmap<ABIInternal>(SB),NOSPLIT,$0
   555  	MOVV	_cgo_mmap(SB), R13
   556  	SUBV	$16, R3		// reserve 16 bytes for sp-8 where fp may be saved.
   557  	JAL	(R13)
   558  	ADDV	$16, R3
   559  	MOVV	R4, R4
   560  	RET
   561  
   562  // func sysMunmap(addr unsafe.Pointer, n uintptr)
   563  TEXT runtime·sysMunmap<ABIInternal>(SB),NOSPLIT,$0
   564  	MOVV	$SYS_munmap, R11
   565  	SYSCALL
   566  	MOVW	$-4096, R5
   567  	BGEU	R5, R4, 2(PC)
   568  	MOVV	R0, 0xf3(R0)	// crash
   569  	RET
   570  
   571  // Call the function stored in _cgo_munmap using the GCC calling convention.
   572  // This must be called on the system stack.
   573  // func callCgoMunmap(addr unsafe.Pointer, n uintptr)
   574  TEXT runtime·callCgoMunmap<ABIInternal>(SB),NOSPLIT,$0
   575  	MOVV	_cgo_munmap(SB), R13
   576  	SUBV	$16, R3		// reserve 16 bytes for sp-8 where fp may be saved.
   577  	JAL	(R13)
   578  	ADDV	$16, R3
   579  	RET
   580  
   581  // func madvise(addr unsafe.Pointer, n uintptr, flags int32)
   582  TEXT runtime·madvise<ABIInternal>(SB),NOSPLIT,$0
   583  	MOVV	$SYS_madvise, R11
   584  	SYSCALL
   585  	RET
   586  
   587  // func futex(addr unsafe.Pointer, op int32, val uint32, ts, addr2 unsafe.Pointer, val3 uint32) int32
   588  TEXT runtime·futex<ABIInternal>(SB),NOSPLIT,$0
   589  	MOVV	$SYS_futex, R11
   590  	SYSCALL
   591  	RET
   592  
   593  // int64 clone(int32 flags, void *stk, M *mp, G *gp, void (*fn)(void));
   594  TEXT runtime·clone<ABIInternal>(SB),NOSPLIT,$0
   595  	// Copy mp, gp, fn off parent stack for use by child.
   596  	// Careful: Linux system call clobbers ???.
   597  	MOVV	R6, R23
   598  	MOVV	R7, R24
   599  	MOVV	R8, R25
   600  
   601  	MOVV	R23, -8(R5)
   602  	MOVV	R24, -16(R5)
   603  	MOVV	R25, -24(R5)
   604  	MOVV	$1234, R23
   605  	MOVV	R23, -32(R5)
   606  
   607  	MOVV	$SYS_clone, R11
   608  	SYSCALL
   609  
   610  	// In parent, return.
   611  	BEQ	R4, 2(PC)
   612  	RET
   613  
   614  	// In child, on new stack.
   615  	MOVV	-32(R3), R23
   616  	MOVV	$1234, R19
   617  	BEQ	R23, R19, 2(PC)
   618  	MOVV	R0, 0(R0)
   619  
   620  	// Initialize m->procid to Linux tid
   621  	MOVV	$SYS_gettid, R11
   622  	SYSCALL
   623  
   624  	MOVV	-24(R3), R25		// fn
   625  	MOVV	-16(R3), R24		// g
   626  	MOVV	-8(R3), R23		// m
   627  
   628  	BEQ	R23, nog
   629  	BEQ	R24, nog
   630  
   631  	MOVV	R4, m_procid(R23)
   632  
   633  	// TODO: setup TLS.
   634  
   635  	// In child, set up new stack
   636  	MOVV	R23, g_m(R24)
   637  	MOVV	R24, g
   638  	//CALL	runtime·stackcheck(SB)
   639  
   640  nog:
   641  	// Call fn
   642  	JAL	(R25)
   643  
   644  	// It shouldn't return.	 If it does, exit that thread.
   645  	MOVW	$111, R4
   646  	MOVV	$SYS_exit, R11
   647  	SYSCALL
   648  	JMP	-3(PC)	// keep exiting
   649  
   650  // func sigaltstack(new, old *stackt)
   651  TEXT runtime·sigaltstack<ABIInternal>(SB),NOSPLIT,$0
   652  	MOVV	$SYS_sigaltstack, R11
   653  	SYSCALL
   654  	MOVW	$-4096, R5
   655  	BGEU	R5, R4, 2(PC)
   656  	MOVV	R0, 0xf1(R0)	// crash
   657  	RET
   658  
   659  // func osyield()
   660  TEXT runtime·osyield<ABIInternal>(SB),NOSPLIT,$0
   661  	MOVV	$SYS_sched_yield, R11
   662  	SYSCALL
   663  	RET
   664  
   665  // func sched_getaffinity(pid, len uintptr, buf *uintptr) int32
   666  TEXT runtime·sched_getaffinity<ABIInternal>(SB),NOSPLIT,$0
   667  	MOVV	$SYS_sched_getaffinity, R11
   668  	SYSCALL
   669  	RET
   670  
   671  // func sbrk0() uintptr
   672  TEXT runtime·sbrk0<ABIInternal>(SB),NOSPLIT,$0
   673  	// Implemented as brk(NULL).
   674  	MOVV	$0, R4
   675  	MOVV	$SYS_brk, R11
   676  	SYSCALL
   677  	RET
   678  
   679  // unimplemented, only needed for android; declared in stubs_linux.go
   680  TEXT runtime·access(SB),$0-20
   681  	MOVV	R0, 2(R0)
   682  	MOVW	R0, ret+16(FP) // for vet
   683  	RET
   684  
   685  // unimplemented, only needed for android; declared in stubs_linux.go
   686  TEXT runtime·connect(SB),$0-28
   687  	MOVV	R0, 2(R0)
   688  	MOVW	R0, ret+24(FP) // for vet
   689  	RET
   690  
   691  // unimplemented, only needed for android; declared in stubs_linux.go
   692  TEXT runtime·socket(SB),$0-20
   693  	MOVV	R0, 2(R0)
   694  	MOVW	R0, ret+16(FP) // for vet
   695  	RET
   696  
   697  // func vgetrandom1(buf *byte, length uintptr, flags uint32, state uintptr, stateSize uintptr) int
   698  TEXT runtime·vgetrandom1<ABIInternal>(SB),NOSPLIT,$16-48
   699  	MOVV	R3, R23
   700  
   701  	MOVV	runtime·vdsoGetrandomSym(SB), R12
   702  
   703  	MOVV	g_m(g), R24
   704  
   705  	MOVV	m_vdsoPC(R24), R13
   706  	MOVV	R13, 8(R3)
   707  	MOVV	m_vdsoSP(R24), R13
   708  	MOVV	R13, 16(R3)
   709  	MOVV	R1, m_vdsoPC(R24)
   710  	MOVV    $buf-8(FP), R13
   711  	MOVV	R13, m_vdsoSP(R24)
   712  
   713  	AND	$~15, R3
   714  
   715  	MOVBU	runtime·iscgo(SB), R13
   716  	BNE	R13, nosaveg
   717  	MOVV	m_gsignal(R24), R13
   718  	BEQ	R13, nosaveg
   719  	BEQ	g, R13, nosaveg
   720  	MOVV	(g_stack+stack_lo)(R13), R25
   721  	MOVV	g, (R25)
   722  
   723  	JAL	(R12)
   724  
   725  	MOVV	R0, (R25)
   726  	JMP	restore
   727  
   728  nosaveg:
   729  	JAL	(R12)
   730  
   731  restore:
   732  	MOVV	R23, R3
   733  	MOVV	16(R3), R25
   734  	MOVV	R25, m_vdsoSP(R24)
   735  	MOVV	8(R3), R25
   736  	MOVV	R25, m_vdsoPC(R24)
   737  	NOP	R4 // Satisfy go vet, since the return value comes from the vDSO function.
   738  	RET
   739  

View as plain text