test/kernel/fixture.rad 6.1 KiB raw
1
//! Shared setup for machine-test domains.
2
3
use std::arch::rv64;
4
use kernel::abi;
5
use kernel::backing;
6
use kernel::budgets;
7
use kernel::calls;
8
use kernel::capability;
9
use kernel::domains;
10
use kernel::limits;
11
use kernel::loader;
12
use kernel::pages;
13
use kernel::physical;
14
use kernel::slots;
15
use kernel::state;
16
17
/// Bytes in one 64-bit argument or register value.
18
export constant WORD_BYTES: u32 = 8;
19
/// Bits in each lifecycle continuation-ownership word.
20
export constant CONTEXT_BITS: u32 = 32;
21
/// Machine privilege in the saved mstatus MPP field.
22
export constant MACHINE_PRIVILEGE: u64 = 0x1800;
23
/// Machine privilege, interrupt-enable state and vector state restored by mret.
24
export constant MACHINE_STATUS: u64 =
25
    MACHINE_PRIVILEGE | rv64::MSTATUS_MPIE | rv64::MSTATUS_VS_INITIAL;
26
27
/// A pending domain and its initial execution context.
28
export record Actor: Copy {
29
    /// Execute capability in the fixture's bootstrap table.
30
    handle: abi::Handle,
31
    /// Generation-bearing domain identity.
32
    domain: abi::Ref,
33
    /// Initial context identity.
34
    context: abi::Ref,
35
}
36
37
/// Initialize bootstrap authority before any hart can access fixture resources.
38
export unsafe fn bootstrap(table: &mut capability::Table) -> abi::Handle {
39
    let pending = try! slots::reserve(&mut state::SHARED.domains.slots[..]);
40
    let owner = try! slots::commit(
41
        &mut state::SHARED.domains.slots[..], pending
42
    );
43
    capability::initialize(table, owner);
44
    try! backing::registerDomain(&mut state::SHARED.pages.backings, owner);
45
    return try! capability::install(table, capability::Entry {
46
        kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::CREATE | abi::ALLOCATE)
47
    });
48
}
49
50
/// Copy package bytes to a fresh bootstrap-owned page allocation. The caller
51
/// must have exclusive access to shared page metadata.
52
export unsafe fn input(
53
    table: &mut capability::Table, authority: abi::Handle, bytes: &[u8]
54
) -> abi::Handle {
55
    let handle = try! pages::allocate(
56
        &mut state::SHARED.pages,
57
        table,
58
        authority,
59
        (bytes.len as u64 + limits::FRAME_SIZE - 1) / limits::FRAME_SIZE
60
    );
61
    let page = try! pages::get(
62
        &state::SHARED.pages, (try! capability::get(table, handle)).object
63
    );
64
    try! physical::write(&state::SHARED.pages.backings.pool, page.base, bytes);
65
    return handle;
66
}
67
68
/// Load a fixture package before dispatch starts.
69
export unsafe fn load(
70
    table: &mut capability::Table, authority: abi::Handle, bytes: &[u8]
71
) -> abi::Handle {
72
    let source = input(table, authority, bytes);
73
    return try! loader::load(
74
        &mut state::SHARED.loader,
75
        &mut state::SHARED.pages,
76
        &mut state::SHARED.registry,
77
        table,
78
        loader::Request {
79
            authority, source, offset: 0, length: bytes.len as u64
80
        }
81
    );
82
}
83
84
/// Create a pending fixture actor before dispatch starts.
85
export unsafe fn create(
86
    table: &mut capability::Table, authority: abi::Handle, image: abi::Handle
87
) -> Actor {
88
    let handle = try! domains::create(
89
        &mut state::SHARED.domains,
90
        &mut state::SHARED.pages.backings,
91
        &state::SHARED.registry,
92
        table,
93
        authority,
94
        image
95
    );
96
    let domain = (try! capability::get(table, handle)).object;
97
    let initial = (try! domains::get(&state::SHARED.domains, domain)).initial;
98
    return Actor { handle, domain, context: initial };
99
}
100
101
/// Prepare a machine-mode checker or worker before dispatch starts.
102
export unsafe fn machine(domain: abi::Ref, entry: u64, gp: u64) {
103
    let object = try! domains::get(&state::SHARED.domains, domain);
104
    set state::SHARED.domains.records[domain.index].state =
105
        domains::Lifecycle::Active;
106
    let context = &mut state::SHARED.domains.contexts[object.initial.index];
107
    set context.frame.pc = entry;
108
    set context.frame.status = MACHINE_STATUS;
109
    set context.frame.registers[*rv64::SP as u32] = context.kernelStack.end;
110
    set context.frame.registers[*rv64::GP as u32] = gp;
111
}
112
113
/// Bind disjoint child windows and a final checker window on hart zero. The
114
/// caller runs this before dispatch starts. Times use machine timer ticks.
115
export unsafe fn schedule(
116
    table: &mut capability::Table, actors: &[Actor], checker: Actor, start: u64, span: u64, now: u64
117
) {
118
    let mut remaining = try! budgets::seed(
119
        &mut state::SHARED.budgets, table, 0, start, start + (actors.len as u64 + 1) * span
120
    );
121
    for actor, i in actors {
122
        let next = try! budgets::split(
123
            &mut state::SHARED.budgets, table, remaining, start + (i as u64 + 1) * span, now
124
        );
125
        let bound = try! calls::rebind(
126
            &mut state::SHARED,
127
            table,
128
            budgets::Binding {
129
                budget: remaining, domain: actor.handle, context: actor.context
130
            },
131
            now
132
        );
133
        set remaining = next;
134
    }
135
    let bound = try! calls::rebind(
136
        &mut state::SHARED,
137
        table,
138
        budgets::Binding {
139
            budget: remaining, domain: checker.handle, context: checker.context
140
        },
141
        now
142
    );
143
}
144
145
/// Allocate a writable stack in a child table before dispatch starts.
146
export unsafe fn stack(
147
    table: &mut capability::Table, authority: abi::Handle, child: &mut capability::Table
148
) -> abi::Handle {
149
    let page = try! pages::allocate(
150
        &mut state::SHARED.pages, table, authority, 1
151
    );
152
    return try! pages::transfer(
153
        &mut state::SHARED.pages, table, child, page, (abi::READ | abi::WRITE) as u64
154
    );
155
}
156
157
/// Activate the domain that an Execute capability in `table` names. The caller
158
/// must have exclusive access to the shared domain and page stores.
159
export unsafe fn activate(
160
    table: &capability::Table, handle: abi::Handle, stack: abi::Handle, args: abi::Handle, size: u64
161
) throws (abi::Error) {
162
    let entry = try capability::lookup(table, handle, abi::Kind::Domain, abi::Rights(
163
        abi::EXECUTE
164
    ));
165
    let domain = try domains::get(&state::SHARED.domains, entry.object);
166
    try domains::activate(
167
        &mut state::SHARED.domains,
168
        &mut state::SHARED.pages,
169
        entry.object,
170
        &mut *domain.memory,
171
        stack,
172
        args,
173
        size
174
    );
175
}