.githooks/
compiler/
kernel/
lib/
scripts/
seed/
sublime/
test/
boot/
bootstrap/
compiler/
cycles/
dispatch/
kernel/
machine.ras
2.0 KiB
spin.rad
11.3 KiB
format/
host/
integration/
kernel/
loader/
manifests/
mmio/
native/
packages/
runtime/
scheduling/
shared/
slots/
smp/
std/
sync/
termination/
tests/
trap/
verify/
Makefile
9.2 KiB
kernel.rad
776 B
runner.rad
11.6 KiB
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
test/dispatch/spin.rad
raw
| 1 | //! User calls, targeted handoff, and timer preemption of busy loops. |
| 2 | export mod abi; |
| 3 | export mod sys; |
| 4 | /// RV64 direct-call intrinsic used to check the machine ABI. |
| 5 | @intrinsic fn ecall(number: u32, a0: i64, a1: i64, a2: i64, a3: i64) -> i64; |
| 6 | @intrinsic fn ecallBuffer( |
| 7 | number: u32, a0: i64, a1: i64, a2: i64, a3: i64, buffer: &mut opaque |
| 8 | ) -> i64; |
| 9 | |
| 10 | /// Query a Page into a buffer that is smaller than the record. |
| 11 | fn shortQuery(handle: i64) -> i64 { |
| 12 | let mut one: [u64; 1] = [0; 1]; |
| 13 | return ecallBuffer(80, handle, 0, 0, 0, &mut one); |
| 14 | } |
| 15 | /// Private query output used to check writable package-state authority. |
| 16 | unsafe static INFO: [u64; 2] = [0; 2]; |
| 17 | /// Private progress counter observed by the machine fixture. |
| 18 | export unsafe static count: u64 = 0; |
| 19 | /// Additional-context progress in the same private package state. |
| 20 | export unsafe static extraCount: u64 = 0; |
| 21 | /// Successful user-side return and destruction checks. |
| 22 | export unsafe static destroyed: u64 = 0; |
| 23 | /// Generation-bearing identity of the child that returns normally. |
| 24 | export unsafe static childId: u64 = 0; |
| 25 | /// Local self-Domain authority shared by both user contexts. |
| 26 | unsafe static SELF: abi::Handle = abi::Handle(0); |
| 27 | |
| 28 | /// Update shared private state and return from an additional context. |
| 29 | export unsafe fn worker(args: u64, size: u64) { |
| 30 | assert args == 0 and size == 0; |
| 31 | let current = try! sys::queryContext(SELF, abi::CURRENT_CONTEXT); |
| 32 | let initial = try! sys::queryContext(SELF, 0); |
| 33 | assert current.context <> initial.context and current.budget <> 0; |
| 34 | assert initial.budget == 0; |
| 35 | set extraCount += 1; |
| 36 | // A quarter of the span of the fixture's budget windows. |
| 37 | try! sys::timeout(8000000, 77); |
| 38 | sys::wait(); |
| 39 | set extraCount += 1; |
| 40 | set count += 1; |
| 41 | } |
| 42 | |
| 43 | /// Reach a page and the event ring as slices: the rights of the handle decide the access. |
| 44 | fn pageSlices(domain: abi::Handle, events: abi::Handle, allocated: abi::Handle) { |
| 45 | // The memory of a page is reached as a slice, with the rights of the handle. |
| 46 | let writable = try! sys::pageBytesMut(allocated); |
| 47 | assert writable.len == 4096; |
| 48 | set writable[0] = 7; |
| 49 | set writable[4095] = 9; |
| 50 | let narrowed = try! sys::capabilityGrant(allocated, abi::Handle(0), abi::READ as u64); |
| 51 | let seen = try! sys::pageBytes(narrowed); |
| 52 | assert seen.len == 4096 and seen[0] == 7 and seen[4095] == 9; |
| 53 | let mut rejected = false; |
| 54 | try sys::pageBytesMut(narrowed) catch error { |
| 55 | assert error == abi::Error::Denied; |
| 56 | set rejected = true; |
| 57 | }; |
| 58 | assert rejected; |
| 59 | set rejected = false; |
| 60 | try sys::pageBytes(domain) catch error { |
| 61 | assert error == abi::Error::BadHandle; |
| 62 | set rejected = true; |
| 63 | }; |
| 64 | assert rejected; |
| 65 | let ring = try! sys::pageBytesMut(events); |
| 66 | assert ring.len > 0; |
| 67 | try! sys::capabilityDrop(narrowed); |
| 68 | } |
| 69 | |
| 70 | /// The allocation error, which the compiler knows. |
| 71 | @language union AllocError: Copy { OutOfMemory } |
| 72 | |
| 73 | /// The allocator view of a heap, which the compiler knows. |
| 74 | @language record Allocator {} |
| 75 | |
| 76 | /// A record that holds a pointer, which only a heap can provide. |
| 77 | record Item { |
| 78 | /// Owned pointer to a number. |
| 79 | p: *u32, |
| 80 | } |
| 81 | |
| 82 | /// Numbers that the items point to. |
| 83 | static NUMBERS: [u32; 2] = [20, 22]; |
| 84 | |
| 85 | /// A Page becomes a heap that grows slices of pointers, and nothing reaches its memory again. |
| 86 | fn pageHeaps() { |
| 87 | let page = try! sys::pageAllocate(abi::Handle(0), 1); |
| 88 | let touched = try! sys::pageAllocate(abi::Handle(0), 1); |
| 89 | let bytes = try! sys::pageBytesMut(touched); |
| 90 | set bytes[0] = 1; |
| 91 | let mut refused = false; |
| 92 | try sys::heapFromPage(touched) catch error { |
| 93 | assert error == abi::Error::Denied; |
| 94 | set refused = true; |
| 95 | }; |
| 96 | assert refused; |
| 97 | try! sys::capabilityDrop(touched); |
| 98 | |
| 99 | let heap = try! sys::heapFromPage(page); |
| 100 | let mut items: *mut [Item] = &mut []; |
| 101 | for i in 0 .. 2 { |
| 102 | try! items.append(Item { p: &NUMBERS[i] }, heap); |
| 103 | } |
| 104 | assert *items[0].p + *items[1].p == 42; |
| 105 | // The handle is consumed, and the memory is no longer a page. |
| 106 | set refused = false; |
| 107 | try sys::pageBytes(page) catch error { |
| 108 | assert error == abi::Error::BadHandle; |
| 109 | set refused = true; |
| 110 | }; |
| 111 | assert refused; |
| 112 | set refused = false; |
| 113 | try sys::heapFromPage(page) catch error { |
| 114 | assert error == abi::Error::BadHandle; |
| 115 | set refused = true; |
| 116 | }; |
| 117 | assert refused; |
| 118 | // Growth past the page throws. |
| 119 | let mut big: *mut [Item] = &mut []; |
| 120 | try big.reserve(1000, heap) catch error { |
| 121 | assert error == AllocError::OutOfMemory; |
| 122 | return; |
| 123 | }; |
| 124 | assert false; |
| 125 | } |
| 126 | |
| 127 | /// Check Page, Events, Domain and Image queries and Page lifecycle calls. |
| 128 | unsafe fn basics( |
| 129 | query: abi::Handle, domainHandle: abi::Handle, events: abi::Handle, package: abi::Handle |
| 130 | ) { |
| 131 | let info = sys::queryPage(query); |
| 132 | assert info.base <> 0 and info.count == 1; |
| 133 | let mut rejected = false; |
| 134 | try sys::pageAllocate(abi::Handle(0), 0) catch error { |
| 135 | assert error == abi::Error::InvalidArg; |
| 136 | set rejected = true; |
| 137 | }; |
| 138 | assert rejected; |
| 139 | let allocated = try! sys::pageAllocate(abi::Handle(0), 1); |
| 140 | assert sys::queryPage(allocated).count == 1; |
| 141 | pageSlices(domainHandle, events, allocated); |
| 142 | pageHeaps(); |
| 143 | let narrowed = try! sys::capabilityGrant(allocated, abi::Handle(0), abi::READ as u64); |
| 144 | try! sys::capabilityDrop(narrowed); |
| 145 | try! sys::capabilityDrop(allocated); |
| 146 | assert sys::queryEvents(events).capacity == 256; |
| 147 | let domain = sys::queryDomain(domainHandle); |
| 148 | assert domain.id <> 0 and domain.state == 1; |
| 149 | let image = try! sys::queryImage(package); |
| 150 | assert image.entry <> 0 and image.codeBase <> 0 and image.codeBytes > 0 and image.dataBytes > 0; |
| 151 | } |
| 152 | |
| 153 | /// Split the future window into the windows of the extra context and the child, and bind them. |
| 154 | unsafe fn partition( |
| 155 | future: abi::Handle, domainHandle: abi::Handle, extra: u64, child: abi::Handle |
| 156 | ) { |
| 157 | let later = try! sys::queryBudget(future); |
| 158 | let tail = try! sys::budgetSplit(future, later.start + (later.end - later.start) / 2); |
| 159 | let childWindow = try! sys::budgetSplit(tail, later.start + (later.end - later.start) * 5 / 8); |
| 160 | let resumption = try! sys::budgetSplit( |
| 161 | childWindow, later.start + (later.end - later.start) * 11 / 16 |
| 162 | ); |
| 163 | let awakened = try! sys::budgetSplit( |
| 164 | resumption, later.start + (later.end - later.start) * 3 / 4 |
| 165 | ); |
| 166 | try! sys::budgetSplit( |
| 167 | awakened, later.start + (later.end - later.start) * 13 / 16 |
| 168 | ); |
| 169 | let bound = try! sys::budgetBind(tail, domainHandle, extra); |
| 170 | try! sys::budgetBind(awakened, SELF, extra); |
| 171 | let initial = try! sys::queryContext(child, 0); |
| 172 | try! sys::budgetBind(childWindow, child, initial.context); |
| 173 | } |
| 174 | |
| 175 | /// Check the error boundary of raw calls and buffers. |
| 176 | unsafe fn rawCalls(handle: i64) { |
| 177 | let mut local: [u64; 2] = [0; 2]; |
| 178 | assert ecallBuffer(80, handle, 0, 0, 0, &mut local) == 0; |
| 179 | assert local[0] <> 0 and local[1] == 1; |
| 180 | assert ecallBuffer(80, handle, 0, 0, 0, &mut INFO) == 0; |
| 181 | assert INFO[0] == local[0] and INFO[1] == local[1]; |
| 182 | assert shortQuery(handle) == -4; |
| 183 | assert ecall(80, handle, 0, 0, 0) == -4; |
| 184 | assert ecallBuffer(80, handle, 4096, 0, 0, &mut local) == -4; |
| 185 | assert ecall(0xffffffff, 0, 0, 0, 0) == -4; |
| 186 | } |
| 187 | |
| 188 | /// Check direct calls and handoff, then exercise independently bound contexts. |
| 189 | @default unsafe fn main(env: *abi::Env) { |
| 190 | if env.argsSize == 0 { |
| 191 | try! sys::timeout(10000, 88); |
| 192 | sys::wait(); |
| 193 | return; |
| 194 | } |
| 195 | assert env.argsSize == 40; |
| 196 | // The arguments Page holds a Page capability, the resident package, the self-Domain |
| 197 | // capability, the future window, and the completion checker. |
| 198 | let startup = try! sys::pageWords(abi::Handle(env.args)); |
| 199 | let query = startup[0]; |
| 200 | let package = abi::Handle(startup[1]); |
| 201 | let domainHandle = abi::Handle(startup[2]); |
| 202 | let futureHandle = abi::Handle(startup[3]); |
| 203 | let parentHandle = abi::Handle(startup[4]); |
| 204 | let events = abi::Handle(env.eventsHandle); |
| 205 | set SELF = domainHandle; |
| 206 | let handle = query as i64; |
| 207 | basics(abi::Handle(query), domainHandle, events, package); |
| 208 | let domain = sys::queryDomain(domainHandle); |
| 209 | let mut rejected = false; |
| 210 | let child = try! sys::domainCreate(abi::Handle(0), package); |
| 211 | set childId = sys::queryDomain(child).id; |
| 212 | try! sys::domainReparent(child, parentHandle); |
| 213 | let stack = try! sys::pageAllocate(abi::Handle(0), 1); |
| 214 | let childStack = try! sys::capabilityTransfer(stack, child, (abi::READ | abi::WRITE) as u64); |
| 215 | try! sys::domainActivate(child, childStack, abi::Handle(0), 0); |
| 216 | assert sys::queryDomain(child).state == 1; |
| 217 | set rejected = false; |
| 218 | try sys::domainActivate(child, childStack, abi::Handle(0), 0) catch error { |
| 219 | assert error == abi::Error::NotPending; |
| 220 | set rejected = true; |
| 221 | }; |
| 222 | assert rejected; |
| 223 | set rejected = false; |
| 224 | try sys::imageLoad(abi::Handle(0), abi::Handle(query), 0, 0) catch error { |
| 225 | assert error == abi::Error::InvalidArg; |
| 226 | set rejected = true; |
| 227 | }; |
| 228 | assert rejected; |
| 229 | let current = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)); |
| 230 | assert current.context <> 0 and current.hart == 0 and current.budget <> 0; |
| 231 | let initial = try! sys::queryContext(domainHandle, 0); |
| 232 | assert initial.context == current.context and initial.budget == 0; |
| 233 | set rejected = false; |
| 234 | try sys::queryContext(child, abi::CURRENT_CONTEXT) catch error { |
| 235 | assert error == abi::Error::InvalidArg; |
| 236 | set rejected = true; |
| 237 | }; |
| 238 | assert rejected; |
| 239 | let window = try! sys::queryBudget(abi::Handle(current.budget)); |
| 240 | assert window.context == current.context |
| 241 | and window.hart == current.hart |
| 242 | and window.start < window.end; |
| 243 | let extraStack = try! sys::pageAllocate(abi::Handle(0), 1); |
| 244 | let extraMemory = sys::queryPage(extraStack); |
| 245 | let extra = try! sys::contextCreate(domainHandle, abi::ContextStart { |
| 246 | entry: worker as u64, stack: *extraStack, args: 0, size: 0 |
| 247 | }); |
| 248 | let context = try! sys::queryContext(domainHandle, extra); |
| 249 | assert context.context == extra and context.domain == domain.id; |
| 250 | assert context.stackBase == extraMemory.base and context.stackTop == extraMemory.base + 4096; |
| 251 | let future = futureHandle; |
| 252 | partition(future, domainHandle, extra, child); |
| 253 | rawCalls(handle); |
| 254 | set rejected = false; |
| 255 | try sys::yield(SELF) catch error { |
| 256 | assert error == abi::Error::Busy; |
| 257 | set rejected = true; |
| 258 | }; |
| 259 | let after = try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT); |
| 260 | assert rejected and after.budget == current.budget; |
| 261 | set count = 1; |
| 262 | try! sys::yield(child); |
| 263 | let continued = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)); |
| 264 | assert continued.context == current.context and continued.budget == *future; |
| 265 | while extraCount == 0 { |
| 266 | set count += 1; |
| 267 | } |
| 268 | let waiting = try! sys::queryContext(SELF, extra); |
| 269 | assert waiting.state == 2 and waiting.hart == 0xffffffffffffffff; |
| 270 | try! sys::wakeup(SELF, 123); |
| 271 | assert extraCount == 1 or extraCount == 2; |
| 272 | while extraCount == 1 { |
| 273 | set count += 1; |
| 274 | } |
| 275 | let stopped = try! sys::queryContext(domainHandle, extra); |
| 276 | assert stopped.state == 1 and stopped.hart == 0xffffffffffffffff; |
| 277 | try! sys::contextDestroy(domainHandle, extra); |
| 278 | set rejected = false; |
| 279 | try sys::queryContext(domainHandle, extra) catch error { |
| 280 | assert error == abi::Error::BadHandle; |
| 281 | set rejected = true; |
| 282 | }; |
| 283 | assert rejected; |
| 284 | set destroyed = 1; |
| 285 | while true { |
| 286 | set count += 1; |
| 287 | } |
| 288 | } |