Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building virtio example with zig #65

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
15256d6
Zig build for virtio example
erichchan999 Jul 5, 2024
423f1a6
Rename uio_blk_driver -> uio_driver_blk
erichchan999 Jul 5, 2024
c7e91b0
Add set -e to scripts
erichchan999 Jul 5, 2024
11057f7
Pipe tools stderr to /dev/null to silence zig build errors
erichchan999 Jul 5, 2024
cda5ce5
Remove problematic stdout -l print in fdisk
erichchan999 Jul 5, 2024
cd5ea46
ci: add Zig build for virtio example
Ivan-Velickovic Jul 8, 2024
316ce3b
examples/virtio: rename in build.zig.zon
Ivan-Velickovic Jul 8, 2024
d176e6c
Remove trailing whitespace
Ivan-Velickovic Jul 8, 2024
e1da8ea
Refactor disables from the base linux.dts into disable.dts in qemu, a…
erichchan999 Jul 8, 2024
b7ae7d6
Update virtio example disable.dts changes to zig build
erichchan999 Jul 8, 2024
d72261c
Various fixes
erichchan999 Jul 8, 2024
16742a8
test commit to see if CI passes now
Ivan-Velickovic Jul 8, 2024
b973f13
ci: fix, oopsies
Ivan-Velickovic Jul 8, 2024
b921fbf
build.zig: cleanup
Ivan-Velickovic Jul 8, 2024
f9e3c3a
More cleanup
Ivan-Velickovic Jul 8, 2024
4a02493
Cleanup
Ivan-Velickovic Jul 8, 2024
01fd5ea
shell.nix: fixes/additions
Ivan-Velickovic Aug 7, 2024
689718c
Delete file from rebase
Ivan-Velickovic Aug 7, 2024
9112aba
build.zig: fix for rebase
Ivan-Velickovic Aug 7, 2024
fc775ae
ci: build virtIO example for QEMU
Ivan-Velickovic Aug 7, 2024
ef48554
Remove file after rebase
Ivan-Velickovic Aug 7, 2024
44f75b6
License fixes
Ivan-Velickovic Aug 7, 2024
3ce5ef5
Revert rename of UIO block driver
Ivan-Velickovic Aug 7, 2024
4db1568
Remove TODO
Ivan-Velickovic Aug 7, 2024
10d814d
cleanup
Ivan-Velickovic Aug 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Files:
build.zig.zon
examples/simple/build.zig.zon
examples/zig/build.zig.zon
examples/virtio/build.zig.zon
examples/rust/Cargo.lock
examples/rust/.cargo/config
examples/rust/aarch64-microkit-minimal.json
Expand Down
163 changes: 149 additions & 14 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
// SPDX-License-Identifier: BSD-2-Clause

const std = @import("std");
const LazyPath = std.Build.LazyPath;

const src = [_][]const u8{
"src/guest.c",
"src/util/util.c",
"src/util/printf.c",
"src/virtio/block.c",
"src/virtio/console.c",
"src/virtio/mmio.c",
"src/virtio/sound.c",
};

const src_aarch64_vgic_v2 = [_][]const u8{
Expand All @@ -28,17 +33,111 @@ const src_aarch64 = [_][]const u8{
"src/arch/aarch64/vcpu.c",
};

pub fn dtscat(
b: *std.Build,
base: LazyPath,
overlays: []const LazyPath
) std.meta.Tuple(&.{*std.Build.Step.Run, LazyPath}) {
const dtscat_tool = b.dependencyFromBuildZig(@This(), .{}).path("tools/dtscat");

const cmd = std.Build.Step.Run.create(b, "run dtscat");
cmd.addFileArg(dtscat_tool);
cmd.addFileArg(base);
for (overlays) |overlay| {
cmd.addFileArg(overlay);
}
const output = cmd.captureStdOut();
return .{cmd, output};
}

pub fn compileDts(
b: *std.Build,
dts: LazyPath
) std.meta.Tuple(&.{*std.Build.Step.Run, LazyPath}) {
const cmd = b.addSystemCommand(&[_][]const u8{
"dtc", "-q", "-I", "dts", "-O", "dtb"
});
cmd.addFileArg(dts);
const output = cmd.captureStdOut();
return .{cmd, output};
}

pub fn packrootfs(
b: *std.Build,
rootfs_base: LazyPath,
rootfs_tmp: []const u8,
rootfs_name: []const u8,
home_files: []const LazyPath,
startup_files: []const LazyPath,
) std.meta.Tuple(&.{*std.Build.Step.Run, LazyPath})
{
const packrootfs_tool = b.dependencyFromBuildZig(@This(), .{}).path("tools/packrootfs");
// TODO: Investigate why WF is not working
// const rootfs_wf = b.addWriteFiles();
// const output = rootfs_wf.getDirectory().path(b, rootfs_name);
// const tmp = rootfs_wf.getDirectory().path(b, "tmp");
const cmd = std.Build.Step.Run.create(b, "run packrootfs");
cmd.addFileArg(packrootfs_tool);
cmd.addFileArg(rootfs_base);
cmd.addArg(rootfs_tmp);
cmd.addArg("-o");
const output = cmd.addOutputFileArg(rootfs_name);
cmd.addArg("--home");
for (home_files) |file| {
cmd.addFileArg(file);
}
cmd.addArg("--startup");
for (startup_files) |file| {
cmd.addFileArg(file);
}
return .{cmd, output};
}

pub fn mkvirtdisk(
b: *std.Build,
num_part: usize,
logical_size: usize,
blk_mem: usize,
) std.meta.Tuple(&.{*std.Build.Step.Run, LazyPath}) {
const this_dep = b.dependencyFromBuildZig(@This(), .{});
const mkvirtdisk_tool = this_dep.path("tools/mkvirtdisk");

const num_part_str = b.fmt("{d}", .{ num_part });
const logical_size_str = b.fmt("{d}", .{ logical_size });
const blk_mem_str = b.fmt("{d}", .{ blk_mem });
const cmd = std.Build.Step.Run.create(b, "run mkvirtdisk");
cmd.addFileArg(mkvirtdisk_tool);
// TODO: Investigate another way to do this, don't use /tmp?
const virtdisk = cmd.addOutputFileArg("/tmp/virtdisk");
cmd.addArg(num_part_str);
cmd.addArg(logical_size_str);
cmd.addArg(blk_mem_str);
return .{cmd, virtdisk};
}

pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});

