-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
197 lines (159 loc) · 6.85 KB
/
build.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
const std = @import("std");
const Builder = std.build.Builder;
const sabaton = @import("extern/Sabaton/build.zig");
fn baremetal_target(exec: *std.build.LibExeObjStep, arch: std.Target.Cpu.Arch) void {
var disabled_features = std.Target.Cpu.Feature.Set.empty;
var enabled_feautres = std.Target.Cpu.Feature.Set.empty;
switch (arch) {
.x86_64 => {
const features = std.Target.x86.Feature;
disabled_features.addFeature(@enumToInt(features.mmx));
disabled_features.addFeature(@enumToInt(features.sse));
disabled_features.addFeature(@enumToInt(features.sse2));
disabled_features.addFeature(@enumToInt(features.avx));
disabled_features.addFeature(@enumToInt(features.avx2));
enabled_feautres.addFeature(@enumToInt(features.soft_float));
exec.code_model = .kernel;
},
.aarch64 => {
const features = std.Target.aarch64.Feature;
disabled_features.addFeature(@enumToInt(features.fp_armv8));
disabled_features.addFeature(@enumToInt(features.crypto));
disabled_features.addFeature(@enumToInt(features.neon));
exec.code_model = .small;
},
else => unreachable,
}
exec.disable_stack_probing = true;
exec.setTarget(.{
.cpu_arch = arch,
.os_tag = std.Target.Os.Tag.freestanding,
.abi = std.Target.Abi.none,
.cpu_features_sub = disabled_features,
.cpu_features_add = enabled_feautres,
});
}
fn stivale2_kernel(b: *Builder, arch: std.Target.Cpu.Arch) *std.build.LibExeObjStep {
const kernel_filename = b.fmt("kernel_{s}.elf", .{@tagName(arch)});
const kernel = b.addExecutable(kernel_filename, "kernel/stivale2.zig");
kernel.addIncludeDir("extern/stivale/");
kernel.setMainPkgPath(".");
kernel.setOutputDir(b.cache_root);
kernel.setBuildMode(.ReleaseSafe);
kernel.install();
baremetal_target(kernel, arch);
kernel.setLinkerScriptPath(.{ .path = "kernel/linker.ld" });
b.default_step.dependOn(&kernel.step);
return kernel;
}
fn run_qemu_with_x86_bios_image(b: *Builder, image_path: []const u8) *std.build.RunStep {
const cmd = &[_][]const u8{
// zig fmt: off
"qemu-system-x86_64",
"-cdrom", image_path,
"-debugcon", "stdio",
"-vga", "virtio",
"-m", "4G",
"-machine", "q35,accel=kvm:whpx:tcg",
// zig fmt: on
};
const run_step = b.addSystemCommand(cmd);
const run_command = b.step("run-x86_64-bios", "Run on x86_64 with Limine BIOS bootloader");
run_command.dependOn(&run_step.step);
return run_step;
}
fn get_ovmf(_: *Builder) ![]const u8 {
if (std.os.getenv("OVMF_PATH")) |p|
return p;
return "OVMF path not found - please set envvar OVMF_PATH";
}
fn run_qemu_with_x86_uefi_image(b: *Builder, image_path: []const u8) *std.build.RunStep {
const cmd = &[_][]const u8{
// zig fmt: off
"qemu-system-x86_64",
"-cdrom", image_path,
"-debugcon", "stdio",
"-vga", "virtio",
"-m", "4G",
"-machine", "q35,accel=kvm:whpx:tcg",
"-drive", b.fmt("if=pflash,format=raw,unit=0,file={s},readonly=on", .{get_ovmf(b)}),
// zig fmt: on
};
const run_step = b.addSystemCommand(cmd);
const run_command = b.step("run-x86_64-uefi", "Run on x86_64 with Limine UEFI bootloader");
run_command.dependOn(&run_step.step);
return run_step;
}
fn run_qemu_with_sabaton(b: *Builder, kernel: *std.build.LibExeObjStep) *std.build.RunStep {
const bootloader_blob = sabaton.aarch64VirtBlob(b);
const bootloader_path = b.getInstallPath(bootloader_blob.dest_dir, bootloader_blob.dest_filename);
const kernel_path = b.getInstallPath(kernel.install_step.?.dest_dir, kernel.out_filename);
const cmd = &[_][]const u8{
// zig fmt: off
"qemu-system-aarch64",
"-M", "virt,accel=kvm:whpx:tcg,gic-version=3",
"-cpu", "cortex-a57",
"-drive", b.fmt("if=pflash,format=raw,file={s},readonly=on", .{bootloader_path}),
"-fw_cfg", b.fmt("opt/Sabaton/kernel,file={s}", .{kernel_path}),
"-m", "4G",
"-serial", "stdio",
"-smp", "4",
"-device", "ramfb",
// zig fmt: on
};
const run_step = b.addSystemCommand(cmd);
run_step.step.dependOn(&kernel.install_step.?.step);
run_step.step.dependOn(&bootloader_blob.step);
const run_command = b.step("run-aarch64", "Run on aarch64 with Sabaton bootloader");
run_command.dependOn(&run_step.step);
return run_step;
}
fn build_limine_image(b: *Builder, kernel: *std.build.LibExeObjStep, image_path: []const u8) *std.build.RunStep {
const img_dir = b.fmt("{s}/img_dir", .{b.cache_root});
const kernel_path = b.getInstallPath(kernel.install_step.?.dest_dir, kernel.out_filename);
const cmd = &[_][]const u8{
"/bin/sh", "-c",
std.mem.concat(b.allocator, u8, &[_][]const u8{
// zig fmt: off
"rm ", image_path, " || true && ",
"mkdir -p ", img_dir, "/EFI/BOOT && ",
"make -C extern/limine-bin limine-install && ",
"cp ",
"extern/limine-bin/limine-eltorito-efi.bin ",
"extern/limine-bin/limine-cd.bin ",
"extern/limine-bin/limine.sys ",
"limine.cfg ",
img_dir, " && ",
"cp extern/limine-bin/BOOTX64.EFI ", img_dir, "/EFI/BOOT/ && ",
"cp ", kernel_path, " ", img_dir, " && ",
"xorriso -as mkisofs -quiet -b limine-cd.bin ",
"-no-emul-boot -boot-load-size 4 -boot-info-table ",
"--efi-boot limine-eltorito-efi.bin ",
"-efi-boot-part --efi-boot-image --protective-msdos-label ",
img_dir, " -o ", image_path, " && ",
"extern/limine-bin/limine-install ", image_path, " && ",
"true",
// zig fmt: on
}) catch unreachable,
};
const image_step = b.addSystemCommand(cmd);
image_step.step.dependOn(&kernel.install_step.?.step);
b.default_step.dependOn(&image_step.step);
const image_command = b.step("x86_64-universal-image", "Build the x86_64 universal (bios and uefi) image");
image_command.dependOn(&image_step.step);
return image_step;
}
fn build_x86(b: *Builder) void {
const kernel = stivale2_kernel(b, .x86_64);
const image_path = b.fmt("{s}/universal.iso", .{b.cache_root});
const image = build_limine_image(b, kernel, image_path);
const uefi_step = run_qemu_with_x86_uefi_image(b, image_path);
uefi_step.step.dependOn(&image.step);
const bios_step = run_qemu_with_x86_bios_image(b, image_path);
bios_step.step.dependOn(&image.step);
}
pub fn build(b: *Builder) void {
build_x86(b);
// Just boots your kernel using sabaton, without a filesystem.
_ = run_qemu_with_sabaton(b, stivale2_kernel(b, .aarch64));
}