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 "textflag.h"
7#include "asm_ppc64x.h"
8#include "cgo/abi_ppc64x.h"
9
10TEXT _rt0_ppc64le_linux(SB),NOSPLIT,$0
11	XOR R0, R0	  // Make sure R0 is zero before _main
12	BR _main<>(SB)
13
14TEXT _rt0_ppc64le_linux_lib(SB),NOSPLIT|NOFRAME,$0
15	// This is called with ELFv2 calling conventions. Convert to Go.
16	// Allocate argument storage for call to newosproc0.
17	STACK_AND_SAVE_HOST_TO_GO_ABI(16)
18
19	MOVD	R3, _rt0_ppc64le_linux_lib_argc<>(SB)
20	MOVD	R4, _rt0_ppc64le_linux_lib_argv<>(SB)
21
22	// Synchronous initialization.
23	MOVD	$runtime·libpreinit(SB), R12
24	MOVD	R12, CTR
25	BL	(CTR)
26
27	// Create a new thread to do the runtime initialization and return.
28	MOVD	_cgo_sys_thread_create(SB), R12
29	CMP	$0, R12
30	BEQ	nocgo
31	MOVD	$_rt0_ppc64le_linux_lib_go(SB), R3
32	MOVD	$0, R4
33	MOVD	R12, CTR
34	BL	(CTR)
35	BR	done
36
37nocgo:
38	MOVD	$0x800000, R12                     // stacksize = 8192KB
39	MOVD	R12, 8+FIXED_FRAME(R1)
40	MOVD	$_rt0_ppc64le_linux_lib_go(SB), R12
41	MOVD	R12, 16+FIXED_FRAME(R1)
42	MOVD	$runtime·newosproc0(SB),R12
43	MOVD	R12, CTR
44	BL	(CTR)
45
46done:
47	// Restore and return to ELFv2 caller.
48	UNSTACK_AND_RESTORE_GO_TO_HOST_ABI(16)
49	RET
50
51TEXT _rt0_ppc64le_linux_lib_go(SB),NOSPLIT,$0
52	MOVD	_rt0_ppc64le_linux_lib_argc<>(SB), R3
53	MOVD	_rt0_ppc64le_linux_lib_argv<>(SB), R4
54	MOVD	$runtime·rt0_go(SB), R12
55	MOVD	R12, CTR
56	BR	(CTR)
57
58DATA _rt0_ppc64le_linux_lib_argc<>(SB)/8, $0
59GLOBL _rt0_ppc64le_linux_lib_argc<>(SB),NOPTR, $8
60DATA _rt0_ppc64le_linux_lib_argv<>(SB)/8, $0
61GLOBL _rt0_ppc64le_linux_lib_argv<>(SB),NOPTR, $8
62
63TEXT _main<>(SB),NOSPLIT,$-8
64	// In a statically linked binary, the stack contains argc,
65	// argv as argc string pointers followed by a NULL, envv as a
66	// sequence of string pointers followed by a NULL, and auxv.
67	// The TLS pointer should be initialized to 0.
68	//
69	// In an ELFv2 compliant dynamically linked binary, R3 contains argc,
70	// R4 contains argv, R5 contains envp, R6 contains auxv, and R13
71	// contains the TLS pointer.
72	//
73	// When loading via glibc, the first doubleword on the stack points
74	// to NULL a value. (that is *(uintptr)(R1) == 0). This is used to
75	// differentiate static vs dynamically linked binaries.
76	//
77	// If loading with the musl loader, it doesn't follow the ELFv2 ABI. It
78	// passes argc/argv similar to the linux kernel, R13 (TLS) is
79	// initialized, and R3/R4 are undefined.
80	MOVD	(R1), R12
81	CMP	R12, $0
82	BEQ	tls_and_argcv_in_reg
83
84	// Arguments are passed via the stack (musl loader or a static binary)
85	MOVD	0(R1), R3 // argc
86	ADD	$8, R1, R4 // argv
87
88	// Did the TLS pointer get set? If so, don't change it (e.g musl).
89	CMP	R13, $0
90	BNE	tls_and_argcv_in_reg
91
92	MOVD	$runtime·m0+m_tls(SB), R13 // TLS
93	ADD	$0x7000, R13
94
95tls_and_argcv_in_reg:
96	BR	main(SB)
97
98TEXT main(SB),NOSPLIT,$-8
99	MOVD	$runtime·rt0_go(SB), R12
100	MOVD	R12, CTR
101	BR	(CTR)
102