-
Notifications
You must be signed in to change notification settings - Fork 0
/
crt0.leros.c
98 lines (77 loc) · 2.29 KB
/
crt0.leros.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
asm(" .text ");
asm(" .globl start ");
asm(" start: ");
#ifndef LEROS_STACK_PTR
#define LEROS_STACK_PTR 0x7FFFFFF0
#endif
#define STR(x) #x
#define XSTR(s) STR(s)
__attribute__((naked))
void _start(){
// Load stack pointer in register r1
asm("loadi " XSTR(LEROS_STACK_PTR) "\n"
"loadhi " XSTR(LEROS_STACK_PTR >> 8) "\n"
"loadh2i " XSTR(LEROS_STACK_PTR >> 16) "\n"
"loadh3i " XSTR(LEROS_STACK_PTR >> 24) "\n"
"store r1\n");
// Constant register initialization
asm("loadi 0x0\n"
"store r100\n"
"loadi 0x1\n"
"store r101\n"
"loadi 0x80\n"
"loadhi 0x0\n"
"store r102\n"
"loadi 0x0\n"
"loadhi 0x80\n"
"loadh2i 0x0\n"
"store r103\n"
"loadi 0x0\n"
"loadh3i 0x80\n"
"store r104\n"
"loadi 0xFF\n"
"loadhi 0x0\n"
"store r105\n"
"loadhi 0xFF\n"
"loadh2i 0x0\n"
"store r106\n"
"loadi 0x0\n"
"loadh2i 0xFF\n"
"store r107\n"
"loadi 0xFF\n"
"loadh3i 0x7F\n"
"store r108\n");
// Runtime function pointers
asm("loadi __ashlsi3\n"
"loadhi __ashlsi3\n"
"loadh2i __ashlsi3\n"
"loadh3i __ashlsi3\n"
"store r120\n"
"loadi __ashrsi3\n"
"loadhi __ashrsi3\n"
"loadh2i __ashrsi3\n"
"loadh3i __ashrsi3\n"
"store r121\n"
"loadi __lshrsi3\n"
"loadhi __lshrsi3\n"
"loadh2i __lshrsi3\n"
"loadh3i __lshrsi3\n"
"store r122\n");
// At some point we should initialize the C library here
// Kernel or host should have passed argc in r4 and argv in r5
// Call main
asm("loadi main\n"
"loadhi main\n"
"loadh2i main\n"
"loadh3i main\n"
"jal r0\n");
// Todo: Implement C's exit() stdlib function
// Call exit()
// asm("load exit\n"
// "loadh exit\n"
// "loadh2 exit\n"
// "loadh3 exit\n"
// "jal r0\n");
// Signal simulator to
asm("scall 0");
}