1// Copyright 2019 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 "textflag.h"
6#include "cgo/abi_arm64.h"
7
8// On FreeBSD argc/argv are passed in R0, not RSP
9TEXT _rt0_arm64_freebsd(SB),NOSPLIT|NOFRAME,$0
10	ADD	$8, R0, R1	// argv
11	MOVD	0(R0), R0	// argc
12	BL	main(SB)
13
14// When building with -buildmode=c-shared, this symbol is called when the shared
15// library is loaded.
16TEXT _rt0_arm64_freebsd_lib(SB),NOSPLIT,$184
17	// Preserve callee-save registers.
18	SAVE_R19_TO_R28(24)
19	SAVE_F8_TO_F15(104)
20
21	// Initialize g as null in case of using g later e.g. sigaction in cgo_sigaction.go
22	MOVD	ZR, g
23
24	MOVD	R0, _rt0_arm64_freebsd_lib_argc<>(SB)
25	MOVD	R1, _rt0_arm64_freebsd_lib_argv<>(SB)
26
27	// Synchronous initialization.
28	MOVD	$runtime·libpreinit(SB), R4
29	BL	(R4)
30
31	// Create a new thread to do the runtime initialization and return.
32	MOVD	_cgo_sys_thread_create(SB), R4
33	CBZ	R4, nocgo
34	MOVD	$_rt0_arm64_freebsd_lib_go(SB), R0
35	MOVD	$0, R1
36	SUB	$16, RSP	// reserve 16 bytes for sp-8 where fp may be saved.
37	BL	(R4)
38	ADD	$16, RSP
39	B	restore
40
41nocgo:
42	MOVD	$0x800000, R0                     // stacksize = 8192KB
43	MOVD	$_rt0_arm64_freebsd_lib_go(SB), R1
44	MOVD	R0, 8(RSP)
45	MOVD	R1, 16(RSP)
46	MOVD	$runtime·newosproc0(SB),R4
47	BL	(R4)
48
49restore:
50	// Restore callee-save registers.
51	RESTORE_R19_TO_R28(24)
52	RESTORE_F8_TO_F15(104)
53	RET
54
55TEXT _rt0_arm64_freebsd_lib_go(SB),NOSPLIT,$0
56	MOVD	_rt0_arm64_freebsd_lib_argc<>(SB), R0
57	MOVD	_rt0_arm64_freebsd_lib_argv<>(SB), R1
58	MOVD	$runtime·rt0_go(SB),R4
59	B       (R4)
60
61DATA _rt0_arm64_freebsd_lib_argc<>(SB)/8, $0
62GLOBL _rt0_arm64_freebsd_lib_argc<>(SB),NOPTR, $8
63DATA _rt0_arm64_freebsd_lib_argv<>(SB)/8, $0
64GLOBL _rt0_arm64_freebsd_lib_argv<>(SB),NOPTR, $8
65
66
67TEXT main(SB),NOSPLIT|NOFRAME,$0
68	MOVD	$runtime·rt0_go(SB), R2
69	BL	(R2)
70exit:
71	MOVD	$0, R0
72	MOVD	$1, R8	// SYS_exit
73	SVC
74	B	exit
75