1/*
2 * Copyright 2020 ETH Zurich
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
17 */
18
19/* Copyright (c) 2017  SiFive Inc. All rights reserved.
20 * Copyright (c) 2019  ETH Zürich and University of Bologna
21 * This copyrighted material is made available to anyone wishing to use,
22 * modify, copy, or redistribute it subject to the terms and conditions
23 * of the FreeBSD License.   This program is distributed in the hope that
24 * it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
25 * including the implied warranties of MERCHANTABILITY or FITNESS FOR
26 * A PARTICULAR PURPOSE.  A copy of this license is available at
27 * http://www.opensource.org/licenses.
28 */
29
30/* entry point */
31.section .text.start
32.global _start
33.type _start, @function
34
35_start:
36/* initialize global pointer */
37.option push
38.option norelax
391:	auipc gp, %pcrel_hi(__global_pointer$)
40	addi  gp, gp, %pcrel_lo(1b)
41.option pop
42
43#if defined(ARCHI_HAS_CLUSTER)
44	/* TODO */
45#endif
46/* initialize stack pointer */
47	la sp, __stack_top
48
49	/*or a0, a0, 0*/
50	and a0, a0, 0
51	csrw mie, a0 // disable interrupts
52	csrr a0, mie
53/* set vector table address */
54	la a0, __vector_start
55	or a0, a0, 1 /* enable vectored mode (hardcoded anyway for CV32E40P) */
56	csrw mtvec, a0
57
58/* clear the bss segment */
59	la t0, __bss_start
60        la t1, __bss_end
61/* TODO: optionally compile this out to speed simulation */
621:
63	sw zero,0(t0)
64	addi t0, t0, 4
65	bltu t0, t1, 1b
66
67/* new-style constructors and destructors */
68#if defined(__PULP_USE_LIBC)
69	la a0, __libc_fini_array
70	call atexit
71	call __libc_init_array
72#endif
73
74/* call main */
75	lw a0, 0(sp)                    /* a0 = argc */
76	addi a1, sp, __SIZEOF_POINTER__ /* a1 = argv */
77	li a2, 0                        /* a2 = envp = NULL */
78	call entry
79	tail exit
80
81.size  _start, .-_start
82
83.global _init
84.type   _init, @function
85.global _fini
86.type   _fini, @function
87_init:
88_fini:
89 /* These don't have to do anything since we use init_array/fini_array. Prevent
90    missing symbol error */
91	ret
92.size  _init, .-_init
93.size _fini, .-_fini
94