test/integration/kernel.rad 6.0 KiB raw
1
//! Kernel fixture images: the kernel built from explicitly mapped modules,
2
//! assembled with a fixture's machine code, and the binary inputs a fixture
3
//! embeds as byte arrays.
4
5
use std::fs;
6
use std::mem;
7
use std::process;
8
use std::process::Outcome;
9
use std::process::Output;
10
use std::process::Target;
11
use std::sys;
12
use super::harness;
13
use super::harness::Expect;
14
use super::harness::Harness;
15
16
/// Directory of the kernel modules and assembly.
17
constant KERNEL_DIR: *[u8] = "kernel/kernel";
18
/// Module value of the kernel ABI, for the fixture packages.
19
export constant ABI: *[u8] = "abi=kernel/kernel/abi.rad";
20
/// Module value of the system calls, for the fixture packages.
21
export constant SYS: *[u8] = "sys=kernel/kernel/sys.rad";
22
/// Module value of the memory-mapped registers, for fixtures with devices.
23
export constant MMIO: *[u8] = "mmio=kernel/mmio.rad";
24
/// Directory of the binary IL of the standard library and the kernel.
25
export constant RIL_DIR: *[u8] = "bin";
26
27
/// A binary file to embed and the name of the array that holds it.
28
export record Embedded: Copy {
29
    /// Name of the declared array.
30
    name: *[u8],
31
    /// Path of the binary file.
32
    file: *[u8],
33
}
34
35
/// Write `inputs.rad` in the work directory: one declaration
36
/// `export static NAME: [u8; LENGTH] = [ ... ];` per entry, in order.
37
fn embed(h: &mut Harness, entries: &[Embedded]) throws (sys::Error) {
38
    let mut out = try fs::create(h.path(&["inputs.rad"]));
39
    out.write("//! Binary inputs for the fixture.\n");
40
    for entry in entries {
41
        let data = try h.load(entry.file) catch err {
42
            try out.close() catch {};
43
            throw err;
44
        };
45
        out.write(
46
            h.join(
47
                &[
48
                    "\n/// Binary RIL bytes for ",
49
                    entry.name,
50
                    ".\nexport static ",
51
                    entry.name,
52
                    ": [u8; ",
53
                    h.num(data.len), "] = [\n",
54
                ]
55
            )
56
        );
57
        for byte, i in data {
58
            out.write(h.join(&[h.num(byte as u32), ","]));
59
            if (i + 1) % 16 == 0 or i + 1 == data.len {
60
                out.write("\n");
61
            }
62
        }
63
        out.write("];\n");
64
    }
65
    try out.close();
66
}
67
68
/// Link the packages into a machine image with the image tool, from the
69
/// kernel binary IL `std/std.ril.bin` and `kernel/kernel.ril.bin` in `ril`,
70
/// the assembly `assembly`, and the boot packages. The tool must end with
71
/// `want`, and its output in `<image>.log` must meet the expectations.
72
export fn link(
73
    h: &mut Harness,
74
    what: *[u8],
75
    ril: *[u8],
76
    assembly: *[u8],
77
    image: *[u8],
78
    boot: &[*[u8]],
79
    want: Outcome,
80
    expects: &[Expect]
81
) -> bool {
82
    h.add(&["-std-asm", "bin/std.ras"]);
83
    h.add(
84
        &[
85
            h.join(&[ril, "/std/std.ril.bin"]),
86
            h.join(&[ril, "/kernel/kernel.ril.bin"]),
87
            assembly,
88
            image,
89
        ]
90
    );
91
    h.add(boot);
92
    let output = Output::File(h.join(&[image, ".log"]));
93
    let tool = h.cmd(
94
        "bin/kernel.build.rv64", h.take(), Target::Inherit, harness::COMPILE_STEPS, output
95
    );
96
    return h.run(what, tool, want, expects);
97
}
98
99
/// Write to `output` the concatenation of `first`, if given, and the kernel
100
/// assembly files in ascending order of name.
101
export fn concatAssembly(h: &mut Harness, output: *[u8], first: ?*[u8])
102
    throws (sys::Error)
103
{
104
    let mut sources: *mut [*[u8]] = &mut [];
105
    if let path = first {
106
        try! sources.append(path, h.arena);
107
    }
108
    for path in try h.listFiles(KERNEL_DIR, ".ras") {
109
        try! sources.append(path, h.arena);
110
    }
111
    try h.concat(output, &sources[..]);
112
}
113
114
/// Compile the fixture package `name` from `module` and the `-mod` values
115
/// `mods` to binary IL in the work directory, as the entry of its own
116
/// compilation.
117
export fn package(h: &mut Harness, name: *[u8], module: *[u8], mods: &[*[u8]]) -> bool {
118
    let work = h.work;
119
    h.add(&["-pkg", name, "-mod", module]);
120
    for value in mods {
121
        h.add(&["-mod", value]);
122
    }
123
    h.add(&["-entry", name, "-ril", work]);
124
    return h.compile(h.join(&["compile ", name]), h.out(&[name, ".log"]));
125
}
126
127
/// Build the machine fixture `fixture` into `image`, working in the work
128
/// directory. The kernel is compiled with the fixture's modules mapped onto its
129
/// own by name, and with the byte arrays of the binary files `entries` as the
130
/// module `dispatchcheck/inputs`. `boot` lists binary IL packages to load at
131
/// boot. Returns whether the image was built.
132
export fn image(
133
    h: &mut Harness,
134
    fixture: *[u8],
135
    image: *[u8],
136
    entries: &[Embedded],
137
    boot: &[*[u8]]
138
) -> bool throws (sys::Error) {
139
    let dir = h.join(&["test/", fixture]);
140
    let work = h.work;
141
    try h.addStd(".rad");
142
    h.add(&["-pkg", "kernel", "-manifest", "seed/dependencies/std", "-mod", "test/kernel.rad"]);
143
    for path in try h.listFiles(KERNEL_DIR, ".rad") {
144
        let name = &path[KERNEL_DIR.len + 1 .. path.len - ".rad".len];
145
        // The fixture replaces the kernel's root module and its tests.
146
        if not mem::eq(name, "tests") and not mem::eq(name, "kernel") {
147
            h.named(name, path);
148
        }
149
    }
150
    h.named("fixture", "test/kernel/fixture.rad");
151
    h.named("dispatchcheck", h.join(&[dir, "/kernel/dispatchcheck.rad"]));
152
    let cases = h.join(&[dir, "/cases.rad"]);
153
    if try harness::exists(cases) {
154
        h.named("dispatchcheck/cases", cases);
155
    }
156
    if entries.len > 0 {
157
        try embed(h, entries);
158
        h.named("dispatchcheck/inputs", h.path(&["inputs.rad"]));
159
    }
160
    if mem::eq(fixture, "smp") or mem::eq(fixture, "cycles") {
161
        h.named("dispatchcheck/invariants", "test/smp/kernel/invariants.rad");
162
    }
163
    h.add(&["-entry", "kernel", "-ril", work]);
164
    if not h.compile(h.join(&["build kernel for ", fixture]), h.out(&["kernel.log"])) {
165
        return false;
166
    }
167
    let machine = h.path(&["machine.ras"]);
168
    try concatAssembly(h, machine, h.join(&[dir, "/machine.ras"]));
169
    return link(h, h.join(&["link ", fixture]), work, machine, image, boot, harness::OK, &[]);
170
}