test/mmio/kernel/dispatchcheck.rad 6.6 KiB raw
1
//! Native fixed-width MMIO and authority rejection before register access.
2
export mod inputs;
3
export mod cases;
4
5
use kernel::fixture;
6
use kernel::state;
7
use kernel::abi;
8
use kernel::pages;
9
use kernel::capability;
10
use kernel::domains;
11
use kernel::dispatch;
12
use kernel::boot;
13
use kernel::events;
14
use kernel::sync;
15
use kernel::devices;
16
use kernel::platform;
17
18
/// Number of child scenarios, in CPU-window order.
19
constant COUNT: u32 = 9;
20
/// Delay before the first child window, in machine timer ticks.
21
constant START_DELAY: u64 = 1000000;
22
/// Duration of each child and checker window, in machine timer ticks.
23
constant WINDOW_TICKS: u64 = 2000000;
24
25
/// User actions and capability configurations in CPU-window order.
26
/// The expected lifecycle event is CHILD_EXIT with EXIT_STATUS for Widths,
27
/// and FAULT with FAULT_ABORT for each rejected access.
28
static SCENARIOS: [cases::Scenario; COUNT] = [
29
    cases::Scenario::Widths,
30
    cases::Scenario::MissingHandle,
31
    cases::Scenario::ReadOnly,
32
    cases::Scenario::WriteOnly,
33
    cases::Scenario::Misaligned,
34
    cases::Scenario::OutOfBounds,
35
    cases::Scenario::DroppedHandle,
36
    cases::Scenario::WrongKind,
37
    cases::Scenario::AddressOverflow,
38
];
39
40
// State retained across setup and verification is initialized before
41
// dispatch starts. The scheduled checker reads it after the user workload.
42
/// Bootstrap creation, allocation, and budget authority.
43
unsafe static TABLE: capability::Table = undefined;
44
/// Child domain identities retained by terminal events.
45
unsafe static CHILDREN: [fixture::Actor; COUNT] = undefined;
46
/// Surviving event receiver and native completion checker.
47
unsafe static PARENT: fixture::Actor = undefined;
48
/// Register storage exposed only through a synthetic Device capability.
49
static REGISTERS: [u64; cases::REGION_SIZE as u32 / fixture::WORD_BYTES] =
50
    [0; cases::REGION_SIZE as u32 / fixture::WORD_BYTES];
