Text file src/runtime/asm_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  #include "go_asm.h"
     6  #include "go_tls.h"
     7  #include "funcdata.h"
     8  #include "textflag.h"
     9  #include "cgo/abi_loong64.h"
    10  
    11  // When building with -buildmode=c-shared, this symbol is called when the shared
    12  // library is loaded.
    13  TEXT _rt0_loong64_lib(SB),NOSPLIT,$168
    14  	// Preserve callee-save registers.
    15  	SAVE_R22_TO_R31(3*8)
    16  	SAVE_F24_TO_F31(13*8)
    17  
    18  	// Initialize g as nil in case of using g later e.g. sigaction in cgo_sigaction.go
    19  	MOVV	R0, g
    20  
    21  	MOVV	R4, _rt0_loong64_lib_argc<>(SB)
    22  	MOVV	R5, _rt0_loong64_lib_argv<>(SB)
    23  
    24  	MOVV	$runtime·libInit(SB), R19
    25  	JAL	(R19)
    26  
    27  	// Restore callee-save registers.
    28  	RESTORE_R22_TO_R31(3*8)
    29  	RESTORE_F24_TO_F31(13*8)
    30  	RET
    31  
    32  TEXT runtime·rt0_lib_go<ABIInternal>(SB),NOSPLIT,$0
    33  	MOVV	_rt0_loong64_lib_argc<>(SB), R4
    34  	MOVV	_rt0_loong64_lib_argv<>(SB), R5
    35  	MOVV	$runtime·rt0_go(SB),R19
    36  	JMP	(R19)
    37  
    38  DATA _rt0_loong64_lib_argc<>(SB)/8, $0
    39  GLOBL _rt0_loong64_lib_argc<>(SB),NOPTR, $8
    40  DATA _rt0_loong64_lib_argv<>(SB)/8, $0
    41  GLOBL _rt0_loong64_lib_argv<>(SB),NOPTR, $8
    42  
    43  #define	REGCTXT	R29
    44  
    45  TEXT runtime·rt0_go(SB),NOSPLIT|TOPFRAME,$0
    46  	// R3 = stack; R4 = argc; R5 = argv
    47  
    48  	ADDV	$-24, R3
    49  	MOVW	R4, 8(R3) // argc
    50  	MOVV	R5, 16(R3) // argv
    51  
    52  	// create istack out of the given (operating system) stack.
    53  	// _cgo_init may update stackguard.
    54  	MOVV	$runtime·g0(SB), g
    55  	MOVV	$(-64*1024), R30
    56  	ADDV	R30, R3, R19
    57  	MOVV	R19, g_stackguard0(g)
    58  	MOVV	R19, g_stackguard1(g)
    59  	MOVV	R19, (g_stack+stack_lo)(g)
    60  	MOVV	R3, (g_stack+stack_hi)(g)
    61  
    62  	// if there is a _cgo_init, call it using the gcc ABI.
    63  	MOVV	_cgo_init(SB), R25
    64  	BEQ	R25, nocgo
    65  
    66  	MOVV	R0, R7	// arg 3: not used
    67  	MOVV	R0, R6	// arg 2: not used
    68  	MOVV	$setg_gcc<>(SB), R5	// arg 1: setg
    69  	MOVV	g, R4	// arg 0: G
    70  	JAL	(R25)
    71  
    72  nocgo:
    73  	JAL	runtime·save_g(SB)
    74  	// update stackguard after _cgo_init
    75  	MOVV	(g_stack+stack_lo)(g), R19
    76  	ADDV	$const_stackGuard, R19
    77  	MOVV	R19, g_stackguard0(g)
    78  	MOVV	R19, g_stackguard1(g)
    79  
    80  	// set the per-goroutine and per-mach "registers"
    81  	MOVV	$runtime·m0(SB), R19
    82  
    83  	// save m->g0 = g0
    84  	MOVV	g, m_g0(R19)
    85  	// save m0 to g0->m
    86  	MOVV	R19, g_m(g)
    87  
    88  	JAL	runtime·check(SB)
    89  
    90  	// args are already prepared
    91  	JAL	runtime·args(SB)
    92  	JAL	runtime·osinit(SB)
    93  	JAL	runtime·schedinit(SB)
    94  
    95  	// create a new goroutine to start program
    96  	MOVV	$runtime·mainPC(SB), R19		// entry
    97  	ADDV	$-16, R3
    98  	MOVV	R19, 8(R3)
    99  	MOVV	R0, 0(R3)
   100  	JAL	runtime·newproc(SB)
   101  	ADDV	$16, R3
   102  
   103  	// start this M
   104  	JAL	runtime·mstart(SB)
   105  
   106  	// Prevent dead-code elimination of debugCallV2 and debugPinnerV1, which are
   107  	// intended to be called by debuggers.
   108  	MOVV	$runtime·debugPinnerV1<ABIInternal>(SB), R0
   109  	MOVV	$runtime·debugCallV2<ABIInternal>(SB), R0
   110  
   111  	MOVV	R0, 1(R0)
   112  	RET
   113  
   114  DATA	runtime·mainPC+0(SB)/8,$runtime·main<ABIInternal>(SB)
   115  GLOBL	runtime·mainPC(SB),RODATA,$8
   116  
   117  TEXT runtime·breakpoint(SB),NOSPLIT|NOFRAME,$0-0
   118  	BREAK
   119  	RET
   120  
   121  TEXT runtime·asminit(SB),NOSPLIT|NOFRAME,$0-0
   122  	RET
   123  
   124  TEXT runtime·mstart(SB),NOSPLIT|TOPFRAME,$0
   125  	JAL     runtime·mstart0(SB)
   126  	RET // not reached
   127  
   128  // func cputicks() int64
   129  TEXT runtime·cputicks<ABIInternal>(SB),NOSPLIT,$0-8
   130  	RDTIMED	R0, R4
   131  	RET
   132  
   133  /*
   134   *  go-routine
   135   */
   136  
   137  // void gogo(Gobuf*)
   138  // restore state from Gobuf; longjmp
   139  TEXT runtime·gogo(SB), NOSPLIT|NOFRAME, $0-8
   140  	MOVV	buf+0(FP), R4
   141  	MOVV	gobuf_g(R4), R5
   142  	MOVV	0(R5), R0	// make sure g != nil
   143  	JMP	gogo<>(SB)
   144  
   145  TEXT gogo<>(SB), NOSPLIT|NOFRAME, $0
   146  	MOVV	R5, g
   147  	JAL	runtime·save_g(SB)
   148  
   149  	MOVV	gobuf_sp(R4), R3
   150  	MOVV	gobuf_lr(R4), R1
   151  	MOVV	gobuf_ctxt(R4), REGCTXT
   152  	MOVV	R0, gobuf_sp(R4)
   153  	MOVV	R0, gobuf_lr(R4)
   154  	MOVV	R0, gobuf_ctxt(R4)
   155  	MOVV	gobuf_pc(R4), R6
   156  	JMP	(R6)
   157  
   158  // void mcall(fn func(*g))
   159  // Switch to m->g0's stack, call fn(g).
   160  // Fn must never return. It should gogo(&g->sched)
   161  // to keep running g.
   162  TEXT runtime·mcall<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-8
   163  	MOVV	R4, REGCTXT
   164  	// Save caller state in g->sched
   165  	MOVV	R3, (g_sched+gobuf_sp)(g)
   166  	MOVV	R1, (g_sched+gobuf_pc)(g)
   167  	MOVV	R0, (g_sched+gobuf_lr)(g)
   168  
   169  	// Switch to m->g0 & its stack, call fn.
   170  	MOVV	g, R4		// arg = g
   171  	MOVV	g_m(g), R20
   172  	MOVV	m_g0(R20), g
   173  	JAL	runtime·save_g(SB)
   174  	BNE	g, R4, 2(PC)
   175  	JMP	runtime·badmcall(SB)
   176  	MOVV	0(REGCTXT), R20			// code pointer
   177  	MOVV	(g_sched+gobuf_sp)(g), R3	// sp = m->g0->sched.sp
   178  	ADDV	$-16, R3
   179  	MOVV	R4, 8(R3)
   180  	MOVV	R0, 0(R3)
   181  	JAL	(R20)
   182  	JMP	runtime·badmcall2(SB)
   183  
   184  // systemstack_switch is a dummy routine that systemstack leaves at the bottom
   185  // of the G stack. We need to distinguish the routine that
   186  // lives at the bottom of the G stack from the one that lives
   187  // at the top of the system stack because the one at the top of
   188  // the system stack terminates the stack walk (see topofstack()).
   189  TEXT runtime·systemstack_switch(SB), NOSPLIT, $0-0
   190  	UNDEF
   191  	JAL	(R1)	// make sure this function is not leaf
   192  	RET
   193  
   194  // func systemstack(fn func())
   195  TEXT runtime·systemstack(SB), NOSPLIT, $0-8
   196  	MOVV	fn+0(FP), R19	// R19 = fn
   197  	MOVV	R19, REGCTXT		// context
   198  	MOVV	g_m(g), R4	// R4 = m
   199  
   200  	MOVV	m_gsignal(R4), R5	// R5 = gsignal
   201  	BEQ	g, R5, noswitch
   202  
   203  	MOVV	m_g0(R4), R5	// R5 = g0
   204  	BEQ	g, R5, noswitch
   205  
   206  	MOVV	m_curg(R4), R6
   207  	BEQ	g, R6, switch
   208  
   209  	// Bad: g is not gsignal, not g0, not curg. What is it?
   210  	// Hide call from linker nosplit analysis.
   211  	MOVV	$runtime·badsystemstack(SB), R7
   212  	JAL	(R7)
   213  	JAL	runtime·abort(SB)
   214  
   215  switch:
   216  	// save our state in g->sched. Pretend to
   217  	// be systemstack_switch if the G stack is scanned.
   218  	JAL	gosave_systemstack_switch<>(SB)
   219  
   220  	// switch to g0
   221  	MOVV	R5, g
   222  	JAL	runtime·save_g(SB)
   223  	MOVV	(g_sched+gobuf_sp)(g), R19
   224  	MOVV	R19, R3
   225  
   226  	// call target function
   227  	MOVV	0(REGCTXT), R6	// code pointer
   228  	JAL	(R6)
   229  
   230  	// switch back to g
   231  	MOVV	g_m(g), R4
   232  	MOVV	m_curg(R4), g
   233  	JAL	runtime·save_g(SB)
   234  	MOVV	(g_sched+gobuf_sp)(g), R3
   235  	MOVV	R0, (g_sched+gobuf_sp)(g)
   236  	RET
   237  
   238  noswitch:
   239  	// already on m stack, just call directly
   240  	// Using a tail call here cleans up tracebacks since we won't stop
   241  	// at an intermediate systemstack.
   242  	MOVV	0(REGCTXT), R4	// code pointer
   243  	MOVV	0(R3), R1	// restore LR
   244  	ADDV	$8, R3
   245  	JMP	(R4)
   246  
   247  // func switchToCrashStack0(fn func())
   248  TEXT runtime·switchToCrashStack0<ABIInternal>(SB),NOSPLIT,$0-8
   249  	MOVV	R4, REGCTXT	// context register
   250  	MOVV	g_m(g), R5	// curm
   251  
   252  	// set g to gcrash
   253  	MOVV	$runtime·gcrash(SB), g	// g = &gcrash
   254  	JAL	runtime·save_g(SB)
   255  	MOVV	R5, g_m(g)	// g.m = curm
   256  	MOVV	g, m_g0(R5)	// curm.g0 = g
   257  
   258  	// switch to crashstack
   259  	MOVV	(g_stack+stack_hi)(g), R5
   260  	ADDV	$(-4*8), R5, R3
   261  
   262  	// call target function
   263  	MOVV	0(REGCTXT), R6
   264  	JAL	(R6)
   265  
   266  	// should never return
   267  	JAL	runtime·abort(SB)
   268  	UNDEF
   269  
   270  /*
   271   * support for morestack
   272   */
   273  
   274  // Called during function prolog when more stack is needed.
   275  // Caller has already loaded:
   276  // loong64: R31: LR
   277  //
   278  // The traceback routines see morestack on a g0 as being
   279  // the top of a stack (for example, morestack calling newstack
   280  // calling the scheduler calling newm calling gc), so we must
   281  // record an argument size. For that purpose, it has no arguments.
   282  TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0
   283  	// Called from f.
   284  	// Set g->sched to context in f.
   285  	MOVV	R3, (g_sched+gobuf_sp)(g)
   286  	MOVV	R1, (g_sched+gobuf_pc)(g)
   287  	MOVV	R31, (g_sched+gobuf_lr)(g)
   288  	MOVV	REGCTXT, (g_sched+gobuf_ctxt)(g)
   289  
   290  	// Cannot grow scheduler stack (m->g0).
   291  	MOVV	g_m(g), R7
   292  	MOVV	m_g0(R7), R8
   293  	BNE	g, R8, 3(PC)
   294  	JAL	runtime·badmorestackg0(SB)
   295  	JAL	runtime·abort(SB)
   296  
   297  	// Cannot grow signal stack (m->gsignal).
   298  	MOVV	m_gsignal(R7), R8
   299  	BNE	g, R8, 3(PC)
   300  	JAL	runtime·badmorestackgsignal(SB)
   301  	JAL	runtime·abort(SB)
   302  
   303  	// Called from f.
   304  	// Set m->morebuf to f's caller.
   305  	MOVV	R31, (m_morebuf+gobuf_pc)(R7)	// f's caller's PC
   306  	MOVV	R3, (m_morebuf+gobuf_sp)(R7)	// f's caller's SP
   307  	MOVV	g, (m_morebuf+gobuf_g)(R7)
   308  
   309  	// Call newstack on m->g0's stack.
   310  	MOVV	m_g0(R7), g
   311  	JAL	runtime·save_g(SB)
   312  	MOVV	(g_sched+gobuf_sp)(g), R3
   313  	// Create a stack frame on g0 to call newstack.
   314  	MOVV	R0, -8(R3)	// Zero saved LR in frame
   315  	ADDV	$-8, R3
   316  	JAL	runtime·newstack(SB)
   317  
   318  	// Not reached, but make sure the return PC from the call to newstack
   319  	// is still in this function, and not the beginning of the next.
   320  	UNDEF
   321  
   322  TEXT runtime·morestack_noctxt(SB),NOSPLIT|NOFRAME,$0-0
   323  	// Force SPWRITE. This function doesn't actually write SP,
   324  	// but it is called with a special calling convention where
   325  	// the caller doesn't save LR on stack but passes it as a
   326  	// register (R5), and the unwinder currently doesn't understand.
   327  	// Make it SPWRITE to stop unwinding. (See issue 54332)
   328  	MOVV    R3, R3
   329  
   330  	MOVV	R0, REGCTXT
   331  	JMP	runtime·morestack(SB)
   332  
   333  // reflectcall: call a function with the given argument list
   334  // func call(stackArgsType *_type, f *FuncVal, stackArgs *byte, stackArgsSize, stackRetOffset, frameSize uint32, regArgs *abi.RegArgs).
   335  // we don't have variable-sized frames, so we use a small number
   336  // of constant-sized-frame functions to encode a few bits of size in the pc.
   337  // Caution: ugly multiline assembly macros in your future!
   338  
   339  #define DISPATCH(NAME,MAXSIZE)		\
   340  	MOVV	$MAXSIZE, R30;		\
   341  	SGTU	R19, R30, R30;		\
   342  	BNE	R30, 3(PC);			\
   343  	MOVV	$NAME(SB), R4;	\
   344  	JMP	(R4)
   345  // Note: can't just "BR NAME(SB)" - bad inlining results.
   346  
   347  TEXT ·reflectcall(SB), NOSPLIT|NOFRAME, $0-48
   348  	MOVWU frameSize+32(FP), R19
   349  	DISPATCH(runtime·call32, 32)
   350  	DISPATCH(runtime·call64, 64)
   351  	DISPATCH(runtime·call128, 128)
   352  	DISPATCH(runtime·call256, 256)
   353  	DISPATCH(runtime·call512, 512)
   354  	DISPATCH(runtime·call1024, 1024)
   355  	DISPATCH(runtime·call2048, 2048)
   356  	DISPATCH(runtime·call4096, 4096)
   357  	DISPATCH(runtime·call8192, 8192)
   358  	DISPATCH(runtime·call16384, 16384)
   359  	DISPATCH(runtime·call32768, 32768)
   360  	DISPATCH(runtime·call65536, 65536)
   361  	DISPATCH(runtime·call131072, 131072)
   362  	DISPATCH(runtime·call262144, 262144)
   363  	DISPATCH(runtime·call524288, 524288)
   364  	DISPATCH(runtime·call1048576, 1048576)
   365  	DISPATCH(runtime·call2097152, 2097152)
   366  	DISPATCH(runtime·call4194304, 4194304)
   367  	DISPATCH(runtime·call8388608, 8388608)
   368  	DISPATCH(runtime·call16777216, 16777216)
   369  	DISPATCH(runtime·call33554432, 33554432)
   370  	DISPATCH(runtime·call67108864, 67108864)
   371  	DISPATCH(runtime·call134217728, 134217728)
   372  	DISPATCH(runtime·call268435456, 268435456)
   373  	DISPATCH(runtime·call536870912, 536870912)
   374  	DISPATCH(runtime·call1073741824, 1073741824)
   375  	MOVV	$runtime·badreflectcall(SB), R4
   376  	JMP	(R4)
   377  
   378  #define CALLFN(NAME,MAXSIZE)			\
   379  TEXT NAME(SB), WRAPPER, $MAXSIZE-48;		\
   380  	NO_LOCAL_POINTERS;			\
   381  	/* copy arguments to stack */		\
   382  	MOVV	arg+16(FP), R4;			\
   383  	MOVWU	argsize+24(FP), R5;		\
   384  	MOVV	R3, R12;			\
   385  	MOVV	$16, R13;			\
   386  	ADDV	$8, R12;			\
   387  	BLT	R5, R13, check8;		\
   388  	/* copy 16 bytes a time */		\
   389  	MOVBU	internal∕cpu·Loong64+const_offsetLOONG64HasLSX(SB), R16;	\
   390  	BEQ	R16, copy16_again;		\
   391  loop16:;					\
   392  	VMOVQ	(R4), V0;			\
   393  	ADDV	$16, R4;			\
   394  	ADDV	$-16, R5;			\
   395  	VMOVQ	V0, (R12);			\
   396  	ADDV	$16, R12;			\
   397  	BGE	R5, R13, loop16;		\
   398  	JMP	check8;				\
   399  copy16_again:;					\
   400  	MOVV	(R4), R14;			\
   401  	MOVV	8(R4), R15;			\
   402  	ADDV	$16, R4;			\
   403  	ADDV	$-16, R5;			\
   404  	MOVV	R14, (R12);			\
   405  	MOVV	R15, 8(R12);			\
   406  	ADDV	$16, R12;			\
   407  	BGE	R5, R13, copy16_again;		\
   408  check8:;					\
   409  	/* R13 = 8 */;				\
   410  	SRLV	$1, R13;			\
   411  	BLT	R5, R13, 6(PC);			\
   412  	/* copy 8 bytes a time */		\
   413  	MOVV	(R4), R14;			\
   414  	ADDV	$8, R4;				\
   415  	ADDV	$-8, R5;			\
   416  	MOVV	R14, (R12);			\
   417  	ADDV	$8, R12;			\
   418  	BEQ     R5, R0, 7(PC);  		\
   419  	/* copy 1 byte a time for the rest */	\
   420  	MOVBU   (R4), R14;      		\
   421  	ADDV    $1, R4;         		\
   422  	ADDV    $-1, R5;        		\
   423  	MOVBU   R14, (R12);     		\
   424  	ADDV    $1, R12;        		\
   425  	JMP     -6(PC);         		\
   426  	/* set up argument registers */		\
   427  	MOVV	regArgs+40(FP), R25;		\
   428  	JAL	·unspillArgs(SB);		\
   429  	/* call function */			\
   430  	MOVV	f+8(FP), REGCTXT;		\
   431  	MOVV	(REGCTXT), R25;			\
   432  	PCDATA  $PCDATA_StackMapIndex, $0;	\
   433  	JAL	(R25);				\
   434  	/* copy return values back */		\
   435  	MOVV	regArgs+40(FP), R25;		\
   436  	JAL	·spillArgs(SB);			\
   437  	MOVV	argtype+0(FP), R7;		\
   438  	MOVV	arg+16(FP), R4;			\
   439  	MOVWU	n+24(FP), R5;			\
   440  	MOVWU	retoffset+28(FP), R6;		\
   441  	ADDV	$8, R3, R12;			\
   442  	ADDV	R6, R12; 			\
   443  	ADDV	R6, R4;				\
   444  	SUBVU	R6, R5;				\
   445  	JAL	callRet<>(SB);			\
   446  	RET
   447  
   448  // callRet copies return values back at the end of call*. This is a
   449  // separate function so it can allocate stack space for the arguments
   450  // to reflectcallmove. It does not follow the Go ABI; it expects its
   451  // arguments in registers.
   452  TEXT callRet<>(SB), NOSPLIT, $40-0
   453  	NO_LOCAL_POINTERS
   454  	MOVV	R7, 8(R3)
   455  	MOVV	R4, 16(R3)
   456  	MOVV	R12, 24(R3)
   457  	MOVV	R5, 32(R3)
   458  	MOVV	R25, 40(R3)
   459  	JAL	runtime·reflectcallmove(SB)
   460  	RET
   461  
   462  CALLFN(·call16, 16)
   463  CALLFN(·call32, 32)
   464  CALLFN(·call64, 64)
   465  CALLFN(·call128, 128)
   466  CALLFN(·call256, 256)
   467  CALLFN(·call512, 512)
   468  CALLFN(·call1024, 1024)
   469  CALLFN(·call2048, 2048)
   470  CALLFN(·call4096, 4096)
   471  CALLFN(·call8192, 8192)
   472  CALLFN(·call16384, 16384)
   473  CALLFN(·call32768, 32768)
   474  CALLFN(·call65536, 65536)
   475  CALLFN(·call131072, 131072)
   476  CALLFN(·call262144, 262144)
   477  CALLFN(·call524288, 524288)
   478  CALLFN(·call1048576, 1048576)
   479  CALLFN(·call2097152, 2097152)
   480  CALLFN(·call4194304, 4194304)
   481  CALLFN(·call8388608, 8388608)
   482  CALLFN(·call16777216, 16777216)
   483  CALLFN(·call33554432, 33554432)
   484  CALLFN(·call67108864, 67108864)
   485  CALLFN(·call134217728, 134217728)
   486  CALLFN(·call268435456, 268435456)
   487  CALLFN(·call536870912, 536870912)
   488  CALLFN(·call1073741824, 1073741824)
   489  
   490  TEXT runtime·procyieldAsm<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-0
   491  	BEQ	R4, done
   492  	RDTIMED	R0, R5
   493  	IBAR	$0
   494  delay:
   495  	NOP
   496  	RDTIMED	R0, R6
   497  	SUBV	R5, R6
   498  	BGEU	R4, R6, delay
   499  done:
   500  	RET
   501  
   502  // Save state of caller into g->sched.
   503  // but using fake PC from systemstack_switch.
   504  // Must only be called from functions with no locals ($0)
   505  // or else unwinding from systemstack_switch is incorrect.
   506  // Smashes R19.
   507  TEXT gosave_systemstack_switch<>(SB),NOSPLIT|NOFRAME,$0
   508  	MOVV    $runtime·systemstack_switch(SB), R19
   509  	ADDV	$8, R19
   510  	MOVV	R19, (g_sched+gobuf_pc)(g)
   511  	MOVV	R3, (g_sched+gobuf_sp)(g)
   512  	MOVV	R0, (g_sched+gobuf_lr)(g)
   513  	// Assert ctxt is zero. See func save.
   514  	MOVV	(g_sched+gobuf_ctxt)(g), R19
   515  	BEQ	R19, 2(PC)
   516  	JAL	runtime·abort(SB)
   517  	RET
   518  
   519  // func asmcgocall(fn, arg unsafe.Pointer) int32
   520  // Call fn(arg) on the scheduler stack,
   521  // aligned appropriately for the gcc ABI.
   522  // See cgocall.go for more details.
   523  TEXT ·asmcgocall(SB),NOSPLIT,$0-20
   524  	MOVV	fn+0(FP), R25
   525  	MOVV	arg+8(FP), R4
   526  
   527  	MOVV	R3, R12	// save original stack pointer
   528  	MOVV	g, R13
   529  
   530  	// Figure out if we need to switch to m->g0 stack.
   531  	// We get called to create new OS threads too, and those
   532  	// come in on the m->g0 stack already.
   533  	BEQ	g, R0, nosave
   534  	MOVV	g_m(g), R5
   535  	MOVV	m_gsignal(R5), R6
   536  	BEQ	R6, g, g0
   537  	MOVV	m_g0(R5), R6
   538  	BEQ	R6, g, g0
   539  
   540  	JAL	gosave_systemstack_switch<>(SB)
   541  	MOVV	R6, g
   542  	JAL	runtime·save_g(SB)
   543  	MOVV	(g_sched+gobuf_sp)(g), R3
   544  
   545  	// Now on a scheduling stack (a pthread-created stack).
   546  g0:
   547  	// Save room for two of our pointers.
   548  	ADDV	$-16, R3
   549  	MOVV	R13, 0(R3)	// save old g on stack
   550  	MOVV	(g_stack+stack_hi)(R13), R13
   551  	SUBVU	R12, R13
   552  	MOVV	R13, 8(R3)	// save depth in old g stack (can't just save SP, as stack might be copied during a callback)
   553  	JAL	(R25)
   554  
   555  	// Restore g, stack pointer. R4 is return value.
   556  	MOVV	0(R3), g
   557  	JAL	runtime·save_g(SB)
   558  	MOVV	(g_stack+stack_hi)(g), R5
   559  	MOVV	8(R3), R6
   560  	SUBVU	R6, R5
   561  	MOVV	R5, R3
   562  
   563  	MOVW	R4, ret+16(FP)
   564  	RET
   565  
   566  nosave:
   567  	// Running on a system stack, perhaps even without a g.
   568  	// Having no g can happen during thread creation or thread teardown.
   569  	MOVV	fn+0(FP), R25
   570  	MOVV	arg+8(FP), R4
   571  	MOVV	R3, R12
   572  	ADDV	$-16, R3
   573  	MOVV	R0, 0(R3)	// Where above code stores g, in case someone looks during debugging.
   574  	MOVV	R12, 8(R3)	// Save original stack pointer.
   575  	JAL	(R25)
   576  	MOVV	8(R3), R3	// Restore stack pointer.
   577  	MOVW	R4, ret+16(FP)
   578  	RET
   579  
   580  // func asmcgocall_no_g(fn, arg unsafe.Pointer)
   581  // Call fn(arg) aligned appropriately for the gcc ABI.
   582  // Called on a system stack, and there may be no g yet.
   583  TEXT ·asmcgocall_no_g(SB),NOSPLIT,$0-16
   584  	MOVV	fn+0(FP), R25
   585  	MOVV	arg+8(FP), R4
   586  	JAL	(R25)
   587  	RET
   588  
   589  // func cgocallback(fn, frame unsafe.Pointer, ctxt uintptr)
   590  // See cgocall.go for more details.
   591  TEXT ·cgocallback(SB),NOSPLIT,$24-24
   592  	NO_LOCAL_POINTERS
   593  
   594  	// Skip cgocallbackg, just dropm when fn is nil, and frame is the saved g.
   595  	// It is used to dropm while thread is exiting.
   596  	MOVV    fn+0(FP), R5
   597  	BNE	R5, loadg
   598  	// Restore the g from frame.
   599  	MOVV    frame+8(FP), g
   600  	JMP	dropm
   601  
   602  loadg:
   603  	// Load m and g from thread-local storage.
   604  	MOVB	runtime·iscgo(SB), R19
   605  	BEQ	R19, nocgo
   606  	JAL	runtime·load_g(SB)
   607  nocgo:
   608  
   609  	// If g is nil, Go did not create the current thread,
   610  	// or if this thread never called into Go on pthread platforms.
   611  	// Call needm to obtain one for temporary use.
   612  	// In this case, we're running on the thread stack, so there's
   613  	// lots of space, but the linker doesn't know. Hide the call from
   614  	// the linker analysis by using an indirect call.
   615  	BEQ	g, needm
   616  
   617  	MOVV	g_m(g), R12
   618  	MOVV	R12, savedm-8(SP)
   619  	JMP	havem
   620  
   621  needm:
   622  	MOVV	g, savedm-8(SP) // g is zero, so is m.
   623  	MOVV	$runtime·needAndBindM(SB), R4
   624  	JAL	(R4)
   625  
   626  	// Set m->sched.sp = SP, so that if a panic happens
   627  	// during the function we are about to execute, it will
   628  	// have a valid SP to run on the g0 stack.
   629  	// The next few lines (after the havem label)
   630  	// will save this SP onto the stack and then write
   631  	// the same SP back to m->sched.sp. That seems redundant,
   632  	// but if an unrecovered panic happens, unwindm will
   633  	// restore the g->sched.sp from the stack location
   634  	// and then systemstack will try to use it. If we don't set it here,
   635  	// that restored SP will be uninitialized (typically 0) and
   636  	// will not be usable.
   637  	MOVV	g_m(g), R12
   638  	MOVV	m_g0(R12), R19
   639  	MOVV	R3, (g_sched+gobuf_sp)(R19)
   640  
   641  havem:
   642  	// Now there's a valid m, and we're running on its m->g0.
   643  	// Save current m->g0->sched.sp on stack and then set it to SP.
   644  	// Save current sp in m->g0->sched.sp in preparation for
   645  	// switch back to m->curg stack.
   646  	// NOTE: unwindm knows that the saved g->sched.sp is at 8(R29) aka savedsp-16(SP).
   647  	MOVV	m_g0(R12), R19
   648  	MOVV	(g_sched+gobuf_sp)(R19), R13
   649  	MOVV	R13, savedsp-24(SP) // must match frame size
   650  	MOVV	R3, (g_sched+gobuf_sp)(R19)
   651  
   652  	// Switch to m->curg stack and call runtime.cgocallbackg.
   653  	// Because we are taking over the execution of m->curg
   654  	// but *not* resuming what had been running, we need to
   655  	// save that information (m->curg->sched) so we can restore it.
   656  	// We can restore m->curg->sched.sp easily, because calling
   657  	// runtime.cgocallbackg leaves SP unchanged upon return.
   658  	// To save m->curg->sched.pc, we push it onto the stack.
   659  	// This has the added benefit that it looks to the traceback
   660  	// routine like cgocallbackg is going to return to that
   661  	// PC (because the frame we allocate below has the same
   662  	// size as cgocallback_gofunc's frame declared above)
   663  	// so that the traceback will seamlessly trace back into
   664  	// the earlier calls.
   665  	MOVV	m_curg(R12), g
   666  	JAL	runtime·save_g(SB)
   667  	MOVV	(g_sched+gobuf_sp)(g), R13 // prepare stack as R13
   668  	MOVV	(g_sched+gobuf_pc)(g), R7
   669  	MOVV	R7, -(24+8)(R13) // "saved LR"; must match frame size
   670  
   671  	MOVV    fn+0(FP), R4
   672  	MOVV    frame+8(FP), R5
   673  	MOVV    ctxt+16(FP), R6
   674  	SUBV	$(24+8), R13	// Allocate the same frame size on the g stack
   675  	MOVV	R13, R3		// switch stack
   676  	JAL	runtime·cgocallbackg<ABIInternal>(SB)
   677  
   678  	// Restore g->sched (== m->curg->sched) from saved values.
   679  	MOVV	0(R3), R7
   680  	MOVV	R7, (g_sched+gobuf_pc)(g)
   681  	MOVV	$(24+8)(R3), R13 // must match frame size
   682  	MOVV	R13, (g_sched+gobuf_sp)(g)
   683  
   684  	// Switch back to m->g0's stack and restore m->g0->sched.sp.
   685  	// (Unlike m->curg, the g0 goroutine never uses sched.pc,
   686  	// so we do not have to restore it.)
   687  	MOVV	g_m(g), R12
   688  	MOVV	m_g0(R12), g
   689  	JAL	runtime·save_g(SB)
   690  	MOVV	(g_sched+gobuf_sp)(g), R3
   691  	MOVV	savedsp-24(SP), R13 // must match frame size
   692  	MOVV	R13, (g_sched+gobuf_sp)(g)
   693  
   694  	// If the m on entry was nil, we called needm above to borrow an m,
   695  	// 1. for the duration of the call on non-pthread platforms,
   696  	// 2. or the duration of the C thread alive on pthread platforms.
   697  	// If the m on entry wasn't nil,
   698  	// 1. the thread might be a Go thread,
   699  	// 2. or it wasn't the first call from a C thread on pthread platforms,
   700  	//    since then we skip dropm to resue the m in the first call.
   701  	MOVV	savedm-8(SP), R12
   702  	BNE	R12, droppedm
   703  
   704  	// Skip dropm to reuse it in the next call, when a pthread key has been created.
   705  	MOVV	_cgo_pthread_key_created(SB), R12
   706  	// It means cgo is disabled when _cgo_pthread_key_created is a nil pointer, need dropm.
   707  	BEQ	R12, dropm
   708  	MOVV    (R12), R12
   709  	BNE	R12, droppedm
   710  
   711  dropm:
   712  	MOVV	$runtime·dropm(SB), R4
   713  	JAL	(R4)
   714  droppedm:
   715  
   716  	// Done!
   717  	RET
   718  
   719  // void setg(G*); set g. for use by needm.
   720  TEXT runtime·setg(SB), NOSPLIT, $0-8
   721  	MOVV	gg+0(FP), g
   722  	// This only happens if iscgo, so jump straight to save_g
   723  	JAL	runtime·save_g(SB)
   724  	RET
   725  
   726  // void setg_gcc(G*); set g called from gcc with g in R4
   727  TEXT setg_gcc<>(SB),NOSPLIT,$0-0
   728  	MOVV	R4, g
   729  	JAL	runtime·save_g(SB)
   730  	RET
   731  
   732  TEXT runtime·abort(SB),NOSPLIT|NOFRAME,$0-0
   733  	MOVW	(R0), R0
   734  	UNDEF
   735  
   736  // Called from cgo wrappers, this function returns g->m->curg.stack.hi.
   737  // Must obey the gcc calling convention.
   738  TEXT _cgo_topofstack(SB),NOSPLIT,$16
   739  	// g (R22) and REGTMP (R30)  might be clobbered by load_g. They
   740  	// are callee-save in the gcc calling convention, so save them.
   741  	MOVV	R30, savedREGTMP-16(SP)
   742  	MOVV	g, savedG-8(SP)
   743  
   744  	JAL	runtime·load_g(SB)
   745  	MOVV	g_m(g), R19
   746  	MOVV	m_curg(R19), R19
   747  	MOVV	(g_stack+stack_hi)(R19), R4 // return value in R4
   748  
   749  	MOVV	savedG-8(SP), g
   750  	MOVV	savedREGTMP-16(SP), R30
   751  	RET
   752  
   753  // The top-most function running on a goroutine
   754  // returns to goexit+PCQuantum.
   755  TEXT runtime·goexit(SB),NOSPLIT|NOFRAME|TOPFRAME,$0-0
   756  	NOOP
   757  	JAL	runtime·goexit1(SB)	// does not return
   758  	// traceback from goexit1 must hit code range of goexit
   759  	NOOP
   760  
   761  // This is called from .init_array and follows the platform, not Go, ABI.
   762  TEXT runtime·addmoduledata(SB),NOSPLIT,$0-0
   763  	ADDV	$-0x10, R3
   764  	MOVV	R30, 8(R3) // The access to global variables below implicitly uses R30, which is callee-save
   765  	MOVV	runtime·lastmoduledatap(SB), R12
   766  	MOVV	R4, moduledata_next(R12)
   767  	MOVV	R4, runtime·lastmoduledatap(SB)
   768  	MOVV	8(R3), R30
   769  	ADDV	$0x10, R3
   770  	RET
   771  
   772  // spillArgs stores return values from registers to a *internal/abi.RegArgs in R25.
   773  TEXT ·spillArgs(SB),NOSPLIT,$0-0
   774  	MOVV	R4, (0*8)(R25)
   775  	MOVV	R5, (1*8)(R25)
   776  	MOVV	R6, (2*8)(R25)
   777  	MOVV	R7, (3*8)(R25)
   778  	MOVV	R8, (4*8)(R25)
   779  	MOVV	R9, (5*8)(R25)
   780  	MOVV	R10, (6*8)(R25)
   781  	MOVV	R11, (7*8)(R25)
   782  	MOVV	R12, (8*8)(R25)
   783  	MOVV	R13, (9*8)(R25)
   784  	MOVV	R14, (10*8)(R25)
   785  	MOVV	R15, (11*8)(R25)
   786  	MOVV	R16, (12*8)(R25)
   787  	MOVV	R17, (13*8)(R25)
   788  	MOVV	R18, (14*8)(R25)
   789  	MOVV	R19, (15*8)(R25)
   790  	MOVD	F0, (16*8)(R25)
   791  	MOVD	F1, (17*8)(R25)
   792  	MOVD	F2, (18*8)(R25)
   793  	MOVD	F3, (19*8)(R25)
   794  	MOVD	F4, (20*8)(R25)
   795  	MOVD	F5, (21*8)(R25)
   796  	MOVD	F6, (22*8)(R25)
   797  	MOVD	F7, (23*8)(R25)
   798  	MOVD	F8, (24*8)(R25)
   799  	MOVD	F9, (25*8)(R25)
   800  	MOVD	F10, (26*8)(R25)
   801  	MOVD	F11, (27*8)(R25)
   802  	MOVD	F12, (28*8)(R25)
   803  	MOVD	F13, (29*8)(R25)
   804  	MOVD	F14, (30*8)(R25)
   805  	MOVD	F15, (31*8)(R25)
   806  	RET
   807  
   808  // unspillArgs loads args into registers from a *internal/abi.RegArgs in R25.
   809  TEXT ·unspillArgs(SB),NOSPLIT,$0-0
   810  	MOVV	(0*8)(R25), R4
   811  	MOVV	(1*8)(R25), R5
   812  	MOVV	(2*8)(R25), R6
   813  	MOVV	(3*8)(R25), R7
   814  	MOVV	(4*8)(R25), R8
   815  	MOVV	(5*8)(R25), R9
   816  	MOVV	(6*8)(R25), R10
   817  	MOVV	(7*8)(R25), R11
   818  	MOVV	(8*8)(R25), R12
   819  	MOVV	(9*8)(R25), R13
   820  	MOVV	(10*8)(R25), R14
   821  	MOVV	(11*8)(R25), R15
   822  	MOVV	(12*8)(R25), R16
   823  	MOVV	(13*8)(R25), R17
   824  	MOVV	(14*8)(R25), R18
   825  	MOVV	(15*8)(R25), R19
   826  	MOVD	(16*8)(R25), F0
   827  	MOVD	(17*8)(R25), F1
   828  	MOVD	(18*8)(R25), F2
   829  	MOVD	(19*8)(R25), F3
   830  	MOVD	(20*8)(R25), F4
   831  	MOVD	(21*8)(R25), F5
   832  	MOVD	(22*8)(R25), F6
   833  	MOVD	(23*8)(R25), F7
   834  	MOVD	(24*8)(R25), F8
   835  	MOVD	(25*8)(R25), F9
   836  	MOVD	(26*8)(R25), F10
   837  	MOVD	(27*8)(R25), F11
   838  	MOVD	(28*8)(R25), F12
   839  	MOVD	(29*8)(R25), F13
   840  	MOVD	(30*8)(R25), F14
   841  	MOVD	(31*8)(R25), F15
   842  	RET
   843  
   844  // gcWriteBarrier informs the GC about heap pointer writes.
   845  //
   846  // gcWriteBarrier does NOT follow the Go ABI. It accepts the
   847  // number of bytes of buffer needed in R29, and returns a pointer
   848  // to the buffer space in R29.
   849  // It clobbers R30 (the linker temp register).
   850  // The act of CALLing gcWriteBarrier will clobber R1 (LR).
   851  // It does not clobber any other general-purpose registers,
   852  // but may clobber others (e.g., floating point registers).
   853  TEXT gcWriteBarrier<>(SB),NOSPLIT,$216
   854  	// Save the registers clobbered by the fast path.
   855  	MOVV	R19, 208(R3)
   856  	MOVV	R13, 216(R3)
   857  retry:
   858  	MOVV	g_m(g), R19
   859  	MOVV	m_p(R19), R19
   860  	MOVV	(p_wbBuf+wbBuf_next)(R19), R13
   861  	MOVV	(p_wbBuf+wbBuf_end)(R19), R30 // R30 is linker temp register
   862  	// Increment wbBuf.next position.
   863  	ADDV	R29, R13
   864  	// Is the buffer full?
   865  	BLTU	R30, R13, flush
   866  	// Commit to the larger buffer.
   867  	MOVV	R13, (p_wbBuf+wbBuf_next)(R19)
   868  	// Make return value (the original next position)
   869  	SUBV	R29, R13, R29
   870  	// Restore registers.
   871  	MOVV	208(R3), R19
   872  	MOVV	216(R3), R13
   873  	RET
   874  
   875  flush:
   876  	// Save all general purpose registers since these could be
   877  	// clobbered by wbBufFlush and were not saved by the caller.
   878  	MOVV	R27, 8(R3)
   879  	MOVV	R28, 16(R3)
   880  	// R1 is LR, which was saved by the prologue.
   881  	MOVV	R2, 24(R3)
   882  	// R3 is SP.
   883  	MOVV	R4, 32(R3)
   884  	MOVV	R5, 40(R3)
   885  	MOVV	R6, 48(R3)
   886  	MOVV	R7, 56(R3)
   887  	MOVV	R8, 64(R3)
   888  	MOVV	R9, 72(R3)
   889  	MOVV	R10, 80(R3)
   890  	MOVV	R11, 88(R3)
   891  	MOVV	R12, 96(R3)
   892  	// R13 already saved
   893  	MOVV	R14, 104(R3)
   894  	MOVV	R15, 112(R3)
   895  	MOVV	R16, 120(R3)
   896  	MOVV	R17, 128(R3)
   897  	MOVV	R18, 136(R3)
   898  	// R19 already saved
   899  	MOVV	R20, 144(R3)
   900  	MOVV	R21, 152(R3)
   901  	// R22 is g.
   902  	MOVV	R23, 160(R3)
   903  	MOVV	R24, 168(R3)
   904  	MOVV	R25, 176(R3)
   905  	MOVV	R26, 184(R3)
   906  	// R27 already saved
   907  	// R28 already saved.
   908  	MOVV	R29, 192(R3)
   909  	// R30 is tmp register.
   910  	MOVV	R31, 200(R3)
   911  
   912  	CALL	runtime·wbBufFlush(SB)
   913  
   914  	MOVV	8(R3), R27
   915  	MOVV	16(R3), R28
   916  	MOVV	24(R3), R2
   917  	MOVV	32(R3), R4
   918  	MOVV	40(R3), R5
   919  	MOVV	48(R3), R6
   920  	MOVV	56(R3), R7
   921  	MOVV	64(R3), R8
   922  	MOVV	72(R3), R9
   923  	MOVV	80(R3), R10
   924  	MOVV	88(R3), R11
   925  	MOVV	96(R3), R12
   926  	MOVV	104(R3), R14
   927  	MOVV	112(R3), R15
   928  	MOVV	120(R3), R16
   929  	MOVV	128(R3), R17
   930  	MOVV	136(R3), R18
   931  	MOVV	144(R3), R20
   932  	MOVV	152(R3), R21
   933  	MOVV	160(R3), R23
   934  	MOVV	168(R3), R24
   935  	MOVV	176(R3), R25
   936  	MOVV	184(R3), R26
   937  	MOVV	192(R3), R29
   938  	MOVV	200(R3), R31
   939  	JMP	retry
   940  
   941  TEXT runtime·gcWriteBarrier1<ABIInternal>(SB),NOSPLIT,$0
   942  	MOVV	$8, R29
   943  	JMP	gcWriteBarrier<>(SB)
   944  TEXT runtime·gcWriteBarrier2<ABIInternal>(SB),NOSPLIT,$0
   945  	MOVV	$16, R29
   946  	JMP	gcWriteBarrier<>(SB)
   947  TEXT runtime·gcWriteBarrier3<ABIInternal>(SB),NOSPLIT,$0
   948  	MOVV	$24, R29
   949  	JMP	gcWriteBarrier<>(SB)
   950  TEXT runtime·gcWriteBarrier4<ABIInternal>(SB),NOSPLIT,$0
   951  	MOVV	$32, R29
   952  	JMP	gcWriteBarrier<>(SB)
   953  TEXT runtime·gcWriteBarrier5<ABIInternal>(SB),NOSPLIT,$0
   954  	MOVV	$40, R29
   955  	JMP	gcWriteBarrier<>(SB)
   956  TEXT runtime·gcWriteBarrier6<ABIInternal>(SB),NOSPLIT,$0
   957  	MOVV	$48, R29
   958  	JMP	gcWriteBarrier<>(SB)
   959  TEXT runtime·gcWriteBarrier7<ABIInternal>(SB),NOSPLIT,$0
   960  	MOVV	$56, R29
   961  	JMP	gcWriteBarrier<>(SB)
   962  TEXT runtime·gcWriteBarrier8<ABIInternal>(SB),NOSPLIT,$0
   963  	MOVV	$64, R29
   964  	JMP	gcWriteBarrier<>(SB)
   965  
   966  DATA	debugCallFrameTooLarge<>+0x00(SB)/20, $"call frame too large"
   967  GLOBL	debugCallFrameTooLarge<>(SB), RODATA, $20	// Size duplicated below
   968  
   969  // debugCallV2 is the entry point for debugger-injected function
   970  // calls on running goroutines. It informs the runtime that a
   971  // debug call has been injected and creates a call frame for the
   972  // debugger to fill in.
   973  //
   974  // To inject a function call, a debugger should:
   975  // 1. Check that the goroutine is in state _Grunning and that
   976  //    there are at least 280 bytes free on the stack.
   977  // 2. Set SP as SP-8.
   978  // 3. Store the current LR in (SP) (using the SP after step 2).
   979  // 4. Store the current PC in the LR register.
   980  // 5. Write the desired argument frame size at SP-8
   981  // 6. Save all machine registers so they can be restored later by the debugger.
   982  // 7. Set the PC to debugCallV2 and resume execution.
   983  //
   984  // If the goroutine is in state _Grunnable, then it's not generally
   985  // safe to inject a call because it may return out via other runtime
   986  // operations. Instead, the debugger should unwind the stack to find
   987  // the return to non-runtime code, add a temporary breakpoint there,
   988  // and inject the call once that breakpoint is hit.
   989  //
   990  // If the goroutine is in any other state, it's not safe to inject a call.
   991  //
   992  // This function communicates back to the debugger by setting R19 and
   993  // invoking BREAK to raise a breakpoint signal. Note that the signal PC of
   994  // the signal triggered by the BREAK instruction is the PC where the signal
   995  // is trapped, not the next PC, so to resume execution, the debugger needs
   996  // to set the signal PC to PC+4. See the comments in the implementation for
   997  // the protocol the debugger is expected to follow. InjectDebugCall in the
   998  // runtime tests demonstrates this protocol.
   999  //
  1000  // The debugger must ensure that any pointers passed to the function
  1001  // obey escape analysis requirements. Specifically, it must not pass
  1002  // a stack pointer to an escaping argument. debugCallV2 cannot check
  1003  // this invariant.
  1004  //
  1005  // This is ABIInternal because Go code injects its PC directly into new
  1006  // goroutine stacks.
  1007  TEXT runtime·debugCallV2<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-0
  1008  	MOVV    R1, -272(R3)
  1009  	ADDV    $-272, R3
  1010  
  1011  	// We can't do anything that might clobber any of these
  1012  	// registers before this.
  1013  	MOVV    R2, (4*8)(R3)
  1014  	MOVV    R4, (5*8)(R3)
  1015  	MOVV    R5, (6*8)(R3)
  1016  	MOVV    R6, (7*8)(R3)
  1017  	MOVV    R7, (8*8)(R3)
  1018  	MOVV    R8, (9*8)(R3)
  1019  	MOVV    R9, (10*8)(R3)
  1020  	MOVV    R10, (11*8)(R3)
  1021  	MOVV    R11, (12*8)(R3)
  1022  	MOVV    R12, (13*8)(R3)
  1023  	MOVV    R13, (14*8)(R3)
  1024  	MOVV    R14, (15*8)(R3)
  1025  	MOVV    R15, (16*8)(R3)
  1026  	MOVV    R16, (17*8)(R3)
  1027  	MOVV    R17, (18*8)(R3)
  1028  	MOVV    R18, (19*8)(R3)
  1029  	MOVV    R19, (20*8)(R3)
  1030  	MOVV    R20, (21*8)(R3)
  1031  	MOVV    R21, (22*8)(R3)
  1032  	MOVV    g, (23*8)(R3)
  1033  	MOVV    R23, (24*8)(R3)
  1034  	MOVV    R24, (25*8)(R3)
  1035  	MOVV    R25, (26*8)(R3)
  1036  	MOVV    R26, (27*8)(R3)
  1037  	MOVV    R27, (28*8)(R3)
  1038  	MOVV    R28, (29*8)(R3)
  1039  	MOVV    R29, (30*8)(R3)
  1040  	MOVV    R30, (31*8)(R3)
  1041  	MOVV    R31, (32*8)(R3)
  1042  
  1043  	// Perform a safe-point check.
  1044  	MOVV    R1, 8(R3)
  1045  	CALL    runtime·debugCallCheck(SB)
  1046  	MOVV    16(R3), R30
  1047  	BEQ R30, good
  1048  
  1049  	// The safety check failed. Put the reason string at the top
  1050  	// of the stack.
  1051  	MOVV    R30, 8(R3)
  1052  
  1053  	MOVV    24(R3), R30
  1054  	MOVV    R30, 16(R3)
  1055  
  1056  	MOVV    $8, R19
  1057  	BREAK
  1058  	JMP restore
  1059  
  1060  good:
  1061  	// Registers are saved and it's safe to make a call.
  1062  	// Open up a call frame, moving the stack if necessary.
  1063  	//
  1064  	// Once the frame is allocated, this will set R19 to 0 and
  1065  	// invoke BREAK. The debugger should write the argument
  1066  	// frame for the call at SP+8, set up argument registers,
  1067  	// set the LR as the signal PC + 4, set the PC to the function
  1068  	// to call, set R29 to point to the closure (if a closure call),
  1069  	// and resume execution.
  1070  	//
  1071  	// If the function returns, this will set R19 to 1 and invoke
  1072  	// BREAK. The debugger can then inspect any return value saved
  1073  	// on the stack at SP+8 and in registers. To resume execution,
  1074  	// the debugger should restore the LR from (SP).
  1075  	//
  1076  	// If the function panics, this will set R19 to 2 and invoke BREAK.
  1077  	// The interface{} value of the panic will be at SP+8. The debugger
  1078  	// can inspect the panic value and resume execution again.
  1079  #define DEBUG_CALL_DISPATCH(NAME,MAXSIZE)	\
  1080  	MOVV    $MAXSIZE, R27;         \
  1081  	BLT R27, R30, 5(PC);            \
  1082  	MOVV    $NAME(SB), R28;			\
  1083  	MOVV    R28, 8(R3);			\
  1084  	CALL    runtime·debugCallWrap(SB);	\
  1085  	JMP restore
  1086  
  1087  	MOVV    264(R3), R30 // the argument frame size
  1088  	DEBUG_CALL_DISPATCH(debugCall32<>, 32)
  1089  	DEBUG_CALL_DISPATCH(debugCall64<>, 64)
  1090  	DEBUG_CALL_DISPATCH(debugCall128<>, 128)
  1091  	DEBUG_CALL_DISPATCH(debugCall256<>, 256)
  1092  	DEBUG_CALL_DISPATCH(debugCall512<>, 512)
  1093  	DEBUG_CALL_DISPATCH(debugCall1024<>, 1024)
  1094  	DEBUG_CALL_DISPATCH(debugCall2048<>, 2048)
  1095  	DEBUG_CALL_DISPATCH(debugCall4096<>, 4096)
  1096  	DEBUG_CALL_DISPATCH(debugCall8192<>, 8192)
  1097  	DEBUG_CALL_DISPATCH(debugCall16384<>, 16384)
  1098  	DEBUG_CALL_DISPATCH(debugCall32768<>, 32768)
  1099  	DEBUG_CALL_DISPATCH(debugCall65536<>, 65536)
  1100  	// The frame size is too large. Report the error.
  1101  	MOVV    $debugCallFrameTooLarge<>(SB), R30
  1102  	MOVV    R30, 8(R3)
  1103  	MOVV    $20, R30
  1104  	MOVV    R30, 16(R3) // length of debugCallFrameTooLarge string
  1105  	MOVV    $8, R19
  1106  	BREAK
  1107  	JMP restore
  1108  
  1109  restore:
  1110  	// Calls and failures resume here.
  1111  	//
  1112  	// Set R19 to 16 and invoke BREAK. The debugger should restore
  1113  	// all registers except for PC and SP and resume execution.
  1114  	MOVV    $16, R19
  1115  	BREAK
  1116  	// We must not modify flags after this point.
  1117  
  1118  	// Restore pointer-containing registers, which may have been
  1119  	// modified from the debugger's copy by stack copying.
  1120  	MOVV    (4*8)(R3), R2
  1121  	MOVV    (5*8)(R3), R4
  1122  	MOVV    (6*8)(R3), R5
  1123  	MOVV    (7*8)(R3), R6
  1124  	MOVV    (8*8)(R3), R7
  1125  	MOVV    (9*8)(R3), R8
  1126  	MOVV    (10*8)(R3), R9
  1127  	MOVV    (11*8)(R3), R10
  1128  	MOVV    (12*8)(R3), R11
  1129  	MOVV    (13*8)(R3), R12
  1130  	MOVV    (14*8)(R3), R13
  1131  	MOVV    (15*8)(R3), R14
  1132  	MOVV    (16*8)(R3), R15
  1133  	MOVV    (17*8)(R3), R16
  1134  	MOVV    (18*8)(R3), R17
  1135  	MOVV    (19*8)(R3), R18
  1136  	MOVV    (20*8)(R3), R19
  1137  	MOVV    (21*8)(R3), R20
  1138  	MOVV    (22*8)(R3), R21
  1139  	MOVV    (23*8)(R3), g
  1140  	MOVV    (24*8)(R3), R23
  1141  	MOVV    (25*8)(R3), R24
  1142  	MOVV    (26*8)(R3), R25
  1143  	MOVV    (27*8)(R3), R26
  1144  	MOVV    (28*8)(R3), R27
  1145  	MOVV    (29*8)(R3), R28
  1146  	MOVV    (30*8)(R3), R29
  1147  	MOVV    (31*8)(R3), R30
  1148  	MOVV    (32*8)(R3), R31
  1149  
  1150  	MOVV    0(R3), R30
  1151  	ADDV    $280, R3 // Add 8 more bytes, see saveSigContext
  1152  	MOVV    -8(R3), R1
  1153  	JMP (R30)
  1154  
  1155  // runtime.debugCallCheck assumes that functions defined with the
  1156  // DEBUG_CALL_FN macro are safe points to inject calls.
  1157  #define DEBUG_CALL_FN(NAME,MAXSIZE)		\
  1158  TEXT NAME(SB),WRAPPER,$MAXSIZE-0;		\
  1159  	NO_LOCAL_POINTERS;		\
  1160  	MOVV    $0, R19;		\
  1161  	BREAK;		\
  1162  	MOVV    $1, R19;		\
  1163  	BREAK;		\
  1164  	RET
  1165  DEBUG_CALL_FN(debugCall32<>, 32)
  1166  DEBUG_CALL_FN(debugCall64<>, 64)
  1167  DEBUG_CALL_FN(debugCall128<>, 128)
  1168  DEBUG_CALL_FN(debugCall256<>, 256)
  1169  DEBUG_CALL_FN(debugCall512<>, 512)
  1170  DEBUG_CALL_FN(debugCall1024<>, 1024)
  1171  DEBUG_CALL_FN(debugCall2048<>, 2048)
  1172  DEBUG_CALL_FN(debugCall4096<>, 4096)
  1173  DEBUG_CALL_FN(debugCall8192<>, 8192)
  1174  DEBUG_CALL_FN(debugCall16384<>, 16384)
  1175  DEBUG_CALL_FN(debugCall32768<>, 32768)
  1176  DEBUG_CALL_FN(debugCall65536<>, 65536)
  1177  
  1178  // func debugCallPanicked(val interface{})
  1179  TEXT runtime·debugCallPanicked(SB),NOSPLIT,$16-16
  1180  	// Copy the panic value to the top of stack at SP+8.
  1181  	MOVV    val_type+0(FP), R30
  1182  	MOVV    R30, 8(R3)
  1183  	MOVV    val_data+8(FP), R30
  1184  	MOVV    R30, 16(R3)
  1185  	MOVV    $2, R19
  1186  	BREAK
  1187  	RET
  1188  
  1189  TEXT runtime·panicBounds<ABIInternal>(SB),NOSPLIT,$144-0
  1190  	NO_LOCAL_POINTERS
  1191  	// Save all 16 int registers that could have an index in them.
  1192  	// They may be pointers, but if they are they are dead.
  1193  	// Skip R0 aka ZERO, R1 aka LR, R2 aka thread pointer, R3 aka SP.
  1194  	MOVV	R4, 24(R3)
  1195  	MOVV	R5, 32(R3)
  1196  	MOVV	R6, 40(R3)
  1197  	MOVV	R7, 48(R3)
  1198  	MOVV	R8, 56(R3)
  1199  	MOVV	R9, 64(R3)
  1200  	MOVV	R10, 72(R3)
  1201  	MOVV	R11, 80(R3)
  1202  	MOVV	R12, 88(R3)
  1203  	MOVV	R13, 96(R3)
  1204  	MOVV	R14, 104(R3)
  1205  	MOVV	R15, 112(R3)
  1206  	MOVV	R16, 120(R3)
  1207  	MOVV	R17, 128(R3)
  1208  	MOVV	R18, 136(R3)
  1209  	MOVV	R19, 144(R3)
  1210  
  1211  	MOVV	R1, R4		// PC immediately after call to panicBounds
  1212  	ADDV	$24, R3, R5	// pointer to save area
  1213  	CALL	runtime·panicBounds64<ABIInternal>(SB)
  1214  	RET
  1215  

View as plain text