test/integration/machine.rad 9.1 KiB raw
1
//! Fixtures that run on a bare machine: synchronization, traps, shared
2
//! packages and the kernel dispatch fixtures. Also the rejection of slot
3
//! reservation misuse and the check of the checked-in seed image.
4
5
use std::arch::rv64::image;
6
use std::fs;
7
use std::io;
8
use std::mem;
9
use std::process;
10
use std::process::Outcome;
11
use std::process::Target;
12
use std::sys;
13
use super::harness;
14
use super::harness::Expect;
15
use super::harness::Harness;
16
use super::kernel;
17
use super::kernel::Embedded;
18
use super::linking;
19
20
/// The checked-in seed compiler image.
21
constant SEED: *[u8] = "seed/radiance.rv64";
22
23
/// Run `tool` on the arguments to build `image`, then run the image on
24
/// `harts` harts of a bare machine with the host default memory.
25
fn buildAndRun(
26
    h: &mut Harness,
27
    what: *[u8],
28
    tool: *[u8],
29
    args: &[*[u8]],
30
    image: *[u8],
31
    harts: u32
32
) -> bool {
33
    if not h.tool(h.join(&["build ", what]), tool, args) {
34
        return false;
35
    }
36
    let target = Target::Machine(process::Machine { harts, memory: 0, irq: nil });
37
    let run = h.cmd(image, &[], target, harness::MACHINE_STEPS, harness::INHERIT);
38
    return h.run(what, run, harness::OK, &[]);
39
}
40
41
/// Assemble the files `sources` into one image and run it on `harts` harts.
42
fn assembleAndRun(
43
    h: &mut Harness,
44
    what: *[u8],
45
    prefix: *[u8],
46
    sources: &[*[u8]],
47
    harts: u32
48
) throws (sys::Error) {
49
    let assembly = h.path(&[prefix, ".ras"]);
50
    let image = h.path(&[prefix, ".rv64"]);
51
    try h.concat(assembly, sources);
52
    buildAndRun(h, what, "test/sync/build.rv64", &[assembly, image], image, harts);
53
}
54
55
/// Run the synchronization fixture on 1, 2 and 8 harts.
56
export fn sync(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
57
    try h.begin("sync");
58
    let mark = h.failed;
59
    for harts in &[1, 2, 8] {
60
        let count = h.num(harts);
61
        let header = h.path(&["harts", count, ".ras"]);
62
        try fs::writeFileParts(header, &[".constant HARTS ", count, ";\n"]);
63
        let sources = [header, "test/sync/machine.ras", "kernel/kernel/sync.ras"];
64
        let what = h.join(&["synchronization on ", count, " harts"]);
65
        try assembleAndRun(h, what, h.join(&["sync", count]), &sources[..], harts);
66
    }
67
    h.done(
68
        mark, "synchronization: code publication, locks, atomics, and MMIO passed on 1/2/8 harts"
69
    );
70
}
71
72
/// Run the trap fixtures on one hart.
73
export fn trap(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
74
    try h.begin("trap");
75
    let mark = h.failed;
76
    for fixture in &["machine", "reservations", "vector"] {
77
        let sources = [h.join(&["test/trap/", fixture, ".ras"]), "kernel/kernel/trap.ras"];
78
        let what = h.join(&["trap fixture ", fixture]);
79
        try assembleAndRun(h, what, h.join(&["trap.", fixture]), &sources[..], 1);
80
    }
81
    h.done(
82
        mark,
83
        "trap contexts: machine/user registers, nested timer, illegal instruction, and stacks passed"
84
    );
85
    h.done(mark, "trap reservations: entry, resume, and user context isolation passed");
86
    h.done(mark, "trap vectors: complete state, nested timer and user context switches passed");
87
}
88
89
/// Compile two packages once and run them with two private state graphs, then
90
/// link a package built alone with an entry built beside an unrelated package,
91
/// whose typed error tags must agree.
92
export fn shared(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
93
    try h.begin("shared");
94
    let mark = h.failed;
95
    h.add(&["-pkg", "support", "-mod", "test/shared/support.rad"]);
96
    let work = h.work;
97
    h.add(&["-pkg", "app", "-mod", "test/shared/app.rad", "-entry", "app", "-ril", work]);
98
    if h.compile("compile shared packages", h.out(&["compile.log"])) {
99
        let image = h.path(&["native.rv64"]);
100
        let packages = [h.path(&["support/support.ril.bin"]), h.path(&["app/app.ril.bin"]), image];
101
        buildAndRun(h, "shared packages", "test/shared/build.rv64", &packages[..], image, 0);
102
    }
103
    h.done(
104
        mark, "shared packages: calls, callbacks, private pointers, and independent state passed"
105
    );
106
    let alone = h.path(&["alone"]);
107
    let combined = h.path(&["combined"]);
108
    try harness::makeDirs(alone);
109
    try harness::makeDirs(combined);
110
    h.add(&["-pkg", "support", "-mod", "test/shared/errors/support.rad", "-ril", alone]);
111
    let first = h.compile("compile support alone", h.out(&["alone.log"]));
112
    h.add(&["-pkg", "noise", "-mod", "test/shared/errors/noise.rad"]);
113
    h.add(&["-pkg", "support", "-mod", "test/shared/errors/support.rad"]);
114
    h.add(&[
115
        "-pkg", "app", "-mod", "test/shared/errors/app.rad", "-entry", "app", "-ril", combined
116
    ]);
117
    let second = h.compile("compile support beside noise", h.out(&["combined.log"]));
118
    if first and second {
119
        let image = h.path(&["errors.rv64"]);
120
        let packages = [
121
            h.join(&[alone, "/support/support.ril.bin"]),
122
            h.join(&[combined, "/app/app.ril.bin"]),
123
            image,
124
            "catch",
125
        ];
126
        buildAndRun(
127
            h, "typed catch across compilations", "test/shared/build.rv64", &packages[..], image, 0
128
        );
129
    }
130
    h.done(mark, "shared packages: typed catch agrees across separate compilations");
131
    try linking::check(h);
132
}
133
134
/// Check that misuse of reservations fails during compilation: one consumed
135
/// twice and one that is never consumed.
136
export fn slots(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
137
    try h.begin("slots");
138
    let names = ["duplicate", "abandon"];
139
    let diagnostics = ["linear value used after consumption", "linear value is not consumed"];
140
    for name, i in &names[..] {
141
        let what = h.join(&["reservation rejection: ", name]);
142
        h.add(
143
            &[
144
                "-pkg",
145
                "kernel",
146
                "-mod",
147
                "test/slots/kernel.rad",
148
                "-mod",
149
                "slots=kernel/kernel/slots.rad",
150
            ]
151
        );
152
        h.add(&["-mod", kernel::ABI, "-mod", "limits=kernel/kernel/limits.rad"]);
153
        h.add(&["-pkg", name, "-mod", h.join(&["test/slots/", name, ".rad"]), "-entry", name]);
154
        h.add(&["-o", h.path(&["test.rv64"])]);
155
        let compile = h.cc(h.take(), h.out(&[name, ".log"]));
156
        let diagnosed = Expect::Contains(diagnostics[i]);
157
        let mark = h.failed;
158
        h.run(what, compile, Outcome::Exited(1), &[diagnosed]);
159
        h.done(mark, h.join(&[what, " passed"]));
160
    }
161
}
162
163
/// Check that the seed is a self-contained RV64 image whose size is the
164
/// header plus its three stored sections.
165
export fn seed(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
166
    let info = try fs::stat(SEED) else {
167
        h.fail("seed test", h.join(&[SEED, " does not exist"]));
168
        return;
169
    };
170
    let header = try h.load(SEED);
171
    let layout = try image::parse(header) catch {
172
        h.fail("seed test", "seed is not a self-contained RV64 image");
173
        return;
174
    };
175
    let expected = image::HEADER_SIZE as u64
176
        + layout.code.size as u64
177
        + layout.roData.size as u64
178
        + layout.rwData.size as u64;
179
    if info.size <> expected {
180
        h.fail(
181
            "seed test",
182
            h.join(
183
                &[
184
                    "seed image size ", h.num(info.size as u32),
185
                    " does not match the header sections ", h.num(expected as u32),
186
                ]
187
            )
188
        );
189
        return;
190
    }
191
    h.pass();
192
    io::printLn("seed test: ok");
193
}
194
195
/// Build the dispatch fixture `fixture` into `image`. The image appears
196
/// complete or not at all: it is built in `bin/test/dispatch-build-<fixture>`
197
/// and renamed into place.
198
export fn dispatchBuild(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
199
    let fixture = args[0];
200
    try h.begin(h.join(&["dispatch-build-", fixture]));
201
    let cases = h.join(&["test/", fixture, "/cases.rad"]);
202
    let mut mods = [kernel::ABI, kernel::SYS, kernel::MMIO, h.join(&["cases=", cases])];
203
    let mut count: u32 = 2;
204
    if mem::eq(fixture, "mmio") {
205
        set count = 3;
206
    }
207
    if try harness::exists(cases) {
208
        set mods[count] = mods[3];
209
        set count += 1;
210
    }
211
    let module = h.join(&["test/", fixture, "/spin.rad"]);
212
    let staged = h.path(&["dispatch.rv64"]);
213
    let inputs = [Embedded { name: "INPUT", file: h.path(&["spin/spin.ril.bin"]) }];
214
    if kernel::package(h, "spin", module, &mods[.. count])
215
        and try kernel::image(h, fixture, staged, &inputs[..], &[])
216
    {
217
        try fs::rename(staged, args[1]);
218
    }
219
}
220
221
/// Build the controller and worker images that the cycles fixture loads after
222
/// dispatch starts, and the kernel image that embeds them, into the image.
223
export fn cyclesBuild(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
224
    try h.begin("cycles-build");
225
    let mods = ["support=test/cycles/support.rad", kernel::ABI, kernel::SYS, kernel::MMIO];
226
    let spin = kernel::package(h, "spin", "test/cycles/spin.rad", &mods[..]);
227
    let job = kernel::package(h, "job", "test/cycles/job.rad", &mods[.. 3]);
228
    let staged = h.path(&["cycles.rv64"]);
229
    let inputs = [
230
        Embedded { name: "spin", file: h.path(&["spin/spin.ril.bin"]) },
231
        Embedded { name: "job",  file: h.path(&["job/job.ril.bin"]) },
232
    ];
233
    if spin and job and try kernel::image(h, "cycles", staged, &inputs[..], &[]) {
234
        try fs::rename(staged, args[0]);
235
    }
236
}