51
52
/// Map a validated physical page.
53
fn memory(address: u64) -> *mut u8;
54
/// Current kernel package-state base.
55
fn kernelGp() -> u64;
56
/// Native entry for the completion checker.
57
fn completion() -> u64;
58
/// Signal successful completion to the machine fixture.
59
fn finish();
60
61
/// Give each child a disjoint window, followed by the surviving parent.
62
export unsafe fn setup() {
63
    let authority = fixture::bootstrap(&mut TABLE);
64
    let image = fixture::load(&mut TABLE, authority, &inputs::INPUT[..]);
65
    set PARENT = fixture::create(&mut TABLE, authority, image);
66
    let parentHandle = PARENT.handle;
67
    fixture::machine(PARENT.domain, completion(), kernelGp());
68
    let scratch = state::SHARED.devices.count;
69
    set state::SHARED.devices.regions[scratch] = {
70
        kind: platform::Kind::Uart,
71
        info: { base: &mut REGISTERS[0] as u64, size: cases::REGION_SIZE },
72
        rights: abi::Rights(
73
            abi::READ
74
                | abi::WRITE
75
                | abi::GRANT
76
                | abi::TRANSFER
77
        ),
78
    };
79
    set state::SHARED.devices.count += 1;
80
    let mut clockDevice: u32 = 0;
81
    let mut finish: u32 = 0;
82
    for i in 0 .. scratch {
83
        if state::SHARED.devices.regions[i].kind == platform::Kind::Clint {
84
            set clockDevice = i;
85
        }
86
        if state::SHARED.devices.regions[i].kind == platform::Kind::Finish {
87
            set finish = i;
88
        }
89
    }
90
    for i in 0 .. COUNT {
91
        let actor = fixture::create(&mut TABLE, authority, image);
92
        let handle = actor.handle;
93
        let object = actor.domain;
94
        let mut child = try! domains::get(&state::SHARED.domains, object);
95
        let scenario = SCENARIOS[i];
96
        set CHILDREN[i] = actor;
97
        try! domains::reparent(
98
            &mut state::SHARED.domains, &TABLE, handle, parentHandle
99
        );
100
        let moved = fixture::stack(
101
            &mut TABLE, authority, &mut child.memory.table
102
        );
103
        let page = try! pages::allocate(
104
            &mut state::SHARED.pages, &mut TABLE, authority, 1
105
        );
106
        let granted = try! pages::grant(
107
            &mut state::SHARED.pages, &TABLE, &mut child.memory.table, page, abi::READ as u64
108
        );
109
        let storage = try! pages::get(
110
            &state::SHARED.pages, (try! capability::get(&TABLE, page)).object
111
        );
112
        let mut region = scratch;
113
        if scenario == cases::Scenario::ReadOnly
114
            or scenario == cases::Scenario::DroppedHandle
115
        {
116
            set region = finish;
117
        }
118
        let mut device = try! devices::install(
119
            &state::SHARED.devices, &mut child.memory.table, region
120
        );
121
        let slot = (try! abi::decode(device)).object.index;
122
        if scenario == cases::Scenario::ReadOnly {
123
            set child.memory.table.entries[slot].rights =
124
                abi::Rights(abi::READ);
125
        }
126
        if scenario == cases::Scenario::WriteOnly {
127
            set child.memory.table.entries[slot].rights =
128
                abi::Rights(abi::WRITE);
129
        }
130
        if scenario == cases::Scenario::MissingHandle {
131
            set device = abi::Handle(0);
132
        }
133
        if scenario == cases::Scenario::WrongKind {
134
            set device = granted;
135
        }
136
        let timerHandle = try! devices::install(
137
            &state::SHARED.devices, &mut child.memory.table, clockDevice
138
        );
139
        let args = @sliceOf(memory(storage.base) as *mut u64, 3);
140
        set args[0] = *device;
141
        set args[1] = *timerHandle;
142
        set args[2] = scenario as u64;
143
        try! fixture::activate(&TABLE, handle, moved, granted, 24);
144
    }
145
    let timer = try! dispatch::timer(&boot::PLATFORM, 0);
146
    let clock = dispatch::now(timer);
147
    fixture::schedule(
148
        &mut TABLE, &CHILDREN[..], PARENT, clock + START_DELAY, WINDOW_TICKS, clock
149
    );
150
}
151
152
/// Check each terminal kind, status, and generation while the parent continues
153
/// to run.
154
export unsafe fn verify() {
155
    let guard = sync::enter();
156
    let mut parent = try! domains::get(&state::SHARED.domains, PARENT.domain);
157
    for scenario, i in &SCENARIOS[..] {
158
        let actor = CHILDREN[i];
159
        let event = try! events::pop(&mut parent.memory.ring)
160
            else panic "verify: missing termination event";
161
        assert event.value == abi::id(actor.domain);
162
        let valid = scenario == cases::Scenario::Widths;
163
        let kind = events::CHILD_EXIT if valid else events::FAULT;
164
        let code = cases::EXIT_STATUS if valid else abi::FAULT_ABORT;
165
        assert event.kind == kind and event.code == code;
166
        assert state::SHARED.domains.records[actor.domain.index].state
167
            == domains::Lifecycle::Dead;
168
    }
169
    try! events::refresh(
170
        &mut state::SHARED.domains.events, PARENT.domain, &parent.memory.ring
171
    );
172
    assert try! events::pop(&mut parent.memory.ring) == nil;
173
    sync::leave(guard);
174
    finish();
175
}