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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
#import "Basic";
#import "POSIX";
// This prototype is suposed to allow me to use the "open" posix syscall
// that returns a file-descriptor (fd) that can then be used by the mmap
// syscall that allows to map a file to memory.
//
// Syscall info: https://filippo.io/linux-syscall-table/
main :: () {
//sys_exit(-1);
size_stat, error_stat := sys_stat("./dummy");
print("> dymmy has stat size % widh error %\n", size_stat.st_size, error_stat);
fd, error_open := sys_open("./dummy", O_RDONLY, O_RDONLY);
print("> opened dummy with fd % and errno %\n", fd, error_open);
if fd < 0 return;
//LOOPER :: "\\|/-";
//idx := 0;
//loop: string;
//loop.count = 1;
//for 0..3 {
//sleep_milliseconds(1000);
//loop.data = *LOOPER.data[idx];
//idx = (idx + 1) % LOOPER.count;
//print("\rlooping %", loop);
//}
size_lseek, error_lseek := sys_lseek(fd, 0, SEEK_END);
print("\r> dummy has lseek size % with error %\n", size_lseek, error_lseek);
size_fstat, error_fstat := sys_fstat(fd);
print("\r> dummy has fstat size % with error %\n", size_fstat.st_size, error_fstat);
error_close := sys_close(fd);
print("\r> close(%) with errno %\n", fd, error_close);
print("---\n");
error :s64;
fd, error = sys_open("./fail", O_RDONLY, O_RDONLY);
print("$ open(fail) returned fd % and error %\n", fd, error);
error = sys_close(456);
print("$ close(456) returned error %\n", error);
}
sys_stat :: (path: string) -> result: stat_t, error: s64{
print("\r# sys_stat\n");
#if OS == .LINUX {
SYS_STAT :: 4;
stats: stat_t = ---;
result: s64 = ---;
#if CPU == .X64 {
print("\r# LINUX:ASM\n");
path_p: *s8 = xx path.data;
stats_p := *stats;
#asm SYSCALL_SYSRET {
mov rax: gpr === a, SYS_STAT;
mov rdi: gpr === di, path_p;
mov rsi: gpr === si, stats_p;
syscall
rcx: gpr === c, // Implicitly used by syscall to store RIP
r11: gpr === 11, // Implicitly used by syscall to store RFLAGS
rax, rdi, rsi;
mov result, rax;
}
if result < 0
return .{}, -result; // Error.
else
return stats, 0; // OK.
} else {
print("\r# LINUX:LIBC\n");
path_p: *s8 = xx path.data;
error := syscall(SYS_STAT, path_p, *stats);
if error < 0
return .{}, errno(); // Error.
else
return stats, 0; // OK.
}
} else {
print("TODO Implement this\n"); // TODO Implement this.
}
}
sys_fstat :: (file_descriptor: s64) -> result: stat_t, error: s64{
print("\r# sys_fstat\n");
#if OS == .LINUX {
SYS_FSTAT :: 5;
stats: stat_t = ---;
stats_p := *stats;
result: s64 = ---;
#if CPU == .X64 {
print("\r# LINUX:ASM\n");
#asm SYSCALL_SYSRET {
mov rax: gpr === a, SYS_FSTAT;
mov rdi: gpr === di, file_descriptor;
mov rsi: gpr === si, stats_p;
syscall
rcx: gpr === c, // Implicitly used by syscall to store RIP
r11: gpr === 11, // Implicitly used by syscall to store RFLAGS
rax, rdi, rsi;
mov result, rax;
}
if result < 0
return .{}, -result; // Error.
else
return stats, 0; // OK.
} else {
print("\r# LINUX:LIBC\n");
error := syscall(SYS_FSTAT, file_descriptor, *result);
if error < 0
return .{}, errno(); // Error.
else
return stats, 0; // OK.
}
} else {
print("TODO Implement this\n"); // TODO Implement this.
}
}
sys_lseek :: (file_descriptor: s64, offset: s64, whence: s64) -> offset_location: s64, error: s64 {
print("\r# sys_lseek\n");
#if OS == .LINUX {
SYS_LSEEK :: 8;
result: s64 = ---;
#if CPU == .X64 {
print("\r# LINUX:ASM\n");
#asm SYSCALL_SYSRET {
mov rax: gpr === a, SYS_LSEEK;
mov rdi: gpr === di, file_descriptor;
mov rsi: gpr === si, offset;
mov rdx: gpr === d, whence;
//syscall rcx, r11, rax, rdi, rsi, rdx; // TODO Cleanup?
syscall
rcx: gpr === c, // Implicitly used by syscall to store RIP
r11: gpr === 11, // Implicitly used by syscall to store RFLAGS
rax, rdi, rsi, rdx;
mov result, rax;
}
if result < 0
return -1, -result; // Error.
else
return result, 0; // OK.
} else {
print("\r# LINUX:LIBC\n");
result = syscall(SYS_LSEEK, file_descriptor, offset, whence);
if result < 0
return -1, errno(); // Error.
else
return result, 0; // OK.
}
} else {
print("TODO Implement this\n"); // TODO Implement this.
}
}
sys_open :: (path: string, flags: s64, mode: s64) -> file_descriptor: s64, error: s64 {
print("\r# sys_open\n");
#if OS == .LINUX {
SYS_OPEN :: 2;
data: *s8 = xx path.data;
result: s64 = ---;
#if CPU == .X64 {
print("\r# LINUX:ASM\n");
#asm SYSCALL_SYSRET {
mov rax: gpr === a, SYS_OPEN;
mov rdi: gpr === di, data;
mov rsi: gpr === si, flags;
mov rdx: gpr === d, mode; // TODO Is this really used?
//syscall rcx, r11, rax, rdi, rsi, rdx; // TODO Cleanup?
syscall
rcx: gpr === c, // Implicitly used by syscall to store RIP
r11: gpr === 11, // Implicitly used by syscall to store RFLAGS
rax, rdi, rsi, rdx;
mov result, rax;
}
if result < 0
return -1, -result; // Error.
else
return result, 0; // OK.
} else {
print("\r# LINUX:LIBC\n");
result = syscall(SYS_OPEN, data, flags, mode);
if result < 0
return -1, errno(); // Error.
else
return result, 0; // OK.
}
} else {
print("TODO Implement this\n"); // TODO Implement this.
}
}
sys_close :: (file_descriptor: s64) -> error: s64 {
print("\r# sys_close\n");
#if OS == .LINUX {
SYS_CLOSE :: 3;
result: s64 = ---;
#if CPU == .X64 {
print("\r# LINUX:ASM\n");
#asm SYSCALL_SYSRET {
mov rax: gpr === a, SYS_CLOSE;
mov rdi: gpr === di, file_descriptor;
syscall
rcx: gpr === c, // Implicitly used by syscall to store RIP
r11: gpr === 11, // Implicitly used by syscall to store RFLAGS
rax, rdi;
mov result, rax;
}
return ifx result < 0 then -result else 0;
} else {
print("\r# LINUX:LIBC\n");
result = syscall(SYS_CLOSE, file_descriptor);
return ifx result < 0 then errno() else 0;
}
} else {
print("TODO Implement this\n"); // TODO Implement this.
}
}
|