...
Run Format

Text file src/runtime/asm_arm.s

Documentation: runtime

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	#include "go_asm.h"
     6	#include "go_tls.h"
     7	#include "funcdata.h"
     8	#include "textflag.h"
     9	
    10	// _rt0_arm is common startup code for most ARM systems when using
    11	// internal linking. This is the entry point for the program from the
    12	// kernel for an ordinary -buildmode=exe program. The stack holds the
    13	// number of arguments and the C-style argv.
    14	TEXT _rt0_arm(SB),NOSPLIT|NOFRAME,$0
    15		MOVW	(R13), R0	// argc
    16		MOVW	$4(R13), R1		// argv
    17		B	runtime·rt0_go(SB)
    18	
    19	// main is common startup code for most ARM systems when using
    20	// external linking. The C startup code will call the symbol "main"
    21	// passing argc and argv in the usual C ABI registers R0 and R1.
    22	TEXT main(SB),NOSPLIT|NOFRAME,$0
    23		B	runtime·rt0_go(SB)
    24	
    25	// _rt0_arm_lib is common startup code for most ARM systems when
    26	// using -buildmode=c-archive or -buildmode=c-shared. The linker will
    27	// arrange to invoke this function as a global constructor (for
    28	// c-archive) or when the shared library is loaded (for c-shared).
    29	// We expect argc and argv to be passed in the usual C ABI registers
    30	// R0 and R1.
    31	TEXT _rt0_arm_lib(SB),NOSPLIT,$104
    32		// Preserve callee-save registers. Raspberry Pi's dlopen(), for example,
    33		// actually cares that R11 is preserved.
    34		MOVW	R4, 12(R13)
    35		MOVW	R5, 16(R13)
    36		MOVW	R6, 20(R13)
    37		MOVW	R7, 24(R13)
    38		MOVW	R8, 28(R13)
    39		MOVW	g, 32(R13)
    40		MOVW	R11, 36(R13)
    41	
    42		// Skip floating point registers on GOARM < 6.
    43		MOVB    runtime·goarm(SB), R11
    44		CMP	$6, R11
    45		BLT	skipfpsave
    46		MOVD	F8, (40+8*0)(R13)
    47		MOVD	F9, (40+8*1)(R13)
    48		MOVD	F10, (40+8*2)(R13)
    49		MOVD	F11, (40+8*3)(R13)
    50		MOVD	F12, (40+8*4)(R13)
    51		MOVD	F13, (40+8*5)(R13)
    52		MOVD	F14, (40+8*6)(R13)
    53		MOVD	F15, (40+8*7)(R13)
    54	skipfpsave:
    55		// Save argc/argv.
    56		MOVW	R0, _rt0_arm_lib_argc<>(SB)
    57		MOVW	R1, _rt0_arm_lib_argv<>(SB)
    58	
    59		MOVW	$0, g // Initialize g.
    60	
    61		// Synchronous initialization.
    62		CALL	runtime·libpreinit(SB)
    63	
    64		// Create a new thread to do the runtime initialization.
    65		MOVW	_cgo_sys_thread_create(SB), R2
    66		CMP	$0, R2
    67		BEQ	nocgo
    68		MOVW	$_rt0_arm_lib_go<>(SB), R0
    69		MOVW	$0, R1
    70		BL	(R2)
    71		B	rr
    72	nocgo:
    73		MOVW	$0x800000, R0                     // stacksize = 8192KB
    74		MOVW	$_rt0_arm_lib_go<>(SB), R1  // fn
    75		MOVW	R0, 4(R13)
    76		MOVW	R1, 8(R13)
    77		BL	runtime·newosproc0(SB)
    78	rr:
    79		// Restore callee-save registers and return.
    80		MOVB    runtime·goarm(SB), R11
    81		CMP	$6, R11
    82		BLT	skipfprest
    83		MOVD	(40+8*0)(R13), F8
    84		MOVD	(40+8*1)(R13), F9
    85		MOVD	(40+8*2)(R13), F10
    86		MOVD	(40+8*3)(R13), F11
    87		MOVD	(40+8*4)(R13), F12
    88		MOVD	(40+8*5)(R13), F13
    89		MOVD	(40+8*6)(R13), F14
    90		MOVD	(40+8*7)(R13), F15
    91	skipfprest:
    92		MOVW	12(R13), R4
    93		MOVW	16(R13), R5
    94		MOVW	20(R13), R6
    95		MOVW	24(R13), R7
    96		MOVW	28(R13), R8
    97		MOVW	32(R13), g
    98		MOVW	36(R13), R11
    99		RET
   100	
   101	// _rt0_arm_lib_go initializes the Go runtime.
   102	// This is started in a separate thread by _rt0_arm_lib.
   103	TEXT _rt0_arm_lib_go<>(SB),NOSPLIT,$8
   104		MOVW	_rt0_arm_lib_argc<>(SB), R0
   105		MOVW	_rt0_arm_lib_argv<>(SB), R1
   106		B	runtime·rt0_go(SB)
   107	
   108	DATA _rt0_arm_lib_argc<>(SB)/4,$0
   109	GLOBL _rt0_arm_lib_argc<>(SB),NOPTR,$4
   110	DATA _rt0_arm_lib_argv<>(SB)/4,$0
   111	GLOBL _rt0_arm_lib_argv<>(SB),NOPTR,$4
   112	
   113	// using NOFRAME means do not save LR on stack.
   114	// argc is in R0, argv is in R1.
   115	TEXT runtime·rt0_go(SB),NOSPLIT|NOFRAME,$0
   116		MOVW	$0xcafebabe, R12
   117	
   118		// copy arguments forward on an even stack
   119		// use R13 instead of SP to avoid linker rewriting the offsets
   120		SUB	$64, R13		// plenty of scratch
   121		AND	$~7, R13
   122		MOVW	R0, 60(R13)		// save argc, argv away
   123		MOVW	R1, 64(R13)
   124	
   125		// set up g register
   126		// g is R10
   127		MOVW	$runtime·g0(SB), g
   128		MOVW	$runtime·m0(SB), R8
   129	
   130		// save m->g0 = g0
   131		MOVW	g, m_g0(R8)
   132		// save g->m = m0
   133		MOVW	R8, g_m(g)
   134	
   135		// create istack out of the OS stack
   136		// (1MB of system stack is available on iOS and Android)
   137		MOVW	$(-64*1024+104)(R13), R0
   138		MOVW	R0, g_stackguard0(g)
   139		MOVW	R0, g_stackguard1(g)
   140		MOVW	R0, (g_stack+stack_lo)(g)
   141		MOVW	R13, (g_stack+stack_hi)(g)
   142	
   143		BL	runtime·emptyfunc(SB)	// fault if stack check is wrong
   144	
   145		BL	runtime·_initcgo(SB)	// will clobber R0-R3
   146	
   147		// update stackguard after _cgo_init
   148		MOVW	(g_stack+stack_lo)(g), R0
   149		ADD	$const__StackGuard, R0
   150		MOVW	R0, g_stackguard0(g)
   151		MOVW	R0, g_stackguard1(g)
   152	
   153		BL	runtime·check(SB)
   154	
   155		// saved argc, argv
   156		MOVW	60(R13), R0
   157		MOVW	R0, 4(R13)
   158		MOVW	64(R13), R1
   159		MOVW	R1, 8(R13)
   160		BL	runtime·args(SB)
   161		BL	runtime·checkgoarm(SB)
   162		BL	runtime·osinit(SB)
   163		BL	runtime·schedinit(SB)
   164	
   165		// create a new goroutine to start program
   166		MOVW	$runtime·mainPC(SB), R0
   167		MOVW.W	R0, -4(R13)
   168		MOVW	$8, R0
   169		MOVW.W	R0, -4(R13)
   170		MOVW	$0, R0
   171		MOVW.W	R0, -4(R13)	// push $0 as guard
   172		BL	runtime·newproc(SB)
   173		MOVW	$12(R13), R13	// pop args and LR
   174	
   175		// start this M
   176		BL	runtime·mstart(SB)
   177	
   178		MOVW	$1234, R0
   179		MOVW	$1000, R1
   180		MOVW	R0, (R1)	// fail hard
   181	
   182	DATA	runtime·mainPC+0(SB)/4,$runtime·main(SB)
   183	GLOBL	runtime·mainPC(SB),RODATA,$4
   184	
   185	TEXT runtime·breakpoint(SB),NOSPLIT,$0-0
   186		// gdb won't skip this breakpoint instruction automatically,
   187		// so you must manually "set $pc+=4" to skip it and continue.
   188	#ifdef GOOS_nacl
   189		WORD	$0xe125be7f	// BKPT 0x5bef, NACL_INSTR_ARM_BREAKPOINT
   190	#else
   191	#ifdef GOOS_plan9
   192		WORD	$0xD1200070	// undefined instruction used as armv5 breakpoint in Plan 9
   193	#else
   194		WORD	$0xe7f001f0	// undefined instruction that gdb understands is a software breakpoint
   195	#endif
   196	#endif
   197		RET
   198	
   199	TEXT runtime·asminit(SB),NOSPLIT,$0-0
   200		// disable runfast (flush-to-zero) mode of vfp if runtime.goarm > 5
   201		MOVB	runtime·goarm(SB), R11
   202		CMP	$5, R11
   203		BLE	4(PC)
   204		WORD	$0xeef1ba10	// vmrs r11, fpscr
   205		BIC	$(1<<24), R11
   206		WORD	$0xeee1ba10	// vmsr fpscr, r11
   207		RET
   208	
   209	/*
   210	 *  go-routine
   211	 */
   212	
   213	// void gosave(Gobuf*)
   214	// save state in Gobuf; setjmp
   215	TEXT runtime·gosave(SB),NOSPLIT|NOFRAME,$0-4
   216		MOVW	buf+0(FP), R0
   217		MOVW	R13, gobuf_sp(R0)
   218		MOVW	LR, gobuf_pc(R0)
   219		MOVW	g, gobuf_g(R0)
   220		MOVW	$0, R11
   221		MOVW	R11, gobuf_lr(R0)
   222		MOVW	R11, gobuf_ret(R0)
   223		// Assert ctxt is zero. See func save.
   224		MOVW	gobuf_ctxt(R0), R0
   225		CMP	R0, R11
   226		B.EQ	2(PC)
   227		CALL	runtime·badctxt(SB)
   228		RET
   229	
   230	// void gogo(Gobuf*)
   231	// restore state from Gobuf; longjmp
   232	TEXT runtime·gogo(SB),NOSPLIT,$8-4
   233		MOVW	buf+0(FP), R1
   234		MOVW	gobuf_g(R1), R0
   235		BL	setg<>(SB)
   236	
   237		// NOTE: We updated g above, and we are about to update SP.
   238		// Until LR and PC are also updated, the g/SP/LR/PC quadruple
   239		// are out of sync and must not be used as the basis of a traceback.
   240		// Sigprof skips the traceback when SP is not within g's bounds,
   241		// and when the PC is inside this function, runtime.gogo.
   242		// Since we are about to update SP, until we complete runtime.gogo
   243		// we must not leave this function. In particular, no calls
   244		// after this point: it must be straight-line code until the
   245		// final B instruction.
   246		// See large comment in sigprof for more details.
   247		MOVW	gobuf_sp(R1), R13	// restore SP==R13
   248		MOVW	gobuf_lr(R1), LR
   249		MOVW	gobuf_ret(R1), R0
   250		MOVW	gobuf_ctxt(R1), R7
   251		MOVW	$0, R11
   252		MOVW	R11, gobuf_sp(R1)	// clear to help garbage collector
   253		MOVW	R11, gobuf_ret(R1)
   254		MOVW	R11, gobuf_lr(R1)
   255		MOVW	R11, gobuf_ctxt(R1)
   256		MOVW	gobuf_pc(R1), R11
   257		CMP	R11, R11 // set condition codes for == test, needed by stack split
   258		B	(R11)
   259	
   260	// func mcall(fn func(*g))
   261	// Switch to m->g0's stack, call fn(g).
   262	// Fn must never return. It should gogo(&g->sched)
   263	// to keep running g.
   264	TEXT runtime·mcall(SB),NOSPLIT|NOFRAME,$0-4
   265		// Save caller state in g->sched.
   266		MOVW	R13, (g_sched+gobuf_sp)(g)
   267		MOVW	LR, (g_sched+gobuf_pc)(g)
   268		MOVW	$0, R11
   269		MOVW	R11, (g_sched+gobuf_lr)(g)
   270		MOVW	g, (g_sched+gobuf_g)(g)
   271	
   272		// Switch to m->g0 & its stack, call fn.
   273		MOVW	g, R1
   274		MOVW	g_m(g), R8
   275		MOVW	m_g0(R8), R0
   276		BL	setg<>(SB)
   277		CMP	g, R1
   278		B.NE	2(PC)
   279		B	runtime·badmcall(SB)
   280		MOVB	runtime·iscgo(SB), R11
   281		CMP	$0, R11
   282		BL.NE	runtime·save_g(SB)
   283		MOVW	fn+0(FP), R0
   284		MOVW	(g_sched+gobuf_sp)(g), R13
   285		SUB	$8, R13
   286		MOVW	R1, 4(R13)
   287		MOVW	R0, R7
   288		MOVW	0(R0), R0
   289		BL	(R0)
   290		B	runtime·badmcall2(SB)
   291		RET
   292	
   293	// systemstack_switch is a dummy routine that systemstack leaves at the bottom
   294	// of the G stack. We need to distinguish the routine that
   295	// lives at the bottom of the G stack from the one that lives
   296	// at the top of the system stack because the one at the top of
   297	// the system stack terminates the stack walk (see topofstack()).
   298	TEXT runtime·systemstack_switch(SB),NOSPLIT,$0-0
   299		MOVW	$0, R0
   300		BL	(R0) // clobber lr to ensure push {lr} is kept
   301		RET
   302	
   303	// func systemstack(fn func())
   304	TEXT runtime·systemstack(SB),NOSPLIT,$0-4
   305		MOVW	fn+0(FP), R0	// R0 = fn
   306		MOVW	g_m(g), R1	// R1 = m
   307	
   308		MOVW	m_gsignal(R1), R2	// R2 = gsignal
   309		CMP	g, R2
   310		B.EQ	noswitch
   311	
   312		MOVW	m_g0(R1), R2	// R2 = g0
   313		CMP	g, R2
   314		B.EQ	noswitch
   315	
   316		MOVW	m_curg(R1), R3
   317		CMP	g, R3
   318		B.EQ	switch
   319	
   320		// Bad: g is not gsignal, not g0, not curg. What is it?
   321		// Hide call from linker nosplit analysis.
   322		MOVW	$runtime·badsystemstack(SB), R0
   323		BL	(R0)
   324		B	runtime·abort(SB)
   325	
   326	switch:
   327		// save our state in g->sched. Pretend to
   328		// be systemstack_switch if the G stack is scanned.
   329		MOVW	$runtime·systemstack_switch(SB), R3
   330	#ifdef GOOS_nacl
   331		ADD	$4, R3, R3 // get past nacl-insert bic instruction
   332	#endif
   333		ADD	$4, R3, R3 // get past push {lr}
   334		MOVW	R3, (g_sched+gobuf_pc)(g)
   335		MOVW	R13, (g_sched+gobuf_sp)(g)
   336		MOVW	LR, (g_sched+gobuf_lr)(g)
   337		MOVW	g, (g_sched+gobuf_g)(g)
   338	
   339		// switch to g0
   340		MOVW	R0, R5
   341		MOVW	R2, R0
   342		BL	setg<>(SB)
   343		MOVW	R5, R0
   344		MOVW	(g_sched+gobuf_sp)(R2), R3
   345		// make it look like mstart called systemstack on g0, to stop traceback
   346		SUB	$4, R3, R3
   347		MOVW	$runtime·mstart(SB), R4
   348		MOVW	R4, 0(R3)
   349		MOVW	R3, R13
   350	
   351		// call target function
   352		MOVW	R0, R7
   353		MOVW	0(R0), R0
   354		BL	(R0)
   355	
   356		// switch back to g
   357		MOVW	g_m(g), R1
   358		MOVW	m_curg(R1), R0
   359		BL	setg<>(SB)
   360		MOVW	(g_sched+gobuf_sp)(g), R13
   361		MOVW	$0, R3
   362		MOVW	R3, (g_sched+gobuf_sp)(g)
   363		RET
   364	
   365	noswitch:
   366		// Using a tail call here cleans up tracebacks since we won't stop
   367		// at an intermediate systemstack.
   368		MOVW	R0, R7
   369		MOVW	0(R0), R0
   370		MOVW.P	4(R13), R14	// restore LR
   371		B	(R0)
   372	
   373	/*
   374	 * support for morestack
   375	 */
   376	
   377	// Called during function prolog when more stack is needed.
   378	// R3 prolog's LR
   379	// using NOFRAME means do not save LR on stack.
   380	//
   381	// The traceback routines see morestack on a g0 as being
   382	// the top of a stack (for example, morestack calling newstack
   383	// calling the scheduler calling newm calling gc), so we must
   384	// record an argument size. For that purpose, it has no arguments.
   385	TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0
   386		// Cannot grow scheduler stack (m->g0).
   387		MOVW	g_m(g), R8
   388		MOVW	m_g0(R8), R4
   389		CMP	g, R4
   390		BNE	3(PC)
   391		BL	runtime·badmorestackg0(SB)
   392		B	runtime·abort(SB)
   393	
   394		// Cannot grow signal stack (m->gsignal).
   395		MOVW	m_gsignal(R8), R4
   396		CMP	g, R4
   397		BNE	3(PC)
   398		BL	runtime·badmorestackgsignal(SB)
   399		B	runtime·abort(SB)
   400	
   401		// Called from f.
   402		// Set g->sched to context in f.
   403		MOVW	R13, (g_sched+gobuf_sp)(g)
   404		MOVW	LR, (g_sched+gobuf_pc)(g)
   405		MOVW	R3, (g_sched+gobuf_lr)(g)
   406		MOVW	R7, (g_sched+gobuf_ctxt)(g)
   407	
   408		// Called from f.
   409		// Set m->morebuf to f's caller.
   410		MOVW	R3, (m_morebuf+gobuf_pc)(R8)	// f's caller's PC
   411		MOVW	R13, (m_morebuf+gobuf_sp)(R8)	// f's caller's SP
   412		MOVW	g, (m_morebuf+gobuf_g)(R8)
   413	
   414		// Call newstack on m->g0's stack.
   415		MOVW	m_g0(R8), R0
   416		BL	setg<>(SB)
   417		MOVW	(g_sched+gobuf_sp)(g), R13
   418		MOVW	$0, R0
   419		MOVW.W  R0, -4(R13)	// create a call frame on g0 (saved LR)
   420		BL	runtime·newstack(SB)
   421	
   422		// Not reached, but make sure the return PC from the call to newstack
   423		// is still in this function, and not the beginning of the next.
   424		RET
   425	
   426	TEXT runtime·morestack_noctxt(SB),NOSPLIT|NOFRAME,$0-0
   427		MOVW	$0, R7
   428		B runtime·morestack(SB)
   429	
   430	// reflectcall: call a function with the given argument list
   431	// func call(argtype *_type, f *FuncVal, arg *byte, argsize, retoffset uint32).
   432	// we don't have variable-sized frames, so we use a small number
   433	// of constant-sized-frame functions to encode a few bits of size in the pc.
   434	// Caution: ugly multiline assembly macros in your future!
   435	
   436	#define DISPATCH(NAME,MAXSIZE)		\
   437		CMP	$MAXSIZE, R0;		\
   438		B.HI	3(PC);			\
   439		MOVW	$NAME(SB), R1;		\
   440		B	(R1)
   441	
   442	TEXT reflect·call(SB), NOSPLIT, $0-0
   443		B	·reflectcall(SB)
   444	
   445	TEXT ·reflectcall(SB),NOSPLIT|NOFRAME,$0-20
   446		MOVW	argsize+12(FP), R0
   447		DISPATCH(runtime·call16, 16)
   448		DISPATCH(runtime·call32, 32)
   449		DISPATCH(runtime·call64, 64)
   450		DISPATCH(runtime·call128, 128)
   451		DISPATCH(runtime·call256, 256)
   452		DISPATCH(runtime·call512, 512)
   453		DISPATCH(runtime·call1024, 1024)
   454		DISPATCH(runtime·call2048, 2048)
   455		DISPATCH(runtime·call4096, 4096)
   456		DISPATCH(runtime·call8192, 8192)
   457		DISPATCH(runtime·call16384, 16384)
   458		DISPATCH(runtime·call32768, 32768)
   459		DISPATCH(runtime·call65536, 65536)
   460		DISPATCH(runtime·call131072, 131072)
   461		DISPATCH(runtime·call262144, 262144)
   462		DISPATCH(runtime·call524288, 524288)
   463		DISPATCH(runtime·call1048576, 1048576)
   464		DISPATCH(runtime·call2097152, 2097152)
   465		DISPATCH(runtime·call4194304, 4194304)
   466		DISPATCH(runtime·call8388608, 8388608)
   467		DISPATCH(runtime·call16777216, 16777216)
   468		DISPATCH(runtime·call33554432, 33554432)
   469		DISPATCH(runtime·call67108864, 67108864)
   470		DISPATCH(runtime·call134217728, 134217728)
   471		DISPATCH(runtime·call268435456, 268435456)
   472		DISPATCH(runtime·call536870912, 536870912)
   473		DISPATCH(runtime·call1073741824, 1073741824)
   474		MOVW	$runtime·badreflectcall(SB), R1
   475		B	(R1)
   476	
   477	#define CALLFN(NAME,MAXSIZE)			\
   478	TEXT NAME(SB), WRAPPER, $MAXSIZE-20;		\
   479		NO_LOCAL_POINTERS;			\
   480		/* copy arguments to stack */		\
   481		MOVW	argptr+8(FP), R0;		\
   482		MOVW	argsize+12(FP), R2;		\
   483		ADD	$4, R13, R1;			\
   484		CMP	$0, R2;				\
   485		B.EQ	5(PC);				\
   486		MOVBU.P	1(R0), R5;			\
   487		MOVBU.P R5, 1(R1);			\
   488		SUB	$1, R2, R2;			\
   489		B	-5(PC);				\
   490		/* call function */			\
   491		MOVW	f+4(FP), R7;			\
   492		MOVW	(R7), R0;			\
   493		PCDATA  $PCDATA_StackMapIndex, $0;	\
   494		BL	(R0);				\
   495		/* copy return values back */		\
   496		MOVW	argtype+0(FP), R4;		\
   497		MOVW	argptr+8(FP), R0;		\
   498		MOVW	argsize+12(FP), R2;		\
   499		MOVW	retoffset+16(FP), R3;		\
   500		ADD	$4, R13, R1;			\
   501		ADD	R3, R1;				\
   502		ADD	R3, R0;				\
   503		SUB	R3, R2;				\
   504		BL	callRet<>(SB);			\
   505		RET
   506	
   507	// callRet copies return values back at the end of call*. This is a
   508	// separate function so it can allocate stack space for the arguments
   509	// to reflectcallmove. It does not follow the Go ABI; it expects its
   510	// arguments in registers.
   511	TEXT callRet<>(SB), NOSPLIT, $16-0
   512		MOVW	R4, 4(R13)
   513		MOVW	R0, 8(R13)
   514		MOVW	R1, 12(R13)
   515		MOVW	R2, 16(R13)
   516		BL	runtime·reflectcallmove(SB)
   517		RET	
   518	
   519	CALLFN(·call16, 16)
   520	CALLFN(·call32, 32)
   521	CALLFN(·call64, 64)
   522	CALLFN(·call128, 128)
   523	CALLFN(·call256, 256)
   524	CALLFN(·call512, 512)
   525	CALLFN(·call1024, 1024)
   526	CALLFN(·call2048, 2048)
   527	CALLFN(·call4096, 4096)
   528	CALLFN(·call8192, 8192)
   529	CALLFN(·call16384, 16384)
   530	CALLFN(·call32768, 32768)
   531	CALLFN(·call65536, 65536)
   532	CALLFN(·call131072, 131072)
   533	CALLFN(·call262144, 262144)
   534	CALLFN(·call524288, 524288)
   535	CALLFN(·call1048576, 1048576)
   536	CALLFN(·call2097152, 2097152)
   537	CALLFN(·call4194304, 4194304)
   538	CALLFN(·call8388608, 8388608)
   539	CALLFN(·call16777216, 16777216)
   540	CALLFN(·call33554432, 33554432)
   541	CALLFN(·call67108864, 67108864)
   542	CALLFN(·call134217728, 134217728)
   543	CALLFN(·call268435456, 268435456)
   544	CALLFN(·call536870912, 536870912)
   545	CALLFN(·call1073741824, 1073741824)
   546	
   547	// void jmpdefer(fn, sp);
   548	// called from deferreturn.
   549	// 1. grab stored LR for caller
   550	// 2. sub 4 bytes to get back to BL deferreturn
   551	// 3. B to fn
   552	// TODO(rsc): Push things on stack and then use pop
   553	// to load all registers simultaneously, so that a profiling
   554	// interrupt can never see mismatched SP/LR/PC.
   555	// (And double-check that pop is atomic in that way.)
   556	TEXT runtime·jmpdefer(SB),NOSPLIT,$0-8
   557		MOVW	0(R13), LR
   558		MOVW	$-4(LR), LR	// BL deferreturn
   559		MOVW	fv+0(FP), R7
   560		MOVW	argp+4(FP), R13
   561		MOVW	$-4(R13), R13	// SP is 4 below argp, due to saved LR
   562		MOVW	0(R7), R1
   563		B	(R1)
   564	
   565	// Save state of caller into g->sched. Smashes R11.
   566	TEXT gosave<>(SB),NOSPLIT|NOFRAME,$0
   567		MOVW	LR, (g_sched+gobuf_pc)(g)
   568		MOVW	R13, (g_sched+gobuf_sp)(g)
   569		MOVW	$0, R11
   570		MOVW	R11, (g_sched+gobuf_lr)(g)
   571		MOVW	R11, (g_sched+gobuf_ret)(g)
   572		MOVW	R11, (g_sched+gobuf_ctxt)(g)
   573		// Assert ctxt is zero. See func save.
   574		MOVW	(g_sched+gobuf_ctxt)(g), R11
   575		CMP	$0, R11
   576		B.EQ	2(PC)
   577		CALL	runtime·badctxt(SB)
   578		RET
   579	
   580	// func asmcgocall(fn, arg unsafe.Pointer) int32
   581	// Call fn(arg) on the scheduler stack,
   582	// aligned appropriately for the gcc ABI.
   583	// See cgocall.go for more details.
   584	TEXT ·asmcgocall(SB),NOSPLIT,$0-12
   585		MOVW	fn+0(FP), R1
   586		MOVW	arg+4(FP), R0
   587	
   588		MOVW	R13, R2
   589		CMP	$0, g
   590		BEQ nosave
   591		MOVW	g, R4
   592	
   593		// Figure out if we need to switch to m->g0 stack.
   594		// We get called to create new OS threads too, and those
   595		// come in on the m->g0 stack already.
   596		MOVW	g_m(g), R8
   597		MOVW	m_gsignal(R8), R3
   598		CMP	R3, g
   599		BEQ	nosave
   600		MOVW	m_g0(R8), R3
   601		CMP	R3, g
   602		BEQ	nosave
   603		BL	gosave<>(SB)
   604		MOVW	R0, R5
   605		MOVW	R3, R0
   606		BL	setg<>(SB)
   607		MOVW	R5, R0
   608		MOVW	(g_sched+gobuf_sp)(g), R13
   609	
   610		// Now on a scheduling stack (a pthread-created stack).
   611		SUB	$24, R13
   612		BIC	$0x7, R13	// alignment for gcc ABI
   613		MOVW	R4, 20(R13) // save old g
   614		MOVW	(g_stack+stack_hi)(R4), R4
   615		SUB	R2, R4
   616		MOVW	R4, 16(R13)	// save depth in stack (can't just save SP, as stack might be copied during a callback)
   617		BL	(R1)
   618	
   619		// Restore registers, g, stack pointer.
   620		MOVW	R0, R5
   621		MOVW	20(R13), R0
   622		BL	setg<>(SB)
   623		MOVW	(g_stack+stack_hi)(g), R1
   624		MOVW	16(R13), R2
   625		SUB	R2, R1
   626		MOVW	R5, R0
   627		MOVW	R1, R13
   628	
   629		MOVW	R0, ret+8(FP)
   630		RET
   631	
   632	nosave:
   633		// Running on a system stack, perhaps even without a g.
   634		// Having no g can happen during thread creation or thread teardown
   635		// (see needm/dropm on Solaris, for example).
   636		// This code is like the above sequence but without saving/restoring g
   637		// and without worrying about the stack moving out from under us
   638		// (because we're on a system stack, not a goroutine stack).
   639		// The above code could be used directly if already on a system stack,
   640		// but then the only path through this code would be a rare case on Solaris.
   641		// Using this code for all "already on system stack" calls exercises it more,
   642		// which should help keep it correct.
   643		SUB	$24, R13
   644		BIC	$0x7, R13	// alignment for gcc ABI
   645		// save null g in case someone looks during debugging.
   646		MOVW	$0, R4
   647		MOVW	R4, 20(R13)
   648		MOVW	R2, 16(R13)	// Save old stack pointer.
   649		BL	(R1)
   650		// Restore stack pointer.
   651		MOVW	16(R13), R2
   652		MOVW	R2, R13
   653		MOVW	R0, ret+8(FP)
   654		RET
   655	
   656	// cgocallback(void (*fn)(void*), void *frame, uintptr framesize, uintptr ctxt)
   657	// Turn the fn into a Go func (by taking its address) and call
   658	// cgocallback_gofunc.
   659	TEXT runtime·cgocallback(SB),NOSPLIT,$16-16
   660		MOVW	$fn+0(FP), R0
   661		MOVW	R0, 4(R13)
   662		MOVW	frame+4(FP), R0
   663		MOVW	R0, 8(R13)
   664		MOVW	framesize+8(FP), R0
   665		MOVW	R0, 12(R13)
   666		MOVW	ctxt+12(FP), R0
   667		MOVW	R0, 16(R13)
   668		MOVW	$runtime·cgocallback_gofunc(SB), R0
   669		BL	(R0)
   670		RET
   671	
   672	// cgocallback_gofunc(void (*fn)(void*), void *frame, uintptr framesize, uintptr ctxt)
   673	// See cgocall.go for more details.
   674	TEXT	·cgocallback_gofunc(SB),NOSPLIT,$8-16
   675		NO_LOCAL_POINTERS
   676		
   677		// Load m and g from thread-local storage.
   678		MOVB	runtime·iscgo(SB), R0
   679		CMP	$0, R0
   680		BL.NE	runtime·load_g(SB)
   681	
   682		// If g is nil, Go did not create the current thread.
   683		// Call needm to obtain one for temporary use.
   684		// In this case, we're running on the thread stack, so there's
   685		// lots of space, but the linker doesn't know. Hide the call from
   686		// the linker analysis by using an indirect call.
   687		CMP	$0, g
   688		B.EQ	needm
   689	
   690		MOVW	g_m(g), R8
   691		MOVW	R8, savedm-4(SP)
   692		B	havem
   693	
   694	needm:
   695		MOVW	g, savedm-4(SP) // g is zero, so is m.
   696		MOVW	$runtime·needm(SB), R0
   697		BL	(R0)
   698	
   699		// Set m->sched.sp = SP, so that if a panic happens
   700		// during the function we are about to execute, it will
   701		// have a valid SP to run on the g0 stack.
   702		// The next few lines (after the havem label)
   703		// will save this SP onto the stack and then write
   704		// the same SP back to m->sched.sp. That seems redundant,
   705		// but if an unrecovered panic happens, unwindm will
   706		// restore the g->sched.sp from the stack location
   707		// and then systemstack will try to use it. If we don't set it here,
   708		// that restored SP will be uninitialized (typically 0) and
   709		// will not be usable.
   710		MOVW	g_m(g), R8
   711		MOVW	m_g0(R8), R3
   712		MOVW	R13, (g_sched+gobuf_sp)(R3)
   713	
   714	havem:
   715		// Now there's a valid m, and we're running on its m->g0.
   716		// Save current m->g0->sched.sp on stack and then set it to SP.
   717		// Save current sp in m->g0->sched.sp in preparation for
   718		// switch back to m->curg stack.
   719		// NOTE: unwindm knows that the saved g->sched.sp is at 4(R13) aka savedsp-8(SP).
   720		MOVW	m_g0(R8), R3
   721		MOVW	(g_sched+gobuf_sp)(R3), R4
   722		MOVW	R4, savedsp-8(SP)
   723		MOVW	R13, (g_sched+gobuf_sp)(R3)
   724	
   725		// Switch to m->curg stack and call runtime.cgocallbackg.
   726		// Because we are taking over the execution of m->curg
   727		// but *not* resuming what had been running, we need to
   728		// save that information (m->curg->sched) so we can restore it.
   729		// We can restore m->curg->sched.sp easily, because calling
   730		// runtime.cgocallbackg leaves SP unchanged upon return.
   731		// To save m->curg->sched.pc, we push it onto the stack.
   732		// This has the added benefit that it looks to the traceback
   733		// routine like cgocallbackg is going to return to that
   734		// PC (because the frame we allocate below has the same
   735		// size as cgocallback_gofunc's frame declared above)
   736		// so that the traceback will seamlessly trace back into
   737		// the earlier calls.
   738		//
   739		// In the new goroutine, -4(SP) is unused (where SP refers to
   740		// m->curg's SP while we're setting it up, before we've adjusted it).
   741		MOVW	m_curg(R8), R0
   742		BL	setg<>(SB)
   743		MOVW	(g_sched+gobuf_sp)(g), R4 // prepare stack as R4
   744		MOVW	(g_sched+gobuf_pc)(g), R5
   745		MOVW	R5, -12(R4)
   746		MOVW	ctxt+12(FP), R0
   747		MOVW	R0, -8(R4)
   748		MOVW	$-12(R4), R13
   749		BL	runtime·cgocallbackg(SB)
   750	
   751		// Restore g->sched (== m->curg->sched) from saved values.
   752		MOVW	0(R13), R5
   753		MOVW	R5, (g_sched+gobuf_pc)(g)
   754		MOVW	$12(R13), R4
   755		MOVW	R4, (g_sched+gobuf_sp)(g)
   756	
   757		// Switch back to m->g0's stack and restore m->g0->sched.sp.
   758		// (Unlike m->curg, the g0 goroutine never uses sched.pc,
   759		// so we do not have to restore it.)
   760		MOVW	g_m(g), R8
   761		MOVW	m_g0(R8), R0
   762		BL	setg<>(SB)
   763		MOVW	(g_sched+gobuf_sp)(g), R13
   764		MOVW	savedsp-8(SP), R4
   765		MOVW	R4, (g_sched+gobuf_sp)(g)
   766	
   767		// If the m on entry was nil, we called needm above to borrow an m
   768		// for the duration of the call. Since the call is over, return it with dropm.
   769		MOVW	savedm-4(SP), R6
   770		CMP	$0, R6
   771		B.NE	3(PC)
   772		MOVW	$runtime·dropm(SB), R0
   773		BL	(R0)
   774	
   775		// Done!
   776		RET
   777	
   778	// void setg(G*); set g. for use by needm.
   779	TEXT runtime·setg(SB),NOSPLIT|NOFRAME,$0-4
   780		MOVW	gg+0(FP), R0
   781		B	setg<>(SB)
   782	
   783	TEXT setg<>(SB),NOSPLIT|NOFRAME,$0-0
   784		MOVW	R0, g
   785	
   786		// Save g to thread-local storage.
   787		MOVB	runtime·iscgo(SB), R0
   788		CMP	$0, R0
   789		B.EQ	2(PC)
   790		B	runtime·save_g(SB)
   791	
   792		MOVW	g, R0
   793		RET
   794	
   795	TEXT runtime·emptyfunc(SB),0,$0-0
   796		RET
   797	
   798	TEXT runtime·abort(SB),NOSPLIT|NOFRAME,$0-0
   799		MOVW	$0, R0
   800		MOVW	(R0), R1
   801	
   802	// armPublicationBarrier is a native store/store barrier for ARMv7+.
   803	// On earlier ARM revisions, armPublicationBarrier is a no-op.
   804	// This will not work on SMP ARMv6 machines, if any are in use.
   805	// To implement publicationBarrier in sys_$GOOS_arm.s using the native
   806	// instructions, use:
   807	//
   808	//	TEXT ·publicationBarrier(SB),NOSPLIT|NOFRAME,$0-0
   809	//		B	runtime·armPublicationBarrier(SB)
   810	//
   811	TEXT runtime·armPublicationBarrier(SB),NOSPLIT|NOFRAME,$0-0
   812		MOVB	runtime·goarm(SB), R11
   813		CMP	$7, R11
   814		BLT	2(PC)
   815		DMB	MB_ST
   816		RET
   817	
   818	// AES hashing not implemented for ARM
   819	TEXT runtime·aeshash(SB),NOSPLIT|NOFRAME,$0-0
   820		MOVW	$0, R0
   821		MOVW	(R0), R1
   822	TEXT runtime·aeshash32(SB),NOSPLIT|NOFRAME,$0-0
   823		MOVW	$0, R0
   824		MOVW	(R0), R1
   825	TEXT runtime·aeshash64(SB),NOSPLIT|NOFRAME,$0-0
   826		MOVW	$0, R0
   827		MOVW	(R0), R1
   828	TEXT runtime·aeshashstr(SB),NOSPLIT|NOFRAME,$0-0
   829		MOVW	$0, R0
   830		MOVW	(R0), R1
   831	
   832	TEXT runtime·return0(SB),NOSPLIT,$0
   833		MOVW	$0, R0
   834		RET
   835	
   836	TEXT runtime·procyield(SB),NOSPLIT|NOFRAME,$0
   837		MOVW	cycles+0(FP), R1
   838		MOVW	$0, R0
   839	yieldloop:
   840		WORD	$0xe320f001	// YIELD (NOP pre-ARMv6K)
   841		CMP	R0, R1
   842		B.NE	2(PC)
   843		RET
   844		SUB	$1, R1
   845		B yieldloop
   846	
   847	// Called from cgo wrappers, this function returns g->m->curg.stack.hi.
   848	// Must obey the gcc calling convention.
   849	TEXT _cgo_topofstack(SB),NOSPLIT,$8
   850		// R11 and g register are clobbered by load_g. They are
   851		// callee-save in the gcc calling convention, so save them here.
   852		MOVW	R11, saveR11-4(SP)
   853		MOVW	g, saveG-8(SP)
   854		
   855		BL	runtime·load_g(SB)
   856		MOVW	g_m(g), R0
   857		MOVW	m_curg(R0), R0
   858		MOVW	(g_stack+stack_hi)(R0), R0
   859		
   860		MOVW	saveG-8(SP), g
   861		MOVW	saveR11-4(SP), R11
   862		RET
   863	
   864	// The top-most function running on a goroutine
   865	// returns to goexit+PCQuantum.
   866	TEXT runtime·goexit(SB),NOSPLIT|NOFRAME,$0-0
   867		MOVW	R0, R0	// NOP
   868		BL	runtime·goexit1(SB)	// does not return
   869		// traceback from goexit1 must hit code range of goexit
   870		MOVW	R0, R0	// NOP
   871	
   872	// x -> x/1000000, x%1000000, called from Go with args, results on stack.
   873	TEXT runtime·usplit(SB),NOSPLIT,$0-12
   874		MOVW	x+0(FP), R0
   875		CALL	runtime·usplitR0(SB)
   876		MOVW	R0, q+4(FP)
   877		MOVW	R1, r+8(FP)
   878		RET
   879	
   880	// R0, R1 = R0/1000000, R0%1000000
   881	TEXT runtime·usplitR0(SB),NOSPLIT,$0
   882		// magic multiply to avoid software divide without available m.
   883		// see output of go tool compile -S for x/1000000.
   884		MOVW	R0, R3
   885		MOVW	$1125899907, R1
   886		MULLU	R1, R0, (R0, R1)
   887		MOVW	R0>>18, R0
   888		MOVW	$1000000, R1
   889		MULU	R0, R1
   890		SUB	R1, R3, R1
   891		RET
   892	
   893	TEXT runtime·sigreturn(SB),NOSPLIT,$0-0
   894		RET
   895	
   896	#ifndef GOOS_nacl
   897	// This is called from .init_array and follows the platform, not Go, ABI.
   898	TEXT runtime·addmoduledata(SB),NOSPLIT,$0-8
   899		MOVW	R9, saver9-4(SP) // The access to global variables below implicitly uses R9, which is callee-save
   900		MOVW	R11, saver11-8(SP) // Likewise, R11 is the temp register, but callee-save in C ABI
   901		MOVW	runtime·lastmoduledatap(SB), R1
   902		MOVW	R0, moduledata_next(R1)
   903		MOVW	R0, runtime·lastmoduledatap(SB)
   904		MOVW	saver11-8(SP), R11
   905		MOVW	saver9-4(SP), R9
   906		RET
   907	#endif
   908	
   909	TEXT ·checkASM(SB),NOSPLIT,$0-1
   910		MOVW	$1, R3
   911		MOVB	R3, ret+0(FP)
   912		RET
   913	
   914	// gcWriteBarrier performs a heap pointer write and informs the GC.
   915	//
   916	// gcWriteBarrier does NOT follow the Go ABI. It takes two arguments:
   917	// - R2 is the destination of the write
   918	// - R3 is the value being written at R2
   919	// It clobbers condition codes.
   920	// It does not clobber any other general-purpose registers,
   921	// but may clobber others (e.g., floating point registers).
   922	// The act of CALLing gcWriteBarrier will clobber R14 (LR).
   923	TEXT runtime·gcWriteBarrier(SB),NOSPLIT|NOFRAME,$0
   924		// Save the registers clobbered by the fast path.
   925		MOVM.DB.W	[R0,R1], (R13)
   926		MOVW	g_m(g), R0
   927		MOVW	m_p(R0), R0
   928		MOVW	(p_wbBuf+wbBuf_next)(R0), R1
   929		// Increment wbBuf.next position.
   930		ADD	$8, R1
   931		MOVW	R1, (p_wbBuf+wbBuf_next)(R0)
   932		MOVW	(p_wbBuf+wbBuf_end)(R0), R0
   933		CMP	R1, R0
   934		// Record the write.
   935		MOVW	R3, -8(R1)	// Record value
   936		MOVW	(R2), R0	// TODO: This turns bad writes into bad reads.
   937		MOVW	R0, -4(R1)	// Record *slot
   938		// Is the buffer full? (flags set in CMP above)
   939		B.EQ	flush
   940	ret:
   941		MOVM.IA.W	(R13), [R0,R1]
   942		// Do the write.
   943		MOVW	R3, (R2)
   944		// Normally RET on nacl clobbers R12, but because this
   945		// function has no frame it doesn't have to usual epilogue.
   946		RET
   947	
   948	flush:
   949		// Save all general purpose registers since these could be
   950		// clobbered by wbBufFlush and were not saved by the caller.
   951		//
   952		// R0 and R1 were saved at entry.
   953		// R10 is g, so preserved.
   954		// R11 is linker temp, so no need to save.
   955		// R13 is stack pointer.
   956		// R15 is PC.
   957		//
   958		// This also sets up R2 and R3 as the arguments to wbBufFlush.
   959		MOVM.DB.W	[R2-R9,R12], (R13)
   960		// Save R14 (LR) because the fast path above doesn't save it,
   961		// but needs it to RET. This is after the MOVM so it appears below
   962		// the arguments in the stack frame.
   963		MOVM.DB.W	[R14], (R13)
   964	
   965		// This takes arguments R2 and R3.
   966		CALL	runtime·wbBufFlush(SB)
   967	
   968		MOVM.IA.W	(R13), [R14]
   969		MOVM.IA.W	(R13), [R2-R9,R12]
   970		JMP	ret

View as plain text