/
entry.d
123 lines (91 loc) · 1.93 KB
/
entry.d
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* entry.d
*
* The entry point to an app.
*
* License: Public Domain
*
*/
import user.syscall;
import libos.libdeepmajik.threadscheduler;
import libos.libdeepmajik.umm;
import user.environment;
import libos.console;
import libos.keyboard;
// Will be linked to the user's main function
int main(char[][]);
extern(C) ubyte _edata;
extern(C) ubyte _end;
ubyte* startBSS = &_edata;
ubyte* endBSS = &_end;
// Upcall Vector Table
void function()[2] UVT = [&start, &_enterThreadScheduler];
extern(C) ubyte* UVTbase = cast(ubyte*)UVT.ptr;
ubyte[1024] tempStack;
ubyte* tempStackTop = &tempStack[tempStack.length - 8];
/*extern(C) void _start(int thing) {
asm{
naked;
//stackless equivalent of "UVT[thing]();"
movq RSI, UVTbase;
sal RDI, 3;
addq RSI, RDI;
jmp [RSI];
}
}*/
void start(){
// Zero the BSS, equivalent to start2()
asm {
naked;
// zero rbp
xor RBP, RBP;
// load the addresses of the beginning and end of the BSS
mov RDX, startBSS;
//mov RDX, [RDX];
mov RCX, endBSS;
//mov RCX, [RCX];
// zero, one byte at a time
loop:
movb [RDX], 0;
inc RDX;
cmp RCX, RDX;
jne loop;
// now set the stack
movq RSP, tempStackTop;
call start3;
}
}
void start2(){
ubyte* startBSS = &_edata;
ubyte* endBSS = &_end;
for( ; startBSS != endBSS; startBSS++) {
*startBSS = 0x00;
}
start3();
}
void start3(){
//UsermodeMemoryManager.
char[][] argv = MessageInAbottle.getMyBottle().argv;
ulong argvlen = cast(ulong)argv.length;
ulong argvptr = cast(ulong)argv.ptr;
// __ENV ?
MessageInAbottle* bottle = MessageInAbottle.getMyBottle();
if(bottle.stdoutIsTTY){
Console.initialize(bottle.stdout.ptr);
}else{
//assert(false);
}
if(bottle.stdinIsTTY){
Keyboard.initialize(cast(ushort*)bottle.stdin.ptr);
}else{
//assert(false);
}
init();
XombThread* mainThread = threadCreate(&main);
mainThread.schedule();
asm{
mov RDI, argvlen;
mov RSI, argvptr;
}
_enterThreadScheduler();
}