-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.zig
308 lines (281 loc) · 10.7 KB
/
main.zig
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const builtin = @import("builtin");
const Bitmap = @import("video_core_frame_buffer.zig").Bitmap;
const Color = @import("video_core_frame_buffer.zig").Color;
const debug = @import("debug.zig");
const FrameBuffer = @import("video_core_frame_buffer.zig").FrameBuffer;
const Metrics = @import("video_core_metrics.zig").Metrics;
const serial = @import("serial.zig");
const time = @import("time.zig");
const std = @import("std");
// The linker will make the address of these global variables equal
// to the value we are interested in. The memory at the address
// could alias any uninitialized global variable in the kernel.
extern var __bss_start: u8;
extern var __bss_end: u8;
extern var __end_init: u8;
comptime {
// .text.boot to keep this in the first portion of the binary
// Note: this code cannot be changed via the bootloader.
asm (
\\.section .text.boot
\\.globl _start
\\_start:
\\ mrs x0,mpidr_el1
\\ mov x1,#0xC1000000
\\ bic x0,x0,x1
\\ cbz x0,master
\\ b hang
\\master:
\\ mov sp,#0x08000000
\\ mov x0,#0x800 //exception_vector_table
\\ msr vbar_el3,x0
\\ msr vbar_el2,x0
\\ msr vbar_el1,x0
\\ bl kernelMainAt0x1100
\\hang:
\\ wfe
\\ b hang
\\exception_vector_table:
\\.balign 0x800
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
\\.balign 0x80
\\ b shortExceptionHandlerAt0x1000
);
}
export fn shortExceptionHandlerAt0x1000() linksection(".text.exception") void {
exceptionHandler();
}
pub fn panic(message: []const u8, trace: ?*builtin.StackTrace) noreturn {
debug.panic(trace, "KERNEL PANIC: {}", .{message});
}
fn exceptionHandler() void {
serial.log("arm exception taken", .{});
var current_el = asm ("mrs %[current_el], CurrentEL"
: [current_el] "=r" (-> usize)
);
serial.log("CurrentEL {x} exception level {}", .{ current_el, current_el >> 2 & 0x3 });
var esr_el3 = asm ("mrs %[esr_el3], esr_el3"
: [esr_el3] "=r" (-> usize)
);
serial.log("esr_el3 {x} code 0x{x}", .{ esr_el3, esr_el3 >> 26 & 0x3f });
var elr_el3 = asm ("mrs %[elr_el3], elr_el3"
: [elr_el3] "=r" (-> usize)
);
serial.log("elr_el3 {x}", .{elr_el3});
var spsr_el3 = asm ("mrs %[spsr_el3], spsr_el3"
: [spsr_el3] "=r" (-> usize)
);
serial.log("spsr_el3 {x}", .{spsr_el3});
var far_el3 = asm ("mrs %[far_el3], far_el3"
: [far_el3] "=r" (-> usize)
);
serial.log("far_el3 {x}", .{far_el3});
serial.log("execution is now stopped in arm exception handler", .{});
while (true) {
asm volatile ("wfe");
}
}
export fn kernelMainAt0x1100() linksection(".text.main") noreturn {
// clear .bss
@memset(@as(*volatile [1]u8, &__bss_start), 0, @ptrToInt(&__bss_end) - @ptrToInt(&__bss_start));
serial.init();
serial.log("\n{} {} ...", .{ name, version });
time.init();
metrics.init();
fb.init(&metrics);
icon.init(&fb, &logo_bmp_file);
logo.init(&fb, &logo_bmp_file);
screen_activity.init();
serial_activity.init();
while (true) {
screen_activity.update();
serial_activity.update();
}
}
const ScreenActivity = struct {
height: u32,
color: Color,
color32: u32,
top: u32,
x: i32,
y: i32,
vel_x: i32,
vel_y: i32,
ref_seconds: f32,
pixel_counter: u32,
fn init(self: *ScreenActivity) void {
self.color = color_yellow;
self.color32 = self.color.to32();
self.height = logo.height;
self.top = logo.height + margin;
self.x = 0;
self.y = 0;
self.vel_x = 10;
self.vel_y = 10;
time.update();
self.ref_seconds = time.seconds;
self.pixel_counter = 0;
}
fn update(self: *ScreenActivity) void {
time.update();
const new_ref_secs = self.ref_seconds + 0.05;
if (time.seconds >= new_ref_secs) {
const clear_x = @intCast(u32, self.x);
const clear_y = @intCast(u32, self.y);
fb.clearRect(clear_x, clear_y, logo.width, logo.height, color_black);
self.ref_seconds = new_ref_secs;
self.x += self.vel_x;
self.y += self.vel_y;
if (self.x + @as(i32, logo.width) >= @intCast(i32, fb.virtual_width)) {
self.x = @intCast(i32, fb.virtual_width - logo.width);
self.vel_x = -self.vel_x;
}
if (self.y + @as(i32, logo.height) >= @intCast(i32, fb.virtual_height)) {
self.y = @intCast(i32, fb.virtual_height - logo.height);
self.vel_y = -self.vel_y;
}
if (self.x < 0) {
self.x = 0;
self.vel_x = -self.vel_x;
}
if (self.y < 0) {
self.y = 0;
self.vel_y = -self.vel_y;
}
const draw_x = @intCast(u32, self.x);
const draw_y = @intCast(u32, self.y);
logo.drawRect(logo.width, logo.height, 0, 0, draw_x, draw_y);
}
}
};
const SerialActivity = struct {
boot_magic_index: usize,
fn init(self: *SerialActivity) void {
self.boot_magic_index = 0;
serial.log("now echoing input on uart1 ...", .{});
}
fn update(self: *SerialActivity) void {
if (!serial.isReadByteReady()) {
return;
}
const boot_magic = [_]u8{ 6, 6, 6 };
const byte = serial.readByte();
if (byte == boot_magic[self.boot_magic_index]) {
self.boot_magic_index += 1;
if (self.boot_magic_index != boot_magic.len)
return;
// It's time to receive the new kernel. First
// we skip over the .text.boot bytes, verifying that they
// are unchanged.
const new_kernel_len = serial.in.readIntLittle(u32) catch unreachable;
serial.log("New kernel image detected, {Bi:2}", .{new_kernel_len});
const text_boot = @intToPtr([*]allowzero const u8, 0)[0..@ptrToInt(&__end_init)];
for (text_boot) |text_boot_byte, byte_index| {
const new_byte = serial.readByte();
if (new_byte != text_boot_byte) {
debug.panic(@errorReturnTrace(), "new_kernel[{}] expected: 0x{x} actual: 0x{x}", .{
byte_index,
text_boot_byte,
new_byte,
});
}
}
const start_addr = @ptrToInt(shortExceptionHandlerAt0x1000);
const bytes_left = new_kernel_len - start_addr;
var pad = start_addr - text_boot.len;
while (pad > 0) : (pad -= 1) {
_ = serial.readByte();
}
// Next we copy the bootloader code to the correct memory address,
// and then jump to it.
// Read the ELF
var bootloader_code_ptr = @as([*]const u8, &bootloader_code); // TODO remove workaround `var`
const ehdr = @ptrCast(*const std.elf.Elf64_Ehdr, bootloader_code_ptr);
var phdr_addr = bootloader_code_ptr + ehdr.e_phoff;
var phdr_i: usize = 0;
while (phdr_i < ehdr.e_phnum) : ({
phdr_i += 1;
phdr_addr += ehdr.e_phentsize;
}) {
const this_ph = @ptrCast(*const std.elf.Elf64_Phdr, @alignCast(@alignOf(std.elf.Elf64_Phdr), phdr_addr));
switch (this_ph.p_type) {
std.elf.PT_LOAD => {
const src_ptr = bootloader_code_ptr + this_ph.p_offset;
const src_len = this_ph.p_filesz;
const dest_ptr = @intToPtr([*]u8, this_ph.p_vaddr);
const dest_len = this_ph.p_memsz;
const pad_len = dest_len - src_len;
const copy_len = dest_len - pad_len;
@memcpy(dest_ptr, src_ptr, copy_len);
@memset(dest_ptr + copy_len, 0, pad_len);
},
std.elf.PT_GNU_STACK => {}, // ignore
else => debug.panic(@errorReturnTrace(), "unexpected ELF Program Header load type: {}", .{this_ph.p_type}),
}
}
serial.log("Loading new image...", .{});
asm volatile (
\\mov sp,#0x08000000
\\bl bootloader_main
:
: [arg0] "{x0}" (start_addr),
[arg1] "{x1}" (bytes_left)
);
unreachable;
}
switch (byte) {
'\r' => {
serial.writeText("\n");
},
else => serial.writeByte(byte),
}
}
};
const build_options = @import("build_options");
const bootloader_code align(@alignOf(std.elf.Elf64_Ehdr)) = @embedFile("../" ++ build_options.bootloader_exe_path).*;
var screen_activity: ScreenActivity = undefined;
var serial_activity: SerialActivity = undefined;
var fb: FrameBuffer = undefined;
var metrics: Metrics = undefined;
var icon: Bitmap = undefined;
var logo: Bitmap = undefined;
var icon_bmp_file align(@alignOf(u32)) = @embedFile("../assets/zig-icon.bmp").*;
var logo_bmp_file align(@alignOf(u32)) = @embedFile("../assets/zig-logo.bmp").*;
const margin = 10;
const color_red = Color{ .red = 255, .green = 0, .blue = 0, .alpha = 255 };
const color_green = Color{ .red = 0, .green = 255, .blue = 0, .alpha = 255 };
const color_blue = Color{ .red = 0, .green = 0, .blue = 255, .alpha = 255 };
const color_yellow = Color{ .red = 255, .green = 255, .blue = 0, .alpha = 255 };
const color_white = Color{ .red = 255, .green = 255, .blue = 255, .alpha = 255 };
const color_black = Color{ .red = 0, .green = 0, .blue = 0, .alpha = 255 };
const name = "ClashOS";
const version = "0.2";