github.com/AESNooper/go/src@v0.0.0-20220218095104-b56a4ab1bbbb/runtime/asm_s390x.s (about)

     1  // Copyright 2016 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  #include "go_asm.h"
     6  #include "go_tls.h"
     7  #include "funcdata.h"
     8  #include "textflag.h"
     9  
    10  // _rt0_s390x_lib is common startup code for s390x systems when
    11  // using -buildmode=c-archive or -buildmode=c-shared. The linker will
    12  // arrange to invoke this function as a global constructor (for
    13  // c-archive) or when the shared library is loaded (for c-shared).
    14  // We expect argc and argv to be passed in the usual C ABI registers
    15  // R2 and R3.
    16  TEXT _rt0_s390x_lib(SB), NOSPLIT|NOFRAME, $0
    17  	STMG	R6, R15, 48(R15)
    18  	MOVD	R2, _rt0_s390x_lib_argc<>(SB)
    19  	MOVD	R3, _rt0_s390x_lib_argv<>(SB)
    20  
    21  	// Save R6-R15 in the register save area of the calling function.
    22  	STMG	R6, R15, 48(R15)
    23  
    24  	// Allocate 80 bytes on the stack.
    25  	MOVD	$-80(R15), R15
    26  
    27  	// Save F8-F15 in our stack frame.
    28  	FMOVD	F8, 16(R15)
    29  	FMOVD	F9, 24(R15)
    30  	FMOVD	F10, 32(R15)
    31  	FMOVD	F11, 40(R15)
    32  	FMOVD	F12, 48(R15)
    33  	FMOVD	F13, 56(R15)
    34  	FMOVD	F14, 64(R15)
    35  	FMOVD	F15, 72(R15)
    36  
    37  	// Synchronous initialization.
    38  	MOVD	$runtime·libpreinit(SB), R1
    39  	BL	R1
    40  
    41  	// Create a new thread to finish Go runtime initialization.
    42  	MOVD	_cgo_sys_thread_create(SB), R1
    43  	CMP	R1, $0
    44  	BEQ	nocgo
    45  	MOVD	$_rt0_s390x_lib_go(SB), R2
    46  	MOVD	$0, R3
    47  	BL	R1
    48  	BR	restore
    49  
    50  nocgo:
    51  	MOVD	$0x800000, R1              // stacksize
    52  	MOVD	R1, 0(R15)
    53  	MOVD	$_rt0_s390x_lib_go(SB), R1
    54  	MOVD	R1, 8(R15)                 // fn
    55  	MOVD	$runtime·newosproc(SB), R1
    56  	BL	R1
    57  
    58  restore:
    59  	// Restore F8-F15 from our stack frame.
    60  	FMOVD	16(R15), F8
    61  	FMOVD	24(R15), F9
    62  	FMOVD	32(R15), F10
    63  	FMOVD	40(R15), F11
    64  	FMOVD	48(R15), F12
    65  	FMOVD	56(R15), F13
    66  	FMOVD	64(R15), F14
    67  	FMOVD	72(R15), F15
    68  	MOVD	$80(R15), R15
    69  
    70  	// Restore R6-R15.
    71  	LMG	48(R15), R6, R15
    72  	RET
    73  
    74  // _rt0_s390x_lib_go initializes the Go runtime.
    75  // This is started in a separate thread by _rt0_s390x_lib.
    76  TEXT _rt0_s390x_lib_go(SB), NOSPLIT|NOFRAME, $0
    77  	MOVD	_rt0_s390x_lib_argc<>(SB), R2
    78  	MOVD	_rt0_s390x_lib_argv<>(SB), R3
    79  	MOVD	$runtime·rt0_go(SB), R1
    80  	BR	R1
    81  
    82  DATA _rt0_s390x_lib_argc<>(SB)/8, $0
    83  GLOBL _rt0_s390x_lib_argc<>(SB), NOPTR, $8
    84  DATA _rt0_s90x_lib_argv<>(SB)/8, $0
    85  GLOBL _rt0_s390x_lib_argv<>(SB), NOPTR, $8
    86  
    87  TEXT runtime·rt0_go(SB),NOSPLIT|TOPFRAME,$0
    88  	// R2 = argc; R3 = argv; R11 = temp; R13 = g; R15 = stack pointer
    89  	// C TLS base pointer in AR0:AR1
    90  
    91  	// initialize essential registers
    92  	XOR	R0, R0
    93  
    94  	SUB	$24, R15
    95  	MOVW	R2, 8(R15) // argc
    96  	MOVD	R3, 16(R15) // argv
    97  
    98  	// create istack out of the given (operating system) stack.
    99  	// _cgo_init may update stackguard.
   100  	MOVD	$runtime·g0(SB), g
   101  	MOVD	R15, R11
   102  	SUB	$(64*1024), R11
   103  	MOVD	R11, g_stackguard0(g)
   104  	MOVD	R11, g_stackguard1(g)
   105  	MOVD	R11, (g_stack+stack_lo)(g)
   106  	MOVD	R15, (g_stack+stack_hi)(g)
   107  
   108  	// if there is a _cgo_init, call it using the gcc ABI.
   109  	MOVD	_cgo_init(SB), R11
   110  	CMPBEQ	R11, $0, nocgo
   111  	MOVW	AR0, R4			// (AR0 << 32 | AR1) is the TLS base pointer; MOVD is translated to EAR
   112  	SLD	$32, R4, R4
   113  	MOVW	AR1, R4			// arg 2: TLS base pointer
   114  	MOVD	$setg_gcc<>(SB), R3 	// arg 1: setg
   115  	MOVD	g, R2			// arg 0: G
   116  	// C functions expect 160 bytes of space on caller stack frame
   117  	// and an 8-byte aligned stack pointer
   118  	MOVD	R15, R9			// save current stack (R9 is preserved in the Linux ABI)
   119  	SUB	$160, R15		// reserve 160 bytes
   120  	MOVD    $~7, R6
   121  	AND 	R6, R15			// 8-byte align
   122  	BL	R11			// this call clobbers volatile registers according to Linux ABI (R0-R5, R14)
   123  	MOVD	R9, R15			// restore stack
   124  	XOR	R0, R0			// zero R0
   125  
   126  nocgo:
   127  	// update stackguard after _cgo_init
   128  	MOVD	(g_stack+stack_lo)(g), R2
   129  	ADD	$const__StackGuard, R2
   130  	MOVD	R2, g_stackguard0(g)
   131  	MOVD	R2, g_stackguard1(g)
   132  
   133  	// set the per-goroutine and per-mach "registers"
   134  	MOVD	$runtime·m0(SB), R2
   135  
   136  	// save m->g0 = g0
   137  	MOVD	g, m_g0(R2)
   138  	// save m0 to g0->m
   139  	MOVD	R2, g_m(g)
   140  
   141  	BL	runtime·check(SB)
   142  
   143  	// argc/argv are already prepared on stack
   144  	BL	runtime·args(SB)
   145  	BL	runtime·osinit(SB)
   146  	BL	runtime·schedinit(SB)
   147  
   148  	// create a new goroutine to start program
   149  	MOVD	$runtime·mainPC(SB), R2		// entry
   150  	SUB     $16, R15
   151  	MOVD 	R2, 8(R15)
   152  	MOVD 	$0, 0(R15)
   153  	BL	runtime·newproc(SB)
   154  	ADD	$16, R15
   155  
   156  	// start this M
   157  	BL	runtime·mstart(SB)
   158  
   159  	MOVD	$0, 1(R0)
   160  	RET
   161  
   162  DATA	runtime·mainPC+0(SB)/8,$runtime·main(SB)
   163  GLOBL	runtime·mainPC(SB),RODATA,$8
   164  
   165  TEXT runtime·breakpoint(SB),NOSPLIT|NOFRAME,$0-0
   166  	MOVD	$0, 2(R0)
   167  	RET
   168  
   169  TEXT runtime·asminit(SB),NOSPLIT|NOFRAME,$0-0
   170  	RET
   171  
   172  TEXT runtime·mstart(SB),NOSPLIT|TOPFRAME,$0
   173  	CALL	runtime·mstart0(SB)
   174  	RET // not reached
   175  
   176  /*
   177   *  go-routine
   178   */
   179  
   180  // void gogo(Gobuf*)
   181  // restore state from Gobuf; longjmp
   182  TEXT runtime·gogo(SB), NOSPLIT|NOFRAME, $0-8
   183  	MOVD	buf+0(FP), R5
   184  	MOVD	gobuf_g(R5), R6
   185  	MOVD	0(R6), R7	// make sure g != nil
   186  	BR	gogo<>(SB)
   187  
   188  TEXT gogo<>(SB), NOSPLIT|NOFRAME, $0
   189  	MOVD	R6, g
   190  	BL	runtime·save_g(SB)
   191  
   192  	MOVD	0(g), R4
   193  	MOVD	gobuf_sp(R5), R15
   194  	MOVD	gobuf_lr(R5), LR
   195  	MOVD	gobuf_ret(R5), R3
   196  	MOVD	gobuf_ctxt(R5), R12
   197  	MOVD	$0, gobuf_sp(R5)
   198  	MOVD	$0, gobuf_ret(R5)
   199  	MOVD	$0, gobuf_lr(R5)
   200  	MOVD	$0, gobuf_ctxt(R5)
   201  	CMP	R0, R0 // set condition codes for == test, needed by stack split
   202  	MOVD	gobuf_pc(R5), R6
   203  	BR	(R6)
   204  
   205  // void mcall(fn func(*g))
   206  // Switch to m->g0's stack, call fn(g).
   207  // Fn must never return.  It should gogo(&g->sched)
   208  // to keep running g.
   209  TEXT runtime·mcall(SB), NOSPLIT, $-8-8
   210  	// Save caller state in g->sched
   211  	MOVD	R15, (g_sched+gobuf_sp)(g)
   212  	MOVD	LR, (g_sched+gobuf_pc)(g)
   213  	MOVD	$0, (g_sched+gobuf_lr)(g)
   214  
   215  	// Switch to m->g0 & its stack, call fn.
   216  	MOVD	g, R3
   217  	MOVD	g_m(g), R8
   218  	MOVD	m_g0(R8), g
   219  	BL	runtime·save_g(SB)
   220  	CMP	g, R3
   221  	BNE	2(PC)
   222  	BR	runtime·badmcall(SB)
   223  	MOVD	fn+0(FP), R12			// context
   224  	MOVD	0(R12), R4			// code pointer
   225  	MOVD	(g_sched+gobuf_sp)(g), R15	// sp = m->g0->sched.sp
   226  	SUB	$16, R15
   227  	MOVD	R3, 8(R15)
   228  	MOVD	$0, 0(R15)
   229  	BL	(R4)
   230  	BR	runtime·badmcall2(SB)
   231  
   232  // systemstack_switch is a dummy routine that systemstack leaves at the bottom
   233  // of the G stack.  We need to distinguish the routine that
   234  // lives at the bottom of the G stack from the one that lives
   235  // at the top of the system stack because the one at the top of
   236  // the system stack terminates the stack walk (see topofstack()).
   237  TEXT runtime·systemstack_switch(SB), NOSPLIT, $0-0
   238  	UNDEF
   239  	BL	(LR)	// make sure this function is not leaf
   240  	RET
   241  
   242  // func systemstack(fn func())
   243  TEXT runtime·systemstack(SB), NOSPLIT, $0-8
   244  	MOVD	fn+0(FP), R3	// R3 = fn
   245  	MOVD	R3, R12		// context
   246  	MOVD	g_m(g), R4	// R4 = m
   247  
   248  	MOVD	m_gsignal(R4), R5	// R5 = gsignal
   249  	CMPBEQ	g, R5, noswitch
   250  
   251  	MOVD	m_g0(R4), R5	// R5 = g0
   252  	CMPBEQ	g, R5, noswitch
   253  
   254  	MOVD	m_curg(R4), R6
   255  	CMPBEQ	g, R6, switch
   256  
   257  	// Bad: g is not gsignal, not g0, not curg. What is it?
   258  	// Hide call from linker nosplit analysis.
   259  	MOVD	$runtime·badsystemstack(SB), R3
   260  	BL	(R3)
   261  	BL	runtime·abort(SB)
   262  
   263  switch:
   264  	// save our state in g->sched.  Pretend to
   265  	// be systemstack_switch if the G stack is scanned.
   266  	BL	gosave_systemstack_switch<>(SB)
   267  
   268  	// switch to g0
   269  	MOVD	R5, g
   270  	BL	runtime·save_g(SB)
   271  	MOVD	(g_sched+gobuf_sp)(g), R15
   272  
   273  	// call target function
   274  	MOVD	0(R12), R3	// code pointer
   275  	BL	(R3)
   276  
   277  	// switch back to g
   278  	MOVD	g_m(g), R3
   279  	MOVD	m_curg(R3), g
   280  	BL	runtime·save_g(SB)
   281  	MOVD	(g_sched+gobuf_sp)(g), R15
   282  	MOVD	$0, (g_sched+gobuf_sp)(g)
   283  	RET
   284  
   285  noswitch:
   286  	// already on m stack, just call directly
   287  	// Using a tail call here cleans up tracebacks since we won't stop
   288  	// at an intermediate systemstack.
   289  	MOVD	0(R12), R3	// code pointer
   290  	MOVD	0(R15), LR	// restore LR
   291  	ADD	$8, R15
   292  	BR	(R3)
   293  
   294  /*
   295   * support for morestack
   296   */
   297  
   298  // Called during function prolog when more stack is needed.
   299  // Caller has already loaded:
   300  // R3: framesize, R4: argsize, R5: LR
   301  //
   302  // The traceback routines see morestack on a g0 as being
   303  // the top of a stack (for example, morestack calling newstack
   304  // calling the scheduler calling newm calling gc), so we must
   305  // record an argument size. For that purpose, it has no arguments.
   306  TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0
   307  	// Cannot grow scheduler stack (m->g0).
   308  	MOVD	g_m(g), R7
   309  	MOVD	m_g0(R7), R8
   310  	CMPBNE	g, R8, 3(PC)
   311  	BL	runtime·badmorestackg0(SB)
   312  	BL	runtime·abort(SB)
   313  
   314  	// Cannot grow signal stack (m->gsignal).
   315  	MOVD	m_gsignal(R7), R8
   316  	CMP	g, R8
   317  	BNE	3(PC)
   318  	BL	runtime·badmorestackgsignal(SB)
   319  	BL	runtime·abort(SB)
   320  
   321  	// Called from f.
   322  	// Set g->sched to context in f.
   323  	MOVD	R15, (g_sched+gobuf_sp)(g)
   324  	MOVD	LR, R8
   325  	MOVD	R8, (g_sched+gobuf_pc)(g)
   326  	MOVD	R5, (g_sched+gobuf_lr)(g)
   327  	MOVD	R12, (g_sched+gobuf_ctxt)(g)
   328  
   329  	// Called from f.
   330  	// Set m->morebuf to f's caller.
   331  	MOVD	R5, (m_morebuf+gobuf_pc)(R7)	// f's caller's PC
   332  	MOVD	R15, (m_morebuf+gobuf_sp)(R7)	// f's caller's SP
   333  	MOVD	g, (m_morebuf+gobuf_g)(R7)
   334  
   335  	// Call newstack on m->g0's stack.
   336  	MOVD	m_g0(R7), g
   337  	BL	runtime·save_g(SB)
   338  	MOVD	(g_sched+gobuf_sp)(g), R15
   339  	// Create a stack frame on g0 to call newstack.
   340  	MOVD	$0, -8(R15)	// Zero saved LR in frame
   341  	SUB	$8, R15
   342  	BL	runtime·newstack(SB)
   343  
   344  	// Not reached, but make sure the return PC from the call to newstack
   345  	// is still in this function, and not the beginning of the next.
   346  	UNDEF
   347  
   348  TEXT runtime·morestack_noctxt(SB),NOSPLIT|NOFRAME,$0-0
   349  	MOVD	$0, R12
   350  	BR	runtime·morestack(SB)
   351  
   352  // reflectcall: call a function with the given argument list
   353  // func call(stackArgsType *_type, f *FuncVal, stackArgs *byte, stackArgsSize, stackRetOffset, frameSize uint32, regArgs *abi.RegArgs).
   354  // we don't have variable-sized frames, so we use a small number
   355  // of constant-sized-frame functions to encode a few bits of size in the pc.
   356  // Caution: ugly multiline assembly macros in your future!
   357  
   358  #define DISPATCH(NAME,MAXSIZE)		\
   359  	MOVD	$MAXSIZE, R4;		\
   360  	CMP	R3, R4;		\
   361  	BGT	3(PC);			\
   362  	MOVD	$NAME(SB), R5;	\
   363  	BR	(R5)
   364  // Note: can't just "BR NAME(SB)" - bad inlining results.
   365  
   366  TEXT ·reflectcall(SB), NOSPLIT, $-8-48
   367  	MOVWZ	frameSize+32(FP), R3
   368  	DISPATCH(runtime·call16, 16)
   369  	DISPATCH(runtime·call32, 32)
   370  	DISPATCH(runtime·call64, 64)
   371  	DISPATCH(runtime·call128, 128)
   372  	DISPATCH(runtime·call256, 256)
   373  	DISPATCH(runtime·call512, 512)
   374  	DISPATCH(runtime·call1024, 1024)
   375  	DISPATCH(runtime·call2048, 2048)
   376  	DISPATCH(runtime·call4096, 4096)
   377  	DISPATCH(runtime·call8192, 8192)
   378  	DISPATCH(runtime·call16384, 16384)
   379  	DISPATCH(runtime·call32768, 32768)
   380  	DISPATCH(runtime·call65536, 65536)
   381  	DISPATCH(runtime·call131072, 131072)
   382  	DISPATCH(runtime·call262144, 262144)
   383  	DISPATCH(runtime·call524288, 524288)
   384  	DISPATCH(runtime·call1048576, 1048576)
   385  	DISPATCH(runtime·call2097152, 2097152)
   386  	DISPATCH(runtime·call4194304, 4194304)
   387  	DISPATCH(runtime·call8388608, 8388608)
   388  	DISPATCH(runtime·call16777216, 16777216)
   389  	DISPATCH(runtime·call33554432, 33554432)
   390  	DISPATCH(runtime·call67108864, 67108864)
   391  	DISPATCH(runtime·call134217728, 134217728)
   392  	DISPATCH(runtime·call268435456, 268435456)
   393  	DISPATCH(runtime·call536870912, 536870912)
   394  	DISPATCH(runtime·call1073741824, 1073741824)
   395  	MOVD	$runtime·badreflectcall(SB), R5
   396  	BR	(R5)
   397  
   398  #define CALLFN(NAME,MAXSIZE)			\
   399  TEXT NAME(SB), WRAPPER, $MAXSIZE-48;		\
   400  	NO_LOCAL_POINTERS;			\
   401  	/* copy arguments to stack */		\
   402  	MOVD	stackArgs+16(FP), R4;			\
   403  	MOVWZ	stackArgsSize+24(FP), R5;		\
   404  	MOVD	$stack-MAXSIZE(SP), R6;		\
   405  loopArgs: /* copy 256 bytes at a time */	\
   406  	CMP	R5, $256;			\
   407  	BLT	tailArgs;			\
   408  	SUB	$256, R5;			\
   409  	MVC	$256, 0(R4), 0(R6);		\
   410  	MOVD	$256(R4), R4;			\
   411  	MOVD	$256(R6), R6;			\
   412  	BR	loopArgs;			\
   413  tailArgs: /* copy remaining bytes */		\
   414  	CMP	R5, $0;				\
   415  	BEQ	callFunction;			\
   416  	SUB	$1, R5;				\
   417  	EXRL	$callfnMVC<>(SB), R5;		\
   418  callFunction:					\
   419  	MOVD	f+8(FP), R12;			\
   420  	MOVD	(R12), R8;			\
   421  	PCDATA  $PCDATA_StackMapIndex, $0;	\
   422  	BL	(R8);				\
   423  	/* copy return values back */		\
   424  	MOVD	stackArgsType+0(FP), R7;		\
   425  	MOVD	stackArgs+16(FP), R6;			\
   426  	MOVWZ	stackArgsSize+24(FP), R5;			\
   427  	MOVD	$stack-MAXSIZE(SP), R4;		\
   428  	MOVWZ	stackRetOffset+28(FP), R1;		\
   429  	ADD	R1, R4;				\
   430  	ADD	R1, R6;				\
   431  	SUB	R1, R5;				\
   432  	BL	callRet<>(SB);			\
   433  	RET
   434  
   435  // callRet copies return values back at the end of call*. This is a
   436  // separate function so it can allocate stack space for the arguments
   437  // to reflectcallmove. It does not follow the Go ABI; it expects its
   438  // arguments in registers.
   439  TEXT callRet<>(SB), NOSPLIT, $40-0
   440  	MOVD	R7, 8(R15)
   441  	MOVD	R6, 16(R15)
   442  	MOVD	R4, 24(R15)
   443  	MOVD	R5, 32(R15)
   444  	MOVD	$0, 40(R15)
   445  	BL	runtime·reflectcallmove(SB)
   446  	RET
   447  
   448  CALLFN(·call16, 16)
   449  CALLFN(·call32, 32)
   450  CALLFN(·call64, 64)
   451  CALLFN(·call128, 128)
   452  CALLFN(·call256, 256)
   453  CALLFN(·call512, 512)
   454  CALLFN(·call1024, 1024)
   455  CALLFN(·call2048, 2048)
   456  CALLFN(·call4096, 4096)
   457  CALLFN(·call8192, 8192)
   458  CALLFN(·call16384, 16384)
   459  CALLFN(·call32768, 32768)
   460  CALLFN(·call65536, 65536)
   461  CALLFN(·call131072, 131072)
   462  CALLFN(·call262144, 262144)
   463  CALLFN(·call524288, 524288)
   464  CALLFN(·call1048576, 1048576)
   465  CALLFN(·call2097152, 2097152)
   466  CALLFN(·call4194304, 4194304)
   467  CALLFN(·call8388608, 8388608)
   468  CALLFN(·call16777216, 16777216)
   469  CALLFN(·call33554432, 33554432)
   470  CALLFN(·call67108864, 67108864)
   471  CALLFN(·call134217728, 134217728)
   472  CALLFN(·call268435456, 268435456)
   473  CALLFN(·call536870912, 536870912)
   474  CALLFN(·call1073741824, 1073741824)
   475  
   476  // Not a function: target for EXRL (execute relative long) instruction.
   477  TEXT callfnMVC<>(SB),NOSPLIT|NOFRAME,$0-0
   478  	MVC	$1, 0(R4), 0(R6)
   479  
   480  TEXT runtime·procyield(SB),NOSPLIT,$0-0
   481  	RET
   482  
   483  // Save state of caller into g->sched,
   484  // but using fake PC from systemstack_switch.
   485  // Must only be called from functions with no locals ($0)
   486  // or else unwinding from systemstack_switch is incorrect.
   487  // Smashes R1.
   488  TEXT gosave_systemstack_switch<>(SB),NOSPLIT|NOFRAME,$0
   489  	MOVD	$runtime·systemstack_switch(SB), R1
   490  	ADD	$16, R1	// get past prologue
   491  	MOVD	R1, (g_sched+gobuf_pc)(g)
   492  	MOVD	R15, (g_sched+gobuf_sp)(g)
   493  	MOVD	$0, (g_sched+gobuf_lr)(g)
   494  	MOVD	$0, (g_sched+gobuf_ret)(g)
   495  	// Assert ctxt is zero. See func save.
   496  	MOVD	(g_sched+gobuf_ctxt)(g), R1
   497  	CMPBEQ	R1, $0, 2(PC)
   498  	BL	runtime·abort(SB)
   499  	RET
   500  
   501  // func asmcgocall(fn, arg unsafe.Pointer) int32
   502  // Call fn(arg) on the scheduler stack,
   503  // aligned appropriately for the gcc ABI.
   504  // See cgocall.go for more details.
   505  TEXT ·asmcgocall(SB),NOSPLIT,$0-20
   506  	// R2 = argc; R3 = argv; R11 = temp; R13 = g; R15 = stack pointer
   507  	// C TLS base pointer in AR0:AR1
   508  	MOVD	fn+0(FP), R3
   509  	MOVD	arg+8(FP), R4
   510  
   511  	MOVD	R15, R2		// save original stack pointer
   512  	MOVD	g, R5
   513  
   514  	// Figure out if we need to switch to m->g0 stack.
   515  	// We get called to create new OS threads too, and those
   516  	// come in on the m->g0 stack already. Or we might already
   517  	// be on the m->gsignal stack.
   518  	MOVD	g_m(g), R6
   519  	MOVD	m_gsignal(R6), R7
   520  	CMPBEQ	R7, g, g0
   521  	MOVD	m_g0(R6), R7
   522  	CMPBEQ	R7, g, g0
   523  	BL	gosave_systemstack_switch<>(SB)
   524  	MOVD	R7, g
   525  	BL	runtime·save_g(SB)
   526  	MOVD	(g_sched+gobuf_sp)(g), R15
   527  
   528  	// Now on a scheduling stack (a pthread-created stack).
   529  g0:
   530  	// Save room for two of our pointers, plus 160 bytes of callee
   531  	// save area that lives on the caller stack.
   532  	SUB	$176, R15
   533  	MOVD	$~7, R6
   534  	AND	R6, R15                 // 8-byte alignment for gcc ABI
   535  	MOVD	R5, 168(R15)             // save old g on stack
   536  	MOVD	(g_stack+stack_hi)(R5), R5
   537  	SUB	R2, R5
   538  	MOVD	R5, 160(R15)             // save depth in old g stack (can't just save SP, as stack might be copied during a callback)
   539  	MOVD	$0, 0(R15)              // clear back chain pointer (TODO can we give it real back trace information?)
   540  	MOVD	R4, R2                  // arg in R2
   541  	BL	R3                      // can clobber: R0-R5, R14, F0-F3, F5, F7-F15
   542  
   543  	XOR	R0, R0                  // set R0 back to 0.
   544  	// Restore g, stack pointer.
   545  	MOVD	168(R15), g
   546  	BL	runtime·save_g(SB)
   547  	MOVD	(g_stack+stack_hi)(g), R5
   548  	MOVD	160(R15), R6
   549  	SUB	R6, R5
   550  	MOVD	R5, R15
   551  
   552  	MOVW	R2, ret+16(FP)
   553  	RET
   554  
   555  // cgocallback(fn, frame unsafe.Pointer, ctxt uintptr)
   556  // See cgocall.go for more details.
   557  TEXT ·cgocallback(SB),NOSPLIT,$24-24
   558  	NO_LOCAL_POINTERS
   559  
   560  	// Load m and g from thread-local storage.
   561  	MOVB	runtime·iscgo(SB), R3
   562  	CMPBEQ	R3, $0, nocgo
   563  	BL	runtime·load_g(SB)
   564  
   565  nocgo:
   566  	// If g is nil, Go did not create the current thread.
   567  	// Call needm to obtain one for temporary use.
   568  	// In this case, we're running on the thread stack, so there's
   569  	// lots of space, but the linker doesn't know. Hide the call from
   570  	// the linker analysis by using an indirect call.
   571  	CMPBEQ	g, $0, needm
   572  
   573  	MOVD	g_m(g), R8
   574  	MOVD	R8, savedm-8(SP)
   575  	BR	havem
   576  
   577  needm:
   578  	MOVD	g, savedm-8(SP) // g is zero, so is m.
   579  	MOVD	$runtime·needm(SB), R3
   580  	BL	(R3)
   581  
   582  	// Set m->sched.sp = SP, so that if a panic happens
   583  	// during the function we are about to execute, it will
   584  	// have a valid SP to run on the g0 stack.
   585  	// The next few lines (after the havem label)
   586  	// will save this SP onto the stack and then write
   587  	// the same SP back to m->sched.sp. That seems redundant,
   588  	// but if an unrecovered panic happens, unwindm will
   589  	// restore the g->sched.sp from the stack location
   590  	// and then systemstack will try to use it. If we don't set it here,
   591  	// that restored SP will be uninitialized (typically 0) and
   592  	// will not be usable.
   593  	MOVD	g_m(g), R8
   594  	MOVD	m_g0(R8), R3
   595  	MOVD	R15, (g_sched+gobuf_sp)(R3)
   596  
   597  havem:
   598  	// Now there's a valid m, and we're running on its m->g0.
   599  	// Save current m->g0->sched.sp on stack and then set it to SP.
   600  	// Save current sp in m->g0->sched.sp in preparation for
   601  	// switch back to m->curg stack.
   602  	// NOTE: unwindm knows that the saved g->sched.sp is at 8(R1) aka savedsp-16(SP).
   603  	MOVD	m_g0(R8), R3
   604  	MOVD	(g_sched+gobuf_sp)(R3), R4
   605  	MOVD	R4, savedsp-24(SP)	// must match frame size
   606  	MOVD	R15, (g_sched+gobuf_sp)(R3)
   607  
   608  	// Switch to m->curg stack and call runtime.cgocallbackg.
   609  	// Because we are taking over the execution of m->curg
   610  	// but *not* resuming what had been running, we need to
   611  	// save that information (m->curg->sched) so we can restore it.
   612  	// We can restore m->curg->sched.sp easily, because calling
   613  	// runtime.cgocallbackg leaves SP unchanged upon return.
   614  	// To save m->curg->sched.pc, we push it onto the curg stack and
   615  	// open a frame the same size as cgocallback's g0 frame.
   616  	// Once we switch to the curg stack, the pushed PC will appear
   617  	// to be the return PC of cgocallback, so that the traceback
   618  	// will seamlessly trace back into the earlier calls.
   619  	MOVD	m_curg(R8), g
   620  	BL	runtime·save_g(SB)
   621  	MOVD	(g_sched+gobuf_sp)(g), R4 // prepare stack as R4
   622  	MOVD	(g_sched+gobuf_pc)(g), R5
   623  	MOVD	R5, -(24+8)(R4)	// "saved LR"; must match frame size
   624  	// Gather our arguments into registers.
   625  	MOVD	fn+0(FP), R1
   626  	MOVD	frame+8(FP), R2
   627  	MOVD	ctxt+16(FP), R3
   628  	MOVD	$-(24+8)(R4), R15	// switch stack; must match frame size
   629  	MOVD	R1, 8(R15)
   630  	MOVD	R2, 16(R15)
   631  	MOVD	R3, 24(R15)
   632  	BL	runtime·cgocallbackg(SB)
   633  
   634  	// Restore g->sched (== m->curg->sched) from saved values.
   635  	MOVD	0(R15), R5
   636  	MOVD	R5, (g_sched+gobuf_pc)(g)
   637  	MOVD	$(24+8)(R15), R4	// must match frame size
   638  	MOVD	R4, (g_sched+gobuf_sp)(g)
   639  
   640  	// Switch back to m->g0's stack and restore m->g0->sched.sp.
   641  	// (Unlike m->curg, the g0 goroutine never uses sched.pc,
   642  	// so we do not have to restore it.)
   643  	MOVD	g_m(g), R8
   644  	MOVD	m_g0(R8), g
   645  	BL	runtime·save_g(SB)
   646  	MOVD	(g_sched+gobuf_sp)(g), R15
   647  	MOVD	savedsp-24(SP), R4	// must match frame size
   648  	MOVD	R4, (g_sched+gobuf_sp)(g)
   649  
   650  	// If the m on entry was nil, we called needm above to borrow an m
   651  	// for the duration of the call. Since the call is over, return it with dropm.
   652  	MOVD	savedm-8(SP), R6
   653  	CMPBNE	R6, $0, droppedm
   654  	MOVD	$runtime·dropm(SB), R3
   655  	BL	(R3)
   656  droppedm:
   657  
   658  	// Done!
   659  	RET
   660  
   661  // void setg(G*); set g. for use by needm.
   662  TEXT runtime·setg(SB), NOSPLIT, $0-8
   663  	MOVD	gg+0(FP), g
   664  	// This only happens if iscgo, so jump straight to save_g
   665  	BL	runtime·save_g(SB)
   666  	RET
   667  
   668  // void setg_gcc(G*); set g in C TLS.
   669  // Must obey the gcc calling convention.
   670  TEXT setg_gcc<>(SB),NOSPLIT|NOFRAME,$0-0
   671  	// The standard prologue clobbers LR (R14), which is callee-save in
   672  	// the C ABI, so we have to use NOFRAME and save LR ourselves.
   673  	MOVD	LR, R1
   674  	// Also save g, R10, and R11 since they're callee-save in C ABI
   675  	MOVD	R10, R3
   676  	MOVD	g, R4
   677  	MOVD	R11, R5
   678  
   679  	MOVD	R2, g
   680  	BL	runtime·save_g(SB)
   681  
   682  	MOVD	R5, R11
   683  	MOVD	R4, g
   684  	MOVD	R3, R10
   685  	MOVD	R1, LR
   686  	RET
   687  
   688  TEXT runtime·abort(SB),NOSPLIT|NOFRAME,$0-0
   689  	MOVW	(R0), R0
   690  	UNDEF
   691  
   692  // int64 runtime·cputicks(void)
   693  TEXT runtime·cputicks(SB),NOSPLIT,$0-8
   694  	// The TOD clock on s390 counts from the year 1900 in ~250ps intervals.
   695  	// This means that since about 1972 the msb has been set, making the
   696  	// result of a call to STORE CLOCK (stck) a negative number.
   697  	// We clear the msb to make it positive.
   698  	STCK	ret+0(FP)      // serialises before and after call
   699  	MOVD	ret+0(FP), R3  // R3 will wrap to 0 in the year 2043
   700  	SLD	$1, R3
   701  	SRD	$1, R3
   702  	MOVD	R3, ret+0(FP)
   703  	RET
   704  
   705  // AES hashing not implemented for s390x
   706  TEXT runtime·memhash(SB),NOSPLIT|NOFRAME,$0-32
   707  	JMP	runtime·memhashFallback(SB)
   708  TEXT runtime·strhash(SB),NOSPLIT|NOFRAME,$0-24
   709  	JMP	runtime·strhashFallback(SB)
   710  TEXT runtime·memhash32(SB),NOSPLIT|NOFRAME,$0-24
   711  	JMP	runtime·memhash32Fallback(SB)
   712  TEXT runtime·memhash64(SB),NOSPLIT|NOFRAME,$0-24
   713  	JMP	runtime·memhash64Fallback(SB)
   714  
   715  TEXT runtime·return0(SB), NOSPLIT, $0
   716  	MOVW	$0, R3
   717  	RET
   718  
   719  // Called from cgo wrappers, this function returns g->m->curg.stack.hi.
   720  // Must obey the gcc calling convention.
   721  TEXT _cgo_topofstack(SB),NOSPLIT|NOFRAME,$0
   722  	// g (R13), R10, R11 and LR (R14) are callee-save in the C ABI, so save them
   723  	MOVD	g, R1
   724  	MOVD	R10, R3
   725  	MOVD	LR, R4
   726  	MOVD	R11, R5
   727  
   728  	BL	runtime·load_g(SB)	// clobbers g (R13), R10, R11
   729  	MOVD	g_m(g), R2
   730  	MOVD	m_curg(R2), R2
   731  	MOVD	(g_stack+stack_hi)(R2), R2
   732  
   733  	MOVD	R1, g
   734  	MOVD	R3, R10
   735  	MOVD	R4, LR
   736  	MOVD	R5, R11
   737  	RET
   738  
   739  // The top-most function running on a goroutine
   740  // returns to goexit+PCQuantum.
   741  TEXT runtime·goexit(SB),NOSPLIT|NOFRAME|TOPFRAME,$0-0
   742  	BYTE $0x07; BYTE $0x00; // 2-byte nop
   743  	BL	runtime·goexit1(SB)	// does not return
   744  	// traceback from goexit1 must hit code range of goexit
   745  	BYTE $0x07; BYTE $0x00; // 2-byte nop
   746  
   747  TEXT ·publicationBarrier(SB),NOSPLIT|NOFRAME,$0-0
   748  	// Stores are already ordered on s390x, so this is just a
   749  	// compile barrier.
   750  	RET
   751  
   752  // This is called from .init_array and follows the platform, not Go, ABI.
   753  // We are overly conservative. We could only save the registers we use.
   754  // However, since this function is only called once per loaded module
   755  // performance is unimportant.
   756  TEXT runtime·addmoduledata(SB),NOSPLIT|NOFRAME,$0-0
   757  	// Save R6-R15 in the register save area of the calling function.
   758  	// Don't bother saving F8-F15 as we aren't doing any calls.
   759  	STMG	R6, R15, 48(R15)
   760  
   761  	// append the argument (passed in R2, as per the ELF ABI) to the
   762  	// moduledata linked list.
   763  	MOVD	runtime·lastmoduledatap(SB), R1
   764  	MOVD	R2, moduledata_next(R1)
   765  	MOVD	R2, runtime·lastmoduledatap(SB)
   766  
   767  	// Restore R6-R15.
   768  	LMG	48(R15), R6, R15
   769  	RET
   770  
   771  TEXT ·checkASM(SB),NOSPLIT,$0-1
   772  	MOVB	$1, ret+0(FP)
   773  	RET
   774  
   775  // gcWriteBarrier performs a heap pointer write and informs the GC.
   776  //
   777  // gcWriteBarrier does NOT follow the Go ABI. It takes two arguments:
   778  // - R2 is the destination of the write
   779  // - R3 is the value being written at R2.
   780  // It clobbers R10 (the temp register) and R1 (used by PLT stub).
   781  // It does not clobber any other general-purpose registers,
   782  // but may clobber others (e.g., floating point registers).
   783  TEXT runtime·gcWriteBarrier(SB),NOSPLIT,$96
   784  	// Save the registers clobbered by the fast path.
   785  	MOVD	R4, 96(R15)
   786  	MOVD	g_m(g), R1
   787  	MOVD	m_p(R1), R1
   788  	// Increment wbBuf.next position.
   789  	MOVD	$16, R4
   790  	ADD	(p_wbBuf+wbBuf_next)(R1), R4
   791  	MOVD	R4, (p_wbBuf+wbBuf_next)(R1)
   792  	MOVD	(p_wbBuf+wbBuf_end)(R1), R1
   793  	// Record the write.
   794  	MOVD	R3, -16(R4) // Record value
   795  	MOVD	(R2), R10   // TODO: This turns bad writes into bad reads.
   796  	MOVD	R10, -8(R4) // Record *slot
   797  	// Is the buffer full?
   798  	CMPBEQ	R4, R1, flush
   799  ret:
   800  	MOVD	96(R15), R4
   801  	// Do the write.
   802  	MOVD	R3, (R2)
   803  	RET
   804  
   805  flush:
   806  	// Save all general purpose registers since these could be
   807  	// clobbered by wbBufFlush and were not saved by the caller.
   808  	STMG	R2, R3, 8(R15)   // set R2 and R3 as arguments for wbBufFlush
   809  	MOVD	R0, 24(R15)
   810  	// R1 already saved.
   811  	// R4 already saved.
   812  	STMG	R5, R12, 32(R15) // save R5 - R12
   813  	// R13 is g.
   814  	// R14 is LR.
   815  	// R15 is SP.
   816  
   817  	// This takes arguments R2 and R3.
   818  	CALL	runtime·wbBufFlush(SB)
   819  
   820  	LMG	8(R15), R2, R3   // restore R2 - R3
   821  	MOVD	24(R15), R0      // restore R0
   822  	LMG	32(R15), R5, R12 // restore R5 - R12
   823  	JMP	ret
   824  
   825  // Note: these functions use a special calling convention to save generated code space.
   826  // Arguments are passed in registers, but the space for those arguments are allocated
   827  // in the caller's stack frame. These stubs write the args into that stack space and
   828  // then tail call to the corresponding runtime handler.
   829  // The tail call makes these stubs disappear in backtraces.
   830  TEXT runtime·panicIndex(SB),NOSPLIT,$0-16
   831  	MOVD	R0, x+0(FP)
   832  	MOVD	R1, y+8(FP)
   833  	JMP	runtime·goPanicIndex(SB)
   834  TEXT runtime·panicIndexU(SB),NOSPLIT,$0-16
   835  	MOVD	R0, x+0(FP)
   836  	MOVD	R1, y+8(FP)
   837  	JMP	runtime·goPanicIndexU(SB)
   838  TEXT runtime·panicSliceAlen(SB),NOSPLIT,$0-16
   839  	MOVD	R1, x+0(FP)
   840  	MOVD	R2, y+8(FP)
   841  	JMP	runtime·goPanicSliceAlen(SB)
   842  TEXT runtime·panicSliceAlenU(SB),NOSPLIT,$0-16
   843  	MOVD	R1, x+0(FP)
   844  	MOVD	R2, y+8(FP)
   845  	JMP	runtime·goPanicSliceAlenU(SB)
   846  TEXT runtime·panicSliceAcap(SB),NOSPLIT,$0-16
   847  	MOVD	R1, x+0(FP)
   848  	MOVD	R2, y+8(FP)
   849  	JMP	runtime·goPanicSliceAcap(SB)
   850  TEXT runtime·panicSliceAcapU(SB),NOSPLIT,$0-16
   851  	MOVD	R1, x+0(FP)
   852  	MOVD	R2, y+8(FP)
   853  	JMP	runtime·goPanicSliceAcapU(SB)
   854  TEXT runtime·panicSliceB(SB),NOSPLIT,$0-16
   855  	MOVD	R0, x+0(FP)
   856  	MOVD	R1, y+8(FP)
   857  	JMP	runtime·goPanicSliceB(SB)
   858  TEXT runtime·panicSliceBU(SB),NOSPLIT,$0-16
   859  	MOVD	R0, x+0(FP)
   860  	MOVD	R1, y+8(FP)
   861  	JMP	runtime·goPanicSliceBU(SB)
   862  TEXT runtime·panicSlice3Alen(SB),NOSPLIT,$0-16
   863  	MOVD	R2, x+0(FP)
   864  	MOVD	R3, y+8(FP)
   865  	JMP	runtime·goPanicSlice3Alen(SB)
   866  TEXT runtime·panicSlice3AlenU(SB),NOSPLIT,$0-16
   867  	MOVD	R2, x+0(FP)
   868  	MOVD	R3, y+8(FP)
   869  	JMP	runtime·goPanicSlice3AlenU(SB)
   870  TEXT runtime·panicSlice3Acap(SB),NOSPLIT,$0-16
   871  	MOVD	R2, x+0(FP)
   872  	MOVD	R3, y+8(FP)
   873  	JMP	runtime·goPanicSlice3Acap(SB)
   874  TEXT runtime·panicSlice3AcapU(SB),NOSPLIT,$0-16
   875  	MOVD	R2, x+0(FP)
   876  	MOVD	R3, y+8(FP)
   877  	JMP	runtime·goPanicSlice3AcapU(SB)
   878  TEXT runtime·panicSlice3B(SB),NOSPLIT,$0-16
   879  	MOVD	R1, x+0(FP)
   880  	MOVD	R2, y+8(FP)
   881  	JMP	runtime·goPanicSlice3B(SB)
   882  TEXT runtime·panicSlice3BU(SB),NOSPLIT,$0-16
   883  	MOVD	R1, x+0(FP)
   884  	MOVD	R2, y+8(FP)
   885  	JMP	runtime·goPanicSlice3BU(SB)
   886  TEXT runtime·panicSlice3C(SB),NOSPLIT,$0-16
   887  	MOVD	R0, x+0(FP)
   888  	MOVD	R1, y+8(FP)
   889  	JMP	runtime·goPanicSlice3C(SB)
   890  TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-16
   891  	MOVD	R0, x+0(FP)
   892  	MOVD	R1, y+8(FP)
   893  	JMP	runtime·goPanicSlice3CU(SB)
   894  TEXT runtime·panicSliceConvert(SB),NOSPLIT,$0-16
   895  	MOVD	R2, x+0(FP)
   896  	MOVD	R3, y+8(FP)
   897  	JMP	runtime·goPanicSliceConvert(SB)