test/integration/boot.rad 11.3 KiB raw
1
//! Kernel boot and user-mode fixtures that run the production kernel image:
2
//! boot on every hart count, bootstrap, runtime loading, scheduling, and the
3
//! runtime loader with hostile packages.
4
5
use std::fs;
6
use std::io;
7
use std::process;
8
use std::sys;
9
use super::harness;
10
use super::kernel;
11
use super::harness::Harness;
12
use super::harness::Expect;
13
use std::process::Outcome;
14
use super::kernel::Embedded;
15
16
/// Hostile packages the generator writes, with the names of their arrays.
17
constant HOSTILE: [Embedded; 14] = [
18
    Embedded { name: "HOSTILE_FORGED",    file: "forged" },
19
    Embedded { name: "HOSTILE_OVERWRITE", file: "overwrite" },
20
    Embedded { name: "HOSTILE_LEAK",      file: "leak" },
21
    Embedded { name: "HOSTILE_EXTERN",    file: "extern" },
22
    Embedded { name: "HOSTILE_DEPENDENT", file: "dependent" },
23
    Embedded { name: "HOSTILE_BUFFER",    file: "buffer" },
24
    Embedded { name: "HOSTILE_HEAPREAD",  file: "heapread" },
25
    Embedded { name: "HOSTILE_HEAPSLICE", file: "heapslice" },
26
    Embedded { name: "HOSTILE_HEAPFORGE", file: "heapforge" },
27
    Embedded { name: "HOSTILE_HEAPTYPED", file: "heaptyped" },
28
    Embedded { name: "HOSTILE_HEAPRESET", file: "heapreset" },
29
    Embedded { name: "HOSTILE_MIXED",     file: "mixed" },
30
    Embedded { name: "HOSTILE_ZEROCAP",   file: "zerocap" },
31
    Embedded { name: "HOSTILE_OMITCAP",   file: "omitcap" },
32
];
33
/// Exit status of the hosted loaded package.
34
constant LOADED_STATUS: i32 = 43;
35
/// Exit status of the hosted package that grows slices in a heap.
36
constant GROWN_STATUS: i32 = 42;
37
/// Number of exported functions that the runtime fixture adds to the loaded
38
/// package.
39
constant GENERATED_FUNCTIONS: u32 = 128;
40
41
/// Tick budget of the startup run. The run reaches idle on every hart and keeps
42
/// running, so it ends at the limit by design, and the budget is how long the
43
/// kernel gets to reach idle.
44
constant STARTUP_STEPS: u64 = 80000000;
45
/// Tick budget of the trap run, which ends at the limit by design after the
46
/// kernel reports the trap.
47
constant TRAP_STEPS: u64 = 20000000;
48
49
/// Build an image from the kernel assembly, led by `first`, and the installed
50
/// kernel binary IL. Returns the path of the image, or `nil` when it failed.
51
fn assemble(h: &mut Harness, name: *[u8], first: *[u8]) -> ?*[u8] throws (sys::Error) {
52
    let assembly = h.path(&[name, ".ras"]);
53
    let image = h.path(&[name, ".rv64"]);
54
    let what = h.join(&["build the ", name, " image"]);
55
    try kernel::concatAssembly(h, assembly, first);
56
    if kernel::link(h, what, kernel::RIL_DIR, assembly, image, &[], harness::OK, &[]) {
57
        return image;
58
    }
59
    return nil;
60
}
61
62
/// Boot the kernel on each supported hart count, run the production kernel to
63
/// idle on eight harts, check the trap fixture, and check that invalid assembly
64
/// is rejected with its location.
65
export fn boot(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
66
    try h.begin("boot");
67
    if let image = try assemble(h, "boot", "test/boot/machine.ras") {
68
        let ready = [
69
            Expect::Line("kernel: platform ready"),
70
            Expect::Line("kernel: boot catalog ready"),
71
        ];
72
        for harts in &[1, 2, 8] {
73
            let count = h.num(harts);
74
            let mark = h.failed;
75
            let run = h.machine(image, harts, h.out(&["boot", count, ".log"]));
76
            h.run(h.join(&["kernel boot on ", count, " harts"]), run, harness::OK, &ready[..]);
77
            h.done(mark, h.join(&["kernel boot: ", count, " harts passed"]));
78
        }
79
    }
80
    let mark = h.failed;
81
    let mut startup = h.machine("bin/kernel.rv64", 8, h.out(&["startup.log"]));
82
    set startup.maxSteps = STARTUP_STEPS;
83
    let idle = Expect::Matching { text: "wfi=1 mcause=0x0", count: 8 };
84
    h.run("kernel startup", startup, Outcome::StepLimit, &[idle]);
85
    h.done(mark, "kernel startup: all eight harts reached machine idle without traps");
86
    if let image = try assemble(h, "trap", "test/boot/trap.ras") {
87
        let mark = h.failed;
88
        let mut trap = h.machine(image, 2, h.out(&["trap.log"]));
89
        set trap.maxSteps = TRAP_STEPS;
90
        let reported = [
91
            Expect::Lines { text: "kernel: unexpected trap", count: 1 },
92
            Expect::Matching { text: "wfi=1",                count: 2 },
93
        ];
94
        h.run("kernel trap", trap, Outcome::StepLimit, &reported[..]);
95
        h.done(mark, "kernel trap: shared kernel state restored from the hart anchor");
96
    }
97
    let invalid = h.path(&["invalid.ras"]);
98
    let location = h.join(&[invalid, ":2:1: error: unknown instruction"]);
99
    try fs::writeFile(invalid, ".text;\ninvalid;\n");
100
    let rejected = Outcome::Exited(1);
101
    let expects = [Expect::Contains(location)];
102
    let image = h.path(&["invalid.rv64"]);
103
    kernel::link(
104
        h, "invalid assembly", kernel::RIL_DIR, invalid, image, &[], rejected, &expects[..]
105
    );
106
}
107
108
/// Construct root from a boot package and check that the kernel retains
109
/// authority after root exits.
110
export fn bootstrap(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
111
    try h.begin("bootstrap");
112
    let root = kernel::package(h, "loaded", "test/bootstrap/root.rad", &[kernel::ABI, kernel::SYS]);
113
    let service = kernel::package(h, "service", "test/bootstrap/service.rad", &[]);
114
    let image = h.path(&["bootstrap.rv64"]);
115
    let packages = [h.path(&["loaded/loaded.ril.bin"]), h.path(&["service/service.ril.bin"])];
116
    if root
117
        and service
118
        and try kernel::image(h, "bootstrap", image, &[], &packages[..])
119
        and h.boot("bootstrap", image, 1)
120
    {
121
        io::printLn("bootstrap: root exit preserves allocation and creation authority");
122
    }
123
}
124
125
/// Compile, cancel and retry a package through scheduled user calls.
126
export fn runtime(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
127
    try h.begin("runtime");
128
    let mods = ["cases=test/runtime/cases.rad", kernel::ABI, kernel::SYS];
129
    let spin = kernel::package(h, "spin", "test/runtime/spin.rad", &mods[..]);
130
    // The loaded package is the loader fixture with many added functions.
131
    let source = h.path(&["loaded.rad"]);
132
    let mut out = try fs::create(source);
133
    out.write(try h.load("test/loader/loaded.rad"));
134
    for index in 0 .. GENERATED_FUNCTIONS {
135
        let n = h.num(index);
136
        out.write(
137
            h.join(
138
                &[
139
                    "\n/// Compute a distinct exported value.\nexport fn value", n,
140
                    "(input: u64) -> u64 { return input + ", n, "; }\n",
141
                ]
142
            )
143
        );
144
    }
145
    try out.close();
146
    let loaded = kernel::package(h, "loaded", source, &[]);
147
    let image = h.path(&["dispatch.rv64"]);
148
    let inputs = [
149
        Embedded { name: "INPUT",   file: h.path(&["spin/spin.ril.bin"]) },
150
        Embedded { name: "PAYLOAD", file: h.path(&["loaded/loaded.ril.bin"]) },
151
    ];
152
    if spin
153
        and loaded
154
        and try kernel::image(h, "runtime", image, &inputs[..], &[])
155
        and h.boot("runtime", image, 1)
156
    {
157
        io::printLn("runtime: scheduled cancellation, retry, and publication passed");
158
    }
159
}
160
161
/// Execute root handoff, user-space yields and window expiry in fixture
162
/// packages, on each hart count in `args`, or on 1, 2 and 8 harts when `args`
163
/// is empty.
164
export fn scheduling(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
165
    let mut harts: *mut [u32] = &mut [];
166
    for arg in args {
167
        try! harts.append(try harness::number(arg), h.arena);
168
    }
169
    if args.len == 0 {
170
        for count in &[1, 2, 8] {
171
            try! harts.append(count, h.arena);
172
        }
173
    }
174
    try h.begin("scheduling");
175
    let mods = [kernel::ABI, kernel::SYS, kernel::MMIO];
176
    let root = kernel::package(h, "root", "test/scheduling/root.rad", &mods[.. 2]);
177
    let scheduler = kernel::package(h, "scheduler", "test/scheduling/scheduler.rad", &mods[..]);
178
    let packages = [h.path(&["root/root.ril.bin"]), h.path(&["scheduler/scheduler.ril.bin"])];
179
    let assembly = h.path(&["scheduling.ras"]);
180
    let image = h.path(&["scheduling.rv64"]);
181
    try kernel::concatAssembly(h, assembly, nil);
182
    let linked = kernel::link(
183
        h,
184
        "build the scheduling image",
185
        kernel::RIL_DIR,
186
        assembly,
187
        image,
188
        &packages[..],
189
        harness::OK,
190
        &[]
191
    );
192
    if root and scheduler and linked {
193
        for count in &harts[..] {
194
            let number = h.num(count);
195
            if h.boot(h.join(&["scheduling on ", number, " harts"]), image, count) {
196
                io::print(
197
                    h.join(
198
                        &[
199
                            "scheduling: ", number,
200
                            "-hart root handoff, four user-space yields, 513 retired windows, and checked shutdown passed\n",
201
                        ]
202
                    )
203
                );
204
            }
205
        }
206
    }
207
}
208
209
/// Build the loader fixture, run it, and check that the image tool rejects a
210
/// forged package and writes no image for it.
211
export fn loader(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
212
    try h.begin("loader");
213
    let mark = h.failed;
214
    let program = h.path(&["hosted.rv64"]);
215
    let il = kernel::package(h, "loaded", "test/loader/loaded.rad", &[]);
216
    h.add(&["-pkg", "loaded", "-mod", "test/loader/loaded.rad", "-entry", "loaded", "-o", program]);
217
    let image = h.compile("compile loaded to an image", h.out(&["image.log"]));
218
    let hosted = h.cmd(program, &[], harness::HOSTED, harness::RUN_STEPS, harness::INHERIT);
219
    let grownProgram = h.path(&["grown.rv64"]);
220
    let grownIl = kernel::package(h, "grown", "test/loader/grown.rad", &[]);
221
    h.add(&[
222
        "-pkg", "grown", "-mod", "test/loader/grown.rad", "-entry", "grown", "-o", grownProgram
223
    ]);
224
    let grownImage = h.compile("compile grown to an image", h.out(&["grown.log"]));
225
    let grownHosted = h.cmd(
226
        grownProgram, &[], harness::HOSTED, harness::RUN_STEPS, harness::INHERIT
227
    );
228
    let hostile = h.path(&["hostile"]);
229
    try fs::makeDir(hostile);
230
    let mut entries: *mut [Embedded] = &mut [];
231
    try! entries.append(
232
        Embedded { name: "INPUT", file: h.path(&["loaded/loaded.ril.bin"]) }, h.arena
233
    );
234
    try! entries.append(
235
        Embedded { name: "GROWN", file: h.path(&["grown/grown.ril.bin"]) }, h.arena
236
    );
237
    for entry in &HOSTILE[..] {
238
        let file = h.join(&[hostile, "/", entry.file, ".ril.bin"]);
239
        try! entries.append(Embedded { name: entry.name, file }, h.arena);
240
    }
241
    let fixture = h.path(&["loader.rv64"]);
242
    let rejected = h.path(&["rejected.rv64"]);
243
    let work = h.work;
244
    let machine = h.path(&["machine.ras"]);
245
    let forged = [h.join(&[hostile, "/forged.ril.bin"])];
246
    let diagnosed = [Expect::Contains("boot package fails verification: Type")];
247
    if il
248
        and image
249
        and h.run("hosted loaded package", hosted, Outcome::Exited(LOADED_STATUS), &[])
250
        and grownIl
251
        and grownImage
252
        and h.run("hosted grown package", grownHosted, Outcome::Exited(GROWN_STATUS), &[])
253
        and h.tool("generate hostile packages", "test/loader/hostile.rv64", &[hostile])
254
        and try kernel::image(h, "loader", fixture, &entries[..], &[])
255
        and h.boot("loader fixture", fixture, 0)
256
    {
257
        let status = Outcome::Exited(1);
258
        kernel::link(
259
            h, "hostile package", work, machine, rejected, &forged[..], status, &diagnosed[..]
260
        );
261
        h.expect("hostile package: no image", rejected, Expect::Absent);
262
        h.done(
263
            mark,
264
            "runtime loader: hosted and loaded entries agree, and hostile packages are rejected"
265
        );
266
    }
267
}