const libmicrokit_include_opt = b.option([]const u8, "libmicrokit_include", "Path to the libmicrokit include directory") orelse null;
const microkit_board_opt = b.option([]const u8, "microkit_board", "Name of Microkit board") orelse null;

const libmicrokit_opt = b.option([]const u8, "libmicrokit", "Path to libmicrokit.a") orelse "";
const libmicrokit_include_opt = b.option([]const u8, "libmicrokit_include", "Path to the libmicrokit include directory") orelse "";
const libmicrokit_linker_script_opt = b.option([]const u8, "libmicrokit_linker_script", "Path to the libmicrokit linker script") orelse "";
const microkit_board_opt = b.option([]const u8, "microkit_board", "Name of Microkit board") orelse "";
// Default to vGIC version 2
const arm_vgic_version = b.option(usize, "arm_vgic_version", "ARM vGIC version to emulate") orelse null;
const arm_vgic_version = b.option(usize, "arm_vgic_version", "ARM vGIC version to emulate") orelse 2;
const blk_config_include_opt = b.option([]const u8, "blk_config_include", "Include path to block config header") orelse "";

const libmicrokit_include = libmicrokit_include_opt.?;
const libmicrokit_include = libmicrokit_include_opt;
const blk_config_include = std.Build.LazyPath{ .cwd_relative = blk_config_include_opt };

const sddf_dep = b.dependency("sddf", .{
.target = target,
.optimize = optimize,
.libmicrokit = @as([]const u8, libmicrokit_opt),
.libmicrokit_include = @as([]const u8, libmicrokit_include),
.libmicrokit_linker_script = @as([]const u8, libmicrokit_linker_script_opt),
.blk_config_include = @as([]const u8, blk_config_include_opt),
});

