test/cycles/spin.rad 3.9 KiB raw
1
//! Concurrent load, spawn, yield, fault, and teardown cycles.
2
export mod support;
3
export mod abi;
4
export mod sys;
5
export mod mmio;
6
7
/// Ordered shared-memory access boundary for the single event consumer.
8
@intrinsic fn memoryFence();
9
10
/// Published event wire record.
11
record Event: Copy {
12
    /// Notification kind.
13
    kind: u16,
14
    /// Reserved zero bits.
15
    reserved: u16,
16
    /// Terminal status or fault code.
17
    code: u32,
18
    /// Generation-bearing child identity.
19
    value: u64,
20
}
21
22
/// Shared single-consumer event ring.
23
record Ring: Copy {
24
    /// Published event slots.
25
    data: [Event; 256],
26
    /// Consumer progress.
27
    head: u32,
28
    /// Producer progress.
29
    tail: u32,
30
    /// Ring index mask.
31
    mask: u32,
32
}
33
34
/// Reach the ring of an Events handle as one writable record.
35
@intrinsic fn pageSliceRing(handle: u64, write: bool, out: &mut *mut [Ring]) -> i64;
36
37
/// Reach the shared event ring of the controller.
38
fn eventRing(handle: abi::Handle) -> *mut [Ring] {
39
    let mut rings: *mut [Ring] = &mut [];
40
    assert pageSliceRing(*handle, true, &mut rings) == 0;
41
    return rings;
42
}
43
44
/// Repeat publication lookup and private worker lifetimes on this hart.
45
@default unsafe fn main(env: *abi::Env) {
46
    // The arguments Page holds the readable binary worker package and its byte length, the
47
    // read-only clock, the capability of the Page itself, the reserved worker interval in
48
    // clock ticks, and the worker code base and the number of checked worker lifetimes, which
49
    // the controller reports to the checker.
50
    assert env.argsSize == 56;
51
    let args = try! sys::pageWordsMut(abi::Handle(env.args));
52
    let source = abi::Handle(args[0]);
53
    let length = args[1];
54
    let clock = abi::Handle(args[2]);
55
    let page = abi::Handle(args[3]);
56
    let quantum = args[4];
57
    let rings = eventRing(abi::Handle(env.eventsHandle));
58
    assert rings.len == 1;
59
    let initial = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT));
60
    for turn in 0 .. 4 {
61
        assert support::advance() == turn as u64 + 1;
62
        let mut image = abi::Handle(0);
63
        while image == abi::Handle(0) {
64
            set image = try sys::imageLoad(abi::Handle(0), source, 0, length) catch error {
65
                assert error == abi::Error::Busy;
66
                continue;
67
            };
68
        }
69
        let loaded = try! sys::queryImage(image);
70
        if turn == 0 {
71
            set args[5] = loaded.codeBase;
72
        }
73
        assert loaded.codeBase == args[5];
74
        let child = try! sys::domainCreate(abi::Handle(0), image);
75
        let identity = sys::queryDomain(child).id;
76
        let stack = try! sys::pageAllocate(abi::Handle(0), 4);
77
        let moved = try! sys::capabilityTransfer(stack, child, abi::RIGHTS_MASK as u64);
78
        let shared = try! sys::capabilityGrant(page, child, abi::READ as u64);
79
        try! sys::domainActivate(child, moved, shared, (turn % 2) as u64);
80
        let current = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT));
81
        assert current.context == initial.context and current.hart == initial.hart;
82
        let later = try! sys::budgetSplit(
83
            abi::Handle(current.budget),
84
            mmio::read64(
85
                clock, 0
86
            ) + quantum
87
        );
88
        try! sys::yield(child);
89
        assert (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)).budget == *later;
90
        let ring = &mut rings[0];
91
        let head = ring.head;
92
        let tail = ring.tail;
93
        memoryFence();
94
        assert head <> tail and ring.mask == 255;
95
        let event = ring.data[head & ring.mask];
96
        assert event.value == identity and event.reserved == 0;
97
        if turn % 2 == 0 {
98
            assert event.kind == 4 and event.code == 9;
99
        } else {
100
            assert event.kind == 3 and event.code == 3;
101
        }
102
        memoryFence();
103
        set ring.head = head + 1;
104
        try! sys::capabilityDrop(image);
105
        set args[6] = turn as u64 + 1;
106
    }
107
    sys::exit(68);
108
}