test/runtime/kernel/dispatchcheck.rad 13.6 KiB raw
1
//! Runtime compiler ownership across scheduled windows and domain destruction.
2
export mod inputs;
3
export mod cases;
4
5
use std::fmt;
6
use kernel::abi;
7
use kernel::budgets;
8
use kernel::calls;
9
use kernel::capability;
10
use kernel::dispatch;
11
use kernel::domains;
12
use kernel::fixture;
13
use kernel::frames;
14
use kernel::instances;
15
use kernel::lifecycle;
16
use kernel::loader;
17
use kernel::pages;
18
use kernel::registry;
19
use kernel::remote;
20
use kernel::slots;
21
use kernel::state;
22
use kernel::sync;
23
24
/// Delay before the first user window, in timer ticks.
25
constant START_DELAY: u64 = 1000000;
26
/// End of the first interrupted load, relative to the first window start. It
27
/// must fall after the loader reserves its output and before its compilation
28
/// ends, so cancellation covers unpublished native code and metadata.
29
constant FIRST_WINDOW_END: u64 = 6000000;
30
/// End of the controller's cancellation window.
31
constant CANCEL_WINDOW_END: u64 = 50000000;
32
/// End of the retry loader's first window.
33
constant RETRY_WINDOW_END: u64 = 56000000;
34
/// End of the witness window.
35
constant WITNESS_WINDOW_END: u64 = 58000000;
36
/// End of the retry loader's final window.
37
constant FINAL_WINDOW_END: u64 = 300000000;
38
/// Duration of the final machine verification window.
39
constant CHECK_WINDOW: u64 = 10000000;
40
/// Maximum maintenance requests while reclaiming cancelled compiler storage.
41
constant RECLAIM_ATTEMPTS: u32 = 10000;
42
/// Maximum frames inspected in one metadata critical section.
43
constant FRAME_BATCH: u32 = 256;
44
45
/// Actor and end tick of one scheduled execution window.
46
record Window: Copy {
47
    /// Actor that receives the window.
48
    actor: u32,
49
    /// Exclusive end relative to the schedule start.
50
    end: u64,
51
}
52
53
// State retained across setup and verification is initialized before
54
// dispatch starts. The scheduled checker reads it after the user workload.
55
/// Bootstrap resource capabilities.
56
unsafe static TABLE: capability::Table = undefined;
57
/// First loader, retry loader, witness, and controller domain handles,
58
/// generation-bearing domain identities, and initial context identities.
59
unsafe static ACTORS: [fixture::Actor; cases::ACTORS] = undefined;
60
/// Controller capability for the first loader.
61
unsafe static VICTIM: abi::Handle = undefined;
62
/// Controller capability for the binary input page.
63
unsafe static SOURCE: abi::Handle = undefined;
64
/// Physical address of shared user progress and package identity.
65
unsafe static PROGRESS: u64 = undefined;
66
/// Start of the controller's final execution window.
67
unsafe static FINAL: u64 = 0;
68
/// Write one diagnostic byte to the fixture UART.
69
fn put(byte: u8);
70
/// Write a fixed-width hexadecimal diagnostic value.
71
fn number(value: u64) {
72
    let mut text: [u8; 16] = [0; 16];
73
    for byte in fmt::formatHexU64(value, &mut text[..]) {
74
        put(byte);
75
    }
76
}
77
78
/// Map a validated physical page.
79
fn memory(address: u64) -> *mut u8;
80
/// Current kernel package-state base.
81
fn kernelGp() -> u64;
82
/// Native controller entry address.
83
fn completion() -> u64;
84
/// Execute published code with one private package-state table.
85
fn invoke(address: u64, table: u64) -> i64;
86
/// Signal native fixture completion.
87
fn finish();
88
89
/// Install user actors and disjoint windows that interrupt two compiler calls.
90
export unsafe fn setup() {
91
    let authority = fixture::bootstrap(&mut TABLE);
92
    let image = fixture::load(&mut TABLE, authority, &inputs::INPUT[..]);
93
    let payload = fixture::input(&mut TABLE, authority, &inputs::PAYLOAD[..]);
94
    let progress = try! pages::allocate(
95
        &mut state::SHARED.pages, &mut TABLE, authority, 1
96
    );
97
    let shared = try! pages::get(
98
        &state::SHARED.pages, (try! capability::get(&TABLE, progress)).object
99
    );
100
    set PROGRESS = shared.base;
101
    for i in 0 .. cases::ACTORS {
102
        let actor = fixture::create(&mut TABLE, authority, image);
103
        let handle = actor.handle;
104
        let object = actor.domain;
105
        let mut child = try! domains::get(&state::SHARED.domains, object);
106
        let mut rights = abi::ALLOCATE;
107
        if i == cases::CONTROLLER {
108
            set rights |= abi::CREATE;
109
        }
110
        try! capability::install(&mut child.memory.table, capability::Entry {
111
            kind: abi::Kind::Domain, object, rights: abi::Rights(rights)
112
        });
113
        set ACTORS[i] = actor;
114
        let readable = try! pages::grant(
115
            &mut state::SHARED.pages, &TABLE, &mut child.memory.table, payload, abi::READ as u64
116
        );
117
        let writable = try! pages::grant(
118
            &mut state::SHARED.pages,
119
            &TABLE,
120
            &mut child.memory.table,
121
            progress,
122
            (abi::READ | abi::WRITE) as u64
123
        );
124
        if i == cases::CONTROLLER {
125
            set SOURCE = readable;
126
            set VICTIM = try! capability::install(&mut child.memory.table, capability::Entry {
127
                kind: abi::Kind::Domain,
128
                object: ACTORS[cases::FIRST_LOADER].domain,
129
                rights: abi::Rights(abi::DESTROY),
130
            });
131
            fixture::machine(object, completion(), kernelGp());
132
            continue;
133
        }
134
        let moved = fixture::stack(
135
            &mut TABLE, authority, &mut child.memory.table
136
        );
137
        let startup = try! pages::allocate(
138
            &mut state::SHARED.pages, &mut TABLE, authority, 1
139
        );
140
        let granted = try! pages::grant(
141
            &mut state::SHARED.pages, &TABLE, &mut child.memory.table, startup, abi::READ as u64
142
        );
143
        let storage = try! pages::get(
144
            &state::SHARED.pages, (try! capability::get(&TABLE, startup)).object
145
        );
146
        let args = @sliceOf(memory(storage.base) as *mut u64, 4);
147
        set args[0] = *readable;
148
        set args[1] = inputs::PAYLOAD.len as u64;
149
        set args[2] = *writable;
150
        set args[3] = i as u64;
151
        try! fixture::activate(&TABLE, handle, moved, granted, 32);
152
    }
153
    for byte in "runtime actors ready\n" {
154
        put(byte);
155
    }
156
    let now = dispatch::clock();
157
    let start = now + START_DELAY;
158
    set FINAL = start + FINAL_WINDOW_END;
159
    let mut remaining = try! budgets::seed(
160
        &mut state::SHARED.budgets, &mut TABLE, 0, start, FINAL + CHECK_WINDOW
161
    );
162
    let windows = [
163
        Window { actor: cases::FIRST_LOADER, end: FIRST_WINDOW_END },
164
        Window { actor: cases::CONTROLLER,   end: CANCEL_WINDOW_END },
165
        Window { actor: cases::RETRY_LOADER, end: RETRY_WINDOW_END },
166
        Window { actor: cases::WITNESS,      end: WITNESS_WINDOW_END },
167
        Window { actor: cases::RETRY_LOADER, end: FINAL_WINDOW_END },
168
    ];
169
    for window in &windows[..] {
170
        let next = try! budgets::split(
171
            &mut state::SHARED.budgets, &mut TABLE, remaining, start + window.end, now
172
        );
173
        let actor = window.actor;
174
        let bound = try! calls::rebind(
175
            &mut state::SHARED,
176
            &mut TABLE,
177
            budgets::Binding {
178
                budget: remaining, domain: ACTORS[actor].handle, context: ACTORS[actor].context
179
            },
180
            now
181
        );
182
        set remaining = next;
183
    }
184
    let bound = try! calls::rebind(
185
        &mut state::SHARED,
186
        &mut TABLE,
187
        budgets::Binding {
188
            budget: remaining,
189
            domain: ACTORS[cases::CONTROLLER].handle,
190
            context: ACTORS[cases::CONTROLLER].context,
191
        },
192
        now
193
    );
194
}
195
196
/// Verify cancellation, competing admission, progress, and resident code reuse.
197
/// This entry runs only in the controller's reserved machine windows.
198
export unsafe fn verify() {
199
    let loading = checkInterruptedLoad();
200
    checkCompetingLoad();
201
    cancelLoad(&loading);
202
    while dispatch::clock() < FINAL {}
203
    checkPublishedLoad();
204
    finish();
205
}
206
207
/// Snapshot the stopped compiler continuation under the metadata lock.
208
unsafe fn checkInterruptedLoad() -> lifecycle::Loading {
209
    let progress = @sliceOf(memory(PROGRESS) as *mut u64, cases::ACTORS);
210
    let guard = sync::enter();
211
    let first = ACTORS[cases::FIRST_LOADER].context;
212
    for byte in "runtime first progress and owner: " {
213
        put(byte);
214
    }
215
    number(progress[cases::FIRST_LOADER]);
216
    put(32);
217
    if let holder = state::SHARED.loader.owner {
218
        number(abi::id(holder));
219
    }
220
    put(10);
221
    assert progress[cases::FIRST_LOADER] == cases::STARTED
222
        and state::SHARED.loader.owner == ACTORS[cases::FIRST_LOADER].domain;
223
    assert state::SHARED.domains.contexts[first.index].reservation;
224
    let mode = state::SHARED.domains.contexts[first.index].frame.status
225
        & fixture::MACHINE_PRIVILEGE;
226
    assert mode == fixture::MACHINE_PRIVILEGE;
227
    let allocation = state::SHARED.lifecycle.records[first.index].allocation;
228
    let mut loading: lifecycle::Loading = undefined;
229
    match allocation {
230
        case lifecycle::Allocation::Load(value) => {
231
            set loading = value;
232
        }
233
        else => panic "checkInterruptedLoad: loader reservation required",
234
    }
235
    let output = loading.output else panic "checkInterruptedLoad: output required";
236
    sync::leave(guard);
237
    return loading;
238
}
239
240
/// A separate controller call must fail while the stopped compiler owns
241
/// loading.
242
unsafe fn checkCompetingLoad() {
243
    let mut busy = false;
244
    try calls::synchronized(
245
        ACTORS[cases::CONTROLLER].domain,
246
        abi::Operation::ImageLoad as u32,
247
        &[
248
            0, *SOURCE, 0,
249
            inputs::PAYLOAD.len as u64,
250
        ],
251
        dispatch::clock()
252
    ) catch error {
253
        assert error == abi::Error::Busy;
254
        set busy = true;
255
    };
256
    assert busy;
257
    for byte in "runtime competing load rejected\n" {
258
        put(byte);
259
    }
260
}
261
262
/// Destroy the stopped loader and verify unpublished output and frame recovery.
263
/// Each request and inspection uses its own bounded metadata critical section.
264
unsafe fn cancelLoad(loading: &lifecycle::Loading) {
265
    let first = ACTORS[cases::FIRST_LOADER].context;
266
    let output = loading.output else panic "cancelLoad: output required";
267
    let destroyed = try! calls::synchronized(
268
        ACTORS[cases::CONTROLLER].domain,
269
        abi::Operation::DomainDestroy as u32,
270
        &[*VICTIM, 0, 0, 0],
271
        dispatch::clock()
272
    );
273
    assert destroyed == 0;
274
    let checking = sync::enter();
275
    let request = try! dispatch::request(
276
        &mut state::SHARED.remote, 0, remote::Action::Reschedule
277
    );
278
    sync::leave(checking);
279
    let result = try! dispatch::awaitRequest(0, request);
280
    assert result == 0;
281
    let cancelled = sync::enter();
282
    assert state::SHARED.loader.owner == nil;
283
    let active = state::SHARED.lifecycle.active[first.index / fixture::CONTEXT_BITS];
284
    let mask = 1 as u32 << (first.index % fixture::CONTEXT_BITS);
285
    assert (active & mask) == 0;
286
    assert state::SHARED.registry.slots[output.object.index].state
287
        == slots::State::Free;
288
    sync::leave(cancelled);
289
    let mut reclaimed = false;
290
    for attempt in 0 .. RECLAIM_ATTEMPTS {
291
        let guard = sync::enter();
292
        set reclaimed = state::SHARED.pages.backings.pool.retiredCount == 0;
293
        if reclaimed {
294
            sync::leave(guard);
295
            break;
296
        }
297
        let request = try! dispatch::request(
298
            &mut state::SHARED.remote, 0, remote::Action::Reschedule
299
        );
300
        sync::leave(guard);
301
        let result = try! dispatch::awaitRequest(0, request);
302
        assert result == 0;
303
    }
304
    assert reclaimed;
305
    for run in [loader::workspace(&loading.input), output.code, output.metadata] {
306
        let mut offset: u32 = 0;
307
        while offset < run.count {
308
            let mut count = run.count - offset;
309
            if count > FRAME_BATCH {
310
                set count = FRAME_BATCH;
311
            }
312
            let recovered = sync::enter();
313
            for i in run.first + offset .. run.first + offset + count {
314
                assert frames::available(&state::SHARED.pages.backings.pool, i);
315
            }
316
            sync::leave(recovered);
317
            set offset += count;
318
        }
319
    }
320
    for byte in "runtime cancellation passed\n" {
321
        put(byte);
322
    }
323
}
324
325
/// Verify retry progress, resident identity, and execution in a fresh instance.
326
unsafe fn checkPublishedLoad() {
327
    let progress = @sliceOf(memory(PROGRESS) as *mut u64, cases::ACTORS);
328
    let complete = sync::enter();
329
    for byte in "runtime final progress: " {
330
        put(byte);
331
    }
332
    for i in 0 .. cases::ACTORS {
333
        number(progress[i]);
334
        put(32);
335
    }
336
    put(10);
337
    assert progress[cases::FIRST_LOADER] == cases::STARTED;
338
    assert progress[cases::RETRY_LOADER] == cases::COMPLETE;
339
    assert progress[cases::WITNESS] == cases::STARTED;
340
    assert state::SHARED.loader.owner == nil;
341
    let package = try! registry::get(&state::SHARED.registry, abi::reference(
342
        progress[cases::IMAGE_ID]
343
    ));
344
    let entry = package.entry else panic "checkPublishedLoad: published entry required";
345
    let mut controller = try! domains::get(
346
        &state::SHARED.domains, ACTORS[cases::CONTROLLER].domain
347
    );
348
    let image = try! registry::install(
349
        &state::SHARED.registry,
350
        &mut controller.memory.table,
351
        abi::reference(progress[cases::IMAGE_ID])
352
    );
353
    sync::leave(complete);
354
    let handle =
355
        abi::Handle(try! calls::synchronized(
356
            ACTORS[cases::CONTROLLER].domain,
357
            abi::Operation::DomainCreate as u32,
358
            &[0, *image, 0, 0],
359
            dispatch::clock()
360
        ));
361
    let created = sync::enter();
362
    let object =
363
        (try! capability::get(
364
            &controller.memory.table, handle
365
        )).object;
366
    let domain = try! domains::get(&state::SHARED.domains, object);
367
    sync::leave(created);
368
    assert invoke(entry, instances::address(
369
        &state::SHARED.domains.tables[object.index]
370
    )) == 43;
371
    assert invoke(entry, instances::address(
372
        &state::SHARED.domains.tables[object.index]
373
    )) == 85;
374
    for byte in "runtime retry published executable code\n" {
375
        put(byte);
376
    }
377
}