const libvmm = b.addStaticLibrary(.{
.name = "vmm",
Expand All @@ -48,35 +147,71 @@ pub fn build(b: *std.Build) void {

const src_arch = switch (target.result.cpu.arch) {
.aarch64 => blk: {
const vgic_src = switch (arm_vgic_version.?) {
const vgic_src = switch (arm_vgic_version) {
2 => src_aarch64_vgic_v2,
3 => src_aarch64_vgic_v3,
else => @panic("Unsupported vGIC version given"),
};

break :blk src_aarch64 ++ vgic_src;
break :blk &(src_aarch64 ++ vgic_src);
},
else => {
std.log.err("Unsupported libvmm architecture given '{s}'", .{ @tagName(target.result.cpu.arch) });
std.posix.exit(1);
@panic(b.fmt("Unsupported libvmm architecture given '{s}'", .{ @tagName(target.result.cpu.arch) }));
}
};
libvmm.addCSourceFiles(.{
.files = &(src ++ src_arch),
.flags = &.{

const libvmm_flags = &.{
"-Wall",
"-Werror",
"-Wno-unused-function",
"-mstrict-align",
"-fno-sanitize=undefined", // @ivanv: ideally we wouldn't have to turn off UBSAN
b.fmt("-DBOARD_{s}", .{ microkit_board_opt.? }) // @ivanv: shouldn't be needed as the library should not depend on the board
}
b.fmt("-DBOARD_{s}", .{ microkit_board_opt }) // @ivanv: shouldn't be needed as the library should not depend on the board
};
libvmm.addCSourceFiles(.{
.files = &src,
.flags = libvmm_flags,
});
libvmm.addCSourceFiles(.{
.files = src_arch,
.flags = libvmm_flags,
});

libvmm.addIncludePath(b.path("include"));
libvmm.addIncludePath(sddf_dep.path("include"));
libvmm.addIncludePath(.{ .cwd_relative = libmicrokit_include });
libvmm.linkLibrary(sddf_dep.artifact("util"));
libvmm.linkLibrary(sddf_dep.artifact("util_putchar_debug"));

libvmm.installHeadersDirectory(b.path("include/libvmm"), "libvmm", .{});

b.installArtifact(libvmm);

var target_userlevel_query = target.query;
target_userlevel_query.os_tag = .linux;
target_userlevel_query.abi = .musl;
const target_userlevel = b.resolveTargetQuery(target_userlevel_query);
Ivan-Velickovic marked this conversation as resolved.
Show resolved Hide resolved

const uio_driver_blk = b.addExecutable(.{
.name = "uio_blk_driver",
.target = target_userlevel,
.optimize = optimize,
.strip = false,
});
uio_driver_blk.addCSourceFiles(.{
.files = &.{
"tools/linux/uio_drivers/blk/blk.c",
"tools/linux/uio/libuio.c",
},
.flags = &.{
"-Wall",
"-Werror",
"-Wno-unused-function",
}
});
uio_driver_blk.linkLibC();
uio_driver_blk.addIncludePath(b.path("tools/linux/include"));
uio_driver_blk.addIncludePath(blk_config_include);
uio_driver_blk.addIncludePath(sddf_dep.path("include"));
b.installArtifact(uio_driver_blk);
}
6 changes: 6 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
.name = "libvmm",
.version = "0.1.0",

.dependencies = .{
.sddf = .{
.path = "dep/sddf"
}
},

.paths = .{
"LICENSE",
"README.md",
Expand Down
31 changes: 26 additions & 5 deletions ci/examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ simulate_simple_make() {
./ci/buildroot_login.exp ${BUILD_DIR}/loader.img
}

build_virtio() {
build_virtio_make() {
BOARD=$1
CONFIG=$2
echo "CI|INFO: building virtio example via Make with board: $BOARD and config: $CONFIG"
Expand All @@ -101,6 +101,22 @@ build_virtio() {
MICROKIT_SDK=${SDK_PATH}
}

build_virtio_zig() {
BOARD=$1
CONFIG=$2
echo "CI|INFO: building virtio example via Zig with board: $BOARD and config: $CONFIG"
BUILD_DIR="${PWD}/build/examples/virtio/zig/${BOARD}/${CONFIG}"
EXAMPLE_DIR="${PWD}/examples/virtio"
mkdir -p ${BUILD_DIR}
pushd ${EXAMPLE_DIR}
zig build \
-Dsdk=${SDK_PATH} \
-Dboard=${BOARD} \
-Dconfig=${CONFIG} \
-p ${BUILD_DIR}
popd
}

simulate_zig() {
echo "CI|INFO: simulating Zig example with config: $1"
BUILD_DIR="${PWD}/build/examples/zig/qemu_virt_aarch64/${CONFIG}/${ZIG_OPTIMIZE}"
Expand Down Expand Up @@ -155,10 +171,15 @@ simulate_zig "release" "ReleaseSmall"
# Linux-specific utilities not being available.
# if [ "$(uname)" == "Linux" ]; then

# build_virtio "qemu_virt_aarch64" "debug"
# build_virtio "qemu_virt_aarch64" "release"
# build_virtio "odroidc4" "debug"
# build_virtio "odroidc4" "release"
build_virtio_make "qemu_virt_aarch64" "debug"
build_virtio_make "qemu_virt_aarch64" "release"
# build_virtio_make "odroidc4" "debug"
# build_virtio_make "odroidc4" "release"

build_virtio_zig "qemu_virt_aarch64" "debug"
build_virtio_zig "qemu_virt_aarch64" "release"
# build_virtio_zig "odroidc4" "debug"
# build_virtio_zig "odroidc4" "release"

# fi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
interrupts = <0x00 0x01 0x04>;
reg = <0x00 0x9000000 0x00 0x1000>;
compatible = "arm,pl011\0arm,primecell";
status = "disabled";
};

pmu {
Expand All @@ -62,7 +61,6 @@
bank-width = <0x04>;
reg = <0x00 0x00 0x00 0x4000000 0x00 0x4000000 0x00 0x4000000>;
compatible = "cfi-flash";
status = "disabled";
};

cpus {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024, UNSW
*
* SPDX-License-Identifier: BSD-2-Clause
*/
/ {
/delete-node/ pl011@9000000;
/delete-node/ flash@0;

pl011@9000000 {
clock-names = "uartclk\0apb_pclk";
clocks = <0x8000 0x8000>;
interrupts = <0x00 0x01 0x04>;
reg = <0x00 0x9000000 0x00 0x1000>;
compatible = "arm,pl011\0arm,primecell";
status = "disabled";
};

flash@0 {
bank-width = <0x04>;
reg = <0x00 0x00 0x00 0x4000000 0x00 0x4000000 0x00 0x4000000>;
compatible = "cfi-flash";
status = "disabled";
};
};
Loading
Loading