1/*
2 * Copyright (c) 2021 Travis Geiselbrecht
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8#include <lk/asm.h>
9
10.section .text.boot
11FUNCTION(_start)
12    // load the first 4 args that were pushed on whatever stack we have
13    // NOTE: assumes stack is pointing at at least readable memory
14    movl    %sp@(4),%d0
15    movl    %sp@(8),%d1
16    movl    %sp@(12),%d2
17    movl    %sp@(16),%d3
18
19#if ARCH_DO_RELOCATION
20    lea     %pc@(_start),%a0 // load the current address using PC relative addressing mode
21    movl    #_start,%a1      // load the same symbol absolutely
22    cmpal   %a0,%a1
23    beqs    bss_clear
24
25    // load the end address for loop termination
26    movl    #_end,%a2
27
28    // copy forwards
29    // NOTE: assumes the source and target do not overlap
300:
31    movel   %a0@+,%a1@+
32    cmpal   %a1,%a2
33    bne     0b
34
35    // branch to the new location
36    movl    #bss_clear,%a0
37    jmp     %a0@
38#endif
39
40    // clear bss
41bss_clear:
42    lea     __bss_start,%a0
43    lea     __bss_end,%a1
44    cmpl    %a0,%a1
45    beqs    1f
46    // zero 4 bytes at a time
470:
48    clrl    %a0@+
49    cmpal   %a1,%a0
50    bne     0b
511:
52
53    // load the initial stack pointer
54    lea     _default_stack_top,%sp
55
56    // branch into C land with 4 args off the previous stack
57    movl    %d3,%sp@-
58    movl    %d2,%sp@-
59    movl    %d1,%sp@-
60    movl    %d0,%sp@-
61    jsr     lk_main
62
63    // if we return from main just loop forever
64    bra     .
65END_FUNCTION(_start)
66
67.bss
68.align 4
69_default_stack_base:
70    .skip 4096
71_default_stack_top:
72
73