kernel: Implement the RV64 capability runtime
22d4bde6c6f80a95d1828971e3ca32317fc79e7256df4591ba1e445d5766bdbc
Boot from platform data and run isolated domains with capability tables, physical memory ownership, verified runtime package loading, and bounded object storage. Provide context dispatch, time-window budgets, interrupts, system calls, cross-hart transactions, and user-space scheduling. Track domain lifetimes, parenting, events, waits, and deferred cleanup. Use checked physical access tokens and hart-local state at the hardware boundary. Test the runtime through unit tests and production machine fixtures, including execution with one, two, and eight harts. Assisted-by: Codex:gpt-6
1 parent
b25a4634
Makefile
+1 -0
| 30 | 30 | @$(HOSTED) $(SEED) $(SEED_OPTS) -o $@ |
|
| 31 | 31 | ||
| 32 | 32 | $(BIN_DIR): |
|
| 33 | 33 | @mkdir -p $@ |
|
| 34 | 34 | ||
| 35 | + | include kernel/Makefile |
|
| 35 | 36 | include test/Makefile |
|
| 36 | 37 | ||
| 37 | 38 | # Format the standard library and kernel sources in place. Set `FMT_SRC` to |
|
| 38 | 39 | # format other files. |
|
| 39 | 40 | FMT_SRC ?= $(STD_TEST_LIB) $(shell find kernel -name '*.rad' | sort) |
kernel/Makefile
added
+33 -0
| 1 | + | # Kernel artifacts. Run from the repository root. |
|
| 2 | + | ||
| 3 | + | # Kernel modules and tests use a separate package and test entry point. |
|
| 4 | + | KERNEL_MODULES := $(filter-out kernel/kernel/tests.rad, \ |
|
| 5 | + | $(wildcard kernel/kernel/*.rad)) |
|
| 6 | + | KERNEL_SRC := kernel/kernel.rad $(KERNEL_MODULES) |
|
| 7 | + | KERNEL_ASM := kernel/kernel/boot.ras kernel/kernel/physical.ras \ |
|
| 8 | + | kernel/kernel/sync.ras kernel/kernel/trap.ras kernel/kernel/loader.ras \ |
|
| 9 | + | kernel/kernel/domains.ras kernel/kernel/dispatch.ras kernel/kernel/registers.ras |
|
| 10 | + | KERNEL_TEST_SRC := kernel/kernel/tests.rad $(wildcard kernel/kernel/tests/*.rad) |
|
| 11 | + | KERNEL := -pkg kernel $(patsubst %,-mod %,$(sort $(KERNEL_SRC))) |
|
| 12 | + | KERNEL_TEST := $(BIN_DIR)/kernel.test.rv64 |
|
| 13 | + | ||
| 14 | + | $(KERNEL_TEST): $(KERNEL_SRC) $(KERNEL_TEST_SRC) $(KERNEL_ASM) \ |
|
| 15 | + | $(STD_TEST_LIB) std.lib.test $(RAD_BIN) kernel/Makefile |
|
| 16 | + | @echo "radiance kernel tests => $@" |
|
| 17 | + | @$(RADIANCE) -test $(STD_TEST) $(KERNEL) $(patsubst %,-mod %,$(KERNEL_TEST_SRC) \ |
|
| 18 | + | $(KERNEL_ASM)) -entry kernel -o $@ |
|
| 19 | + | ||
| 20 | + | # Native kernel image. |
|
| 21 | + | $(BIN_DIR)/kernel.ril $(BIN_DIR)/std.ril &: $(KERNEL_SRC) $(STD_LIB) \ |
|
| 22 | + | $(RAD_BIN) kernel/Makefile |
|
| 23 | + | @$(RADIANCE) $(STD) $(KERNEL) -entry kernel -ril $(BIN_DIR) |
|
| 24 | + | ||
| 25 | + | $(BIN_DIR)/kernel.build.rv64: kernel/tools/build.rad $(STD_LIB) $(RAD_BIN) \ |
|
| 26 | + | kernel/Makefile |
|
| 27 | + | @$(RADIANCE) $(STD) -pkg build -mod $< -entry build -o $@ |
|
| 28 | + | ||
| 29 | + | $(BIN_DIR)/kernel.rv64: $(BIN_DIR)/std.ril $(BIN_DIR)/kernel.ril \ |
|
| 30 | + | $(BIN_DIR)/kernel.build.rv64 $(KERNEL_ASM) kernel/Makefile |
|
| 31 | + | @cat $(KERNEL_ASM) > $(BIN_DIR)/kernel.ras |
|
| 32 | + | @$(HOSTED) $(BIN_DIR)/kernel.build.rv64 -- $(BIN_DIR)/std.ril \ |
|
| 33 | + | $(BIN_DIR)/kernel.ril $(BIN_DIR)/kernel.ras $@ |
kernel/kernel.rad
added
+40 -0
| 1 | + | //! Kernel resource management and machine execution. |
|
| 2 | + | ||
| 3 | + | @test use std::testing; |
|
| 4 | + | ||
| 5 | + | export mod fdt; |
|
| 6 | + | export mod abi; |
|
| 7 | + | export mod limits; |
|
| 8 | + | export mod slots; |
|
| 9 | + | export mod range; |
|
| 10 | + | export mod sync; |
|
| 11 | + | export mod platform; |
|
| 12 | + | export mod trap; |
|
| 13 | + | export mod capability; |
|
| 14 | + | export mod transactions; |
|
| 15 | + | export mod events; |
|
| 16 | + | export mod interrupts; |
|
| 17 | + | export mod devices; |
|
| 18 | + | export mod registers; |
|
| 19 | + | export mod plic; |
|
| 20 | + | export mod registry; |
|
| 21 | + | export mod frames; |
|
| 22 | + | export mod physical; |
|
| 23 | + | export mod backing; |
|
| 24 | + | export mod pages; |
|
| 25 | + | export mod loader; |
|
| 26 | + | export mod instances; |
|
| 27 | + | export mod domains; |
|
| 28 | + | export mod lifecycle; |
|
| 29 | + | export mod budgets; |
|
| 30 | + | export mod dispatch; |
|
| 31 | + | export mod timers; |
|
| 32 | + | export mod remote; |
|
| 33 | + | export mod state; |
|
| 34 | + | export mod locked; |
|
| 35 | + | export mod access; |
|
| 36 | + | export mod calls; |
|
| 37 | + | export mod sys; |
|
| 38 | + | export mod boot; |
|
| 39 | + | export mod startup; |
|
| 40 | + | @test export mod tests; |
kernel/kernel/abi.rad
added
+383 -0
| 1 | + | //! Kernel handle representation, rights, errors, and operation numbers. |
|
| 2 | + | ||
| 3 | + | /// Low 24 bits used for the handle slot index. |
|
| 4 | + | constant HANDLE_INDEX_MASK: u64 = 0xffffff; |
|
| 5 | + | /// Handle bits reserved between index and generation. |
|
| 6 | + | constant HANDLE_RESERVED_MASK: u64 = 0x0f000000; |
|
| 7 | + | /// Bit offset of the handle kind. |
|
| 8 | + | constant HANDLE_KIND_SHIFT: u64 = 60; |
|
| 9 | + | /// Bit offset of the handle generation. |
|
| 10 | + | constant HANDLE_GENERATION_SHIFT: u64 = 28; |
|
| 11 | + | ||
| 12 | + | /// Domain-relative capability value. Zero is the self sentinel where allowed. |
|
| 13 | + | export record Handle(u64): Copy; |
|
| 14 | + | /// Rights granted by a live capability-table entry. |
|
| 15 | + | export record Rights(u16): Copy; |
|
| 16 | + | ||
| 17 | + | /// Startup values of a protection domain. All fields are plain data: the arguments and the |
|
| 18 | + | /// event ring are reached through Page access on a handle, not through an address. |
|
| 19 | + | export record Env: Copy { |
|
| 20 | + | /// Domain-relative Page handle of the startup arguments, or zero. |
|
| 21 | + | args: u64, |
|
| 22 | + | /// Number of startup argument bytes in the Page. |
|
| 23 | + | argsSize: u64, |
|
| 24 | + | /// Installed domain-relative Events handle. |
|
| 25 | + | eventsHandle: u64, |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | /// Maximum number of boot handles: the self and Events slots, one slot per Device and |
|
| 29 | + | /// Interrupt, and one Budget per hart. It equals 2 + DEVICES + INTERRUPTS + HARTS from the |
|
| 30 | + | /// kernel's limits, which user packages cannot import; the kernel asserts the equality at startup. |
|
| 31 | + | export constant BOOT_HANDLES: u32 = 266; |
|
| 32 | + | ||
| 33 | + | /// Initial root resources copied into root-owned startup memory. |
|
| 34 | + | export record BootInfo: Copy { |
|
| 35 | + | /// Number of Device handles after the self and Events slots. |
|
| 36 | + | devices: u64, |
|
| 37 | + | /// Number of Interrupt handles following the Device handles. |
|
| 38 | + | interrupts: u64, |
|
| 39 | + | /// Bit mask of online harts, whose Budget handles follow in hart order. |
|
| 40 | + | harts: u64, |
|
| 41 | + | /// Machine timer ticks per second. |
|
| 42 | + | timebase: u64, |
|
| 43 | + | /// Image used to construct root and additional instances of its program. |
|
| 44 | + | rootImage: Handle, |
|
| 45 | + | /// Initial user-space scheduler image. |
|
| 46 | + | schedulerImage: Handle, |
|
| 47 | + | /// Read-only machine-time device, or zero when absent. |
|
| 48 | + | clock: Handle, |
|
| 49 | + | /// Platform shutdown device, or zero when absent. |
|
| 50 | + | shutdown: Handle, |
|
| 51 | + | /// Page that owns this header and the copied handle array. |
|
| 52 | + | arguments: Handle, |
|
| 53 | + | /// Number of valid entries in `handles`. |
|
| 54 | + | count: u64, |
|
| 55 | + | /// Copied capability values in their initial slot order. Entries from `count` on are zero. |
|
| 56 | + | handles: [u64; BOOT_HANDLES], |
|
| 57 | + | } |
|
| 58 | + | ||
| 59 | + | /// Physical extent returned by QueryPage. |
|
| 60 | + | export record PageInfo: Copy { |
|
| 61 | + | /// Address of the first physical frame. |
|
| 62 | + | base: u64, |
|
| 63 | + | /// Number of contiguous physical frames. |
|
| 64 | + | count: u64, |
|
| 65 | + | } |
|
| 66 | + | ||
| 67 | + | /// Physical MMIO extent returned by QueryDevice. |
|
| 68 | + | export record DeviceInfo: Copy { |
|
| 69 | + | /// Physical base address of the register region. |
|
| 70 | + | base: u64, |
|
| 71 | + | /// Size of the register region in bytes. |
|
| 72 | + | size: u64, |
|
| 73 | + | } |
|
| 74 | + | ||
| 75 | + | /// Physical source returned by QueryInterrupt. |
|
| 76 | + | export record InterruptInfo: Copy { |
|
| 77 | + | /// PLIC source number; zero does not name an interrupt. |
|
| 78 | + | number: u32, |
|
| 79 | + | } |
|
| 80 | + | ||
| 81 | + | /// Domain identity and lifetime returned by QueryDomain. |
|
| 82 | + | export record DomainInfo: Copy { |
|
| 83 | + | /// Generation-bearing domain identity. |
|
| 84 | + | id: u64, |
|
| 85 | + | /// Historical creator identity. |
|
| 86 | + | creator: u64, |
|
| 87 | + | /// Current parent identity, or zero if absent. |
|
| 88 | + | parent: u64, |
|
| 89 | + | /// Pending, Active, or Dead encoded as zero, one, or two. |
|
| 90 | + | state: u64, |
|
| 91 | + | } |
|
| 92 | + | ||
| 93 | + | /// Shared notification queue shape returned by QueryEvents. |
|
| 94 | + | export record EventsInfo: Copy { |
|
| 95 | + | /// Number of entries in the installed ring. |
|
| 96 | + | capacity: u64, |
|
| 97 | + | } |
|
| 98 | + | ||
| 99 | + | /// Shared code and private state extents returned by QueryImage. |
|
| 100 | + | export record ImageInfo: Copy { |
|
| 101 | + | /// Generation-bearing resident package identity. |
|
| 102 | + | id: u64, |
|
| 103 | + | /// Default native entry, or zero if absent. |
|
| 104 | + | entry: u64, |
|
| 105 | + | /// Address of shared executable code. |
|
| 106 | + | codeBase: u64, |
|
| 107 | + | /// Number of shared executable bytes. |
|
| 108 | + | codeBytes: u64, |
|
| 109 | + | /// Number of private state bytes per instance. |
|
| 110 | + | dataBytes: u64, |
|
| 111 | + | } |
|
| 112 | + | ||
| 113 | + | /// Entry, stack and arguments of an additional context. |
|
| 114 | + | export record ContextStart: Copy { |
|
| 115 | + | /// Exported function of the target domain's package graph, other than a package entry. |
|
| 116 | + | entry: u64, |
|
| 117 | + | /// Handle in the target's table of a writable Page that no one else holds and that was |
|
| 118 | + | /// never accessed. The context consumes it for its stack. |
|
| 119 | + | stack: u64, |
|
| 120 | + | /// First argument, passed in a0 as data. |
|
| 121 | + | args: u64, |
|
| 122 | + | /// Second argument, passed in a1 as data. |
|
| 123 | + | size: u64, |
|
| 124 | + | } |
|
| 125 | + | ||
| 126 | + | /// Select the calling context in a context query. |
|
| 127 | + | export constant CURRENT_CONTEXT: u64 = 0xffffffffffffffff; |
|
| 128 | + | ||
| 129 | + | /// Identity, owner, and user stack bounds of a live context. |
|
| 130 | + | export record ContextInfo: Copy { |
|
| 131 | + | /// Generation-bearing context identity. |
|
| 132 | + | context: u64, |
|
| 133 | + | /// Owning domain identity. |
|
| 134 | + | domain: u64, |
|
| 135 | + | /// First byte of the reserved user stack. |
|
| 136 | + | stackBase: u64, |
|
| 137 | + | /// Exclusive upper bound of the reserved user stack. |
|
| 138 | + | stackTop: u64, |
|
| 139 | + | /// Ready, Stopped, or Waiting encoded as zero, one, or two. |
|
| 140 | + | state: u64, |
|
| 141 | + | /// Executing hart, or all ones while no hart owns execution. |
|
| 142 | + | hart: u64, |
|
| 143 | + | /// Calling context budget handle, or zero for another context. |
|
| 144 | + | budget: u64, |
|
| 145 | + | } |
|
| 146 | + | ||
| 147 | + | /// Identity and CPU authority of the calling execution context. |
|
| 148 | + | export record CurrentContextInfo: Copy { |
|
| 149 | + | /// Generation-bearing context identity. |
|
| 150 | + | context: u64, |
|
| 151 | + | /// Physical hart executing this call. |
|
| 152 | + | hart: u64, |
|
| 153 | + | /// Domain-relative handle for the executing budget. |
|
| 154 | + | budget: u64, |
|
| 155 | + | } |
|
| 156 | + | ||
| 157 | + | /// Exclusive CPU authority returned by QueryBudget. |
|
| 158 | + | export record BudgetInfo: Copy { |
|
| 159 | + | /// Physical hart authorized by this window. |
|
| 160 | + | hart: u64, |
|
| 161 | + | /// Inclusive first mtime tick. |
|
| 162 | + | start: u64, |
|
| 163 | + | /// Exclusive last mtime tick. |
|
| 164 | + | end: u64, |
|
| 165 | + | /// Generation-bearing context identity, or zero if unbound. |
|
| 166 | + | context: u64, |
|
| 167 | + | } |
|
| 168 | + | ||
| 169 | + | /// Read object contents or metadata. |
|
| 170 | + | export constant READ: u16 = 1; |
|
| 171 | + | /// Modify object contents. |
|
| 172 | + | export constant WRITE: u16 = 2; |
|
| 173 | + | /// Execute an image or entry. |
|
| 174 | + | export constant EXECUTE: u16 = 4; |
|
| 175 | + | /// Grant an attenuated capability. |
|
| 176 | + | export constant GRANT: u16 = 8; |
|
| 177 | + | /// Transfer a capability or CPU authority. |
|
| 178 | + | export constant TRANSFER: u16 = 16; |
|
| 179 | + | /// Create a domain. |
|
| 180 | + | export constant CREATE: u16 = 32; |
|
| 181 | + | /// Allocate physical memory. |
|
| 182 | + | export constant ALLOCATE: u16 = 64; |
|
| 183 | + | /// Wake or manage the parent relationship of a domain. |
|
| 184 | + | export constant WAKE: u16 = 128; |
|
| 185 | + | /// Destroy a domain. |
|
| 186 | + | export constant DESTROY: u16 = 256; |
|
| 187 | + | /// All defined right bits. |
|
| 188 | + | export constant RIGHTS_MASK: u16 = 511; |
|
| 189 | + | /// Cascade destruction through creation ancestry. |
|
| 190 | + | export constant CASCADE: u64 = 1; |
|
| 191 | + | /// Fault code for explicit user Abort, separate from CPU exception codes. |
|
| 192 | + | export constant FAULT_ABORT: u32 = 256; |
|
| 193 | + | /// Fault code for destruction by another domain, separate from CPU exception codes. |
|
| 194 | + | export constant FAULT_DESTROYED: u32 = 257; |
|
| 195 | + | /// Fault code for a domain whose shared event ring failed validation. |
|
| 196 | + | export constant FAULT_RING: u32 = 258; |
|
| 197 | + | ||
| 198 | + | /// Object kind stored in the high four handle bits. |
|
| 199 | + | export union Kind: Copy { |
|
| 200 | + | /// Unoccupied table entry. |
|
| 201 | + | Empty = 0, |
|
| 202 | + | /// Contiguous physical memory. |
|
| 203 | + | Page = 1, |
|
| 204 | + | /// Interrupt source ownership. |
|
| 205 | + | Interrupt = 2, |
|
| 206 | + | /// Device memory region. |
|
| 207 | + | Device = 3, |
|
| 208 | + | /// Domain notification queue. |
|
| 209 | + | Events = 4, |
|
| 210 | + | /// Domain management authority. |
|
| 211 | + | Domain = 5, |
|
| 212 | + | /// Exclusive CPU time window. |
|
| 213 | + | Budget = 6, |
|
| 214 | + | /// Resident executable package graph entry. |
|
| 215 | + | Image = 7, |
|
| 216 | + | } |
|
| 217 | + | ||
| 218 | + | /// Scalar error codes; the syscall ABI returns their negatives. |
|
| 219 | + | export union Error: Copy { |
|
| 220 | + | /// Successful operation. |
|
| 221 | + | Ok = 0, |
|
| 222 | + | /// Missing, stale, or incorrectly typed handle. |
|
| 223 | + | BadHandle = 1, |
|
| 224 | + | /// Required rights are absent. |
|
| 225 | + | Denied = 2, |
|
| 226 | + | /// Physical memory is exhausted. |
|
| 227 | + | OutOfMemory = 3, |
|
| 228 | + | /// Invalid argument, alignment, or reserved bits. |
|
| 229 | + | InvalidArg = 4, |
|
| 230 | + | /// The operation cannot proceed in the current state. |
|
| 231 | + | Busy = 5, |
|
| 232 | + | /// The supplied executable image is invalid. |
|
| 233 | + | VerifyFailed = 6, |
|
| 234 | + | /// The domain is not pending activation. |
|
| 235 | + | NotPending = 7, |
|
| 236 | + | /// Fixed kernel storage is exhausted. |
|
| 237 | + | Exhausted = 8, |
|
| 238 | + | } |
|
| 239 | + | ||
| 240 | + | /// Operation identifier passed in a7. |
|
| 241 | + | export union Operation: Copy { |
|
| 242 | + | /// Grant capability rights. |
|
| 243 | + | CapabilityGrant = 10, |
|
| 244 | + | /// Transfer a capability. |
|
| 245 | + | CapabilityTransfer = 11, |
|
| 246 | + | /// Drop a capability. |
|
| 247 | + | CapabilityDrop = 12, |
|
| 248 | + | /// Create a pending domain from an image. |
|
| 249 | + | DomainCreate = 20, |
|
| 250 | + | /// Activate a pending domain. |
|
| 251 | + | DomainActivate = 21, |
|
| 252 | + | /// Destroy a domain. |
|
| 253 | + | DomainDestroy = 22, |
|
| 254 | + | /// Select a new lifecycle-event parent. |
|
| 255 | + | DomainReparent = 23, |
|
| 256 | + | /// Terminate normally. |
|
| 257 | + | DomainExit = 24, |
|
| 258 | + | /// Terminate with an abort report. |
|
| 259 | + | DomainAbort = 25, |
|
| 260 | + | /// Transfer the remaining active window to a target domain. |
|
| 261 | + | DomainYield = 26, |
|
| 262 | + | /// Allocate physical frames. |
|
| 263 | + | PageAllocate = 30, |
|
| 264 | + | /// Split an exclusive page object. |
|
| 265 | + | PageSplit = 31, |
|
| 266 | + | /// Reach the memory of a Page or the caller's Events handle, for `page.slice`. |
|
| 267 | + | PageAccess = 32, |
|
| 268 | + | /// Request a timeout event. |
|
| 269 | + | EventTimeout = 40, |
|
| 270 | + | /// Wait for an event. |
|
| 271 | + | EventWait = 41, |
|
| 272 | + | /// Deliver a wake token. |
|
| 273 | + | EventWakeup = 42, |
|
| 274 | + | /// Load a binary RIL package, which the kernel verifies. |
|
| 275 | + | ImageLoad = 50, |
|
| 276 | + | /// Create an additional execution context. |
|
| 277 | + | ContextCreate = 60, |
|
| 278 | + | /// Destroy an additional execution context. |
|
| 279 | + | ContextDestroy = 61, |
|
| 280 | + | /// Report normal return from the calling execution context. |
|
| 281 | + | ContextReturn = 62, |
|
| 282 | + | /// Partition a CPU time window. |
|
| 283 | + | BudgetSplit = 70, |
|
| 284 | + | /// Combine adjacent CPU time windows. |
|
| 285 | + | BudgetMerge = 71, |
|
| 286 | + | /// Bind and transfer a CPU time window to a context. |
|
| 287 | + | BudgetBind = 72, |
|
| 288 | + | /// Query page metadata. |
|
| 289 | + | QueryPage = 80, |
|
| 290 | + | /// Query device metadata. |
|
| 291 | + | QueryDevice = 81, |
|
| 292 | + | /// Query interrupt metadata. |
|
| 293 | + | QueryInterrupt = 82, |
|
| 294 | + | /// Query domain metadata. |
|
| 295 | + | QueryDomain = 83, |
|
| 296 | + | /// Query event queue metadata. |
|
| 297 | + | QueryEvents = 84, |
|
| 298 | + | /// Query a resident image. |
|
| 299 | + | QueryImage = 85, |
|
| 300 | + | /// Query an execution context. |
|
| 301 | + | QueryContext = 86, |
|
| 302 | + | /// Query a CPU time window. |
|
| 303 | + | QueryBudget = 87, |
|
| 304 | + | /// Check a device register access made by compiler-generated code. |
|
| 305 | + | DeviceAccess = 90, |
|
| 306 | + | } |
|
| 307 | + | ||
| 308 | + | /// Generation-bearing reference within one object table. |
|
| 309 | + | export record Ref: Copy { |
|
| 310 | + | /// Table index. |
|
| 311 | + | index: u32, |
|
| 312 | + | /// Generation of the indexed slot. |
|
| 313 | + | generation: u32, |
|
| 314 | + | } |
|
| 315 | + | ||
| 316 | + | /// Structurally valid handle fields. Lookup must also check the live table entry. |
|
| 317 | + | export record Decoded: Copy { |
|
| 318 | + | /// Encoded object kind. |
|
| 319 | + | kind: Kind, |
|
| 320 | + | /// Table slot and generation. |
|
| 321 | + | object: Ref, |
|
| 322 | + | } |
|
| 323 | + | ||
| 324 | + | /// Pack kind, 32-bit generation, and 24-bit index with zero reserved bits. |
|
| 325 | + | export fn handle(kind: Kind, object: Ref) -> Handle throws (Error) { |
|
| 326 | + | if kind == Kind::Empty or object.index as u64 > HANDLE_INDEX_MASK { |
|
| 327 | + | throw Error::InvalidArg; |
|
| 328 | + | } |
|
| 329 | + | return Handle( |
|
| 330 | + | (kind as u64 << HANDLE_KIND_SHIFT) |
|
| 331 | + | | (object.generation as u64 << HANDLE_GENERATION_SHIFT) |
|
| 332 | + | | object.index as u64 |
|
| 333 | + | ); |
|
| 334 | + | } |
|
| 335 | + | ||
| 336 | + | /// Decode a nonzero handle and reject reserved bits or unsupported kinds. |
|
| 337 | + | export fn decode(value: Handle) -> Decoded throws (Error) { |
|
| 338 | + | let bits = *value; |
|
| 339 | + | if bits == 0 or (bits & HANDLE_RESERVED_MASK) <> 0 { |
|
| 340 | + | throw Error::BadHandle; |
|
| 341 | + | } |
|
| 342 | + | let mut kind = Kind::Empty; |
|
| 343 | + | match bits >> HANDLE_KIND_SHIFT { |
|
| 344 | + | case Kind::Page as u64 => set kind = Kind::Page, |
|
| 345 | + | case Kind::Interrupt as u64 => set kind = Kind::Interrupt, |
|
| 346 | + | case Kind::Device as u64 => set kind = Kind::Device, |
|
| 347 | + | case Kind::Events as u64 => set kind = Kind::Events, |
|
| 348 | + | case Kind::Domain as u64 => set kind = Kind::Domain, |
|
| 349 | + | case Kind::Budget as u64 => set kind = Kind::Budget, |
|
| 350 | + | case Kind::Image as u64 => set kind = Kind::Image, |
|
| 351 | + | else => throw Error::BadHandle, |
|
| 352 | + | } |
|
| 353 | + | return Decoded { |
|
| 354 | + | kind, |
|
| 355 | + | object: Ref { |
|
| 356 | + | index: (bits & HANDLE_INDEX_MASK) as u32, |
|
| 357 | + | generation: (bits >> HANDLE_GENERATION_SHIFT) as u32, |
|
| 358 | + | }, |
|
| 359 | + | }; |
|
| 360 | + | } |
|
| 361 | + | ||
| 362 | + | /// Validate a caller-supplied rights mask. |
|
| 363 | + | export fn rights(bits: u64) -> Rights throws (Error) { |
|
| 364 | + | if (bits & ~(RIGHTS_MASK as u64)) <> 0 { |
|
| 365 | + | throw Error::InvalidArg; |
|
| 366 | + | } |
|
| 367 | + | return Rights(bits as u16); |
|
| 368 | + | } |
|
| 369 | + | ||
| 370 | + | /// Check that all requested rights are present. |
|
| 371 | + | export fn permits(granted: Rights, requested: Rights) -> bool { |
|
| 372 | + | return (*granted & *requested) == *requested; |
|
| 373 | + | } |
|
| 374 | + | ||
| 375 | + | /// Encode a generation-bearing internal ID without reserved bits. |
|
| 376 | + | export fn id(object: Ref) -> u64 { |
|
| 377 | + | return (object.generation as u64 << 32) | object.index as u64; |
|
| 378 | + | } |
|
| 379 | + | ||
| 380 | + | /// Decode an internal ID. Object lookup validates its scope and liveness. |
|
| 381 | + | export fn reference(value: u64) -> Ref { |
|
| 382 | + | return Ref { index: value as u32, generation: (value >> 32) as u32 }; |
|
| 383 | + | } |
kernel/kernel/access.rad
added
+94 -0
| 1 | + | //! Caller memory and regions reached by calls: the buffer registers of a user call, and |
|
| 2 | + | //! the memory of a Page or Events handle that `page.slice` turns into a slice. |
|
| 3 | + | ||
| 4 | + | use super::abi; |
|
| 5 | + | use super::limits; |
|
| 6 | + | use super::capability; |
|
| 7 | + | use super::pages; |
|
| 8 | + | use super::backing; |
|
| 9 | + | use super::domains; |
|
| 10 | + | use super::events; |
|
| 11 | + | use super::state; |
|
| 12 | + | use std::arch::rv64; |
|
| 13 | + | ||
| 14 | + | /// The two result words of a call: a0 and, for a call that returns a region, a1. |
|
| 15 | + | export record Reply: Copy { |
|
| 16 | + | /// The result, or the base address of a region. |
|
| 17 | + | value: u64, |
|
| 18 | + | /// The size in bytes of a region, and zero for other calls. |
|
| 19 | + | extra: u64, |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | /// Give the caller access to memory as a slice: the bytes of a Page with the requested |
|
| 23 | + | /// rights, or the event ring behind the caller's own Events handle. The result is the base |
|
| 24 | + | /// address and the size in bytes. A Page's allocation is marked as accessed, which never |
|
| 25 | + | /// clears. |
|
| 26 | + | export fn pageAccess( |
|
| 27 | + | shared: &mut state::Shared, |
|
| 28 | + | domain: &domains::Domain, |
|
| 29 | + | memory: &mut domains::Memory, |
|
| 30 | + | handle: abi::Handle, |
|
| 31 | + | arguments: &[u64] |
|
| 32 | + | ) -> Reply throws (abi::Error) { |
|
| 33 | + | if arguments[1] > 1 or arguments[2] <> 0 or arguments[3] <> 0 { |
|
| 34 | + | throw abi::Error::InvalidArg; |
|
| 35 | + | } |
|
| 36 | + | let entry = try capability::get(&memory.table, handle); |
|
| 37 | + | if entry.kind == abi::Kind::Events { |
|
| 38 | + | // Only the domain's own ring is memory that it may reach. |
|
| 39 | + | if *domain.events <> *handle { |
|
| 40 | + | throw abi::Error::Denied; |
|
| 41 | + | } |
|
| 42 | + | return Reply { value: &memory.ring as u64, extra: @sizeOf(events::Ring) as u64 }; |
|
| 43 | + | } |
|
| 44 | + | let mut rights = abi::READ; |
|
| 45 | + | if arguments[1] == 1 { |
|
| 46 | + | set rights |= abi::WRITE; |
|
| 47 | + | } |
|
| 48 | + | let permission = try capability::lookup(&memory.table, handle, abi::Kind::Page, abi::Rights( |
|
| 49 | + | rights |
|
| 50 | + | )); |
|
| 51 | + | let page = try pages::get(&shared.pages, permission.object); |
|
| 52 | + | try backing::markAccessed(&mut shared.pages.backings, page.backing); |
|
| 53 | + | return Reply { value: page.base, extra: page.count as u64 * limits::FRAME_SIZE }; |
|
| 54 | + | } |
|
| 55 | + | ||
| 56 | + | /// Translate the registers of a user call into the arguments of the operation. |
|
| 57 | + | /// |
|
| 58 | + | /// Registers a0 to a3 hold plain values: handles, numbers and rights. A user address is |
|
| 59 | + | /// never one of them. An operation that reads or writes the caller's memory does so only |
|
| 60 | + | /// through the buffer registers, a4 for the address and a5 for the size, which become the |
|
| 61 | + | /// address and size arguments that the operation validates against the caller's pages. |
|
| 62 | + | /// The operand registers that such an operation does not use must be zero. |
|
| 63 | + | export fn arguments(operation: u64, registers: &[u64]) -> [u64; 4] throws (abi::Error) { |
|
| 64 | + | let a0 = registers[*rv64::A0 as u32]; |
|
| 65 | + | let a1 = registers[*rv64::A1 as u32]; |
|
| 66 | + | let a2 = registers[*rv64::A2 as u32]; |
|
| 67 | + | let a3 = registers[*rv64::A3 as u32]; |
|
| 68 | + | let address = registers[*rv64::A4 as u32]; |
|
| 69 | + | let size = registers[*rv64::A5 as u32]; |
|
| 70 | + | match operation { |
|
| 71 | + | case |
|
| 72 | + | abi::Operation::QueryPage as u64, |
|
| 73 | + | abi::Operation::QueryDevice as u64, |
|
| 74 | + | abi::Operation::QueryInterrupt as u64, |
|
| 75 | + | abi::Operation::QueryDomain as u64, |
|
| 76 | + | abi::Operation::QueryEvents as u64, |
|
| 77 | + | abi::Operation::QueryImage as u64, |
|
| 78 | + | abi::Operation::QueryBudget as u64, |
|
| 79 | + | abi::Operation::ContextCreate as u64 => |
|
| 80 | + | { |
|
| 81 | + | if (a1 | a2 | a3) <> 0 { |
|
| 82 | + | throw abi::Error::InvalidArg; |
|
| 83 | + | } |
|
| 84 | + | return [a0, address, size, 0]; |
|
| 85 | + | } |
|
| 86 | + | case abi::Operation::QueryContext as u64 => { |
|
| 87 | + | if (a2 | a3) <> 0 { |
|
| 88 | + | throw abi::Error::InvalidArg; |
|
| 89 | + | } |
|
| 90 | + | return [a0, a1, address, size]; |
|
| 91 | + | } |
|
| 92 | + | else => return [a0, a1, a2, a3], |
|
| 93 | + | } |
|
| 94 | + | } |
kernel/kernel/backing.rad
added
+240 -0
| 1 | + | //! Allocation lifetimes shared by split pages and exposed domains. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | use super::limits; |
|
| 5 | + | use super::slots; |
|
| 6 | + | use super::frames; |
|
| 7 | + | use std::mem; |
|
| 8 | + | ||
| 9 | + | /// Words required for one exposure bit per domain slot. |
|
| 10 | + | constant EXPOSURE_WORDS: u32 = (limits::DOMAINS + 31) / 32; |
|
| 11 | + | ||
| 12 | + | /// Mark domain index `n` in an exposure bitmap. |
|
| 13 | + | fn exposePut(words: &mut [u32], n: u32) { |
|
| 14 | + | set words[n / 32] |= 1 << (n % 32); |
|
| 15 | + | } |
|
| 16 | + | ||
| 17 | + | /// Clear domain index `n` in an exposure bitmap. |
|
| 18 | + | fn exposeClear(words: &mut [u32], n: u32) { |
|
| 19 | + | set words[n / 32] &= ~(1 << (n % 32)); |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | /// Test domain index `n` in an exposure bitmap. |
|
| 23 | + | fn exposeContains(words: &[u32], n: u32) -> bool { |
|
| 24 | + | return (words[n / 32] & (1 << (n % 32))) <> 0; |
|
| 25 | + | } |
|
| 26 | + | ||
| 27 | + | /// A physical allocation retained independently of its current page objects. |
|
| 28 | + | export record Allocation: Copy { |
|
| 29 | + | /// Complete physical frame ownership, unchanged by page splits. |
|
| 30 | + | run: frames::Run, |
|
| 31 | + | /// Number of page objects with live handles. |
|
| 32 | + | pages: u32, |
|
| 33 | + | /// Number of live domains with lifetime exposure. |
|
| 34 | + | exposed: u32, |
|
| 35 | + | /// Domain-index exposure bitmap. Domain exit clears bits before slot reuse. |
|
| 36 | + | exposures: [u32; EXPOSURE_WORDS], |
|
| 37 | + | /// Whether verified code has reached the memory as a slice. Page splits keep it, |
|
| 38 | + | /// because the flag belongs to the allocation and not to a page object. |
|
| 39 | + | accessed: bool, |
|
| 40 | + | } |
|
| 41 | + | ||
| 42 | + | /// Fixed allocation records and generation-checked exposure-domain membership. |
|
| 43 | + | export record Store: Copy { |
|
| 44 | + | /// Physical frames owned by backing allocations. |
|
| 45 | + | pool: frames::Pool, |
|
| 46 | + | /// Backing-record lifetimes. |
|
| 47 | + | slots: [slots::Slot; limits::ALLOCATIONS], |
|
| 48 | + | /// Payloads for live backing records. |
|
| 49 | + | records: [Allocation; limits::ALLOCATIONS], |
|
| 50 | + | /// Exclusive upper bound of backing slots that have been published. |
|
| 51 | + | used: u32, |
|
| 52 | + | /// Live domain generation, or zero after all of that domain's exposures end. |
|
| 53 | + | domains: [u32; limits::DOMAINS], |
|
| 54 | + | /// Zero while open; otherwise one plus the next exposure slot to inspect. |
|
| 55 | + | ending: [u32; limits::DOMAINS], |
|
| 56 | + | } |
|
| 57 | + | ||
| 58 | + | /// Initialize fresh backing and exposure-domain tables. |
|
| 59 | + | export fn initialize(store: &mut Store) { |
|
| 60 | + | set store.used = 0; |
|
| 61 | + | set store.pool.count = 0; |
|
| 62 | + | set store.pool.retiredHead = 0; |
|
| 63 | + | set store.pool.retiredCount = 0; |
|
| 64 | + | slots::initialize(&mut store.slots[..]); |
|
| 65 | + | for i in 0..limits::DOMAINS { |
|
| 66 | + | set store.domains[i] = 0; |
|
| 67 | + | set store.ending[i] = 0; |
|
| 68 | + | } |
|
| 69 | + | } |
|
| 70 | + | ||
| 71 | + | /// Register a domain before it can receive allocation exposure. |
|
| 72 | + | export fn registerDomain(store: &mut Store, domain: abi::Ref) throws (abi::Error) { |
|
| 73 | + | if domain.index >= limits::DOMAINS or domain.generation == 0 { |
|
| 74 | + | throw abi::Error::InvalidArg; |
|
| 75 | + | } |
|
| 76 | + | if store.domains[domain.index] <> 0 { |
|
| 77 | + | throw abi::Error::Busy; |
|
| 78 | + | } |
|
| 79 | + | set store.domains[domain.index] = domain.generation; |
|
| 80 | + | } |
|
| 81 | + | ||
| 82 | + | /// Test exposure-domain generation without reading beyond the fixed table. |
|
| 83 | + | export fn domainLive(store: &Store, domain: abi::Ref) -> bool { |
|
| 84 | + | return domain.index < limits::DOMAINS |
|
| 85 | + | and domain.generation <> 0 |
|
| 86 | + | and store.domains[domain.index] == domain.generation |
|
| 87 | + | and store.ending[domain.index] == 0; |
|
| 88 | + | } |
|
| 89 | + | ||
| 90 | + | /// Publish reserved metadata with one page object and its live origin exposure. |
|
| 91 | + | /// The caller owns the committed run and has validated the origin domain. |
|
| 92 | + | export fn publish( |
|
| 93 | + | store: &mut Store, reservation: slots::Reservation, run: frames::Run, origin: abi::Ref |
|
| 94 | + | ) -> abi::Ref { |
|
| 95 | + | assert domainLive(store, origin); |
|
| 96 | + | let object = slots::reference(&reservation); |
|
| 97 | + | assert slots::matches(&store.slots[..], object, slots::State::Reserved); |
|
| 98 | + | if object.index >= store.used { |
|
| 99 | + | set store.used = object.index + 1; |
|
| 100 | + | } |
|
| 101 | + | set store.records[object.index].run = run; |
|
| 102 | + | set store.records[object.index].pages = 1; |
|
| 103 | + | set store.records[object.index].exposed = 1; |
|
| 104 | + | set store.records[object.index].exposures = [0; EXPOSURE_WORDS]; |
|
| 105 | + | set store.records[object.index].accessed = false; |
|
| 106 | + | exposePut(&mut store.records[object.index].exposures[..], origin.index); |
|
| 107 | + | return try! slots::commit(&mut store.slots[..], reservation); |
|
| 108 | + | } |
|
| 109 | + | ||
| 110 | + | /// Require a live generation-bearing backing reference. |
|
| 111 | + | fn require(store: &Store, object: abi::Ref) throws (abi::Error) { |
|
| 112 | + | if not slots::matches(&store.slots[..], object, slots::State::Live) { |
|
| 113 | + | throw abi::Error::BadHandle; |
|
| 114 | + | } |
|
| 115 | + | } |
|
| 116 | + | ||
| 117 | + | /// Record that the allocation was reached through `page.slice`. |
|
| 118 | + | export fn markAccessed(store: &mut Store, object: abi::Ref) throws (abi::Error) { |
|
| 119 | + | try require(store, object); |
|
| 120 | + | set store.records[object.index].accessed = true; |
|
| 121 | + | } |
|
| 122 | + | ||
| 123 | + | /// Whether the allocation was ever reached through `page.slice`. |
|
| 124 | + | export fn accessed(store: &Store, object: abi::Ref) -> bool throws (abi::Error) { |
|
| 125 | + | try require(store, object); |
|
| 126 | + | return store.records[object.index].accessed; |
|
| 127 | + | } |
|
| 128 | + | ||
| 129 | + | /// Whether the allocation has exactly one page object, so that no split sibling exists. |
|
| 130 | + | export fn sole(store: &Store, object: abi::Ref) -> bool throws (abi::Error) { |
|
| 131 | + | try require(store, object); |
|
| 132 | + | return store.records[object.index].pages == 1; |
|
| 133 | + | } |
|
| 134 | + | ||
| 135 | + | /// Add one split page to the allocation's lifetime count. |
|
| 136 | + | export fn retainPage(store: &mut Store, object: abi::Ref) throws (abi::Error) { |
|
| 137 | + | try require(store, object); |
|
| 138 | + | if store.records[object.index].pages == 0 { |
|
| 139 | + | throw abi::Error::BadHandle; |
|
| 140 | + | } |
|
| 141 | + | if store.records[object.index].pages == mem::U32_MAX { |
|
| 142 | + | throw abi::Error::Exhausted; |
|
| 143 | + | } |
|
| 144 | + | set store.records[object.index].pages += 1; |
|
| 145 | + | } |
|
| 146 | + | ||
| 147 | + | /// Record access for a domain for the rest of that domain generation's lifetime. |
|
| 148 | + | export fn expose(store: &mut Store, object: abi::Ref, domain: abi::Ref) throws (abi::Error) { |
|
| 149 | + | try require(store, object); |
|
| 150 | + | if store.records[object.index].pages == 0 or not domainLive(store, domain) { |
|
| 151 | + | throw abi::Error::BadHandle; |
|
| 152 | + | } |
|
| 153 | + | if exposeContains(&store.records[object.index].exposures[..], domain.index) { |
|
| 154 | + | return; |
|
| 155 | + | } |
|
| 156 | + | exposePut(&mut store.records[object.index].exposures[..], domain.index); |
|
| 157 | + | set store.records[object.index].exposed += 1; |
|
| 158 | + | } |
|
| 159 | + | ||
| 160 | + | /// Reclaim only when no page object and no domain exposure remains. |
|
| 161 | + | fn reclaim( |
|
| 162 | + | store: &mut Store, |
|
| 163 | + | object: abi::Ref, |
|
| 164 | + | release: fn(&mut frames::Pool, frames::Run) throws (abi::Error) |
|
| 165 | + | ) { |
|
| 166 | + | if store.records[object.index].pages <> 0 or store.records[object.index].exposed <> 0 { |
|
| 167 | + | return; |
|
| 168 | + | } |
|
| 169 | + | let run = store.records[object.index].run; |
|
| 170 | + | try! release(&mut store.pool, run); |
|
| 171 | + | try! slots::release(&mut store.slots[..], object); |
|
| 172 | + | } |
|
| 173 | + | ||
| 174 | + | /// Release one page object after its final handle disappears. |
|
| 175 | + | export fn releasePage(store: &mut Store, object: abi::Ref) throws (abi::Error) { |
|
| 176 | + | try require(store, object); |
|
| 177 | + | if store.records[object.index].pages == 0 { |
|
| 178 | + | throw abi::Error::BadHandle; |
|
| 179 | + | } |
|
| 180 | + | set store.records[object.index].pages -= 1; |
|
| 181 | + | reclaim(store, object, frames::release); |
|
| 182 | + | } |
|
| 183 | + | ||
| 184 | + | /// End all exposure while the caller has exclusive access to metadata. |
|
| 185 | + | export fn endDomain(store: &mut Store, domain: abi::Ref) throws (abi::Error) { |
|
| 186 | + | assert try end(store, domain, limits::ALLOCATIONS, frames::release); |
|
| 187 | + | } |
|
| 188 | + | ||
| 189 | + | /// Inspect at most limit backing slots and retire allocations whose lifetimes end. |
|
| 190 | + | /// Returns true after all exposure is clear and the domain slot can be registered again. |
|
| 191 | + | export fn endDomainStep(store: &mut Store, domain: abi::Ref, limit: u32) -> bool |
|
| 192 | + | throws (abi::Error) |
|
| 193 | + | { |
|
| 194 | + | return try end(store, domain, limit, frames::retire); |
|
| 195 | + | } |
|
| 196 | + | ||
| 197 | + | /// Close exposure admission and advance one generation's retained scan cursor. |
|
| 198 | + | fn end( |
|
| 199 | + | store: &mut Store, |
|
| 200 | + | domain: abi::Ref, |
|
| 201 | + | limit: u32, |
|
| 202 | + | release: fn(&mut frames::Pool, frames::Run) throws (abi::Error) |
|
| 203 | + | ) -> bool throws (abi::Error) { |
|
| 204 | + | if domain.index >= limits::DOMAINS |
|
| 205 | + | or domain.generation == 0 |
|
| 206 | + | or store.domains[domain.index] <> domain.generation |
|
| 207 | + | { |
|
| 208 | + | throw abi::Error::BadHandle; |
|
| 209 | + | } |
|
| 210 | + | if limit == 0 { |
|
| 211 | + | throw abi::Error::InvalidArg; |
|
| 212 | + | } |
|
| 213 | + | if store.ending[domain.index] == 0 { |
|
| 214 | + | set store.ending[domain.index] = 1; |
|
| 215 | + | } |
|
| 216 | + | let first = store.ending[domain.index] - 1; |
|
| 217 | + | let mut count = store.used - first; |
|
| 218 | + | if count > limit { |
|
| 219 | + | set count = limit; |
|
| 220 | + | } |
|
| 221 | + | let next = first + count; |
|
| 222 | + | for i in first..next { |
|
| 223 | + | if store.slots[i].state <> slots::State::Live { |
|
| 224 | + | continue; |
|
| 225 | + | } |
|
| 226 | + | if not exposeContains(&store.records[i].exposures[..], domain.index) { |
|
| 227 | + | continue; |
|
| 228 | + | } |
|
| 229 | + | exposeClear(&mut store.records[i].exposures[..], domain.index); |
|
| 230 | + | set store.records[i].exposed -= 1; |
|
| 231 | + | reclaim(store, abi::Ref { index: i, generation: store.slots[i].generation }, release); |
|
| 232 | + | } |
|
| 233 | + | if next < store.used { |
|
| 234 | + | set store.ending[domain.index] = next + 1; |
|
| 235 | + | return false; |
|
| 236 | + | } |
|
| 237 | + | set store.domains[domain.index] = 0; |
|
| 238 | + | set store.ending[domain.index] = 0; |
|
| 239 | + | return true; |
|
| 240 | + | } |
kernel/kernel/boot.rad
added
+242 -0
| 1 | + | //! Machine entry boundary and published platform state. |
|
| 2 | + | ||
| 3 | + | use super::state; |
|
| 4 | + | use super::platform; |
|
| 5 | + | use super::startup; |
|
| 6 | + | use super::fdt; |
|
| 7 | + | use super::range; |
|
| 8 | + | use super::limits; |
|
| 9 | + | use super::sync; |
|
| 10 | + | use super::trap; |
|
| 11 | + | use super::pages; |
|
| 12 | + | use super::registry; |
|
| 13 | + | use super::domains; |
|
| 14 | + | use super::lifecycle; |
|
| 15 | + | use super::loader; |
|
| 16 | + | use super::budgets; |
|
| 17 | + | use super::dispatch; |
|
| 18 | + | use super::timers; |
|
| 19 | + | use super::remote; |
|
| 20 | + | use super::interrupts; |
|
| 21 | + | use super::devices; |
|
| 22 | + | use super::plic; |
|
| 23 | + | use super::registers; |
|
| 24 | + | use std::arch::rv64::shared::catalog; |
|
| 25 | + | use std::mem; |
|
| 26 | + | use std::arch::rv64; |
|
| 27 | + | ||
| 28 | + | /// UART transmit data register offset. |
|
| 29 | + | constant UART_DATA: u64 = 0; |
|
| 30 | + | /// UART line status register offset. |
|
| 31 | + | constant UART_STATUS: u64 = 5; |
|
| 32 | + | /// UART transmit holding register empty bit. |
|
| 33 | + | constant UART_READY: u8 = 1 << 5; |
|
| 34 | + | /// Bytes through the UART line status register. |
|
| 35 | + | constant UART_SIZE: u64 = UART_STATUS + 1; |
|
| 36 | + | /// Maximum polls per diagnostic output byte. |
|
| 37 | + | constant UART_POLLS: u32 = 1000000; |
|
| 38 | + | ||
| 39 | + | /// Platform data published by hart zero before secondary initialization. |
|
| 40 | + | /// Hart zero builds it in place and then releases READY. It is never written |
|
| 41 | + | /// again, so every later read, which `published` performs, follows the acquire. |
|
| 42 | + | export unsafe static PLATFORM: platform::Platform = undefined; |
|
| 43 | + | /// Release/acquire publication flag for PLATFORM. |
|
| 44 | + | static READY: u64 = 0; |
|
| 45 | + | /// Number of harts that validated their machine stack. |
|
| 46 | + | static ARRIVED: u64 = 0; |
|
| 47 | + | /// Root resource publication before user dispatch begins. |
|
| 48 | + | static STARTED: u64 = 0; |
|
| 49 | + | /// Trap anchors owned by their physical harts. Hart `i` writes entry `i` once in |
|
| 50 | + | /// `enter`, before it installs the entry; afterwards only trap entry on that hart |
|
| 51 | + | /// (through mscratch) and `run` on that hart reach it. |
|
| 52 | + | unsafe static HARTS: [trap::Hart; limits::HARTS] = undefined; |
|
| 53 | + | ||
| 54 | + | /// Largest device tree the boot contract accepts, in bytes. |
|
| 55 | + | constant MAX_TREE: u32 = 65536; |
|
| 56 | + | /// Bytes of a device tree header. |
|
| 57 | + | constant TREE_HEADER: u32 = fdt::HEADER_SIZE; |
|
| 58 | + | ||
| 59 | + | /// Read the platform that hart zero published. Reading earlier is a kernel bug. |
|
| 60 | + | export fn published() -> platform::Platform { |
|
| 61 | + | assert sync::loadAcquire(&READY) <> 0; |
|
| 62 | + | // READY was released after the last write to PLATFORM, which is read-only now. |
|
| 63 | + | unsafe { |
|
| 64 | + | return PLATFORM; |
|
| 65 | + | } |
|
| 66 | + | } |
|
| 67 | + | ||
| 68 | + | /// Dispatch interrupts, user calls, and user faults; halt on kernel faults. |
|
| 69 | + | /// The caller is machine trap entry on a hart that finished `enter`. |
|
| 70 | + | unsafe fn unexpected(frame: &mut trap::Frame, hart: &mut trap::Hart) { |
|
| 71 | + | let cause = trap::classify(frame.cause); |
|
| 72 | + | if cause == trap::Cause::Timer |
|
| 73 | + | or cause == trap::Cause::Software |
|
| 74 | + | or cause == trap::Cause::External |
|
| 75 | + | { |
|
| 76 | + | dispatch::interrupt(frame, hart); |
|
| 77 | + | } |
|
| 78 | + | if cause == trap::Cause::UserCall and trap::fromUser(frame) { |
|
| 79 | + | dispatch::call(frame, hart); |
|
| 80 | + | } |
|
| 81 | + | if trap::fromUser(frame) { |
|
| 82 | + | dispatch::fault(frame, hart); |
|
| 83 | + | } |
|
| 84 | + | print(&published(), "kernel: unexpected trap\n"); |
|
| 85 | + | trap::halt(); |
|
| 86 | + | } |
|
| 87 | + | ||
| 88 | + | /// Send a bounded diagnostic through the discovered byte-wide UART. |
|
| 89 | + | fn print(machine: &platform::Platform, message: *[u8]) { |
|
| 90 | + | let uart = try registers::open( |
|
| 91 | + | machine, platform::Kind::Uart, UART_SIZE |
|
| 92 | + | ) catch { |
|
| 93 | + | return; |
|
| 94 | + | }; |
|
| 95 | + | for byte in message { |
|
| 96 | + | let mut polls: u32 = 0; |
|
| 97 | + | while (registers::read8(uart, UART_STATUS) & UART_READY) == 0 { |
|
| 98 | + | set polls += 1; |
|
| 99 | + | if polls == UART_POLLS { |
|
| 100 | + | return; |
|
| 101 | + | } |
|
| 102 | + | } |
|
| 103 | + | registers::write8(uart, UART_DATA, byte); |
|
| 104 | + | } |
|
| 105 | + | } |
|
| 106 | + | ||
| 107 | + | /// Check the device tree header and return the tree size. |
|
| 108 | + | /// The tree must be aligned and bounded, and its header must fit before the size is read. |
|
| 109 | + | export fn treeSize(header: &[u8], address: u64) -> u32 { |
|
| 110 | + | assert (address & (rv64::DWORD_SIZE as u64 - 1)) == 0 |
|
| 111 | + | and address <= mem::U64_MAX - MAX_TREE as u64; |
|
| 112 | + | let size = try! fdt::integer(header, fdt::SIZE_OFFSET, 4) as u32; |
|
| 113 | + | assert size >= TREE_HEADER and size <= MAX_TREE; |
|
| 114 | + | return size; |
|
| 115 | + | } |
|
| 116 | + | ||
| 117 | + | /// Initialize every global store from the device tree and boot catalog. Hart zero |
|
| 118 | + | /// runs this once, before any other hart touches kernel state. |
|
| 119 | + | fn bringUp( |
|
| 120 | + | machine: &mut platform::Platform, |
|
| 121 | + | shared: &mut state::Shared, |
|
| 122 | + | tree: &[u8], |
|
| 123 | + | address: u64, |
|
| 124 | + | bootCatalog: &[catalog::Entry], |
|
| 125 | + | bases: &[u64] |
|
| 126 | + | ) { |
|
| 127 | + | try! platform::decode(tree, machine); |
|
| 128 | + | let treeRange = range::new(address, tree.len as u64) else panic "bringUp: FDT range"; |
|
| 129 | + | assert platform::inRam(machine, treeRange); |
|
| 130 | + | try! platform::protect(machine, treeRange); |
|
| 131 | + | try! pages::initialize(&mut shared.pages, machine); |
|
| 132 | + | domains::initialize(&mut shared.domains); |
|
| 133 | + | lifecycle::initialize(&mut shared.lifecycle); |
|
| 134 | + | loader::initialize(&mut shared.loader); |
|
| 135 | + | set shared.maintenance = state::Maintenance { next: 0, hart: nil, interval: 0 }; |
|
| 136 | + | budgets::initialize(&mut shared.budgets); |
|
| 137 | + | timers::initialize(&mut shared.timers); |
|
| 138 | + | remote::initialize(&mut shared.remote, machine.harts); |
|
| 139 | + | interrupts::initialize(&mut shared.interrupts, machine.irqSources); |
|
| 140 | + | try! devices::initialize(&mut shared.devices, machine); |
|
| 141 | + | assert bootCatalog.len >= 2 and bootCatalog.len <= limits::PACKAGES; |
|
| 142 | + | registry::initialize(&mut shared.registry); |
|
| 143 | + | try! registry::boot(&mut shared.registry, bootCatalog); |
|
| 144 | + | let library = registry::find( |
|
| 145 | + | ®istry::capture(&shared.registry), &"std"[..] |
|
| 146 | + | ) else panic "bringUp: std"; |
|
| 147 | + | let kernel = registry::find( |
|
| 148 | + | ®istry::capture(&shared.registry), &"kernel"[..] |
|
| 149 | + | ) else panic "bringUp: kernel"; |
|
| 150 | + | assert library.index == 0 and kernel.index == 1; |
|
| 151 | + | assert bases[library.index] <> 0 and bases[kernel.index] <> 0; |
|
| 152 | + | print(machine, "kernel: boot catalog ready\n"); |
|
| 153 | + | print(machine, "kernel: platform ready\n"); |
|
| 154 | + | } |
|
| 155 | + | ||
| 156 | + | /// Count the online harts. |
|
| 157 | + | export fn online(machine: &platform::Platform) -> u64 { |
|
| 158 | + | let mut count: u64 = 0; |
|
| 159 | + | for id in 0..limits::HARTS { |
|
| 160 | + | if (machine.harts & (1 << id)) <> 0 { |
|
| 161 | + | set count += 1; |
|
| 162 | + | } |
|
| 163 | + | } |
|
| 164 | + | return count; |
|
| 165 | + | } |
|
| 166 | + | ||
| 167 | + | /// Initialize one hart from firmware arguments. Return true for the last hart. |
|
| 168 | + | /// Firmware supplies a mapped FDT, a catalog of `entryCount` entries, a state |
|
| 169 | + | /// table of `limits::PACKAGES` words, and a disjoint, reserved stack for each hart. |
|
| 170 | + | export unsafe fn enter( |
|
| 171 | + | hart: u64, tree: *u8, stackTop: u64, entries: *catalog::Entry, entryCount: u32, stateTable: *u64 |
|
| 172 | + | ) -> bool { |
|
| 173 | + | assert hart < limits::HARTS as u64; |
|
| 174 | + | if hart == 0 { |
|
| 175 | + | assert entryCount >= 2 and entryCount <= limits::PACKAGES; |
|
| 176 | + | // The firmware pointers are the only unchecked inputs; the lengths come from |
|
| 177 | + | // the tree header and the boot contract. A slice never points at address 0, |
|
| 178 | + | // so each pointer must be non-null. |
|
| 179 | + | assert tree as u64 <> 0 and entries as u64 <> 0 and stateTable as u64 <> 0; |
|
| 180 | + | let header = @sliceOf(tree, TREE_HEADER); |
|
| 181 | + | let size = treeSize(&header[..], tree as u64); |
|
| 182 | + | let blob = @sliceOf(tree, size); |
|
| 183 | + | let bootCatalog = @sliceOf(entries, entryCount); |
|
| 184 | + | let bases = @sliceOf(stateTable, limits::PACKAGES); |
|
| 185 | + | bringUp( |
|
| 186 | + | &mut PLATFORM, &mut state::SHARED, &blob[..], tree as u64, &bootCatalog[..], &bases[..] |
|
| 187 | + | ); |
|
| 188 | + | try! plic::initialize(&PLATFORM); |
|
| 189 | + | sync::storeRelease(&mut READY, 1); |
|
| 190 | + | } else { |
|
| 191 | + | while sync::loadAcquire(&READY) == 0 {} |
|
| 192 | + | } |
|
| 193 | + | let machine = published(); |
|
| 194 | + | assert (machine.harts & (1 << hart as u32)) <> 0; |
|
| 195 | + | assert stackTop == machine.stacks[hart as u32].end |
|
| 196 | + | and (stackTop & (rv64::STACK_ALIGNMENT as u64 - 1)) == 0; |
|
| 197 | + | let stack = machine.stacks[hart as u32]; |
|
| 198 | + | set HARTS[hart as u32] = trap::Hart { |
|
| 199 | + | stackTop: stack.end, |
|
| 200 | + | stackBottom: stack.start, |
|
| 201 | + | kernelGp: stateTable as u64, |
|
| 202 | + | handler: unexpected, |
|
| 203 | + | savedT0: 0, |
|
| 204 | + | savedT1: 0, |
|
| 205 | + | savedSp: 0, |
|
| 206 | + | dispatchTop: stack.end, |
|
| 207 | + | dispatchFrame: undefined, |
|
| 208 | + | }; |
|
| 209 | + | trap::install(&mut HARTS[hart as u32]); |
|
| 210 | + | try! dispatch::initialize(&machine, hart as u32, stateTable as u64); |
|
| 211 | + | return sync::fetchAdd(&mut ARRIVED, 1) + 1 == online(&machine); |
|
| 212 | + | } |
|
| 213 | + | ||
| 214 | + | /// Publish configured root resources once, then enter dispatch on every hart. |
|
| 215 | + | /// The caller passes the value that `enter` returned on this hart, which is true |
|
| 216 | + | /// on exactly one hart, after every online hart has arrived. |
|
| 217 | + | export unsafe fn launch(last: bool) -> ! { |
|
| 218 | + | if last { |
|
| 219 | + | let machine = published(); |
|
| 220 | + | let shared = &mut state::SHARED; |
|
| 221 | + | if let root = registry::find(®istry::capture(&shared.registry), &"root"[..]) { |
|
| 222 | + | let scheduler = registry::find( |
|
| 223 | + | ®istry::capture(&shared.registry), &"scheduler"[..] |
|
| 224 | + | ) else panic "launch: missing scheduler image"; |
|
| 225 | + | // Every other hart waits for STARTED, so this hart alone reaches the metadata. |
|
| 226 | + | try! startup::create(&machine, shared, root, scheduler); |
|
| 227 | + | print(&machine, "kernel: root ready\n"); |
|
| 228 | + | } |
|
| 229 | + | sync::storeRelease(&mut STARTED, 1); |
|
| 230 | + | } else { |
|
| 231 | + | while sync::loadAcquire(&STARTED) == 0 {} |
|
| 232 | + | } |
|
| 233 | + | run(); |
|
| 234 | + | } |
|
| 235 | + | ||
| 236 | + | /// Enter dispatch after every online hart publishes its initialized state. |
|
| 237 | + | export unsafe fn run() -> ! { |
|
| 238 | + | let count = online(&published()); |
|
| 239 | + | while sync::loadAcquire(&ARRIVED) < count {} |
|
| 240 | + | sync::syncInstructions(); |
|
| 241 | + | dispatch::start(&mut HARTS[dispatch::hart()]); |
|
| 242 | + | } |
kernel/kernel/boot.ras
added
+23 -0
| 1 | + | //! RV64 machine startup. Firmware supplies a0=hart, a1=FDT, and aligned sp. |
|
| 2 | + | .text; |
|
| 3 | + | .export @kernel::boot::start; |
|
| 4 | + | .export @kernel::boot::initialize; |
|
| 5 | + | ||
| 6 | + | @kernel::boot::start |
|
| 7 | + | call @kernel::boot::initialize; |
|
| 8 | + | call @kernel::boot::launch; |
|
| 9 | + | ebreak; |
|
| 10 | + | ||
| 11 | + | // Initialize machine CSRs and preserve the firmware stack across Radiance entry. |
|
| 12 | + | @kernel::boot::initialize |
|
| 13 | + | csrw mie %zero; |
|
| 14 | + | csrw mstatus %zero; |
|
| 15 | + | csrw mscratch %sp; |
|
| 16 | + | mv %a2 %sp; |
|
| 17 | + | mv %a5 %gp; |
|
| 18 | + | addi %sp %sp -16; |
|
| 19 | + | sd %ra 0(%sp); |
|
| 20 | + | call @kernel::boot::enter; |
|
| 21 | + | ld %ra 0(%sp); |
|
| 22 | + | addi %sp %sp 16; |
|
| 23 | + | ret; |
kernel/kernel/budgets.rad
added
+404 -0
| 1 | + | //! Exclusive half-open CPU intervals and their capability ownership. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | use super::limits; |
|
| 5 | + | use super::slots; |
|
| 6 | + | use super::capability; |
|
| 7 | + | use super::transactions; |
|
| 8 | + | use super::domains; |
|
| 9 | + | ||
| 10 | + | /// Rights on an initial CPU budget. |
|
| 11 | + | export constant DEFAULT_RIGHTS: u16 = abi::READ | abi::WRITE | abi::TRANSFER; |
|
| 12 | + | ||
| 13 | + | /// One retained interval on a physical hart. |
|
| 14 | + | export record Window: Copy { |
|
| 15 | + | /// Physical hart on which this interval grants CPU authority. |
|
| 16 | + | hart: u32, |
|
| 17 | + | /// Inclusive first authorized tick. |
|
| 18 | + | start: u64, |
|
| 19 | + | /// Exclusive end of CPU authority. |
|
| 20 | + | end: u64, |
|
| 21 | + | /// Domain holding the sole capability for this interval. |
|
| 22 | + | owner: abi::Ref, |
|
| 23 | + | /// Sole live handle in the owner's table. |
|
| 24 | + | handle: abi::Handle, |
|
| 25 | + | /// Selected execution context, if the interval is bound. |
|
| 26 | + | context: ?abi::Ref, |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | /// Bounded CPU authority derived from one initial interval per hart. |
|
| 30 | + | export record Store: Copy { |
|
| 31 | + | /// Budget object generations. |
|
| 32 | + | slots: [slots::Slot; limits::BUDGETS], |
|
| 33 | + | /// Payload for each published interval. |
|
| 34 | + | windows: [Window; limits::BUDGETS], |
|
| 35 | + | /// Harts whose initial interval has already been issued. |
|
| 36 | + | seeded: u64, |
|
| 37 | + | } |
|
| 38 | + | ||
| 39 | + | /// Initialize fresh budget metadata before boot issues CPU authority. |
|
| 40 | + | export fn initialize(store: &mut Store) { |
|
| 41 | + | slots::initialize(&mut store.slots[..]); |
|
| 42 | + | set store.seeded = 0; |
|
| 43 | + | } |
|
| 44 | + | ||
| 45 | + | /// Read a retained budget object after checking its generation. |
|
| 46 | + | export fn get(store: &Store, object: abi::Ref) -> Window throws (abi::Error) { |
|
| 47 | + | if not slots::matches(&store.slots[..], object, slots::State::Live) { |
|
| 48 | + | throw abi::Error::BadHandle; |
|
| 49 | + | } |
|
| 50 | + | return store.windows[object.index]; |
|
| 51 | + | } |
|
| 52 | + | ||
| 53 | + | /// Validate the unique capability owner and return the budget object reference. |
|
| 54 | + | fn owned(store: &Store, table: &capability::Table, handle: abi::Handle, rights: u16) -> abi::Ref |
|
| 55 | + | throws (abi::Error) |
|
| 56 | + | { |
|
| 57 | + | let entry = try capability::lookup(table, handle, abi::Kind::Budget, abi::Rights(rights)); |
|
| 58 | + | let window = try get(store, entry.object); |
|
| 59 | + | if window.owner <> table.owner or window.handle <> handle { |
|
| 60 | + | throw abi::Error::BadHandle; |
|
| 61 | + | } |
|
| 62 | + | return entry.object; |
|
| 63 | + | } |
|
| 64 | + | ||
| 65 | + | /// Publish a new interval after reserving both object and capability capacity. |
|
| 66 | + | fn install( |
|
| 67 | + | store: &mut Store, table: &mut capability::Table, window: Window, rights: abi::Rights |
|
| 68 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 69 | + | let handleSlot = try slots::reserve(&mut table.slots[..]); |
|
| 70 | + | let budgetSlot = try slots::reserve(&mut store.slots[..]) catch err { |
|
| 71 | + | try! slots::cancel(&mut table.slots[..], handleSlot); |
|
| 72 | + | throw err; |
|
| 73 | + | }; |
|
| 74 | + | let object = slots::reference(&budgetSlot); |
|
| 75 | + | let reference = slots::reference(&handleSlot); |
|
| 76 | + | let handle = try! abi::handle(abi::Kind::Budget, reference); |
|
| 77 | + | set store.windows[object.index] = window; |
|
| 78 | + | set store.windows[object.index].owner = table.owner; |
|
| 79 | + | set store.windows[object.index].handle = handle; |
|
| 80 | + | let live = try! slots::commit(&mut store.slots[..], budgetSlot); |
|
| 81 | + | return capability::publish(table, handleSlot, capability::Entry { |
|
| 82 | + | kind: abi::Kind::Budget, object: live, rights |
|
| 83 | + | }); |
|
| 84 | + | } |
|
| 85 | + | ||
| 86 | + | /// Issue one initial interval per hart during trusted boot initialization. |
|
| 87 | + | /// Only partition, merge, transfer, and consumption can change authority after this call. |
|
| 88 | + | export fn seed( |
|
| 89 | + | store: &mut Store, table: &mut capability::Table, hart: u32, start: u64, end: u64 |
|
| 90 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 91 | + | if hart >= limits::HARTS or start >= end { |
|
| 92 | + | throw abi::Error::InvalidArg; |
|
| 93 | + | } |
|
| 94 | + | if (store.seeded & (1 << hart as u64)) <> 0 { |
|
| 95 | + | throw abi::Error::Busy; |
|
| 96 | + | } |
|
| 97 | + | let handle = try install(store, table, Window { |
|
| 98 | + | hart, start, end, owner: table.owner, handle: abi::Handle(0), context: nil |
|
| 99 | + | }, abi::Rights( |
|
| 100 | + | DEFAULT_RIGHTS |
|
| 101 | + | )); |
|
| 102 | + | set store.seeded |= 1 << hart as u64; |
|
| 103 | + | return handle; |
|
| 104 | + | } |
|
| 105 | + | ||
| 106 | + | /// Read retained interval metadata through its unique capability. |
|
| 107 | + | export fn query(store: &Store, table: &capability::Table, handle: abi::Handle) -> Window |
|
| 108 | + | throws (abi::Error) |
|
| 109 | + | { |
|
| 110 | + | let object = try owned(store, table, handle, abi::READ); |
|
| 111 | + | return store.windows[object.index]; |
|
| 112 | + | } |
|
| 113 | + | ||
| 114 | + | /// Discard the elapsed prefix and reject fully expired authority. |
|
| 115 | + | fn remaining(window: Window, now: u64) -> Window throws (abi::Error) { |
|
| 116 | + | if now >= window.end { |
|
| 117 | + | throw abi::Error::Busy; |
|
| 118 | + | } |
|
| 119 | + | let mut result = window; |
|
| 120 | + | if now > result.start { |
|
| 121 | + | set result.start = now; |
|
| 122 | + | } |
|
| 123 | + | return result; |
|
| 124 | + | } |
|
| 125 | + | ||
| 126 | + | /// Split unconsumed authority, retaining the left handle and returning the right. |
|
| 127 | + | export fn split( |
|
| 128 | + | store: &mut Store, table: &mut capability::Table, handle: abi::Handle, at: u64, now: u64 |
|
| 129 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 130 | + | let object = try owned(store, table, handle, abi::WRITE); |
|
| 131 | + | let window = try remaining(store.windows[object.index], now); |
|
| 132 | + | if at <= window.start or at >= window.end { |
|
| 133 | + | throw abi::Error::InvalidArg; |
|
| 134 | + | } |
|
| 135 | + | let entry = try! capability::get(table, handle); |
|
| 136 | + | let mut right = window; |
|
| 137 | + | set right.start = at; |
|
| 138 | + | let result = try install(store, table, right, entry.rights); |
|
| 139 | + | set store.windows[object.index].start = window.start; |
|
| 140 | + | set store.windows[object.index].end = at; |
|
| 141 | + | return result; |
|
| 142 | + | } |
|
| 143 | + | ||
| 144 | + | /// Combine adjacent intervals on the same hart with the same context binding. |
|
| 145 | + | /// The first handle survives with the intersection of both capability rights. |
|
| 146 | + | export fn merge( |
|
| 147 | + | store: &mut Store, |
|
| 148 | + | table: &mut capability::Table, |
|
| 149 | + | first: abi::Handle, |
|
| 150 | + | second: abi::Handle, |
|
| 151 | + | now: u64 |
|
| 152 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 153 | + | if first == second { |
|
| 154 | + | throw abi::Error::InvalidArg; |
|
| 155 | + | } |
|
| 156 | + | let a = try owned(store, table, first, abi::WRITE); |
|
| 157 | + | let b = try owned(store, table, second, abi::WRITE); |
|
| 158 | + | let left = try remaining(store.windows[a.index], now); |
|
| 159 | + | let right = try remaining(store.windows[b.index], now); |
|
| 160 | + | if left.hart <> right.hart or left.context <> right.context { |
|
| 161 | + | throw abi::Error::InvalidArg; |
|
| 162 | + | } |
|
| 163 | + | let mut start = left.start; |
|
| 164 | + | let mut end = right.end; |
|
| 165 | + | if left.end <> right.start { |
|
| 166 | + | if right.end <> left.start { |
|
| 167 | + | throw abi::Error::InvalidArg; |
|
| 168 | + | } |
|
| 169 | + | set start = right.start; |
|
| 170 | + | set end = left.end; |
|
| 171 | + | } |
|
| 172 | + | let firstEntry = try! capability::get(table, first); |
|
| 173 | + | let secondEntry = try! capability::get(table, second); |
|
| 174 | + | let firstSlot = try! abi::decode(first); |
|
| 175 | + | set table.entries[firstSlot.object.index].rights = |
|
| 176 | + | abi::Rights(*firstEntry.rights & *secondEntry.rights); |
|
| 177 | + | set store.windows[a.index].start = start; |
|
| 178 | + | set store.windows[a.index].end = end; |
|
| 179 | + | let removed = try! capability::invalidate(table, second); |
|
| 180 | + | try! slots::release(&mut store.slots[..], b); |
|
| 181 | + | return first; |
|
| 182 | + | } |
|
| 183 | + | ||
| 184 | + | /// Relinquish a held interval and invalidate its sole capability. |
|
| 185 | + | export fn drop(store: &mut Store, table: &mut capability::Table, handle: abi::Handle) |
|
| 186 | + | throws (abi::Error) |
|
| 187 | + | { |
|
| 188 | + | let object = try owned(store, table, handle, 0); |
|
| 189 | + | let removed = try! capability::invalidate(table, handle); |
|
| 190 | + | try! slots::release(&mut store.slots[..], object); |
|
| 191 | + | } |
|
| 192 | + | ||
| 193 | + | /// Retire an expired interval and its sole handle. |
|
| 194 | + | export fn expire( |
|
| 195 | + | store: &mut Store, table: &mut capability::Table, handle: abi::Handle, now: u64 |
|
| 196 | + | ) -> bool throws (abi::Error) { |
|
| 197 | + | let object = try owned(store, table, handle, 0); |
|
| 198 | + | if now < store.windows[object.index].end { |
|
| 199 | + | return false; |
|
| 200 | + | } |
|
| 201 | + | let removed = try! capability::invalidate(table, handle); |
|
| 202 | + | try! slots::release(&mut store.slots[..], object); |
|
| 203 | + | return true; |
|
| 204 | + | } |
|
| 205 | + | ||
| 206 | + | /// Transfer unconsumed authority, clear its context binding, and attenuate rights. |
|
| 207 | + | /// The caller keeps both domains alive and serializes both capability tables. |
|
| 208 | + | export fn transfer( |
|
| 209 | + | store: &mut Store, |
|
| 210 | + | source: &mut capability::Table, |
|
| 211 | + | target: &mut capability::Table, |
|
| 212 | + | handle: abi::Handle, |
|
| 213 | + | rights: u64, |
|
| 214 | + | now: u64 |
|
| 215 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 216 | + | let object = try owned(store, source, handle, abi::TRANSFER); |
|
| 217 | + | let mut window = try remaining(store.windows[object.index], now); |
|
| 218 | + | let entry = try! capability::get(source, handle); |
|
| 219 | + | let owner = source.owner; |
|
| 220 | + | let pending = try transactions::reserve(target, owner, handle, entry, rights, true); |
|
| 221 | + | let result = transactions::transfer(source, target, pending); |
|
| 222 | + | set window.owner = target.owner; |
|
| 223 | + | set window.handle = result; |
|
| 224 | + | set window.context = nil; |
|
| 225 | + | set store.windows[object.index] = window; |
|
| 226 | + | return result; |
|
| 227 | + | } |
|
| 228 | + | ||
| 229 | + | /// Capability and context selected by a budget-binding request. |
|
| 230 | + | export record Binding: Copy { |
|
| 231 | + | /// Exclusive CPU authority to bind or transfer. |
|
| 232 | + | budget: abi::Handle, |
|
| 233 | + | /// Domain capability carrying Wake authority for the receiver. |
|
| 234 | + | domain: abi::Handle, |
|
| 235 | + | /// Live execution context owned by the receiver. |
|
| 236 | + | context: abi::Ref, |
|
| 237 | + | } |
|
| 238 | + | ||
| 239 | + | /// Reject overlapping execution authority for one context across all harts. |
|
| 240 | + | fn available(store: &Store, object: abi::Ref, candidate: Window, context: abi::Ref) -> bool { |
|
| 241 | + | for i in 0..limits::BUDGETS { |
|
| 242 | + | if i == object.index or store.slots[i].state <> slots::State::Live { |
|
| 243 | + | continue; |
|
| 244 | + | } |
|
| 245 | + | let other = store.windows[i]; |
|
| 246 | + | if other.context == context |
|
| 247 | + | and candidate.start < other.end |
|
| 248 | + | and other.start < candidate.end |
|
| 249 | + | { |
|
| 250 | + | return false; |
|
| 251 | + | } |
|
| 252 | + | } |
|
| 253 | + | return true; |
|
| 254 | + | } |
|
| 255 | + | ||
| 256 | + | /// A validated binding: the window to rebind and the domain that receives it. |
|
| 257 | + | export opaque record Checked: Copy { |
|
| 258 | + | /// Budget object to bind. |
|
| 259 | + | object: abi::Ref, |
|
| 260 | + | /// Remaining interval that the binding keeps. |
|
| 261 | + | candidate: Window, |
|
| 262 | + | /// Domain that owns the selected context. |
|
| 263 | + | target: abi::Ref, |
|
| 264 | + | /// Context to select. |
|
| 265 | + | context: abi::Ref, |
|
| 266 | + | } |
|
| 267 | + | ||
| 268 | + | /// Validate a binding request and report the receiving domain. When that is not |
|
| 269 | + | /// the caller's domain, the caller transfers the unique capability to the |
|
| 270 | + | /// receiver's table and then calls `commit`. |
|
| 271 | + | /// The caller serializes the budget, domain lifetimes, and the caller's table. |
|
| 272 | + | export fn check( |
|
| 273 | + | store: &Store, |
|
| 274 | + | domainStore: &domains::Store, |
|
| 275 | + | table: &capability::Table, |
|
| 276 | + | request: Binding, |
|
| 277 | + | now: u64 |
|
| 278 | + | ) -> Checked throws (abi::Error) { |
|
| 279 | + | let object = try owned(store, table, request.budget, abi::TRANSFER); |
|
| 280 | + | let candidate = try remaining(store.windows[object.index], now); |
|
| 281 | + | let permission = try capability::lookup(table, request.domain, abi::Kind::Domain, abi::Rights( |
|
| 282 | + | abi::WAKE |
|
| 283 | + | )); |
|
| 284 | + | let target = try domains::get(domainStore, permission.object); |
|
| 285 | + | if target.state == domains::Lifecycle::Dead { |
|
| 286 | + | throw abi::Error::BadHandle; |
|
| 287 | + | } |
|
| 288 | + | if not slots::matches(&domainStore.contextSlots[..], request.context, slots::State::Live) |
|
| 289 | + | or domainStore.contexts[request.context.index].owner <> permission.object |
|
| 290 | + | { |
|
| 291 | + | throw abi::Error::BadHandle; |
|
| 292 | + | } |
|
| 293 | + | if domainStore.contexts[request.context.index].state == domains::ContextState::Stopped { |
|
| 294 | + | throw abi::Error::InvalidArg; |
|
| 295 | + | } |
|
| 296 | + | if not available(store, object, candidate, request.context) { |
|
| 297 | + | throw abi::Error::Busy; |
|
| 298 | + | } |
|
| 299 | + | return Checked { object, candidate, target: permission.object, context: request.context }; |
|
| 300 | + | } |
|
| 301 | + | ||
| 302 | + | /// Domain that receives the budget of a checked binding. |
|
| 303 | + | export fn receiver(checked: &Checked) -> abi::Ref { |
|
| 304 | + | return checked.target; |
|
| 305 | + | } |
|
| 306 | + | ||
| 307 | + | /// Select the context of a checked binding, after any transfer to the receiver. |
|
| 308 | + | export fn commit(store: &mut Store, checked: Checked) { |
|
| 309 | + | set store.windows[checked.object.index].start = checked.candidate.start; |
|
| 310 | + | set store.windows[checked.object.index].context = checked.context; |
|
| 311 | + | } |
|
| 312 | + | ||
| 313 | + | /// Select a context in the caller's own domain. |
|
| 314 | + | /// The caller serializes the budget, domain lifetimes, and its table. |
|
| 315 | + | export fn bind( |
|
| 316 | + | store: &mut Store, |
|
| 317 | + | domainStore: &domains::Store, |
|
| 318 | + | table: &capability::Table, |
|
| 319 | + | request: Binding, |
|
| 320 | + | now: u64 |
|
| 321 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 322 | + | let checked = try check(&*store, domainStore, table, request, now); |
|
| 323 | + | assert checked.target == table.owner; |
|
| 324 | + | commit(store, checked); |
|
| 325 | + | return request.budget; |
|
| 326 | + | } |
|
| 327 | + | ||
| 328 | + | /// Check that the running context can hand its budget to the target's initial context. |
|
| 329 | + | /// Returns the binding that performs the handoff, which the caller applies with |
|
| 330 | + | /// `check` and `commit` and reports a rejected context as Busy. |
|
| 331 | + | export fn handoffRequest( |
|
| 332 | + | store: &Store, |
|
| 333 | + | domainStore: &domains::Store, |
|
| 334 | + | table: &capability::Table, |
|
| 335 | + | current: abi::CurrentContextInfo, |
|
| 336 | + | target: abi::Handle, |
|
| 337 | + | now: u64 |
|
| 338 | + | ) -> Binding throws (abi::Error) { |
|
| 339 | + | let handle = abi::Handle(current.budget); |
|
| 340 | + | let object = try owned(store, table, handle, abi::TRANSFER); |
|
| 341 | + | let window = try get(store, object); |
|
| 342 | + | if window.hart as u64 <> current.hart |
|
| 343 | + | or window.context <> abi::reference(current.context) |
|
| 344 | + | or now < window.start |
|
| 345 | + | or now >= window.end |
|
| 346 | + | { |
|
| 347 | + | throw abi::Error::Busy; |
|
| 348 | + | } |
|
| 349 | + | let permit = try capability::lookup(table, target, abi::Kind::Domain, abi::Rights(abi::WAKE)); |
|
| 350 | + | let receiver = try domains::get(domainStore, permit.object); |
|
| 351 | + | if receiver.state == domains::Lifecycle::Dead { |
|
| 352 | + | throw abi::Error::BadHandle; |
|
| 353 | + | } |
|
| 354 | + | if receiver.state <> domains::Lifecycle::Active |
|
| 355 | + | or abi::id(receiver.initial) == current.context |
|
| 356 | + | { |
|
| 357 | + | throw abi::Error::Busy; |
|
| 358 | + | } |
|
| 359 | + | let context = try domains::context(domainStore, permit.object, receiver.initial) catch { |
|
| 360 | + | throw abi::Error::Busy; |
|
| 361 | + | }; |
|
| 362 | + | if context.state <> domains::ContextState::Ready or context.hart <> nil { |
|
| 363 | + | throw abi::Error::Busy; |
|
| 364 | + | } |
|
| 365 | + | return Binding { budget: handle, domain: target, context: receiver.initial }; |
|
| 366 | + | } |
|
| 367 | + | ||
| 368 | + | /// Test half-open execution authority on the selected hart at the current tick. |
|
| 369 | + | export fn active(store: &Store, object: abi::Ref, hart: u32, now: u64) -> bool throws (abi::Error) { |
|
| 370 | + | let window = try get(store, object); |
|
| 371 | + | return window.context <> nil |
|
| 372 | + | and window.hart == hart |
|
| 373 | + | and window.start <= now |
|
| 374 | + | and now < window.end; |
|
| 375 | + | } |
|
| 376 | + | ||
| 377 | + | /// Find all harts with retained authority for a receiver's live contexts. |
|
| 378 | + | export fn wakeTargets( |
|
| 379 | + | windows: &Store, contexts: &domains::Store, receiver: abi::Ref, now: u64 |
|
| 380 | + | ) -> u32 { |
|
| 381 | + | let mut targets: u32 = 0; |
|
| 382 | + | for i in 0..limits::BUDGETS { |
|
| 383 | + | if windows.slots[i].state <> slots::State::Live { |
|
| 384 | + | continue; |
|
| 385 | + | } |
|
| 386 | + | let window = windows.windows[i]; |
|
| 387 | + | if window.owner <> receiver or window.end <= now { |
|
| 388 | + | continue; |
|
| 389 | + | } |
|
| 390 | + | let context = window.context else { |
|
| 391 | + | continue; |
|
| 392 | + | }; |
|
| 393 | + | if not slots::matches(&contexts.contextSlots[..], context, slots::State::Live) { |
|
| 394 | + | continue; |
|
| 395 | + | } |
|
| 396 | + | if contexts.contexts[context.index].owner <> receiver |
|
| 397 | + | or contexts.contexts[context.index].state == domains::ContextState::Stopped |
|
| 398 | + | { |
|
| 399 | + | continue; |
|
| 400 | + | } |
|
| 401 | + | set targets |= 1 << window.hart; |
|
| 402 | + | } |
|
| 403 | + | return targets; |
|
| 404 | + | } |
kernel/kernel/calls.rad
added
+1544 -0
| 1 | + | //! Direct-call execution and checked copies into domain-owned memory. |
|
| 2 | + | //! |
|
| 3 | + | //! Call logic is safe code over explicit stores, the caller's capability table, |
|
| 4 | + | //! and plain values. The entry points in the last section are the only code |
|
| 5 | + | //! here that reads hart-local state, takes the metadata lock, reaches the |
|
| 6 | + | //! shared stores, or locates a domain's table. Each locked region has one |
|
| 7 | + | //! such access, one call into safe logic, and one release of the lock. |
|
| 8 | + | ||
| 9 | + | use std::lang::il::binary; |
|
| 10 | + | use super::abi; |
|
| 11 | + | use super::slots; |
|
| 12 | + | use super::limits; |
|
| 13 | + | use super::capability; |
|
| 14 | + | use super::pages; |
|
| 15 | + | use super::range; |
|
| 16 | + | use super::access; |
|
| 17 | + | use super::physical; |
|
| 18 | + | use super::domains; |
|
| 19 | + | use super::instances; |
|
| 20 | + | use super::registry; |
|
| 21 | + | use super::budgets; |
|
| 22 | + | use super::trap; |
|
| 23 | + | use super::events; |
|
| 24 | + | use super::transactions; |
|
| 25 | + | use super::loader; |
|
| 26 | + | use super::dispatch; |
|
| 27 | + | use super::timers; |
|
| 28 | + | use super::sync; |
|
| 29 | + | use super::remote; |
|
| 30 | + | use super::interrupts; |
|
| 31 | + | use super::devices; |
|
| 32 | + | use super::plic; |
|
| 33 | + | use super::lifecycle; |
|
| 34 | + | use super::state; |
|
| 35 | + | use super::locked; |
|
| 36 | + | use std::mem; |
|
| 37 | + | use std::arch::rv64; |
|
| 38 | + | use std::lang::il; |
|
| 39 | + | ||
| 40 | + | // Types // |
|
| 41 | + | ||
| 42 | + | /// Query result for a context without a physical hart. |
|
| 43 | + | constant NO_HART: u64 = mem::U64_MAX; |
|
| 44 | + | ||
| 45 | + | /// Hart-local facts that safe call logic receives instead of reading. |
|
| 46 | + | export record Running: Copy { |
|
| 47 | + | /// Platform clock in mtime ticks. |
|
| 48 | + | now: u64, |
|
| 49 | + | /// Identity and budget of the running context, or nil when the hart runs none. |
|
| 50 | + | current: ?abi::CurrentContextInfo, |
|
| 51 | + | } |
|
| 52 | + | ||
| 53 | + | /// A budget change queued for the hart that owns the budget. |
|
| 54 | + | export record Forward: Copy { |
|
| 55 | + | /// Queued request that the caller awaits. |
|
| 56 | + | request: abi::Ref, |
|
| 57 | + | /// Hart that must be signalled to take the request. |
|
| 58 | + | hart: u32, |
|
| 59 | + | } |
|
| 60 | + | ||
| 61 | + | /// Result of a call that runs in one locked region. |
|
| 62 | + | export union Outcome: Copy { |
|
| 63 | + | /// The calling hart executed the operation and produced this result. |
|
| 64 | + | Value(u64), |
|
| 65 | + | /// The calling hart gave access to a region, which takes two result words. |
|
| 66 | + | Region(access::Reply), |
|
| 67 | + | /// A termination ran; these harts retain stopped contexts to release. |
|
| 68 | + | Stopped(u32), |
|
| 69 | + | /// Another hart executes the operation at its dispatch boundary. |
|
| 70 | + | Forwarded(Forward), |
|
| 71 | + | } |
|
| 72 | + | ||
| 73 | + | /// Result of the locked region that follows loader input decoding. |
|
| 74 | + | export union Admitted: Once { |
|
| 75 | + | /// An identical package was already resident, so the load is complete. |
|
| 76 | + | Finished(abi::Handle), |
|
| 77 | + | /// Output is reserved. Compile outside the lock, then publish. The tokens now include the |
|
| 78 | + | /// output's. |
|
| 79 | + | Compile(lifecycle::Lease, lifecycle::LoadAccess), |
|
| 80 | + | } |
|
| 81 | + | ||
| 82 | + | // Validation // |
|
| 83 | + | ||
| 84 | + | /// Resolve the owning hart for an operation that can change CPU authority. |
|
| 85 | + | fn budgetHart( |
|
| 86 | + | windows: &budgets::Store, table: &capability::Table, operation: u64, arguments: &[u64] |
|
| 87 | + | ) -> ?u32 throws (abi::Error) { |
|
| 88 | + | match operation { |
|
| 89 | + | case |
|
| 90 | + | abi::Operation::CapabilityTransfer as u64, |
|
| 91 | + | abi::Operation::CapabilityDrop as u64, |
|
| 92 | + | abi::Operation::BudgetSplit as u64, |
|
| 93 | + | abi::Operation::BudgetMerge as u64, |
|
| 94 | + | abi::Operation::BudgetBind as u64 => |
|
| 95 | + | {} |
|
| 96 | + | else => return nil, |
|
| 97 | + | } |
|
| 98 | + | let entry = try capability::get(table, abi::Handle(arguments[0])); |
|
| 99 | + | if entry.kind <> abi::Kind::Budget { |
|
| 100 | + | return nil; |
|
| 101 | + | } |
|
| 102 | + | return (try budgets::get(windows, entry.object)).hart; |
|
| 103 | + | } |
|
| 104 | + | ||
| 105 | + | /// Validate resident object generations for capabilities without page ownership. |
|
| 106 | + | fn resident(shared: &state::Shared, entry: capability::Entry) throws (abi::Error) { |
|
| 107 | + | match entry.kind { |
|
| 108 | + | case abi::Kind::Domain, abi::Kind::Events => { |
|
| 109 | + | let object = try domains::get(&shared.domains, entry.object); |
|
| 110 | + | if object.state == domains::Lifecycle::Dead { |
|
| 111 | + | throw abi::Error::BadHandle; |
|
| 112 | + | } |
|
| 113 | + | } |
|
| 114 | + | case abi::Kind::Device => { |
|
| 115 | + | let object = try devices::get(&shared.devices, entry.object); |
|
| 116 | + | } |
|
| 117 | + | case abi::Kind::Image => { |
|
| 118 | + | let object = try registry::get(&shared.registry, entry.object); |
|
| 119 | + | } |
|
| 120 | + | else => throw abi::Error::BadHandle, |
|
| 121 | + | } |
|
| 122 | + | } |
|
| 123 | + | ||
| 124 | + | /// Reject expired domain authority before an allocation can reserve resources. |
|
| 125 | + | fn allocationAuthority( |
|
| 126 | + | shared: &state::Shared, table: &capability::Table, operation: u64, handle: abi::Handle |
|
| 127 | + | ) throws (abi::Error) { |
|
| 128 | + | let create = abi::Operation::DomainCreate as u64; |
|
| 129 | + | if operation <> create |
|
| 130 | + | and operation <> abi::Operation::PageAllocate as u64 |
|
| 131 | + | and operation <> abi::Operation::ImageLoad as u64 |
|
| 132 | + | { |
|
| 133 | + | return; |
|
| 134 | + | } |
|
| 135 | + | let rights = abi::CREATE if operation == create else abi::ALLOCATE; |
|
| 136 | + | try resident(shared, try capability::authority(table, handle, abi::Rights(rights))); |
|
| 137 | + | } |
|
| 138 | + | ||
| 139 | + | /// Require an active calling domain with live allocation authority. |
|
| 140 | + | fn admit( |
|
| 141 | + | shared: &state::Shared, |
|
| 142 | + | domain: &domains::Domain, |
|
| 143 | + | table: &capability::Table, |
|
| 144 | + | operation: u64, |
|
| 145 | + | handle: abi::Handle |
|
| 146 | + | ) |
|
| 147 | + | throws (abi::Error) |
|
| 148 | + | { |
|
| 149 | + | if domain.state <> domains::Lifecycle::Active { |
|
| 150 | + | throw abi::Error::BadHandle; |
|
| 151 | + | } |
|
| 152 | + | try allocationAuthority(shared, table, operation, handle); |
|
| 153 | + | } |
|
| 154 | + | ||
| 155 | + | /// Check a retained domain's lifetime while metadata is serialized. |
|
| 156 | + | fn live(store: &domains::Store, owner: abi::Ref) -> bool { |
|
| 157 | + | return slots::matches(&store.slots[..], owner, slots::State::Live) |
|
| 158 | + | and store.records[owner.index].state <> domains::Lifecycle::Dead; |
|
| 159 | + | } |
|
| 160 | + | ||
| 161 | + | // Delegation // |
|
| 162 | + | ||
| 163 | + | /// Resolve the domain that receives a grant or transfer. |
|
| 164 | + | fn receiver( |
|
| 165 | + | shared: &state::Shared, table: &capability::Table, target: abi::Handle, moving: bool |
|
| 166 | + | ) -> abi::Ref throws (abi::Error) { |
|
| 167 | + | let owner = table.owner; |
|
| 168 | + | let mut receiver = owner; |
|
| 169 | + | if *target <> 0 { |
|
| 170 | + | let permission = try capability::lookup(table, target, abi::Kind::Domain, abi::Rights(0)); |
|
| 171 | + | set receiver = permission.object; |
|
| 172 | + | } |
|
| 173 | + | if moving and receiver == owner { |
|
| 174 | + | throw abi::Error::InvalidArg; |
|
| 175 | + | } |
|
| 176 | + | let destination = try domains::get(&shared.domains, receiver); |
|
| 177 | + | if destination.state == domains::Lifecycle::Dead { |
|
| 178 | + | throw abi::Error::BadHandle; |
|
| 179 | + | } |
|
| 180 | + | return receiver; |
|
| 181 | + | } |
|
| 182 | + | ||
| 183 | + | /// Grant authority to the calling domain itself. Budget and interrupt grants are |
|
| 184 | + | /// not possible, because only a transfer can move them. |
|
| 185 | + | fn grantSelf( |
|
| 186 | + | shared: &mut state::Shared, table: &mut capability::Table, handle: abi::Handle, rights: u64 |
|
| 187 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 188 | + | let entry = try capability::get(table, handle); |
|
| 189 | + | if entry.kind == abi::Kind::Page { |
|
| 190 | + | return try pages::selfGrant(&mut shared.pages, table, handle, rights); |
|
| 191 | + | } |
|
| 192 | + | if entry.kind == abi::Kind::Budget or entry.kind == abi::Kind::Interrupt { |
|
| 193 | + | throw abi::Error::Denied; |
|
| 194 | + | } |
|
| 195 | + | try resident(&*shared, entry); |
|
| 196 | + | let owner = table.owner; |
|
| 197 | + | let pending = try transactions::reserve(table, owner, handle, entry, rights, false); |
|
| 198 | + | return transactions::grant(table, pending); |
|
| 199 | + | } |
|
| 200 | + | ||
| 201 | + | /// Grant or transfer authority between the tables of two distinct domains. |
|
| 202 | + | fn grantOther( |
|
| 203 | + | shared: &mut state::Shared, |
|
| 204 | + | source: &mut capability::Table, |
|
| 205 | + | destination: &mut capability::Table, |
|
| 206 | + | handle: abi::Handle, |
|
| 207 | + | rights: u64, |
|
| 208 | + | moving: bool, |
|
| 209 | + | now: u64 |
|
| 210 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 211 | + | let entry = try capability::get(source, handle); |
|
| 212 | + | if entry.kind == abi::Kind::Page { |
|
| 213 | + | if moving { |
|
| 214 | + | return try pages::transfer(&mut shared.pages, source, destination, handle, rights); |
|
| 215 | + | } |
|
| 216 | + | return try pages::grant(&mut shared.pages, source, destination, handle, rights); |
|
| 217 | + | } |
|
| 218 | + | if entry.kind == abi::Kind::Budget { |
|
| 219 | + | if not moving { |
|
| 220 | + | throw abi::Error::Denied; |
|
| 221 | + | } |
|
| 222 | + | return try budgets::transfer(&mut shared.budgets, source, destination, handle, rights, now); |
|
| 223 | + | } |
|
| 224 | + | if entry.kind == abi::Kind::Interrupt { |
|
| 225 | + | if not moving { |
|
| 226 | + | throw abi::Error::Denied; |
|
| 227 | + | } |
|
| 228 | + | return try interrupts::transfer( |
|
| 229 | + | &mut shared.interrupts, source, destination, handle, rights |
|
| 230 | + | ); |
|
| 231 | + | } |
|
| 232 | + | try resident(&*shared, entry); |
|
| 233 | + | let pending = try transactions::reserve( |
|
| 234 | + | destination, source.owner, handle, entry, rights, moving |
|
| 235 | + | ); |
|
| 236 | + | if moving { |
|
| 237 | + | return transactions::transfer(source, destination, pending); |
|
| 238 | + | } |
|
| 239 | + | return transactions::grant(destination, pending); |
|
| 240 | + | } |
|
| 241 | + | ||
| 242 | + | /// Grant or transfer authority to a live domain other than the caller. Its table |
|
| 243 | + | /// is located by `domains::peer`, which proves it differs from the caller's. |
|
| 244 | + | fn deliver( |
|
| 245 | + | shared: &mut state::Shared, |
|
| 246 | + | table: &mut capability::Table, |
|
| 247 | + | receiver: abi::Ref, |
|
| 248 | + | handle: abi::Handle, |
|
| 249 | + | rights: u64, |
|
| 250 | + | moving: bool, |
|
| 251 | + | now: u64 |
|
| 252 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 253 | + | let destination = try domains::peer(&shared.domains, table.owner, receiver); |
|
| 254 | + | let mut result = abi::Handle(0); |
|
| 255 | + | // The receiver's metadata is a different allocation from the caller's, and |
|
| 256 | + | // the metadata lock serializes both tables. |
|
| 257 | + | unsafe { |
|
| 258 | + | set result = try grantOther( |
|
| 259 | + | shared, table, &mut destination.table, handle, rights, moving, now |
|
| 260 | + | ); |
|
| 261 | + | } |
|
| 262 | + | return result; |
|
| 263 | + | } |
|
| 264 | + | ||
| 265 | + | /// Grant or transfer authority while both domain tables remain serialized. |
|
| 266 | + | /// A receiver other than the caller needs a second table, which `deliver` |
|
| 267 | + | /// locates after safe code resolved the receiver and showed it differs from the caller. |
|
| 268 | + | fn delegate( |
|
| 269 | + | shared: &mut state::Shared, |
|
| 270 | + | table: &mut capability::Table, |
|
| 271 | + | handle: abi::Handle, |
|
| 272 | + | target: abi::Handle, |
|
| 273 | + | rights: u64, |
|
| 274 | + | moving: bool, |
|
| 275 | + | now: u64 |
|
| 276 | + | ) -> abi::Handle |
|
| 277 | + | throws (abi::Error) |
|
| 278 | + | { |
|
| 279 | + | let receiver = try receiver(&*shared, &*table, target, moving); |
|
| 280 | + | if receiver == table.owner { |
|
| 281 | + | return try grantSelf(shared, table, handle, rights); |
|
| 282 | + | } |
|
| 283 | + | return try deliver(shared, table, receiver, handle, rights, moving, now); |
|
| 284 | + | } |
|
| 285 | + | ||
| 286 | + | /// Select a context for a budget, moving its capability to the context's domain |
|
| 287 | + | /// when that is not the caller's. |
|
| 288 | + | export fn rebind( |
|
| 289 | + | shared: &mut state::Shared, table: &mut capability::Table, request: budgets::Binding, now: u64 |
|
| 290 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 291 | + | let checked = try budgets::check(&shared.budgets, &shared.domains, &*table, request, now); |
|
| 292 | + | let mut result = request.budget; |
|
| 293 | + | if budgets::receiver(&checked) <> table.owner { |
|
| 294 | + | let entry = try! capability::get(&*table, request.budget); |
|
| 295 | + | set result = try deliver( |
|
| 296 | + | shared, |
|
| 297 | + | table, |
|
| 298 | + | budgets::receiver(&checked), |
|
| 299 | + | request.budget, |
|
| 300 | + | *entry.rights as u64, |
|
| 301 | + | true, |
|
| 302 | + | now |
|
| 303 | + | ); |
|
| 304 | + | } |
|
| 305 | + | budgets::commit(&mut shared.budgets, checked); |
|
| 306 | + | return result; |
|
| 307 | + | } |
|
| 308 | + | ||
| 309 | + | /// Activate the domain that an Execute capability names, which can be the caller. |
|
| 310 | + | fn activate( |
|
| 311 | + | shared: &mut state::Shared, |
|
| 312 | + | memory: &mut domains::Memory, |
|
| 313 | + | handle: abi::Handle, |
|
| 314 | + | stack: abi::Handle, |
|
| 315 | + | args: abi::Handle, |
|
| 316 | + | size: u64 |
|
| 317 | + | ) throws (abi::Error) { |
|
| 318 | + | let owner = memory.table.owner; |
|
| 319 | + | let entry = try capability::lookup(&memory.table, handle, abi::Kind::Domain, abi::Rights( |
|
| 320 | + | abi::EXECUTE |
|
| 321 | + | )); |
|
| 322 | + | if entry.object == owner { |
|
| 323 | + | try domains::activate( |
|
| 324 | + | &mut shared.domains, &mut shared.pages, owner, memory, stack, args, size |
|
| 325 | + | ); |
|
| 326 | + | return; |
|
| 327 | + | } |
|
| 328 | + | let target = try domains::peer(&shared.domains, owner, entry.object); |
|
| 329 | + | // The target's metadata is a different allocation from the caller's, and the |
|
| 330 | + | // metadata lock serializes both. |
|
| 331 | + | unsafe { |
|
| 332 | + | try domains::activate( |
|
| 333 | + | &mut shared.domains, &mut shared.pages, entry.object, &mut *target, stack, args, size |
|
| 334 | + | ); |
|
| 335 | + | } |
|
| 336 | + | } |
|
| 337 | + | ||
| 338 | + | /// Send a wakeup token through a Domain capability with Wake authority. |
|
| 339 | + | fn wakeup(shared: &mut state::Shared, memory: &mut domains::Memory, handle: abi::Handle, token: u32) |
|
| 340 | + | throws (abi::Error) |
|
| 341 | + | { |
|
| 342 | + | let owner = memory.table.owner; |
|
| 343 | + | let permit = try capability::lookup(&memory.table, handle, abi::Kind::Domain, abi::Rights( |
|
| 344 | + | abi::WAKE |
|
| 345 | + | )); |
|
| 346 | + | let event = events::Event { |
|
| 347 | + | kind: events::WAKEUP, reserved: 0, code: token, value: abi::id(owner) |
|
| 348 | + | }; |
|
| 349 | + | let mut sent = false; |
|
| 350 | + | if permit.object == owner { |
|
| 351 | + | set sent = try domains::notify(&mut shared.domains, owner, &mut memory.ring, event); |
|
| 352 | + | } else { |
|
| 353 | + | // The receiver differs from the caller, so nothing borrows its metadata. |
|
| 354 | + | set sent = try domains::serve( |
|
| 355 | + | &mut shared.domains, permit.object, domains::RingRequest::Send(event) |
|
| 356 | + | ); |
|
| 357 | + | } |
|
| 358 | + | if not sent { |
|
| 359 | + | throw abi::Error::Busy; |
|
| 360 | + | } |
|
| 361 | + | } |
|
| 362 | + | ||
| 363 | + | /// Reserve a context in the domain that an Execute capability names, which can be the caller. |
|
| 364 | + | fn reserveIn( |
|
| 365 | + | shared: &mut state::Shared, |
|
| 366 | + | memory: &mut domains::Memory, |
|
| 367 | + | handle: abi::Handle, |
|
| 368 | + | start: abi::ContextStart |
|
| 369 | + | ) -> domains::ContextReservation throws (abi::Error) { |
|
| 370 | + | let owner = memory.table.owner; |
|
| 371 | + | let permission = try capability::authority(&memory.table, handle, abi::Rights(abi::EXECUTE)); |
|
| 372 | + | if permission.object == owner { |
|
| 373 | + | return try domains::contextReserve( |
|
| 374 | + | &mut shared.domains, |
|
| 375 | + | &mut shared.pages, |
|
| 376 | + | &shared.registry, |
|
| 377 | + | owner, |
|
| 378 | + | start, |
|
| 379 | + | &mut memory.table |
|
| 380 | + | ); |
|
| 381 | + | } |
|
| 382 | + | let target = try domains::peer(&shared.domains, owner, permission.object); |
|
| 383 | + | // The target's metadata is a different allocation from the caller's, and the metadata |
|
| 384 | + | // lock serializes both. The context consumes a stack Page from the target's table. |
|
| 385 | + | unsafe { |
|
| 386 | + | return try domains::contextReserve( |
|
| 387 | + | &mut shared.domains, |
|
| 388 | + | &mut shared.pages, |
|
| 389 | + | &shared.registry, |
|
| 390 | + | permission.object, |
|
| 391 | + | start, |
|
| 392 | + | &mut target.table |
|
| 393 | + | ); |
|
| 394 | + | } |
|
| 395 | + | } |
|
| 396 | + | ||
| 397 | + | /// Drop one capability and release the object-specific ownership it carries. |
|
| 398 | + | fn drop( |
|
| 399 | + | shared: &mut state::Shared, |
|
| 400 | + | domain: &domains::Domain, |
|
| 401 | + | table: &mut capability::Table, |
|
| 402 | + | handle: abi::Handle |
|
| 403 | + | ) |
|
| 404 | + | throws (abi::Error) |
|
| 405 | + | { |
|
| 406 | + | let entry = try transactions::dropping(&*table, handle, domain.events); |
|
| 407 | + | match entry.kind { |
|
| 408 | + | case abi::Kind::Page => try pages::drop(&mut shared.pages, table, handle), |
|
| 409 | + | case abi::Kind::Budget => try budgets::drop(&mut shared.budgets, table, handle), |
|
| 410 | + | case abi::Kind::Interrupt => { |
|
| 411 | + | let number = try interrupts::query(&shared.interrupts, &*table, handle); |
|
| 412 | + | plic::mask(number); |
|
| 413 | + | try! interrupts::drop(&mut shared.interrupts, table, handle); |
|
| 414 | + | } |
|
| 415 | + | else => { |
|
| 416 | + | try! capability::invalidate(table, handle); |
|
| 417 | + | } |
|
| 418 | + | } |
|
| 419 | + | } |
|
| 420 | + | ||
| 421 | + | // User Buffers // |
|
| 422 | + | ||
| 423 | + | /// Test whether one nonwrapping extent contains the complete result range. |
|
| 424 | + | fn contains(base: u64, size: u64, address: u64, length: u64) -> bool { |
|
| 425 | + | return address >= base and address - base <= size and length <= size - (address - base); |
|
| 426 | + | } |
|
| 427 | + | ||
| 428 | + | /// Validate aligned Page or private package-data storage with the given rights. |
|
| 429 | + | /// Package-state tables and kernel metadata cannot hold caller-supplied records. |
|
| 430 | + | /// `table` is the calling domain's own table, which selects its package bases. `stack` is the |
|
| 431 | + | /// calling context's stack, which no handle names, so it is always writable and readable by it. |
|
| 432 | + | fn buffer( |
|
| 433 | + | shared: &state::Shared, |
|
| 434 | + | table: &capability::Table, |
|
| 435 | + | stack: range::Range, |
|
| 436 | + | address: u64, |
|
| 437 | + | size: u64, |
|
| 438 | + | rights: abi::Rights, |
|
| 439 | + | alignment: u64 |
|
| 440 | + | ) -> bool { |
|
| 441 | + | assert alignment == 4 or alignment == 8; |
|
| 442 | + | if (address & (alignment - 1)) <> 0 or size > mem::U64_MAX - address { |
|
| 443 | + | return false; |
|
| 444 | + | } |
|
| 445 | + | if contains(stack.start, stack.end - stack.start, address, size) { |
|
| 446 | + | return true; |
|
| 447 | + | } |
|
| 448 | + | if pages::accessible(&shared.pages, table, address, size, rights) { |
|
| 449 | + | return true; |
|
| 450 | + | } |
|
| 451 | + | for i in 0..limits::PACKAGES { |
|
| 452 | + | if shared.registry.slots[i].state <> slots::State::Live { |
|
| 453 | + | continue; |
|
| 454 | + | } |
|
| 455 | + | let base = instances::base(&shared.domains.tables[table.owner.index], i); |
|
| 456 | + | if base == 0 { |
|
| 457 | + | continue; |
|
| 458 | + | } |
|
| 459 | + | let package = try! registry::get(&shared.registry, abi::Ref { |
|
| 460 | + | index: i, generation: shared.registry.slots[i].generation |
|
| 461 | + | }); |
|
| 462 | + | if contains(base, package.memory as u64, address, size) { |
|
| 463 | + | return true; |
|
| 464 | + | } |
|
| 465 | + | } |
|
| 466 | + | return false; |
|
| 467 | + | } |
|
| 468 | + | ||
| 469 | + | /// Copy a fixed query record only after validating its exact buffer size. |
|
| 470 | + | fn reply( |
|
| 471 | + | shared: &state::Shared, |
|
| 472 | + | table: &capability::Table, |
|
| 473 | + | stack: range::Range, |
|
| 474 | + | arguments: &[u64], |
|
| 475 | + | words: &[u64] |
|
| 476 | + | ) |
|
| 477 | + | throws (abi::Error) |
|
| 478 | + | { |
|
| 479 | + | let size = words.len as u64 * 8; |
|
| 480 | + | if arguments[2] <> size |
|
| 481 | + | or arguments[3] <> 0 |
|
| 482 | + | or not buffer( |
|
| 483 | + | shared, table, stack, arguments[1], size, abi::Rights(abi::WRITE), 8 |
|
| 484 | + | ) |
|
| 485 | + | { |
|
| 486 | + | throw abi::Error::InvalidArg; |
|
| 487 | + | } |
|
| 488 | + | try physical::writeWords(&shared.pages.backings.pool, arguments[1], words) catch { |
|
| 489 | + | throw abi::Error::InvalidArg; |
|
| 490 | + | }; |
|
| 491 | + | } |
|
| 492 | + | ||
| 493 | + | // Execution // |
|
| 494 | + | ||
| 495 | + | /// Validate a termination call and return harts that must release stopped contexts. |
|
| 496 | + | fn terminate( |
|
| 497 | + | shared: &mut state::Shared, |
|
| 498 | + | domain: &domains::Domain, |
|
| 499 | + | table: &capability::Table, |
|
| 500 | + | operation: u64, |
|
| 501 | + | arguments: &[u64] |
|
| 502 | + | ) -> u32 throws (abi::Error) { |
|
| 503 | + | assert arguments.len == 4; |
|
| 504 | + | if domain.state <> domains::Lifecycle::Active { |
|
| 505 | + | throw abi::Error::BadHandle; |
|
| 506 | + | } |
|
| 507 | + | if arguments[2] <> 0 or arguments[3] <> 0 { |
|
| 508 | + | throw abi::Error::InvalidArg; |
|
| 509 | + | } |
|
| 510 | + | let owner = table.owner; |
|
| 511 | + | match operation { |
|
| 512 | + | case abi::Operation::DomainExit as u64 => { |
|
| 513 | + | if arguments[0] > mem::U32_MAX as u64 or arguments[1] <> 0 { |
|
| 514 | + | throw abi::Error::InvalidArg; |
|
| 515 | + | } |
|
| 516 | + | return try domains::terminate( |
|
| 517 | + | &mut shared.domains, owner, events::CHILD_EXIT, arguments[0] as u32 |
|
| 518 | + | ); |
|
| 519 | + | } |
|
| 520 | + | case abi::Operation::DomainAbort as u64 => { |
|
| 521 | + | if arguments[0] <> 0 or arguments[1] <> 0 { |
|
| 522 | + | throw abi::Error::InvalidArg; |
|
| 523 | + | } |
|
| 524 | + | return try domains::terminate( |
|
| 525 | + | &mut shared.domains, owner, events::FAULT, abi::FAULT_ABORT |
|
| 526 | + | ); |
|
| 527 | + | } |
|
| 528 | + | case abi::Operation::DomainDestroy as u64 => { |
|
| 529 | + | let flags = arguments[1]; |
|
| 530 | + | if (flags & ~abi::CASCADE) <> 0 or (arguments[0] == 0 and flags <> 0) { |
|
| 531 | + | throw abi::Error::InvalidArg; |
|
| 532 | + | } |
|
| 533 | + | let mut target = owner; |
|
| 534 | + | if arguments[0] <> 0 { |
|
| 535 | + | let entry = try capability::lookup( |
|
| 536 | + | table, abi::Handle(arguments[0]), abi::Kind::Domain, abi::Rights(abi::DESTROY) |
|
| 537 | + | ); |
|
| 538 | + | set target = entry.object; |
|
| 539 | + | if (try domains::get(&shared.domains, target)).state == domains::Lifecycle::Dead { |
|
| 540 | + | throw abi::Error::BadHandle; |
|
| 541 | + | } |
|
| 542 | + | } |
|
| 543 | + | let mut kind = events::CHILD_EXIT; |
|
| 544 | + | let mut code: u32 = 0; |
|
| 545 | + | if target <> owner { |
|
| 546 | + | set kind = events::FAULT; |
|
| 547 | + | set code = abi::FAULT_DESTROYED; |
|
| 548 | + | } |
|
| 549 | + | if flags == abi::CASCADE { |
|
| 550 | + | return try domains::terminateTree(&mut shared.domains, target, kind, code); |
|
| 551 | + | } |
|
| 552 | + | return try domains::terminate(&mut shared.domains, target, kind, code); |
|
| 553 | + | } |
|
| 554 | + | else => throw abi::Error::InvalidArg, |
|
| 555 | + | } |
|
| 556 | + | } |
|
| 557 | + | ||
| 558 | + | /// Execute one operation for the calling domain with all metadata serialized. |
|
| 559 | + | /// `memory` is the caller's own metadata and `domain` is its published record. |
|
| 560 | + | /// Allocation, creation, loading, and termination are staged by their own entry |
|
| 561 | + | /// points and are not executed here. |
|
| 562 | + | fn execute( |
|
| 563 | + | shared: &mut state::Shared, |
|
| 564 | + | domain: &domains::Domain, |
|
| 565 | + | memory: &mut domains::Memory, |
|
| 566 | + | operation: u64, |
|
| 567 | + | arguments: &[u64], |
|
| 568 | + | running: Running |
|
| 569 | + | ) -> u64 throws (abi::Error) { |
|
| 570 | + | assert arguments.len == 4; |
|
| 571 | + | let handle = abi::Handle(arguments[0]); |
|
| 572 | + | let stack = domains::stackOf(&shared.domains, running.current); |
|
| 573 | + | // These operations borrow the caller's whole metadata and need no allocation authority. |
|
| 574 | + | match operation { |
|
| 575 | + | case abi::Operation::DomainActivate as u64 => { |
|
| 576 | + | try activate( |
|
| 577 | + | shared, |
|
| 578 | + | memory, |
|
| 579 | + | handle, |
|
| 580 | + | abi::Handle(arguments[1]), |
|
| 581 | + | abi::Handle(arguments[2]), |
|
| 582 | + | arguments[3] |
|
| 583 | + | ); |
|
| 584 | + | return 0; |
|
| 585 | + | } |
|
| 586 | + | case abi::Operation::EventWakeup as u64 => { |
|
| 587 | + | if arguments[1] > mem::U32_MAX as u64 |
|
| 588 | + | or arguments[2] <> 0 |
|
| 589 | + | or arguments[3] <> 0 |
|
| 590 | + | { |
|
| 591 | + | throw abi::Error::InvalidArg; |
|
| 592 | + | } |
|
| 593 | + | try wakeup(shared, memory, handle, arguments[1] as u32); |
|
| 594 | + | return 0; |
|
| 595 | + | } |
|
| 596 | + | else => {} |
|
| 597 | + | } |
|
| 598 | + | let table = &mut memory.table; |
|
| 599 | + | let owner = table.owner; |
|
| 600 | + | match operation { |
|
| 601 | + | case abi::Operation::CapabilityGrant as u64, abi::Operation::CapabilityTransfer as u64 => { |
|
| 602 | + | let moving = operation == abi::Operation::CapabilityTransfer as u64; |
|
| 603 | + | return *try delegate( |
|
| 604 | + | shared, table, handle, abi::Handle(arguments[1]), arguments[2], moving, running.now |
|
| 605 | + | ); |
|
| 606 | + | } |
|
| 607 | + | case abi::Operation::DeviceAccess as u64 => { |
|
| 608 | + | let flags = arguments[2]; |
|
| 609 | + | if (flags & ~il::DEVICE_FLAGS_MASK) <> 0 { |
|
| 610 | + | throw abi::Error::InvalidArg; |
|
| 611 | + | } |
|
| 612 | + | return try devices::address( |
|
| 613 | + | &shared.devices, |
|
| 614 | + | &*table, |
|
| 615 | + | handle, |
|
| 616 | + | arguments[1], |
|
| 617 | + | flags & il::DEVICE_WIDTH_MASK, |
|
| 618 | + | (flags & il::DEVICE_WRITE) <> 0 |
|
| 619 | + | ); |
|
| 620 | + | } |
|
| 621 | + | case abi::Operation::CapabilityDrop as u64 => { |
|
| 622 | + | try drop(shared, domain, table, handle); |
|
| 623 | + | return 0; |
|
| 624 | + | } |
|
| 625 | + | case abi::Operation::DomainReparent as u64 => { |
|
| 626 | + | if arguments[2] <> 0 or arguments[3] <> 0 { |
|
| 627 | + | throw abi::Error::InvalidArg; |
|
| 628 | + | } |
|
| 629 | + | try domains::reparent(&mut shared.domains, &*table, handle, abi::Handle(arguments[1])); |
|
| 630 | + | return 0; |
|
| 631 | + | } |
|
| 632 | + | case abi::Operation::PageSplit as u64 => |
|
| 633 | + | return *try pages::split( |
|
| 634 | + | &mut shared.pages, table, handle, arguments[1] |
|
| 635 | + | ), |
|
| 636 | + | case abi::Operation::DomainYield as u64 => { |
|
| 637 | + | if arguments[1] <> 0 or arguments[2] <> 0 or arguments[3] <> 0 { |
|
| 638 | + | throw abi::Error::InvalidArg; |
|
| 639 | + | } |
|
| 640 | + | let current = running.current else panic "execute: no current context"; |
|
| 641 | + | let request = try budgets::handoffRequest( |
|
| 642 | + | &shared.budgets, &shared.domains, &*table, current, handle, running.now |
|
| 643 | + | ); |
|
| 644 | + | try rebind(shared, table, request, running.now) catch error { |
|
| 645 | + | if error == abi::Error::InvalidArg { |
|
| 646 | + | throw abi::Error::Busy; |
|
| 647 | + | } |
|
| 648 | + | throw error; |
|
| 649 | + | }; |
|
| 650 | + | return 0; |
|
| 651 | + | } |
|
| 652 | + | case abi::Operation::EventTimeout as u64 => { |
|
| 653 | + | if arguments[1] > mem::U32_MAX as u64 |
|
| 654 | + | or arguments[2] <> 0 |
|
| 655 | + | or arguments[3] <> 0 |
|
| 656 | + | { |
|
| 657 | + | throw abi::Error::InvalidArg; |
|
| 658 | + | } |
|
| 659 | + | let current = running.current else panic "execute: no current context"; |
|
| 660 | + | try timers::request( |
|
| 661 | + | &mut shared.timers, |
|
| 662 | + | &shared.domains, |
|
| 663 | + | owner, |
|
| 664 | + | current.hart as u32, |
|
| 665 | + | running.now, |
|
| 666 | + | arguments[0], |
|
| 667 | + | arguments[1] as u32 |
|
| 668 | + | ); |
|
| 669 | + | return 0; |
|
| 670 | + | } |
|
| 671 | + | case abi::Operation::EventWait as u64 => { |
|
| 672 | + | if (arguments[0] | arguments[1] | arguments[2] | arguments[3]) <> 0 { |
|
| 673 | + | throw abi::Error::InvalidArg; |
|
| 674 | + | } |
|
| 675 | + | let current = running.current else panic "execute: no current context"; |
|
| 676 | + | let published = try domains::wait( |
|
| 677 | + | &mut shared.domains, owner, abi::reference(current.context), &mut memory.ring |
|
| 678 | + | ); |
|
| 679 | + | // Contexts readied by terminal publication may be parked on other harts. |
|
| 680 | + | if published { |
|
| 681 | + | dispatch::reschedule(budgets::wakeTargets( |
|
| 682 | + | &shared.budgets, &shared.domains, owner, running.now |
|
| 683 | + | )); |
|
| 684 | + | } |
|
| 685 | + | return 0; |
|
| 686 | + | } |
|
| 687 | + | case abi::Operation::QueryPage as u64 => { |
|
| 688 | + | let entry = try capability::lookup(&*table, handle, abi::Kind::Page, abi::Rights(0)); |
|
| 689 | + | let page = try pages::get(&shared.pages, entry.object); |
|
| 690 | + | try reply(&*shared, &*table, stack, arguments, &[page.base, page.count as u64]); |
|
| 691 | + | return 0; |
|
| 692 | + | } |
|
| 693 | + | case abi::Operation::QueryDevice as u64 => { |
|
| 694 | + | let entry = try capability::lookup(&*table, handle, abi::Kind::Device, abi::Rights(0)); |
|
| 695 | + | let device = try devices::get(&shared.devices, entry.object); |
|
| 696 | + | try reply(&*shared, &*table, stack, arguments, &[device.info.base, device.info.size]); |
|
| 697 | + | return 0; |
|
| 698 | + | } |
|
| 699 | + | case abi::Operation::QueryInterrupt as u64 => { |
|
| 700 | + | let number = try interrupts::query(&shared.interrupts, &*table, handle); |
|
| 701 | + | if arguments[2] <> @sizeOf(abi::InterruptInfo) as u64 |
|
| 702 | + | or arguments[3] <> 0 |
|
| 703 | + | or not buffer( |
|
| 704 | + | &*shared, &*table, stack, arguments[1], 4, abi::Rights(abi::WRITE), 4 |
|
| 705 | + | ) |
|
| 706 | + | { |
|
| 707 | + | throw abi::Error::InvalidArg; |
|
| 708 | + | } |
|
| 709 | + | try physical::write32(&shared.pages.backings.pool, arguments[1], number) catch { |
|
| 710 | + | throw abi::Error::InvalidArg; |
|
| 711 | + | }; |
|
| 712 | + | return 0; |
|
| 713 | + | } |
|
| 714 | + | case abi::Operation::QueryDomain as u64 => { |
|
| 715 | + | let entry = try capability::lookup(&*table, handle, abi::Kind::Domain, abi::Rights(0)); |
|
| 716 | + | // A dead domain keeps its record until its identity is released, so the |
|
| 717 | + | // query reports Dead instead of failing until then. |
|
| 718 | + | if not slots::matches(&shared.domains.slots[..], entry.object, slots::State::Live) { |
|
| 719 | + | throw abi::Error::BadHandle; |
|
| 720 | + | } |
|
| 721 | + | let target = shared.domains.records[entry.object.index]; |
|
| 722 | + | let mut parent: u64 = 0; |
|
| 723 | + | if let object = target.parent { |
|
| 724 | + | set parent = abi::id(object); |
|
| 725 | + | } |
|
| 726 | + | try reply(&*shared, &*table, stack, arguments, &[ |
|
| 727 | + | abi::id(entry.object), abi::id(target.creator), parent, target.state as u64 |
|
| 728 | + | ]); |
|
| 729 | + | return 0; |
|
| 730 | + | } |
|
| 731 | + | case abi::Operation::QueryEvents as u64 => { |
|
| 732 | + | let entry = try capability::lookup(&*table, handle, abi::Kind::Events, abi::Rights(0)); |
|
| 733 | + | let target = try domains::get(&shared.domains, entry.object); |
|
| 734 | + | if target.state == domains::Lifecycle::Dead { |
|
| 735 | + | throw abi::Error::BadHandle; |
|
| 736 | + | } |
|
| 737 | + | try reply(&*shared, &*table, stack, arguments, &[events::CAPACITY as u64]); |
|
| 738 | + | return 0; |
|
| 739 | + | } |
|
| 740 | + | case abi::Operation::QueryImage as u64 => { |
|
| 741 | + | let object = try registry::image(&shared.registry, &*table, handle, abi::Rights( |
|
| 742 | + | abi::READ |
|
| 743 | + | )); |
|
| 744 | + | let package = try! registry::get(&shared.registry, object); |
|
| 745 | + | let mut entry: u64 = 0; |
|
| 746 | + | if let address = package.entry { |
|
| 747 | + | set entry = address; |
|
| 748 | + | } |
|
| 749 | + | try reply( |
|
| 750 | + | &*shared, |
|
| 751 | + | &*table, |
|
| 752 | + | stack, |
|
| 753 | + | arguments, |
|
| 754 | + | &[ |
|
| 755 | + | abi::id(object), |
|
| 756 | + | entry, |
|
| 757 | + | package.codeAddress, |
|
| 758 | + | package.code.len as u64 * 4, |
|
| 759 | + | package.memory as u64, |
|
| 760 | + | ] |
|
| 761 | + | ); |
|
| 762 | + | return 0; |
|
| 763 | + | } |
|
| 764 | + | case abi::Operation::ContextDestroy as u64 => { |
|
| 765 | + | if arguments[2] <> 0 or arguments[3] <> 0 { |
|
| 766 | + | throw abi::Error::InvalidArg; |
|
| 767 | + | } |
|
| 768 | + | try domains::contextDestroy( |
|
| 769 | + | &mut shared.domains, |
|
| 770 | + | &mut shared.pages, |
|
| 771 | + | &*table, |
|
| 772 | + | handle, |
|
| 773 | + | abi::reference(arguments[1]) |
|
| 774 | + | ); |
|
| 775 | + | return 0; |
|
| 776 | + | } |
|
| 777 | + | case abi::Operation::QueryContext as u64 => { |
|
| 778 | + | let permission = try capability::authority(&*table, handle, abi::Rights(0)); |
|
| 779 | + | let target = try domains::get(&shared.domains, permission.object); |
|
| 780 | + | if target.state == domains::Lifecycle::Dead { |
|
| 781 | + | throw abi::Error::BadHandle; |
|
| 782 | + | } |
|
| 783 | + | let mut object = target.initial; |
|
| 784 | + | let mut budget: u64 = 0; |
|
| 785 | + | if arguments[1] == abi::CURRENT_CONTEXT { |
|
| 786 | + | let current = running.current else panic "execute: no current context"; |
|
| 787 | + | if permission.object <> owner { |
|
| 788 | + | throw abi::Error::InvalidArg; |
|
| 789 | + | } |
|
| 790 | + | set object = abi::reference(current.context); |
|
| 791 | + | set budget = current.budget; |
|
| 792 | + | } else if arguments[1] <> 0 { |
|
| 793 | + | set object = abi::reference(arguments[1]); |
|
| 794 | + | } |
|
| 795 | + | let context = try domains::context(&shared.domains, permission.object, object); |
|
| 796 | + | let mut hart: u64 = NO_HART; |
|
| 797 | + | if let id = context.hart { |
|
| 798 | + | set hart = id as u64; |
|
| 799 | + | } |
|
| 800 | + | try reply( |
|
| 801 | + | &*shared, |
|
| 802 | + | &*table, |
|
| 803 | + | stack, |
|
| 804 | + | &[0, arguments[2], arguments[3], 0], |
|
| 805 | + | &[ |
|
| 806 | + | abi::id(object), |
|
| 807 | + | abi::id(permission.object), |
|
| 808 | + | context.userStack.start, |
|
| 809 | + | context.userStack.end, |
|
| 810 | + | context.state as u64, |
|
| 811 | + | hart, |
|
| 812 | + | budget, |
|
| 813 | + | ] |
|
| 814 | + | ); |
|
| 815 | + | return 0; |
|
| 816 | + | } |
|
| 817 | + | case abi::Operation::BudgetSplit as u64 => |
|
| 818 | + | return *try budgets::split( |
|
| 819 | + | &mut shared.budgets, table, handle, arguments[1], running.now |
|
| 820 | + | ), |
|
| 821 | + | case abi::Operation::BudgetMerge as u64 => |
|
| 822 | + | return *try budgets::merge( |
|
| 823 | + | &mut shared.budgets, table, handle, abi::Handle(arguments[1]), running.now |
|
| 824 | + | ), |
|
| 825 | + | case abi::Operation::QueryBudget as u64 => { |
|
| 826 | + | let window = try budgets::query(&shared.budgets, &*table, handle); |
|
| 827 | + | let mut context: u64 = 0; |
|
| 828 | + | if let object = window.context { |
|
| 829 | + | set context = abi::id(object); |
|
| 830 | + | } |
|
| 831 | + | try reply(&*shared, &*table, stack, arguments, &[ |
|
| 832 | + | window.hart as u64, window.start, window.end, context |
|
| 833 | + | ]); |
|
| 834 | + | return 0; |
|
| 835 | + | } |
|
| 836 | + | case abi::Operation::BudgetBind as u64 => |
|
| 837 | + | return *try rebind( |
|
| 838 | + | shared, |
|
| 839 | + | table, |
|
| 840 | + | budgets::Binding { |
|
| 841 | + | budget: handle, |
|
| 842 | + | domain: abi::Handle(arguments[1]), |
|
| 843 | + | context: abi::reference(arguments[2]), |
|
| 844 | + | }, |
|
| 845 | + | running.now |
|
| 846 | + | ), |
|
| 847 | + | else => throw abi::Error::InvalidArg, |
|
| 848 | + | } |
|
| 849 | + | } |
|
| 850 | + | ||
| 851 | + | /// Admit a call, then route it: terminate, queue it for the hart that owns its |
|
| 852 | + | /// budget, or execute it on the calling hart. |
|
| 853 | + | export fn submit( |
|
| 854 | + | shared: &mut state::Shared, |
|
| 855 | + | domain: &domains::Domain, |
|
| 856 | + | memory: &mut domains::Memory, |
|
| 857 | + | operation: u64, |
|
| 858 | + | arguments: &[u64], |
|
| 859 | + | hart: u32, |
|
| 860 | + | running: Running |
|
| 861 | + | ) -> Outcome throws (abi::Error) { |
|
| 862 | + | assert arguments.len == 4; |
|
| 863 | + | try admit(&*shared, domain, &memory.table, operation, abi::Handle(arguments[0])); |
|
| 864 | + | match operation { |
|
| 865 | + | case |
|
| 866 | + | abi::Operation::DomainDestroy as u64, |
|
| 867 | + | abi::Operation::DomainAbort as u64, |
|
| 868 | + | abi::Operation::DomainExit as u64 => |
|
| 869 | + | return Outcome::Stopped(try terminate( |
|
| 870 | + | shared, domain, &memory.table, operation, arguments |
|
| 871 | + | )), |
|
| 872 | + | else => {} |
|
| 873 | + | } |
|
| 874 | + | if operation == abi::Operation::PageAccess as u64 { |
|
| 875 | + | return Outcome::Region(try access::pageAccess( |
|
| 876 | + | shared, domain, memory, abi::Handle(arguments[0]), arguments |
|
| 877 | + | )); |
|
| 878 | + | } |
|
| 879 | + | let target = try budgetHart(&shared.budgets, &memory.table, operation, arguments); |
|
| 880 | + | if let destination = target { |
|
| 881 | + | if destination <> hart { |
|
| 882 | + | let current = running.current else panic "submit: no current context"; |
|
| 883 | + | let call = remote::Call { |
|
| 884 | + | owner: memory.table.owner, |
|
| 885 | + | context: abi::reference(current.context), |
|
| 886 | + | operation, |
|
| 887 | + | arguments: [arguments[0], arguments[1], arguments[2], arguments[3]], |
|
| 888 | + | }; |
|
| 889 | + | let request = try remote::send( |
|
| 890 | + | &mut shared.remote, hart, destination, remote::Action::Budget(call) |
|
| 891 | + | ); |
|
| 892 | + | assert shared.domains.contexts[call.context.index].pending == nil; |
|
| 893 | + | set shared.domains.contexts[call.context.index].pending = request; |
|
| 894 | + | return Outcome::Forwarded(Forward { request, hart: destination }); |
|
| 895 | + | } |
|
| 896 | + | } |
|
| 897 | + | return Outcome::Value(try execute(shared, domain, memory, operation, arguments, running)); |
|
| 898 | + | } |
|
| 899 | + | ||
| 900 | + | /// Execute a queued budget change at its owning hart's dispatch boundary. |
|
| 901 | + | fn settle( |
|
| 902 | + | shared: &mut state::Shared, |
|
| 903 | + | domain: &domains::Domain, |
|
| 904 | + | memory: &mut domains::Memory, |
|
| 905 | + | call: remote::Call, |
|
| 906 | + | hart: u32, |
|
| 907 | + | now: u64 |
|
| 908 | + | ) -> remote::Reply { |
|
| 909 | + | if domain.state <> domains::Lifecycle::Active { |
|
| 910 | + | return remote::Reply::Error(abi::Error::BadHandle); |
|
| 911 | + | } |
|
| 912 | + | let target = try budgetHart( |
|
| 913 | + | &shared.budgets, &memory.table, call.operation, &call.arguments[..] |
|
| 914 | + | ) catch error { |
|
| 915 | + | return remote::Reply::Error(error); |
|
| 916 | + | }; |
|
| 917 | + | if target <> hart { |
|
| 918 | + | return remote::Reply::Error(abi::Error::BadHandle); |
|
| 919 | + | } |
|
| 920 | + | let result = try execute(shared, domain, memory, call.operation, &call.arguments[..], Running { |
|
| 921 | + | now, current: nil |
|
| 922 | + | }) catch error { |
|
| 923 | + | return remote::Reply::Error(error); |
|
| 924 | + | }; |
|
| 925 | + | return remote::Reply::Value(result); |
|
| 926 | + | } |
|
| 927 | + | ||
| 928 | + | /// Reserve remote dispatch capacity, execute activation or a wakeup, and publish |
|
| 929 | + | /// the readiness request for every other hart that retains the receiver's contexts. |
|
| 930 | + | export fn publishReady( |
|
| 931 | + | shared: &mut state::Shared, |
|
| 932 | + | domain: &domains::Domain, |
|
| 933 | + | memory: &mut domains::Memory, |
|
| 934 | + | operation: u64, |
|
| 935 | + | arguments: &[u64], |
|
| 936 | + | source: u32, |
|
| 937 | + | running: Running |
|
| 938 | + | ) -> remote::Batch |
|
| 939 | + | throws (abi::Error) |
|
| 940 | + | { |
|
| 941 | + | if domain.state <> domains::Lifecycle::Active { |
|
| 942 | + | throw abi::Error::BadHandle; |
|
| 943 | + | } |
|
| 944 | + | let current = running.current else panic "publishReady: no current context"; |
|
| 945 | + | let rights = abi::EXECUTE if operation == abi::Operation::DomainActivate as u64 else abi::WAKE; |
|
| 946 | + | let permit = try capability::lookup( |
|
| 947 | + | &memory.table, abi::Handle(arguments[0]), abi::Kind::Domain, abi::Rights(rights) |
|
| 948 | + | ); |
|
| 949 | + | let targets = budgets::wakeTargets( |
|
| 950 | + | &shared.budgets, &shared.domains, permit.object, running.now |
|
| 951 | + | ) & ~(1 << source); |
|
| 952 | + | let pending = try remote::reserve(&mut shared.remote, source, targets); |
|
| 953 | + | let result = try execute(shared, domain, memory, operation, arguments, running) catch error { |
|
| 954 | + | remote::cancelReservation(&mut shared.remote, pending); |
|
| 955 | + | throw error; |
|
| 956 | + | }; |
|
| 957 | + | return remote::publish(&mut shared.remote, pending, remote::Action::Ready( |
|
| 958 | + | abi::reference(current.context) |
|
| 959 | + | )); |
|
| 960 | + | } |
|
| 961 | + | ||
| 962 | + | // Reservations // |
|
| 963 | + | ||
| 964 | + | /// Reserve a new domain's storage and retain it under the calling context. |
|
| 965 | + | export fn reserveDomain( |
|
| 966 | + | shared: &mut state::Shared, |
|
| 967 | + | domain: &domains::Domain, |
|
| 968 | + | table: &mut capability::Table, |
|
| 969 | + | arguments: &[u64], |
|
| 970 | + | current: ?abi::CurrentContextInfo |
|
| 971 | + | ) -> lifecycle::DomainHeld |
|
| 972 | + | throws (abi::Error) |
|
| 973 | + | { |
|
| 974 | + | let running = current else panic "reserveDomain: no current context"; |
|
| 975 | + | try admit(&*shared, domain, &*table, abi::Operation::DomainCreate as u64, abi::Handle( |
|
| 976 | + | arguments[0] |
|
| 977 | + | )); |
|
| 978 | + | let pending = try domains::reserve( |
|
| 979 | + | &mut shared.domains, |
|
| 980 | + | &mut shared.pages.backings, |
|
| 981 | + | &shared.registry, |
|
| 982 | + | table, |
|
| 983 | + | abi::Handle(arguments[0]), |
|
| 984 | + | abi::Handle(arguments[1]) |
|
| 985 | + | ); |
|
| 986 | + | return lifecycle::holdDomain( |
|
| 987 | + | &mut shared.lifecycle, &mut shared.domains, abi::reference(running.context), pending |
|
| 988 | + | ); |
|
| 989 | + | } |
|
| 990 | + | ||
| 991 | + | /// Return the reservation of a domain whose private initialization failed. |
|
| 992 | + | export fn abandonDomain( |
|
| 993 | + | shared: &mut state::Shared, |
|
| 994 | + | table: &mut capability::Table, |
|
| 995 | + | lease: lifecycle::Lease, |
|
| 996 | + | access: domains::CreationAccess |
|
| 997 | + | ) { |
|
| 998 | + | let failed = lifecycle::takeDomain(&mut shared.lifecycle, &mut shared.domains, lease, access); |
|
| 999 | + | domains::cancel(&mut shared.domains, &mut shared.pages.backings, table, failed); |
|
| 1000 | + | } |
|
| 1001 | + | ||
| 1002 | + | /// Publish an initialized domain unless its creator died during initialization. |
|
| 1003 | + | export fn publishDomain( |
|
| 1004 | + | shared: &mut state::Shared, |
|
| 1005 | + | table: &mut capability::Table, |
|
| 1006 | + | lease: lifecycle::Lease, |
|
| 1007 | + | access: domains::CreationAccess |
|
| 1008 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 1009 | + | let ready = lifecycle::takeDomain(&mut shared.lifecycle, &mut shared.domains, lease, access); |
|
| 1010 | + | if not live(&shared.domains, table.owner) { |
|
| 1011 | + | domains::cancel(&mut shared.domains, &mut shared.pages.backings, table, ready); |
|
| 1012 | + | throw abi::Error::BadHandle; |
|
| 1013 | + | } |
|
| 1014 | + | return try domains::publish(&mut shared.domains, &mut shared.pages.backings, table, ready); |
|
| 1015 | + | } |
|
| 1016 | + | ||
| 1017 | + | /// Reserve zeroed pages and retain them under the calling context. |
|
| 1018 | + | export fn reservePage( |
|
| 1019 | + | shared: &mut state::Shared, |
|
| 1020 | + | domain: &domains::Domain, |
|
| 1021 | + | table: &mut capability::Table, |
|
| 1022 | + | arguments: &[u64], |
|
| 1023 | + | current: ?abi::CurrentContextInfo |
|
| 1024 | + | ) -> lifecycle::PageHeld throws (abi::Error) { |
|
| 1025 | + | let running = current else panic "reservePage: no current context"; |
|
| 1026 | + | try admit(&*shared, domain, &*table, abi::Operation::PageAllocate as u64, abi::Handle( |
|
| 1027 | + | arguments[0] |
|
| 1028 | + | )); |
|
| 1029 | + | let pending = try pages::reserve( |
|
| 1030 | + | &mut shared.pages, table, abi::Handle(arguments[0]), arguments[1] |
|
| 1031 | + | ); |
|
| 1032 | + | return lifecycle::holdPage( |
|
| 1033 | + | &mut shared.lifecycle, &mut shared.domains, abi::reference(running.context), pending |
|
| 1034 | + | ); |
|
| 1035 | + | } |
|
| 1036 | + | ||
| 1037 | + | /// Publish cleared pages unless their owner died while they were cleared. |
|
| 1038 | + | export fn publishPage( |
|
| 1039 | + | shared: &mut state::Shared, |
|
| 1040 | + | table: &mut capability::Table, |
|
| 1041 | + | lease: lifecycle::Lease, |
|
| 1042 | + | access: physical::Access |
|
| 1043 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 1044 | + | let ready = lifecycle::takePage(&mut shared.lifecycle, &mut shared.domains, lease, access); |
|
| 1045 | + | if not live(&shared.domains, table.owner) { |
|
| 1046 | + | pages::cancel(&mut shared.pages, table, ready); |
|
| 1047 | + | throw abi::Error::BadHandle; |
|
| 1048 | + | } |
|
| 1049 | + | return pages::publish(&mut shared.pages, table, ready); |
|
| 1050 | + | } |
|
| 1051 | + | ||
| 1052 | + | /// Read a context start record and reserve the context under the calling context. |
|
| 1053 | + | export fn reserveContext( |
|
| 1054 | + | shared: &mut state::Shared, |
|
| 1055 | + | domain: &domains::Domain, |
|
| 1056 | + | memory: &mut domains::Memory, |
|
| 1057 | + | arguments: &[u64], |
|
| 1058 | + | current: ?abi::CurrentContextInfo |
|
| 1059 | + | ) -> lifecycle::ContextHeld |
|
| 1060 | + | throws (abi::Error) |
|
| 1061 | + | { |
|
| 1062 | + | let running = current else panic "reserveContext: no current context"; |
|
| 1063 | + | try admit(&*shared, domain, &memory.table, abi::Operation::ContextCreate as u64, abi::Handle( |
|
| 1064 | + | arguments[0] |
|
| 1065 | + | )); |
|
| 1066 | + | if arguments[2] <> @sizeOf(abi::ContextStart) as u64 |
|
| 1067 | + | or arguments[3] <> 0 |
|
| 1068 | + | or not buffer( |
|
| 1069 | + | &*shared, |
|
| 1070 | + | &memory.table, |
|
| 1071 | + | domains::stackOf(&shared.domains, current), |
|
| 1072 | + | arguments[1], |
|
| 1073 | + | arguments[2], |
|
| 1074 | + | abi::Rights(abi::READ), |
|
| 1075 | + | 8 |
|
| 1076 | + | ) |
|
| 1077 | + | { |
|
| 1078 | + | throw abi::Error::InvalidArg; |
|
| 1079 | + | } |
|
| 1080 | + | let mut words: [u64; 4] = undefined; |
|
| 1081 | + | try physical::readWords(&shared.pages.backings.pool, arguments[1], &mut words[..]) catch { |
|
| 1082 | + | throw abi::Error::InvalidArg; |
|
| 1083 | + | }; |
|
| 1084 | + | let start = abi::ContextStart { |
|
| 1085 | + | entry: words[0], stack: words[1], args: words[2], size: words[3] |
|
| 1086 | + | }; |
|
| 1087 | + | let pending = try reserveIn(shared, memory, abi::Handle(arguments[0]), start); |
|
| 1088 | + | return lifecycle::holdContext( |
|
| 1089 | + | &mut shared.lifecycle, &mut shared.domains, abi::reference(running.context), pending |
|
| 1090 | + | ); |
|
| 1091 | + | } |
|
| 1092 | + | ||
| 1093 | + | /// Publish an initialized context unless its owner or target died while it was cleared. |
|
| 1094 | + | export fn publishContext( |
|
| 1095 | + | shared: &mut state::Shared, owner: abi::Ref, lease: lifecycle::Lease, access: physical::Access |
|
| 1096 | + | ) -> abi::Ref throws (abi::Error) { |
|
| 1097 | + | let target = lifecycle::contextTarget(&shared.lifecycle, &lease); |
|
| 1098 | + | let ready = lifecycle::takeContext(&mut shared.lifecycle, &mut shared.domains, lease, access); |
|
| 1099 | + | if not live(&shared.domains, owner) or not live(&shared.domains, target) { |
|
| 1100 | + | domains::contextCancel(&mut shared.domains, &mut shared.pages, ready); |
|
| 1101 | + | throw abi::Error::BadHandle; |
|
| 1102 | + | } |
|
| 1103 | + | return domains::contextPublish(&mut shared.domains, ready); |
|
| 1104 | + | } |
|
| 1105 | + | ||
| 1106 | + | /// Reserve an Image handle and the compiler workspace under the calling context. |
|
| 1107 | + | export fn reserveLoad( |
|
| 1108 | + | shared: &mut state::Shared, |
|
| 1109 | + | domain: &domains::Domain, |
|
| 1110 | + | table: &mut capability::Table, |
|
| 1111 | + | request: loader::Request, |
|
| 1112 | + | current: ?abi::CurrentContextInfo |
|
| 1113 | + | ) -> lifecycle::LoadHeld |
|
| 1114 | + | throws (abi::Error) |
|
| 1115 | + | { |
|
| 1116 | + | let running = current else panic "reserveLoad: no current context"; |
|
| 1117 | + | try admit(&*shared, domain, &*table, abi::Operation::ImageLoad as u64, request.authority); |
|
| 1118 | + | let pending = try loader::reserve(&mut shared.loader, &mut shared.pages, table, request); |
|
| 1119 | + | return lifecycle::holdLoad( |
|
| 1120 | + | &mut shared.lifecycle, &mut shared.domains, abi::reference(running.context), pending |
|
| 1121 | + | ); |
|
| 1122 | + | } |
|
| 1123 | + | ||
| 1124 | + | /// Choose how a decoded load continues: finish with a resident package, or |
|
| 1125 | + | /// reserve output for compilation. A dead caller or an error cancels the load. |
|
| 1126 | + | export fn admitLoad( |
|
| 1127 | + | shared: &mut state::Shared, |
|
| 1128 | + | table: &mut capability::Table, |
|
| 1129 | + | lease: lifecycle::Lease, |
|
| 1130 | + | access: lifecycle::LoadAccess, |
|
| 1131 | + | existing: ?abi::Ref |
|
| 1132 | + | ) -> Admitted throws (abi::Error) { |
|
| 1133 | + | if not live(&shared.domains, table.owner) { |
|
| 1134 | + | lifecycle::cancelLoad( |
|
| 1135 | + | &mut shared.lifecycle, |
|
| 1136 | + | &mut shared.domains, |
|
| 1137 | + | &mut shared.pages, |
|
| 1138 | + | &mut shared.loader, |
|
| 1139 | + | &mut shared.registry, |
|
| 1140 | + | table, |
|
| 1141 | + | lease, |
|
| 1142 | + | access |
|
| 1143 | + | ); |
|
| 1144 | + | throw abi::Error::BadHandle; |
|
| 1145 | + | } |
|
| 1146 | + | if let object = existing { |
|
| 1147 | + | let result = try lifecycle::finishLoad( |
|
| 1148 | + | &mut shared.lifecycle, |
|
| 1149 | + | &mut shared.domains, |
|
| 1150 | + | &mut shared.pages, |
|
| 1151 | + | &mut shared.loader, |
|
| 1152 | + | &mut shared.registry, |
|
| 1153 | + | table, |
|
| 1154 | + | lease, |
|
| 1155 | + | lifecycle::LoadResult::Existing(object, access) |
|
| 1156 | + | ); |
|
| 1157 | + | return Admitted::Finished(result); |
|
| 1158 | + | } |
|
| 1159 | + | let output = try lifecycle::reserveOutput( |
|
| 1160 | + | &mut shared.lifecycle, &lease, &mut shared.pages, &mut shared.registry |
|
| 1161 | + | ) catch error { |
|
| 1162 | + | lifecycle::cancelLoad( |
|
| 1163 | + | &mut shared.lifecycle, |
|
| 1164 | + | &mut shared.domains, |
|
| 1165 | + | &mut shared.pages, |
|
| 1166 | + | &mut shared.loader, |
|
| 1167 | + | &mut shared.registry, |
|
| 1168 | + | table, |
|
| 1169 | + | lease, |
|
| 1170 | + | access |
|
| 1171 | + | ); |
|
| 1172 | + | throw error; |
|
| 1173 | + | }; |
|
| 1174 | + | return Admitted::Compile(lease, lifecycle::withOutput(access, output)); |
|
| 1175 | + | } |
|
| 1176 | + | ||
| 1177 | + | // Entry Points // |
|
| 1178 | + | ||
| 1179 | + | /// Admit and execute one operation for `owner` on hart 0 without taking the metadata |
|
| 1180 | + | /// lock, as if no context were running, and return both result words. Operations that |
|
| 1181 | + | /// need a running context panic. Allocation, creation, and loading are staged by the |
|
| 1182 | + | /// reserve and publish functions. The caller serializes metadata, as the unit tests do. |
|
| 1183 | + | export unsafe fn invokeReply( |
|
| 1184 | + | owner: abi::Ref, operation: u64, arguments: &[u64], now: u64 |
|
| 1185 | + | ) -> access::Reply throws (abi::Error) { |
|
| 1186 | + | assert arguments.len == 4; |
|
| 1187 | + | let shared = &mut state::SHARED; |
|
| 1188 | + | let domain = try domains::get(&shared.domains, owner); |
|
| 1189 | + | // The record came from `get`; see `Domain.memory`. |
|
| 1190 | + | let memory = &mut *domain.memory; |
|
| 1191 | + | let running = Running { now, current: nil }; |
|
| 1192 | + | match try submit(shared, &domain, memory, operation, arguments, 0, running) { |
|
| 1193 | + | case Outcome::Value(value) => return access::Reply { value, extra: 0 }, |
|
| 1194 | + | case Outcome::Region(reply) => return reply, |
|
| 1195 | + | case Outcome::Stopped(harts) => return access::Reply { value: 0, extra: 0 }, |
|
| 1196 | + | case Outcome::Forwarded(forward) => panic "invokeReply: forwarded call", |
|
| 1197 | + | } |
|
| 1198 | + | } |
|
| 1199 | + | ||
| 1200 | + | /// Like [`invokeReply`], for an operation with one result word. |
|
| 1201 | + | export unsafe fn invoke(owner: abi::Ref, operation: u64, arguments: &[u64], now: u64) -> u64 |
|
| 1202 | + | throws (abi::Error) |
|
| 1203 | + | { |
|
| 1204 | + | return (try invokeReply(owner, operation, arguments, now)).value; |
|
| 1205 | + | } |
|
| 1206 | + | ||
| 1207 | + | /// Validate a termination call for `owner` and return harts that must release stopped contexts. |
|
| 1208 | + | /// The caller serializes metadata. |
|
| 1209 | + | export unsafe fn termination(owner: abi::Ref, operation: u64, arguments: &[u64]) -> u32 |
|
| 1210 | + | throws (abi::Error) |
|
| 1211 | + | { |
|
| 1212 | + | let shared = &mut state::SHARED; |
|
| 1213 | + | let domain = try domains::get(&shared.domains, owner); |
|
| 1214 | + | // The record came from `get`; see `Domain.memory`. |
|
| 1215 | + | let table = &domain.memory.table; |
|
| 1216 | + | return try terminate(shared, &domain, table, operation, arguments); |
|
| 1217 | + | } |
|
| 1218 | + | ||
| 1219 | + | /// Execute a queued budget change at its owning hart's dispatch boundary. |
|
| 1220 | + | /// The guard proves that the caller holds the metadata lock. |
|
| 1221 | + | export fn remoteBudget( |
|
| 1222 | + | guard: &sync::Guard, shared: &mut state::Shared, call: remote::Call, hart: u32, now: u64 |
|
| 1223 | + | ) -> remote::Reply { |
|
| 1224 | + | let domain = try domains::get(&shared.domains, call.owner) catch error { |
|
| 1225 | + | return remote::Reply::Error(error); |
|
| 1226 | + | }; |
|
| 1227 | + | // The record came from `get`, and the guard serializes its metadata; see `Domain.memory`. |
|
| 1228 | + | unsafe { |
|
| 1229 | + | let memory = &mut *domain.memory; |
|
| 1230 | + | return settle(shared, &domain, memory, call, hart, now); |
|
| 1231 | + | } |
|
| 1232 | + | } |
|
| 1233 | + | ||
| 1234 | + | /// Return the compiler workspace and reserved handle of a load that cannot finish. |
|
| 1235 | + | /// The reservation pins the loading domain's storage; see `Domain.memory`. |
|
| 1236 | + | fn cancelLoad(domain: &domains::Domain, lease: lifecycle::Lease, access: lifecycle::LoadAccess) { |
|
| 1237 | + | let guard = sync::enter(); |
|
| 1238 | + | locked::cancelLoad(&guard, domain, lease, access); |
|
| 1239 | + | sync::leave(guard); |
|
| 1240 | + | } |
|
| 1241 | + | ||
| 1242 | + | /// Load a package with metadata locked only around resource transactions. |
|
| 1243 | + | /// A retained lease keeps the workspace and output allocated while compilation |
|
| 1244 | + | /// reads resident packages outside the lock. |
|
| 1245 | + | fn runtimeLoad(owner: abi::Ref, arguments: &[u64]) -> u64 throws (abi::Error) { |
|
| 1246 | + | let guard = sync::enter(); |
|
| 1247 | + | let domain = try locked::lookup(&guard, owner) catch error { |
|
| 1248 | + | sync::leave(guard); |
|
| 1249 | + | throw error; |
|
| 1250 | + | }; |
|
| 1251 | + | let begun = try locked::beginLoad(&guard, &domain, arguments) catch error { |
|
| 1252 | + | sync::leave(guard); |
|
| 1253 | + | throw error; |
|
| 1254 | + | }; |
|
| 1255 | + | sync::leave(guard); |
|
| 1256 | + | match begun { |
|
| 1257 | + | case locked::LoadBegun::Held(lease, start, access) => { |
|
| 1258 | + | // The lease keeps this call's workspace allocated and exclusive, and this |
|
| 1259 | + | // continuation is the only holder of its tokens. |
|
| 1260 | + | let input = try lifecycle::decodeLoad(&start.loading, &access, &lease) catch error { |
|
| 1261 | + | cancelLoad(&domain, lease, access); |
|
| 1262 | + | throw error; |
|
| 1263 | + | }; |
|
| 1264 | + | let existing = try lifecycle::identifyLoad( |
|
| 1265 | + | &start.resident, &start.loading, &access, &lease, &input |
|
| 1266 | + | ) catch error { |
|
| 1267 | + | cancelLoad(&domain, lease, access); |
|
| 1268 | + | throw error; |
|
| 1269 | + | }; |
|
| 1270 | + | return try finishLoad(&domain, lease, access, &input, existing); |
|
| 1271 | + | } |
|
| 1272 | + | } |
|
| 1273 | + | } |
|
| 1274 | + | ||
| 1275 | + | /// Admit the decoded load under the lock, then compile its output and publish it. |
|
| 1276 | + | fn finishLoad( |
|
| 1277 | + | domain: &domains::Domain, |
|
| 1278 | + | lease: lifecycle::Lease, |
|
| 1279 | + | access: lifecycle::LoadAccess, |
|
| 1280 | + | input: &binary::Package, |
|
| 1281 | + | existing: ?abi::Ref |
|
| 1282 | + | ) -> u64 throws (abi::Error) { |
|
| 1283 | + | let allocationGuard = sync::enter(); |
|
| 1284 | + | let admitted = try locked::continueLoad( |
|
| 1285 | + | &allocationGuard, domain, lease, access, existing |
|
| 1286 | + | ) catch error { |
|
| 1287 | + | sync::leave(allocationGuard); |
|
| 1288 | + | throw error; |
|
| 1289 | + | }; |
|
| 1290 | + | match admitted { |
|
| 1291 | + | case Admitted::Finished(handle) => { |
|
| 1292 | + | sync::leave(allocationGuard); |
|
| 1293 | + | return *handle; |
|
| 1294 | + | } |
|
| 1295 | + | case Admitted::Compile(lease, access) => { |
|
| 1296 | + | // The output was reserved above, so the capture includes its package slot. |
|
| 1297 | + | let start = locked::captureCompile(&allocationGuard, &lease); |
|
| 1298 | + | sync::leave(allocationGuard); |
|
| 1299 | + | let compiled = try lifecycle::compileLoad( |
|
| 1300 | + | &start.resident, &start.loading, &access, &lease, input |
|
| 1301 | + | ) catch error { |
|
| 1302 | + | cancelLoad(domain, lease, access); |
|
| 1303 | + | throw error; |
|
| 1304 | + | }; |
|
| 1305 | + | let publicationGuard = sync::enter(); |
|
| 1306 | + | let result = try locked::finishLoad( |
|
| 1307 | + | &publicationGuard, domain, lease, access, compiled |
|
| 1308 | + | ) catch error { |
|
| 1309 | + | sync::leave(publicationGuard); |
|
| 1310 | + | throw error; |
|
| 1311 | + | }; |
|
| 1312 | + | sync::leave(publicationGuard); |
|
| 1313 | + | return *result; |
|
| 1314 | + | } |
|
| 1315 | + | } |
|
| 1316 | + | } |
|
| 1317 | + | ||
| 1318 | + | /// Create a domain: reserve under the lock, initialize its private storage unlocked, then publish. |
|
| 1319 | + | fn createDomain(owner: abi::Ref, arguments: &[u64]) -> u64 throws (abi::Error) { |
|
| 1320 | + | let guard = sync::enter(); |
|
| 1321 | + | let domain = try locked::lookup(&guard, owner) catch error { |
|
| 1322 | + | sync::leave(guard); |
|
| 1323 | + | throw error; |
|
| 1324 | + | }; |
|
| 1325 | + | let begun = try locked::beginDomain(&guard, &domain, arguments) catch error { |
|
| 1326 | + | sync::leave(guard); |
|
| 1327 | + | throw error; |
|
| 1328 | + | }; |
|
| 1329 | + | sync::leave(guard); |
|
| 1330 | + | match begun { |
|
| 1331 | + | case locked::DomainBegun::Held(lease, start, access) => { |
|
| 1332 | + | // The lease keeps this call's creation storage private, and this |
|
| 1333 | + | // continuation is the only holder of its tokens. |
|
| 1334 | + | try domains::prepareCreation(&start.resident, &start.creation, &access) catch error { |
|
| 1335 | + | let failureGuard = sync::enter(); |
|
| 1336 | + | locked::abandonDomain(&failureGuard, &domain, lease, access); |
|
| 1337 | + | sync::leave(failureGuard); |
|
| 1338 | + | throw error; |
|
| 1339 | + | }; |
|
| 1340 | + | let publicationGuard = sync::enter(); |
|
| 1341 | + | let result = try locked::publishDomain( |
|
| 1342 | + | &publicationGuard, &domain, lease, access |
|
| 1343 | + | ) catch error { |
|
| 1344 | + | sync::leave(publicationGuard); |
|
| 1345 | + | throw error; |
|
| 1346 | + | }; |
|
| 1347 | + | sync::leave(publicationGuard); |
|
| 1348 | + | return *result; |
|
| 1349 | + | } |
|
| 1350 | + | } |
|
| 1351 | + | } |
|
| 1352 | + | ||
| 1353 | + | /// Allocate pages: reserve under the lock, clear the frames unlocked, then publish. |
|
| 1354 | + | fn allocatePage(owner: abi::Ref, arguments: &[u64]) -> u64 throws (abi::Error) { |
|
| 1355 | + | let guard = sync::enter(); |
|
| 1356 | + | let domain = try locked::lookup(&guard, owner) catch error { |
|
| 1357 | + | sync::leave(guard); |
|
| 1358 | + | throw error; |
|
| 1359 | + | }; |
|
| 1360 | + | let begun = try locked::beginPage(&guard, &domain, arguments) catch error { |
|
| 1361 | + | sync::leave(guard); |
|
| 1362 | + | throw error; |
|
| 1363 | + | }; |
|
| 1364 | + | sync::leave(guard); |
|
| 1365 | + | match begun { |
|
| 1366 | + | case lifecycle::PageHeld::Held(lease, access) => { |
|
| 1367 | + | // The lease keeps this call's page frames private, and this |
|
| 1368 | + | // continuation is the only holder of their token. |
|
| 1369 | + | physical::clear(&access); |
|
| 1370 | + | let publicationGuard = sync::enter(); |
|
| 1371 | + | let result = try locked::publishPage( |
|
| 1372 | + | &publicationGuard, &domain, lease, access |
|
| 1373 | + | ) catch error { |
|
| 1374 | + | sync::leave(publicationGuard); |
|
| 1375 | + | throw error; |
|
| 1376 | + | }; |
|
| 1377 | + | sync::leave(publicationGuard); |
|
| 1378 | + | return *result; |
|
| 1379 | + | } |
|
| 1380 | + | } |
|
| 1381 | + | } |
|
| 1382 | + | ||
| 1383 | + | /// Create a context: reserve under the lock, clear its stack unlocked, then publish. |
|
| 1384 | + | fn createContext(owner: abi::Ref, arguments: &[u64]) -> u64 throws (abi::Error) { |
|
| 1385 | + | let guard = sync::enter(); |
|
| 1386 | + | let domain = try locked::lookup(&guard, owner) catch error { |
|
| 1387 | + | sync::leave(guard); |
|
| 1388 | + | throw error; |
|
| 1389 | + | }; |
|
| 1390 | + | let begun = try locked::beginContext(&guard, &domain, arguments) catch error { |
|
| 1391 | + | sync::leave(guard); |
|
| 1392 | + | throw error; |
|
| 1393 | + | }; |
|
| 1394 | + | sync::leave(guard); |
|
| 1395 | + | match begun { |
|
| 1396 | + | case lifecycle::ContextHeld::Held(lease, access) => { |
|
| 1397 | + | // The lease keeps this call's stack frames private, and this |
|
| 1398 | + | // continuation is the only holder of their token. |
|
| 1399 | + | physical::clear(&access); |
|
| 1400 | + | let publicationGuard = sync::enter(); |
|
| 1401 | + | let result = try locked::publishContext( |
|
| 1402 | + | &publicationGuard, owner, lease, access |
|
| 1403 | + | ) catch error { |
|
| 1404 | + | sync::leave(publicationGuard); |
|
| 1405 | + | throw error; |
|
| 1406 | + | }; |
|
| 1407 | + | sync::leave(publicationGuard); |
|
| 1408 | + | return abi::id(result); |
|
| 1409 | + | } |
|
| 1410 | + | } |
|
| 1411 | + | } |
|
| 1412 | + | ||
| 1413 | + | /// Run any other call in one locked region. A result that needs hart signalling is |
|
| 1414 | + | /// acted on before the lock is released, and a forwarded call is awaited after it. |
|
| 1415 | + | fn submitCall(owner: abi::Ref, operation: u64, arguments: &[u64]) -> access::Reply |
|
| 1416 | + | throws (abi::Error) |
|
| 1417 | + | { |
|
| 1418 | + | let guard = sync::enter(); |
|
| 1419 | + | let domain = try locked::lookup(&guard, owner) catch error { |
|
| 1420 | + | sync::leave(guard); |
|
| 1421 | + | throw error; |
|
| 1422 | + | }; |
|
| 1423 | + | let source = dispatch::hart(); |
|
| 1424 | + | let outcome = try locked::submit(&guard, &domain, operation, arguments, source) catch error { |
|
| 1425 | + | sync::leave(guard); |
|
| 1426 | + | throw error; |
|
| 1427 | + | }; |
|
| 1428 | + | if let case Outcome::Stopped(harts) = outcome { |
|
| 1429 | + | dispatch::reschedule(harts); |
|
| 1430 | + | } |
|
| 1431 | + | if let case Outcome::Forwarded(forward) = outcome { |
|
| 1432 | + | dispatch::signal(forward.hart); |
|
| 1433 | + | } |
|
| 1434 | + | sync::leave(guard); |
|
| 1435 | + | match outcome { |
|
| 1436 | + | case Outcome::Value(value) => return access::Reply { value, extra: 0 }, |
|
| 1437 | + | case Outcome::Region(reply) => return reply, |
|
| 1438 | + | case Outcome::Stopped(harts) => return access::Reply { value: 0, extra: 0 }, |
|
| 1439 | + | case Outcome::Forwarded(forward) => { |
|
| 1440 | + | return access::Reply { |
|
| 1441 | + | value: try dispatch::awaitRequest(source, forward.request), extra: 0 |
|
| 1442 | + | }; |
|
| 1443 | + | } |
|
| 1444 | + | } |
|
| 1445 | + | } |
|
| 1446 | + | ||
| 1447 | + | /// Execute a call with short metadata transactions and private bulk initialization, and |
|
| 1448 | + | /// return both result words. Context ownership and pending calls retain storage across |
|
| 1449 | + | /// dispatch boundaries. |
|
| 1450 | + | export fn respond(owner: abi::Ref, operation: u64, arguments: &[u64], now: u64) -> access::Reply |
|
| 1451 | + | throws (abi::Error) |
|
| 1452 | + | { |
|
| 1453 | + | assert arguments.len == 4; |
|
| 1454 | + | match operation { |
|
| 1455 | + | case abi::Operation::DomainActivate as u64, abi::Operation::EventWakeup as u64 => { |
|
| 1456 | + | return access::Reply { value: try runtimeReady(owner, operation, arguments), extra: 0 }; |
|
| 1457 | + | } |
|
| 1458 | + | case abi::Operation::ImageLoad as u64 => { |
|
| 1459 | + | return access::Reply { value: try runtimeLoad(owner, arguments), extra: 0 }; |
|
| 1460 | + | } |
|
| 1461 | + | case abi::Operation::DomainCreate as u64 => { |
|
| 1462 | + | return access::Reply { value: try createDomain(owner, arguments), extra: 0 }; |
|
| 1463 | + | } |
|
| 1464 | + | case abi::Operation::PageAllocate as u64 => { |
|
| 1465 | + | return access::Reply { value: try allocatePage(owner, arguments), extra: 0 }; |
|
| 1466 | + | } |
|
| 1467 | + | case abi::Operation::ContextCreate as u64 => { |
|
| 1468 | + | return access::Reply { value: try createContext(owner, arguments), extra: 0 }; |
|
| 1469 | + | } |
|
| 1470 | + | else => return try submitCall(owner, operation, arguments), |
|
| 1471 | + | } |
|
| 1472 | + | } |
|
| 1473 | + | ||
| 1474 | + | /// Like [`respond`], for an operation with one result word. |
|
| 1475 | + | export fn synchronized(owner: abi::Ref, operation: u64, arguments: &[u64], now: u64) -> u64 |
|
| 1476 | + | throws (abi::Error) |
|
| 1477 | + | { |
|
| 1478 | + | return (try respond(owner, operation, arguments, now)).value; |
|
| 1479 | + | } |
|
| 1480 | + | ||
| 1481 | + | /// Reserve remote dispatch capacity before activation or event publication. |
|
| 1482 | + | fn runtimeReady(owner: abi::Ref, operation: u64, arguments: &[u64]) -> u64 throws (abi::Error) { |
|
| 1483 | + | if operation == abi::Operation::EventWakeup as u64 |
|
| 1484 | + | and (arguments[1] > mem::U32_MAX as u64 |
|
| 1485 | + | or arguments[2] <> 0 |
|
| 1486 | + | or arguments[3] <> 0) |
|
| 1487 | + | { |
|
| 1488 | + | throw abi::Error::InvalidArg; |
|
| 1489 | + | } |
|
| 1490 | + | let guard = sync::enter(); |
|
| 1491 | + | let domain = try locked::lookup(&guard, owner) catch error { |
|
| 1492 | + | sync::leave(guard); |
|
| 1493 | + | throw error; |
|
| 1494 | + | }; |
|
| 1495 | + | let readied = try locked::beginReady( |
|
| 1496 | + | &guard, &domain, operation, arguments, dispatch::hart() |
|
| 1497 | + | ) catch error { |
|
| 1498 | + | sync::leave(guard); |
|
| 1499 | + | throw error; |
|
| 1500 | + | }; |
|
| 1501 | + | sync::leave(guard); |
|
| 1502 | + | dispatch::finishReady(readied.context, &readied.batch); |
|
| 1503 | + | return 0; |
|
| 1504 | + | } |
|
| 1505 | + | ||
| 1506 | + | /// Complete a user ecall through the supplied execution boundary. |
|
| 1507 | + | export fn handle( |
|
| 1508 | + | owner: abi::Ref, |
|
| 1509 | + | frame: &mut trap::Frame, |
|
| 1510 | + | hart: u32, |
|
| 1511 | + | now: u64, |
|
| 1512 | + | execute: fn(abi::Ref, u64, &[u64], u64) -> access::Reply throws (abi::Error) |
|
| 1513 | + | ) { |
|
| 1514 | + | assert trap::fromUser(frame) and frame.cause == 8; |
|
| 1515 | + | assert trap::advanceCall(frame); |
|
| 1516 | + | let operation = frame.registers[*rv64::A7 as u32]; |
|
| 1517 | + | let args = try access::arguments(operation, &frame.registers[..]) catch error { |
|
| 1518 | + | set frame.registers[*rv64::A0 as u32] = 0 as u64 - error as u64; |
|
| 1519 | + | return; |
|
| 1520 | + | }; |
|
| 1521 | + | // The result is a0, and a1 is the size of a region. A call never leaves a stale a1. |
|
| 1522 | + | if operation == abi::Operation::PageAccess as u64 { |
|
| 1523 | + | set frame.registers[*rv64::A1 as u32] = 0; |
|
| 1524 | + | } |
|
| 1525 | + | let reply = try execute(owner, operation, &args[..], now) catch error { |
|
| 1526 | + | if operation == abi::Operation::DeviceAccess as u64 |
|
| 1527 | + | or (operation >= abi::Operation::QueryPage as u64 |
|
| 1528 | + | and operation <= abi::Operation::QueryEvents as u64 |
|
| 1529 | + | and error == abi::Error::BadHandle) |
|
| 1530 | + | { |
|
| 1531 | + | try execute( |
|
| 1532 | + | owner, abi::Operation::DomainAbort as u64, &[0, 0, 0, 0], now |
|
| 1533 | + | ) catch stopped { |
|
| 1534 | + | assert stopped == abi::Error::BadHandle; |
|
| 1535 | + | }; |
|
| 1536 | + | } |
|
| 1537 | + | set frame.registers[*rv64::A0 as u32] = 0 as u64 - error as u64; |
|
| 1538 | + | return; |
|
| 1539 | + | }; |
|
| 1540 | + | set frame.registers[*rv64::A0 as u32] = reply.value; |
|
| 1541 | + | if operation == abi::Operation::PageAccess as u64 { |
|
| 1542 | + | set frame.registers[*rv64::A1 as u32] = reply.extra; |
|
| 1543 | + | } |
|
| 1544 | + | } |
kernel/kernel/capability.rad
added
+124 -0
| 1 | + | //! Domain-relative capability tables. Callers serialize shared table mutations. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | use super::limits; |
|
| 5 | + | use super::slots; |
|
| 6 | + | ||
| 7 | + | /// Authority stored in one live capability slot. |
|
| 8 | + | export record Entry: Copy { |
|
| 9 | + | /// Authoritative kernel object kind. |
|
| 10 | + | kind: abi::Kind, |
|
| 11 | + | /// Generation-bearing reference in the table for this object kind. |
|
| 12 | + | object: abi::Ref, |
|
| 13 | + | /// Rights granted by this entry. |
|
| 14 | + | rights: abi::Rights, |
|
| 15 | + | } |
|
| 16 | + | ||
| 17 | + | /// Fixed capability storage for one domain. Object records own their lifetimes. |
|
| 18 | + | export record Table: Copy { |
|
| 19 | + | /// Domain whose calls select this table. |
|
| 20 | + | owner: abi::Ref, |
|
| 21 | + | /// Capability-slot generations and publication states. |
|
| 22 | + | slots: [slots::Slot; limits::HANDLES], |
|
| 23 | + | /// Payloads for live slots. Other payloads must not be read. |
|
| 24 | + | entries: [Entry; limits::HANDLES], |
|
| 25 | + | } |
|
| 26 | + | ||
| 27 | + | /// Initialize a fresh table for a valid domain reference. |
|
| 28 | + | export fn initialize(table: &mut Table, owner: abi::Ref) { |
|
| 29 | + | assert owner.index < limits::DOMAINS and owner.generation <> 0; |
|
| 30 | + | set table.owner = owner; |
|
| 31 | + | slots::initialize(&mut table.slots[..]); |
|
| 32 | + | } |
|
| 33 | + | ||
| 34 | + | /// Install authority after its object reference and lifetime have been acquired. |
|
| 35 | + | /// Invalid metadata or exhausted capacity leaves the table unchanged. |
|
| 36 | + | export fn install(table: &mut Table, entry: Entry) -> abi::Handle throws (abi::Error) { |
|
| 37 | + | if entry.kind == abi::Kind::Empty or entry.object.generation == 0 { |
|
| 38 | + | throw abi::Error::InvalidArg; |
|
| 39 | + | } |
|
| 40 | + | let rights = try abi::rights(*entry.rights as u64); |
|
| 41 | + | let reservation = try slots::reserve(&mut table.slots[..]); |
|
| 42 | + | return publish(table, reservation, entry); |
|
| 43 | + | } |
|
| 44 | + | ||
| 45 | + | /// Publish valid authority into a reservation from this table. |
|
| 46 | + | /// The caller has validated metadata and acquired the object's lifetime reference. |
|
| 47 | + | export fn publish(table: &mut Table, reservation: slots::Reservation, entry: Entry) -> abi::Handle { |
|
| 48 | + | let object = slots::reference(&reservation); |
|
| 49 | + | assert slots::matches(&table.slots[..], object, slots::State::Reserved); |
|
| 50 | + | set table.entries[object.index] = entry; |
|
| 51 | + | let live = try! slots::commit(&mut table.slots[..], reservation); |
|
| 52 | + | return try! abi::handle(entry.kind, live); |
|
| 53 | + | } |
|
| 54 | + | ||
| 55 | + | /// Read authoritative metadata after checking all encoded handle fields. |
|
| 56 | + | /// The caller validates the returned object reference against its object table. |
|
| 57 | + | export fn get(table: &Table, handle: abi::Handle) -> Entry throws (abi::Error) { |
|
| 58 | + | let decoded = try abi::decode(handle); |
|
| 59 | + | if not slots::matches(&table.slots[..], decoded.object, slots::State::Live) { |
|
| 60 | + | throw abi::Error::BadHandle; |
|
| 61 | + | } |
|
| 62 | + | let entry = table.entries[decoded.object.index]; |
|
| 63 | + | if entry.kind <> decoded.kind { |
|
| 64 | + | throw abi::Error::BadHandle; |
|
| 65 | + | } |
|
| 66 | + | return entry; |
|
| 67 | + | } |
|
| 68 | + | ||
| 69 | + | /// Require a live capability of the requested kind with all requested rights. |
|
| 70 | + | export fn lookup(table: &Table, handle: abi::Handle, kind: abi::Kind, rights: abi::Rights) -> Entry |
|
| 71 | + | throws (abi::Error) |
|
| 72 | + | { |
|
| 73 | + | let entry = try get(table, handle); |
|
| 74 | + | if entry.kind <> kind { |
|
| 75 | + | throw abi::Error::BadHandle; |
|
| 76 | + | } |
|
| 77 | + | if not abi::permits(entry.rights, rights) { |
|
| 78 | + | throw abi::Error::Denied; |
|
| 79 | + | } |
|
| 80 | + | return entry; |
|
| 81 | + | } |
|
| 82 | + | ||
| 83 | + | /// Obtain the current typed handle at a caller-relative slot index. |
|
| 84 | + | export fn at(table: &Table, index: u32, kind: abi::Kind) -> abi::Handle throws (abi::Error) { |
|
| 85 | + | if index >= limits::HANDLES or table.slots[index].state <> slots::State::Live { |
|
| 86 | + | throw abi::Error::BadHandle; |
|
| 87 | + | } |
|
| 88 | + | if table.entries[index].kind <> kind { |
|
| 89 | + | throw abi::Error::BadHandle; |
|
| 90 | + | } |
|
| 91 | + | return try! abi::handle(kind, abi::Ref { index, generation: table.slots[index].generation }); |
|
| 92 | + | } |
|
| 93 | + | ||
| 94 | + | /// Invalidate a handle and return the payload for object-specific release. |
|
| 95 | + | /// The caller completes payload teardown in the same serialized transaction. |
|
| 96 | + | export fn invalidate(table: &mut Table, handle: abi::Handle) -> Entry throws (abi::Error) { |
|
| 97 | + | let entry = try get(table, handle); |
|
| 98 | + | let decoded = try! abi::decode(handle); |
|
| 99 | + | try slots::release(&mut table.slots[..], decoded.object); |
|
| 100 | + | return entry; |
|
| 101 | + | } |
|
| 102 | + | ||
| 103 | + | /// Resolve resource authority. Zero selects the first live self-Domain entry |
|
| 104 | + | /// with all requested rights. Nonzero handles use ordinary Domain lookup. |
|
| 105 | + | export fn authority(table: &Table, handle: abi::Handle, rights: abi::Rights) -> Entry |
|
| 106 | + | throws (abi::Error) |
|
| 107 | + | { |
|
| 108 | + | if *handle <> 0 { |
|
| 109 | + | return try lookup(table, handle, abi::Kind::Domain, rights); |
|
| 110 | + | } |
|
| 111 | + | for i in 0..limits::HANDLES { |
|
| 112 | + | if table.slots[i].state <> slots::State::Live { |
|
| 113 | + | continue; |
|
| 114 | + | } |
|
| 115 | + | let entry = table.entries[i]; |
|
| 116 | + | if entry.kind == abi::Kind::Domain |
|
| 117 | + | and entry.object == table.owner |
|
| 118 | + | and abi::permits(entry.rights, rights) |
|
| 119 | + | { |
|
| 120 | + | return entry; |
|
| 121 | + | } |
|
| 122 | + | } |
|
| 123 | + | throw abi::Error::Denied; |
|
| 124 | + | } |
kernel/kernel/devices.rad
added
+110 -0
| 1 | + | //! Immutable MMIO regions and domain-relative device authority. |
|
| 2 | + | use super::abi; |
|
| 3 | + | use super::limits; |
|
| 4 | + | use super::platform; |
|
| 5 | + | use super::capability; |
|
| 6 | + | use std::mem; |
|
| 7 | + | use std::arch::rv64; |
|
| 8 | + | ||
| 9 | + | /// One platform register region available to user-space drivers. |
|
| 10 | + | export record Region: Copy { |
|
| 11 | + | /// Platform mechanism used during bootstrap handoff. |
|
| 12 | + | kind: platform::Kind, |
|
| 13 | + | /// Physical register extent returned by QueryDevice. |
|
| 14 | + | info: abi::DeviceInfo, |
|
| 15 | + | /// Maximum rights installed by bootstrap. |
|
| 16 | + | rights: abi::Rights, |
|
| 17 | + | } |
|
| 18 | + | ||
| 19 | + | /// Device identities retained for the complete boot lifetime. |
|
| 20 | + | export record Store: Copy { |
|
| 21 | + | /// Exported regions in device-tree order. |
|
| 22 | + | regions: [Region; limits::DEVICES], |
|
| 23 | + | /// Number of published regions. |
|
| 24 | + | count: u32, |
|
| 25 | + | } |
|
| 26 | + | ||
| 27 | + | /// Publish driver regions and the read-only CLINT time register. |
|
| 28 | + | /// Interrupt-controller and timer-programming registers stay kernel-private. |
|
| 29 | + | export fn initialize(store: &mut Store, machine: &platform::Platform) throws (abi::Error) { |
|
| 30 | + | set store.count = 0; |
|
| 31 | + | if machine.deviceCount > limits::DEVICES { |
|
| 32 | + | throw abi::Error::InvalidArg; |
|
| 33 | + | } |
|
| 34 | + | let mut count: u32 = 0; |
|
| 35 | + | for i in 0..machine.deviceCount { |
|
| 36 | + | let device = machine.devices[i]; |
|
| 37 | + | if device.memory.start >= device.memory.end { |
|
| 38 | + | throw abi::Error::InvalidArg; |
|
| 39 | + | } |
|
| 40 | + | if device.kind == platform::Kind::Plic { |
|
| 41 | + | continue; |
|
| 42 | + | } |
|
| 43 | + | let mut base = device.memory.start; |
|
| 44 | + | let mut size = device.memory.end - base; |
|
| 45 | + | let mut rights = abi::READ | abi::WRITE | abi::GRANT | abi::TRANSFER; |
|
| 46 | + | if device.kind == platform::Kind::Clint { |
|
| 47 | + | if size < platform::CLINT_SIZE { |
|
| 48 | + | throw abi::Error::InvalidArg; |
|
| 49 | + | } |
|
| 50 | + | set base += platform::CLINT_CLOCK; |
|
| 51 | + | set size = rv64::DWORD_SIZE as u64; |
|
| 52 | + | set rights &= ~abi::WRITE; |
|
| 53 | + | } |
|
| 54 | + | set store.regions[count] = Region { |
|
| 55 | + | kind: device.kind, info: abi::DeviceInfo { base, size }, rights: abi::Rights(rights) |
|
| 56 | + | }; |
|
| 57 | + | set count += 1; |
|
| 58 | + | } |
|
| 59 | + | set store.count = count; |
|
| 60 | + | } |
|
| 61 | + | ||
| 62 | + | /// Resolve an immutable device identity; physical regions use generation one. |
|
| 63 | + | export fn get(store: &Store, object: abi::Ref) -> Region throws (abi::Error) { |
|
| 64 | + | if object.index >= store.count or object.generation <> 1 { |
|
| 65 | + | throw abi::Error::BadHandle; |
|
| 66 | + | } |
|
| 67 | + | return store.regions[object.index]; |
|
| 68 | + | } |
|
| 69 | + | ||
| 70 | + | /// Install bootstrap authority for one published physical device. |
|
| 71 | + | export fn install(store: &Store, table: &mut capability::Table, index: u32) -> abi::Handle |
|
| 72 | + | throws (abi::Error) |
|
| 73 | + | { |
|
| 74 | + | let object = abi::Ref { index, generation: 1 }; |
|
| 75 | + | let region = try get(store, object); |
|
| 76 | + | return try capability::install(table, capability::Entry { |
|
| 77 | + | kind: abi::Kind::Device, object, rights: region.rights |
|
| 78 | + | }); |
|
| 79 | + | } |
|
| 80 | + | ||
| 81 | + | /// Validate one register access and return its physical address to generated code. |
|
| 82 | + | export fn address( |
|
| 83 | + | store: &Store, |
|
| 84 | + | table: &capability::Table, |
|
| 85 | + | handle: abi::Handle, |
|
| 86 | + | offset: u64, |
|
| 87 | + | width: u64, |
|
| 88 | + | writing: bool |
|
| 89 | + | ) -> u64 throws (abi::Error) { |
|
| 90 | + | let mut right = abi::READ; |
|
| 91 | + | if writing { |
|
| 92 | + | set right = abi::WRITE; |
|
| 93 | + | } |
|
| 94 | + | let entry = try capability::lookup(table, handle, abi::Kind::Device, abi::Rights(right)); |
|
| 95 | + | let region = (try get(store, entry.object)).info; |
|
| 96 | + | if width <> 1 and width <> 2 and width <> 4 and width <> 8 { |
|
| 97 | + | throw abi::Error::InvalidArg; |
|
| 98 | + | } |
|
| 99 | + | if offset > region.size |
|
| 100 | + | or width > region.size - offset |
|
| 101 | + | or region.base > mem::U64_MAX - offset |
|
| 102 | + | { |
|
| 103 | + | throw abi::Error::InvalidArg; |
|
| 104 | + | } |
|
| 105 | + | let address = region.base + offset; |
|
| 106 | + | if (address & (width - 1)) <> 0 { |
|
| 107 | + | throw abi::Error::InvalidArg; |
|
| 108 | + | } |
|
| 109 | + | return address; |
|
| 110 | + | } |
kernel/kernel/dispatch.rad
added
+747 -0
| 1 | + | //! Hart-local selection of runnable CPU authority and timer boundaries. |
|
| 2 | + | ||
| 3 | + | use super::state; |
|
| 4 | + | use super::abi; |
|
| 5 | + | use super::limits; |
|
| 6 | + | use super::slots; |
|
| 7 | + | use super::budgets; |
|
| 8 | + | use super::domains; |
|
| 9 | + | use super::lifecycle; |
|
| 10 | + | use super::loader; |
|
| 11 | + | use super::registry; |
|
| 12 | + | use super::frames; |
|
| 13 | + | use super::interrupts; |
|
| 14 | + | use super::events; |
|
| 15 | + | use super::pages; |
|
| 16 | + | use super::platform; |
|
| 17 | + | use super::trap; |
|
| 18 | + | use super::range; |
|
| 19 | + | use super::calls; |
|
| 20 | + | use super::timers; |
|
| 21 | + | use super::sync; |
|
| 22 | + | use super::remote; |
|
| 23 | + | use super::plic; |
|
| 24 | + | use super::registers; |
|
| 25 | + | use std::mem; |
|
| 26 | + | use std::arch::rv64; |
|
| 27 | + | ||
| 28 | + | /// Base maintenance passes per second. |
|
| 29 | + | constant MAINTENANCE_HZ: u64 = 100; |
|
| 30 | + | /// Maximum frames reclaimed in one maintenance pass. |
|
| 31 | + | constant RECLAIM_BATCH: u32 = 256; |
|
| 32 | + | ||
| 33 | + | /// Context ownership retained by one physical hart. Only that hart's trap entries |
|
| 34 | + | /// change it; see `ACTIVE`. |
|
| 35 | + | export record State: Copy { |
|
| 36 | + | /// Context currently executing or interrupted on this hart. |
|
| 37 | + | current: ?abi::Ref, |
|
| 38 | + | /// Budget selected for the current context. |
|
| 39 | + | budget: ?abi::Ref, |
|
| 40 | + | } |
|
| 41 | + | ||
| 42 | + | /// Fixed dispatch facts of one physical hart, written once by `initialize`. |
|
| 43 | + | export record Config: Copy { |
|
| 44 | + | /// Physical hart whose windows this configuration selects. |
|
| 45 | + | hart: u32, |
|
| 46 | + | /// M-mode frame that enters the hart's idle loop. |
|
| 47 | + | idle: trap::Frame, |
|
| 48 | + | /// Firmware stack reserved for idle trap handling. |
|
| 49 | + | stack: range::Range, |
|
| 50 | + | /// Minimum ticks between scheduled maintenance passes. |
|
| 51 | + | maintenance: u64, |
|
| 52 | + | } |
|
| 53 | + | ||
| 54 | + | /// Largest multiple of the base maintenance interval that unproductive passes |
|
| 55 | + | /// back off to. Hart zero runs every pass while other harts sleep, so a pass |
|
| 56 | + | /// that cannot finish its work (such as reaping a domain whose parent has not |
|
| 57 | + | /// run) must not repeat at the base cadence. At the emulator timebase this |
|
| 58 | + | /// bounds the added latency for newly arrived work to 0.64 seconds. |
|
| 59 | + | export constant MAINTENANCE_BACKOFF: u64 = 64; |
|
| 60 | + | ||
| 61 | + | /// Offset of the shared mtime register in the CLINT window. |
|
| 62 | + | export constant CLOCK: u64 = platform::CLINT_CLOCK; |
|
| 63 | + | /// Offset of the first mtimecmp register in the CLINT window. |
|
| 64 | + | constant COMPARE: u64 = platform::CLINT_COMPARE; |
|
| 65 | + | /// Bytes a CLINT window must span to hold every supported hart's registers. |
|
| 66 | + | constant CLINT_SIZE: u64 = platform::CLINT_SIZE; |
|
| 67 | + | ||
| 68 | + | /// Validated RV64 CLINT registers owned by one hart. |
|
| 69 | + | export record Timer: Copy { |
|
| 70 | + | /// Checked CLINT register extent. |
|
| 71 | + | window: registers::Window, |
|
| 72 | + | /// Offset of this hart's mtimecmp register. |
|
| 73 | + | compare: u64, |
|
| 74 | + | /// Offset of this hart's machine software-interrupt word. |
|
| 75 | + | software: u64, |
|
| 76 | + | } |
|
| 77 | + | ||
| 78 | + | /// Timer registers of each initialized hart, which any hart reads to signal it. |
|
| 79 | + | /// Each hart writes its own entry once in `initialize` before it counts itself |
|
| 80 | + | /// arrived. The arrival counter orders that write before every read by another |
|
| 81 | + | /// hart, and the table is never written again. |
|
| 82 | + | static TIMERS: [?Timer; limits::HARTS] = [nil; limits::HARTS]; |
|
| 83 | + | /// Fixed dispatch facts of each hart. A hart reads only its own entry, which it writes |
|
| 84 | + | /// once in `initialize` before it publishes its timer. Read an entry only after the |
|
| 85 | + | /// hart's `TIMERS` entry is set. |
|
| 86 | + | static CONFIGS: [Config; limits::HARTS] = undefined; |
|
| 87 | + | /// Hart-local dispatch ownership, indexed by the executing hart. A hart accesses |
|
| 88 | + | /// only its own entry, and only with machine interrupts masked: the trap entries |
|
| 89 | + | /// `choose`, `call`, and `fault` run in trap context, and `running` requires a |
|
| 90 | + | /// metadata guard. Masking keeps a second trap entry from overlapping an access, |
|
| 91 | + | /// so no two accesses to an entry are ever live together. No other hart reads it. |
|
| 92 | + | unsafe static ACTIVE: [State; limits::HARTS] = [State { current: nil, budget: nil }; limits::HARTS]; |
|
| 93 | + | ||
| 94 | + | /// Read the executing physical hart identifier. |
|
| 95 | + | export fn hart() -> u32; |
|
| 96 | + | /// Read the current machine stack pointer. |
|
| 97 | + | fn stackPointer() -> u64; |
|
| 98 | + | /// Trap anchor of the hart currently executing this continuation. |
|
| 99 | + | /// The result is valid only while this hart's mscratch names an installed Hart. |
|
| 100 | + | unsafe fn localAnchor() -> *mut trap::Hart; |
|
| 101 | + | /// Address of the M-mode idle loop. |
|
| 102 | + | fn idleAddress() -> u64; |
|
| 103 | + | /// Enable machine interrupt sources with global interrupts disabled. |
|
| 104 | + | /// Interrupts must be masked, and handlers for the enabled sources must be installed. |
|
| 105 | + | unsafe fn enable(); |
|
| 106 | + | ||
| 107 | + | /// Initialize this hart's idle frame and validated timer before installing dispatch. |
|
| 108 | + | /// The caller is the hart `id`, runs it once with machine interrupts masked, and |
|
| 109 | + | /// does not count the hart as arrived until it returns: no hart signals this hart |
|
| 110 | + | /// or enters dispatch before then. |
|
| 111 | + | export unsafe fn initialize(machine: &platform::Platform, id: u32, kernelGp: u64) |
|
| 112 | + | throws (abi::Error) |
|
| 113 | + | { |
|
| 114 | + | assert id == hart(); |
|
| 115 | + | assert TIMERS[id] == nil; |
|
| 116 | + | let device = try timer(machine, id); |
|
| 117 | + | let stack = machine.stacks[id]; |
|
| 118 | + | let mut idle = trap::Frame { |
|
| 119 | + | registers: [0; rv64::NUM_REGISTERS], |
|
| 120 | + | pc: idleAddress(), |
|
| 121 | + | status: rv64::MSTATUS_MPP_MACHINE |
|
| 122 | + | | rv64::MSTATUS_MPIE, |
|
| 123 | + | cause: 0, |
|
| 124 | + | value: 0, |
|
| 125 | + | }; |
|
| 126 | + | set idle.registers[*rv64::SP as u32] = stack.end; |
|
| 127 | + | set idle.registers[*rv64::GP as u32] = kernelGp; |
|
| 128 | + | let mut maintenance = machine.timebase as u64 / MAINTENANCE_HZ; |
|
| 129 | + | if maintenance == 0 { |
|
| 130 | + | set maintenance = 1; |
|
| 131 | + | } |
|
| 132 | + | set CONFIGS[id] = Config { hart: id, idle, stack, maintenance }; |
|
| 133 | + | set TIMERS[id] = device; |
|
| 134 | + | arm(device, timers::NO_DEADLINE); |
|
| 135 | + | } |
|
| 136 | + | ||
| 137 | + | /// Tail-enter dispatch on the firmware stack and resume the selected frame. |
|
| 138 | + | /// The caller must pass the live trap frame and anchor of the executing hart. |
|
| 139 | + | export unsafe fn interrupt(frame: &mut trap::Frame, anchor: &mut trap::Hart) -> !; |
|
| 140 | + | ||
| 141 | + | /// A delivered request that a pass acknowledges after it selects the next frame. |
|
| 142 | + | record Delivered: Copy { |
|
| 143 | + | /// Request to acknowledge. |
|
| 144 | + | id: abi::Ref, |
|
| 145 | + | /// Hart that posted the request. |
|
| 146 | + | source: u32, |
|
| 147 | + | /// Context whose runnability the request can change, if any. |
|
| 148 | + | context: ?abi::Ref, |
|
| 149 | + | } |
|
| 150 | + | ||
| 151 | + | /// Select authority from hart-owned storage with shared metadata serialized. |
|
| 152 | + | /// The caller is the trap entry of the executing hart, with the hart's live frame |
|
| 153 | + | /// and anchor and machine interrupts masked. |
|
| 154 | + | export unsafe fn choose(frame: &mut trap::Frame, anchor: &mut trap::Hart) { |
|
| 155 | + | let id = hart(); |
|
| 156 | + | assert TIMERS[id] <> nil; |
|
| 157 | + | let config = &CONFIGS[id]; |
|
| 158 | + | let pointer = stackPointer(); |
|
| 159 | + | assert pointer >= config.stack.start and pointer < config.stack.end; |
|
| 160 | + | // The one mutable access to this hart's entry in this trap. See `ACTIVE`. |
|
| 161 | + | let local = &mut ACTIVE[id]; |
|
| 162 | + | let guard = sync::enter(); |
|
| 163 | + | // The guard serializes the shared metadata. |
|
| 164 | + | service(&guard, config, local, &mut state::SHARED, frame, anchor); |
|
| 165 | + | sync::leave(guard); |
|
| 166 | + | } |
|
| 167 | + | ||
| 168 | + | /// Run one hart's dispatch pass: save the interrupted context, serve remote |
|
| 169 | + | /// requests, timers, interrupts, and maintenance, then install the next frame. |
|
| 170 | + | /// The guard proves the metadata lock is held. |
|
| 171 | + | fn service( |
|
| 172 | + | guard: &sync::Guard, |
|
| 173 | + | config: &Config, |
|
| 174 | + | local: &mut State, |
|
| 175 | + | shared: &mut state::Shared, |
|
| 176 | + | frame: &mut trap::Frame, |
|
| 177 | + | anchor: &mut trap::Hart |
|
| 178 | + | ) { |
|
| 179 | + | let id = config.hart; |
|
| 180 | + | let device = TIMERS[id] else panic "service: hart without timer"; |
|
| 181 | + | if shared.maintenance.hart == id { |
|
| 182 | + | set shared.maintenance.hart = nil; |
|
| 183 | + | } |
|
| 184 | + | registers::write32(device.window, device.software, 0); |
|
| 185 | + | let previous = local.current; |
|
| 186 | + | save(local, &mut shared.domains, frame); |
|
| 187 | + | if trap::classify(frame.cause) == trap::Cause::External { |
|
| 188 | + | plic::claim(&mut shared.interrupts, id); |
|
| 189 | + | } |
|
| 190 | + | let blank = Delivered { id: abi::Ref { index: 0, generation: 0 }, source: 0, context: nil }; |
|
| 191 | + | let mut deliveries = [blank; limits::REMOTE_PER_HART]; |
|
| 192 | + | let mut count: u32 = 0; |
|
| 193 | + | for i in 0..limits::REMOTE_PER_HART { |
|
| 194 | + | let delivery = try! remote::take(&mut shared.remote, id) else { |
|
| 195 | + | break; |
|
| 196 | + | }; |
|
| 197 | + | let mut context: ?abi::Ref = nil; |
|
| 198 | + | match delivery.request.action { |
|
| 199 | + | case remote::Action::Reschedule => {} |
|
| 200 | + | case remote::Action::Ready(target) => { |
|
| 201 | + | set context = target; |
|
| 202 | + | } |
|
| 203 | + | case remote::Action::Budget(call) => { |
|
| 204 | + | let reply = calls::remoteBudget(guard, shared, call, id, now(device)); |
|
| 205 | + | remote::respond(&mut shared.remote, id, delivery.id, reply); |
|
| 206 | + | set context = call.context; |
|
| 207 | + | } |
|
| 208 | + | } |
|
| 209 | + | set deliveries[count] = Delivered { |
|
| 210 | + | id: delivery.id, source: delivery.request.source, context |
|
| 211 | + | }; |
|
| 212 | + | set count += 1; |
|
| 213 | + | } |
|
| 214 | + | let clock = now(device); |
|
| 215 | + | retire(shared, id, clock); |
|
| 216 | + | let maintenanceDue = clock >= shared.maintenance.next; |
|
| 217 | + | let timeout = try! timers::service( |
|
| 218 | + | &mut shared.timers, &mut shared.domains, &shared.budgets, id, clock |
|
| 219 | + | ); |
|
| 220 | + | let mut terminals: u32 = 0; |
|
| 221 | + | if shared.domains.events.pending > 0 or maintenanceDue { |
|
| 222 | + | set terminals = lifecycle::service(&mut shared.domains, &shared.budgets, clock); |
|
| 223 | + | } |
|
| 224 | + | let notified = timeout.harts |
|
| 225 | + | | plic::service( |
|
| 226 | + | &mut shared.interrupts, &mut shared.domains, &shared.budgets, clock |
|
| 227 | + | ) |
|
| 228 | + | | terminals; |
|
| 229 | + | let choice = try! exchange( |
|
| 230 | + | local, config, &shared.budgets, &mut shared.domains, frame, anchor, clock |
|
| 231 | + | ); |
|
| 232 | + | let mut deadline = choice.deadline; |
|
| 233 | + | if timeout.deadline < deadline { |
|
| 234 | + | set deadline = timeout.deadline; |
|
| 235 | + | } |
|
| 236 | + | reschedule(notified); |
|
| 237 | + | if previous <> choice.context { |
|
| 238 | + | if let context = previous { |
|
| 239 | + | signalContext(&shared.domains, &shared.budgets, context, clock); |
|
| 240 | + | } |
|
| 241 | + | } |
|
| 242 | + | for delivery in &deliveries[..count] { |
|
| 243 | + | assert remote::acknowledge(&mut shared.remote, id, delivery.id); |
|
| 244 | + | if delivery.source <> id { |
|
| 245 | + | signal(delivery.source); |
|
| 246 | + | } |
|
| 247 | + | if let context = delivery.context { |
|
| 248 | + | signalContext(&shared.domains, &shared.budgets, context, clock); |
|
| 249 | + | } |
|
| 250 | + | } |
|
| 251 | + | lifecycle::cancelRequests(&mut shared.domains, &mut shared.remote); |
|
| 252 | + | let cancelled = lifecycle::cancelAllocations( |
|
| 253 | + | &mut shared.lifecycle, |
|
| 254 | + | &mut shared.domains, |
|
| 255 | + | &mut shared.pages, |
|
| 256 | + | &mut shared.loader, |
|
| 257 | + | &mut shared.registry |
|
| 258 | + | ); |
|
| 259 | + | let mut maintenance = timeout.pending |
|
| 260 | + | or cancelled |
|
| 261 | + | or shared.domains.dead > 0 |
|
| 262 | + | or shared.pages.backings.pool.retiredCount > 0; |
|
| 263 | + | if maintenance and maintenanceDue { |
|
| 264 | + | let reaped = lifecycle::reapNext( |
|
| 265 | + | &mut shared.domains, &mut shared.pages, &mut shared.budgets, &mut shared.interrupts |
|
| 266 | + | ); |
|
| 267 | + | let reclaimed = frames::reclaim( |
|
| 268 | + | &mut shared.pages.backings.pool, RECLAIM_BATCH |
|
| 269 | + | ); |
|
| 270 | + | set maintenance = timeout.pending |
|
| 271 | + | or cancelled |
|
| 272 | + | or reaped |
|
| 273 | + | or shared.domains.dead > 0 |
|
| 274 | + | or shared.pages.backings.pool.retiredCount > 0; |
|
| 275 | + | let progress = timeout.pending or cancelled or reaped or reclaimed > 0 or terminals <> 0; |
|
| 276 | + | set shared.maintenance.interval = maintenanceInterval( |
|
| 277 | + | config.maintenance, shared.maintenance.interval, progress |
|
| 278 | + | ); |
|
| 279 | + | let after = now(device); |
|
| 280 | + | set shared.maintenance.next = timers::NO_DEADLINE; |
|
| 281 | + | if shared.maintenance.interval < mem::U64_MAX - after { |
|
| 282 | + | set shared.maintenance.next = after + shared.maintenance.interval; |
|
| 283 | + | } |
|
| 284 | + | } |
|
| 285 | + | if not maintenance { |
|
| 286 | + | set shared.maintenance.interval = config.maintenance; |
|
| 287 | + | } |
|
| 288 | + | if maintenance and (timeout.pending or shared.maintenance.hart == nil) { |
|
| 289 | + | if shared.maintenance.hart == nil { |
|
| 290 | + | set shared.maintenance.hart = id; |
|
| 291 | + | } |
|
| 292 | + | if shared.maintenance.next < deadline { |
|
| 293 | + | set deadline = shared.maintenance.next; |
|
| 294 | + | } |
|
| 295 | + | } |
|
| 296 | + | arm(device, deadline); |
|
| 297 | + | } |
|
| 298 | + | ||
| 299 | + | /// Release every expired window on `hart` together with its handle. |
|
| 300 | + | /// Only this hart selects its windows, and `save` has cleared its selection, so |
|
| 301 | + | /// no dispatch state refers to a retired window. |
|
| 302 | + | fn retire(shared: &mut state::Shared, hart: u32, now: u64) { |
|
| 303 | + | for i in 0..limits::BUDGETS { |
|
| 304 | + | if shared.budgets.slots[i].state <> slots::State::Live { |
|
| 305 | + | continue; |
|
| 306 | + | } |
|
| 307 | + | let window = shared.budgets.windows[i]; |
|
| 308 | + | if window.hart <> hart or window.end > now { |
|
| 309 | + | continue; |
|
| 310 | + | } |
|
| 311 | + | let owner = try! domains::get(&shared.domains, window.owner); |
|
| 312 | + | // A live window's handle keeps the owner's storage mapped, and the metadata |
|
| 313 | + | // lock serializes its table; see `Domain.memory`. |
|
| 314 | + | unsafe { |
|
| 315 | + | assert try! budgets::expire( |
|
| 316 | + | &mut shared.budgets, &mut owner.memory.table, window.handle, now |
|
| 317 | + | ); |
|
| 318 | + | } |
|
| 319 | + | } |
|
| 320 | + | } |
|
| 321 | + | ||
| 322 | + | /// Ticks to wait after a due maintenance pass. A productive pass, or none, |
|
| 323 | + | /// selects `base`; an unproductive one doubles `current` up to |
|
| 324 | + | /// `MAINTENANCE_BACKOFF` times `base`. |
|
| 325 | + | export fn maintenanceInterval(base: u64, current: u64, progress: bool) -> u64 { |
|
| 326 | + | if progress { |
|
| 327 | + | return base; |
|
| 328 | + | } |
|
| 329 | + | let mut cap: u64 = timers::NO_DEADLINE; |
|
| 330 | + | if base <= cap / MAINTENANCE_BACKOFF { |
|
| 331 | + | set cap = base * MAINTENANCE_BACKOFF; |
|
| 332 | + | } |
|
| 333 | + | let mut interval = current; |
|
| 334 | + | if interval < base { |
|
| 335 | + | set interval = base; |
|
| 336 | + | } |
|
| 337 | + | if interval >= cap / 2 { |
|
| 338 | + | return cap; |
|
| 339 | + | } |
|
| 340 | + | return interval * 2; |
|
| 341 | + | } |
|
| 342 | + | ||
| 343 | + | /// Signal an initialized online hart. Other harts read only its published timer. |
|
| 344 | + | export fn signal(target: u32) { |
|
| 345 | + | let device = TIMERS[target] else panic "signal: hart not initialized"; |
|
| 346 | + | registers::write32(device.window, device.software, 1); |
|
| 347 | + | } |
|
| 348 | + | ||
| 349 | + | /// Signal other harts that retain readied or stopped contexts, under the metadata lock. |
|
| 350 | + | export fn reschedule(harts: u32) { |
|
| 351 | + | let source = hart(); |
|
| 352 | + | for target in 0..limits::HARTS { |
|
| 353 | + | if target <> source and (harts & (1 << target)) <> 0 { |
|
| 354 | + | signal(target); |
|
| 355 | + | } |
|
| 356 | + | } |
|
| 357 | + | } |
|
| 358 | + | ||
| 359 | + | /// Notify the current owner or the eligible hart after a context is released. |
|
| 360 | + | fn signalContext(contexts: &domains::Store, windows: &budgets::Store, context: abi::Ref, now: u64) { |
|
| 361 | + | if not slots::matches(&contexts.contextSlots[..], context, slots::State::Live) { |
|
| 362 | + | return; |
|
| 363 | + | } |
|
| 364 | + | if let owner = contexts.contexts[context.index].hart { |
|
| 365 | + | signal(owner); |
|
| 366 | + | return; |
|
| 367 | + | } |
|
| 368 | + | for i in 0..limits::BUDGETS { |
|
| 369 | + | if windows.slots[i].state <> slots::State::Live { |
|
| 370 | + | continue; |
|
| 371 | + | } |
|
| 372 | + | let window = windows.windows[i]; |
|
| 373 | + | if window.context == context and window.start <= now and now < window.end { |
|
| 374 | + | signal(window.hart); |
|
| 375 | + | } |
|
| 376 | + | } |
|
| 377 | + | } |
|
| 378 | + | ||
| 379 | + | /// Post a local action and interrupt its target. The caller holds the metadata |
|
| 380 | + | /// lock, which serializes the request queues. |
|
| 381 | + | export fn request(queues: &mut remote::Store, target: u32, action: remote::Action) -> abi::Ref |
|
| 382 | + | throws (abi::Error) |
|
| 383 | + | { |
|
| 384 | + | let id = try remote::send(queues, hart(), target, action); |
|
| 385 | + | signal(target); |
|
| 386 | + | return id; |
|
| 387 | + | } |
|
| 388 | + | ||
| 389 | + | /// Retain the caller and signal every published readiness request under the lock. |
|
| 390 | + | export fn startReady(store: &mut domains::Store, context: abi::Ref, batch: &remote::Batch) { |
|
| 391 | + | assert store.contexts[context.index].pending == nil; |
|
| 392 | + | for target in 0..limits::HARTS { |
|
| 393 | + | if (batch.targets & (1 << target)) == 0 { |
|
| 394 | + | continue; |
|
| 395 | + | } |
|
| 396 | + | if store.contexts[context.index].pending == nil { |
|
| 397 | + | set store.contexts[context.index].pending = batch.ids[target]; |
|
| 398 | + | } |
|
| 399 | + | signal(target); |
|
| 400 | + | } |
|
| 401 | + | } |
|
| 402 | + | ||
| 403 | + | /// Consume every readiness acknowledgement before releasing the caller's storage. |
|
| 404 | + | export fn finishReady(context: abi::Ref, batch: &remote::Batch) { |
|
| 405 | + | for target in 0..limits::HARTS { |
|
| 406 | + | if (batch.targets & (1 << target)) == 0 { |
|
| 407 | + | continue; |
|
| 408 | + | } |
|
| 409 | + | waitRequest(batch.source, batch.ids[target]); |
|
| 410 | + | } |
|
| 411 | + | let guard = sync::enter(); |
|
| 412 | + | // The guard serializes the shared metadata. |
|
| 413 | + | unsafe { |
|
| 414 | + | settleReady(&mut state::SHARED.domains, &mut state::SHARED.remote, context, batch); |
|
| 415 | + | } |
|
| 416 | + | sync::leave(guard); |
|
| 417 | + | } |
|
| 418 | + | ||
| 419 | + | /// Release the readiness requests that every target acknowledged. |
|
| 420 | + | fn settleReady( |
|
| 421 | + | store: &mut domains::Store, queues: &mut remote::Store, context: abi::Ref, batch: &remote::Batch |
|
| 422 | + | ) { |
|
| 423 | + | assert slots::matches(&store.contextSlots[..], context, slots::State::Live); |
|
| 424 | + | for target in 0..limits::HARTS { |
|
| 425 | + | if (batch.targets & (1 << target)) == 0 { |
|
| 426 | + | continue; |
|
| 427 | + | } |
|
| 428 | + | assert try! remote::result(queues, batch.source, batch.ids[target]) == 0; |
|
| 429 | + | try! remote::release(queues, batch.source, batch.ids[target]); |
|
| 430 | + | } |
|
| 431 | + | set store.contexts[context.index].pending = nil; |
|
| 432 | + | } |
|
| 433 | + | ||
| 434 | + | /// Wait for an interrupt with global interrupts masked, then enable delivery. |
|
| 435 | + | /// Interrupts must be masked; the call enables delivery on return. |
|
| 436 | + | unsafe fn waitInterrupt(); |
|
| 437 | + | ||
| 438 | + | /// Release a completed call's context retention and request generation. |
|
| 439 | + | fn releaseRequest( |
|
| 440 | + | store: &mut domains::Store, queues: &mut remote::Store, source: u32, request: abi::Ref |
|
| 441 | + | ) { |
|
| 442 | + | if let case remote::Action::Budget(call) = queues.requests[request.index].action { |
|
| 443 | + | assert slots::matches(&store.contextSlots[..], call.context, slots::State::Live); |
|
| 444 | + | assert store.contexts[call.context.index].pending == request; |
|
| 445 | + | set store.contexts[call.context.index].pending = nil; |
|
| 446 | + | } |
|
| 447 | + | try! remote::release(queues, source, request); |
|
| 448 | + | } |
|
| 449 | + | ||
| 450 | + | /// Await acknowledgement while retaining the record and allowing preemption. |
|
| 451 | + | fn waitRequest(source: u32, request: abi::Ref) { |
|
| 452 | + | // The saved interrupt state returns to `restoreInterrupts` after the loop, and |
|
| 453 | + | // the loop leaves interrupts masked whenever it checks the request. Each check |
|
| 454 | + | // holds the guard, which serializes the shared metadata. |
|
| 455 | + | unsafe { |
|
| 456 | + | let interrupts = sync::maskInterrupts(); |
|
| 457 | + | loop { |
|
| 458 | + | let guard = sync::enter(); |
|
| 459 | + | let complete = try! remote::completed(&state::SHARED.remote, source, request); |
|
| 460 | + | sync::leave(guard); |
|
| 461 | + | if complete { |
|
| 462 | + | break; |
|
| 463 | + | } |
|
| 464 | + | waitInterrupt(); |
|
| 465 | + | sync::maskInterrupts(); |
|
| 466 | + | } |
|
| 467 | + | sync::restoreInterrupts(interrupts); |
|
| 468 | + | } |
|
| 469 | + | } |
|
| 470 | + | ||
| 471 | + | /// Await a retained reply through a preemptible kernel continuation. |
|
| 472 | + | export fn awaitRequest(source: u32, request: abi::Ref) -> u64 throws (abi::Error) { |
|
| 473 | + | waitRequest(source, request); |
|
| 474 | + | let guard = sync::enter(); |
|
| 475 | + | let mut result: u64 = 0; |
|
| 476 | + | // The guard serializes the shared metadata. |
|
| 477 | + | unsafe { |
|
| 478 | + | set result = try takeReply( |
|
| 479 | + | &mut state::SHARED.domains, &mut state::SHARED.remote, source, request |
|
| 480 | + | ) catch error { |
|
| 481 | + | sync::leave(guard); |
|
| 482 | + | throw error; |
|
| 483 | + | }; |
|
| 484 | + | } |
|
| 485 | + | sync::leave(guard); |
|
| 486 | + | return result; |
|
| 487 | + | } |
|
| 488 | + | ||
| 489 | + | /// Take a completed request's result and release the request in every case. |
|
| 490 | + | export fn takeReply( |
|
| 491 | + | store: &mut domains::Store, queues: &mut remote::Store, source: u32, request: abi::Ref |
|
| 492 | + | ) -> u64 throws (abi::Error) { |
|
| 493 | + | let result = try remote::result(queues, source, request) catch error { |
|
| 494 | + | releaseRequest(store, queues, source, request); |
|
| 495 | + | throw error; |
|
| 496 | + | }; |
|
| 497 | + | releaseRequest(store, queues, source, request); |
|
| 498 | + | return result; |
|
| 499 | + | } |
|
| 500 | + | ||
| 501 | + | /// Read mtime through the executing hart's validated timer mapping. |
|
| 502 | + | export fn clock() -> u64 { |
|
| 503 | + | let device = TIMERS[hart()] else panic "clock: hart not initialized"; |
|
| 504 | + | return now(device); |
|
| 505 | + | } |
|
| 506 | + | ||
| 507 | + | /// Read the running context's identity and domain-relative CPU authority. |
|
| 508 | + | /// The guard keeps interrupts masked on this hart. |
|
| 509 | + | export fn current(guard: &sync::Guard, windows: &budgets::Store) -> abi::CurrentContextInfo { |
|
| 510 | + | let info = running(guard, windows) else panic "current: no current context"; |
|
| 511 | + | return info; |
|
| 512 | + | } |
|
| 513 | + | ||
| 514 | + | /// Read the running context's identity and domain-relative CPU authority, or nil |
|
| 515 | + | /// while this hart runs no context. A running context always has a budget. |
|
| 516 | + | /// The guard keeps interrupts masked on this hart, so no trap entry changes its |
|
| 517 | + | /// entry while it is read. |
|
| 518 | + | export fn running(guard: &sync::Guard, windows: &budgets::Store) -> ?abi::CurrentContextInfo { |
|
| 519 | + | let id = hart(); |
|
| 520 | + | let mut active = State { current: nil, budget: nil }; |
|
| 521 | + | // See `ACTIVE`. |
|
| 522 | + | unsafe { |
|
| 523 | + | set active = ACTIVE[id]; |
|
| 524 | + | } |
|
| 525 | + | let context = active.current else { |
|
| 526 | + | return nil; |
|
| 527 | + | }; |
|
| 528 | + | let budget = active.budget else panic "running: no current budget"; |
|
| 529 | + | let window = try! budgets::get(windows, budget); |
|
| 530 | + | return abi::CurrentContextInfo { |
|
| 531 | + | context: abi::id(context), hart: id as u64, budget: *window.handle |
|
| 532 | + | }; |
|
| 533 | + | } |
|
| 534 | + | ||
| 535 | + | /// Complete the current user's call and select authority at the return boundary. |
|
| 536 | + | /// The caller is the trap entry of the executing hart, with its live frame and |
|
| 537 | + | /// anchor and machine interrupts masked. |
|
| 538 | + | export unsafe fn call(frame: &mut trap::Frame, anchor: &mut trap::Hart) -> ! { |
|
| 539 | + | let id = hart(); |
|
| 540 | + | let device = TIMERS[id] else panic "call: hart not initialized"; |
|
| 541 | + | // Trap context: this read completes before any interrupt is enabled. |
|
| 542 | + | let context = ACTIVE[id].current else trap::halt(); |
|
| 543 | + | let guard = sync::enter(); |
|
| 544 | + | let shared = &mut state::SHARED; |
|
| 545 | + | let owner = contextOwner(&shared.domains, context); |
|
| 546 | + | if frame.registers[*rv64::A7 as u32] == abi::Operation::ContextReturn as u64 { |
|
| 547 | + | assert trap::advanceCall(frame); |
|
| 548 | + | let stopped = try! domains::returned(&mut shared.domains, owner, context); |
|
| 549 | + | reschedule(stopped); |
|
| 550 | + | sync::leave(guard); |
|
| 551 | + | } else { |
|
| 552 | + | sync::leave(guard); |
|
| 553 | + | sync::restoreInterrupts(8); |
|
| 554 | + | calls::handle(owner, frame, id, now(device), calls::respond); |
|
| 555 | + | let interrupts = sync::maskInterrupts(); |
|
| 556 | + | } |
|
| 557 | + | interrupt(frame, &mut *localAnchor()); |
|
| 558 | + | } |
|
| 559 | + | ||
| 560 | + | /// Stop a faulting user domain and resume another authorized context. |
|
| 561 | + | /// The caller is the trap entry of the executing hart, with its live frame and |
|
| 562 | + | /// anchor and machine interrupts masked. |
|
| 563 | + | export unsafe fn fault(frame: &mut trap::Frame, anchor: &mut trap::Hart) -> ! { |
|
| 564 | + | assert trap::fromUser(frame); |
|
| 565 | + | let guard = sync::enter(); |
|
| 566 | + | let shared = &mut state::SHARED; |
|
| 567 | + | // Trap context: this read completes before any interrupt is enabled. |
|
| 568 | + | let context = ACTIVE[hart()].current else panic "fault: no current context"; |
|
| 569 | + | let owner = contextOwner(&shared.domains, context); |
|
| 570 | + | let stopped = try! domains::terminate( |
|
| 571 | + | &mut shared.domains, owner, events::FAULT, frame.cause as u32 |
|
| 572 | + | ); |
|
| 573 | + | reschedule(stopped); |
|
| 574 | + | sync::leave(guard); |
|
| 575 | + | interrupt(frame, &mut *localAnchor()); |
|
| 576 | + | } |
|
| 577 | + | ||
| 578 | + | /// Find the domain that owns a live context. |
|
| 579 | + | fn contextOwner(store: &domains::Store, context: abi::Ref) -> abi::Ref { |
|
| 580 | + | assert slots::matches(&store.contextSlots[..], context, slots::State::Live); |
|
| 581 | + | return store.contexts[context.index].owner; |
|
| 582 | + | } |
|
| 583 | + | ||
| 584 | + | /// Enter the first authorized context or the hart's retained idle frame. |
|
| 585 | + | /// The caller is the executing hart after every online hart has arrived. |
|
| 586 | + | export unsafe fn start(anchor: &mut trap::Hart) -> ! { |
|
| 587 | + | let id = hart(); |
|
| 588 | + | assert TIMERS[id] <> nil; |
|
| 589 | + | let mut frame = CONFIGS[id].idle; |
|
| 590 | + | enable(); |
|
| 591 | + | interrupt(&mut frame, anchor); |
|
| 592 | + | } |
|
| 593 | + | ||
| 594 | + | /// Select one CLINT extent and derive the selected hart's timer registers. |
|
| 595 | + | export fn timer(machine: &platform::Platform, hart: u32) -> Timer throws (abi::Error) { |
|
| 596 | + | if hart >= limits::HARTS or (machine.harts & (1 << hart)) == 0 { |
|
| 597 | + | throw abi::Error::InvalidArg; |
|
| 598 | + | } |
|
| 599 | + | let mut result: ?Timer = nil; |
|
| 600 | + | for i in 0..machine.deviceCount { |
|
| 601 | + | let device = machine.devices[i]; |
|
| 602 | + | if device.kind <> platform::Kind::Clint { |
|
| 603 | + | continue; |
|
| 604 | + | } |
|
| 605 | + | if result <> nil or (device.memory.start & 7) <> 0 { |
|
| 606 | + | throw abi::Error::InvalidArg; |
|
| 607 | + | } |
|
| 608 | + | let window = try registers::open(machine, platform::Kind::Clint, CLINT_SIZE); |
|
| 609 | + | set result = Timer { |
|
| 610 | + | window, |
|
| 611 | + | compare: COMPARE |
|
| 612 | + | + hart as u64 * platform::CLINT_COMPARE_STRIDE, |
|
| 613 | + | software: hart as u64 * platform::CLINT_SOFTWARE_STRIDE, |
|
| 614 | + | }; |
|
| 615 | + | } |
|
| 616 | + | let selected = result else { |
|
| 617 | + | throw abi::Error::InvalidArg; |
|
| 618 | + | }; |
|
| 619 | + | return selected; |
|
| 620 | + | } |
|
| 621 | + | ||
| 622 | + | /// Read the platform's monotonic clock in mtime ticks. |
|
| 623 | + | /// The timer must come from `timer`, which validates the CLINT extent. |
|
| 624 | + | export fn now(timer: Timer) -> u64 { |
|
| 625 | + | return registers::read64(timer.window, CLOCK); |
|
| 626 | + | } |
|
| 627 | + | ||
| 628 | + | /// Program the next boundary while this hart's timer handling is serialized. |
|
| 629 | + | /// The timer must come from `timer`, which validates the CLINT extent. |
|
| 630 | + | export fn arm(timer: Timer, deadline: u64) { |
|
| 631 | + | registers::write64(timer.window, timer.compare, deadline); |
|
| 632 | + | } |
|
| 633 | + | ||
| 634 | + | /// Runnable context and the next boundary that requires a dispatch decision. |
|
| 635 | + | export record Choice: Copy { |
|
| 636 | + | /// Current exclusive interval, or nil while idle. |
|
| 637 | + | budget: ?abi::Ref, |
|
| 638 | + | /// Context authorized to execute now, or nil while idle. |
|
| 639 | + | context: ?abi::Ref, |
|
| 640 | + | /// Platform mtime tick for the next timer interrupt. |
|
| 641 | + | deadline: u64, |
|
| 642 | + | } |
|
| 643 | + | ||
| 644 | + | /// Test whether a live context belongs to the active domain owning its budget. |
|
| 645 | + | fn runnable(store: &domains::Store, owner: abi::Ref, context: abi::Ref) -> bool { |
|
| 646 | + | if not slots::matches(&store.slots[..], owner, slots::State::Live) { |
|
| 647 | + | return false; |
|
| 648 | + | } |
|
| 649 | + | if store.records[owner.index].state <> domains::Lifecycle::Active { |
|
| 650 | + | return false; |
|
| 651 | + | } |
|
| 652 | + | if not slots::matches(&store.contextSlots[..], context, slots::State::Live) { |
|
| 653 | + | return false; |
|
| 654 | + | } |
|
| 655 | + | return store.contexts[context.index].owner == owner |
|
| 656 | + | and store.contexts[context.index].state == domains::ContextState::Ready; |
|
| 657 | + | } |
|
| 658 | + | ||
| 659 | + | /// Select the unique runnable window and the earliest relevant timer boundary. |
|
| 660 | + | /// Every live window on the hart sets a boundary at its end, where `retire` releases it. |
|
| 661 | + | /// The caller serializes budget and domain metadata while taking this snapshot. |
|
| 662 | + | export fn select(windows: &budgets::Store, contexts: &domains::Store, hart: u32, now: u64) -> Choice |
|
| 663 | + | throws (abi::Error) |
|
| 664 | + | { |
|
| 665 | + | if hart >= limits::HARTS { |
|
| 666 | + | throw abi::Error::InvalidArg; |
|
| 667 | + | } |
|
| 668 | + | let mut result = Choice { |
|
| 669 | + | budget: nil, context: nil, deadline: timers::NO_DEADLINE |
|
| 670 | + | }; |
|
| 671 | + | for i in 0..limits::BUDGETS { |
|
| 672 | + | if windows.slots[i].state <> slots::State::Live { |
|
| 673 | + | continue; |
|
| 674 | + | } |
|
| 675 | + | let window = windows.windows[i]; |
|
| 676 | + | if window.hart <> hart or window.end <= now { |
|
| 677 | + | continue; |
|
| 678 | + | } |
|
| 679 | + | if window.end < result.deadline { |
|
| 680 | + | set result.deadline = window.end; |
|
| 681 | + | } |
|
| 682 | + | let context = window.context else { |
|
| 683 | + | continue; |
|
| 684 | + | }; |
|
| 685 | + | if not runnable(contexts, window.owner, context) { |
|
| 686 | + | continue; |
|
| 687 | + | } |
|
| 688 | + | if window.start > now { |
|
| 689 | + | if window.start < result.deadline { |
|
| 690 | + | set result.deadline = window.start; |
|
| 691 | + | } |
|
| 692 | + | continue; |
|
| 693 | + | } |
|
| 694 | + | if let owner = contexts.contexts[context.index].hart { |
|
| 695 | + | if owner <> hart { |
|
| 696 | + | continue; |
|
| 697 | + | } |
|
| 698 | + | } |
|
| 699 | + | assert result.budget == nil; |
|
| 700 | + | set result.budget = abi::Ref { index: i, generation: windows.slots[i].generation }; |
|
| 701 | + | set result.context = context; |
|
| 702 | + | } |
|
| 703 | + | return result; |
|
| 704 | + | } |
|
| 705 | + | ||
| 706 | + | /// Save the interrupted context and install the selected frame and kernel stack. |
|
| 707 | + | /// Interrupts remain disabled and shared metadata stays serialized through exchange. |
|
| 708 | + | export fn exchange( |
|
| 709 | + | state: &mut State, |
|
| 710 | + | config: &Config, |
|
| 711 | + | windows: &budgets::Store, |
|
| 712 | + | contexts: &mut domains::Store, |
|
| 713 | + | frame: &mut trap::Frame, |
|
| 714 | + | anchor: &mut trap::Hart, |
|
| 715 | + | now: u64 |
|
| 716 | + | ) -> Choice throws (abi::Error) { |
|
| 717 | + | let choice = try select(windows, contexts, config.hart, now); |
|
| 718 | + | save(state, contexts, frame); |
|
| 719 | + | if let next = choice.context { |
|
| 720 | + | // The metadata lock makes published code visible before local fetch synchronization. |
|
| 721 | + | sync::syncInstructions(); |
|
| 722 | + | let context = contexts.contexts[next.index]; |
|
| 723 | + | assert context.kernelStack.start < context.kernelStack.end; |
|
| 724 | + | set *frame = context.frame; |
|
| 725 | + | set anchor.stackBottom = context.kernelStack.start; |
|
| 726 | + | set anchor.stackTop = context.kernelStack.end; |
|
| 727 | + | set contexts.contexts[next.index].hart = config.hart; |
|
| 728 | + | } else { |
|
| 729 | + | set *frame = config.idle; |
|
| 730 | + | set anchor.stackBottom = config.stack.start; |
|
| 731 | + | set anchor.stackTop = config.stack.end; |
|
| 732 | + | } |
|
| 733 | + | set state.current = choice.context; |
|
| 734 | + | set state.budget = choice.budget; |
|
| 735 | + | return choice; |
|
| 736 | + | } |
|
| 737 | + | ||
| 738 | + | /// Retain a context frame before releasing this hart's execution ownership. |
|
| 739 | + | fn save(state: &mut State, contexts: &mut domains::Store, frame: &trap::Frame) { |
|
| 740 | + | if let current = state.current { |
|
| 741 | + | assert slots::matches(&contexts.contextSlots[..], current, slots::State::Live); |
|
| 742 | + | set contexts.contexts[current.index].frame = *frame; |
|
| 743 | + | set contexts.contexts[current.index].hart = nil; |
|
| 744 | + | } |
|
| 745 | + | set state.current = nil; |
|
| 746 | + | set state.budget = nil; |
|
| 747 | + | } |
kernel/kernel/dispatch.ras
added
+78 -0
| 1 | + | //! RV64 hart identity, trap entry, and idle-loop primitives. |
|
| 2 | + | // Machine interrupt-enable bit in mstatus. |
|
| 3 | + | .constant DISPATCH_MIE 8; |
|
| 4 | + | // Machine software, timer, and external interrupt enable bits. |
|
| 5 | + | .constant DISPATCH_SOURCES 2184; |
|
| 6 | + | // Bytes in the saved Frame record. |
|
| 7 | + | .constant DISPATCH_FRAME_SIZE 288; |
|
| 8 | + | // Bytes in one saved frame word. |
|
| 9 | + | .constant DISPATCH_WORD_SIZE 8; |
|
| 10 | + | // Words copied into the retained dispatch frame. |
|
| 11 | + | .constant DISPATCH_FRAME_WORDS DISPATCH_FRAME_SIZE / DISPATCH_WORD_SIZE; |
|
| 12 | + | // Hart dispatchFrame field offset. |
|
| 13 | + | .constant DISPATCH_HART_FRAME 64; |
|
| 14 | + | // Hart dispatchTop field offset. |
|
| 15 | + | .constant DISPATCH_HART_STACK 56; |
|
| 16 | + | ||
| 17 | + | .text; |
|
| 18 | + | .export @kernel::dispatch::hart; |
|
| 19 | + | .export @kernel::dispatch::stackPointer; |
|
| 20 | + | .export @kernel::dispatch::localAnchor; |
|
| 21 | + | .export @kernel::dispatch::idleAddress; |
|
| 22 | + | .export @kernel::dispatch::enable; |
|
| 23 | + | .export @kernel::dispatch::interrupt; |
|
| 24 | + | .export @kernel::dispatch::waitInterrupt; |
|
| 25 | + | ||
| 26 | + | // A pending enabled interrupt wakes wfi while global delivery is masked. |
|
| 27 | + | @kernel::dispatch::waitInterrupt |
|
| 28 | + | wfi; |
|
| 29 | + | csrsi mstatus DISPATCH_MIE; |
|
| 30 | + | ret; |
|
| 31 | + | ||
| 32 | + | // Retain the frame outside the context stack before publishing its release. |
|
| 33 | + | @kernel::dispatch::interrupt |
|
| 34 | + | mv %t0 %a0; |
|
| 35 | + | addi %t1 %a1 DISPATCH_HART_FRAME; |
|
| 36 | + | li %t2 DISPATCH_FRAME_WORDS; |
|
| 37 | + | @retainDispatchFrame |
|
| 38 | + | ld %t3 0(%t0); |
|
| 39 | + | sd %t3 0(%t1); |
|
| 40 | + | addi %t0 %t0 DISPATCH_WORD_SIZE; |
|
| 41 | + | addi %t1 %t1 DISPATCH_WORD_SIZE; |
|
| 42 | + | addi %t2 %t2 -1; |
|
| 43 | + | bnez %t2 @retainDispatchFrame; |
|
| 44 | + | ld %sp DISPATCH_HART_STACK(%a1); |
|
| 45 | + | addi %a0 %a1 DISPATCH_HART_FRAME; |
|
| 46 | + | call @kernel::dispatch::choose; |
|
| 47 | + | csrr %a0 mscratch; |
|
| 48 | + | addi %a0 %a0 DISPATCH_HART_FRAME; |
|
| 49 | + | tail @kernel::trap::resume; |
|
| 50 | + | ||
| 51 | + | // The executing hart selects its private dispatch runtime slot. |
|
| 52 | + | @kernel::dispatch::hart |
|
| 53 | + | csrr %a0 mhartid; |
|
| 54 | + | ret; |
|
| 55 | + | ||
| 56 | + | // Dispatch releases context stacks only from the hart's firmware stack. |
|
| 57 | + | @kernel::dispatch::stackPointer |
|
| 58 | + | mv %a0 %sp; |
|
| 59 | + | ret; |
|
| 60 | + | ||
| 61 | + | // A resumed kernel continuation uses its executing hart's trap storage. |
|
| 62 | + | @kernel::dispatch::localAnchor |
|
| 63 | + | csrr %a0 mscratch; |
|
| 64 | + | ret; |
|
| 65 | + | ||
| 66 | + | // Saved idle frames enter this loop with machine interrupts enabled by mret. |
|
| 67 | + | @kernel::dispatch::idleAddress |
|
| 68 | + | la %a0 @dispatchIdle; |
|
| 69 | + | ret; |
|
| 70 | + | @dispatchIdle |
|
| 71 | + | wfi; |
|
| 72 | + | j @dispatchIdle; |
|
| 73 | + | ||
| 74 | + | // Global interrupt enable is restored from the selected frame by mret. |
|
| 75 | + | @kernel::dispatch::enable |
|
| 76 | + | li %t0 DISPATCH_SOURCES; |
|
| 77 | + | csrs mie %t0; |
|
| 78 | + | ret; |
kernel/kernel/domains.rad
added
+1331 -0
| 1 | + | //! Domain creation and ownership of initial contexts and private memory. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | use super::limits; |
|
| 5 | + | use super::slots; |
|
| 6 | + | use super::frames; |
|
| 7 | + | use super::physical; |
|
| 8 | + | use super::backing; |
|
| 9 | + | use super::capability; |
|
| 10 | + | use super::registry; |
|
| 11 | + | use super::instances; |
|
| 12 | + | use super::events; |
|
| 13 | + | use super::trap; |
|
| 14 | + | use super::range; |
|
| 15 | + | use super::pages; |
|
| 16 | + | use std::arch::rv64::shared; |
|
| 17 | + | use std::arch::rv64; |
|
| 18 | + | ||
| 19 | + | /// Retained kernel stack capacity for one suspended execution context. |
|
| 20 | + | export constant KERNEL_STACK_PAGES: u32 = 64; |
|
| 21 | + | ||
| 22 | + | /// Management rights returned to a domain's creator. |
|
| 23 | + | export constant MANAGEMENT_RIGHTS: u16 = |
|
| 24 | + | abi::DESTROY | abi::EXECUTE | abi::GRANT | abi::TRANSFER | abi::WAKE; |
|
| 25 | + | ||
| 26 | + | /// Words needed to retain one ancestry bit per domain slot. |
|
| 27 | + | constant ANCESTRY_WORDS: u32 = (limits::DOMAINS + 31) / 32; |
|
| 28 | + | ||
| 29 | + | /// Protection-domain lifetime independent of execution budgets. |
|
| 30 | + | export union Lifecycle: Copy { |
|
| 31 | + | /// Resources can be installed before one-time activation. |
|
| 32 | + | Pending, |
|
| 33 | + | /// The domain can execute when a context has CPU authority. |
|
| 34 | + | Active, |
|
| 35 | + | /// Contexts cannot be selected; hart release and resource cleanup can remain. |
|
| 36 | + | Dead, |
|
| 37 | + | } |
|
| 38 | + | ||
| 39 | + | /// Resource teardown phase of a domain slot, in transition order. |
|
| 40 | + | export union Teardown: Copy { |
|
| 41 | + | /// Private storage is mapped and teardown has not started. |
|
| 42 | + | Live, |
|
| 43 | + | /// Quiescence admitted the domain to resource teardown; storage is still mapped. |
|
| 44 | + | Reaping, |
|
| 45 | + | /// Private storage is retired; terminal identity can remain until acknowledged. |
|
| 46 | + | Retired, |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | /// Private domain metadata and its shared event ring. |
|
| 50 | + | export record Memory: Copy { |
|
| 51 | + | /// Authoritative capabilities; only the kernel accesses this table. |
|
| 52 | + | table: capability::Table, |
|
| 53 | + | /// Event payload and consumer counters exposed to this domain. |
|
| 54 | + | ring: events::Ring, |
|
| 55 | + | /// Startup values exposed read-only to this domain. |
|
| 56 | + | env: abi::Env, |
|
| 57 | + | } |
|
| 58 | + | ||
| 59 | + | /// Eligibility to resume a live execution context. |
|
| 60 | + | export union ContextState: Copy { |
|
| 61 | + | /// The context can execute with CPU authority in an active domain. |
|
| 62 | + | Ready, |
|
| 63 | + | /// The context has returned and cannot execute again. |
|
| 64 | + | Stopped, |
|
| 65 | + | /// The context has no unread notification and waits for one. |
|
| 66 | + | Waiting, |
|
| 67 | + | } |
|
| 68 | + | ||
| 69 | + | /// Saved integer state owned by one execution context. |
|
| 70 | + | export record Context: Copy { |
|
| 71 | + | /// Domain that owns this context. |
|
| 72 | + | owner: abi::Ref, |
|
| 73 | + | /// Whether this context can resume. |
|
| 74 | + | state: ContextState, |
|
| 75 | + | /// Hart retaining this context as its current execution owner. |
|
| 76 | + | hart: ?u32, |
|
| 77 | + | /// Remote call whose retained continuation requires this context's storage. |
|
| 78 | + | pending: ?abi::Ref, |
|
| 79 | + | /// A kernel call retains unpublished resources and requires this context's stack. |
|
| 80 | + | reservation: bool, |
|
| 81 | + | /// Physical frames retained until this context stops. |
|
| 82 | + | kernelFrames: frames::Run, |
|
| 83 | + | /// Exclusive stack bounds used by trap entry and suspended M-mode calls. |
|
| 84 | + | kernelStack: range::Range, |
|
| 85 | + | /// Exclusive user stack extent; empty until initial activation. |
|
| 86 | + | userStack: range::Range, |
|
| 87 | + | /// Architectural state used when this context resumes. |
|
| 88 | + | frame: trap::Frame, |
|
| 89 | + | } |
|
| 90 | + | ||
| 91 | + | /// Published domain resources and lifecycle metadata. |
|
| 92 | + | export record Domain: Copy { |
|
| 93 | + | /// Activation and teardown state. |
|
| 94 | + | state: Lifecycle, |
|
| 95 | + | /// Historical creator generation. |
|
| 96 | + | creator: abi::Ref, |
|
| 97 | + | /// Current lifecycle-event receiver, if present. |
|
| 98 | + | parent: ?abi::Ref, |
|
| 99 | + | /// Root resident image for this domain's private graph. |
|
| 100 | + | image: abi::Ref, |
|
| 101 | + | /// Initial execution context, before additional contexts are created. |
|
| 102 | + | initial: abi::Ref, |
|
| 103 | + | /// Frames containing the capability table and event ring. |
|
| 104 | + | allocation: frames::Run, |
|
| 105 | + | /// Mapped domain metadata, valid until the domain's storage is reclaimed. |
|
| 106 | + | /// Dereference it only while the metadata lock serializes the store that holds |
|
| 107 | + | /// this record, so the lock holder is the sole accessor. Only trust boundaries |
|
| 108 | + | /// do so, and everything else receives `&mut Memory`, a table, or a ring: |
|
| 109 | + | /// the call entry points and boot (the caller's own metadata), `peer` and its |
|
| 110 | + | /// users in calls (a second domain), `serve` (receivers in service loops), |
|
| 111 | + | /// `retire` in dispatch (owners of expired budgets), and the teardown passes |
|
| 112 | + | /// in lifecycle (dead domains). |
|
| 113 | + | memory: *unsafe mut Memory, |
|
| 114 | + | /// Frames holding the private package state and its base table. The table's |
|
| 115 | + | /// token is `Store.tables[slot]`. |
|
| 116 | + | graph: frames::Run, |
|
| 117 | + | /// Nondelegable Events handle in the domain's own table. |
|
| 118 | + | events: abi::Handle, |
|
| 119 | + | } |
|
| 120 | + | ||
| 121 | + | /// Fixed domain, context, and notification metadata under caller serialization. |
|
| 122 | + | export record Store { |
|
| 123 | + | /// Domain generations, including retained dead identities. |
|
| 124 | + | slots: [slots::Slot; limits::DOMAINS], |
|
| 125 | + | /// Payload for each published domain. |
|
| 126 | + | records: [Domain; limits::DOMAINS], |
|
| 127 | + | /// Base table of each domain's package state. A slot's token is valid from |
|
| 128 | + | /// publication until `retireStorage` releases the instance frames, which |
|
| 129 | + | /// overwrites it with the empty token. Zeroed storage is the empty token. |
|
| 130 | + | tables: [physical::Access; limits::DOMAINS], |
|
| 131 | + | /// Teardown phase of each domain slot. |
|
| 132 | + | teardown: [Teardown; limits::DOMAINS], |
|
| 133 | + | /// Number of dead domain identities retained by the store. |
|
| 134 | + | dead: u32, |
|
| 135 | + | /// First domain slot considered by the next reaper pass. |
|
| 136 | + | reapCursor: u32, |
|
| 137 | + | /// Creation ancestors by domain slot. Clear a column before its ancestor slot is reused. |
|
| 138 | + | ancestors: [[u32; ANCESTRY_WORDS]; limits::DOMAINS], |
|
| 139 | + | /// Context generations shared by initial and additional contexts. |
|
| 140 | + | contextSlots: [slots::Slot; limits::CONTEXTS], |
|
| 141 | + | /// Payload for each published execution context. |
|
| 142 | + | contexts: [Context; limits::CONTEXTS], |
|
| 143 | + | /// Trusted event producers and terminal records. |
|
| 144 | + | events: events::Store, |
|
| 145 | + | } |
|
| 146 | + | ||
| 147 | + | /// View an allocated physical extent that contains a complete Memory record. |
|
| 148 | + | /// The extent must be allocated, aligned, and hold a complete Memory record. |
|
| 149 | + | unsafe fn memory(address: u64) -> *unsafe mut Memory; |
|
| 150 | + | /// Address of the U-mode normal-return call sequence. |
|
| 151 | + | fn returnAddress() -> u64; |
|
| 152 | + | ||
| 153 | + | /// Initialize fresh metadata before boot-domain publication. |
|
| 154 | + | export fn initialize(store: &mut Store) { |
|
| 155 | + | set store.dead = 0; |
|
| 156 | + | set store.reapCursor = 0; |
|
| 157 | + | slots::initialize(&mut store.slots[..]); |
|
| 158 | + | slots::initialize(&mut store.contextSlots[..]); |
|
| 159 | + | for i in 0..limits::CONTEXTS { |
|
| 160 | + | set store.contexts[i].reservation = false; |
|
| 161 | + | } |
|
| 162 | + | for i in 0..limits::DOMAINS { |
|
| 163 | + | set store.tables[i] = physical::empty(); |
|
| 164 | + | } |
|
| 165 | + | for i in 0..limits::DOMAINS { |
|
| 166 | + | set store.ancestors[i] = [0; ANCESTRY_WORDS]; |
|
| 167 | + | set store.teardown[i] = Teardown::Live; |
|
| 168 | + | } |
|
| 169 | + | events::initialize(&mut store.events); |
|
| 170 | + | } |
|
| 171 | + | ||
| 172 | + | /// Read a domain record after validating its retained generation. |
|
| 173 | + | export fn get(store: &Store, object: abi::Ref) -> Domain throws (abi::Error) { |
|
| 174 | + | if not slots::matches(&store.slots[..], object, slots::State::Live) |
|
| 175 | + | or store.teardown[object.index] == Teardown::Retired |
|
| 176 | + | { |
|
| 177 | + | throw abi::Error::BadHandle; |
|
| 178 | + | } |
|
| 179 | + | return store.records[object.index]; |
|
| 180 | + | } |
|
| 181 | + | ||
| 182 | + | /// Test whether all published and unpublished execution ownership has ended. |
|
| 183 | + | export fn quiescent(store: &Store, owner: abi::Ref) -> bool throws (abi::Error) { |
|
| 184 | + | let domain = try get(store, owner); |
|
| 185 | + | if domain.state <> Lifecycle::Dead { |
|
| 186 | + | return false; |
|
| 187 | + | } |
|
| 188 | + | for i in 0..limits::CONTEXTS { |
|
| 189 | + | let state = store.contextSlots[i].state; |
|
| 190 | + | if state <> slots::State::Live and state <> slots::State::Reserved { |
|
| 191 | + | continue; |
|
| 192 | + | } |
|
| 193 | + | if store.contexts[i].owner <> owner { |
|
| 194 | + | continue; |
|
| 195 | + | } |
|
| 196 | + | if state == slots::State::Reserved |
|
| 197 | + | or store.contexts[i].state <> ContextState::Stopped |
|
| 198 | + | or store.contexts[i].hart <> nil |
|
| 199 | + | or store.contexts[i].pending <> nil |
|
| 200 | + | or store.contexts[i].reservation |
|
| 201 | + | { |
|
| 202 | + | return false; |
|
| 203 | + | } |
|
| 204 | + | } |
|
| 205 | + | return true; |
|
| 206 | + | } |
|
| 207 | + | ||
| 208 | + | /// Retire at most one stopped context after all domain execution ownership ends. |
|
| 209 | + | export fn retireContext(store: &mut Store, pool: &mut frames::Pool, owner: abi::Ref) -> bool |
|
| 210 | + | throws (abi::Error) |
|
| 211 | + | { |
|
| 212 | + | if not try quiescent(store, owner) { |
|
| 213 | + | throw abi::Error::Busy; |
|
| 214 | + | } |
|
| 215 | + | for i in 0..limits::CONTEXTS { |
|
| 216 | + | if store.contextSlots[i].state <> slots::State::Live or store.contexts[i].owner <> owner { |
|
| 217 | + | continue; |
|
| 218 | + | } |
|
| 219 | + | let object = abi::Ref { index: i, generation: store.contextSlots[i].generation }; |
|
| 220 | + | try! frames::retire(pool, store.contexts[i].kernelFrames); |
|
| 221 | + | try! slots::release(&mut store.contextSlots[..], object); |
|
| 222 | + | return true; |
|
| 223 | + | } |
|
| 224 | + | return false; |
|
| 225 | + | } |
|
| 226 | + | ||
| 227 | + | /// Retire private domain storage after contexts, handles, and exposures are gone. |
|
| 228 | + | /// `table` is the capability table in the domain's own metadata. |
|
| 229 | + | export fn retireStorage( |
|
| 230 | + | store: &mut Store, backings: &mut backing::Store, owner: abi::Ref, table: &capability::Table |
|
| 231 | + | ) |
|
| 232 | + | throws (abi::Error) |
|
| 233 | + | { |
|
| 234 | + | assert table.owner == owner; |
|
| 235 | + | let domain = try get(store, owner); |
|
| 236 | + | if not try quiescent(store, owner) { |
|
| 237 | + | throw abi::Error::Busy; |
|
| 238 | + | } |
|
| 239 | + | for i in 0..limits::CONTEXTS { |
|
| 240 | + | if store.contextSlots[i].state == slots::State::Live and store.contexts[i].owner == owner { |
|
| 241 | + | throw abi::Error::Busy; |
|
| 242 | + | } |
|
| 243 | + | } |
|
| 244 | + | for i in 0..limits::HANDLES { |
|
| 245 | + | let state = table.slots[i].state; |
|
| 246 | + | if state == slots::State::Live or state == slots::State::Reserved { |
|
| 247 | + | throw abi::Error::Busy; |
|
| 248 | + | } |
|
| 249 | + | } |
|
| 250 | + | if backings.domains[owner.index] <> 0 or store.events.queues[owner.index].generation <> 0 { |
|
| 251 | + | throw abi::Error::Busy; |
|
| 252 | + | } |
|
| 253 | + | try! frames::retire(&mut backings.pool, domain.graph); |
|
| 254 | + | set store.tables[owner.index] = physical::empty(); |
|
| 255 | + | try! frames::retire(&mut backings.pool, domain.allocation); |
|
| 256 | + | set store.teardown[owner.index] = Teardown::Retired; |
|
| 257 | + | } |
|
| 258 | + | ||
| 259 | + | /// Release an acknowledged terminal identity and clear its creation-ancestry column. |
|
| 260 | + | export fn releaseIdentity(store: &mut Store, owner: abi::Ref) throws (abi::Error) { |
|
| 261 | + | if not slots::matches(&store.slots[..], owner, slots::State::Live) { |
|
| 262 | + | throw abi::Error::BadHandle; |
|
| 263 | + | } |
|
| 264 | + | if store.teardown[owner.index] <> Teardown::Retired |
|
| 265 | + | or not try events::acknowledged(&store.events, owner) |
|
| 266 | + | { |
|
| 267 | + | throw abi::Error::Busy; |
|
| 268 | + | } |
|
| 269 | + | let word = owner.index / 32; |
|
| 270 | + | let bit = 1 << (owner.index % 32); |
|
| 271 | + | for i in 0..limits::DOMAINS { |
|
| 272 | + | set store.ancestors[i][word] &= ~bit; |
|
| 273 | + | } |
|
| 274 | + | try! events::release(&mut store.events, owner); |
|
| 275 | + | try! slots::release(&mut store.slots[..], owner); |
|
| 276 | + | assert store.dead > 0; |
|
| 277 | + | set store.dead -= 1; |
|
| 278 | + | } |
|
| 279 | + | ||
| 280 | + | /// Read a live context after checking its generation and owning domain. |
|
| 281 | + | export fn context(store: &Store, owner: abi::Ref, object: abi::Ref) -> Context throws (abi::Error) { |
|
| 282 | + | let domain = try get(store, owner); |
|
| 283 | + | if not slots::matches(&store.contextSlots[..], object, slots::State::Live) |
|
| 284 | + | or store.contexts[object.index].owner <> owner |
|
| 285 | + | { |
|
| 286 | + | throw abi::Error::BadHandle; |
|
| 287 | + | } |
|
| 288 | + | return store.contexts[object.index]; |
|
| 289 | + | } |
|
| 290 | + | ||
| 291 | + | /// Change the live lifecycle receiver under shared metadata serialization. |
|
| 292 | + | export fn reparent( |
|
| 293 | + | store: &mut Store, table: &capability::Table, child: abi::Handle, parent: abi::Handle |
|
| 294 | + | ) |
|
| 295 | + | throws (abi::Error) |
|
| 296 | + | { |
|
| 297 | + | let target = try capability::lookup(table, child, abi::Kind::Domain, abi::Rights(abi::DESTROY)); |
|
| 298 | + | let receiver = try capability::lookup(table, parent, abi::Kind::Domain, abi::Rights(abi::WAKE)); |
|
| 299 | + | let domain = try get(store, target.object); |
|
| 300 | + | let destination = try get(store, receiver.object); |
|
| 301 | + | if domain.state == Lifecycle::Dead or destination.state == Lifecycle::Dead { |
|
| 302 | + | throw abi::Error::BadHandle; |
|
| 303 | + | } |
|
| 304 | + | set store.records[target.object.index].parent = receiver.object; |
|
| 305 | + | } |
|
| 306 | + | ||
| 307 | + | /// Retain a terminal event, stop execution, and report harts that must release contexts. |
|
| 308 | + | export fn terminate(store: &mut Store, owner: abi::Ref, kind: u16, code: u32) -> u32 |
|
| 309 | + | throws (abi::Error) |
|
| 310 | + | { |
|
| 311 | + | return try stop(store, owner, false, kind, code); |
|
| 312 | + | } |
|
| 313 | + | ||
| 314 | + | /// Stop a domain and its creation descendants without changing creation ancestry. |
|
| 315 | + | /// Descendants report destruction by another domain. |
|
| 316 | + | export fn terminateTree(store: &mut Store, owner: abi::Ref, kind: u16, code: u32) -> u32 |
|
| 317 | + | throws (abi::Error) |
|
| 318 | + | { |
|
| 319 | + | return try stop(store, owner, true, kind, code); |
|
| 320 | + | } |
|
| 321 | + | ||
| 322 | + | /// Commit terminal state for a selected set before releasing the metadata lock. |
|
| 323 | + | fn stop(store: &mut Store, owner: abi::Ref, tree: bool, kind: u16, code: u32) -> u32 |
|
| 324 | + | throws (abi::Error) |
|
| 325 | + | { |
|
| 326 | + | if kind <> events::CHILD_EXIT and kind <> events::FAULT { |
|
| 327 | + | throw abi::Error::InvalidArg; |
|
| 328 | + | } |
|
| 329 | + | let domain = try get(store, owner); |
|
| 330 | + | let mut generations: [u32; limits::DOMAINS] = [0; limits::DOMAINS]; |
|
| 331 | + | for i in 0..limits::DOMAINS { |
|
| 332 | + | if store.slots[i].state <> slots::State::Live or store.records[i].state == Lifecycle::Dead { |
|
| 333 | + | continue; |
|
| 334 | + | } |
|
| 335 | + | if i <> owner.index and (not tree |
|
| 336 | + | or (store.ancestors[i][owner.index / 32] & (1 << (owner.index % 32))) == 0) |
|
| 337 | + | { |
|
| 338 | + | continue; |
|
| 339 | + | } |
|
| 340 | + | if store.events.terminals[i].state <> events::State::Free { |
|
| 341 | + | throw abi::Error::Busy; |
|
| 342 | + | } |
|
| 343 | + | if store.events.queues[i].generation <> store.slots[i].generation { |
|
| 344 | + | throw abi::Error::BadHandle; |
|
| 345 | + | } |
|
| 346 | + | set generations[i] = store.slots[i].generation; |
|
| 347 | + | } |
|
| 348 | + | for i in 0..limits::DOMAINS { |
|
| 349 | + | if generations[i] == 0 { |
|
| 350 | + | continue; |
|
| 351 | + | } |
|
| 352 | + | let object = abi::Ref { index: i, generation: generations[i] }; |
|
| 353 | + | let terminal = try! events::reserve(&mut store.events, object); |
|
| 354 | + | let mut receiver = abi::Ref { index: 0, generation: 0 }; |
|
| 355 | + | if let parent = store.records[i].parent { |
|
| 356 | + | set receiver = parent; |
|
| 357 | + | } |
|
| 358 | + | let mut terminalKind = kind; |
|
| 359 | + | let mut terminalCode = code; |
|
| 360 | + | if i <> owner.index { |
|
| 361 | + | set terminalKind = events::FAULT; |
|
| 362 | + | set terminalCode = abi::FAULT_DESTROYED; |
|
| 363 | + | } |
|
| 364 | + | events::finish(&mut store.events, terminal, receiver, terminalKind, terminalCode); |
|
| 365 | + | set store.records[i].state = Lifecycle::Dead; |
|
| 366 | + | set store.dead += 1; |
|
| 367 | + | } |
|
| 368 | + | // Unpublished events for a stopped receiver move to its nearest surviving ancestor. |
|
| 369 | + | // An event published in the receiver's ring counts as delivered, and `closeSet` ends it. |
|
| 370 | + | for i in 0..limits::DOMAINS { |
|
| 371 | + | let terminal = store.events.terminals[i]; |
|
| 372 | + | if terminal.state <> events::State::Pending { |
|
| 373 | + | continue; |
|
| 374 | + | } |
|
| 375 | + | if generations[terminal.receiver.index] == terminal.receiver.generation { |
|
| 376 | + | let receiver = heir(store, &generations[..], terminal.receiver, terminal.subject); |
|
| 377 | + | events::redirect(&mut store.events, terminal.subject, receiver); |
|
| 378 | + | } |
|
| 379 | + | } |
|
| 380 | + | try! events::closeSet(&mut store.events, &generations[..]); |
|
| 381 | + | let mut harts: u32 = 0; |
|
| 382 | + | for i in 0..limits::CONTEXTS { |
|
| 383 | + | if store.contextSlots[i].state <> slots::State::Live { |
|
| 384 | + | continue; |
|
| 385 | + | } |
|
| 386 | + | let target = store.contexts[i].owner; |
|
| 387 | + | if generations[target.index] == target.generation { |
|
| 388 | + | set store.contexts[i].state = ContextState::Stopped; |
|
| 389 | + | if let hart = store.contexts[i].hart { |
|
| 390 | + | set harts |= 1 << hart; |
|
| 391 | + | } |
|
| 392 | + | } |
|
| 393 | + | } |
|
| 394 | + | // Survivors move first, because the walk reads the parents of the stopped domains. |
|
| 395 | + | for i in 0..limits::DOMAINS { |
|
| 396 | + | if store.slots[i].state <> slots::State::Live or store.records[i].state == Lifecycle::Dead { |
|
| 397 | + | continue; |
|
| 398 | + | } |
|
| 399 | + | if let parent = store.records[i].parent { |
|
| 400 | + | if generations[parent.index] == parent.generation { |
|
| 401 | + | let child = abi::Ref { index: i, generation: store.slots[i].generation }; |
|
| 402 | + | set store.records[i].parent = heir(store, &generations[..], parent, child); |
|
| 403 | + | } |
|
| 404 | + | } |
|
| 405 | + | } |
|
| 406 | + | for i in 0..limits::DOMAINS { |
|
| 407 | + | if store.slots[i].state <> slots::State::Live { |
|
| 408 | + | continue; |
|
| 409 | + | } |
|
| 410 | + | if let parent = store.records[i].parent { |
|
| 411 | + | if generations[parent.index] == parent.generation { |
|
| 412 | + | set store.records[i].parent = nil; |
|
| 413 | + | } |
|
| 414 | + | } |
|
| 415 | + | } |
|
| 416 | + | return harts; |
|
| 417 | + | } |
|
| 418 | + | ||
| 419 | + | /// Find the nearest ancestor of `stopped`, through current parents, that survives the |
|
| 420 | + | /// termination of the `generations` set, for `child`, whose parent or event receiver |
|
| 421 | + | /// `stopped` was. The parent of a live domain is never dead, so the walk ends at a |
|
| 422 | + | /// survivor or at a domain with no parent. A walk that reaches `child` follows a cycle |
|
| 423 | + | /// through it, and only a cycle within the set reaches the step limit. Neither leaves an |
|
| 424 | + | /// ancestor. |
|
| 425 | + | fn heir(store: &Store, generations: &[u32], stopped: abi::Ref, child: abi::Ref) -> ?abi::Ref { |
|
| 426 | + | let mut current = store.records[stopped.index].parent; |
|
| 427 | + | for step in 0..limits::DOMAINS { |
|
| 428 | + | let parent = current else { |
|
| 429 | + | return nil; |
|
| 430 | + | }; |
|
| 431 | + | if parent == child { |
|
| 432 | + | return nil; |
|
| 433 | + | } |
|
| 434 | + | if generations[parent.index] <> parent.generation { |
|
| 435 | + | return parent; |
|
| 436 | + | } |
|
| 437 | + | set current = store.records[parent.index].parent; |
|
| 438 | + | } |
|
| 439 | + | return nil; |
|
| 440 | + | } |
|
| 441 | + | ||
| 442 | + | /// Stop a returned context; initial return ends execution of its whole domain. |
|
| 443 | + | export fn returned(store: &mut Store, owner: abi::Ref, object: abi::Ref) -> u32 |
|
| 444 | + | throws (abi::Error) |
|
| 445 | + | { |
|
| 446 | + | let current = try context(store, owner, object); |
|
| 447 | + | if store.records[owner.index].initial == object { |
|
| 448 | + | return try terminate(store, owner, events::CHILD_EXIT, 0); |
|
| 449 | + | } |
|
| 450 | + | set store.contexts[object.index].state = ContextState::Stopped; |
|
| 451 | + | return 0; |
|
| 452 | + | } |
|
| 453 | + | ||
| 454 | + | /// Atomically check notifications and suspend the caller if its queue is empty. |
|
| 455 | + | /// `ring` is the event ring in the owner's metadata. The caller serializes this |
|
| 456 | + | /// check with notification publication. Terminal events published by the check |
|
| 457 | + | /// ready the owner's other waiting contexts; the result tells whether any were |
|
| 458 | + | /// published, so the caller can notify the harts that retain those contexts. |
|
| 459 | + | export fn wait( |
|
| 460 | + | store: &mut Store, owner: abi::Ref, object: abi::Ref, ring: &mut events::Ring |
|
| 461 | + | ) -> bool throws (abi::Error) { |
|
| 462 | + | let current = try context(store, owner, object); |
|
| 463 | + | let domain = try get(store, owner); |
|
| 464 | + | if domain.state <> Lifecycle::Active or current.state <> ContextState::Ready { |
|
| 465 | + | throw abi::Error::Busy; |
|
| 466 | + | } |
|
| 467 | + | let flushed = try events::flush(&mut store.events, owner, ring); |
|
| 468 | + | if flushed > 0 { |
|
| 469 | + | readyWaiters(store, owner); |
|
| 470 | + | } |
|
| 471 | + | if store.events.queues[owner.index].head == store.events.queues[owner.index].tail { |
|
| 472 | + | set store.contexts[object.index].state = ContextState::Waiting; |
|
| 473 | + | } |
|
| 474 | + | return flushed > 0; |
|
| 475 | + | } |
|
| 476 | + | ||
| 477 | + | /// Make every waiting context of a receiver runnable after an event was published. |
|
| 478 | + | fn readyWaiters(store: &mut Store, receiver: abi::Ref) { |
|
| 479 | + | for i in 0..limits::CONTEXTS { |
|
| 480 | + | if store.contextSlots[i].state == slots::State::Live |
|
| 481 | + | and store.contexts[i].owner == receiver |
|
| 482 | + | and store.contexts[i].state == ContextState::Waiting |
|
| 483 | + | { |
|
| 484 | + | set store.contexts[i].state = ContextState::Ready; |
|
| 485 | + | } |
|
| 486 | + | } |
|
| 487 | + | } |
|
| 488 | + | ||
| 489 | + | /// Publish an ordinary event and ready all waiting contexts in the receiver. |
|
| 490 | + | /// `ring` is the event ring in the receiver's metadata. The caller serializes |
|
| 491 | + | /// event publication and context state transitions. |
|
| 492 | + | export fn notify( |
|
| 493 | + | store: &mut Store, receiver: abi::Ref, ring: &mut events::Ring, event: events::Event |
|
| 494 | + | ) -> bool throws (abi::Error) { |
|
| 495 | + | let domain = try get(store, receiver); |
|
| 496 | + | if domain.state == Lifecycle::Dead { |
|
| 497 | + | throw abi::Error::BadHandle; |
|
| 498 | + | } |
|
| 499 | + | let sent = try events::send(&mut store.events, receiver, ring, event); |
|
| 500 | + | if not sent { |
|
| 501 | + | return false; |
|
| 502 | + | } |
|
| 503 | + | readyWaiters(store, receiver); |
|
| 504 | + | return true; |
|
| 505 | + | } |
|
| 506 | + | ||
| 507 | + | /// An event-ring operation that a service path applies to one receiver. |
|
| 508 | + | export union RingRequest: Copy { |
|
| 509 | + | /// Publish an ordinary event; the result tells whether the ring accepted it. |
|
| 510 | + | Send(events::Event), |
|
| 511 | + | /// Refresh the queue's view of the receiver's consumer counters. |
|
| 512 | + | Refresh, |
|
| 513 | + | /// Publish the terminal event retained for a subject; the result tells whether it was |
|
| 514 | + | /// published. |
|
| 515 | + | Deliver(abi::Ref), |
|
| 516 | + | } |
|
| 517 | + | ||
| 518 | + | /// Apply an event-ring operation to a live receiver on behalf of a service path. |
|
| 519 | + | /// The accessor for callers that hold no borrow of the receiver's metadata: the |
|
| 520 | + | /// timer, interrupt, and terminal-event loops, and a wakeup of a domain other |
|
| 521 | + | /// than the caller. A caller that borrows the receiver's metadata passes its |
|
| 522 | + | /// ring to `notify` instead. |
|
| 523 | + | export fn serve(store: &mut Store, receiver: abi::Ref, request: RingRequest) -> bool |
|
| 524 | + | throws (abi::Error) |
|
| 525 | + | { |
|
| 526 | + | let domain = try get(store, receiver); |
|
| 527 | + | if domain.state == Lifecycle::Dead { |
|
| 528 | + | throw abi::Error::BadHandle; |
|
| 529 | + | } |
|
| 530 | + | let mut done = true; |
|
| 531 | + | // The record came from `get`, so its storage is not reclaimed. The caller |
|
| 532 | + | // holds the metadata lock and no other borrow of this domain's metadata. |
|
| 533 | + | unsafe { |
|
| 534 | + | match request { |
|
| 535 | + | case RingRequest::Send(event) => { |
|
| 536 | + | set done = try notify(store, receiver, &mut domain.memory.ring, event); |
|
| 537 | + | } |
|
| 538 | + | case RingRequest::Refresh => { |
|
| 539 | + | try events::refresh(&mut store.events, receiver, &domain.memory.ring); |
|
| 540 | + | } |
|
| 541 | + | case RingRequest::Deliver(subject) => { |
|
| 542 | + | set done = try events::deliver(&mut store.events, subject, &mut domain.memory.ring); |
|
| 543 | + | } |
|
| 544 | + | } |
|
| 545 | + | } |
|
| 546 | + | return done; |
|
| 547 | + | } |
|
| 548 | + | ||
| 549 | + | /// Fault a live receiver whose shared event ring failed validation, and report the harts that |
|
| 550 | + | /// must release its contexts. The receiver controls the ring memory, so the failure is its own. |
|
| 551 | + | export fn ringFault(store: &mut Store, receiver: abi::Ref) -> u32 { |
|
| 552 | + | return try! terminate(store, receiver, events::FAULT, abi::FAULT_RING); |
|
| 553 | + | } |
|
| 554 | + | ||
| 555 | + | /// Locate the metadata of a live domain other than `caller`, for an operation |
|
| 556 | + | /// that borrows two domains' metadata at once. Distinct live domains own |
|
| 557 | + | /// distinct storage, so the result never aliases the caller's metadata. Only the |
|
| 558 | + | /// metadata lock holder dereferences it, and only for the duration of the call. |
|
| 559 | + | export fn peer(store: &Store, caller: abi::Ref, other: abi::Ref) -> *unsafe mut Memory |
|
| 560 | + | throws (abi::Error) |
|
| 561 | + | { |
|
| 562 | + | assert caller <> other; |
|
| 563 | + | let domain = try get(store, other); |
|
| 564 | + | return domain.memory; |
|
| 565 | + | } |
|
| 566 | + | ||
| 567 | + | /// Reclaim an additional context after all harts have released execution ownership. |
|
| 568 | + | export fn contextDestroy( |
|
| 569 | + | store: &mut Store, |
|
| 570 | + | memory: &mut pages::Store, |
|
| 571 | + | table: &capability::Table, |
|
| 572 | + | authority: abi::Handle, |
|
| 573 | + | object: abi::Ref |
|
| 574 | + | ) throws (abi::Error) { |
|
| 575 | + | let permit = try capability::authority(table, authority, abi::Rights(abi::DESTROY)); |
|
| 576 | + | let target = try context(store, permit.object, object); |
|
| 577 | + | if store.records[permit.object.index].state == Lifecycle::Dead { |
|
| 578 | + | throw abi::Error::BadHandle; |
|
| 579 | + | } |
|
| 580 | + | if store.records[permit.object.index].initial == object { |
|
| 581 | + | throw abi::Error::Denied; |
|
| 582 | + | } |
|
| 583 | + | if target.hart <> nil or target.pending <> nil or target.reservation { |
|
| 584 | + | throw abi::Error::Busy; |
|
| 585 | + | } |
|
| 586 | + | try! frames::release(&mut memory.backings.pool, target.kernelFrames); |
|
| 587 | + | try! slots::release(&mut store.contextSlots[..], object); |
|
| 588 | + | } |
|
| 589 | + | ||
| 590 | + | /// Stack of the running context, or an empty range when no context runs. The stack is the |
|
| 591 | + | /// context's own memory: the kernel consumed its only handle when it started. |
|
| 592 | + | export fn stackOf(store: &Store, current: ?abi::CurrentContextInfo) -> range::Range { |
|
| 593 | + | if let running = current { |
|
| 594 | + | return store.contexts[abi::reference(running.context).index].userStack; |
|
| 595 | + | } |
|
| 596 | + | return range::Range { start: 0, end: 0 }; |
|
| 597 | + | } |
|
| 598 | + | ||
| 599 | + | /// Activate a pending domain after checking its stack and argument authority. |
|
| 600 | + | /// |
|
| 601 | + | /// `stack` is a handle in the domain's own table of a Page that no one else holds. The |
|
| 602 | + | /// activation consumes it: the context's stack pointer is the top of the Page, and its |
|
| 603 | + | /// stack limit (`tp`) is the base. `args` is a handle of a readable Page of the domain, or |
|
| 604 | + | /// zero, and `size` bytes of it are the startup arguments. It cannot be the `stack` handle. |
|
| 605 | + | /// `metadata` is the domain's own metadata; the caller resolved `target` from an Execute |
|
| 606 | + | /// capability. The caller serializes metadata until the initial frame and state are published. |
|
| 607 | + | export fn activate( |
|
| 608 | + | store: &mut Store, |
|
| 609 | + | memory: &mut pages::Store, |
|
| 610 | + | target: abi::Ref, |
|
| 611 | + | metadata: &mut Memory, |
|
| 612 | + | stack: abi::Handle, |
|
| 613 | + | args: abi::Handle, |
|
| 614 | + | size: u64 |
|
| 615 | + | ) throws (abi::Error) { |
|
| 616 | + | let domain = try get(store, target); |
|
| 617 | + | if domain.state == Lifecycle::Dead { |
|
| 618 | + | throw abi::Error::BadHandle; |
|
| 619 | + | } |
|
| 620 | + | if domain.state <> Lifecycle::Pending { |
|
| 621 | + | throw abi::Error::NotPending; |
|
| 622 | + | } |
|
| 623 | + | assert metadata.table.owner == target; |
|
| 624 | + | // The stack is consumed, so the environment would name a dead handle. |
|
| 625 | + | if *args <> 0 and args == stack { |
|
| 626 | + | throw abi::Error::InvalidArg; |
|
| 627 | + | } |
|
| 628 | + | if *args == 0 { |
|
| 629 | + | if size <> 0 { |
|
| 630 | + | throw abi::Error::InvalidArg; |
|
| 631 | + | } |
|
| 632 | + | } else { |
|
| 633 | + | let entry = try capability::lookup(&metadata.table, args, abi::Kind::Page, abi::Rights( |
|
| 634 | + | abi::READ |
|
| 635 | + | )); |
|
| 636 | + | let page = try pages::get(memory, entry.object); |
|
| 637 | + | if size > page.count as u64 * limits::FRAME_SIZE { |
|
| 638 | + | throw abi::Error::InvalidArg; |
|
| 639 | + | } |
|
| 640 | + | } |
|
| 641 | + | assert slots::matches(&store.contextSlots[..], domain.initial, slots::State::Live); |
|
| 642 | + | let userStack = try pages::consumeStack(memory, &mut metadata.table, stack); |
|
| 643 | + | set metadata.env = abi::Env { args: *args, argsSize: size, eventsHandle: *domain.events }; |
|
| 644 | + | set store.contexts[domain.initial.index].frame.registers[*rv64::A0 as u32] = |
|
| 645 | + | &metadata.env as u64; |
|
| 646 | + | set store.contexts[domain.initial.index].frame.registers[*rv64::SP as u32] = |
|
| 647 | + | userStack.end; |
|
| 648 | + | set store.contexts[domain.initial.index].frame.registers[*rv64::TP as u32] = |
|
| 649 | + | userStack.start; |
|
| 650 | + | set store.contexts[domain.initial.index].userStack = userStack; |
|
| 651 | + | set store.records[target.index].state = Lifecycle::Active; |
|
| 652 | + | } |
|
| 653 | + | ||
| 654 | + | /// Check that an address is an exported function of the target's resident package graph, |
|
| 655 | + | /// and not the entry of a package. The entry takes a pointer to the startup record, and a |
|
| 656 | + | /// context starts with data arguments only. |
|
| 657 | + | fn exportedFunction(table: &physical::Access, packages: ®istry::Store, address: u64) -> bool { |
|
| 658 | + | let mut found = false; |
|
| 659 | + | for i in 0..limits::PACKAGES { |
|
| 660 | + | if instances::base(table, i) == 0 or packages.slots[i].state <> slots::State::Live { |
|
| 661 | + | continue; |
|
| 662 | + | } |
|
| 663 | + | let package = try! registry::get(packages, abi::Ref { |
|
| 664 | + | index: i, generation: packages.slots[i].generation |
|
| 665 | + | }); |
|
| 666 | + | if package.entry == address { |
|
| 667 | + | return false; |
|
| 668 | + | } |
|
| 669 | + | for symbol in package.exports { |
|
| 670 | + | match symbol.target { |
|
| 671 | + | case shared::Target::Function(entry) => { |
|
| 672 | + | if entry == address { |
|
| 673 | + | set found = true; |
|
| 674 | + | } |
|
| 675 | + | } |
|
| 676 | + | else => {} |
|
| 677 | + | } |
|
| 678 | + | } |
|
| 679 | + | } |
|
| 680 | + | return found; |
|
| 681 | + | } |
|
| 682 | + | ||
| 683 | + | /// Private kernel storage retained by an unpublished context. |
|
| 684 | + | export record ContextAllocation: Copy { |
|
| 685 | + | /// Reserved context slot with an initialized user-stack claim. |
|
| 686 | + | object: abi::Ref, |
|
| 687 | + | /// Domain kept live until publication or cancellation. |
|
| 688 | + | owner: abi::Ref, |
|
| 689 | + | /// Reserved kernel-stack frames. |
|
| 690 | + | frames: frames::Run, |
|
| 691 | + | /// Validated physical base for private stack clearing. |
|
| 692 | + | base: u64, |
|
| 693 | + | } |
|
| 694 | + | ||
| 695 | + | /// Context storage that must be published or cancelled under serialization. |
|
| 696 | + | export union ContextReservation: Once { |
|
| 697 | + | /// One reserved context, its private kernel storage, and the token that proves |
|
| 698 | + | /// the stack frames are private. The token covers exactly `frames` from `base`. |
|
| 699 | + | Held(ContextAllocation, physical::Access), |
|
| 700 | + | } |
|
| 701 | + | ||
| 702 | + | /// Reserve an additional context under metadata serialization. |
|
| 703 | + | /// `domain` is the target domain, which the caller resolved from an Execute capability, and |
|
| 704 | + | /// `targetTable` is its own table, which holds the stack Page named by `start.stack`. The |
|
| 705 | + | /// context's stack is consumed like that of an initial context. Its entry is an exported |
|
| 706 | + | /// function that is not a package entry, called with `start.args` and `start.size` as data. |
|
| 707 | + | export fn contextReserve( |
|
| 708 | + | store: &mut Store, |
|
| 709 | + | memory: &mut pages::Store, |
|
| 710 | + | packages: ®istry::Store, |
|
| 711 | + | domain: abi::Ref, |
|
| 712 | + | start: abi::ContextStart, |
|
| 713 | + | targetTable: &mut capability::Table |
|
| 714 | + | ) -> ContextReservation throws (abi::Error) { |
|
| 715 | + | let target = try get(store, domain); |
|
| 716 | + | if target.state == Lifecycle::Dead { |
|
| 717 | + | throw abi::Error::BadHandle; |
|
| 718 | + | } |
|
| 719 | + | assert targetTable.owner == domain; |
|
| 720 | + | if start.entry == 0 or not exportedFunction( |
|
| 721 | + | &store.tables[domain.index], packages, start.entry |
|
| 722 | + | ) |
|
| 723 | + | { |
|
| 724 | + | throw abi::Error::InvalidArg; |
|
| 725 | + | } |
|
| 726 | + | // Check the stack before reserving anything, and consume it once nothing else can fail. |
|
| 727 | + | try pages::stackExtent(&*memory, &*targetTable, abi::Handle(start.stack)); |
|
| 728 | + | let slot = try slots::reserve(&mut store.contextSlots[..]); |
|
| 729 | + | let pending = try frames::reserve(&mut memory.backings.pool, KERNEL_STACK_PAGES) catch error { |
|
| 730 | + | try! slots::cancel(&mut store.contextSlots[..], slot); |
|
| 731 | + | throw error; |
|
| 732 | + | }; |
|
| 733 | + | let access = physical::reserved(&memory.backings.pool, &pending); |
|
| 734 | + | let kernelFrames = frames::commit(pending); |
|
| 735 | + | let kernelStack = try! frames::extent(&memory.backings.pool, kernelFrames); |
|
| 736 | + | let userStack = try! pages::consumeStack(memory, targetTable, abi::Handle(start.stack)); |
|
| 737 | + | let mut frame = trap::Frame { |
|
| 738 | + | registers: [0; rv64::NUM_REGISTERS], |
|
| 739 | + | pc: start.entry, |
|
| 740 | + | status: rv64::MSTATUS_MPIE, |
|
| 741 | + | cause: 0, |
|
| 742 | + | value: 0, |
|
| 743 | + | }; |
|
| 744 | + | set frame.registers[*rv64::RA as u32] = returnAddress(); |
|
| 745 | + | set frame.registers[*rv64::SP as u32] = userStack.end; |
|
| 746 | + | set frame.registers[*rv64::GP as u32] = |
|
| 747 | + | instances::address(&store.tables[domain.index]); |
|
| 748 | + | set frame.registers[*rv64::TP as u32] = userStack.start; |
|
| 749 | + | set frame.registers[*rv64::A0 as u32] = start.args; |
|
| 750 | + | set frame.registers[*rv64::A1 as u32] = start.size; |
|
| 751 | + | let object = slots::reference(&slot); |
|
| 752 | + | set store.contexts[object.index] = Context { |
|
| 753 | + | owner: domain, |
|
| 754 | + | state: ContextState::Ready, |
|
| 755 | + | hart: nil, |
|
| 756 | + | pending: nil, |
|
| 757 | + | reservation: false, |
|
| 758 | + | kernelFrames, |
|
| 759 | + | kernelStack, |
|
| 760 | + | userStack, |
|
| 761 | + | frame, |
|
| 762 | + | }; |
|
| 763 | + | match slot { |
|
| 764 | + | case slots::Reservation::Held(object) => |
|
| 765 | + | return ContextReservation::Held( |
|
| 766 | + | ContextAllocation { |
|
| 767 | + | object, owner: domain, frames: kernelFrames, base: kernelStack.start |
|
| 768 | + | }, |
|
| 769 | + | access |
|
| 770 | + | ), |
|
| 771 | + | } |
|
| 772 | + | } |
|
| 773 | + | ||
| 774 | + | /// Clear an unpublished context's private kernel stack. |
|
| 775 | + | /// The reservation must hold frames that no other reference can reach. |
|
| 776 | + | export fn contextClear(reservation: &ContextReservation) { |
|
| 777 | + | match reservation { |
|
| 778 | + | case ContextReservation::Held( |
|
| 779 | + | allocation, access |
|
| 780 | + | ) => |
|
| 781 | + | physical::clear(access), |
|
| 782 | + | } |
|
| 783 | + | } |
|
| 784 | + | ||
| 785 | + | /// Check that a token covers exactly the stack frames of an allocation. |
|
| 786 | + | export fn contextCovers(allocation: &ContextAllocation, access: &physical::Access) -> bool { |
|
| 787 | + | return physical::covers( |
|
| 788 | + | access, allocation.base, allocation.frames.count as u64 * limits::FRAME_SIZE |
|
| 789 | + | ); |
|
| 790 | + | } |
|
| 791 | + | ||
| 792 | + | /// Check the reserved context and its retained physical allocation. |
|
| 793 | + | fn contextRequired(store: &Store, allocation: &ContextAllocation) { |
|
| 794 | + | assert slots::matches(&store.contextSlots[..], allocation.object, slots::State::Reserved); |
|
| 795 | + | let context = store.contexts[allocation.object.index]; |
|
| 796 | + | assert context.owner == allocation.owner and context.kernelFrames == allocation.frames; |
|
| 797 | + | assert context.hart == nil; |
|
| 798 | + | } |
|
| 799 | + | ||
| 800 | + | /// Publish an initialized context while its domain stays live under serialization. |
|
| 801 | + | export fn contextPublish(store: &mut Store, reservation: ContextReservation) -> abi::Ref { |
|
| 802 | + | match reservation { |
|
| 803 | + | case ContextReservation::Held(allocation, access) => { |
|
| 804 | + | assert contextCovers(&allocation, &access); |
|
| 805 | + | contextRequired(store, &allocation); |
|
| 806 | + | assert slots::matches(&store.slots[..], allocation.owner, slots::State::Live); |
|
| 807 | + | return try! slots::commit(&mut store.contextSlots[..], slots::Reservation::Held( |
|
| 808 | + | allocation.object |
|
| 809 | + | )); |
|
| 810 | + | } |
|
| 811 | + | } |
|
| 812 | + | } |
|
| 813 | + | ||
| 814 | + | /// Return an unpublished context's stack and slot under metadata serialization. |
|
| 815 | + | export fn contextCancel( |
|
| 816 | + | store: &mut Store, memory: &mut pages::Store, reservation: ContextReservation |
|
| 817 | + | ) { |
|
| 818 | + | match reservation { |
|
| 819 | + | case ContextReservation::Held(allocation, access) => |
|
| 820 | + | contextCancelUsing( |
|
| 821 | + | store, memory, allocation, frames::release |
|
| 822 | + | ), |
|
| 823 | + | } |
|
| 824 | + | } |
|
| 825 | + | ||
| 826 | + | /// Cancel a context slot and transfer its private stack to the release operation. |
|
| 827 | + | /// Releasing frames needs only the retained allocation, not its token. |
|
| 828 | + | export fn contextCancelUsing( |
|
| 829 | + | store: &mut Store, |
|
| 830 | + | memory: &mut pages::Store, |
|
| 831 | + | allocation: ContextAllocation, |
|
| 832 | + | release: fn(&mut frames::Pool, frames::Run) throws (abi::Error) |
|
| 833 | + | ) { |
|
| 834 | + | contextRequired(store, &allocation); |
|
| 835 | + | try! release(&mut memory.backings.pool, allocation.frames); |
|
| 836 | + | try! slots::cancel(&mut store.contextSlots[..], slots::Reservation::Held(allocation.object)); |
|
| 837 | + | } |
|
| 838 | + | ||
| 839 | + | /// Create a context while the caller has exclusive access to shared metadata. |
|
| 840 | + | /// `targetTable` is the target domain's own table, as for `contextReserve`. |
|
| 841 | + | export fn contextCreate( |
|
| 842 | + | store: &mut Store, |
|
| 843 | + | memory: &mut pages::Store, |
|
| 844 | + | packages: ®istry::Store, |
|
| 845 | + | domain: abi::Ref, |
|
| 846 | + | start: abi::ContextStart, |
|
| 847 | + | targetTable: &mut capability::Table |
|
| 848 | + | ) -> abi::Ref throws (abi::Error) { |
|
| 849 | + | let pending = try contextReserve(store, memory, packages, domain, start, targetTable); |
|
| 850 | + | contextClear(&pending); |
|
| 851 | + | return contextPublish(store, pending); |
|
| 852 | + | } |
|
| 853 | + | ||
| 854 | + | /// Memory allocated before the domain and its context become observable. |
|
| 855 | + | export record Prepared: Copy { |
|
| 856 | + | /// Frames for capabilities and the event ring. |
|
| 857 | + | allocation: frames::Run, |
|
| 858 | + | /// Physical start of the metadata frames. |
|
| 859 | + | base: u64, |
|
| 860 | + | /// Mapped metadata at the allocation's start. |
|
| 861 | + | memory: *unsafe mut Memory, |
|
| 862 | + | /// Reserved private package-state graph. |
|
| 863 | + | graph: instances::Allocation, |
|
| 864 | + | /// Retained stack allocation for the initial context. |
|
| 865 | + | kernelFrames: frames::Run, |
|
| 866 | + | /// Initial context's mapped stack bounds. |
|
| 867 | + | kernelStack: range::Range, |
|
| 868 | + | } |
|
| 869 | + | ||
| 870 | + | /// Proof that the frames of one unpublished domain are private to its reservation. |
|
| 871 | + | export union CreationAccess { |
|
| 872 | + | /// Tokens for the capability table and event ring frames, the initial context's |
|
| 873 | + | /// kernel stack frames, and the package-state frames, in that order. Each covers |
|
| 874 | + | /// exactly the frames of the `Prepared` field it is named for. |
|
| 875 | + | Held(physical::Access, physical::Access, physical::Access), |
|
| 876 | + | } |
|
| 877 | + | ||
| 878 | + | /// Private storage and the tokens that prove it is private. |
|
| 879 | + | export union Allocated: Once { |
|
| 880 | + | /// Frames reserved for one unpublished domain. |
|
| 881 | + | Held(Prepared, CreationAccess), |
|
| 882 | + | } |
|
| 883 | + | ||
| 884 | + | /// Check that a creation's tokens cover exactly its prepared frames. |
|
| 885 | + | export fn covers(creation: &Creation, access: &CreationAccess) -> bool { |
|
| 886 | + | let storage = &creation.storage; |
|
| 887 | + | match access { |
|
| 888 | + | case CreationAccess::Held(memory, stack, graph) => |
|
| 889 | + | return physical::covers( |
|
| 890 | + | memory, storage.base, storage.allocation.count as u64 * limits::FRAME_SIZE |
|
| 891 | + | ) |
|
| 892 | + | and physical::covers( |
|
| 893 | + | stack, |
|
| 894 | + | storage.kernelStack.start, |
|
| 895 | + | storage.kernelFrames.count as u64 * limits::FRAME_SIZE |
|
| 896 | + | ) |
|
| 897 | + | and instances::covers(&storage.graph, graph), |
|
| 898 | + | } |
|
| 899 | + | } |
|
| 900 | + | ||
| 901 | + | /// Keep the package-state token for the published instance. Publication makes the |
|
| 902 | + | /// metadata and stack tokens unnecessary, since the frames then belong to the domain. |
|
| 903 | + | fn graphToken(access: CreationAccess) -> physical::Access { |
|
| 904 | + | match access { |
|
| 905 | + | case CreationAccess::Held(memory, stack, graph) => return graph, |
|
| 906 | + | } |
|
| 907 | + | } |
|
| 908 | + | ||
| 909 | + | /// Allocate private storage and return all earlier allocations on failure. |
|
| 910 | + | fn allocate(pool: &mut frames::Pool, packages: ®istry::Store, image: abi::Ref) -> Allocated |
|
| 911 | + | throws (abi::Error) |
|
| 912 | + | { |
|
| 913 | + | let pending = try frames::reserve( |
|
| 914 | + | pool, |
|
| 915 | + | (@sizeOf(Memory) |
|
| 916 | + | + (limits::FRAME_SIZE as u32 - 1)) / limits::FRAME_SIZE as u32 |
|
| 917 | + | ); |
|
| 918 | + | let memoryAccess = physical::reserved(&*pool, &pending); |
|
| 919 | + | let allocation = frames::commit(pending); |
|
| 920 | + | let graph = try instances::reserve(packages, pool, image) catch err { |
|
| 921 | + | try! frames::release(pool, allocation); |
|
| 922 | + | throw err; |
|
| 923 | + | }; |
|
| 924 | + | let pendingStack = try frames::reserve(pool, KERNEL_STACK_PAGES) catch err { |
|
| 925 | + | instances::cancel(pool, graph); |
|
| 926 | + | try! frames::release(pool, allocation); |
|
| 927 | + | throw err; |
|
| 928 | + | }; |
|
| 929 | + | let stackAccess = physical::reserved(&*pool, &pendingStack); |
|
| 930 | + | let kernelFrames = frames::commit(pendingStack); |
|
| 931 | + | let kernelStack = try! frames::extent(pool, kernelFrames); |
|
| 932 | + | let extent = try! frames::extent(pool, allocation); |
|
| 933 | + | // The allocation was just reserved and spans a complete Memory record. |
|
| 934 | + | let mut storage: *unsafe mut Memory = undefined; |
|
| 935 | + | unsafe { |
|
| 936 | + | set storage = memory(extent.start); |
|
| 937 | + | } |
|
| 938 | + | match graph { |
|
| 939 | + | case instances::Reservation::Held(graph, graphAccess) => |
|
| 940 | + | return Allocated::Held( |
|
| 941 | + | Prepared { |
|
| 942 | + | allocation, |
|
| 943 | + | base: extent.start, |
|
| 944 | + | memory: storage, |
|
| 945 | + | graph, |
|
| 946 | + | kernelFrames, |
|
| 947 | + | kernelStack, |
|
| 948 | + | }, |
|
| 949 | + | CreationAccess::Held(memoryAccess, stackAccess, graphAccess) |
|
| 950 | + | ), |
|
| 951 | + | } |
|
| 952 | + | } |
|
| 953 | + | ||
| 954 | + | /// Return storage that has never been exposed to a published domain. |
|
| 955 | + | fn discard( |
|
| 956 | + | pool: &mut frames::Pool, |
|
| 957 | + | prepared: Prepared, |
|
| 958 | + | release: fn(&mut frames::Pool, frames::Run) throws (abi::Error) |
|
| 959 | + | ) { |
|
| 960 | + | try! release(pool, prepared.kernelFrames); |
|
| 961 | + | try! release(pool, prepared.graph.frames); |
|
| 962 | + | try! release(pool, prepared.allocation); |
|
| 963 | + | } |
|
| 964 | + | ||
| 965 | + | /// Unpublished domain metadata retained during private initialization. |
|
| 966 | + | export record Creation: Copy { |
|
| 967 | + | /// Calling domain kept live until publication or cancellation. |
|
| 968 | + | owner: abi::Ref, |
|
| 969 | + | /// Reserved capability slot in the calling domain. |
|
| 970 | + | handle: abi::Ref, |
|
| 971 | + | /// Reserved domain slot. |
|
| 972 | + | domain: abi::Ref, |
|
| 973 | + | /// Reserved initial-context slot. |
|
| 974 | + | context: abi::Ref, |
|
| 975 | + | /// Management and inherited authority selected at admission. |
|
| 976 | + | rights: abi::Rights, |
|
| 977 | + | /// Resident root package. |
|
| 978 | + | image: abi::Ref, |
|
| 979 | + | /// Validated initial entry address. |
|
| 980 | + | entry: u64, |
|
| 981 | + | /// Private allocations retained until the domain becomes observable. |
|
| 982 | + | storage: Prepared, |
|
| 983 | + | } |
|
| 984 | + | ||
| 985 | + | /// Domain creation resources that must be published or cancelled. |
|
| 986 | + | export union Reservation: Once { |
|
| 987 | + | /// One private domain, its reserved metadata capacity, and the tokens that prove |
|
| 988 | + | /// its frames are private. The tokens cover exactly the frames of `Creation.storage`. |
|
| 989 | + | Held(Creation, CreationAccess), |
|
| 990 | + | } |
|
| 991 | + | ||
| 992 | + | /// Reserve domain capacity under metadata serialization while the caller stays live. |
|
| 993 | + | export fn reserve( |
|
| 994 | + | store: &mut Store, |
|
| 995 | + | backings: &mut backing::Store, |
|
| 996 | + | packages: ®istry::Store, |
|
| 997 | + | table: &mut capability::Table, |
|
| 998 | + | authority: abi::Handle, |
|
| 999 | + | image: abi::Handle |
|
| 1000 | + | ) -> Reservation |
|
| 1001 | + | throws (abi::Error) |
|
| 1002 | + | { |
|
| 1003 | + | let permit = try capability::authority(table, authority, abi::Rights(abi::CREATE)); |
|
| 1004 | + | if not backing::domainLive(backings, table.owner) or not backing::domainLive( |
|
| 1005 | + | backings, permit.object |
|
| 1006 | + | ) |
|
| 1007 | + | { |
|
| 1008 | + | throw abi::Error::BadHandle; |
|
| 1009 | + | } |
|
| 1010 | + | let root = try registry::image(packages, table, image, abi::Rights(abi::EXECUTE)); |
|
| 1011 | + | let package = try! registry::get(packages, root); |
|
| 1012 | + | let entry = package.entry else { |
|
| 1013 | + | throw abi::Error::VerifyFailed; |
|
| 1014 | + | }; |
|
| 1015 | + | let handleSlot = try slots::reserve(&mut table.slots[..]); |
|
| 1016 | + | let domainSlot = try slots::reserve(&mut store.slots[..]) catch err { |
|
| 1017 | + | try! slots::cancel(&mut table.slots[..], handleSlot); |
|
| 1018 | + | throw err; |
|
| 1019 | + | }; |
|
| 1020 | + | let contextSlot = try slots::reserve(&mut store.contextSlots[..]) catch err { |
|
| 1021 | + | try! slots::cancel(&mut store.slots[..], domainSlot); |
|
| 1022 | + | try! slots::cancel(&mut table.slots[..], handleSlot); |
|
| 1023 | + | throw err; |
|
| 1024 | + | }; |
|
| 1025 | + | let initialSlot = slots::reference(&contextSlot); |
|
| 1026 | + | set store.contexts[initialSlot.index].owner = slots::reference(&domainSlot); |
|
| 1027 | + | set store.contexts[initialSlot.index].userStack = range::Range { start: 0, end: 0 }; |
|
| 1028 | + | let allocated = try allocate(&mut backings.pool, packages, root) catch err { |
|
| 1029 | + | try! slots::cancel(&mut store.contextSlots[..], contextSlot); |
|
| 1030 | + | try! slots::cancel(&mut store.slots[..], domainSlot); |
|
| 1031 | + | try! slots::cancel(&mut table.slots[..], handleSlot); |
|
| 1032 | + | throw err; |
|
| 1033 | + | }; |
|
| 1034 | + | match allocated { |
|
| 1035 | + | case Allocated::Held(prepared, access) => match handleSlot { |
|
| 1036 | + | case slots::Reservation::Held(handle) => match domainSlot { |
|
| 1037 | + | case slots::Reservation::Held(domain) => match contextSlot { |
|
| 1038 | + | case slots::Reservation::Held(context) => |
|
| 1039 | + | return Reservation::Held( |
|
| 1040 | + | Creation { |
|
| 1041 | + | owner: table.owner, |
|
| 1042 | + | handle, |
|
| 1043 | + | domain, |
|
| 1044 | + | context, |
|
| 1045 | + | rights: abi::Rights( |
|
| 1046 | + | MANAGEMENT_RIGHTS | (*permit.rights & (abi::CREATE |
|
| 1047 | + | | abi::ALLOCATE)) |
|
| 1048 | + | ), |
|
| 1049 | + | image: root, |
|
| 1050 | + | entry, |
|
| 1051 | + | storage: prepared, |
|
| 1052 | + | }, |
|
| 1053 | + | access |
|
| 1054 | + | ), |
|
| 1055 | + | }, |
|
| 1056 | + | }, |
|
| 1057 | + | }, |
|
| 1058 | + | } |
|
| 1059 | + | } |
|
| 1060 | + | ||
| 1061 | + | /// Initialize private storage from resident packages while metadata can change. |
|
| 1062 | + | export fn prepare(packages: ®istry::Published, reservation: &Reservation) throws (abi::Error) { |
|
| 1063 | + | match reservation { |
|
| 1064 | + | case Reservation::Held(creation, access) => |
|
| 1065 | + | try prepareCreation( |
|
| 1066 | + | packages, creation, access |
|
| 1067 | + | ), |
|
| 1068 | + | } |
|
| 1069 | + | } |
|
| 1070 | + | ||
| 1071 | + | /// Initialize retained domain storage outside the metadata lock. |
|
| 1072 | + | /// `packages` must have been captured after the creation reserved its graph. |
|
| 1073 | + | export fn prepareCreation( |
|
| 1074 | + | packages: ®istry::Published, creation: &Creation, access: &CreationAccess |
|
| 1075 | + | ) |
|
| 1076 | + | throws (abi::Error) |
|
| 1077 | + | { |
|
| 1078 | + | try prepareStorage(packages, creation.storage, access, creation.domain); |
|
| 1079 | + | } |
|
| 1080 | + | ||
| 1081 | + | /// Initialize one domain's private allocations before capability publication. |
|
| 1082 | + | fn prepareStorage( |
|
| 1083 | + | packages: ®istry::Published, storage: Prepared, access: &CreationAccess, owner: abi::Ref |
|
| 1084 | + | ) |
|
| 1085 | + | throws (abi::Error) |
|
| 1086 | + | { |
|
| 1087 | + | let mut storage = storage; |
|
| 1088 | + | match access { |
|
| 1089 | + | case CreationAccess::Held(memory, stack, graph) => { |
|
| 1090 | + | physical::clear(memory); |
|
| 1091 | + | physical::clear(stack); |
|
| 1092 | + | try instances::fill(packages, &storage.graph.graph, graph, storage.graph.base); |
|
| 1093 | + | } |
|
| 1094 | + | } |
|
| 1095 | + | // The storage is unpublished and exclusive to this reservation, and was just cleared. |
|
| 1096 | + | unsafe { |
|
| 1097 | + | capability::initialize(&mut storage.memory.table, owner); |
|
| 1098 | + | } |
|
| 1099 | + | } |
|
| 1100 | + | ||
| 1101 | + | /// Check the unpublished capacity retained by one creation transaction. |
|
| 1102 | + | fn require(store: &Store, table: &capability::Table, creation: &Creation) { |
|
| 1103 | + | assert table.owner == creation.owner; |
|
| 1104 | + | assert slots::matches(&table.slots[..], creation.handle, slots::State::Reserved); |
|
| 1105 | + | assert slots::matches(&store.slots[..], creation.domain, slots::State::Reserved); |
|
| 1106 | + | assert slots::matches(&store.contextSlots[..], creation.context, slots::State::Reserved); |
|
| 1107 | + | } |
|
| 1108 | + | ||
| 1109 | + | /// Return private domain storage and reserved slots under metadata serialization. |
|
| 1110 | + | export fn cancel( |
|
| 1111 | + | store: &mut Store, |
|
| 1112 | + | backings: &mut backing::Store, |
|
| 1113 | + | table: &mut capability::Table, |
|
| 1114 | + | reservation: Reservation |
|
| 1115 | + | ) { |
|
| 1116 | + | match reservation { |
|
| 1117 | + | case Reservation::Held(creation, access) => |
|
| 1118 | + | cancelUsing( |
|
| 1119 | + | store, backings, table, creation, frames::release |
|
| 1120 | + | ), |
|
| 1121 | + | } |
|
| 1122 | + | } |
|
| 1123 | + | ||
| 1124 | + | /// Cancel private domain slots and transfer their storage to the release operation. |
|
| 1125 | + | /// Releasing frames needs only the retained creation, not its tokens. |
|
| 1126 | + | export fn cancelUsing( |
|
| 1127 | + | store: &mut Store, |
|
| 1128 | + | backings: &mut backing::Store, |
|
| 1129 | + | table: &mut capability::Table, |
|
| 1130 | + | creation: Creation, |
|
| 1131 | + | release: fn(&mut frames::Pool, frames::Run) throws (abi::Error) |
|
| 1132 | + | ) { |
|
| 1133 | + | require(store, table, &creation); |
|
| 1134 | + | try! slots::cancel(&mut table.slots[..], slots::Reservation::Held(creation.handle)); |
|
| 1135 | + | discard(&mut backings.pool, creation.storage, release); |
|
| 1136 | + | try! slots::cancel(&mut store.contextSlots[..], slots::Reservation::Held(creation.context)); |
|
| 1137 | + | try! slots::cancel(&mut store.slots[..], slots::Reservation::Held(creation.domain)); |
|
| 1138 | + | } |
|
| 1139 | + | ||
| 1140 | + | /// Publish an initialized domain and its Events under metadata serialization. |
|
| 1141 | + | export fn publish( |
|
| 1142 | + | store: &mut Store, |
|
| 1143 | + | backings: &mut backing::Store, |
|
| 1144 | + | table: &mut capability::Table, |
|
| 1145 | + | reservation: Reservation |
|
| 1146 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 1147 | + | match reservation { |
|
| 1148 | + | case Reservation::Held(creation, access) => { |
|
| 1149 | + | assert covers(&creation, &access); |
|
| 1150 | + | require(store, table, &creation); |
|
| 1151 | + | let root = creation.owner == creation.domain; |
|
| 1152 | + | if not root and not backing::domainLive(backings, creation.owner) { |
|
| 1153 | + | cancelUsing(store, backings, table, creation, frames::release); |
|
| 1154 | + | throw abi::Error::BadHandle; |
|
| 1155 | + | } |
|
| 1156 | + | let object = creation.domain; |
|
| 1157 | + | let initial = creation.context; |
|
| 1158 | + | let mut prepared = creation.storage; |
|
| 1159 | + | try backing::registerDomain(backings, object) catch err { |
|
| 1160 | + | cancelUsing(store, backings, table, creation, frames::release); |
|
| 1161 | + | throw err; |
|
| 1162 | + | }; |
|
| 1163 | + | // The storage is unpublished and exclusive to this reservation. |
|
| 1164 | + | let mut queue = abi::Handle(0); |
|
| 1165 | + | unsafe { |
|
| 1166 | + | try events::open(&mut store.events, object, &mut prepared.memory.ring) catch err { |
|
| 1167 | + | try! backing::endDomain(backings, object); |
|
| 1168 | + | cancelUsing(store, backings, table, creation, frames::release); |
|
| 1169 | + | throw err; |
|
| 1170 | + | }; |
|
| 1171 | + | set queue = try! capability::install(&mut prepared.memory.table, capability::Entry { |
|
| 1172 | + | kind: abi::Kind::Events, object, rights: abi::Rights(abi::READ | abi::WRITE) |
|
| 1173 | + | }); |
|
| 1174 | + | } |
|
| 1175 | + | // The graph storage was initialized by `prepare` and is exclusive to this reservation. |
|
| 1176 | + | let graph = instances::commit(instances::Reservation::Held( |
|
| 1177 | + | prepared.graph, graphToken(access) |
|
| 1178 | + | )); |
|
| 1179 | + | // MPIE enables interrupts after the first return into U-mode. |
|
| 1180 | + | let mut frame = trap::Frame { |
|
| 1181 | + | registers: [0; rv64::NUM_REGISTERS], |
|
| 1182 | + | pc: creation.entry, |
|
| 1183 | + | status: rv64::MSTATUS_MPIE, |
|
| 1184 | + | cause: 0, |
|
| 1185 | + | value: 0, |
|
| 1186 | + | }; |
|
| 1187 | + | set frame.registers[*rv64::RA as u32] = returnAddress(); |
|
| 1188 | + | let graphFrames = instances::publish(graph, &mut store.tables[object.index]); |
|
| 1189 | + | set frame.registers[*rv64::GP as u32] = |
|
| 1190 | + | instances::address(&store.tables[object.index]); |
|
| 1191 | + | set store.contexts[initial.index] = Context { |
|
| 1192 | + | owner: object, |
|
| 1193 | + | state: ContextState::Ready, |
|
| 1194 | + | hart: nil, |
|
| 1195 | + | pending: nil, |
|
| 1196 | + | reservation: false, |
|
| 1197 | + | kernelFrames: prepared.kernelFrames, |
|
| 1198 | + | kernelStack: prepared.kernelStack, |
|
| 1199 | + | userStack: range::Range { start: 0, end: 0 }, |
|
| 1200 | + | frame, |
|
| 1201 | + | }; |
|
| 1202 | + | set store.teardown[object.index] = Teardown::Live; |
|
| 1203 | + | let mut parent: ?abi::Ref = nil; |
|
| 1204 | + | if not root { |
|
| 1205 | + | set parent = creation.owner; |
|
| 1206 | + | } |
|
| 1207 | + | set store.records[object.index] = Domain { |
|
| 1208 | + | state: Lifecycle::Pending, |
|
| 1209 | + | creator: creation.owner, |
|
| 1210 | + | parent, |
|
| 1211 | + | image: creation.image, |
|
| 1212 | + | initial, |
|
| 1213 | + | allocation: prepared.allocation, |
|
| 1214 | + | memory: prepared.memory, |
|
| 1215 | + | graph: graphFrames, |
|
| 1216 | + | events: queue, |
|
| 1217 | + | }; |
|
| 1218 | + | if not root { |
|
| 1219 | + | set store.ancestors[object.index] = store.ancestors[creation.owner.index]; |
|
| 1220 | + | set store.ancestors[object.index][creation.owner.index / 32] |= |
|
| 1221 | + | 1 << (creation.owner.index % 32); |
|
| 1222 | + | } |
|
| 1223 | + | let context = try! slots::commit(&mut store.contextSlots[..], slots::Reservation::Held( |
|
| 1224 | + | initial |
|
| 1225 | + | )); |
|
| 1226 | + | let domain = try! slots::commit(&mut store.slots[..], slots::Reservation::Held(object)); |
|
| 1227 | + | return capability::publish( |
|
| 1228 | + | table, |
|
| 1229 | + | slots::Reservation::Held(creation.handle), |
|
| 1230 | + | capability::Entry { |
|
| 1231 | + | kind: abi::Kind::Domain, object: domain, rights: creation.rights |
|
| 1232 | + | } |
|
| 1233 | + | ); |
|
| 1234 | + | } |
|
| 1235 | + | } |
|
| 1236 | + | } |
|
| 1237 | + | ||
| 1238 | + | /// Create the first domain with its self capability in slot zero and Events in slot one. |
|
| 1239 | + | /// Boot calls this before any domain, context, or exposure membership is published. |
|
| 1240 | + | export fn bootstrap( |
|
| 1241 | + | store: &mut Store, backings: &mut backing::Store, packages: ®istry::Store, image: abi::Ref |
|
| 1242 | + | ) -> abi::Ref throws (abi::Error) { |
|
| 1243 | + | for slot in store.slots { |
|
| 1244 | + | if slot.state <> slots::State::Free { |
|
| 1245 | + | throw abi::Error::Busy; |
|
| 1246 | + | } |
|
| 1247 | + | } |
|
| 1248 | + | let package = try registry::get(packages, image); |
|
| 1249 | + | let entry = package.entry else { |
|
| 1250 | + | throw abi::Error::VerifyFailed; |
|
| 1251 | + | }; |
|
| 1252 | + | let domainSlot = try slots::reserve(&mut store.slots[..]); |
|
| 1253 | + | let contextSlot = try slots::reserve(&mut store.contextSlots[..]) catch error { |
|
| 1254 | + | try! slots::cancel(&mut store.slots[..], domainSlot); |
|
| 1255 | + | throw error; |
|
| 1256 | + | }; |
|
| 1257 | + | let object = slots::reference(&domainSlot); |
|
| 1258 | + | let allocated = try allocate(&mut backings.pool, packages, image) catch error { |
|
| 1259 | + | try! slots::cancel(&mut store.contextSlots[..], contextSlot); |
|
| 1260 | + | try! slots::cancel(&mut store.slots[..], domainSlot); |
|
| 1261 | + | throw error; |
|
| 1262 | + | }; |
|
| 1263 | + | match allocated { |
|
| 1264 | + | case Allocated::Held(storage, access) => { |
|
| 1265 | + | try prepareStorage(®istry::capture(packages), storage, &access, object) catch error { |
|
| 1266 | + | discard(&mut backings.pool, storage, frames::release); |
|
| 1267 | + | try! slots::cancel(&mut store.contextSlots[..], contextSlot); |
|
| 1268 | + | try! slots::cancel(&mut store.slots[..], domainSlot); |
|
| 1269 | + | throw error; |
|
| 1270 | + | }; |
|
| 1271 | + | let memory = storage.memory; |
|
| 1272 | + | // The root creates itself, so its table is the unpublished table in `storage`, |
|
| 1273 | + | // which is exclusive to this call. |
|
| 1274 | + | unsafe { |
|
| 1275 | + | let handleSlot = try! slots::reserve(&mut memory.table.slots[..]); |
|
| 1276 | + | assert slots::reference(&handleSlot).index == 0; |
|
| 1277 | + | match domainSlot { |
|
| 1278 | + | case slots::Reservation::Held(domain) => match contextSlot { |
|
| 1279 | + | case slots::Reservation::Held(context) => match handleSlot { |
|
| 1280 | + | case slots::Reservation::Held(handle) => { |
|
| 1281 | + | let result = try publish( |
|
| 1282 | + | store, |
|
| 1283 | + | backings, |
|
| 1284 | + | &mut memory.table, |
|
| 1285 | + | Reservation::Held( |
|
| 1286 | + | Creation { |
|
| 1287 | + | owner: object, |
|
| 1288 | + | handle, |
|
| 1289 | + | domain, |
|
| 1290 | + | context, |
|
| 1291 | + | rights: abi::Rights( |
|
| 1292 | + | MANAGEMENT_RIGHTS |
|
| 1293 | + | | abi::CREATE |
|
| 1294 | + | | abi::ALLOCATE |
|
| 1295 | + | ), |
|
| 1296 | + | image, |
|
| 1297 | + | entry, |
|
| 1298 | + | storage, |
|
| 1299 | + | }, |
|
| 1300 | + | access |
|
| 1301 | + | ) |
|
| 1302 | + | ); |
|
| 1303 | + | assert (try! abi::decode(result)).object.index == 0; |
|
| 1304 | + | } |
|
| 1305 | + | }, |
|
| 1306 | + | }, |
|
| 1307 | + | } |
|
| 1308 | + | } |
|
| 1309 | + | } |
|
| 1310 | + | } |
|
| 1311 | + | return object; |
|
| 1312 | + | } |
|
| 1313 | + | ||
| 1314 | + | /// Create a private domain while the caller has exclusive access to metadata. |
|
| 1315 | + | export fn create( |
|
| 1316 | + | store: &mut Store, |
|
| 1317 | + | backings: &mut backing::Store, |
|
| 1318 | + | packages: ®istry::Store, |
|
| 1319 | + | table: &mut capability::Table, |
|
| 1320 | + | authority: abi::Handle, |
|
| 1321 | + | image: abi::Handle |
|
| 1322 | + | ) -> abi::Handle |
|
| 1323 | + | throws (abi::Error) |
|
| 1324 | + | { |
|
| 1325 | + | let pending = try reserve(store, backings, packages, table, authority, image); |
|
| 1326 | + | try prepare(®istry::capture(packages), &pending) catch err { |
|
| 1327 | + | cancel(store, backings, table, pending); |
|
| 1328 | + | throw err; |
|
| 1329 | + | }; |
|
| 1330 | + | return try publish(store, backings, table, pending); |
|
| 1331 | + | } |
kernel/kernel/domains.ras
added
+20 -0
| 1 | + | //! Physical domain metadata views and the context return sequence. |
|
| 2 | + | // ContextReturn kernel operation number. |
|
| 3 | + | .constant DOMAIN_CONTEXT_RETURN 62; |
|
| 4 | + | ||
| 5 | + | .text; |
|
| 6 | + | .export @kernel::domains::memory; |
|
| 7 | + | .export @kernel::domains::returnAddress; |
|
| 8 | + | ||
| 9 | + | // Native entries return to a gp-independent user call sequence. |
|
| 10 | + | @kernel::domains::returnAddress |
|
| 11 | + | la %a0 @contextReturn; |
|
| 12 | + | ret; |
|
| 13 | + | @contextReturn |
|
| 14 | + | li %a7 DOMAIN_CONTEXT_RETURN; |
|
| 15 | + | ecall; |
|
| 16 | + | ebreak; |
|
| 17 | + | ||
| 18 | + | // The allocated frame extent contains one complete aligned Memory record. |
|
| 19 | + | @kernel::domains::memory |
|
| 20 | + | ret; |
kernel/kernel/events.rad
added
+427 -0
| 1 | + | //! Shared event rings and terminal records retained until acknowledgement. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | use super::limits; |
|
| 5 | + | use super::sync; |
|
| 6 | + | use std::mem; |
|
| 7 | + | ||
| 8 | + | /// Number of positions in the shared 32-bit counter space. |
|
| 9 | + | constant COUNTER_MODULUS: u64 = mem::U32_MAX as u64 + 1; |
|
| 10 | + | /// Bits retained when shared event counters wrap. |
|
| 11 | + | constant COUNTER_MASK: u64 = mem::U32_MAX as u64; |
|
| 12 | + | ||
| 13 | + | /// Shared ring entry count, a power of two. |
|
| 14 | + | export constant CAPACITY: u32 = 256; |
|
| 15 | + | /// Occupancy limit for ordinary events, leaving 64 entries for lifecycle events. |
|
| 16 | + | export constant ORDINARY: u32 = 192; |
|
| 17 | + | /// Device interrupt notification. |
|
| 18 | + | export constant INTERRUPT: u16 = 1; |
|
| 19 | + | /// Timer notification. |
|
| 20 | + | export constant TIMEOUT: u16 = 2; |
|
| 21 | + | /// Terminal exception report. |
|
| 22 | + | export constant FAULT: u16 = 3; |
|
| 23 | + | /// Normal domain termination report. |
|
| 24 | + | export constant CHILD_EXIT: u16 = 4; |
|
| 25 | + | /// Peer wake notification. |
|
| 26 | + | export constant WAKEUP: u16 = 5; |
|
| 27 | + | ||
| 28 | + | /// One asynchronous notification in the shared ABI. |
|
| 29 | + | export record Event: Copy { |
|
| 30 | + | /// Notification kind. |
|
| 31 | + | kind: u16, |
|
| 32 | + | /// Zero for every published event. |
|
| 33 | + | reserved: u16, |
|
| 34 | + | /// Interrupt number, status, fault code, or notification token. |
|
| 35 | + | code: u32, |
|
| 36 | + | /// Kind-specific payload, including generation-bearing terminal domain IDs. |
|
| 37 | + | value: u64, |
|
| 38 | + | } |
|
| 39 | + | ||
| 40 | + | /// Shared queue memory. Consumer operations require one serialized reader. |
|
| 41 | + | export record Ring: Copy { |
|
| 42 | + | /// Entries published before the producer advances tail. |
|
| 43 | + | data: [Event; CAPACITY], |
|
| 44 | + | /// Consumer progress, accessed through ordered 32-bit operations. |
|
| 45 | + | head: u32, |
|
| 46 | + | /// Producer progress, accessed through ordered 32-bit operations. |
|
| 47 | + | tail: u32, |
|
| 48 | + | /// CAPACITY minus one. |
|
| 49 | + | mask: u32, |
|
| 50 | + | } |
|
| 51 | + | ||
| 52 | + | /// Trusted producer metadata, separate from consumer-writable ring memory. |
|
| 53 | + | export record Queue: Copy { |
|
| 54 | + | /// Live receiver generation, or zero for a closed queue. |
|
| 55 | + | generation: u32, |
|
| 56 | + | /// Last validated consumer counter. |
|
| 57 | + | head: u32, |
|
| 58 | + | /// Authoritative producer counter. |
|
| 59 | + | tail: u32, |
|
| 60 | + | /// Terminal domain per occupied slot; generation zero denotes an ordinary event. |
|
| 61 | + | subjects: [abi::Ref; CAPACITY], |
|
| 62 | + | /// Interrupt source per occupied slot, or zero for another event kind. |
|
| 63 | + | sources: [u32; CAPACITY], |
|
| 64 | + | } |
|
| 65 | + | ||
| 66 | + | /// Lifetime state of a terminal-event record. |
|
| 67 | + | export union State: Copy { |
|
| 68 | + | /// Available for a new domain's terminal reservation. |
|
| 69 | + | Free, |
|
| 70 | + | /// Capacity owned by an unfinished lifecycle transaction. |
|
| 71 | + | Reserved, |
|
| 72 | + | /// Event retained until its receiver has ring capacity. |
|
| 73 | + | Pending, |
|
| 74 | + | /// Event published and awaiting consumer acknowledgement. |
|
| 75 | + | Queued, |
|
| 76 | + | /// Consumer acknowledged the event, or the receiver exited. |
|
| 77 | + | Acknowledged, |
|
| 78 | + | } |
|
| 79 | + | ||
| 80 | + | /// A domain's terminal notification and its retained identity. |
|
| 81 | + | export record Terminal: Copy { |
|
| 82 | + | /// Domain whose identifier must remain reserved. |
|
| 83 | + | subject: abi::Ref, |
|
| 84 | + | /// Receiver selected when termination commits. |
|
| 85 | + | receiver: abi::Ref, |
|
| 86 | + | /// Complete retained event payload. |
|
| 87 | + | event: Event, |
|
| 88 | + | /// Terminal publication and acknowledgement state. |
|
| 89 | + | state: State, |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// Fixed event metadata. Callers serialize all kernel-side mutations. |
|
| 93 | + | export record Store: Copy { |
|
| 94 | + | /// Terminal events retained for publication into receiver rings. |
|
| 95 | + | pending: u32, |
|
| 96 | + | /// Published terminal events that await consumer acknowledgement. |
|
| 97 | + | queued: u32, |
|
| 98 | + | /// Producer metadata indexed by receiver domain. |
|
| 99 | + | queues: [Queue; limits::DOMAINS], |
|
| 100 | + | /// Terminal storage indexed by the terminating domain. |
|
| 101 | + | terminals: [Terminal; limits::DOMAINS], |
|
| 102 | + | } |
|
| 103 | + | ||
| 104 | + | /// Terminal capacity that must be committed or cancelled for the same domain. |
|
| 105 | + | export union Reservation: Once { |
|
| 106 | + | /// Domain generation owning one terminal record. |
|
| 107 | + | Held(abi::Ref), |
|
| 108 | + | } |
|
| 109 | + | ||
| 110 | + | /// Compute forward distance in the shared 32-bit counter space. |
|
| 111 | + | fn distance(end: u32, start: u32) -> u32 { |
|
| 112 | + | return ((end as u64 |
|
| 113 | + | + COUNTER_MODULUS |
|
| 114 | + | - start as u64) & COUNTER_MASK) as u32; |
|
| 115 | + | } |
|
| 116 | + | ||
| 117 | + | /// Advance a shared counter with explicit modulo arithmetic. |
|
| 118 | + | fn next(value: u32) -> u32 { |
|
| 119 | + | return ((value as u64 + 1) & COUNTER_MASK) as u32; |
|
| 120 | + | } |
|
| 121 | + | ||
| 122 | + | /// Initialize metadata before any queue or terminal record exists. |
|
| 123 | + | export fn initialize(store: &mut Store) { |
|
| 124 | + | set store.pending = 0; |
|
| 125 | + | set store.queued = 0; |
|
| 126 | + | for i in 0..limits::DOMAINS { |
|
| 127 | + | set store.queues[i].generation = 0; |
|
| 128 | + | set store.terminals[i].state = State::Free; |
|
| 129 | + | } |
|
| 130 | + | } |
|
| 131 | + | ||
| 132 | + | /// Test the receiver generation without reading beyond the fixed table. |
|
| 133 | + | fn live(store: &Store, receiver: abi::Ref) -> bool { |
|
| 134 | + | return receiver.index < limits::DOMAINS |
|
| 135 | + | and receiver.generation <> 0 |
|
| 136 | + | and store.queues[receiver.index].generation == receiver.generation; |
|
| 137 | + | } |
|
| 138 | + | ||
| 139 | + | /// Open a queue over mapped shared memory before publishing its Events handle. |
|
| 140 | + | export fn open(store: &mut Store, receiver: abi::Ref, ring: &mut Ring) throws (abi::Error) { |
|
| 141 | + | if receiver.index >= limits::DOMAINS or receiver.generation == 0 { |
|
| 142 | + | throw abi::Error::InvalidArg; |
|
| 143 | + | } |
|
| 144 | + | let queue = &mut store.queues[receiver.index]; |
|
| 145 | + | if queue.generation <> 0 { |
|
| 146 | + | throw abi::Error::Busy; |
|
| 147 | + | } |
|
| 148 | + | set queue.generation = receiver.generation; |
|
| 149 | + | set queue.head = 0; |
|
| 150 | + | set queue.tail = 0; |
|
| 151 | + | set ring.mask = CAPACITY - 1; |
|
| 152 | + | sync::storeRelease32(&mut ring.head, 0); |
|
| 153 | + | sync::storeRelease32(&mut ring.tail, 0); |
|
| 154 | + | } |
|
| 155 | + | ||
| 156 | + | /// Validate consumer progress and acknowledge terminal records before slot reuse. |
|
| 157 | + | export fn refresh(store: &mut Store, receiver: abi::Ref, ring: &Ring) throws (abi::Error) { |
|
| 158 | + | if not live(store, receiver) { |
|
| 159 | + | throw abi::Error::BadHandle; |
|
| 160 | + | } |
|
| 161 | + | let queue = &mut store.queues[receiver.index]; |
|
| 162 | + | let head = sync::loadAcquire32(&ring.head); |
|
| 163 | + | let advance = distance(head, queue.head); |
|
| 164 | + | if advance > distance(queue.tail, queue.head) { |
|
| 165 | + | throw abi::Error::InvalidArg; |
|
| 166 | + | } |
|
| 167 | + | for i in 0..advance { |
|
| 168 | + | let subject = queue.subjects[queue.head & (CAPACITY - 1)]; |
|
| 169 | + | if subject.generation <> 0 { |
|
| 170 | + | let terminal = &mut store.terminals[subject.index]; |
|
| 171 | + | assert terminal.subject == subject |
|
| 172 | + | and terminal.receiver == receiver |
|
| 173 | + | and terminal.state == State::Queued; |
|
| 174 | + | set terminal.state = State::Acknowledged; |
|
| 175 | + | assert store.queued > 0; |
|
| 176 | + | set store.queued -= 1; |
|
| 177 | + | } |
|
| 178 | + | set queue.head = next(queue.head); |
|
| 179 | + | } |
|
| 180 | + | } |
|
| 181 | + | ||
| 182 | + | /// Publish one event after capacity and receiver validation. |
|
| 183 | + | fn publish(queue: &mut Queue, ring: &mut Ring, event: Event, subject: abi::Ref) { |
|
| 184 | + | let index = queue.tail & (CAPACITY - 1); |
|
| 185 | + | set ring.data[index] = event; |
|
| 186 | + | set queue.subjects[index] = subject; |
|
| 187 | + | let mut source: u32 = 0; |
|
| 188 | + | if event.kind == INTERRUPT { |
|
| 189 | + | set source = event.code; |
|
| 190 | + | } |
|
| 191 | + | set queue.sources[index] = source; |
|
| 192 | + | set queue.tail = next(queue.tail); |
|
| 193 | + | sync::storeRelease32(&mut ring.tail, queue.tail); |
|
| 194 | + | } |
|
| 195 | + | ||
| 196 | + | /// Send an ordinary event, returning false when its reserved occupancy limit is reached. |
|
| 197 | + | export fn send(store: &mut Store, receiver: abi::Ref, ring: &mut Ring, event: Event) -> bool |
|
| 198 | + | throws (abi::Error) |
|
| 199 | + | { |
|
| 200 | + | if event.reserved <> 0 or (event.kind <> INTERRUPT |
|
| 201 | + | and event.kind <> TIMEOUT |
|
| 202 | + | and event.kind <> WAKEUP) |
|
| 203 | + | { |
|
| 204 | + | throw abi::Error::InvalidArg; |
|
| 205 | + | } |
|
| 206 | + | if event.kind == INTERRUPT and event.code == 0 { |
|
| 207 | + | throw abi::Error::InvalidArg; |
|
| 208 | + | } |
|
| 209 | + | try refresh(store, receiver, ring); |
|
| 210 | + | if event.kind == INTERRUPT and interruptPending(store, receiver, event.code) { |
|
| 211 | + | return true; |
|
| 212 | + | } |
|
| 213 | + | let queue = &mut store.queues[receiver.index]; |
|
| 214 | + | if distance(queue.tail, queue.head) >= ORDINARY { |
|
| 215 | + | return false; |
|
| 216 | + | } |
|
| 217 | + | publish(queue, ring, event, abi::Ref { index: 0, generation: 0 }); |
|
| 218 | + | return true; |
|
| 219 | + | } |
|
| 220 | + | ||
| 221 | + | /// Test trusted IRQ metadata after the receiver's consumer progress is refreshed. |
|
| 222 | + | export fn interruptPending(store: &Store, receiver: abi::Ref, source: u32) -> bool { |
|
| 223 | + | if source == 0 or not live(store, receiver) { |
|
| 224 | + | return false; |
|
| 225 | + | } |
|
| 226 | + | let queue = &store.queues[receiver.index]; |
|
| 227 | + | for i in 0..distance(queue.tail, queue.head) { |
|
| 228 | + | let index = ((queue.head & (CAPACITY - 1)) + i) & (CAPACITY - 1); |
|
| 229 | + | if queue.sources[index] == source { |
|
| 230 | + | return true; |
|
| 231 | + | } |
|
| 232 | + | } |
|
| 233 | + | return false; |
|
| 234 | + | } |
|
| 235 | + | ||
| 236 | + | /// Read one published event and release its slot. Callers serialize consumers. |
|
| 237 | + | export fn pop(ring: &mut Ring) -> ?Event throws (abi::Error) { |
|
| 238 | + | let head = sync::loadAcquire32(&ring.head); |
|
| 239 | + | let tail = sync::loadAcquire32(&ring.tail); |
|
| 240 | + | let count = distance(tail, head); |
|
| 241 | + | if count > CAPACITY or ring.mask <> CAPACITY - 1 { |
|
| 242 | + | throw abi::Error::InvalidArg; |
|
| 243 | + | } |
|
| 244 | + | if count == 0 { |
|
| 245 | + | return nil; |
|
| 246 | + | } |
|
| 247 | + | let event = ring.data[head & (CAPACITY - 1)]; |
|
| 248 | + | sync::storeRelease32(&mut ring.head, next(head)); |
|
| 249 | + | return event; |
|
| 250 | + | } |
|
| 251 | + | ||
| 252 | + | /// Reserve terminal capacity before committing domain termination. |
|
| 253 | + | export fn reserve(store: &mut Store, subject: abi::Ref) -> Reservation throws (abi::Error) { |
|
| 254 | + | if subject.index >= limits::DOMAINS or subject.generation == 0 { |
|
| 255 | + | throw abi::Error::InvalidArg; |
|
| 256 | + | } |
|
| 257 | + | if store.terminals[subject.index].state <> State::Free { |
|
| 258 | + | throw abi::Error::Busy; |
|
| 259 | + | } |
|
| 260 | + | set store.terminals[subject.index].subject = subject; |
|
| 261 | + | set store.terminals[subject.index].state = State::Reserved; |
|
| 262 | + | return Reservation::Held(subject); |
|
| 263 | + | } |
|
| 264 | + | ||
| 265 | + | /// Cancel terminal capacity before domain termination commits. |
|
| 266 | + | export fn cancel(store: &mut Store, reservation: Reservation) { |
|
| 267 | + | match reservation { |
|
| 268 | + | case Reservation::Held(subject) => { |
|
| 269 | + | assert store.terminals[subject.index].subject == subject |
|
| 270 | + | and store.terminals[subject.index].state == State::Reserved; |
|
| 271 | + | set store.terminals[subject.index].state = State::Free; |
|
| 272 | + | } |
|
| 273 | + | } |
|
| 274 | + | } |
|
| 275 | + | ||
| 276 | + | /// Commit one terminal event to its selected receiver. A missing receiver needs no delivery. |
|
| 277 | + | export fn finish( |
|
| 278 | + | store: &mut Store, reservation: Reservation, receiver: abi::Ref, kind: u16, code: u32 |
|
| 279 | + | ) { |
|
| 280 | + | assert kind == FAULT or kind == CHILD_EXIT; |
|
| 281 | + | let active = live(store, receiver); |
|
| 282 | + | match reservation { |
|
| 283 | + | case Reservation::Held(subject) => { |
|
| 284 | + | let terminal = &mut store.terminals[subject.index]; |
|
| 285 | + | assert terminal.subject == subject and terminal.state == State::Reserved; |
|
| 286 | + | set terminal.receiver = receiver; |
|
| 287 | + | set terminal.event = Event { kind, reserved: 0, code, value: abi::id(subject) }; |
|
| 288 | + | if active { |
|
| 289 | + | set terminal.state = State::Pending; |
|
| 290 | + | set store.pending += 1; |
|
| 291 | + | } else { |
|
| 292 | + | set terminal.state = State::Acknowledged; |
|
| 293 | + | } |
|
| 294 | + | } |
|
| 295 | + | } |
|
| 296 | + | } |
|
| 297 | + | ||
| 298 | + | /// Move an unpublished terminal event from a receiver that is terminating to `receiver`, |
|
| 299 | + | /// where it waits for ring capacity. Without a live receiver the event ends. |
|
| 300 | + | export fn redirect(store: &mut Store, subject: abi::Ref, receiver: ?abi::Ref) { |
|
| 301 | + | assert store.terminals[subject.index].subject == subject |
|
| 302 | + | and store.terminals[subject.index].state == State::Pending; |
|
| 303 | + | if let heir = receiver { |
|
| 304 | + | if live(store, heir) { |
|
| 305 | + | set store.terminals[subject.index].receiver = heir; |
|
| 306 | + | return; |
|
| 307 | + | } |
|
| 308 | + | } |
|
| 309 | + | assert store.pending > 0; |
|
| 310 | + | set store.pending -= 1; |
|
| 311 | + | set store.terminals[subject.index].state = State::Acknowledged; |
|
| 312 | + | } |
|
| 313 | + | ||
| 314 | + | /// Publish one retained terminal event when its receiver has ring capacity. |
|
| 315 | + | export fn deliver(store: &mut Store, subject: abi::Ref, ring: &mut Ring) -> bool |
|
| 316 | + | throws (abi::Error) |
|
| 317 | + | { |
|
| 318 | + | if subject.index >= limits::DOMAINS |
|
| 319 | + | or store.terminals[subject.index].subject <> subject |
|
| 320 | + | or store.terminals[subject.index].state == State::Free |
|
| 321 | + | { |
|
| 322 | + | throw abi::Error::BadHandle; |
|
| 323 | + | } |
|
| 324 | + | let terminal = store.terminals[subject.index]; |
|
| 325 | + | if terminal.state <> State::Pending { |
|
| 326 | + | return false; |
|
| 327 | + | } |
|
| 328 | + | try refresh(store, terminal.receiver, ring); |
|
| 329 | + | let queue = &mut store.queues[terminal.receiver.index]; |
|
| 330 | + | if distance(queue.tail, queue.head) == CAPACITY { |
|
| 331 | + | return false; |
|
| 332 | + | } |
|
| 333 | + | set store.terminals[subject.index].state = State::Queued; |
|
| 334 | + | set store.queued += 1; |
|
| 335 | + | assert store.pending > 0; |
|
| 336 | + | set store.pending -= 1; |
|
| 337 | + | publish(queue, ring, terminal.event, subject); |
|
| 338 | + | return true; |
|
| 339 | + | } |
|
| 340 | + | ||
| 341 | + | /// Publish retained terminal events in domain-index order as ring capacity permits. |
|
| 342 | + | export fn flush(store: &mut Store, receiver: abi::Ref, ring: &mut Ring) -> u32 throws (abi::Error) { |
|
| 343 | + | try refresh(store, receiver, ring); |
|
| 344 | + | let mut count: u32 = 0; |
|
| 345 | + | for i in 0..limits::DOMAINS { |
|
| 346 | + | if distance( |
|
| 347 | + | store.queues[receiver.index].tail, store.queues[receiver.index].head |
|
| 348 | + | ) == CAPACITY |
|
| 349 | + | { |
|
| 350 | + | break; |
|
| 351 | + | } |
|
| 352 | + | if store.terminals[i].state <> State::Pending or store.terminals[i].receiver <> receiver { |
|
| 353 | + | continue; |
|
| 354 | + | } |
|
| 355 | + | let subject = store.terminals[i].subject; |
|
| 356 | + | if try deliver(store, subject, ring) { |
|
| 357 | + | set count += 1; |
|
| 358 | + | } |
|
| 359 | + | } |
|
| 360 | + | return count; |
|
| 361 | + | } |
|
| 362 | + | ||
| 363 | + | /// Report whether a retained terminal identity can be released. |
|
| 364 | + | export fn acknowledged(store: &Store, subject: abi::Ref) -> bool throws (abi::Error) { |
|
| 365 | + | if subject.index >= limits::DOMAINS { |
|
| 366 | + | throw abi::Error::BadHandle; |
|
| 367 | + | } |
|
| 368 | + | if store.terminals[subject.index].state == State::Free |
|
| 369 | + | or store.terminals[subject.index].subject <> subject |
|
| 370 | + | { |
|
| 371 | + | throw abi::Error::BadHandle; |
|
| 372 | + | } |
|
| 373 | + | return store.terminals[subject.index].state == State::Acknowledged; |
|
| 374 | + | } |
|
| 375 | + | ||
| 376 | + | /// Release terminal metadata only after its queued identifier is no longer observable. |
|
| 377 | + | export fn release(store: &mut Store, subject: abi::Ref) throws (abi::Error) { |
|
| 378 | + | if not try acknowledged(store, subject) { |
|
| 379 | + | throw abi::Error::Busy; |
|
| 380 | + | } |
|
| 381 | + | set store.terminals[subject.index].state = State::Free; |
|
| 382 | + | } |
|
| 383 | + | ||
| 384 | + | /// Close a stopped domain's queue and end all terminal references held by that receiver. |
|
| 385 | + | export fn close(store: &mut Store, receiver: abi::Ref) throws (abi::Error) { |
|
| 386 | + | if not live(store, receiver) { |
|
| 387 | + | throw abi::Error::BadHandle; |
|
| 388 | + | } |
|
| 389 | + | let mut generations: [u32; limits::DOMAINS] = [0; limits::DOMAINS]; |
|
| 390 | + | set generations[receiver.index] = receiver.generation; |
|
| 391 | + | try! closeSet(store, &generations[..]); |
|
| 392 | + | } |
|
| 393 | + | ||
| 394 | + | /// Close selected receiver generations and acknowledge their retained terminal events. |
|
| 395 | + | /// Zero entries leave a receiver open. Validate all selected generations before mutation. |
|
| 396 | + | export fn closeSet(store: &mut Store, generations: &[u32]) throws (abi::Error) { |
|
| 397 | + | if generations.len <> limits::DOMAINS { |
|
| 398 | + | throw abi::Error::InvalidArg; |
|
| 399 | + | } |
|
| 400 | + | for i in 0..limits::DOMAINS { |
|
| 401 | + | if generations[i] <> 0 and store.queues[i].generation <> generations[i] { |
|
| 402 | + | throw abi::Error::BadHandle; |
|
| 403 | + | } |
|
| 404 | + | } |
|
| 405 | + | for i in 0..limits::DOMAINS { |
|
| 406 | + | if generations[i] <> 0 { |
|
| 407 | + | set store.queues[i].generation = 0; |
|
| 408 | + | } |
|
| 409 | + | } |
|
| 410 | + | for i in 0..limits::DOMAINS { |
|
| 411 | + | let terminal = store.terminals[i]; |
|
| 412 | + | if terminal.state <> State::Pending and terminal.state <> State::Queued { |
|
| 413 | + | continue; |
|
| 414 | + | } |
|
| 415 | + | let receiver = terminal.receiver; |
|
| 416 | + | if generations[receiver.index] == receiver.generation { |
|
| 417 | + | if store.terminals[i].state == State::Pending { |
|
| 418 | + | assert store.pending > 0; |
|
| 419 | + | set store.pending -= 1; |
|
| 420 | + | } else { |
|
| 421 | + | assert store.queued > 0; |
|
| 422 | + | set store.queued -= 1; |
|
| 423 | + | } |
|
| 424 | + | set store.terminals[i].state = State::Acknowledged; |
|
| 425 | + | } |
|
| 426 | + | } |
|
| 427 | + | } |
kernel/kernel/fdt.rad
added
+316 -0
| 1 | + | //! Bounded flattened device-tree decoding. All offsets refer to the input blob. |
|
| 2 | + | ||
| 3 | + | use super::range; |
|
| 4 | + | ||
| 5 | + | /// Flattened device-tree signature. |
|
| 6 | + | constant MAGIC: u64 = 0xd00dfeed; |
|
| 7 | + | /// Supported flattened device-tree version. |
|
| 8 | + | constant VERSION: u64 = 17; |
|
| 9 | + | /// Bytes in a version-17 device-tree header. |
|
| 10 | + | export constant HEADER_SIZE: u32 = 40; |
|
| 11 | + | /// Offset of the total blob size in the header. |
|
| 12 | + | export constant SIZE_OFFSET: u32 = 4; |
|
| 13 | + | /// Header offset of the structure block location. |
|
| 14 | + | constant STRUCTURE_OFFSET: u32 = 8; |
|
| 15 | + | /// Header offset of the strings block location. |
|
| 16 | + | constant STRINGS_OFFSET: u32 = 12; |
|
| 17 | + | /// Header offset of the reservation block location. |
|
| 18 | + | constant RESERVATIONS_OFFSET: u32 = 16; |
|
| 19 | + | /// Header offset of the format version. |
|
| 20 | + | constant VERSION_OFFSET: u32 = 20; |
|
| 21 | + | /// Header offset of the last compatible version. |
|
| 22 | + | constant COMPATIBLE_VERSION_OFFSET: u32 = 24; |
|
| 23 | + | /// Header offset of the strings block size. |
|
| 24 | + | constant STRINGS_SIZE_OFFSET: u32 = 32; |
|
| 25 | + | /// Header offset of the structure block size. |
|
| 26 | + | constant STRUCTURE_SIZE_OFFSET: u32 = 36; |
|
| 27 | + | /// Bytes in a reservation address and size pair. |
|
| 28 | + | export constant RESERVATION_SIZE: u32 = 16; |
|
| 29 | + | /// Byte alignment of the reservation block. |
|
| 30 | + | constant RESERVATION_ALIGNMENT: u32 = 8; |
|
| 31 | + | /// Byte size and alignment of structure tokens. |
|
| 32 | + | constant TOKEN_SIZE: u32 = 4; |
|
| 33 | + | /// Bytes in a property length and name offset. |
|
| 34 | + | constant PROPERTY_HEADER_SIZE: u32 = 8; |
|
| 35 | + | /// Maximum nesting depth tracked by the child bitmap. |
|
| 36 | + | export constant MAX_DEPTH: u32 = 32; |
|
| 37 | + | /// Structure token that starts a node. |
|
| 38 | + | constant BEGIN_NODE: u64 = 1; |
|
| 39 | + | /// Structure token that closes a node. |
|
| 40 | + | constant END_NODE: u64 = 2; |
|
| 41 | + | /// Structure token that starts a property. |
|
| 42 | + | constant PROPERTY: u64 = 3; |
|
| 43 | + | /// Structure token with no effect. |
|
| 44 | + | constant NOP: u64 = 4; |
|
| 45 | + | /// Structure token that ends the tree. |
|
| 46 | + | constant END: u64 = 9; |
|
| 47 | + | ||
| 48 | + | /// Invalid or unsupported platform description. |
|
| 49 | + | export union Error: Copy { |
|
| 50 | + | /// A field or byte sequence is outside the blob. |
|
| 51 | + | Truncated, |
|
| 52 | + | /// Header, structure, or reservation data is invalid. |
|
| 53 | + | Invalid, |
|
| 54 | + | /// The tree exceeds the supported nesting depth. |
|
| 55 | + | Depth, |
|
| 56 | + | } |
|
| 57 | + | ||
| 58 | + | /// Byte interval within the validated blob. |
|
| 59 | + | export record Span: Copy { |
|
| 60 | + | /// First byte offset. |
|
| 61 | + | start: u32, |
|
| 62 | + | /// Exclusive end offset. |
|
| 63 | + | end: u32, |
|
| 64 | + | } |
|
| 65 | + | ||
| 66 | + | /// Validated header and block extents. |
|
| 67 | + | export record Header: Copy { |
|
| 68 | + | /// Declared blob length. |
|
| 69 | + | size: u32, |
|
| 70 | + | /// Structure block. |
|
| 71 | + | structure: Span, |
|
| 72 | + | /// Property-name strings. |
|
| 73 | + | strings: Span, |
|
| 74 | + | /// Reservation entries, excluding the zero terminator. |
|
| 75 | + | reservations: Span, |
|
| 76 | + | } |
|
| 77 | + | ||
| 78 | + | /// Structure traversal state. Use only with the blob passed to `header`. |
|
| 79 | + | export record Cursor: Copy { |
|
| 80 | + | /// Next structure byte. |
|
| 81 | + | offset: u32, |
|
| 82 | + | /// Number of open nodes. |
|
| 83 | + | depth: u32, |
|
| 84 | + | /// A root node has started. |
|
| 85 | + | started: bool, |
|
| 86 | + | /// The end token has been consumed. |
|
| 87 | + | finished: bool, |
|
| 88 | + | /// Depth bits for nodes whose children have started. |
|
| 89 | + | children: u32, |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// One tree structure item. Names exclude their zero terminator. |
|
| 93 | + | export union Event: Copy { |
|
| 94 | + | /// Node name. |
|
| 95 | + | Begin(Span), |
|
| 96 | + | /// Property name and value. |
|
| 97 | + | Property { name: Span, value: Span }, |
|
| 98 | + | /// End of the current node. |
|
| 99 | + | EndNode, |
|
| 100 | + | /// End of the complete tree. |
|
| 101 | + | End, |
|
| 102 | + | } |
|
| 103 | + | ||
| 104 | + | /// Read a bounded big-endian integer of four or eight bytes. |
|
| 105 | + | export fn integer(bytes: &[u8], offset: u32, width: u32) -> u64 throws (Error) { |
|
| 106 | + | if width <> 4 and width <> 8 { |
|
| 107 | + | throw Error::Invalid; |
|
| 108 | + | } |
|
| 109 | + | if offset > bytes.len or width > bytes.len - offset { |
|
| 110 | + | throw Error::Truncated; |
|
| 111 | + | } |
|
| 112 | + | let mut value: u64 = 0; |
|
| 113 | + | for i in 0..width { |
|
| 114 | + | set value = (value << 8) | bytes[offset + i] as u64; |
|
| 115 | + | } |
|
| 116 | + | return value; |
|
| 117 | + | } |
|
| 118 | + | ||
| 119 | + | /// Check a block's extent without addition overflow. |
|
| 120 | + | fn block(total: u32, start: u32, size: u32) -> Span throws (Error) { |
|
| 121 | + | if start < HEADER_SIZE or start > total or size > total - start { |
|
| 122 | + | throw Error::Truncated; |
|
| 123 | + | } |
|
| 124 | + | return Span { start, end: start + size }; |
|
| 125 | + | } |
|
| 126 | + | ||
| 127 | + | /// Check whether two nonempty spans overlap. |
|
| 128 | + | fn overlaps(a: Span, b: Span) -> bool { |
|
| 129 | + | return a.start < a.end and b.start < b.end and a.start < b.end and b.start < a.end; |
|
| 130 | + | } |
|
| 131 | + | ||
| 132 | + | /// Validate a version-17 header, disjoint blocks, and all memory reservations. |
|
| 133 | + | export fn header(bytes: &[u8]) -> Header throws (Error) { |
|
| 134 | + | if bytes.len < HEADER_SIZE { |
|
| 135 | + | throw Error::Truncated; |
|
| 136 | + | } |
|
| 137 | + | if try integer(bytes, 0, 4) <> MAGIC { |
|
| 138 | + | throw Error::Invalid; |
|
| 139 | + | } |
|
| 140 | + | let size = try integer(bytes, SIZE_OFFSET, 4) as u32; |
|
| 141 | + | if size > bytes.len or size < HEADER_SIZE { |
|
| 142 | + | throw Error::Truncated; |
|
| 143 | + | } |
|
| 144 | + | if try integer(bytes, VERSION_OFFSET, 4) <> VERSION or try integer( |
|
| 145 | + | bytes, COMPATIBLE_VERSION_OFFSET, 4 |
|
| 146 | + | ) > VERSION |
|
| 147 | + | { |
|
| 148 | + | throw Error::Invalid; |
|
| 149 | + | } |
|
| 150 | + | let structure = try block( |
|
| 151 | + | size, |
|
| 152 | + | try integer( |
|
| 153 | + | bytes, STRUCTURE_OFFSET, 4 |
|
| 154 | + | ) as u32, |
|
| 155 | + | try integer( |
|
| 156 | + | bytes, STRUCTURE_SIZE_OFFSET, 4 |
|
| 157 | + | ) as u32 |
|
| 158 | + | ); |
|
| 159 | + | let strings = try block( |
|
| 160 | + | size, |
|
| 161 | + | try integer(bytes, STRINGS_OFFSET, 4) as u32, |
|
| 162 | + | try integer( |
|
| 163 | + | bytes, STRINGS_SIZE_OFFSET, 4 |
|
| 164 | + | ) as u32 |
|
| 165 | + | ); |
|
| 166 | + | let reserved = try integer(bytes, RESERVATIONS_OFFSET, 4) as u32; |
|
| 167 | + | if (structure.start & (TOKEN_SIZE - 1)) <> 0 |
|
| 168 | + | or (structure.end & (TOKEN_SIZE - 1)) <> 0 |
|
| 169 | + | or (reserved & (RESERVATION_ALIGNMENT - 1)) <> 0 |
|
| 170 | + | or reserved < HEADER_SIZE |
|
| 171 | + | { |
|
| 172 | + | throw Error::Invalid; |
|
| 173 | + | } |
|
| 174 | + | let mut end = reserved; |
|
| 175 | + | loop { |
|
| 176 | + | if end > size or size - end < RESERVATION_SIZE { |
|
| 177 | + | throw Error::Truncated; |
|
| 178 | + | } |
|
| 179 | + | let address = try integer(bytes, end, 8); |
|
| 180 | + | let length = try integer(bytes, end + 8, 8); |
|
| 181 | + | if address == 0 and length == 0 { |
|
| 182 | + | break; |
|
| 183 | + | } |
|
| 184 | + | if range::new(address, length) == nil { |
|
| 185 | + | throw Error::Invalid; |
|
| 186 | + | } |
|
| 187 | + | set end += RESERVATION_SIZE; |
|
| 188 | + | } |
|
| 189 | + | let reservations = Span { start: reserved, end: end + RESERVATION_SIZE }; |
|
| 190 | + | if overlaps(structure, strings) |
|
| 191 | + | or overlaps(structure, reservations) |
|
| 192 | + | or overlaps( |
|
| 193 | + | strings, reservations |
|
| 194 | + | ) |
|
| 195 | + | { |
|
| 196 | + | throw Error::Invalid; |
|
| 197 | + | } |
|
| 198 | + | return Header { size, structure, strings, reservations: Span { start: reserved, end } }; |
|
| 199 | + | } |
|
| 200 | + | ||
| 201 | + | /// Start traversal at the first structure token. |
|
| 202 | + | export fn cursor(tree: Header) -> Cursor { |
|
| 203 | + | return Cursor { |
|
| 204 | + | offset: tree.structure.start, depth: 0, started: false, finished: false, children: 0 |
|
| 205 | + | }; |
|
| 206 | + | } |
|
| 207 | + | ||
| 208 | + | /// Find a terminated string inside one validated block. |
|
| 209 | + | fn name(bytes: &[u8], start: u32, end: u32) -> Span throws (Error) { |
|
| 210 | + | if end > bytes.len or start >= end { |
|
| 211 | + | throw Error::Truncated; |
|
| 212 | + | } |
|
| 213 | + | let mut at = start; |
|
| 214 | + | while at < end { |
|
| 215 | + | if bytes[at] == 0 { |
|
| 216 | + | return Span { start, end: at }; |
|
| 217 | + | } |
|
| 218 | + | set at += 1; |
|
| 219 | + | } |
|
| 220 | + | throw Error::Truncated; |
|
| 221 | + | } |
|
| 222 | + | ||
| 223 | + | /// Round an offset to the next token boundary within the structure block. |
|
| 224 | + | fn padded(bytes: &[u8], end: u32, limit: u32) -> u32 throws (Error) { |
|
| 225 | + | let padding = (TOKEN_SIZE - (end & (TOKEN_SIZE - 1))) & (TOKEN_SIZE - 1); |
|
| 226 | + | if end > limit or padding > limit - end { |
|
| 227 | + | throw Error::Truncated; |
|
| 228 | + | } |
|
| 229 | + | for i in end..end + padding { |
|
| 230 | + | if bytes[i] <> 0 { |
|
| 231 | + | throw Error::Invalid; |
|
| 232 | + | } |
|
| 233 | + | } |
|
| 234 | + | return end + padding; |
|
| 235 | + | } |
|
| 236 | + | ||
| 237 | + | /// Read one structure event, skipping NOP tokens. Nesting is bounded to 32 nodes. |
|
| 238 | + | export fn next(bytes: &[u8], tree: Header, state: &mut Cursor) -> Event throws (Error) { |
|
| 239 | + | if state.finished { |
|
| 240 | + | return Event::End; |
|
| 241 | + | } |
|
| 242 | + | loop { |
|
| 243 | + | if state.offset > tree.structure.end |
|
| 244 | + | or tree.structure.end - state.offset < TOKEN_SIZE |
|
| 245 | + | { |
|
| 246 | + | throw Error::Truncated; |
|
| 247 | + | } |
|
| 248 | + | let token = try integer(bytes, state.offset, 4); |
|
| 249 | + | set state.offset += TOKEN_SIZE; |
|
| 250 | + | match token { |
|
| 251 | + | case BEGIN_NODE => { |
|
| 252 | + | if state.depth == MAX_DEPTH { |
|
| 253 | + | throw Error::Depth; |
|
| 254 | + | } |
|
| 255 | + | if state.depth == 0 and state.started { |
|
| 256 | + | throw Error::Invalid; |
|
| 257 | + | } |
|
| 258 | + | let value = try name(bytes, state.offset, tree.structure.end); |
|
| 259 | + | if state.depth == 0 and value.start <> value.end { |
|
| 260 | + | throw Error::Invalid; |
|
| 261 | + | } |
|
| 262 | + | if state.depth > 0 and value.start == value.end { |
|
| 263 | + | throw Error::Invalid; |
|
| 264 | + | } |
|
| 265 | + | if state.depth > 0 { |
|
| 266 | + | set state.children |= 1 << (state.depth - 1); |
|
| 267 | + | } |
|
| 268 | + | set state.children &= ~(1 << state.depth); |
|
| 269 | + | set state.depth += 1; |
|
| 270 | + | set state.started = true; |
|
| 271 | + | set state.offset = try padded(bytes, value.end + 1, tree.structure.end); |
|
| 272 | + | return Event::Begin(value); |
|
| 273 | + | } |
|
| 274 | + | case END_NODE => { |
|
| 275 | + | if state.depth == 0 { |
|
| 276 | + | throw Error::Invalid; |
|
| 277 | + | } |
|
| 278 | + | set state.depth -= 1; |
|
| 279 | + | return Event::EndNode; |
|
| 280 | + | } |
|
| 281 | + | case PROPERTY => { |
|
| 282 | + | if state.depth == 0 or (state.children & (1 << (state.depth - 1))) <> 0 { |
|
| 283 | + | throw Error::Invalid; |
|
| 284 | + | } |
|
| 285 | + | if tree.structure.end - state.offset < PROPERTY_HEADER_SIZE { |
|
| 286 | + | throw Error::Truncated; |
|
| 287 | + | } |
|
| 288 | + | let size = try integer(bytes, state.offset, 4) as u32; |
|
| 289 | + | let offset = try integer(bytes, state.offset + 4, 4) as u32; |
|
| 290 | + | set state.offset += PROPERTY_HEADER_SIZE; |
|
| 291 | + | if size > tree.structure.end - state.offset { |
|
| 292 | + | throw Error::Truncated; |
|
| 293 | + | } |
|
| 294 | + | if offset >= tree.strings.end - tree.strings.start { |
|
| 295 | + | throw Error::Invalid; |
|
| 296 | + | } |
|
| 297 | + | let key = try name(bytes, tree.strings.start + offset, tree.strings.end); |
|
| 298 | + | if key.start == key.end { |
|
| 299 | + | throw Error::Invalid; |
|
| 300 | + | } |
|
| 301 | + | let value = Span { start: state.offset, end: state.offset + size }; |
|
| 302 | + | set state.offset = try padded(bytes, value.end, tree.structure.end); |
|
| 303 | + | return Event::Property { name: key, value }; |
|
| 304 | + | } |
|
| 305 | + | case NOP => {} |
|
| 306 | + | case END => { |
|
| 307 | + | if state.depth <> 0 or not state.started or state.offset <> tree.structure.end { |
|
| 308 | + | throw Error::Invalid; |
|
| 309 | + | } |
|
| 310 | + | set state.finished = true; |
|
| 311 | + | return Event::End; |
|
| 312 | + | } |
|
| 313 | + | else => throw Error::Invalid, |
|
| 314 | + | } |
|
| 315 | + | } |
|
| 316 | + | } |
kernel/kernel/frames.rad
added
+348 -0
| 1 | + | //! Bounded physical-frame reservations. Callers serialize pool mutations. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | use super::limits; |
|
| 5 | + | use super::platform; |
|
| 6 | + | use super::range; |
|
| 7 | + | ||
| 8 | + | /// A contiguous allocation represented by indices in the physical-frame map. |
|
| 9 | + | export record Run: Copy { |
|
| 10 | + | /// First frame-map index. |
|
| 11 | + | first: u32, |
|
| 12 | + | /// Number of physically adjacent frames. |
|
| 13 | + | count: u32, |
|
| 14 | + | } |
|
| 15 | + | ||
| 16 | + | /// Proof of reserved frames. Only this module can build one, so holding a claim |
|
| 17 | + | /// proves that its run is allocated and not reachable by any other owner. |
|
| 18 | + | export opaque record Claim: Copy { |
|
| 19 | + | /// Frames excluded from allocation. |
|
| 20 | + | run: Run, |
|
| 21 | + | } |
|
| 22 | + | ||
| 23 | + | /// Reserved frames that must be committed or cancelled in the same pool. |
|
| 24 | + | export union Reservation: Once { |
|
| 25 | + | /// Frames excluded from subsequent allocation scans. |
|
| 26 | + | Held(Claim), |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | /// Physical addresses and occupancy of allocatable frames, sorted by address. |
|
| 30 | + | export record Pool: Copy { |
|
| 31 | + | /// Physical base address of each eligible frame. |
|
| 32 | + | addresses: [u64; limits::FRAMES], |
|
| 33 | + | /// One set bit per available frame, indexed from the low bit of each word. |
|
| 34 | + | free: [u64; limits::FRAMES / 64], |
|
| 35 | + | /// Number of valid frame-map entries. |
|
| 36 | + | count: u32, |
|
| 37 | + | /// Retired runs whose frames remain unavailable until reclamation. |
|
| 38 | + | retired: [Run; limits::FRAMES], |
|
| 39 | + | /// Index of the next retired run to reclaim. |
|
| 40 | + | retiredHead: u32, |
|
| 41 | + | /// Number of occupied retirement entries. |
|
| 42 | + | retiredCount: u32, |
|
| 43 | + | } |
|
| 44 | + | ||
| 45 | + | /// Insert one range into an address-sorted prefix of fixed storage. |
|
| 46 | + | fn insert(ranges: &mut [range::Range], count: u32, value: range::Range) { |
|
| 47 | + | let mut at = count; |
|
| 48 | + | while at > 0 and ranges[at - 1].start > value.start { |
|
| 49 | + | set ranges[at] = ranges[at - 1]; |
|
| 50 | + | set at -= 1; |
|
| 51 | + | } |
|
| 52 | + | set ranges[at] = value; |
|
| 53 | + | } |
|
| 54 | + | ||
| 55 | + | /// Build a fresh frame map from complete RAM pages with no reserved byte. |
|
| 56 | + | /// The page at physical address 0 is never included. |
|
| 57 | + | /// The pool has zero valid entries if initialization fails. |
|
| 58 | + | export fn initialize(pool: &mut Pool, machine: &platform::Platform) throws (abi::Error) { |
|
| 59 | + | set pool.count = 0; |
|
| 60 | + | set pool.free = [0; limits::FRAMES / 64]; |
|
| 61 | + | set pool.retiredHead = 0; |
|
| 62 | + | set pool.retiredCount = 0; |
|
| 63 | + | if machine.ramCount > platform::RAM_BANKS or machine.reservedCount > platform::RESERVATIONS { |
|
| 64 | + | throw abi::Error::InvalidArg; |
|
| 65 | + | } |
|
| 66 | + | let mut reserved: [range::Range; platform::RESERVATIONS] = undefined; |
|
| 67 | + | for i in 0..machine.reservedCount { |
|
| 68 | + | let value = machine.reserved[i]; |
|
| 69 | + | if value.start >= value.end { |
|
| 70 | + | throw abi::Error::InvalidArg; |
|
| 71 | + | } |
|
| 72 | + | insert(&mut reserved[..], i, value); |
|
| 73 | + | } |
|
| 74 | + | let mut banks: [range::Range; platform::RAM_BANKS] = undefined; |
|
| 75 | + | for i in 0..machine.ramCount { |
|
| 76 | + | let bank = machine.ram[i]; |
|
| 77 | + | if bank.start >= bank.end { |
|
| 78 | + | throw abi::Error::InvalidArg; |
|
| 79 | + | } |
|
| 80 | + | insert(&mut banks[..], i, bank); |
|
| 81 | + | } |
|
| 82 | + | let mut count: u32 = 0; |
|
| 83 | + | let mut next: u32 = 0; |
|
| 84 | + | for i in 0..machine.ramCount { |
|
| 85 | + | let bank = banks[i]; |
|
| 86 | + | if i > 0 and banks[i - 1].end > bank.start { |
|
| 87 | + | throw abi::Error::InvalidArg; |
|
| 88 | + | } |
|
| 89 | + | let padding = |
|
| 90 | + | (limits::FRAME_SIZE - (bank.start & (limits::FRAME_SIZE - 1))) & (limits::FRAME_SIZE |
|
| 91 | + | - 1); |
|
| 92 | + | if padding > bank.end - bank.start { |
|
| 93 | + | continue; |
|
| 94 | + | } |
|
| 95 | + | let mut address = bank.start + padding; |
|
| 96 | + | let end = bank.end & ~(limits::FRAME_SIZE - 1); |
|
| 97 | + | while address < end { |
|
| 98 | + | // Slices must not point at address 0, and every non-empty access |
|
| 99 | + | // token covers pool frames, so the frame at 0 is never pooled. |
|
| 100 | + | if address == 0 { |
|
| 101 | + | set address += limits::FRAME_SIZE; |
|
| 102 | + | continue; |
|
| 103 | + | } |
|
| 104 | + | while next < machine.reservedCount and reserved[next].end <= address { |
|
| 105 | + | set next += 1; |
|
| 106 | + | } |
|
| 107 | + | if next < machine.reservedCount |
|
| 108 | + | and reserved[next].start < address + limits::FRAME_SIZE |
|
| 109 | + | { |
|
| 110 | + | if reserved[next].end >= end { |
|
| 111 | + | break; |
|
| 112 | + | } |
|
| 113 | + | set address = |
|
| 114 | + | (reserved[next].end + limits::FRAME_SIZE - 1) & ~(limits::FRAME_SIZE - 1); |
|
| 115 | + | continue; |
|
| 116 | + | } |
|
| 117 | + | if count == limits::FRAMES { |
|
| 118 | + | throw abi::Error::Exhausted; |
|
| 119 | + | } |
|
| 120 | + | set pool.addresses[count] = address; |
|
| 121 | + | set pool.free[count / 64] |= 1 << (count % 64) as u64; |
|
| 122 | + | set count += 1; |
|
| 123 | + | set address += limits::FRAME_SIZE; |
|
| 124 | + | } |
|
| 125 | + | } |
|
| 126 | + | set pool.count = count; |
|
| 127 | + | } |
|
| 128 | + | ||
| 129 | + | /// Test one valid frame's availability without exposing unused bitmap bits. |
|
| 130 | + | export fn available(pool: &Pool, index: u32) -> bool { |
|
| 131 | + | return index < pool.count and (pool.free[index / 64] & (1 << (index % 64) as u64)) <> 0; |
|
| 132 | + | } |
|
| 133 | + | ||
| 134 | + | /// Find the frame-map index of a frame-aligned physical address. |
|
| 135 | + | export fn find(pool: &Pool, address: u64) -> ?u32 { |
|
| 136 | + | let mut low: u32 = 0; |
|
| 137 | + | let mut high: u32 = pool.count; |
|
| 138 | + | while low < high { |
|
| 139 | + | let middle = low + (high - low) / 2; |
|
| 140 | + | let value = pool.addresses[middle]; |
|
| 141 | + | if value == address { |
|
| 142 | + | return middle; |
|
| 143 | + | } |
|
| 144 | + | if value < address { |
|
| 145 | + | set low = middle + 1; |
|
| 146 | + | } else { |
|
| 147 | + | set high = middle; |
|
| 148 | + | } |
|
| 149 | + | } |
|
| 150 | + | return nil; |
|
| 151 | + | } |
|
| 152 | + | ||
| 153 | + | /// Low bits selected by a nonzero word-sized span. |
|
| 154 | + | fn mask(count: u32) -> u64 { |
|
| 155 | + | assert count > 0 and count <= 64; |
|
| 156 | + | if count == 64 { |
|
| 157 | + | return 0xffffffffffffffff; |
|
| 158 | + | } |
|
| 159 | + | return (1 << count as u64) - 1; |
|
| 160 | + | } |
|
| 161 | + | ||
| 162 | + | /// Change a validated run's occupancy one bitmap word at a time. |
|
| 163 | + | fn assign(pool: &mut Pool, run: Run, free: bool) { |
|
| 164 | + | let mut first = run.first; |
|
| 165 | + | let end = first + run.count; |
|
| 166 | + | while first < end { |
|
| 167 | + | let offset = first % 64; |
|
| 168 | + | let mut count = 64 - offset; |
|
| 169 | + | if count > end - first { |
|
| 170 | + | set count = end - first; |
|
| 171 | + | } |
|
| 172 | + | let bits = mask(count) << offset as u64; |
|
| 173 | + | if free { |
|
| 174 | + | set pool.free[first / 64] |= bits; |
|
| 175 | + | } else { |
|
| 176 | + | set pool.free[first / 64] &= ~bits; |
|
| 177 | + | } |
|
| 178 | + | set first += count; |
|
| 179 | + | } |
|
| 180 | + | } |
|
| 181 | + | ||
| 182 | + | /// Test that every frame of a valid run is allocated, one bitmap word at a time. |
|
| 183 | + | /// Reserved, committed, and retired frames count as allocated. |
|
| 184 | + | export fn allocated(pool: &Pool, run: Run) -> bool { |
|
| 185 | + | if run.count == 0 or run.first >= pool.count or run.count > pool.count - run.first { |
|
| 186 | + | return false; |
|
| 187 | + | } |
|
| 188 | + | let mut first = run.first; |
|
| 189 | + | let end = first + run.count; |
|
| 190 | + | while first < end { |
|
| 191 | + | let offset = first % 64; |
|
| 192 | + | let mut count = 64 - offset; |
|
| 193 | + | if count > end - first { |
|
| 194 | + | set count = end - first; |
|
| 195 | + | } |
|
| 196 | + | if (pool.free[first / 64] & (mask(count) << offset as u64)) <> 0 { |
|
| 197 | + | return false; |
|
| 198 | + | } |
|
| 199 | + | set first += count; |
|
| 200 | + | } |
|
| 201 | + | return true; |
|
| 202 | + | } |
|
| 203 | + | ||
| 204 | + | /// Reserve the first contiguous run, scanning uniform bitmap words together. |
|
| 205 | + | export fn reserve(pool: &mut Pool, count: u32) -> Reservation throws (abi::Error) { |
|
| 206 | + | if count == 0 { |
|
| 207 | + | throw abi::Error::InvalidArg; |
|
| 208 | + | } |
|
| 209 | + | let mut first: u32 = 0; |
|
| 210 | + | let mut found: u32 = 0; |
|
| 211 | + | let mut previous: u64 = 0; |
|
| 212 | + | for word in 0..(pool.count + 63) / 64 { |
|
| 213 | + | let base = word * 64; |
|
| 214 | + | let mut length = pool.count - base; |
|
| 215 | + | if length > 64 { |
|
| 216 | + | set length = 64; |
|
| 217 | + | } |
|
| 218 | + | let full = mask(length); |
|
| 219 | + | let bits = pool.free[word] & full; |
|
| 220 | + | if bits == 0 { |
|
| 221 | + | set found = 0; |
|
| 222 | + | continue; |
|
| 223 | + | } |
|
| 224 | + | let address = pool.addresses[base]; |
|
| 225 | + | if bits == full |
|
| 226 | + | and pool.addresses[base + length - 1] - address == (length - 1) as u64 |
|
| 227 | + | * limits::FRAME_SIZE |
|
| 228 | + | { |
|
| 229 | + | if found == 0 or address <> previous + limits::FRAME_SIZE { |
|
| 230 | + | set first = base; |
|
| 231 | + | set found = 0; |
|
| 232 | + | } |
|
| 233 | + | set found += length; |
|
| 234 | + | set previous = pool.addresses[base + length - 1]; |
|
| 235 | + | } else { |
|
| 236 | + | for offset in 0..length { |
|
| 237 | + | let i = base + offset; |
|
| 238 | + | if (bits & (1 << offset as u64)) == 0 { |
|
| 239 | + | set found = 0; |
|
| 240 | + | continue; |
|
| 241 | + | } |
|
| 242 | + | if found == 0 or pool.addresses[i] <> previous + limits::FRAME_SIZE { |
|
| 243 | + | set first = i; |
|
| 244 | + | set found = 0; |
|
| 245 | + | } |
|
| 246 | + | set found += 1; |
|
| 247 | + | set previous = pool.addresses[i]; |
|
| 248 | + | if found >= count { |
|
| 249 | + | break; |
|
| 250 | + | } |
|
| 251 | + | } |
|
| 252 | + | } |
|
| 253 | + | if found >= count { |
|
| 254 | + | let run = Run { first, count }; |
|
| 255 | + | assign(pool, run, false); |
|
| 256 | + | return Reservation::Held(Claim { run }); |
|
| 257 | + | } |
|
| 258 | + | } |
|
| 259 | + | throw abi::Error::OutOfMemory; |
|
| 260 | + | } |
|
| 261 | + | ||
| 262 | + | /// Read the run of a held reservation without consuming it. |
|
| 263 | + | export fn reserved(reservation: &Reservation) -> Run { |
|
| 264 | + | match reservation { |
|
| 265 | + | case Reservation::Held(claim) => return claim.run, |
|
| 266 | + | } |
|
| 267 | + | } |
|
| 268 | + | ||
| 269 | + | /// Consume a reservation into a backing allocation's frame ownership. |
|
| 270 | + | export fn commit(reservation: Reservation) -> Run { |
|
| 271 | + | match reservation { |
|
| 272 | + | case Reservation::Held(claim) => return claim.run, |
|
| 273 | + | } |
|
| 274 | + | } |
|
| 275 | + | ||
| 276 | + | /// Resolve a complete physical run and reject map gaps or invalid indices. |
|
| 277 | + | export fn extent(pool: &Pool, run: Run) -> range::Range throws (abi::Error) { |
|
| 278 | + | if run.count == 0 or run.first >= pool.count or run.count > pool.count - run.first { |
|
| 279 | + | throw abi::Error::BadHandle; |
|
| 280 | + | } |
|
| 281 | + | let start = pool.addresses[run.first]; |
|
| 282 | + | let length = run.count as u64 * limits::FRAME_SIZE; |
|
| 283 | + | let result = range::new(start, length) else { |
|
| 284 | + | throw abi::Error::BadHandle; |
|
| 285 | + | }; |
|
| 286 | + | if pool.addresses[run.first + run.count - 1] <> result.end - limits::FRAME_SIZE { |
|
| 287 | + | throw abi::Error::BadHandle; |
|
| 288 | + | } |
|
| 289 | + | return result; |
|
| 290 | + | } |
|
| 291 | + | ||
| 292 | + | /// Return a backing allocation's frames after all handles and exposures end. |
|
| 293 | + | /// Only the owner of a committed run may release it. |
|
| 294 | + | export fn release(pool: &mut Pool, run: Run) throws (abi::Error) { |
|
| 295 | + | let memory = try extent(pool, run); |
|
| 296 | + | let mut first = run.first; |
|
| 297 | + | let end = first + run.count; |
|
| 298 | + | while first < end { |
|
| 299 | + | let offset = first % 64; |
|
| 300 | + | let mut count = 64 - offset; |
|
| 301 | + | if count > end - first { |
|
| 302 | + | set count = end - first; |
|
| 303 | + | } |
|
| 304 | + | if (pool.free[first / 64] & (mask(count) << offset as u64)) <> 0 { |
|
| 305 | + | throw abi::Error::BadHandle; |
|
| 306 | + | } |
|
| 307 | + | set first += count; |
|
| 308 | + | } |
|
| 309 | + | assign(pool, run, true); |
|
| 310 | + | } |
|
| 311 | + | ||
| 312 | + | /// Transfer exclusive ownership of a committed run to bounded reclamation. |
|
| 313 | + | /// The caller must not release or retire this run again. |
|
| 314 | + | export fn retire(pool: &mut Pool, run: Run) throws (abi::Error) { |
|
| 315 | + | let memory = try extent(pool, run); |
|
| 316 | + | assert pool.retiredCount < pool.count; |
|
| 317 | + | let tail = (pool.retiredHead + pool.retiredCount) % limits::FRAMES; |
|
| 318 | + | set pool.retired[tail] = run; |
|
| 319 | + | set pool.retiredCount += 1; |
|
| 320 | + | } |
|
| 321 | + | ||
| 322 | + | /// Return at most limit retired frames while metadata is serialized. |
|
| 323 | + | export fn reclaim(pool: &mut Pool, limit: u32) -> u32 { |
|
| 324 | + | let mut returned: u32 = 0; |
|
| 325 | + | while pool.retiredCount > 0 and returned < limit { |
|
| 326 | + | let mut count = pool.retired[pool.retiredHead].count; |
|
| 327 | + | if count > limit - returned { |
|
| 328 | + | set count = limit - returned; |
|
| 329 | + | } |
|
| 330 | + | let batch = Run { first: pool.retired[pool.retiredHead].first, count }; |
|
| 331 | + | try! release(pool, batch); |
|
| 332 | + | set pool.retired[pool.retiredHead].first += count; |
|
| 333 | + | set pool.retired[pool.retiredHead].count -= count; |
|
| 334 | + | set returned += count; |
|
| 335 | + | if pool.retired[pool.retiredHead].count == 0 { |
|
| 336 | + | set pool.retiredHead = (pool.retiredHead + 1) % limits::FRAMES; |
|
| 337 | + | set pool.retiredCount -= 1; |
|
| 338 | + | } |
|
| 339 | + | } |
|
| 340 | + | return returned; |
|
| 341 | + | } |
|
| 342 | + | ||
| 343 | + | /// Cancel unpublished frame ownership and restore its capacity. |
|
| 344 | + | export fn cancel(pool: &mut Pool, reservation: Reservation) throws (abi::Error) { |
|
| 345 | + | match reservation { |
|
| 346 | + | case Reservation::Held(claim) => try release(pool, claim.run), |
|
| 347 | + | } |
|
| 348 | + | } |
kernel/kernel/instances.rad
added
+199 -0
| 1 | + | //! Private package-state allocation and relocation for one domain graph. |
|
| 2 | + | ||
| 3 | + | use std::arch::rv64::shared; |
|
| 4 | + | use super::abi; |
|
| 5 | + | use super::limits; |
|
| 6 | + | use super::frames; |
|
| 7 | + | use super::physical; |
|
| 8 | + | use super::registry; |
|
| 9 | + | ||
| 10 | + | /// Physical storage owned by one private package graph. |
|
| 11 | + | export union Instance { |
|
| 12 | + | /// The complete allocation, retained until the domain is reclaimed, and the |
|
| 13 | + | /// token for its base table, indexed by resident slot. Absent packages have |
|
| 14 | + | /// zero bases. `publish` moves the token into the domain store. |
|
| 15 | + | Held(frames::Run, physical::Access), |
|
| 16 | + | } |
|
| 17 | + | ||
| 18 | + | /// Check that a token covers exactly the frames of an allocation. |
|
| 19 | + | export fn covers(allocation: &Allocation, access: &physical::Access) -> bool { |
|
| 20 | + | return physical::covers( |
|
| 21 | + | access, allocation.base, allocation.frames.count as u64 * limits::FRAME_SIZE |
|
| 22 | + | ); |
|
| 23 | + | } |
|
| 24 | + | ||
| 25 | + | /// Bounded traversal and conservative storage requirement for a package graph. |
|
| 26 | + | export record Graph: Copy { |
|
| 27 | + | /// Reachable package references, each present once. |
|
| 28 | + | packages: [abi::Ref; limits::PACKAGES], |
|
| 29 | + | /// Number of initialized package references. |
|
| 30 | + | count: u32, |
|
| 31 | + | /// Bytes for the base table, private state, and worst-case alignment padding. |
|
| 32 | + | bytes: u64, |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Private storage and the resident graph selected at reservation time. |
|
| 36 | + | export record Allocation: Copy { |
|
| 37 | + | /// Reachable immutable packages and their storage bounds. |
|
| 38 | + | graph: Graph, |
|
| 39 | + | /// Physical frames reserved for this instance. |
|
| 40 | + | frames: frames::Run, |
|
| 41 | + | /// Validated physical base for private initialization. |
|
| 42 | + | base: u64, |
|
| 43 | + | } |
|
| 44 | + | ||
| 45 | + | /// Graph storage that must be committed or returned to its frame pool. |
|
| 46 | + | export union Reservation: Once { |
|
| 47 | + | /// An unpublished private instance and the token that proves its frames are private. |
|
| 48 | + | /// The token covers exactly `frames` from `base`. |
|
| 49 | + | Held(Allocation, physical::Access), |
|
| 50 | + | } |
|
| 51 | + | ||
| 52 | + | /// Collect the reachable immutable packages with a bounded queue. |
|
| 53 | + | fn graph(store: ®istry::Store, root: abi::Ref) -> Graph throws (abi::Error) { |
|
| 54 | + | let package = try registry::get(store, root); |
|
| 55 | + | let mut result = Graph { packages: undefined, count: 1, bytes: limits::PACKAGES as u64 * 8 }; |
|
| 56 | + | let mut seen = [false; limits::PACKAGES]; |
|
| 57 | + | set result.packages[0] = root; |
|
| 58 | + | set seen[root.index] = true; |
|
| 59 | + | let mut next: u32 = 0; |
|
| 60 | + | while next < result.count { |
|
| 61 | + | let object = result.packages[next]; |
|
| 62 | + | let package = try registry::get(store, object); |
|
| 63 | + | if package.alignment == 0 or (package.alignment & (package.alignment - 1)) <> 0 { |
|
| 64 | + | throw abi::Error::VerifyFailed; |
|
| 65 | + | } |
|
| 66 | + | set result.bytes += package.memory as u64 + package.alignment as u64 - 1; |
|
| 67 | + | if result.bytes > limits::FRAMES as u64 * limits::FRAME_SIZE { |
|
| 68 | + | throw abi::Error::OutOfMemory; |
|
| 69 | + | } |
|
| 70 | + | for i in 0..store.counts[object.index] { |
|
| 71 | + | let dependency = store.dependencies[object.index][i]; |
|
| 72 | + | let target = try registry::get(store, dependency); |
|
| 73 | + | if seen[dependency.index] { |
|
| 74 | + | continue; |
|
| 75 | + | } |
|
| 76 | + | set seen[dependency.index] = true; |
|
| 77 | + | set result.packages[result.count] = dependency; |
|
| 78 | + | set result.count += 1; |
|
| 79 | + | } |
|
| 80 | + | set next += 1; |
|
| 81 | + | } |
|
| 82 | + | return result; |
|
| 83 | + | } |
|
| 84 | + | ||
| 85 | + | /// Assign every base before initializing templates and their private pointers. |
|
| 86 | + | /// The caller owns a writable extent of at least graph.bytes bytes at base. |
|
| 87 | + | /// Every package is validated before any byte is written. The view must have |
|
| 88 | + | /// been captured after the graph was reserved, so it holds every package in it. |
|
| 89 | + | export fn fill(view: ®istry::Published, graph: &Graph, token: &physical::Access, base: u64) |
|
| 90 | + | throws (abi::Error) |
|
| 91 | + | { |
|
| 92 | + | let mut table: [u64; limits::PACKAGES] = [0; limits::PACKAGES]; |
|
| 93 | + | let mut next = base + limits::PACKAGES as u64 * 8; |
|
| 94 | + | for object in &graph.packages[..graph.count] { |
|
| 95 | + | let package = try! registry::lookup(view, object); |
|
| 96 | + | let mask = package.alignment as u64 - 1; |
|
| 97 | + | set next = (next + mask) & ~mask; |
|
| 98 | + | set table[object.index] = next; |
|
| 99 | + | set next += package.memory as u64; |
|
| 100 | + | } |
|
| 101 | + | for object in &graph.packages[..graph.count] { |
|
| 102 | + | let package = try! registry::lookup(view, object); |
|
| 103 | + | try shared::checkInstance(&package, &table[..]) catch { |
|
| 104 | + | throw abi::Error::VerifyFailed; |
|
| 105 | + | }; |
|
| 106 | + | } |
|
| 107 | + | try physical::writeWordsAt(token, 0, &table[..]); |
|
| 108 | + | for object in &graph.packages[..graph.count] { |
|
| 109 | + | let package = try! registry::lookup(view, object); |
|
| 110 | + | let state = table[object.index]; |
|
| 111 | + | try physical::zeroAt(token, state - base, package.memory as u64); |
|
| 112 | + | try physical::writeAt(token, state - base, package.template); |
|
| 113 | + | for fixup in package.relocations { |
|
| 114 | + | let target = table[fixup.target.slot] + fixup.target.offset as u64; |
|
| 115 | + | try physical::fillWordsAt( |
|
| 116 | + | token, state + fixup.offset as u64 - base, target, fixup.count |
|
| 117 | + | ); |
|
| 118 | + | } |
|
| 119 | + | } |
|
| 120 | + | } |
|
| 121 | + | ||
| 122 | + | /// Reserve one graph under metadata serialization while its packages stay resident. |
|
| 123 | + | export fn reserve(store: ®istry::Store, pool: &mut frames::Pool, root: abi::Ref) -> Reservation |
|
| 124 | + | throws (abi::Error) |
|
| 125 | + | { |
|
| 126 | + | let layout = try graph(store, root); |
|
| 127 | + | let count = ((layout.bytes + limits::FRAME_SIZE - 1) / limits::FRAME_SIZE) as u32; |
|
| 128 | + | let pending = try frames::reserve(pool, count); |
|
| 129 | + | let access = physical::reserved(&*pool, &pending); |
|
| 130 | + | let run = frames::commit(pending); |
|
| 131 | + | let extent = try! frames::extent(pool, run); |
|
| 132 | + | return Reservation::Held(Allocation { graph: layout, frames: run, base: extent.start }, access); |
|
| 133 | + | } |
|
| 134 | + | ||
| 135 | + | /// Initialize private state from the reservation's immutable resident packages. |
|
| 136 | + | export fn initialize(view: ®istry::Published, reservation: &Reservation) throws (abi::Error) { |
|
| 137 | + | match reservation { |
|
| 138 | + | case Reservation::Held(allocation, access) => |
|
| 139 | + | try fill( |
|
| 140 | + | view, &allocation.graph, access, allocation.base |
|
| 141 | + | ), |
|
| 142 | + | } |
|
| 143 | + | } |
|
| 144 | + | ||
| 145 | + | /// Consume initialized private storage for publication in a domain. |
|
| 146 | + | /// The allocation always holds the base table, because graph bytes start at its size. |
|
| 147 | + | export fn commit(reservation: Reservation) -> Instance { |
|
| 148 | + | match reservation { |
|
| 149 | + | case Reservation::Held(allocation, access) => |
|
| 150 | + | return Instance::Held( |
|
| 151 | + | allocation.frames, try! physical::narrow(access, 0, limits::PACKAGES as u64 * 8) |
|
| 152 | + | ), |
|
| 153 | + | } |
|
| 154 | + | } |
|
| 155 | + | ||
| 156 | + | /// Move an instance's base-table token into `table` and return its frames. |
|
| 157 | + | /// `table` is the owning domain's slot in the store, which must hold no token. |
|
| 158 | + | export fn publish(built: Instance, table: &mut physical::Access) -> frames::Run { |
|
| 159 | + | match built { |
|
| 160 | + | case Instance::Held(run, access) => { |
|
| 161 | + | assert physical::size(table) == 0; |
|
| 162 | + | set *table = access; |
|
| 163 | + | return run; |
|
| 164 | + | } |
|
| 165 | + | } |
|
| 166 | + | } |
|
| 167 | + | ||
| 168 | + | /// Read one package base from an instance's base table. |
|
| 169 | + | /// Only the metadata lock holder rewrites the table. |
|
| 170 | + | export fn base(table: &physical::Access, slot: u32) -> u64 { |
|
| 171 | + | assert slot < limits::PACKAGES; |
|
| 172 | + | return try! physical::read64At(table, slot as u64 * 8); |
|
| 173 | + | } |
|
| 174 | + | ||
| 175 | + | /// Address of an instance's base table, which user code receives as its global pointer. |
|
| 176 | + | export fn address(table: &physical::Access) -> u64 { |
|
| 177 | + | return physical::start(table); |
|
| 178 | + | } |
|
| 179 | + | ||
| 180 | + | /// Return unpublished graph storage under frame-pool serialization. |
|
| 181 | + | export fn cancel(pool: &mut frames::Pool, reservation: Reservation) { |
|
| 182 | + | match reservation { |
|
| 183 | + | case Reservation::Held(allocation, access) => { |
|
| 184 | + | try! frames::release(pool, allocation.frames); |
|
| 185 | + | } |
|
| 186 | + | } |
|
| 187 | + | } |
|
| 188 | + | ||
| 189 | + | /// Allocate and initialize a private graph while the caller has exclusive metadata access. |
|
| 190 | + | export fn create(store: ®istry::Store, pool: &mut frames::Pool, root: abi::Ref) -> Instance |
|
| 191 | + | throws (abi::Error) |
|
| 192 | + | { |
|
| 193 | + | let pending = try reserve(store, pool, root); |
|
| 194 | + | try initialize(®istry::capture(store), &pending) catch err { |
|
| 195 | + | cancel(pool, pending); |
|
| 196 | + | throw err; |
|
| 197 | + | }; |
|
| 198 | + | return commit(pending); |
|
| 199 | + | } |
kernel/kernel/interrupts.rad
added
+176 -0
| 1 | + | //! Exclusive IRQ ownership and retained notification state. |
|
| 2 | + | use super::abi; |
|
| 3 | + | use super::limits; |
|
| 4 | + | use super::slots; |
|
| 5 | + | use super::capability; |
|
| 6 | + | use super::transactions; |
|
| 7 | + | use super::domains; |
|
| 8 | + | use super::events; |
|
| 9 | + | ||
| 10 | + | /// Notification state while hardware delivery is serialized. |
|
| 11 | + | export union State: Copy { |
|
| 12 | + | /// No retained notification prevents the source from being enabled. |
|
| 13 | + | Idle, |
|
| 14 | + | /// The claimed source needs an event in its current receiver. |
|
| 15 | + | Pending, |
|
| 16 | + | /// A queued event must be consumed before the source is enabled. |
|
| 17 | + | Queued, |
|
| 18 | + | } |
|
| 19 | + | ||
| 20 | + | /// One physical source; its table index is the PLIC number minus one. |
|
| 21 | + | export record Interrupt: Copy { |
|
| 22 | + | /// Domain holding the sole interrupt capability. |
|
| 23 | + | owner: abi::Ref, |
|
| 24 | + | /// Sole live capability in the owner's table. |
|
| 25 | + | handle: abi::Handle, |
|
| 26 | + | /// Retained notification and rearming state. |
|
| 27 | + | state: State, |
|
| 28 | + | } |
|
| 29 | + | ||
| 30 | + | /// Fixed source identities and their exclusive receivers. |
|
| 31 | + | export record Store: Copy { |
|
| 32 | + | /// Number of physical sources reported by the platform. |
|
| 33 | + | count: u32, |
|
| 34 | + | /// Source object generations, indexed by PLIC number minus one. |
|
| 35 | + | slots: [slots::Slot; limits::INTERRUPTS], |
|
| 36 | + | /// Published ownership and notification state. |
|
| 37 | + | records: [Interrupt; limits::INTERRUPTS], |
|
| 38 | + | } |
|
| 39 | + | ||
| 40 | + | /// Initialize the bounded physical-source profile before issuing capabilities. |
|
| 41 | + | export fn initialize(store: &mut Store, count: u32) { |
|
| 42 | + | assert count <= limits::INTERRUPTS; |
|
| 43 | + | set store.count = count; |
|
| 44 | + | slots::initialize(&mut store.slots[..]); |
|
| 45 | + | } |
|
| 46 | + | ||
| 47 | + | /// Resolve a source object after checking its generation. |
|
| 48 | + | export fn get(store: &Store, object: abi::Ref) -> Interrupt throws (abi::Error) { |
|
| 49 | + | if object.index >= store.count or not slots::matches( |
|
| 50 | + | &store.slots[..], object, slots::State::Live |
|
| 51 | + | ) |
|
| 52 | + | { |
|
| 53 | + | throw abi::Error::BadHandle; |
|
| 54 | + | } |
|
| 55 | + | return store.records[object.index]; |
|
| 56 | + | } |
|
| 57 | + | ||
| 58 | + | /// Resolve the sole capability owner with the required rights. |
|
| 59 | + | fn owned(store: &Store, table: &capability::Table, handle: abi::Handle, rights: u16) -> abi::Ref |
|
| 60 | + | throws (abi::Error) |
|
| 61 | + | { |
|
| 62 | + | let entry = try capability::lookup(table, handle, abi::Kind::Interrupt, abi::Rights(rights)); |
|
| 63 | + | let source = try get(store, entry.object); |
|
| 64 | + | if source.owner <> table.owner or source.handle <> handle { |
|
| 65 | + | throw abi::Error::BadHandle; |
|
| 66 | + | } |
|
| 67 | + | return entry.object; |
|
| 68 | + | } |
|
| 69 | + | ||
| 70 | + | /// Issue one source during trusted resource initialization. |
|
| 71 | + | export fn seed(store: &mut Store, table: &mut capability::Table, number: u32) -> abi::Handle |
|
| 72 | + | throws (abi::Error) |
|
| 73 | + | { |
|
| 74 | + | if number == 0 or number > store.count { |
|
| 75 | + | throw abi::Error::InvalidArg; |
|
| 76 | + | } |
|
| 77 | + | let index = number - 1; |
|
| 78 | + | if store.slots[index].state <> slots::State::Free { |
|
| 79 | + | throw abi::Error::Busy; |
|
| 80 | + | } |
|
| 81 | + | let pending = try slots::reserve(&mut table.slots[..]); |
|
| 82 | + | let object = abi::Ref { index, generation: store.slots[index].generation }; |
|
| 83 | + | let handle = try! abi::handle(abi::Kind::Interrupt, slots::reference(&pending)); |
|
| 84 | + | set store.slots[index].state = slots::State::Reserved; |
|
| 85 | + | set store.records[index] = Interrupt { owner: table.owner, handle, state: State::Idle }; |
|
| 86 | + | let live = try! slots::commit(&mut store.slots[..], slots::Reservation::Held(object)); |
|
| 87 | + | return capability::publish(table, pending, capability::Entry { |
|
| 88 | + | kind: abi::Kind::Interrupt, object: live, rights: abi::Rights(abi::READ | abi::TRANSFER) |
|
| 89 | + | }); |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// Read the source number through its sole capability. |
|
| 93 | + | export fn query(store: &Store, table: &capability::Table, handle: abi::Handle) -> u32 |
|
| 94 | + | throws (abi::Error) |
|
| 95 | + | { |
|
| 96 | + | return (try owned(store, table, handle, 0)).index + 1; |
|
| 97 | + | } |
|
| 98 | + | ||
| 99 | + | /// Move exclusive ownership after reserving the receiver's capability slot. |
|
| 100 | + | export fn transfer( |
|
| 101 | + | store: &mut Store, |
|
| 102 | + | source: &mut capability::Table, |
|
| 103 | + | target: &mut capability::Table, |
|
| 104 | + | handle: abi::Handle, |
|
| 105 | + | rights: u64 |
|
| 106 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 107 | + | let object = try owned(store, source, handle, abi::TRANSFER); |
|
| 108 | + | let entry = try! capability::get(source, handle); |
|
| 109 | + | let pending = try transactions::reserve(target, source.owner, handle, entry, rights, true); |
|
| 110 | + | let result = transactions::transfer(source, target, pending); |
|
| 111 | + | set store.records[object.index].owner = target.owner; |
|
| 112 | + | set store.records[object.index].handle = result; |
|
| 113 | + | if store.records[object.index].state == State::Queued { |
|
| 114 | + | set store.records[object.index].state = State::Pending; |
|
| 115 | + | } |
|
| 116 | + | return result; |
|
| 117 | + | } |
|
| 118 | + | ||
| 119 | + | /// Invalidate a source capability after its hardware route has been masked. |
|
| 120 | + | export fn drop(store: &mut Store, table: &mut capability::Table, handle: abi::Handle) |
|
| 121 | + | throws (abi::Error) |
|
| 122 | + | { |
|
| 123 | + | let object = try owned(store, table, handle, 0); |
|
| 124 | + | try! capability::invalidate(table, handle); |
|
| 125 | + | try! slots::release(&mut store.slots[..], object); |
|
| 126 | + | } |
|
| 127 | + | ||
| 128 | + | /// Retain a claimed source without scanning domain capability tables. |
|
| 129 | + | export fn claim(store: &mut Store, number: u32) -> bool { |
|
| 130 | + | if number == 0 or number > store.count { |
|
| 131 | + | return false; |
|
| 132 | + | } |
|
| 133 | + | let index = number - 1; |
|
| 134 | + | if store.slots[index].state <> slots::State::Live { |
|
| 135 | + | return false; |
|
| 136 | + | } |
|
| 137 | + | if store.records[index].state == State::Idle { |
|
| 138 | + | set store.records[index].state = State::Pending; |
|
| 139 | + | } |
|
| 140 | + | return true; |
|
| 141 | + | } |
|
| 142 | + | ||
| 143 | + | /// Deliver retained work or acknowledge consumption; true permits hardware rearming. |
|
| 144 | + | export fn service(store: &mut Store, receivers: &mut domains::Store, number: u32) -> bool |
|
| 145 | + | throws (abi::Error) |
|
| 146 | + | { |
|
| 147 | + | if number == 0 or number > store.count { |
|
| 148 | + | throw abi::Error::InvalidArg; |
|
| 149 | + | } |
|
| 150 | + | let index = number - 1; |
|
| 151 | + | if store.slots[index].state <> slots::State::Live { |
|
| 152 | + | return false; |
|
| 153 | + | } |
|
| 154 | + | let source = store.records[index]; |
|
| 155 | + | let domain = try domains::get(receivers, source.owner) catch { |
|
| 156 | + | return false; |
|
| 157 | + | }; |
|
| 158 | + | if domain.state == domains::Lifecycle::Dead { |
|
| 159 | + | return false; |
|
| 160 | + | } |
|
| 161 | + | try domains::serve(receivers, source.owner, domains::RingRequest::Refresh); |
|
| 162 | + | if source.state == State::Queued { |
|
| 163 | + | if events::interruptPending(&receivers.events, source.owner, number) { |
|
| 164 | + | return false; |
|
| 165 | + | } |
|
| 166 | + | set store.records[index].state = State::Idle; |
|
| 167 | + | } else if source.state == State::Pending { |
|
| 168 | + | if try domains::serve(receivers, source.owner, domains::RingRequest::Send(events::Event { |
|
| 169 | + | kind: events::INTERRUPT, reserved: 0, code: number, value: 0 |
|
| 170 | + | })) { |
|
| 171 | + | set store.records[index].state = State::Queued; |
|
| 172 | + | } |
|
| 173 | + | return false; |
|
| 174 | + | } |
|
| 175 | + | return true; |
|
| 176 | + | } |
kernel/kernel/lifecycle.rad
added
+899 -0
| 1 | + | //! Terminal notification delivery and execution eligibility. |
|
| 2 | + | use std::lang::il::binary; |
|
| 3 | + | use super::limits; |
|
| 4 | + | use super::slots; |
|
| 5 | + | use super::events; |
|
| 6 | + | use super::domains; |
|
| 7 | + | use super::budgets; |
|
| 8 | + | use super::abi; |
|
| 9 | + | use super::remote; |
|
| 10 | + | use super::pages; |
|
| 11 | + | use super::registry; |
|
| 12 | + | use super::loader; |
|
| 13 | + | use super::capability; |
|
| 14 | + | use super::frames; |
|
| 15 | + | use super::physical; |
|
| 16 | + | use super::backing; |
|
| 17 | + | use super::interrupts; |
|
| 18 | + | use super::plic; |
|
| 19 | + | ||
| 20 | + | /// Maximum backing records released in one domain cleanup step. |
|
| 21 | + | constant CLEANUP_BATCH: u32 = 64; |
|
| 22 | + | ||
| 23 | + | /// Words needed to track request ownership for every context slot. |
|
| 24 | + | constant CONTEXT_WORDS: u32 = (limits::CONTEXTS + 31) / 32; |
|
| 25 | + | ||
| 26 | + | /// Private input and optional native output retained by a loader call. |
|
| 27 | + | /// The record holds frames and slots only; the continuation holds the tokens. |
|
| 28 | + | export record Loading: Copy { |
|
| 29 | + | /// Workspace and reserved caller handle. |
|
| 30 | + | input: loader::Input, |
|
| 31 | + | /// Output frames and package slot, when compilation has been admitted. |
|
| 32 | + | output: ?loader::Output, |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Tokens for a load's retained input and, once admitted, its output. |
|
| 36 | + | export union LoadAccess { |
|
| 37 | + | /// Workspace and source tokens, before output is reserved. |
|
| 38 | + | Input(loader::InputAccess), |
|
| 39 | + | /// Workspace and source tokens, and the reserved output's tokens. |
|
| 40 | + | Output(loader::InputAccess, loader::OutputAccess), |
|
| 41 | + | } |
|
| 42 | + | ||
| 43 | + | /// Add the tokens of a reserved output to a load's tokens. |
|
| 44 | + | export fn withOutput(access: LoadAccess, output: loader::OutputAccess) -> LoadAccess { |
|
| 45 | + | match access { |
|
| 46 | + | case LoadAccess::Input(input) => return LoadAccess::Output(input, output), |
|
| 47 | + | case LoadAccess::Output(input, previous) => panic "withOutput: output already reserved", |
|
| 48 | + | } |
|
| 49 | + | } |
|
| 50 | + | ||
| 51 | + | /// Successful loader work ready for publication under the metadata lock. |
|
| 52 | + | export union LoadResult: Once { |
|
| 53 | + | /// Matching immutable package already present in the registry, and the load's tokens. |
|
| 54 | + | Existing(abi::Ref, LoadAccess), |
|
| 55 | + | /// Generated code and metadata in the retained output allocation, and the load's tokens. |
|
| 56 | + | Compiled(loader::Compiled, LoadAccess), |
|
| 57 | + | } |
|
| 58 | + | ||
| 59 | + | /// Private storage retained by an unfinished syscall. |
|
| 60 | + | /// |
|
| 61 | + | /// No variant holds a physical token. The continuation that owns the lease holds |
|
| 62 | + | /// the tokens and hands them back when it takes the allocation. Teardown of a |
|
| 63 | + | /// stopped continuation releases frames from these records alone; its stranded |
|
| 64 | + | /// tokens are never used, because the context never resumes. |
|
| 65 | + | export union Allocation: Copy { |
|
| 66 | + | /// Page frames and unpublished capability metadata. |
|
| 67 | + | Page(pages::Allocation), |
|
| 68 | + | /// Additional context slot and private kernel stack. |
|
| 69 | + | Context(domains::ContextAllocation), |
|
| 70 | + | /// Reserved domain generation with its retained creation payload. |
|
| 71 | + | Domain(abi::Ref), |
|
| 72 | + | /// Private compiler workspace and unpublished output. |
|
| 73 | + | Load(Loading), |
|
| 74 | + | } |
|
| 75 | + | ||
| 76 | + | /// One unfinished allocation retained by a caller generation. |
|
| 77 | + | export record HeldAllocation: Copy { |
|
| 78 | + | /// Context whose continuation owns this allocation. |
|
| 79 | + | context: abi::Ref, |
|
| 80 | + | /// Calling domain, which can differ from the allocation target. |
|
| 81 | + | owner: abi::Ref, |
|
| 82 | + | /// Frames and metadata slots excluded from allocation. |
|
| 83 | + | allocation: Allocation, |
|
| 84 | + | } |
|
| 85 | + | ||
| 86 | + | /// Allocations retained while their contexts initialize private frames. |
|
| 87 | + | export record Calls: Copy { |
|
| 88 | + | /// One validity bit per occupied context slot. |
|
| 89 | + | active: [u32; CONTEXT_WORDS], |
|
| 90 | + | /// Reservation payloads indexed by context slot. |
|
| 91 | + | records: [HeldAllocation; limits::CONTEXTS], |
|
| 92 | + | /// Creation payloads valid while their domain slots are reserved. |
|
| 93 | + | creations: [domains::Creation; limits::DOMAINS], |
|
| 94 | + | } |
|
| 95 | + | ||
| 96 | + | /// A continuation's exclusive claim to its retained reservation. |
|
| 97 | + | export union Lease: Once { |
|
| 98 | + | /// Context generation that must take back the reservation before completion. |
|
| 99 | + | Held(Claim), |
|
| 100 | + | } |
|
| 101 | + | ||
| 102 | + | /// Context generation behind a lease. Opaque, so only `hold` can issue a lease. |
|
| 103 | + | export opaque record Claim: Copy { |
|
| 104 | + | /// Context that retains the reservation. |
|
| 105 | + | context: abi::Ref, |
|
| 106 | + | } |
|
| 107 | + | ||
| 108 | + | /// Consume a lease and report the context whose continuation held it. |
|
| 109 | + | export fn leaseContext(lease: Lease) -> abi::Ref { |
|
| 110 | + | match lease { |
|
| 111 | + | case Lease::Held(context) => return context.context, |
|
| 112 | + | } |
|
| 113 | + | } |
|
| 114 | + | ||
| 115 | + | /// Clear allocation ownership before contexts start. |
|
| 116 | + | export fn initialize(calls: &mut Calls) { |
|
| 117 | + | for i in 0..CONTEXT_WORDS { |
|
| 118 | + | set calls.active[i] = 0; |
|
| 119 | + | } |
|
| 120 | + | } |
|
| 121 | + | ||
| 122 | + | /// Retain private storage and pin the caller's kernel stack under the lock. |
|
| 123 | + | fn hold( |
|
| 124 | + | calls: &mut Calls, store: &mut domains::Store, context: abi::Ref, allocation: Allocation |
|
| 125 | + | ) -> Lease { |
|
| 126 | + | assert context.index < limits::CONTEXTS and context.generation <> 0; |
|
| 127 | + | assert slots::matches(&store.contextSlots[..], context, slots::State::Live); |
|
| 128 | + | assert not store.contexts[context.index].reservation; |
|
| 129 | + | let bit = 1 << (context.index % 32); |
|
| 130 | + | assert (calls.active[context.index / 32] & bit) == 0; |
|
| 131 | + | set calls.records[context.index] = HeldAllocation { |
|
| 132 | + | context, owner: store.contexts[context.index].owner, allocation |
|
| 133 | + | }; |
|
| 134 | + | set calls.active[context.index / 32] |= bit; |
|
| 135 | + | set store.contexts[context.index].reservation = true; |
|
| 136 | + | return Lease::Held(Claim { context }); |
|
| 137 | + | } |
|
| 138 | + | ||
| 139 | + | /// A held page reservation: the lease, and the token to clear the frames with. |
|
| 140 | + | export union PageHeld: Once { |
|
| 141 | + | /// The continuation keeps the token until it takes the page back. |
|
| 142 | + | Held(Lease, physical::Access), |
|
| 143 | + | } |
|
| 144 | + | ||
| 145 | + | /// A held context reservation: the lease, and the token to clear the stack with. |
|
| 146 | + | export union ContextHeld: Once { |
|
| 147 | + | /// The continuation keeps the token until it takes the context back. |
|
| 148 | + | Held(Lease, physical::Access), |
|
| 149 | + | } |
|
| 150 | + | ||
| 151 | + | /// A held domain reservation: the lease, and the tokens to initialize its storage with. |
|
| 152 | + | export union DomainHeld: Once { |
|
| 153 | + | /// The continuation keeps the tokens until it takes the domain back. |
|
| 154 | + | Held(Lease, domains::CreationAccess), |
|
| 155 | + | } |
|
| 156 | + | ||
| 157 | + | /// A held load: the lease, and the tokens to decode and compile with. |
|
| 158 | + | export union LoadHeld: Once { |
|
| 159 | + | /// The continuation keeps the tokens until it finishes or cancels the load. |
|
| 160 | + | Held(Lease, LoadAccess), |
|
| 161 | + | } |
|
| 162 | + | ||
| 163 | + | /// Transfer a page reservation into context-owned storage under the metadata lock. |
|
| 164 | + | /// The token goes to the continuation, and only the page record is retained. |
|
| 165 | + | export fn holdPage( |
|
| 166 | + | calls: &mut Calls, |
|
| 167 | + | store: &mut domains::Store, |
|
| 168 | + | context: abi::Ref, |
|
| 169 | + | reservation: pages::Reservation |
|
| 170 | + | ) -> PageHeld { |
|
| 171 | + | match reservation { |
|
| 172 | + | case pages::Reservation::Held(allocation, access) => { |
|
| 173 | + | assert store.contexts[context.index].owner == allocation.owner; |
|
| 174 | + | return PageHeld::Held( |
|
| 175 | + | hold(calls, store, context, Allocation::Page(allocation)), access |
|
| 176 | + | ); |
|
| 177 | + | } |
|
| 178 | + | } |
|
| 179 | + | } |
|
| 180 | + | ||
| 181 | + | /// Transfer an unpublished context into its caller's retained storage. |
|
| 182 | + | /// The token goes to the continuation, and only the context record is retained. |
|
| 183 | + | export fn holdContext( |
|
| 184 | + | calls: &mut Calls, |
|
| 185 | + | store: &mut domains::Store, |
|
| 186 | + | context: abi::Ref, |
|
| 187 | + | reservation: domains::ContextReservation |
|
| 188 | + | ) -> ContextHeld { |
|
| 189 | + | match reservation { |
|
| 190 | + | case domains::ContextReservation::Held(allocation, access) => |
|
| 191 | + | return ContextHeld::Held( |
|
| 192 | + | hold(calls, store, context, Allocation::Context(allocation)), access |
|
| 193 | + | ), |
|
| 194 | + | } |
|
| 195 | + | } |
|
| 196 | + | ||
| 197 | + | /// Retain unpublished domain storage under its reserved domain generation. |
|
| 198 | + | /// The tokens go to the continuation, and only the creation record is retained. |
|
| 199 | + | export fn holdDomain( |
|
| 200 | + | calls: &mut Calls, |
|
| 201 | + | store: &mut domains::Store, |
|
| 202 | + | context: abi::Ref, |
|
| 203 | + | reservation: domains::Reservation |
|
| 204 | + | ) -> DomainHeld { |
|
| 205 | + | match reservation { |
|
| 206 | + | case domains::Reservation::Held(creation, access) => { |
|
| 207 | + | assert slots::matches(&store.slots[..], creation.domain, slots::State::Reserved); |
|
| 208 | + | assert store.contexts[context.index].owner == creation.owner; |
|
| 209 | + | set calls.creations[creation.domain.index] = creation; |
|
| 210 | + | return DomainHeld::Held( |
|
| 211 | + | hold(calls, store, context, Allocation::Domain( |
|
| 212 | + | creation.domain |
|
| 213 | + | )), |
|
| 214 | + | access |
|
| 215 | + | ); |
|
| 216 | + | } |
|
| 217 | + | } |
|
| 218 | + | } |
|
| 219 | + | ||
| 220 | + | /// Read the private payload for an exact reserved domain generation. |
|
| 221 | + | fn creation(calls: &Calls, object: abi::Ref) -> domains::Creation { |
|
| 222 | + | assert object.index < limits::DOMAINS; |
|
| 223 | + | assert calls.creations[object.index].domain == object; |
|
| 224 | + | return calls.creations[object.index]; |
|
| 225 | + | } |
|
| 226 | + | ||
| 227 | + | /// Copy the creation record that a lease retains, while the metadata lock is held. |
|
| 228 | + | /// It holds no token. Initialization outside the lock works from this copy and the |
|
| 229 | + | /// continuation's tokens, and never reads the call records. |
|
| 230 | + | export fn domainCreation(calls: &Calls, lease: &Lease) -> domains::Creation { |
|
| 231 | + | match lease { |
|
| 232 | + | case Lease::Held(context) => match held(calls, context.context) { |
|
| 233 | + | case Allocation::Domain(object) => return creation(calls, object), |
|
| 234 | + | else => panic "domainCreation: domain reservation required", |
|
| 235 | + | }, |
|
| 236 | + | } |
|
| 237 | + | } |
|
| 238 | + | ||
| 239 | + | /// Retain private loader input and pin its caller's kernel stack. |
|
| 240 | + | /// The tokens go to the continuation, and only the input record is retained. |
|
| 241 | + | export fn holdLoad( |
|
| 242 | + | calls: &mut Calls, |
|
| 243 | + | store: &mut domains::Store, |
|
| 244 | + | context: abi::Ref, |
|
| 245 | + | reservation: loader::Reservation |
|
| 246 | + | ) -> LoadHeld { |
|
| 247 | + | match reservation { |
|
| 248 | + | case loader::Reservation::Held(input, access) => { |
|
| 249 | + | assert store.contexts[context.index].owner == loader::owner(&input); |
|
| 250 | + | let lease = hold(calls, store, context, Allocation::Load(Loading { |
|
| 251 | + | input, output: nil |
|
| 252 | + | })); |
|
| 253 | + | return LoadHeld::Held(lease, LoadAccess::Input(access)); |
|
| 254 | + | } |
|
| 255 | + | } |
|
| 256 | + | } |
|
| 257 | + | ||
| 258 | + | /// Read the private loader allocations through their caller's lease. |
|
| 259 | + | export fn load(calls: &Calls, lease: &Lease) -> Loading { |
|
| 260 | + | match lease { |
|
| 261 | + | case Lease::Held(context) => match held(calls, context.context) { |
|
| 262 | + | case Allocation::Load(loading) => return loading, |
|
| 263 | + | else => panic "load: loader reservation required", |
|
| 264 | + | }, |
|
| 265 | + | } |
|
| 266 | + | } |
|
| 267 | + | ||
| 268 | + | /// Decode the private loader input held by a lease. |
|
| 269 | + | /// `loading` is the lease's allocation, copied under the lock with `load`, and |
|
| 270 | + | /// `access` its tokens. The lease keeps the workspace allocated and exclusive |
|
| 271 | + | /// until `cancelLoad` or `finishLoad` takes it. |
|
| 272 | + | export fn decodeLoad(loading: &Loading, access: &LoadAccess, lease: &Lease) -> binary::Package |
|
| 273 | + | throws (abi::Error) |
|
| 274 | + | { |
|
| 275 | + | match access { |
|
| 276 | + | case LoadAccess::Input(input) => return try loader::decodeInput(&loading.input, input), |
|
| 277 | + | case LoadAccess::Output(input, output) => |
|
| 278 | + | return try loader::decodeInput( |
|
| 279 | + | &loading.input, input |
|
| 280 | + | ), |
|
| 281 | + | } |
|
| 282 | + | } |
|
| 283 | + | ||
| 284 | + | /// Find the resident package that matches decoded loader input, if any. |
|
| 285 | + | /// `packages` was captured under the lock; see `decodeLoad` for `loading` and `lease`. |
|
| 286 | + | export fn identifyLoad( |
|
| 287 | + | packages: ®istry::Published, |
|
| 288 | + | loading: &Loading, |
|
| 289 | + | access: &LoadAccess, |
|
| 290 | + | lease: &Lease, |
|
| 291 | + | package: &binary::Package |
|
| 292 | + | ) -> ?abi::Ref throws (abi::Error) { |
|
| 293 | + | match access { |
|
| 294 | + | case LoadAccess::Input(input) => |
|
| 295 | + | return try loader::identifyInput( |
|
| 296 | + | packages, &loading.input, input, package |
|
| 297 | + | ), |
|
| 298 | + | case LoadAccess::Output(input, output) => |
|
| 299 | + | return try loader::identifyInput(packages, &loading.input, input, package), |
|
| 300 | + | } |
|
| 301 | + | } |
|
| 302 | + | ||
| 303 | + | /// Generate native code for decoded loader input into the lease's reserved output. |
|
| 304 | + | /// `loading` must be copied under the lock after `reserveOutput`, and `packages` |
|
| 305 | + | /// captured after it; see `decodeLoad` for `lease`. |
|
| 306 | + | export fn compileLoad( |
|
| 307 | + | packages: ®istry::Published, |
|
| 308 | + | loading: &Loading, |
|
| 309 | + | access: &LoadAccess, |
|
| 310 | + | lease: &Lease, |
|
| 311 | + | package: &binary::Package |
|
| 312 | + | ) -> loader::Compiled throws (abi::Error) { |
|
| 313 | + | let output = loading.output else panic "compileLoad: missing loader output"; |
|
| 314 | + | match access { |
|
| 315 | + | case LoadAccess::Output(input, tokens) => |
|
| 316 | + | return try loader::compileInput( |
|
| 317 | + | packages, &loading.input, input, package, &output, tokens |
|
| 318 | + | ), |
|
| 319 | + | case LoadAccess::Input(input) => panic "compileLoad: missing output token", |
|
| 320 | + | } |
|
| 321 | + | } |
|
| 322 | + | ||
| 323 | + | /// Read the domain that a lease's unpublished context will belong to. |
|
| 324 | + | export fn contextTarget(calls: &Calls, lease: &Lease) -> abi::Ref { |
|
| 325 | + | match lease { |
|
| 326 | + | case Lease::Held(context) => match held(calls, context.context) { |
|
| 327 | + | case Allocation::Context(allocation) => return allocation.owner, |
|
| 328 | + | else => panic "contextTarget: context reservation required", |
|
| 329 | + | }, |
|
| 330 | + | } |
|
| 331 | + | } |
|
| 332 | + | ||
| 333 | + | /// Attach reserved native output to its caller before compilation can start. |
|
| 334 | + | /// Only the output record is retained. The output tokens go to the continuation, |
|
| 335 | + | /// which joins them to its own with `withOutput`. |
|
| 336 | + | export fn reserveOutput( |
|
| 337 | + | calls: &mut Calls, lease: &Lease, memory: &mut pages::Store, packages: &mut registry::Store |
|
| 338 | + | ) -> loader::OutputAccess throws (abi::Error) { |
|
| 339 | + | let mut loading = load(calls, lease); |
|
| 340 | + | assert loading.output == nil; |
|
| 341 | + | let pending = try loader::reserveOutput(&mut memory.backings.pool, packages); |
|
| 342 | + | match pending { |
|
| 343 | + | case loader::OutputReservation::Held(output, tokens) => { |
|
| 344 | + | set loading.output = output; |
|
| 345 | + | match lease { |
|
| 346 | + | case Lease::Held(context) => { |
|
| 347 | + | set calls.records[context.context.index].allocation = Allocation::Load(loading); |
|
| 348 | + | } |
|
| 349 | + | } |
|
| 350 | + | return tokens; |
|
| 351 | + | } |
|
| 352 | + | } |
|
| 353 | + | } |
|
| 354 | + | ||
| 355 | + | /// Return all unpublished loader capacity under the metadata lock. |
|
| 356 | + | fn discardLoad( |
|
| 357 | + | memory: &mut pages::Store, |
|
| 358 | + | state: &mut loader::State, |
|
| 359 | + | packages: &mut registry::Store, |
|
| 360 | + | table: &mut capability::Table, |
|
| 361 | + | loading: Loading, |
|
| 362 | + | release: fn(&mut frames::Pool, frames::Run) throws (abi::Error) |
|
| 363 | + | ) { |
|
| 364 | + | if let output = loading.output { |
|
| 365 | + | loader::cancelOutputUsing(&mut memory.backings.pool, packages, output, release); |
|
| 366 | + | } |
|
| 367 | + | loader::cancelUsing(state, memory, table, loading.input, release); |
|
| 368 | + | } |
|
| 369 | + | ||
| 370 | + | /// Check that a load's tokens pair with its retained record, input and output alike. |
|
| 371 | + | fn loadCovers(loading: &Loading, access: &LoadAccess) -> bool { |
|
| 372 | + | match access { |
|
| 373 | + | case LoadAccess::Input(input) => |
|
| 374 | + | return loading.output == nil |
|
| 375 | + | and loader::covers(&loading.input, input), |
|
| 376 | + | case LoadAccess::Output(input, tokens) => { |
|
| 377 | + | let output = loading.output else { |
|
| 378 | + | return false; |
|
| 379 | + | }; |
|
| 380 | + | return loader::covers(&loading.input, input) and loader::outputCovers(&output, tokens); |
|
| 381 | + | } |
|
| 382 | + | } |
|
| 383 | + | } |
|
| 384 | + | ||
| 385 | + | /// Cancel a live continuation's load and release its stack pin under the lock. |
|
| 386 | + | /// `table` is the loading domain's own table. The continuation returns its tokens. |
|
| 387 | + | export fn cancelLoad( |
|
| 388 | + | calls: &mut Calls, |
|
| 389 | + | store: &mut domains::Store, |
|
| 390 | + | memory: &mut pages::Store, |
|
| 391 | + | state: &mut loader::State, |
|
| 392 | + | packages: &mut registry::Store, |
|
| 393 | + | table: &mut capability::Table, |
|
| 394 | + | lease: Lease, |
|
| 395 | + | access: LoadAccess |
|
| 396 | + | ) { |
|
| 397 | + | match take(calls, store, lease) { |
|
| 398 | + | case Allocation::Load(loading) => { |
|
| 399 | + | assert loadCovers(&loading, &access); |
|
| 400 | + | assert table.owner == loader::owner(&loading.input); |
|
| 401 | + | discardLoad(memory, state, packages, table, loading, frames::release); |
|
| 402 | + | } |
|
| 403 | + | else => panic "cancelLoad: loader reservation required", |
|
| 404 | + | } |
|
| 405 | + | } |
|
| 406 | + | ||
| 407 | + | /// Publish successful loader work and release caller-owned temporary storage. |
|
| 408 | + | /// `table` is the loading domain's own table. The result returns the continuation's tokens. |
|
| 409 | + | export fn finishLoad( |
|
| 410 | + | calls: &mut Calls, |
|
| 411 | + | store: &mut domains::Store, |
|
| 412 | + | memory: &mut pages::Store, |
|
| 413 | + | state: &mut loader::State, |
|
| 414 | + | packages: &mut registry::Store, |
|
| 415 | + | table: &mut capability::Table, |
|
| 416 | + | lease: Lease, |
|
| 417 | + | result: LoadResult |
|
| 418 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 419 | + | match take(calls, store, lease) { |
|
| 420 | + | case Allocation::Load(loading) => { |
|
| 421 | + | assert table.owner == loader::owner(&loading.input); |
|
| 422 | + | let owner = try! domains::get(store, loader::owner(&loading.input)); |
|
| 423 | + | let dead = owner.state == domains::Lifecycle::Dead; |
|
| 424 | + | match result { |
|
| 425 | + | case LoadResult::Existing(existing, access) => { |
|
| 426 | + | assert loadCovers(&loading, &access); |
|
| 427 | + | if dead { |
|
| 428 | + | discardLoad(memory, state, packages, table, loading, frames::release); |
|
| 429 | + | throw abi::Error::BadHandle; |
|
| 430 | + | } |
|
| 431 | + | match access { |
|
| 432 | + | case LoadAccess::Input(input) => |
|
| 433 | + | return try loader::finish( |
|
| 434 | + | state, |
|
| 435 | + | memory, |
|
| 436 | + | table, |
|
| 437 | + | loader::Reservation::Held(loading.input, input), |
|
| 438 | + | existing |
|
| 439 | + | ), |
|
| 440 | + | case LoadAccess::Output( |
|
| 441 | + | input, tokens |
|
| 442 | + | ) => |
|
| 443 | + | panic "finishLoad: unexpected output", |
|
| 444 | + | } |
|
| 445 | + | } |
|
| 446 | + | case LoadResult::Compiled(compiled, access) => { |
|
| 447 | + | assert loadCovers(&loading, &access); |
|
| 448 | + | if dead { |
|
| 449 | + | discardLoad(memory, state, packages, table, loading, frames::release); |
|
| 450 | + | throw abi::Error::BadHandle; |
|
| 451 | + | } |
|
| 452 | + | let output = loading.output else panic "finishLoad: missing loader output"; |
|
| 453 | + | match access { |
|
| 454 | + | case LoadAccess::Output(input, tokens) => { |
|
| 455 | + | let object = loader::publishOutput( |
|
| 456 | + | state, |
|
| 457 | + | &mut memory.backings.pool, |
|
| 458 | + | packages, |
|
| 459 | + | loader::OutputReservation::Held(output, tokens), |
|
| 460 | + | compiled |
|
| 461 | + | ); |
|
| 462 | + | return try loader::finish( |
|
| 463 | + | state, |
|
| 464 | + | memory, |
|
| 465 | + | table, |
|
| 466 | + | loader::Reservation::Held(loading.input, input), |
|
| 467 | + | object |
|
| 468 | + | ); |
|
| 469 | + | } |
|
| 470 | + | case LoadAccess::Input(input) => panic "finishLoad: missing output token", |
|
| 471 | + | } |
|
| 472 | + | } |
|
| 473 | + | } |
|
| 474 | + | } |
|
| 475 | + | else => panic "finishLoad: loader reservation required", |
|
| 476 | + | } |
|
| 477 | + | } |
|
| 478 | + | ||
| 479 | + | /// Read retained storage through its exact caller generation. |
|
| 480 | + | fn held(calls: &Calls, context: abi::Ref) -> Allocation { |
|
| 481 | + | assert context.index < limits::CONTEXTS; |
|
| 482 | + | assert (calls.active[context.index / 32] & (1 << (context.index % 32))) <> 0; |
|
| 483 | + | assert calls.records[context.index].context == context; |
|
| 484 | + | return calls.records[context.index].allocation; |
|
| 485 | + | } |
|
| 486 | + | ||
| 487 | + | /// Remove retained ownership before serialized publication or cancellation. |
|
| 488 | + | fn take(calls: &mut Calls, store: &mut domains::Store, lease: Lease) -> Allocation { |
|
| 489 | + | match lease { |
|
| 490 | + | case Lease::Held(claim) => { |
|
| 491 | + | let context = claim.context; |
|
| 492 | + | let allocation = held(calls, context); |
|
| 493 | + | set calls.active[context.index / 32] &= ~(1 << (context.index % 32)); |
|
| 494 | + | assert slots::matches(&store.contextSlots[..], context, slots::State::Live); |
|
| 495 | + | set store.contexts[context.index].reservation = false; |
|
| 496 | + | return allocation; |
|
| 497 | + | } |
|
| 498 | + | } |
|
| 499 | + | } |
|
| 500 | + | ||
| 501 | + | /// Return a page reservation for publication or cancellation under the lock. |
|
| 502 | + | /// The continuation returns its token, which must cover the retained page. |
|
| 503 | + | export fn takePage( |
|
| 504 | + | calls: &mut Calls, store: &mut domains::Store, lease: Lease, access: physical::Access |
|
| 505 | + | ) -> pages::Reservation { |
|
| 506 | + | match take(calls, store, lease) { |
|
| 507 | + | case Allocation::Page(allocation) => { |
|
| 508 | + | assert pages::covers(&allocation, &access); |
|
| 509 | + | return pages::Reservation::Held(allocation, access); |
|
| 510 | + | } |
|
| 511 | + | else => panic "takePage: page reservation required", |
|
| 512 | + | } |
|
| 513 | + | } |
|
| 514 | + | ||
| 515 | + | /// Return a context reservation for publication or cancellation under the lock. |
|
| 516 | + | /// The continuation returns its token, which must cover the retained stack. |
|
| 517 | + | export fn takeContext( |
|
| 518 | + | calls: &mut Calls, store: &mut domains::Store, lease: Lease, access: physical::Access |
|
| 519 | + | ) -> domains::ContextReservation { |
|
| 520 | + | match take(calls, store, lease) { |
|
| 521 | + | case Allocation::Context(allocation) => { |
|
| 522 | + | assert domains::contextCovers(&allocation, &access); |
|
| 523 | + | return domains::ContextReservation::Held(allocation, access); |
|
| 524 | + | } |
|
| 525 | + | else => panic "takeContext: context reservation required", |
|
| 526 | + | } |
|
| 527 | + | } |
|
| 528 | + | ||
| 529 | + | /// Return domain storage for publication or cancellation under the lock. |
|
| 530 | + | /// The continuation returns its tokens, which must cover the retained creation. |
|
| 531 | + | export fn takeDomain( |
|
| 532 | + | calls: &mut Calls, store: &mut domains::Store, lease: Lease, access: domains::CreationAccess |
|
| 533 | + | ) -> domains::Reservation { |
|
| 534 | + | match take(calls, store, lease) { |
|
| 535 | + | case Allocation::Domain(object) => { |
|
| 536 | + | let retained = creation(calls, object); |
|
| 537 | + | assert domains::covers(&retained, &access); |
|
| 538 | + | return domains::Reservation::Held(retained, access); |
|
| 539 | + | } |
|
| 540 | + | else => panic "takeDomain: domain reservation required", |
|
| 541 | + | } |
|
| 542 | + | } |
|
| 543 | + | ||
| 544 | + | /// Retire one dead caller's private storage after execution ownership ends. |
|
| 545 | + | export fn cancelAllocations( |
|
| 546 | + | calls: &mut Calls, |
|
| 547 | + | store: &mut domains::Store, |
|
| 548 | + | memory: &mut pages::Store, |
|
| 549 | + | state: &mut loader::State, |
|
| 550 | + | packages: &mut registry::Store |
|
| 551 | + | ) -> bool { |
|
| 552 | + | for word in 0..CONTEXT_WORDS { |
|
| 553 | + | if calls.active[word] == 0 { |
|
| 554 | + | continue; |
|
| 555 | + | } |
|
| 556 | + | for offset in 0..32 as u32 { |
|
| 557 | + | let bit: u32 = 1 << offset; |
|
| 558 | + | if (calls.active[word] & bit) == 0 { |
|
| 559 | + | continue; |
|
| 560 | + | } |
|
| 561 | + | let index = word * 32 + offset; |
|
| 562 | + | let retained = calls.records[index]; |
|
| 563 | + | let context = try domains::context(store, retained.owner, retained.context) catch { |
|
| 564 | + | continue; |
|
| 565 | + | }; |
|
| 566 | + | if context.hart <> nil or context.state <> domains::ContextState::Stopped { |
|
| 567 | + | continue; |
|
| 568 | + | } |
|
| 569 | + | let owner = try! domains::get(store, retained.owner); |
|
| 570 | + | if owner.state <> domains::Lifecycle::Dead { |
|
| 571 | + | continue; |
|
| 572 | + | } |
|
| 573 | + | set calls.active[word] &= ~bit; |
|
| 574 | + | // The retained reservation pinned the dead owner's context, so its storage is not |
|
| 575 | + | // reclaimed. |
|
| 576 | + | unsafe { |
|
| 577 | + | match retained.allocation { |
|
| 578 | + | case Allocation::Load(loading) => |
|
| 579 | + | discardLoad( |
|
| 580 | + | memory, |
|
| 581 | + | state, |
|
| 582 | + | packages, |
|
| 583 | + | &mut owner.memory.table, |
|
| 584 | + | loading, |
|
| 585 | + | frames::retire |
|
| 586 | + | ), |
|
| 587 | + | case Allocation::Page(allocation) => |
|
| 588 | + | pages::cancelUsing( |
|
| 589 | + | memory, &mut owner.memory.table, allocation, frames::retire |
|
| 590 | + | ), |
|
| 591 | + | case Allocation::Context(allocation) => |
|
| 592 | + | domains::contextCancelUsing( |
|
| 593 | + | store, memory, allocation, frames::retire |
|
| 594 | + | ), |
|
| 595 | + | case Allocation::Domain(object) => |
|
| 596 | + | domains::cancelUsing( |
|
| 597 | + | store, |
|
| 598 | + | &mut memory.backings, |
|
| 599 | + | &mut owner.memory.table, |
|
| 600 | + | creation(calls, object), |
|
| 601 | + | frames::retire |
|
| 602 | + | ), |
|
| 603 | + | } |
|
| 604 | + | } |
|
| 605 | + | set store.contexts[index].reservation = false; |
|
| 606 | + | return true; |
|
| 607 | + | } |
|
| 608 | + | } |
|
| 609 | + | return false; |
|
| 610 | + | } |
|
| 611 | + | ||
| 612 | + | /// Release stopped callers' remote work after hart ownership ends. |
|
| 613 | + | /// Taken requests keep the context pinned until the target acknowledges them. |
|
| 614 | + | export fn cancelRequests(store: &mut domains::Store, requests: &mut remote::Store) { |
|
| 615 | + | let mut touched: [u32; CONTEXT_WORDS] = [0; CONTEXT_WORDS]; |
|
| 616 | + | let mut retained: [u32; CONTEXT_WORDS] = [0; CONTEXT_WORDS]; |
|
| 617 | + | let mut cancelled = false; |
|
| 618 | + | for i in 0..limits::REMOTE_REQUESTS { |
|
| 619 | + | if requests.slots[i].state <> slots::State::Live { |
|
| 620 | + | continue; |
|
| 621 | + | } |
|
| 622 | + | let request = requests.requests[i]; |
|
| 623 | + | let mut caller: ?abi::Ref = nil; |
|
| 624 | + | match request.action { |
|
| 625 | + | case remote::Action::Budget(call) => { |
|
| 626 | + | set caller = call.context; |
|
| 627 | + | } |
|
| 628 | + | case remote::Action::Ready(context) => { |
|
| 629 | + | set caller = context; |
|
| 630 | + | } |
|
| 631 | + | else => continue, |
|
| 632 | + | } |
|
| 633 | + | let object = caller else { |
|
| 634 | + | continue; |
|
| 635 | + | }; |
|
| 636 | + | if not slots::matches(&store.contextSlots[..], object, slots::State::Live) { |
|
| 637 | + | continue; |
|
| 638 | + | } |
|
| 639 | + | if store.contexts[object.index].hart <> nil |
|
| 640 | + | or store.contexts[object.index].state <> domains::ContextState::Stopped |
|
| 641 | + | { |
|
| 642 | + | continue; |
|
| 643 | + | } |
|
| 644 | + | if not slots::matches( |
|
| 645 | + | &store.slots[..], store.contexts[object.index].owner, slots::State::Live |
|
| 646 | + | ) |
|
| 647 | + | or store.records[store.contexts[object.index].owner.index].state |
|
| 648 | + | <> domains::Lifecycle::Dead |
|
| 649 | + | { |
|
| 650 | + | continue; |
|
| 651 | + | } |
|
| 652 | + | let word = object.index / 32; |
|
| 653 | + | let bit = 1 << (object.index % 32); |
|
| 654 | + | set touched[word] |= bit; |
|
| 655 | + | if request.state == remote::State::Delivered { |
|
| 656 | + | set retained[word] |= bit; |
|
| 657 | + | set store.contexts[object.index].pending = abi::Ref { |
|
| 658 | + | index: i, generation: requests.slots[i].generation |
|
| 659 | + | }; |
|
| 660 | + | continue; |
|
| 661 | + | } |
|
| 662 | + | try! remote::cancel(requests, request.source, abi::Ref { |
|
| 663 | + | index: i, generation: requests.slots[i].generation |
|
| 664 | + | }); |
|
| 665 | + | set cancelled = true; |
|
| 666 | + | } |
|
| 667 | + | if not cancelled { |
|
| 668 | + | return; |
|
| 669 | + | } |
|
| 670 | + | for i in 0..limits::CONTEXTS { |
|
| 671 | + | let bit = 1 << (i % 32); |
|
| 672 | + | if (touched[i / 32] & bit) <> 0 and (retained[i / 32] & bit) == 0 { |
|
| 673 | + | set store.contexts[i].pending = nil; |
|
| 674 | + | } |
|
| 675 | + | } |
|
| 676 | + | } |
|
| 677 | + | ||
| 678 | + | /// Advance one eligible domain from a rotating slot cursor. |
|
| 679 | + | export fn reapNext( |
|
| 680 | + | store: &mut domains::Store, |
|
| 681 | + | memory: &mut pages::Store, |
|
| 682 | + | windows: &mut budgets::Store, |
|
| 683 | + | irqs: &mut interrupts::Store |
|
| 684 | + | ) -> bool { |
|
| 685 | + | if store.dead == 0 { |
|
| 686 | + | return false; |
|
| 687 | + | } |
|
| 688 | + | let mut checked = false; |
|
| 689 | + | let mut blocked: [bool; limits::DOMAINS] = [false; limits::DOMAINS]; |
|
| 690 | + | for offset in 0..limits::DOMAINS { |
|
| 691 | + | let index = (store.reapCursor + offset) % limits::DOMAINS; |
|
| 692 | + | if store.slots[index].state <> slots::State::Live |
|
| 693 | + | or store.records[index].state <> domains::Lifecycle::Dead |
|
| 694 | + | { |
|
| 695 | + | continue; |
|
| 696 | + | } |
|
| 697 | + | let owner = abi::Ref { index, generation: store.slots[index].generation }; |
|
| 698 | + | if store.teardown[index] == domains::Teardown::Retired { |
|
| 699 | + | if not try! events::acknowledged(&store.events, owner) { |
|
| 700 | + | continue; |
|
| 701 | + | } |
|
| 702 | + | } else if store.teardown[index] == domains::Teardown::Live { |
|
| 703 | + | if not checked { |
|
| 704 | + | for i in 0..limits::CONTEXTS { |
|
| 705 | + | let state = store.contextSlots[i].state; |
|
| 706 | + | if state <> slots::State::Live and state <> slots::State::Reserved { |
|
| 707 | + | continue; |
|
| 708 | + | } |
|
| 709 | + | if state == slots::State::Reserved |
|
| 710 | + | or store.contexts[i].state <> domains::ContextState::Stopped |
|
| 711 | + | or store.contexts[i].hart <> nil |
|
| 712 | + | or store.contexts[i].pending <> nil |
|
| 713 | + | or store.contexts[i].reservation |
|
| 714 | + | { |
|
| 715 | + | set blocked[store.contexts[i].owner.index] = true; |
|
| 716 | + | } |
|
| 717 | + | } |
|
| 718 | + | set checked = true; |
|
| 719 | + | } |
|
| 720 | + | if blocked[index] { |
|
| 721 | + | continue; |
|
| 722 | + | } |
|
| 723 | + | } |
|
| 724 | + | let mut done = false; |
|
| 725 | + | if store.teardown[index] == domains::Teardown::Retired { |
|
| 726 | + | set done = try! reapIdentity(store, owner); |
|
| 727 | + | } else { |
|
| 728 | + | let domain = try! domains::get(store, owner); |
|
| 729 | + | // The domain is dead and not reclaimed, so its storage is mapped. The |
|
| 730 | + | // caller holds the metadata lock and nothing else borrows this table. |
|
| 731 | + | unsafe { |
|
| 732 | + | set done = try! reap(store, memory, windows, irqs, owner, &mut domain.memory.table); |
|
| 733 | + | } |
|
| 734 | + | } |
|
| 735 | + | if done { |
|
| 736 | + | set store.reapCursor = (index + 1) % limits::DOMAINS; |
|
| 737 | + | return true; |
|
| 738 | + | } |
|
| 739 | + | } |
|
| 740 | + | return false; |
|
| 741 | + | } |
|
| 742 | + | ||
| 743 | + | /// Release a reclaimed domain's identity once its terminal event is acknowledged. |
|
| 744 | + | /// Returns false while acknowledgement is pending. |
|
| 745 | + | export fn reapIdentity(store: &mut domains::Store, owner: abi::Ref) -> bool throws (abi::Error) { |
|
| 746 | + | if not slots::matches(&store.slots[..], owner, slots::State::Live) { |
|
| 747 | + | throw abi::Error::BadHandle; |
|
| 748 | + | } |
|
| 749 | + | assert store.teardown[owner.index] == domains::Teardown::Retired; |
|
| 750 | + | if not try events::acknowledged(&store.events, owner) { |
|
| 751 | + | return false; |
|
| 752 | + | } |
|
| 753 | + | try! domains::releaseIdentity(store, owner); |
|
| 754 | + | return true; |
|
| 755 | + | } |
|
| 756 | + | ||
| 757 | + | /// Advance one dead domain's resource teardown under the metadata lock. |
|
| 758 | + | /// `table` is the domain's own table; its storage must not be reclaimed. |
|
| 759 | + | /// Returns false while execution ownership is pending. |
|
| 760 | + | export fn reap( |
|
| 761 | + | store: &mut domains::Store, |
|
| 762 | + | memory: &mut pages::Store, |
|
| 763 | + | windows: &mut budgets::Store, |
|
| 764 | + | irqs: &mut interrupts::Store, |
|
| 765 | + | owner: abi::Ref, |
|
| 766 | + | table: &mut capability::Table |
|
| 767 | + | ) -> bool |
|
| 768 | + | throws (abi::Error) |
|
| 769 | + | { |
|
| 770 | + | if not slots::matches(&store.slots[..], owner, slots::State::Live) { |
|
| 771 | + | throw abi::Error::BadHandle; |
|
| 772 | + | } |
|
| 773 | + | assert table.owner == owner and store.teardown[owner.index] <> domains::Teardown::Retired; |
|
| 774 | + | if store.teardown[owner.index] == domains::Teardown::Live { |
|
| 775 | + | if not try domains::quiescent(store, owner) { |
|
| 776 | + | return false; |
|
| 777 | + | } |
|
| 778 | + | set store.teardown[owner.index] = domains::Teardown::Reaping; |
|
| 779 | + | } |
|
| 780 | + | // `retireStorage` runs only after this loop finds no handles. |
|
| 781 | + | for i in 0..limits::HANDLES { |
|
| 782 | + | let slot = table.slots[i]; |
|
| 783 | + | if slot.state == slots::State::Reserved { |
|
| 784 | + | return false; |
|
| 785 | + | } |
|
| 786 | + | if slot.state <> slots::State::Live { |
|
| 787 | + | continue; |
|
| 788 | + | } |
|
| 789 | + | let entry = table.entries[i]; |
|
| 790 | + | let handle = try! abi::handle(entry.kind, abi::Ref { |
|
| 791 | + | index: i, generation: slot.generation |
|
| 792 | + | }); |
|
| 793 | + | match entry.kind { |
|
| 794 | + | case abi::Kind::Page => try! pages::drop(memory, table, handle), |
|
| 795 | + | case abi::Kind::Budget => try! budgets::drop(windows, table, handle), |
|
| 796 | + | case abi::Kind::Interrupt => { |
|
| 797 | + | let number = try! interrupts::query(irqs, &*table, handle); |
|
| 798 | + | plic::mask(number); |
|
| 799 | + | try! interrupts::drop(irqs, table, handle); |
|
| 800 | + | } |
|
| 801 | + | else => { |
|
| 802 | + | try! capability::invalidate(table, handle); |
|
| 803 | + | } |
|
| 804 | + | } |
|
| 805 | + | return true; |
|
| 806 | + | } |
|
| 807 | + | if memory.backings.domains[owner.index] <> 0 { |
|
| 808 | + | try! backing::endDomainStep(&mut memory.backings, owner, CLEANUP_BATCH); |
|
| 809 | + | return true; |
|
| 810 | + | } |
|
| 811 | + | if try! domains::retireContext(store, &mut memory.backings.pool, owner) { |
|
| 812 | + | return true; |
|
| 813 | + | } |
|
| 814 | + | try! domains::retireStorage(store, &mut memory.backings, owner, &*table); |
|
| 815 | + | return true; |
|
| 816 | + | } |
|
| 817 | + | ||
| 818 | + | /// Refresh terminal acknowledgements, publish pending events, and report receiver harts. |
|
| 819 | + | /// The caller holds the metadata lock throughout service. |
|
| 820 | + | export fn service(store: &mut domains::Store, windows: &budgets::Store, now: u64) -> u32 { |
|
| 821 | + | if store.events.pending == 0 and store.events.queued == 0 { |
|
| 822 | + | return 0; |
|
| 823 | + | } |
|
| 824 | + | let mut notified: [bool; limits::DOMAINS] = [false; limits::DOMAINS]; |
|
| 825 | + | let mut changed = false; |
|
| 826 | + | let mut faulted: u32 = 0; |
|
| 827 | + | for i in 0..limits::DOMAINS { |
|
| 828 | + | let state = store.events.terminals[i].state; |
|
| 829 | + | if state <> events::State::Pending and state <> events::State::Queued { |
|
| 830 | + | continue; |
|
| 831 | + | } |
|
| 832 | + | let terminal = store.events.terminals[i]; |
|
| 833 | + | let receiver = try domains::get(store, terminal.receiver) catch { |
|
| 834 | + | continue; |
|
| 835 | + | }; |
|
| 836 | + | if receiver.state == domains::Lifecycle::Dead { |
|
| 837 | + | continue; |
|
| 838 | + | } |
|
| 839 | + | if terminal.state == events::State::Queued { |
|
| 840 | + | try domains::serve(store, terminal.receiver, domains::RingRequest::Refresh) catch err { |
|
| 841 | + | if err == abi::Error::InvalidArg { |
|
| 842 | + | set faulted |= domains::ringFault(store, terminal.receiver); |
|
| 843 | + | } |
|
| 844 | + | }; |
|
| 845 | + | continue; |
|
| 846 | + | } |
|
| 847 | + | let delivered = try domains::serve(store, terminal.receiver, domains::RingRequest::Deliver( |
|
| 848 | + | terminal.subject |
|
| 849 | + | )) catch err { |
|
| 850 | + | // The receiver controls its ring, so a failed validation faults the receiver. |
|
| 851 | + | if err == abi::Error::InvalidArg { |
|
| 852 | + | set faulted |= domains::ringFault(store, terminal.receiver); |
|
| 853 | + | } |
|
| 854 | + | false; |
|
| 855 | + | }; |
|
| 856 | + | if delivered { |
|
| 857 | + | set notified[terminal.receiver.index] = true; |
|
| 858 | + | set changed = true; |
|
| 859 | + | } |
|
| 860 | + | } |
|
| 861 | + | if not changed { |
|
| 862 | + | return faulted; |
|
| 863 | + | } |
|
| 864 | + | for i in 0..limits::CONTEXTS { |
|
| 865 | + | if store.contextSlots[i].state <> slots::State::Live { |
|
| 866 | + | continue; |
|
| 867 | + | } |
|
| 868 | + | if notified[store.contexts[i].owner.index] |
|
| 869 | + | and store.contexts[i].state == domains::ContextState::Waiting |
|
| 870 | + | { |
|
| 871 | + | set store.contexts[i].state = domains::ContextState::Ready; |
|
| 872 | + | } |
|
| 873 | + | } |
|
| 874 | + | let mut harts: u32 = faulted; |
|
| 875 | + | for i in 0..limits::BUDGETS { |
|
| 876 | + | if windows.slots[i].state <> slots::State::Live { |
|
| 877 | + | continue; |
|
| 878 | + | } |
|
| 879 | + | let window = windows.windows[i]; |
|
| 880 | + | if not slots::matches(&store.slots[..], window.owner, slots::State::Live) |
|
| 881 | + | or not notified[window.owner.index] |
|
| 882 | + | or window.end <= now |
|
| 883 | + | { |
|
| 884 | + | continue; |
|
| 885 | + | } |
|
| 886 | + | let context = window.context else { |
|
| 887 | + | continue; |
|
| 888 | + | }; |
|
| 889 | + | if not slots::matches(&store.contextSlots[..], context, slots::State::Live) { |
|
| 890 | + | continue; |
|
| 891 | + | } |
|
| 892 | + | if store.contexts[context.index].owner == window.owner |
|
| 893 | + | and store.contexts[context.index].state <> domains::ContextState::Stopped |
|
| 894 | + | { |
|
| 895 | + | set harts |= 1 << window.hart; |
|
| 896 | + | } |
|
| 897 | + | } |
|
| 898 | + | return harts; |
|
| 899 | + | } |
kernel/kernel/limits.rad
added
+34 -0
| 1 | + | //! Fixed kernel storage profile. |
|
| 2 | + | ||
| 3 | + | /// Maximum resident domains, including retained tombstones. |
|
| 4 | + | export constant DOMAINS: u32 = 512; |
|
| 5 | + | /// Per-domain capability table capacity. |
|
| 6 | + | export constant HANDLES: u32 = 512; |
|
| 7 | + | /// Maximum physical frames tracked by this build. |
|
| 8 | + | export constant FRAMES: u32 = 65536; |
|
| 9 | + | /// Physical frame size in bytes. |
|
| 10 | + | export constant FRAME_SIZE: u64 = 4096; |
|
| 11 | + | /// Maximum page objects, including split allocation segments. |
|
| 12 | + | export constant PAGES: u32 = 65536; |
|
| 13 | + | /// Maximum backing allocation records. |
|
| 14 | + | export constant ALLOCATIONS: u32 = 65536; |
|
| 15 | + | /// Maximum interrupt objects. |
|
| 16 | + | export constant INTERRUPTS: u32 = 128; |
|
| 17 | + | /// Maximum device memory regions. |
|
| 18 | + | export constant DEVICES: u32 = 128; |
|
| 19 | + | /// Maximum budget objects. |
|
| 20 | + | export constant BUDGETS: u32 = 512; |
|
| 21 | + | /// Maximum execution contexts across all domains. |
|
| 22 | + | export constant CONTEXTS: u32 = 2048; |
|
| 23 | + | /// Maximum hardware threads. |
|
| 24 | + | export constant HARTS: u32 = 8; |
|
| 25 | + | /// One-shot timeouts retained by each hart, including pending delivery. |
|
| 26 | + | export constant TIMEOUTS_PER_HART: u32 = 64; |
|
| 27 | + | /// Outstanding cross-hart requests, including acknowledgements awaiting release. |
|
| 28 | + | export constant REMOTE_REQUESTS: u32 = 128; |
|
| 29 | + | /// Queued remote requests accepted by each hart. |
|
| 30 | + | export constant REMOTE_PER_HART: u32 = 16; |
|
| 31 | + | /// Maximum resident package records and gp table entries. |
|
| 32 | + | export constant PACKAGES: u32 = 256; |
|
| 33 | + | /// Generation values never wrap. |
|
| 34 | + | export constant LAST_GENERATION: u32 = 0xffffffff; |
kernel/kernel/loader.rad
added
+747 -0
| 1 | + | //! Bounded verified-RIL loading and unpublished physical-memory ownership. |
|
| 2 | + | ||
| 3 | + | use std::lang::alloc; |
|
| 4 | + | use std::lang::gen::data; |
|
| 5 | + | use std::lang::il::binary; |
|
| 6 | + | use std::lang::il::binary::program; |
|
| 7 | + | use std::lang::il::verify; |
|
| 8 | + | use std::arch::rv64; |
|
| 9 | + | use std::arch::rv64::shared; |
|
| 10 | + | use std::arch::rv64::shared::catalog; |
|
| 11 | + | use super::abi; |
|
| 12 | + | use super::limits; |
|
| 13 | + | use super::slots; |
|
| 14 | + | use super::frames; |
|
| 15 | + | use super::physical; |
|
| 16 | + | use super::pages; |
|
| 17 | + | use super::backing; |
|
| 18 | + | use super::capability; |
|
| 19 | + | use super::registry; |
|
| 20 | + | use super::sync; |
|
| 21 | + | use std::lang::gen::labels; |
|
| 22 | + | use std::lang::gen::regalloc::liveness; |
|
| 23 | + | ||
| 24 | + | /// Number of elements reserved for decoded storage. |
|
| 25 | + | constant DECODED_CAPACITY: u32 = 67108864; |
|
| 26 | + | /// Number of elements reserved for code storage. |
|
| 27 | + | constant CODE_CAPACITY: u32 = 16777216; |
|
| 28 | + | /// Number of elements reserved for scratch storage. |
|
| 29 | + | constant SCRATCH_CAPACITY: u32 = 16777216; |
|
| 30 | + | /// Number of elements reserved for data storage. |
|
| 31 | + | constant DATA_CAPACITY: u32 = 4096; |
|
| 32 | + | /// Number of elements reserved for symbols storage. |
|
| 33 | + | constant SYMBOLS_CAPACITY: u32 = 4096; |
|
| 34 | + | /// Number of elements reserved for exports storage. |
|
| 35 | + | constant EXPORTS_CAPACITY: u32 = 4096; |
|
| 36 | + | /// Number of elements reserved for imports storage. |
|
| 37 | + | constant IMPORTS_CAPACITY: u32 = 8192; |
|
| 38 | + | /// Number of elements reserved for template storage. |
|
| 39 | + | constant TEMPLATE_CAPACITY: u32 = 1048576; |
|
| 40 | + | /// Number of elements reserved for relocations storage. |
|
| 41 | + | constant RELOCATIONS_CAPACITY: u32 = 4096; |
|
| 42 | + | ||
| 43 | + | /// Maximum snapshotted binary RIL size. |
|
| 44 | + | export constant INPUT_BYTES: u32 = 8388608; |
|
| 45 | + | /// Maximum native output extent per load. |
|
| 46 | + | constant CODE_BYTES: u32 = 8388608; |
|
| 47 | + | /// Maximum persistent source and metadata extent per load. |
|
| 48 | + | constant METADATA_BYTES: u32 = 16777216; |
|
| 49 | + | ||
| 50 | + | /// Temporary compiler storage owned by one load. |
|
| 51 | + | export record Workspace: Copy { |
|
| 52 | + | /// Stable input bytes used throughout decoding and compilation. |
|
| 53 | + | input: [u8; INPUT_BYTES], |
|
| 54 | + | /// Decoded binary program and names. |
|
| 55 | + | decoded: [u8; DECODED_CAPACITY], |
|
| 56 | + | /// Native emitter state and generated instructions. |
|
| 57 | + | code: [u8; CODE_CAPACITY], |
|
| 58 | + | /// Reusable per-function storage, and the packed catalog once compilation ends. |
|
| 59 | + | scratch: [u8; SCRATCH_CAPACITY], |
|
| 60 | + | /// Native data layout workspace. |
|
| 61 | + | data: [data::DataSym; DATA_CAPACITY], |
|
| 62 | + | /// Local package symbols. |
|
| 63 | + | symbols: [shared::Symbol; SYMBOLS_CAPACITY], |
|
| 64 | + | /// Public package symbols. |
|
| 65 | + | exports: [shared::Symbol; EXPORTS_CAPACITY], |
|
| 66 | + | /// Admitted dependency symbols. |
|
| 67 | + | imports: [shared::Symbol; IMPORTS_CAPACITY], |
|
| 68 | + | /// Initialized private-state template. |
|
| 69 | + | template: [u8; TEMPLATE_CAPACITY], |
|
| 70 | + | /// Private-state pointer relocations. |
|
| 71 | + | relocations: [shared::Relocation; RELOCATIONS_CAPACITY], |
|
| 72 | + | } |
|
| 73 | + | ||
| 74 | + | /// Physical storage retained by one runtime-loaded package. |
|
| 75 | + | export record Resident: Copy { |
|
| 76 | + | /// Executable frames, including final-page padding. |
|
| 77 | + | code: frames::Run, |
|
| 78 | + | /// Packed catalog entry and retained source bytes. |
|
| 79 | + | metadata: frames::Run, |
|
| 80 | + | } |
|
| 81 | + | ||
| 82 | + | /// Single-loader ownership and persistent frame accounting. |
|
| 83 | + | export record State: Copy { |
|
| 84 | + | /// Domain generation holding the workspace, or nil while it is free. Protected |
|
| 85 | + | /// by the caller's metadata serialization. |
|
| 86 | + | owner: ?abi::Ref, |
|
| 87 | + | /// True for package slots whose frames are owned by this loader. |
|
| 88 | + | live: [bool; limits::PACKAGES], |
|
| 89 | + | /// Physical extents for live runtime-loaded packages. |
|
| 90 | + | resident: [Resident; limits::PACKAGES], |
|
| 91 | + | } |
|
| 92 | + | ||
| 93 | + | /// Arguments supplied to an image load. |
|
| 94 | + | export record Request: Copy { |
|
| 95 | + | /// Self-Domain allocation authority, including the zero sentinel. |
|
| 96 | + | authority: abi::Handle, |
|
| 97 | + | /// Readable page containing the binary RIL bytes. |
|
| 98 | + | source: abi::Handle, |
|
| 99 | + | /// Byte offset within the source page. |
|
| 100 | + | offset: u64, |
|
| 101 | + | /// Number of source bytes to snapshot. |
|
| 102 | + | length: u64, |
|
| 103 | + | } |
|
| 104 | + | ||
| 105 | + | /// Workspace capacity that must be returned to the same frame pool. |
|
| 106 | + | export union Lease: Once { |
|
| 107 | + | /// Private temporary frames owned by the active load. |
|
| 108 | + | Held(frames::Run), |
|
| 109 | + | } |
|
| 110 | + | ||
| 111 | + | /// Private input capacity retained throughout one load. |
|
| 112 | + | /// |
|
| 113 | + | /// Opaque, so only `reserve` builds one, from a freshly acquired lease. Its |
|
| 114 | + | /// `InputAccess` covers a complete Workspace, and the holder of the input's |
|
| 115 | + | /// reservation keeps those frames allocated and exclusive to this load until |
|
| 116 | + | /// `cancel` or `finish` consumes it. These facts justify every `view` of the |
|
| 117 | + | /// workspace below. The record holds no token, so teardown can release the |
|
| 118 | + | /// frames from it alone. |
|
| 119 | + | export opaque record Input: Copy { |
|
| 120 | + | /// Domain kept live while the source and reserved handle are in use. |
|
| 121 | + | owner: abi::Ref, |
|
| 122 | + | /// Reserved Image handle slot in the caller's table. |
|
| 123 | + | handle: abi::Ref, |
|
| 124 | + | /// Private workspace frames. |
|
| 125 | + | frames: frames::Run, |
|
| 126 | + | /// Physical start of the workspace frames. |
|
| 127 | + | base: u64, |
|
| 128 | + | /// Validated physical source address. |
|
| 129 | + | address: u64, |
|
| 130 | + | /// Number of source bytes to snapshot. |
|
| 131 | + | length: u32, |
|
| 132 | + | } |
|
| 133 | + | ||
| 134 | + | /// Proof that the input's workspace and source ranges are allocated. The holder is |
|
| 135 | + | /// the only user of the workspace until the load is cancelled or finished. |
|
| 136 | + | export union InputAccess { |
|
| 137 | + | /// The first token covers a complete Workspace at the start of the workspace |
|
| 138 | + | /// frames. The second covers the source bytes, taken under metadata serialization. |
|
| 139 | + | Held(physical::Access, physical::Access), |
|
| 140 | + | } |
|
| 141 | + | ||
| 142 | + | /// Input capacity that must be cancelled or completed under metadata serialization. |
|
| 143 | + | export union Reservation: Once { |
|
| 144 | + | /// One admitted load, its private compiler storage, and the tokens that prove it. |
|
| 145 | + | Held(Input, InputAccess), |
|
| 146 | + | } |
|
| 147 | + | ||
| 148 | + | /// Check that an input's tokens cover exactly its workspace and source ranges. |
|
| 149 | + | export fn covers(input: &Input, access: &InputAccess) -> bool { |
|
| 150 | + | match access { |
|
| 151 | + | case InputAccess::Held(work, source) => |
|
| 152 | + | return physical::covers(source, input.address, input.length as u64) |
|
| 153 | + | and physical::covers(work, input.base, @sizeOf(Workspace) as u64), |
|
| 154 | + | } |
|
| 155 | + | } |
|
| 156 | + | ||
| 157 | + | /// Map a validated physical workspace extent to its compiler storage. |
|
| 158 | + | /// The address must start a complete Workspace in frames that the caller keeps |
|
| 159 | + | /// allocated and exclusive until the last access through the pointer. The |
|
| 160 | + | /// compiler's arenas and storage take owned slices, so the pointer is owned |
|
| 161 | + | /// even though the frames are recycled after the load: it must not be kept |
|
| 162 | + | /// past the call that derived it. |
|
| 163 | + | unsafe fn view(address: u64) -> *mut Workspace; |
|
| 164 | + | /// Read the native descriptor at a successfully packed metadata extent. |
|
| 165 | + | /// The extent must hold the entry written by a successful pack. |
|
| 166 | + | unsafe fn descriptor(address: u64) -> *unsafe catalog::Entry; |
|
| 167 | + | ||
| 168 | + | /// Reset loader metadata before any workspace or resident allocation exists. |
|
| 169 | + | export fn initialize(state: &mut State) { |
|
| 170 | + | set state.owner = nil; |
|
| 171 | + | for i in 0..limits::PACKAGES { |
|
| 172 | + | set state.live[i] = false; |
|
| 173 | + | } |
|
| 174 | + | } |
|
| 175 | + | ||
| 176 | + | /// Acquire the sole compiler workspace and reserve its private physical storage. |
|
| 177 | + | /// The caller serializes workspace ownership and frame-pool mutations. |
|
| 178 | + | export fn acquire(state: &mut State, pool: &mut frames::Pool, owner: abi::Ref) -> Lease |
|
| 179 | + | throws (abi::Error) |
|
| 180 | + | { |
|
| 181 | + | if owner.index >= limits::DOMAINS or owner.generation == 0 { |
|
| 182 | + | throw abi::Error::BadHandle; |
|
| 183 | + | } |
|
| 184 | + | if state.owner <> nil { |
|
| 185 | + | throw abi::Error::Busy; |
|
| 186 | + | } |
|
| 187 | + | let count = (@sizeOf(Workspace) |
|
| 188 | + | + (limits::FRAME_SIZE as u32 - 1)) / limits::FRAME_SIZE as u32; |
|
| 189 | + | let reservation = try frames::reserve(pool, count); |
|
| 190 | + | set state.owner = owner; |
|
| 191 | + | return Lease::Held(frames::commit(reservation)); |
|
| 192 | + | } |
|
| 193 | + | ||
| 194 | + | /// Return temporary frames and release the compiler workspace under metadata serialization. |
|
| 195 | + | export fn release(state: &mut State, pool: &mut frames::Pool, lease: Lease) { |
|
| 196 | + | releaseUsing(state, pool, lease, frames::release); |
|
| 197 | + | } |
|
| 198 | + | ||
| 199 | + | /// Transfer temporary frames to their release operation and clear workspace ownership. |
|
| 200 | + | fn releaseUsing( |
|
| 201 | + | state: &mut State, |
|
| 202 | + | pool: &mut frames::Pool, |
|
| 203 | + | lease: Lease, |
|
| 204 | + | release: fn(&mut frames::Pool, frames::Run) throws (abi::Error) |
|
| 205 | + | ) { |
|
| 206 | + | match lease { |
|
| 207 | + | case Lease::Held(run) => { |
|
| 208 | + | try! release(pool, run); |
|
| 209 | + | } |
|
| 210 | + | } |
|
| 211 | + | set state.owner = nil; |
|
| 212 | + | } |
|
| 213 | + | ||
| 214 | + | /// Validate allocation authority and a readable source range before taking resources. |
|
| 215 | + | fn source(store: &pages::Store, table: &capability::Table, request: Request) -> u64 |
|
| 216 | + | throws (abi::Error) |
|
| 217 | + | { |
|
| 218 | + | let authority = try capability::authority(table, request.authority, abi::Rights(abi::ALLOCATE)); |
|
| 219 | + | if authority.object <> table.owner { |
|
| 220 | + | throw abi::Error::Denied; |
|
| 221 | + | } |
|
| 222 | + | if not backing::domainLive(&store.backings, table.owner) { |
|
| 223 | + | throw abi::Error::BadHandle; |
|
| 224 | + | } |
|
| 225 | + | let entry = try capability::lookup(table, request.source, abi::Kind::Page, abi::Rights( |
|
| 226 | + | abi::READ |
|
| 227 | + | )); |
|
| 228 | + | let page = try pages::get(store, entry.object); |
|
| 229 | + | let bytes = page.count as u64 * limits::FRAME_SIZE; |
|
| 230 | + | if request.length == 0 or request.offset > bytes or request.length > bytes - request.offset { |
|
| 231 | + | throw abi::Error::InvalidArg; |
|
| 232 | + | } |
|
| 233 | + | if request.length > INPUT_BYTES as u64 { |
|
| 234 | + | throw abi::Error::Exhausted; |
|
| 235 | + | } |
|
| 236 | + | return page.base + request.offset; |
|
| 237 | + | } |
|
| 238 | + | ||
| 239 | + | /// Translate bounded backend storage failures. The verifier has already admitted the IL. |
|
| 240 | + | fn backend(error: shared::Error) -> abi::Error { |
|
| 241 | + | match error { |
|
| 242 | + | case shared::Error::Capacity => return abi::Error::Exhausted, |
|
| 243 | + | case shared::Error::Codegen(inner) => { |
|
| 244 | + | if inner == rv64::Error::Allocation or inner == rv64::Error::Capacity { |
|
| 245 | + | return abi::Error::Exhausted; |
|
| 246 | + | } |
|
| 247 | + | return abi::Error::VerifyFailed; |
|
| 248 | + | } |
|
| 249 | + | else => return abi::Error::VerifyFailed, |
|
| 250 | + | } |
|
| 251 | + | } |
|
| 252 | + | ||
| 253 | + | /// Used native extents and validated metadata ready for publication. |
|
| 254 | + | export record Compiled: Copy { |
|
| 255 | + | /// Pages occupied by generated code. |
|
| 256 | + | code: u32, |
|
| 257 | + | /// Pages occupied by the packed native catalog. |
|
| 258 | + | metadata: u32, |
|
| 259 | + | /// Immutable descriptor and resolved dependencies for the reserved package. |
|
| 260 | + | admission: registry::Admission, |
|
| 261 | + | } |
|
| 262 | + | ||
| 263 | + | /// Compile and pack one candidate into caller-owned physical output extents. |
|
| 264 | + | /// The workspace must satisfy the contract of `view`. It stays unsafe because the |
|
| 265 | + | /// compiler's arenas and storage take owned slices of the workspace. |
|
| 266 | + | unsafe fn generate( |
|
| 267 | + | work: *mut Workspace, |
|
| 268 | + | input: &binary::Package, |
|
| 269 | + | packages: ®istry::Published, |
|
| 270 | + | target: &Output, |
|
| 271 | + | access: &OutputAccess, |
|
| 272 | + | length: u32 |
|
| 273 | + | ) -> Compiled throws (abi::Error) { |
|
| 274 | + | match access { |
|
| 275 | + | case OutputAccess::Held(code, metadata) => { |
|
| 276 | + | let count = try registry::imports( |
|
| 277 | + | packages, &input.dependencies[..], &mut work.imports[..] |
|
| 278 | + | ); |
|
| 279 | + | let arena = alloc::new(&mut work.code[..]); |
|
| 280 | + | let mut scratch = alloc::new(&mut work.scratch[..]); |
|
| 281 | + | let package = try shared::compile( |
|
| 282 | + | &*input, |
|
| 283 | + | target.object.index, |
|
| 284 | + | target.codeBase, |
|
| 285 | + | &work.imports[..count], |
|
| 286 | + | shared::Storage { |
|
| 287 | + | data: &mut work.data[..], |
|
| 288 | + | symbols: &mut work.symbols[..], |
|
| 289 | + | exports: &mut work.exports[..], |
|
| 290 | + | template: &mut work.template[..], |
|
| 291 | + | relocations: &mut work.relocations[..], |
|
| 292 | + | stack: rv64::StackCheck::TpLimit, |
|
| 293 | + | }, |
|
| 294 | + | arena, |
|
| 295 | + | &mut scratch |
|
| 296 | + | ) catch error { |
|
| 297 | + | throw backend(error); |
|
| 298 | + | }; |
|
| 299 | + | if package.code.len > CODE_BYTES / 4 { |
|
| 300 | + | throw abi::Error::Exhausted; |
|
| 301 | + | } |
|
| 302 | + | let codeBytes = package.code.len * 4; |
|
| 303 | + | try physical::writeAt(code, 0, @sliceOf(package.code.ptr as *u8, codeBytes)); |
|
| 304 | + | // Compilation is over, so the label scratch is free to stage the catalog until it is |
|
| 305 | + | // copied out. |
|
| 306 | + | let used = try catalog::pack( |
|
| 307 | + | &[ |
|
| 308 | + | catalog::Entry { |
|
| 309 | + | source: &work.input[..length], package |
|
| 310 | + | }, |
|
| 311 | + | ], |
|
| 312 | + | target.metadataBase, |
|
| 313 | + | &mut work.scratch[..METADATA_BYTES] |
|
| 314 | + | ) catch error { |
|
| 315 | + | throw backend(error); |
|
| 316 | + | }; |
|
| 317 | + | try physical::writeAt(metadata, 0, &work.scratch[..used]); |
|
| 318 | + | let mut codePages = (codeBytes |
|
| 319 | + | + (limits::FRAME_SIZE as u32 - 1)) / limits::FRAME_SIZE as u32; |
|
| 320 | + | if codePages == 0 { |
|
| 321 | + | set codePages = 1; |
|
| 322 | + | } |
|
| 323 | + | // Frames may hold bytes of earlier owners. Zero words are illegal instructions, so a |
|
| 324 | + | // stray jump past the code traps instead of running them. |
|
| 325 | + | let tail = codePages as u64 * limits::FRAME_SIZE - codeBytes as u64; |
|
| 326 | + | try physical::zeroAt(code, codeBytes as u64, tail); |
|
| 327 | + | // Successful packing wrote one entry at metadataBase, in frames private to the output |
|
| 328 | + | // reservation. |
|
| 329 | + | let native = descriptor(target.metadataBase); |
|
| 330 | + | let admission = try registry::admit( |
|
| 331 | + | packages, target.object, native.source, native.package |
|
| 332 | + | ); |
|
| 333 | + | return Compiled { |
|
| 334 | + | code: codePages, |
|
| 335 | + | metadata: (used |
|
| 336 | + | + (limits::FRAME_SIZE as u32 - 1)) / limits::FRAME_SIZE as u32, |
|
| 337 | + | admission, |
|
| 338 | + | }; |
|
| 339 | + | } |
|
| 340 | + | } |
|
| 341 | + | } |
|
| 342 | + | ||
| 343 | + | /// Return unused output frames while preserving at least one frame for each extent. |
|
| 344 | + | fn trim(pool: &mut frames::Pool, run: frames::Run, count: u32) -> frames::Run { |
|
| 345 | + | assert count > 0 and count <= run.count; |
|
| 346 | + | if count < run.count { |
|
| 347 | + | try! frames::release(pool, frames::Run { |
|
| 348 | + | first: run.first + count, count: run.count - count |
|
| 349 | + | }); |
|
| 350 | + | } |
|
| 351 | + | return frames::Run { first: run.first, count }; |
|
| 352 | + | } |
|
| 353 | + | ||
| 354 | + | /// Check that the input's workspace token covers a complete Workspace, and return its address. |
|
| 355 | + | fn workspaceAddress(access: &InputAccess) -> u64 { |
|
| 356 | + | match access { |
|
| 357 | + | case InputAccess::Held(work, source) => { |
|
| 358 | + | assert physical::size(work) >= @sizeOf(Workspace) as u64; |
|
| 359 | + | return physical::start(work); |
|
| 360 | + | } |
|
| 361 | + | } |
|
| 362 | + | } |
|
| 363 | + | ||
| 364 | + | /// Snapshot, decode and verify input in the held private workspace. |
|
| 365 | + | /// The decoded package stays valid until this reservation is completed or cancelled. |
|
| 366 | + | export fn decode(reservation: &Reservation) -> binary::Package throws (abi::Error) { |
|
| 367 | + | match reservation { |
|
| 368 | + | case Reservation::Held(input, access) => |
|
| 369 | + | return try decodeInput( |
|
| 370 | + | input, access |
|
| 371 | + | ), |
|
| 372 | + | } |
|
| 373 | + | } |
|
| 374 | + | ||
| 375 | + | /// Snapshot retained input, decode it, and verify it in its private workspace. Input that |
|
| 376 | + | /// the verifier rejects fails with `VerifyFailed`, and storage exhaustion with `Exhausted`. |
|
| 377 | + | export fn decodeInput(input: &Input, access: &InputAccess) -> binary::Package throws (abi::Error) { |
|
| 378 | + | let address = workspaceAddress(access); |
|
| 379 | + | match access { |
|
| 380 | + | case InputAccess::Held(held, source) => { |
|
| 381 | + | // SAFETY: see `Input`. |
|
| 382 | + | unsafe { |
|
| 383 | + | let work = view(address); |
|
| 384 | + | try physical::readAt(source, 0, &mut work.input[..input.length]); |
|
| 385 | + | let mut decoder = alloc::new(&mut work.decoded[..]); |
|
| 386 | + | let mut package = try program::decode( |
|
| 387 | + | &work.input[..input.length], |
|
| 388 | + | &mut decoder, |
|
| 389 | + | binary::Limits { |
|
| 390 | + | registers: liveness::MAX_SSA_REGS, blocks: labels::MAX_BLOCKS_PER_FN |
|
| 391 | + | } |
|
| 392 | + | ) catch err { |
|
| 393 | + | if err == binary::Error::Storage { |
|
| 394 | + | throw abi::Error::Exhausted; |
|
| 395 | + | } |
|
| 396 | + | throw abi::Error::VerifyFailed; |
|
| 397 | + | }; |
|
| 398 | + | let mut scratch = alloc::new(&mut work.scratch[..]); |
|
| 399 | + | try verify::verifyPackage( |
|
| 400 | + | &mut package, |
|
| 401 | + | shared::verifierConfig(@sizeOf(abi::Env) as u32), |
|
| 402 | + | &mut scratch, |
|
| 403 | + | &mut decoder |
|
| 404 | + | ) catch err { |
|
| 405 | + | if err.rule == verify::Rule::Storage { |
|
| 406 | + | throw abi::Error::Exhausted; |
|
| 407 | + | } |
|
| 408 | + | throw abi::Error::VerifyFailed; |
|
| 409 | + | }; |
|
| 410 | + | return package; |
|
| 411 | + | } |
|
| 412 | + | } |
|
| 413 | + | } |
|
| 414 | + | } |
|
| 415 | + | ||
| 416 | + | /// Reserved native output and its immutable physical extents. |
|
| 417 | + | export record Output: Copy { |
|
| 418 | + | /// Reserved package slot selected for generated state references. |
|
| 419 | + | object: abi::Ref, |
|
| 420 | + | /// Private executable frames. |
|
| 421 | + | code: frames::Run, |
|
| 422 | + | /// Private source and descriptor frames. |
|
| 423 | + | metadata: frames::Run, |
|
| 424 | + | /// Validated physical executable base. |
|
| 425 | + | codeBase: u64, |
|
| 426 | + | /// Validated physical descriptor base. |
|
| 427 | + | metadataBase: u64, |
|
| 428 | + | } |
|
| 429 | + | ||
| 430 | + | /// Proof that the output frames are private to the output reservation. |
|
| 431 | + | export union OutputAccess { |
|
| 432 | + | /// The first token covers the code frames and the second the descriptor frames. |
|
| 433 | + | Held(physical::Access, physical::Access), |
|
| 434 | + | } |
|
| 435 | + | ||
| 436 | + | /// Native output that must be published or cancelled under serialization. |
|
| 437 | + | export union OutputReservation: Once { |
|
| 438 | + | /// One unpublished package, its private output frames, and the tokens that prove them. |
|
| 439 | + | Held(Output, OutputAccess), |
|
| 440 | + | } |
|
| 441 | + | ||
| 442 | + | /// Check that an output's tokens cover exactly its code and descriptor frames. |
|
| 443 | + | export fn outputCovers(output: &Output, access: &OutputAccess) -> bool { |
|
| 444 | + | match access { |
|
| 445 | + | case OutputAccess::Held(code, metadata) => |
|
| 446 | + | return physical::covers( |
|
| 447 | + | code, output.codeBase, output.code.count as u64 * limits::FRAME_SIZE |
|
| 448 | + | ) |
|
| 449 | + | and physical::covers( |
|
| 450 | + | metadata, output.metadataBase, output.metadata.count as u64 * limits::FRAME_SIZE |
|
| 451 | + | ), |
|
| 452 | + | } |
|
| 453 | + | } |
|
| 454 | + | ||
| 455 | + | /// Reserve a package slot and native output under metadata serialization. |
|
| 456 | + | export fn reserveOutput( |
|
| 457 | + | pool: &mut frames::Pool, packages: &mut registry::Store |
|
| 458 | + | ) -> OutputReservation throws (abi::Error) { |
|
| 459 | + | let slot = try registry::reserve(packages); |
|
| 460 | + | let object = slots::reference(&slot); |
|
| 461 | + | let codeReservation = try frames::reserve( |
|
| 462 | + | pool, CODE_BYTES / limits::FRAME_SIZE as u32 |
|
| 463 | + | ) catch err { |
|
| 464 | + | registry::cancel(packages, slot); |
|
| 465 | + | throw err; |
|
| 466 | + | }; |
|
| 467 | + | let codeAccess = physical::reserved(&*pool, &codeReservation); |
|
| 468 | + | let code = frames::commit(codeReservation); |
|
| 469 | + | let metadataReservation = try frames::reserve( |
|
| 470 | + | pool, METADATA_BYTES / limits::FRAME_SIZE as u32 |
|
| 471 | + | ) catch err { |
|
| 472 | + | try! frames::release(pool, code); |
|
| 473 | + | registry::cancel(packages, slot); |
|
| 474 | + | throw err; |
|
| 475 | + | }; |
|
| 476 | + | let metadataAccess = physical::reserved(&*pool, &metadataReservation); |
|
| 477 | + | let metadata = frames::commit(metadataReservation); |
|
| 478 | + | let codeRange = try! frames::extent(pool, code); |
|
| 479 | + | let metadataRange = try! frames::extent(pool, metadata); |
|
| 480 | + | match slot { |
|
| 481 | + | case slots::Reservation::Held(object) => |
|
| 482 | + | return OutputReservation::Held(Output { |
|
| 483 | + | object, code, metadata, codeBase: codeRange.start, metadataBase: metadataRange.start |
|
| 484 | + | }, OutputAccess::Held( |
|
| 485 | + | codeAccess, metadataAccess |
|
| 486 | + | )), |
|
| 487 | + | } |
|
| 488 | + | } |
|
| 489 | + | ||
| 490 | + | /// Compile a decoded package into reserved private output using resident dependencies. |
|
| 491 | + | export fn compile( |
|
| 492 | + | packages: ®istry::Published, |
|
| 493 | + | reservation: &Reservation, |
|
| 494 | + | input: &binary::Package, |
|
| 495 | + | output: &OutputReservation |
|
| 496 | + | ) -> Compiled throws (abi::Error) { |
|
| 497 | + | match reservation { |
|
| 498 | + | case Reservation::Held(source, sourceAccess) => match output { |
|
| 499 | + | case OutputReservation::Held(target, targetAccess) => { |
|
| 500 | + | return try compileInput( |
|
| 501 | + | packages, source, sourceAccess, input, target, targetAccess |
|
| 502 | + | ); |
|
| 503 | + | } |
|
| 504 | + | }, |
|
| 505 | + | } |
|
| 506 | + | } |
|
| 507 | + | ||
| 508 | + | /// Generate native code into retained output while the caller owns its workspace. |
|
| 509 | + | export fn compileInput( |
|
| 510 | + | packages: ®istry::Published, |
|
| 511 | + | source: &Input, |
|
| 512 | + | sourceAccess: &InputAccess, |
|
| 513 | + | input: &binary::Package, |
|
| 514 | + | target: &Output, |
|
| 515 | + | targetAccess: &OutputAccess |
|
| 516 | + | ) -> Compiled |
|
| 517 | + | throws (abi::Error) |
|
| 518 | + | { |
|
| 519 | + | let address = workspaceAddress(sourceAccess); |
|
| 520 | + | // SAFETY: see `Input`. |
|
| 521 | + | unsafe { |
|
| 522 | + | return try generate(view(address), input, packages, target, targetAccess, source.length); |
|
| 523 | + | } |
|
| 524 | + | } |
|
| 525 | + | ||
| 526 | + | /// Return unpublished output frames and its package slot under serialization. |
|
| 527 | + | export fn cancelOutput( |
|
| 528 | + | pool: &mut frames::Pool, packages: &mut registry::Store, reservation: OutputReservation |
|
| 529 | + | ) { |
|
| 530 | + | match reservation { |
|
| 531 | + | case OutputReservation::Held(output, access) => |
|
| 532 | + | cancelOutputUsing( |
|
| 533 | + | pool, packages, output, frames::release |
|
| 534 | + | ), |
|
| 535 | + | } |
|
| 536 | + | } |
|
| 537 | + | ||
| 538 | + | /// Cancel a package slot and transfer private output to the release operation. |
|
| 539 | + | /// Releasing frames needs only the retained output, not its tokens. |
|
| 540 | + | export fn cancelOutputUsing( |
|
| 541 | + | pool: &mut frames::Pool, |
|
| 542 | + | packages: &mut registry::Store, |
|
| 543 | + | output: Output, |
|
| 544 | + | release: fn(&mut frames::Pool, frames::Run) throws (abi::Error) |
|
| 545 | + | ) { |
|
| 546 | + | assert slots::matches(&packages.slots[..], output.object, slots::State::Reserved); |
|
| 547 | + | try! release(pool, output.metadata); |
|
| 548 | + | try! release(pool, output.code); |
|
| 549 | + | registry::cancel(packages, slots::Reservation::Held(output.object)); |
|
| 550 | + | } |
|
| 551 | + | ||
| 552 | + | /// Publish completed native output and retain its used frames under serialization. |
|
| 553 | + | export fn publishOutput( |
|
| 554 | + | state: &mut State, |
|
| 555 | + | pool: &mut frames::Pool, |
|
| 556 | + | packages: &mut registry::Store, |
|
| 557 | + | reservation: OutputReservation, |
|
| 558 | + | sizes: Compiled |
|
| 559 | + | ) -> abi::Ref { |
|
| 560 | + | match reservation { |
|
| 561 | + | case OutputReservation::Held(output, access) => { |
|
| 562 | + | assert outputCovers(&output, &access); |
|
| 563 | + | assert slots::matches(&packages.slots[..], output.object, slots::State::Reserved); |
|
| 564 | + | let retainedCode = trim(pool, output.code, sizes.code); |
|
| 565 | + | let retainedMetadata = trim(pool, output.metadata, sizes.metadata); |
|
| 566 | + | sync::syncInstructions(); |
|
| 567 | + | let result = registry::commit( |
|
| 568 | + | packages, slots::Reservation::Held(output.object), sizes.admission |
|
| 569 | + | ); |
|
| 570 | + | assert result == output.object; |
|
| 571 | + | set state.resident[result.index] = Resident { |
|
| 572 | + | code: retainedCode, metadata: retainedMetadata |
|
| 573 | + | }; |
|
| 574 | + | set state.live[result.index] = true; |
|
| 575 | + | return result; |
|
| 576 | + | } |
|
| 577 | + | } |
|
| 578 | + | } |
|
| 579 | + | ||
| 580 | + | /// Compare the snapshot with immutable resident content while the workspace is held. |
|
| 581 | + | export fn identify( |
|
| 582 | + | packages: ®istry::Published, reservation: &Reservation, input: &binary::Package |
|
| 583 | + | ) -> ?abi::Ref throws (abi::Error) { |
|
| 584 | + | match reservation { |
|
| 585 | + | case Reservation::Held(source, access) => |
|
| 586 | + | return try identifyInput( |
|
| 587 | + | packages, source, access, input |
|
| 588 | + | ), |
|
| 589 | + | } |
|
| 590 | + | } |
|
| 591 | + | ||
| 592 | + | /// Compare retained source bytes with immutable resident package content. |
|
| 593 | + | export fn identifyInput( |
|
| 594 | + | packages: ®istry::Published, source: &Input, access: &InputAccess, input: &binary::Package |
|
| 595 | + | ) -> ?abi::Ref throws (abi::Error) { |
|
| 596 | + | let address = workspaceAddress(access); |
|
| 597 | + | // SAFETY: see `Input`. |
|
| 598 | + | unsafe { |
|
| 599 | + | return try registry::identify( |
|
| 600 | + | packages, &input.name[..], &(*view(address)).input[..source.length] |
|
| 601 | + | ); |
|
| 602 | + | } |
|
| 603 | + | } |
|
| 604 | + | ||
| 605 | + | /// Reserve an Image handle and exclusive workspace under metadata serialization. |
|
| 606 | + | export fn reserve( |
|
| 607 | + | state: &mut State, store: &mut pages::Store, table: &mut capability::Table, request: Request |
|
| 608 | + | ) -> Reservation throws (abi::Error) { |
|
| 609 | + | let address = try source(store, table, request); |
|
| 610 | + | let sourceAccess = try physical::access(&store.backings.pool, address, request.length); |
|
| 611 | + | let handle = try slots::reserve(&mut table.slots[..]); |
|
| 612 | + | let lease = try acquire(state, &mut store.backings.pool, table.owner) catch err { |
|
| 613 | + | try! slots::cancel(&mut table.slots[..], handle); |
|
| 614 | + | throw err; |
|
| 615 | + | }; |
|
| 616 | + | match handle { |
|
| 617 | + | case slots::Reservation::Held(handle) => match lease { |
|
| 618 | + | case Lease::Held(run) => { |
|
| 619 | + | // The lease was just acquired, so its frames are allocated and hold a complete |
|
| 620 | + | // Workspace. |
|
| 621 | + | let extent = try! frames::extent(&store.backings.pool, run); |
|
| 622 | + | let work = try! physical::access( |
|
| 623 | + | &store.backings.pool, extent.start, @sizeOf(Workspace) as u64 |
|
| 624 | + | ); |
|
| 625 | + | return Reservation::Held(Input { |
|
| 626 | + | owner: table.owner, |
|
| 627 | + | handle, |
|
| 628 | + | frames: run, |
|
| 629 | + | base: extent.start, |
|
| 630 | + | address, |
|
| 631 | + | length: request.length as u32, |
|
| 632 | + | }, InputAccess::Held( |
|
| 633 | + | work, sourceAccess |
|
| 634 | + | )); |
|
| 635 | + | } |
|
| 636 | + | }, |
|
| 637 | + | } |
|
| 638 | + | } |
|
| 639 | + | ||
| 640 | + | /// Domain that owns the load, which is kept live while the input is held. |
|
| 641 | + | export fn owner(input: &Input) -> abi::Ref { |
|
| 642 | + | return input.owner; |
|
| 643 | + | } |
|
| 644 | + | ||
| 645 | + | /// Private workspace frames that the input holds until `cancel` or `finish`. |
|
| 646 | + | export fn workspace(input: &Input) -> frames::Run { |
|
| 647 | + | return input.frames; |
|
| 648 | + | } |
|
| 649 | + | ||
| 650 | + | /// Check workspace ownership and the caller's unpublished handle slot. |
|
| 651 | + | fn require(state: &State, table: &capability::Table, input: &Input) { |
|
| 652 | + | assert state.owner == input.owner and table.owner == input.owner; |
|
| 653 | + | assert slots::matches(&table.slots[..], input.handle, slots::State::Reserved); |
|
| 654 | + | } |
|
| 655 | + | ||
| 656 | + | /// Return the workspace and reserved handle under metadata serialization. |
|
| 657 | + | export fn cancel( |
|
| 658 | + | state: &mut State, |
|
| 659 | + | store: &mut pages::Store, |
|
| 660 | + | table: &mut capability::Table, |
|
| 661 | + | reservation: Reservation |
|
| 662 | + | ) { |
|
| 663 | + | match reservation { |
|
| 664 | + | case Reservation::Held(input, access) => |
|
| 665 | + | cancelUsing( |
|
| 666 | + | state, store, table, input, frames::release |
|
| 667 | + | ), |
|
| 668 | + | } |
|
| 669 | + | } |
|
| 670 | + | ||
| 671 | + | /// Cancel a reserved Image handle and transfer its workspace to the release operation. |
|
| 672 | + | /// Releasing frames needs only the retained input, not its tokens. |
|
| 673 | + | export fn cancelUsing( |
|
| 674 | + | state: &mut State, |
|
| 675 | + | store: &mut pages::Store, |
|
| 676 | + | table: &mut capability::Table, |
|
| 677 | + | input: Input, |
|
| 678 | + | release: fn(&mut frames::Pool, frames::Run) throws (abi::Error) |
|
| 679 | + | ) { |
|
| 680 | + | require(state, table, &input); |
|
| 681 | + | releaseUsing(state, &mut store.backings.pool, Lease::Held(input.frames), release); |
|
| 682 | + | try! slots::cancel(&mut table.slots[..], slots::Reservation::Held(input.handle)); |
|
| 683 | + | } |
|
| 684 | + | ||
| 685 | + | /// Install a resident package handle and return the workspace under serialization. |
|
| 686 | + | export fn finish( |
|
| 687 | + | state: &mut State, |
|
| 688 | + | store: &mut pages::Store, |
|
| 689 | + | table: &mut capability::Table, |
|
| 690 | + | reservation: Reservation, |
|
| 691 | + | object: abi::Ref |
|
| 692 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 693 | + | match reservation { |
|
| 694 | + | case Reservation::Held(input, access) => { |
|
| 695 | + | assert covers(&input, &access); |
|
| 696 | + | require(state, table, &input); |
|
| 697 | + | if not backing::domainLive(&store.backings, input.owner) { |
|
| 698 | + | cancelUsing(state, store, table, input, frames::release); |
|
| 699 | + | throw abi::Error::BadHandle; |
|
| 700 | + | } |
|
| 701 | + | let result = capability::publish( |
|
| 702 | + | table, |
|
| 703 | + | slots::Reservation::Held(input.handle), |
|
| 704 | + | capability::Entry { |
|
| 705 | + | kind: abi::Kind::Image, object, rights: abi::Rights(registry::IMAGE_RIGHTS) |
|
| 706 | + | } |
|
| 707 | + | ); |
|
| 708 | + | release(state, &mut store.backings.pool, Lease::Held(input.frames)); |
|
| 709 | + | return result; |
|
| 710 | + | } |
|
| 711 | + | } |
|
| 712 | + | } |
|
| 713 | + | ||
| 714 | + | /// Load verified RIL while the caller has exclusive access to shared metadata. |
|
| 715 | + | export fn load( |
|
| 716 | + | state: &mut State, |
|
| 717 | + | store: &mut pages::Store, |
|
| 718 | + | packages: &mut registry::Store, |
|
| 719 | + | table: &mut capability::Table, |
|
| 720 | + | request: Request |
|
| 721 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 722 | + | let pending = try reserve(state, store, table, request); |
|
| 723 | + | // `pending` holds the workspace lease until `cancel` or `finish` consumes it below, so the |
|
| 724 | + | // workspace stays allocated and exclusive through every decode, identify, and compile step. |
|
| 725 | + | let input = try decode(&pending) catch err { |
|
| 726 | + | cancel(state, store, table, pending); |
|
| 727 | + | throw err; |
|
| 728 | + | }; |
|
| 729 | + | let existing = try identify(®istry::capture(&*packages), &pending, &input) catch err { |
|
| 730 | + | cancel(state, store, table, pending); |
|
| 731 | + | throw err; |
|
| 732 | + | }; |
|
| 733 | + | if let object = existing { |
|
| 734 | + | return try finish(state, store, table, pending, object); |
|
| 735 | + | } |
|
| 736 | + | let output = try reserveOutput(&mut store.backings.pool, packages) catch err { |
|
| 737 | + | cancel(state, store, table, pending); |
|
| 738 | + | throw err; |
|
| 739 | + | }; |
|
| 740 | + | let sizes = try compile(®istry::capture(&*packages), &pending, &input, &output) catch err { |
|
| 741 | + | cancelOutput(&mut store.backings.pool, packages, output); |
|
| 742 | + | cancel(state, store, table, pending); |
|
| 743 | + | throw err; |
|
| 744 | + | }; |
|
| 745 | + | let object = publishOutput(state, &mut store.backings.pool, packages, output, sizes); |
|
| 746 | + | return try finish(state, store, table, pending, object); |
|
| 747 | + | } |
kernel/kernel/loader.ras
added
+12 -0
| 1 | + | //! Physical views of validated loader-owned extents. |
|
| 2 | + | .text; |
|
| 3 | + | .export @kernel::loader::view; |
|
| 4 | + | .export @kernel::loader::descriptor; |
|
| 5 | + | ||
| 6 | + | // A validated workspace address starts one complete aligned Workspace record. |
|
| 7 | + | @kernel::loader::view |
|
| 8 | + | ret; |
|
| 9 | + | ||
| 10 | + | // Successful packing initializes one native catalog entry at this address. |
|
| 11 | + | @kernel::loader::descriptor |
|
| 12 | + | ret; |
kernel/kernel/locked.rad
added
+305 -0
| 1 | + | //! Locked regions of the call entry points. |
|
| 2 | + | //! |
|
| 3 | + | //! Each region runs while its caller holds the metadata lock, which the `Guard` |
|
| 4 | + | //! argument proves. It reaches the shared metadata and the caller's domain memory |
|
| 5 | + | //! once, and runs one safe function from `calls` on them. A reserved call pins its |
|
| 6 | + | //! domain's storage across lock releases, so later regions reuse the `Domain` |
|
| 7 | + | //! record that `lookup` returned; see `Domain.memory`. |
|
| 8 | + | ||
| 9 | + | use std::lang::il::binary; |
|
| 10 | + | use super::abi; |
|
| 11 | + | use super::pages; |
|
| 12 | + | use super::physical; |
|
| 13 | + | use super::domains; |
|
| 14 | + | use super::registry; |
|
| 15 | + | use super::loader; |
|
| 16 | + | use super::dispatch; |
|
| 17 | + | use super::sync; |
|
| 18 | + | use super::remote; |
|
| 19 | + | use super::lifecycle; |
|
| 20 | + | use super::calls; |
|
| 21 | + | use super::state; |
|
| 22 | + | ||
| 23 | + | /// Values captured with a load reservation, which the unlocked steps work from. |
|
| 24 | + | export record LoadStart: Copy { |
|
| 25 | + | /// The call's own load record. |
|
| 26 | + | loading: lifecycle::Loading, |
|
| 27 | + | /// Packages published so far. |
|
| 28 | + | resident: registry::Published, |
|
| 29 | + | } |
|
| 30 | + | ||
| 31 | + | /// Values captured with a domain reservation, which private initialization works from. |
|
| 32 | + | export record DomainStart: Copy { |
|
| 33 | + | /// The call's own creation record. |
|
| 34 | + | creation: domains::Creation, |
|
| 35 | + | /// Packages published so far. |
|
| 36 | + | resident: registry::Published, |
|
| 37 | + | } |
|
| 38 | + | ||
| 39 | + | /// Values captured once a load's output is reserved, which compilation works from. |
|
| 40 | + | export record CompileStart: Copy { |
|
| 41 | + | /// The call's own load record, including its output. |
|
| 42 | + | loading: lifecycle::Loading, |
|
| 43 | + | /// Packages published so far, including the output's package slot. |
|
| 44 | + | resident: registry::Published, |
|
| 45 | + | } |
|
| 46 | + | ||
| 47 | + | /// A readiness request published for other harts, with the caller's context to retain. |
|
| 48 | + | export record Readied: Copy { |
|
| 49 | + | /// Context of the calling hart that waits for the acknowledgements. |
|
| 50 | + | context: abi::Ref, |
|
| 51 | + | /// Published requests, one per target hart. |
|
| 52 | + | batch: remote::Batch, |
|
| 53 | + | } |
|
| 54 | + | ||
| 55 | + | /// A load reserved in the first locked region of the call. |
|
| 56 | + | export union LoadBegun: Once { |
|
| 57 | + | /// The lease, the values captured under the same lock, and the tokens that |
|
| 58 | + | /// only the continuation holding the lease may use. |
|
| 59 | + | Held(lifecycle::Lease, LoadStart, lifecycle::LoadAccess), |
|
| 60 | + | } |
|
| 61 | + | ||
| 62 | + | /// A domain creation reserved in the first locked region of the call. |
|
| 63 | + | export union DomainBegun: Once { |
|
| 64 | + | /// The lease, the values captured under the same lock, and the tokens that |
|
| 65 | + | /// only the continuation holding the lease may use. |
|
| 66 | + | Held(lifecycle::Lease, DomainStart, domains::CreationAccess), |
|
| 67 | + | } |
|
| 68 | + | ||
| 69 | + | /// Look up the calling domain. |
|
| 70 | + | export fn lookup(guard: &sync::Guard, owner: abi::Ref) -> domains::Domain throws (abi::Error) { |
|
| 71 | + | // The guard serializes the shared metadata. |
|
| 72 | + | unsafe { |
|
| 73 | + | return try domains::get(&state::SHARED.domains, owner); |
|
| 74 | + | } |
|
| 75 | + | } |
|
| 76 | + | ||
| 77 | + | /// Return the compiler workspace and reserved handle of a load that cannot finish. |
|
| 78 | + | export fn cancelLoad( |
|
| 79 | + | guard: &sync::Guard, |
|
| 80 | + | domain: &domains::Domain, |
|
| 81 | + | lease: lifecycle::Lease, |
|
| 82 | + | access: lifecycle::LoadAccess |
|
| 83 | + | ) { |
|
| 84 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 85 | + | unsafe { |
|
| 86 | + | let shared = &mut state::SHARED; |
|
| 87 | + | lifecycle::cancelLoad( |
|
| 88 | + | &mut shared.lifecycle, |
|
| 89 | + | &mut shared.domains, |
|
| 90 | + | &mut shared.pages, |
|
| 91 | + | &mut shared.loader, |
|
| 92 | + | &mut shared.registry, |
|
| 93 | + | &mut domain.memory.table, |
|
| 94 | + | lease, |
|
| 95 | + | access |
|
| 96 | + | ); |
|
| 97 | + | } |
|
| 98 | + | } |
|
| 99 | + | ||
| 100 | + | /// Reserve a runtime load. |
|
| 101 | + | export fn beginLoad(guard: &sync::Guard, domain: &domains::Domain, arguments: &[u64]) -> LoadBegun |
|
| 102 | + | throws (abi::Error) |
|
| 103 | + | { |
|
| 104 | + | let request = loader::Request { |
|
| 105 | + | authority: abi::Handle(arguments[0]), |
|
| 106 | + | source: abi::Handle(arguments[1]), |
|
| 107 | + | offset: arguments[2], |
|
| 108 | + | length: arguments[3], |
|
| 109 | + | }; |
|
| 110 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 111 | + | unsafe { |
|
| 112 | + | let shared = &mut state::SHARED; |
|
| 113 | + | let table = &mut domain.memory.table; |
|
| 114 | + | let current = dispatch::running(guard, &shared.budgets); |
|
| 115 | + | let held = try calls::reserveLoad(shared, domain, table, request, current); |
|
| 116 | + | match held { |
|
| 117 | + | case lifecycle::LoadHeld::Held(lease, access) => { |
|
| 118 | + | let loading = lifecycle::load(&shared.lifecycle, &lease); |
|
| 119 | + | return LoadBegun::Held( |
|
| 120 | + | lease, |
|
| 121 | + | LoadStart { |
|
| 122 | + | loading, resident: registry::capture(&shared.registry) |
|
| 123 | + | }, |
|
| 124 | + | access |
|
| 125 | + | ); |
|
| 126 | + | } |
|
| 127 | + | } |
|
| 128 | + | } |
|
| 129 | + | } |
|
| 130 | + | ||
| 131 | + | /// Choose how a decoded load continues: finish with a resident package, or reserve output. |
|
| 132 | + | export fn continueLoad( |
|
| 133 | + | guard: &sync::Guard, |
|
| 134 | + | domain: &domains::Domain, |
|
| 135 | + | lease: lifecycle::Lease, |
|
| 136 | + | access: lifecycle::LoadAccess, |
|
| 137 | + | existing: ?abi::Ref |
|
| 138 | + | ) -> calls::Admitted throws (abi::Error) { |
|
| 139 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 140 | + | unsafe { |
|
| 141 | + | return try calls::admitLoad( |
|
| 142 | + | &mut state::SHARED, &mut domain.memory.table, lease, access, existing |
|
| 143 | + | ); |
|
| 144 | + | } |
|
| 145 | + | } |
|
| 146 | + | ||
| 147 | + | /// Capture what compilation works from after the output was reserved. |
|
| 148 | + | export fn captureCompile(guard: &sync::Guard, lease: &lifecycle::Lease) -> CompileStart { |
|
| 149 | + | // The guard serializes the shared metadata. |
|
| 150 | + | unsafe { |
|
| 151 | + | let loading = lifecycle::load(&state::SHARED.lifecycle, lease); |
|
| 152 | + | return CompileStart { loading, resident: registry::capture(&state::SHARED.registry) }; |
|
| 153 | + | } |
|
| 154 | + | } |
|
| 155 | + | ||
| 156 | + | /// Publish a compiled package. |
|
| 157 | + | export fn finishLoad( |
|
| 158 | + | guard: &sync::Guard, |
|
| 159 | + | domain: &domains::Domain, |
|
| 160 | + | lease: lifecycle::Lease, |
|
| 161 | + | access: lifecycle::LoadAccess, |
|
| 162 | + | compiled: loader::Compiled |
|
| 163 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 164 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 165 | + | unsafe { |
|
| 166 | + | let shared = &mut state::SHARED; |
|
| 167 | + | return try lifecycle::finishLoad( |
|
| 168 | + | &mut shared.lifecycle, |
|
| 169 | + | &mut shared.domains, |
|
| 170 | + | &mut shared.pages, |
|
| 171 | + | &mut shared.loader, |
|
| 172 | + | &mut shared.registry, |
|
| 173 | + | &mut domain.memory.table, |
|
| 174 | + | lease, |
|
| 175 | + | lifecycle::LoadResult::Compiled(compiled, access) |
|
| 176 | + | ); |
|
| 177 | + | } |
|
| 178 | + | } |
|
| 179 | + | ||
| 180 | + | /// Reserve a domain creation. |
|
| 181 | + | export fn beginDomain( |
|
| 182 | + | guard: &sync::Guard, domain: &domains::Domain, arguments: &[u64] |
|
| 183 | + | ) -> DomainBegun throws (abi::Error) { |
|
| 184 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 185 | + | unsafe { |
|
| 186 | + | let shared = &mut state::SHARED; |
|
| 187 | + | let table = &mut domain.memory.table; |
|
| 188 | + | let current = dispatch::running(guard, &shared.budgets); |
|
| 189 | + | let held = try calls::reserveDomain(shared, domain, table, arguments, current); |
|
| 190 | + | match held { |
|
| 191 | + | case lifecycle::DomainHeld::Held(lease, access) => { |
|
| 192 | + | let creation = lifecycle::domainCreation(&shared.lifecycle, &lease); |
|
| 193 | + | let resident = registry::capture(&shared.registry); |
|
| 194 | + | return DomainBegun::Held(lease, DomainStart { creation, resident }, access); |
|
| 195 | + | } |
|
| 196 | + | } |
|
| 197 | + | } |
|
| 198 | + | } |
|
| 199 | + | ||
| 200 | + | /// Return the reservation of a domain whose private initialization failed. |
|
| 201 | + | export fn abandonDomain( |
|
| 202 | + | guard: &sync::Guard, |
|
| 203 | + | domain: &domains::Domain, |
|
| 204 | + | lease: lifecycle::Lease, |
|
| 205 | + | access: domains::CreationAccess |
|
| 206 | + | ) { |
|
| 207 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 208 | + | unsafe { |
|
| 209 | + | calls::abandonDomain(&mut state::SHARED, &mut domain.memory.table, lease, access); |
|
| 210 | + | } |
|
| 211 | + | } |
|
| 212 | + | ||
| 213 | + | /// Publish an initialized domain. |
|
| 214 | + | export fn publishDomain( |
|
| 215 | + | guard: &sync::Guard, |
|
| 216 | + | domain: &domains::Domain, |
|
| 217 | + | lease: lifecycle::Lease, |
|
| 218 | + | access: domains::CreationAccess |
|
| 219 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 220 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 221 | + | unsafe { |
|
| 222 | + | return try calls::publishDomain( |
|
| 223 | + | &mut state::SHARED, &mut domain.memory.table, lease, access |
|
| 224 | + | ); |
|
| 225 | + | } |
|
| 226 | + | } |
|
| 227 | + | ||
| 228 | + | /// Reserve zeroed pages. The result carries the token to clear them with. |
|
| 229 | + | export fn beginPage( |
|
| 230 | + | guard: &sync::Guard, domain: &domains::Domain, arguments: &[u64] |
|
| 231 | + | ) -> lifecycle::PageHeld throws (abi::Error) { |
|
| 232 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 233 | + | unsafe { |
|
| 234 | + | let shared = &mut state::SHARED; |
|
| 235 | + | let table = &mut domain.memory.table; |
|
| 236 | + | let current = dispatch::running(guard, &shared.budgets); |
|
| 237 | + | return try calls::reservePage(shared, domain, table, arguments, current); |
|
| 238 | + | } |
|
| 239 | + | } |
|
| 240 | + | ||
| 241 | + | /// Publish cleared pages. |
|
| 242 | + | export fn publishPage( |
|
| 243 | + | guard: &sync::Guard, domain: &domains::Domain, lease: lifecycle::Lease, access: physical::Access |
|
| 244 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 245 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 246 | + | unsafe { |
|
| 247 | + | return try calls::publishPage(&mut state::SHARED, &mut domain.memory.table, lease, access); |
|
| 248 | + | } |
|
| 249 | + | } |
|
| 250 | + | ||
| 251 | + | /// Reserve a context. The result carries the token to clear its stack with. |
|
| 252 | + | export fn beginContext( |
|
| 253 | + | guard: &sync::Guard, domain: &domains::Domain, arguments: &[u64] |
|
| 254 | + | ) -> lifecycle::ContextHeld throws (abi::Error) { |
|
| 255 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 256 | + | unsafe { |
|
| 257 | + | let shared = &mut state::SHARED; |
|
| 258 | + | let memory = &mut *domain.memory; |
|
| 259 | + | let current = dispatch::running(guard, &shared.budgets); |
|
| 260 | + | return try calls::reserveContext(shared, domain, memory, arguments, current); |
|
| 261 | + | } |
|
| 262 | + | } |
|
| 263 | + | ||
| 264 | + | /// Publish a context whose stack was cleared. |
|
| 265 | + | export fn publishContext( |
|
| 266 | + | guard: &sync::Guard, owner: abi::Ref, lease: lifecycle::Lease, access: physical::Access |
|
| 267 | + | ) -> abi::Ref throws (abi::Error) { |
|
| 268 | + | // The guard serializes the shared metadata. |
|
| 269 | + | unsafe { |
|
| 270 | + | return try calls::publishContext(&mut state::SHARED, owner, lease, access); |
|
| 271 | + | } |
|
| 272 | + | } |
|
| 273 | + | ||
| 274 | + | /// Execute any other call. |
|
| 275 | + | export fn submit( |
|
| 276 | + | guard: &sync::Guard, domain: &domains::Domain, operation: u64, arguments: &[u64], source: u32 |
|
| 277 | + | ) -> calls::Outcome throws (abi::Error) { |
|
| 278 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 279 | + | unsafe { |
|
| 280 | + | let shared = &mut state::SHARED; |
|
| 281 | + | let memory = &mut *domain.memory; |
|
| 282 | + | let current = dispatch::running(guard, &shared.budgets); |
|
| 283 | + | let running = calls::Running { now: dispatch::clock(), current }; |
|
| 284 | + | return try calls::submit(shared, domain, memory, operation, arguments, source, running); |
|
| 285 | + | } |
|
| 286 | + | } |
|
| 287 | + | ||
| 288 | + | /// Publish the readiness request of an activation or wakeup and retain the caller. |
|
| 289 | + | export fn beginReady( |
|
| 290 | + | guard: &sync::Guard, domain: &domains::Domain, operation: u64, arguments: &[u64], source: u32 |
|
| 291 | + | ) -> Readied throws (abi::Error) { |
|
| 292 | + | // The guard serializes the shared metadata, and the record came from `get`. |
|
| 293 | + | unsafe { |
|
| 294 | + | let shared = &mut state::SHARED; |
|
| 295 | + | let memory = &mut *domain.memory; |
|
| 296 | + | let current = dispatch::current(guard, &shared.budgets); |
|
| 297 | + | let running = calls::Running { now: dispatch::clock(), current }; |
|
| 298 | + | let batch = try calls::publishReady( |
|
| 299 | + | shared, domain, memory, operation, arguments, source, running |
|
| 300 | + | ); |
|
| 301 | + | let context = abi::reference(current.context); |
|
| 302 | + | dispatch::startReady(&mut shared.domains, context, &batch); |
|
| 303 | + | return Readied { context, batch }; |
|
| 304 | + | } |
|
| 305 | + | } |
kernel/kernel/pages.rad
added
+424 -0
| 1 | + | //! Physical page objects, allocation, and exclusive splitting. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | use super::limits; |
|
| 5 | + | use super::slots; |
|
| 6 | + | use super::frames; |
|
| 7 | + | use super::backing; |
|
| 8 | + | use super::capability; |
|
| 9 | + | use super::transactions; |
|
| 10 | + | use super::platform; |
|
| 11 | + | use super::physical; |
|
| 12 | + | use super::range; |
|
| 13 | + | use std::mem; |
|
| 14 | + | ||
| 15 | + | /// Page capability rights created by physical allocation. |
|
| 16 | + | export constant DEFAULT_RIGHTS: u16 = abi::READ | abi::WRITE | abi::GRANT | abi::TRANSFER; |
|
| 17 | + | ||
| 18 | + | /// One page-aligned segment of a retained backing allocation. |
|
| 19 | + | export record Page: Copy { |
|
| 20 | + | /// Common backing record, including the complete allocation's exposures. |
|
| 21 | + | backing: abi::Ref, |
|
| 22 | + | /// First physical byte of this segment. |
|
| 23 | + | base: u64, |
|
| 24 | + | /// Number of contiguous frames in this segment. |
|
| 25 | + | count: u32, |
|
| 26 | + | /// Domain that created the original allocation. |
|
| 27 | + | origin: abi::Ref, |
|
| 28 | + | /// Number of live capabilities that name this page object. |
|
| 29 | + | handles: u32, |
|
| 30 | + | } |
|
| 31 | + | ||
| 32 | + | /// Fixed physical-memory metadata. Callers serialize shared mutations. |
|
| 33 | + | export record Store: Copy { |
|
| 34 | + | /// Page-object slot lifetimes. |
|
| 35 | + | slots: [slots::Slot; limits::PAGES], |
|
| 36 | + | /// Live page-object payloads. |
|
| 37 | + | records: [Page; limits::PAGES], |
|
| 38 | + | /// Backing allocations and domain exposures. |
|
| 39 | + | backings: backing::Store, |
|
| 40 | + | } |
|
| 41 | + | ||
| 42 | + | /// Initialize physical-memory metadata before creating domains or pages. |
|
| 43 | + | export fn initialize(store: &mut Store, machine: &platform::Platform) throws (abi::Error) { |
|
| 44 | + | backing::initialize(&mut store.backings); |
|
| 45 | + | try frames::initialize(&mut store.backings.pool, machine); |
|
| 46 | + | slots::initialize(&mut store.slots[..]); |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | /// Read a live page object through its internal generation-bearing reference. |
|
| 50 | + | export fn get(store: &Store, object: abi::Ref) -> Page throws (abi::Error) { |
|
| 51 | + | if not slots::matches(&store.slots[..], object, slots::State::Live) { |
|
| 52 | + | throw abi::Error::BadHandle; |
|
| 53 | + | } |
|
| 54 | + | return store.records[object.index]; |
|
| 55 | + | } |
|
| 56 | + | ||
| 57 | + | /// Check complete byte-range authority through live Page handles. |
|
| 58 | + | /// Adjacent authorized pages can cover one range. No memory is accessed here. |
|
| 59 | + | export fn accessible( |
|
| 60 | + | store: &Store, table: &capability::Table, address: u64, size: u64, rights: abi::Rights |
|
| 61 | + | ) -> bool { |
|
| 62 | + | if size > mem::U64_MAX - address { |
|
| 63 | + | return false; |
|
| 64 | + | } |
|
| 65 | + | let end = address + size; |
|
| 66 | + | let mut cursor = address; |
|
| 67 | + | while cursor < end { |
|
| 68 | + | let before = cursor; |
|
| 69 | + | for i in 0..limits::HANDLES { |
|
| 70 | + | if table.slots[i].state <> slots::State::Live { |
|
| 71 | + | continue; |
|
| 72 | + | } |
|
| 73 | + | let entry = table.entries[i]; |
|
| 74 | + | if entry.kind <> abi::Kind::Page or not abi::permits(entry.rights, rights) { |
|
| 75 | + | continue; |
|
| 76 | + | } |
|
| 77 | + | if not slots::matches(&store.slots[..], entry.object, slots::State::Live) { |
|
| 78 | + | continue; |
|
| 79 | + | } |
|
| 80 | + | let page = store.records[entry.object.index]; |
|
| 81 | + | let limit = page.base + page.count as u64 * limits::FRAME_SIZE; |
|
| 82 | + | if page.base <= cursor and cursor < limit { |
|
| 83 | + | set cursor = limit; |
|
| 84 | + | if cursor >= end { |
|
| 85 | + | return true; |
|
| 86 | + | } |
|
| 87 | + | } |
|
| 88 | + | } |
|
| 89 | + | if cursor == before { |
|
| 90 | + | return false; |
|
| 91 | + | } |
|
| 92 | + | } |
|
| 93 | + | return true; |
|
| 94 | + | } |
|
| 95 | + | ||
| 96 | + | /// Check that a Page can be a stack, and return its extent. |
|
| 97 | + | /// |
|
| 98 | + | /// The Page must be readable and writable, never reached through `page.slice`, and the only |
|
| 99 | + | /// handle to any frame of its allocation: a grant or a split sibling would let someone else |
|
| 100 | + | /// reach the stack. |
|
| 101 | + | export fn stackExtent(store: &Store, table: &capability::Table, handle: abi::Handle) -> range::Range |
|
| 102 | + | throws (abi::Error) |
|
| 103 | + | { |
|
| 104 | + | let entry = try capability::lookup(table, handle, abi::Kind::Page, abi::Rights( |
|
| 105 | + | abi::READ | abi::WRITE |
|
| 106 | + | )); |
|
| 107 | + | let page = try get(store, entry.object); |
|
| 108 | + | if page.handles <> 1 or not try backing::sole(&store.backings, page.backing) { |
|
| 109 | + | throw abi::Error::Busy; |
|
| 110 | + | } |
|
| 111 | + | if try backing::accessed(&store.backings, page.backing) { |
|
| 112 | + | throw abi::Error::Denied; |
|
| 113 | + | } |
|
| 114 | + | return range::Range { |
|
| 115 | + | start: page.base, end: page.base + page.count as u64 * limits::FRAME_SIZE |
|
| 116 | + | }; |
|
| 117 | + | } |
|
| 118 | + | ||
| 119 | + | /// Take the memory of a Page for a stack, and consume its handle. |
|
| 120 | + | /// |
|
| 121 | + | /// On success the handle is gone, so nothing can grant, transfer, split or access the memory |
|
| 122 | + | /// again. The frames stay with the allocation, which the domains that hold exposure keep until |
|
| 123 | + | /// they end. Returns the extent of the Page; see [`stackExtent`] for the conditions. |
|
| 124 | + | export fn consumeStack( |
|
| 125 | + | store: &mut Store, table: &mut capability::Table, handle: abi::Handle |
|
| 126 | + | ) -> range::Range throws (abi::Error) { |
|
| 127 | + | let extent = try stackExtent(&*store, &*table, handle); |
|
| 128 | + | try drop(store, table, handle); |
|
| 129 | + | return extent; |
|
| 130 | + | } |
|
| 131 | + | ||
| 132 | + | /// Private frames and unpublished slots held while memory is cleared. |
|
| 133 | + | export record Allocation: Copy { |
|
| 134 | + | /// Calling domain whose table contains the reserved handle. |
|
| 135 | + | owner: abi::Ref, |
|
| 136 | + | /// Unpublished capability slot. |
|
| 137 | + | handle: abi::Ref, |
|
| 138 | + | /// Unpublished page-object slot. |
|
| 139 | + | page: abi::Ref, |
|
| 140 | + | /// Unpublished backing-allocation slot. |
|
| 141 | + | backing: abi::Ref, |
|
| 142 | + | /// Frames excluded from allocation until publication or cancellation. |
|
| 143 | + | run: frames::Run, |
|
| 144 | + | /// Validated physical base for private initialization. |
|
| 145 | + | base: u64, |
|
| 146 | + | } |
|
| 147 | + | ||
| 148 | + | /// Allocation capacity that must be published or cancelled under serialization. |
|
| 149 | + | export union Reservation: Once { |
|
| 150 | + | /// Private memory, its three reserved metadata slots, and the token that proves |
|
| 151 | + | /// the frames are private. The token covers exactly `run` from `base`. |
|
| 152 | + | Held(Allocation, physical::Access), |
|
| 153 | + | } |
|
| 154 | + | ||
| 155 | + | /// Reserve page capacity under metadata serialization for private initialization. |
|
| 156 | + | /// Allocation authority must name the calling domain. Failure cancels reservations. |
|
| 157 | + | export fn reserve( |
|
| 158 | + | store: &mut Store, table: &mut capability::Table, authority: abi::Handle, count: u64 |
|
| 159 | + | ) -> Reservation throws (abi::Error) { |
|
| 160 | + | if count == 0 { |
|
| 161 | + | throw abi::Error::InvalidArg; |
|
| 162 | + | } |
|
| 163 | + | if count > limits::FRAMES as u64 { |
|
| 164 | + | throw abi::Error::OutOfMemory; |
|
| 165 | + | } |
|
| 166 | + | let owner = table.owner; |
|
| 167 | + | let permission = try capability::authority(table, authority, abi::Rights(abi::ALLOCATE)); |
|
| 168 | + | if permission.object <> owner { |
|
| 169 | + | throw abi::Error::Denied; |
|
| 170 | + | } |
|
| 171 | + | let backings = &mut store.backings; |
|
| 172 | + | if not backing::domainLive(backings, owner) { |
|
| 173 | + | throw abi::Error::BadHandle; |
|
| 174 | + | } |
|
| 175 | + | let handleSlot = try slots::reserve(&mut table.slots[..]); |
|
| 176 | + | let pageSlot = try slots::reserve(&mut store.slots[..]) catch err { |
|
| 177 | + | try! slots::cancel(&mut table.slots[..], handleSlot); |
|
| 178 | + | throw err; |
|
| 179 | + | }; |
|
| 180 | + | let backingSlot = try slots::reserve(&mut backings.slots[..]) catch err { |
|
| 181 | + | try! slots::cancel(&mut store.slots[..], pageSlot); |
|
| 182 | + | try! slots::cancel(&mut table.slots[..], handleSlot); |
|
| 183 | + | throw err; |
|
| 184 | + | }; |
|
| 185 | + | let memory = try frames::reserve(&mut backings.pool, count as u32) catch err { |
|
| 186 | + | try! slots::cancel(&mut backings.slots[..], backingSlot); |
|
| 187 | + | try! slots::cancel(&mut store.slots[..], pageSlot); |
|
| 188 | + | try! slots::cancel(&mut table.slots[..], handleSlot); |
|
| 189 | + | throw err; |
|
| 190 | + | }; |
|
| 191 | + | let access = physical::reserved(&backings.pool, &memory); |
|
| 192 | + | let run = frames::commit(memory); |
|
| 193 | + | let extent = try! frames::extent(&backings.pool, run); |
|
| 194 | + | match handleSlot { |
|
| 195 | + | case slots::Reservation::Held(handle) => match pageSlot { |
|
| 196 | + | case slots::Reservation::Held(page) => match backingSlot { |
|
| 197 | + | case slots::Reservation::Held(backing) => |
|
| 198 | + | return Reservation::Held( |
|
| 199 | + | Allocation { |
|
| 200 | + | owner, handle, page, backing, run, base: extent.start |
|
| 201 | + | }, |
|
| 202 | + | access |
|
| 203 | + | ), |
|
| 204 | + | }, |
|
| 205 | + | }, |
|
| 206 | + | } |
|
| 207 | + | } |
|
| 208 | + | ||
| 209 | + | /// Clear private frames while shared metadata can be used by other harts. |
|
| 210 | + | /// The reservation must hold frames that no other reference can reach. |
|
| 211 | + | export fn clear(reservation: &Reservation) { |
|
| 212 | + | match reservation { |
|
| 213 | + | case Reservation::Held(allocation, access) => physical::clear(access), |
|
| 214 | + | } |
|
| 215 | + | } |
|
| 216 | + | ||
| 217 | + | /// Check that a token covers exactly the frames of an allocation. |
|
| 218 | + | export fn covers(allocation: &Allocation, access: &physical::Access) -> bool { |
|
| 219 | + | return physical::covers( |
|
| 220 | + | access, allocation.base, allocation.run.count as u64 * limits::FRAME_SIZE |
|
| 221 | + | ); |
|
| 222 | + | } |
|
| 223 | + | ||
| 224 | + | /// Check reserved generations before publishing or cancelling an allocation. |
|
| 225 | + | fn require(store: &Store, table: &capability::Table, allocation: &Allocation) { |
|
| 226 | + | assert table.owner == allocation.owner; |
|
| 227 | + | assert slots::matches(&table.slots[..], allocation.handle, slots::State::Reserved); |
|
| 228 | + | assert slots::matches(&store.slots[..], allocation.page, slots::State::Reserved); |
|
| 229 | + | assert slots::matches(&store.backings.slots[..], allocation.backing, slots::State::Reserved); |
|
| 230 | + | } |
|
| 231 | + | ||
| 232 | + | /// Publish cleared memory under metadata serialization while its owner stays live. |
|
| 233 | + | export fn publish( |
|
| 234 | + | store: &mut Store, table: &mut capability::Table, reservation: Reservation |
|
| 235 | + | ) -> abi::Handle { |
|
| 236 | + | match reservation { |
|
| 237 | + | case Reservation::Held(allocation, access) => { |
|
| 238 | + | assert covers(&allocation, &access); |
|
| 239 | + | require(store, table, &allocation); |
|
| 240 | + | assert backing::domainLive(&store.backings, allocation.owner); |
|
| 241 | + | let backing = backing::publish( |
|
| 242 | + | &mut store.backings, |
|
| 243 | + | slots::Reservation::Held(allocation.backing), |
|
| 244 | + | allocation.run, |
|
| 245 | + | allocation.owner |
|
| 246 | + | ); |
|
| 247 | + | set store.records[allocation.page.index] = Page { |
|
| 248 | + | backing, |
|
| 249 | + | base: allocation.base, |
|
| 250 | + | count: allocation.run.count, |
|
| 251 | + | origin: allocation.owner, |
|
| 252 | + | handles: 1, |
|
| 253 | + | }; |
|
| 254 | + | let page = try! slots::commit(&mut store.slots[..], slots::Reservation::Held( |
|
| 255 | + | allocation.page |
|
| 256 | + | )); |
|
| 257 | + | return capability::publish( |
|
| 258 | + | table, |
|
| 259 | + | slots::Reservation::Held(allocation.handle), |
|
| 260 | + | capability::Entry { |
|
| 261 | + | kind: abi::Kind::Page, object: page, rights: abi::Rights(DEFAULT_RIGHTS) |
|
| 262 | + | } |
|
| 263 | + | ); |
|
| 264 | + | } |
|
| 265 | + | } |
|
| 266 | + | } |
|
| 267 | + | ||
| 268 | + | /// Return private allocation capacity under metadata serialization. |
|
| 269 | + | export fn cancel(store: &mut Store, table: &mut capability::Table, reservation: Reservation) { |
|
| 270 | + | match reservation { |
|
| 271 | + | case Reservation::Held(allocation, access) => |
|
| 272 | + | cancelUsing( |
|
| 273 | + | store, table, allocation, frames::release |
|
| 274 | + | ), |
|
| 275 | + | } |
|
| 276 | + | } |
|
| 277 | + | ||
| 278 | + | /// Cancel unpublished slots and transfer their frames to the selected release operation. |
|
| 279 | + | /// Releasing frames needs only the retained allocation, not its token. |
|
| 280 | + | export fn cancelUsing( |
|
| 281 | + | store: &mut Store, |
|
| 282 | + | table: &mut capability::Table, |
|
| 283 | + | allocation: Allocation, |
|
| 284 | + | release: fn(&mut frames::Pool, frames::Run) throws (abi::Error) |
|
| 285 | + | ) { |
|
| 286 | + | require(store, table, &allocation); |
|
| 287 | + | try! release(&mut store.backings.pool, allocation.run); |
|
| 288 | + | try! slots::cancel(&mut store.backings.slots[..], slots::Reservation::Held(allocation.backing)); |
|
| 289 | + | try! slots::cancel(&mut store.slots[..], slots::Reservation::Held(allocation.page)); |
|
| 290 | + | try! slots::cancel(&mut table.slots[..], slots::Reservation::Held(allocation.handle)); |
|
| 291 | + | } |
|
| 292 | + | ||
| 293 | + | /// Allocate zeroed pages while the caller has exclusive access to metadata. |
|
| 294 | + | export fn allocate( |
|
| 295 | + | store: &mut Store, table: &mut capability::Table, authority: abi::Handle, count: u64 |
|
| 296 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 297 | + | let pending = try reserve(store, table, authority, count); |
|
| 298 | + | clear(&pending); |
|
| 299 | + | return publish(store, table, pending); |
|
| 300 | + | } |
|
| 301 | + | ||
| 302 | + | /// Split an exclusive page into two segments with the same backing and rights. |
|
| 303 | + | export fn split( |
|
| 304 | + | store: &mut Store, table: &mut capability::Table, handle: abi::Handle, leftCount: u64 |
|
| 305 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 306 | + | let entry = try capability::lookup(table, handle, abi::Kind::Page, abi::Rights(0)); |
|
| 307 | + | let source = try get(store, entry.object); |
|
| 308 | + | if leftCount == 0 or leftCount >= source.count as u64 { |
|
| 309 | + | throw abi::Error::InvalidArg; |
|
| 310 | + | } |
|
| 311 | + | if source.handles <> 1 { |
|
| 312 | + | throw abi::Error::Busy; |
|
| 313 | + | } |
|
| 314 | + | let handleSlot = try slots::reserve(&mut table.slots[..]); |
|
| 315 | + | let pageSlot = try slots::reserve(&mut store.slots[..]) catch err { |
|
| 316 | + | try! slots::cancel(&mut table.slots[..], handleSlot); |
|
| 317 | + | throw err; |
|
| 318 | + | }; |
|
| 319 | + | let object = slots::reference(&pageSlot); |
|
| 320 | + | try! backing::retainPage(&mut store.backings, source.backing); |
|
| 321 | + | set store.records[object.index] = Page { |
|
| 322 | + | backing: source.backing, |
|
| 323 | + | base: source.base + leftCount * limits::FRAME_SIZE, |
|
| 324 | + | count: source.count - leftCount as u32, |
|
| 325 | + | origin: source.origin, |
|
| 326 | + | handles: 1, |
|
| 327 | + | }; |
|
| 328 | + | set store.records[entry.object.index].count = leftCount as u32; |
|
| 329 | + | let page = try! slots::commit(&mut store.slots[..], pageSlot); |
|
| 330 | + | return capability::publish(table, handleSlot, capability::Entry { |
|
| 331 | + | kind: abi::Kind::Page, object: page, rights: entry.rights |
|
| 332 | + | }); |
|
| 333 | + | } |
|
| 334 | + | ||
| 335 | + | /// Add a capability reference and lifetime exposure before publishing that capability. |
|
| 336 | + | export fn retain(store: &mut Store, object: abi::Ref, receiver: abi::Ref) throws (abi::Error) { |
|
| 337 | + | let page = try get(store, object); |
|
| 338 | + | if page.handles == mem::U32_MAX { |
|
| 339 | + | throw abi::Error::Exhausted; |
|
| 340 | + | } |
|
| 341 | + | try backing::expose(&mut store.backings, page.backing, receiver); |
|
| 342 | + | set store.records[object.index].handles += 1; |
|
| 343 | + | } |
|
| 344 | + | ||
| 345 | + | /// Release a capability reference. The backing retains all lifetime exposures. |
|
| 346 | + | export fn release(store: &mut Store, object: abi::Ref) throws (abi::Error) { |
|
| 347 | + | let page = try get(store, object); |
|
| 348 | + | assert page.handles > 0; |
|
| 349 | + | set store.records[object.index].handles -= 1; |
|
| 350 | + | if page.handles > 1 { |
|
| 351 | + | return; |
|
| 352 | + | } |
|
| 353 | + | try! slots::release(&mut store.slots[..], object); |
|
| 354 | + | try! backing::releasePage(&mut store.backings, page.backing); |
|
| 355 | + | } |
|
| 356 | + | ||
| 357 | + | /// Acquire page lifetime and publish an attenuated capability into a resolved table. |
|
| 358 | + | fn receive( |
|
| 359 | + | store: &mut Store, |
|
| 360 | + | target: &mut capability::Table, |
|
| 361 | + | owner: abi::Ref, |
|
| 362 | + | source: abi::Handle, |
|
| 363 | + | entry: capability::Entry, |
|
| 364 | + | rights: u64 |
|
| 365 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 366 | + | let receiver = target.owner; |
|
| 367 | + | let pending = try transactions::reserve(target, owner, source, entry, rights, false); |
|
| 368 | + | try retain(store, entry.object, receiver) catch err { |
|
| 369 | + | transactions::cancel(target, pending); |
|
| 370 | + | throw err; |
|
| 371 | + | }; |
|
| 372 | + | return transactions::grant(target, pending); |
|
| 373 | + | } |
|
| 374 | + | ||
| 375 | + | /// Grant page authority to a separately resolved domain table. |
|
| 376 | + | /// The caller serializes both tables and page metadata through publication. |
|
| 377 | + | export fn grant( |
|
| 378 | + | store: &mut Store, |
|
| 379 | + | source: &capability::Table, |
|
| 380 | + | target: &mut capability::Table, |
|
| 381 | + | handle: abi::Handle, |
|
| 382 | + | rights: u64 |
|
| 383 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 384 | + | let entry = try capability::lookup(source, handle, abi::Kind::Page, abi::Rights(0)); |
|
| 385 | + | return try receive(store, target, source.owner, handle, entry, rights); |
|
| 386 | + | } |
|
| 387 | + | ||
| 388 | + | /// Create a narrower page capability in the calling domain's table. |
|
| 389 | + | export fn selfGrant( |
|
| 390 | + | store: &mut Store, table: &mut capability::Table, handle: abi::Handle, rights: u64 |
|
| 391 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 392 | + | let entry = try capability::lookup(table, handle, abi::Kind::Page, abi::Rights(0)); |
|
| 393 | + | let owner = table.owner; |
|
| 394 | + | return try receive(store, table, owner, handle, entry, rights); |
|
| 395 | + | } |
|
| 396 | + | ||
| 397 | + | /// Move page authority without changing its handle count or ending source exposure. |
|
| 398 | + | /// The caller serializes the resolved domain tables and page metadata. |
|
| 399 | + | export fn transfer( |
|
| 400 | + | store: &mut Store, |
|
| 401 | + | source: &mut capability::Table, |
|
| 402 | + | target: &mut capability::Table, |
|
| 403 | + | handle: abi::Handle, |
|
| 404 | + | rights: u64 |
|
| 405 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 406 | + | let entry = try capability::lookup(source, handle, abi::Kind::Page, abi::Rights(0)); |
|
| 407 | + | let page = try get(store, entry.object); |
|
| 408 | + | let receiver = target.owner; |
|
| 409 | + | let pending = try transactions::reserve(target, source.owner, handle, entry, rights, true); |
|
| 410 | + | try backing::expose(&mut store.backings, page.backing, receiver) catch err { |
|
| 411 | + | transactions::cancel(target, pending); |
|
| 412 | + | throw err; |
|
| 413 | + | }; |
|
| 414 | + | return transactions::transfer(source, target, pending); |
|
| 415 | + | } |
|
| 416 | + | ||
| 417 | + | /// Drop a page capability and release its object reference under serialization. |
|
| 418 | + | export fn drop(store: &mut Store, table: &mut capability::Table, handle: abi::Handle) |
|
| 419 | + | throws (abi::Error) |
|
| 420 | + | { |
|
| 421 | + | let entry = try capability::lookup(table, handle, abi::Kind::Page, abi::Rights(0)); |
|
| 422 | + | try release(store, entry.object); |
|
| 423 | + | try! capability::invalidate(table, handle); |
|
| 424 | + | } |
kernel/kernel/physical.rad
added
+320 -0
| 1 | + | //! Checked access to allocated RAM by physical address. |
|
| 2 | + | //! |
|
| 3 | + | //! This is the only kernel module that turns a physical address into a pointer |
|
| 4 | + | //! for byte or word data. Every access goes through an `Access`, an opaque |
|
| 5 | + | //! token that only this module can build. A token is built in one of two ways: |
|
| 6 | + | //! |
|
| 7 | + | //! - `reserved` derives it from a linear `frames::Reservation`, which only |
|
| 8 | + | //! `frames` can construct. Holding the reservation proves the frames are |
|
| 9 | + | //! allocated and private to the holder, so the pool bitmap is not consulted. |
|
| 10 | + | //! Code that runs outside the metadata lock must use this form. |
|
| 11 | + | //! - `access` checks a range against the pool: no wrap, a start inside the |
|
| 12 | + | //! pool, physically adjacent frames, and every frame allocated. It reads the |
|
| 13 | + | //! shared bitmap, so callers must hold the metadata lock (or run before other |
|
| 14 | + | //! harts use the pool). |
|
| 15 | + | //! |
|
| 16 | + | //! No token covers physical address 0 except the empty token: `frames` never |
|
| 17 | + | //! pools the page at 0, and zero-length operations build no slice, so no slice |
|
| 18 | + | //! points at address 0. |
|
| 19 | + | //! |
|
| 20 | + | //! The kernel image, statics, stacks, firmware, device windows, and boot data |
|
| 21 | + | //! are outside the pool, and free frames are rejected, so no safe caller can |
|
| 22 | + | //! reach them. |
|
| 23 | + | //! |
|
| 24 | + | //! A token is move-only, so one holder has it at a time, and the holder keeps it |
|
| 25 | + | //! only while the frames are allocated to that holder. Operations borrow it; |
|
| 26 | + | //! `narrow` consumes it. The token moves with its reservation to the lease and |
|
| 27 | + | //! continuation that use it, comes back when the lease is taken, and is dropped |
|
| 28 | + | //! on publication or cancellation. Retained allocation records hold none, so |
|
| 29 | + | //! teardown releases frames from the records alone. A published instance's base |
|
| 30 | + | //! table token lives in `domains::Store.tables` and is replaced by `empty()` when |
|
| 31 | + | //! the instance frames are reclaimed. |
|
| 32 | + | ||
| 33 | + | use super::abi; |
|
| 34 | + | use super::frames; |
|
| 35 | + | use super::limits; |
|
| 36 | + | use std::mem; |
|
| 37 | + | ||
| 38 | + | /// Map a physical address to a pointer. The address must be inside RAM that the |
|
| 39 | + | /// caller checked against the frame pool; this module is the only caller. |
|
| 40 | + | unsafe fn pointer(address: u64) -> *unsafe mut opaque; |
|
| 41 | + | ||
| 42 | + | /// Bytes moved per bounds check by the word operations. |
|
| 43 | + | constant CHUNK: u32 = 512; |
|
| 44 | + | ||
| 45 | + | /// A physical range that is known to be allocated RAM. Offsets in operations |
|
| 46 | + | /// are relative to its first byte. |
|
| 47 | + | export opaque record Access { |
|
| 48 | + | /// First physical byte. |
|
| 49 | + | start: u64, |
|
| 50 | + | /// Number of bytes. |
|
| 51 | + | length: u64, |
|
| 52 | + | } |
|
| 53 | + | ||
| 54 | + | /// A token for no bytes, which grants no access. Every operation on it with a |
|
| 55 | + | /// nonzero length fails, and its all-zero representation is this value. |
|
| 56 | + | export fn empty() -> Access { |
|
| 57 | + | return Access { start: 0, length: 0 }; |
|
| 58 | + | } |
|
| 59 | + | ||
| 60 | + | /// Number of bytes covered by a token. |
|
| 61 | + | export fn size(access: &Access) -> u64 { |
|
| 62 | + | return access.length; |
|
| 63 | + | } |
|
| 64 | + | ||
| 65 | + | /// Test whether a token covers exactly the given physical range. Callers use it to |
|
| 66 | + | /// pair a token with the record that describes the same allocation. |
|
| 67 | + | export fn covers(access: &Access, start: u64, length: u64) -> bool { |
|
| 68 | + | return access.start == start and access.length == length; |
|
| 69 | + | } |
|
| 70 | + | ||
| 71 | + | /// First physical byte of a token. The address is an integer and grants no access. |
|
| 72 | + | export fn start(access: &Access) -> u64 { |
|
| 73 | + | return access.start; |
|
| 74 | + | } |
|
| 75 | + | ||
| 76 | + | /// Derive a token for a sub-range of a token. Narrowing never widens access, |
|
| 77 | + | /// so it is sound; ranges past the end of the token are rejected. |
|
| 78 | + | export fn narrow(access: Access, offset: u64, length: u64) -> Access throws (abi::Error) { |
|
| 79 | + | return Access { start: try within(&access, offset, length), length }; |
|
| 80 | + | } |
|
| 81 | + | ||
| 82 | + | /// Derive a token for the frames of a reservation, which proves they are private. |
|
| 83 | + | export fn reserved(pool: &frames::Pool, reservation: &frames::Reservation) -> Access { |
|
| 84 | + | let range = try! frames::extent(pool, frames::reserved(reservation)); |
|
| 85 | + | return Access { start: range.start, length: range.end - range.start }; |
|
| 86 | + | } |
|
| 87 | + | ||
| 88 | + | /// Check a range against the pool and derive a token for it. Hold the metadata |
|
| 89 | + | /// lock: the bitmap is shared. An empty range at a non-wrapping address is accepted. |
|
| 90 | + | export fn access(pool: &frames::Pool, address: u64, length: u64) -> Access throws (abi::Error) { |
|
| 91 | + | if length > mem::U64_MAX - address { |
|
| 92 | + | throw abi::Error::InvalidArg; |
|
| 93 | + | } |
|
| 94 | + | if length == 0 { |
|
| 95 | + | return Access { start: address, length: 0 }; |
|
| 96 | + | } |
|
| 97 | + | let frame = ~(limits::FRAME_SIZE - 1); |
|
| 98 | + | let first = address & frame; |
|
| 99 | + | let last = (address + length - 1) & frame; |
|
| 100 | + | let index = frames::find(pool, first) else { |
|
| 101 | + | throw abi::Error::InvalidArg; |
|
| 102 | + | }; |
|
| 103 | + | let count = (last - first) / limits::FRAME_SIZE + 1; |
|
| 104 | + | if count > (pool.count - index) as u64 { |
|
| 105 | + | throw abi::Error::InvalidArg; |
|
| 106 | + | } |
|
| 107 | + | // Pool addresses are strictly increasing, so a span of exactly count frames has no gap. |
|
| 108 | + | if pool.addresses[index + count as u32 - 1] <> last { |
|
| 109 | + | throw abi::Error::InvalidArg; |
|
| 110 | + | } |
|
| 111 | + | if not frames::allocated(pool, frames::Run { first: index, count: count as u32 }) { |
|
| 112 | + | throw abi::Error::InvalidArg; |
|
| 113 | + | } |
|
| 114 | + | return Access { start: address, length }; |
|
| 115 | + | } |
|
| 116 | + | ||
| 117 | + | /// Resolve a sub-range of a token to a physical address. |
|
| 118 | + | fn within(access: &Access, offset: u64, length: u64) -> u64 throws (abi::Error) { |
|
| 119 | + | if offset > access.length or length > access.length - offset { |
|
| 120 | + | throw abi::Error::InvalidArg; |
|
| 121 | + | } |
|
| 122 | + | return access.start + offset; |
|
| 123 | + | } |
|
| 124 | + | ||
| 125 | + | /// Clear every byte a token covers. |
|
| 126 | + | export fn clear(access: &Access) { |
|
| 127 | + | try! zeroAt(access, 0, access.length); |
|
| 128 | + | } |
|
| 129 | + | ||
| 130 | + | /// Clear a byte range. Lengths beyond 4 GiB are rejected. |
|
| 131 | + | export fn zeroAt(access: &Access, offset: u64, length: u64) throws (abi::Error) { |
|
| 132 | + | if length > mem::U32_MAX as u64 { |
|
| 133 | + | throw abi::Error::InvalidArg; |
|
| 134 | + | } |
|
| 135 | + | let address = try within(access, offset, length); |
|
| 136 | + | if length == 0 { |
|
| 137 | + | return; |
|
| 138 | + | } |
|
| 139 | + | // SAFETY: within proved the range lies in the token's allocated RAM. The |
|
| 140 | + | // head and tail are bytes; the aligned middle is stored as whole words. |
|
| 141 | + | unsafe { |
|
| 142 | + | let mut lead = ((8 - (address & 7)) & 7) as u32; |
|
| 143 | + | if lead > length as u32 { |
|
| 144 | + | set lead = length as u32; |
|
| 145 | + | } |
|
| 146 | + | let words = (length as u32 - lead) / 8; |
|
| 147 | + | let tail = (length as u32 - lead) % 8; |
|
| 148 | + | let head = @sliceOf(pointer(address) as *unsafe mut u8, lead); |
|
| 149 | + | for i in 0..head.len { |
|
| 150 | + | set head[i] = 0; |
|
| 151 | + | } |
|
| 152 | + | let middle = @sliceOf(pointer(address + lead as u64) as *unsafe mut u64, words); |
|
| 153 | + | for i in 0..middle.len { |
|
| 154 | + | set middle[i] = 0; |
|
| 155 | + | } |
|
| 156 | + | let end = @sliceOf( |
|
| 157 | + | pointer(address + lead as u64 + words as u64 * 8) as *unsafe mut u8, tail |
|
| 158 | + | ); |
|
| 159 | + | for i in 0..end.len { |
|
| 160 | + | set end[i] = 0; |
|
| 161 | + | } |
|
| 162 | + | } |
|
| 163 | + | } |
|
| 164 | + | ||
| 165 | + | /// Copy kernel bytes into physical memory. |
|
| 166 | + | export fn writeAt(access: &Access, offset: u64, source: &[u8]) throws (abi::Error) { |
|
| 167 | + | let address = try within(access, offset, source.len as u64); |
|
| 168 | + | if source.len == 0 { |
|
| 169 | + | return; |
|
| 170 | + | } |
|
| 171 | + | // SAFETY: within proved the range lies in the token's allocated RAM. |
|
| 172 | + | unsafe { |
|
| 173 | + | let target = @sliceOf(pointer(address) as *unsafe mut u8, source.len); |
|
| 174 | + | for x, i in source { |
|
| 175 | + | set target[i] = x; |
|
| 176 | + | } |
|
| 177 | + | } |
|
| 178 | + | } |
|
| 179 | + | ||
| 180 | + | /// Copy physical memory into kernel bytes. |
|
| 181 | + | export fn readAt(access: &Access, offset: u64, target: &mut [u8]) throws (abi::Error) { |
|
| 182 | + | let address = try within(access, offset, target.len as u64); |
|
| 183 | + | if target.len == 0 { |
|
| 184 | + | return; |
|
| 185 | + | } |
|
| 186 | + | // SAFETY: within proved the range lies in the token's allocated RAM. |
|
| 187 | + | unsafe { |
|
| 188 | + | let source = @sliceOf(pointer(address) as *unsafe u8, target.len); |
|
| 189 | + | for i in 0..target.len { |
|
| 190 | + | set target[i] = source[i]; |
|
| 191 | + | } |
|
| 192 | + | } |
|
| 193 | + | } |
|
| 194 | + | ||
| 195 | + | /// Store a little-endian value in a byte buffer. |
|
| 196 | + | fn encode(buffer: &mut [u8], offset: u32, value: u64, width: u32) { |
|
| 197 | + | for i in 0..width { |
|
| 198 | + | set buffer[offset + i] = (value >> (i as u64 * 8)) as u8; |
|
| 199 | + | } |
|
| 200 | + | } |
|
| 201 | + | ||
| 202 | + | /// Load a little-endian value from a byte buffer. |
|
| 203 | + | fn decode(buffer: &[u8], offset: u32, width: u32) -> u64 { |
|
| 204 | + | let mut value: u64 = 0; |
|
| 205 | + | for i in 0..width { |
|
| 206 | + | set value |= buffer[offset + i] as u64 << (i as u64 * 8); |
|
| 207 | + | } |
|
| 208 | + | return value; |
|
| 209 | + | } |
|
| 210 | + | ||
| 211 | + | /// Store one little-endian 32-bit word. The address needs no alignment. |
|
| 212 | + | export fn write32At(access: &Access, offset: u64, value: u32) throws (abi::Error) { |
|
| 213 | + | let mut bytes: [u8; 4] = undefined; |
|
| 214 | + | encode(&mut bytes[..], 0, value as u64, 4); |
|
| 215 | + | try writeAt(access, offset, &bytes[..]); |
|
| 216 | + | } |
|
| 217 | + | ||
| 218 | + | /// Load one little-endian 64-bit word. The address needs no alignment. |
|
| 219 | + | export fn read64At(access: &Access, offset: u64) -> u64 throws (abi::Error) { |
|
| 220 | + | let mut bytes: [u8; 8] = undefined; |
|
| 221 | + | try readAt(access, offset, &mut bytes[..]); |
|
| 222 | + | return decode(&bytes[..], 0, 8); |
|
| 223 | + | } |
|
| 224 | + | ||
| 225 | + | /// Store consecutive little-endian 64-bit words. The address needs no alignment. |
|
| 226 | + | export fn writeWordsAt(access: &Access, offset: u64, words: &[u64]) throws (abi::Error) { |
|
| 227 | + | let checked = try within(access, offset, words.len as u64 * 8); |
|
| 228 | + | let mut bytes: [u8; CHUNK] = undefined; |
|
| 229 | + | let mut done: u32 = 0; |
|
| 230 | + | while done < words.len { |
|
| 231 | + | let mut count = words.len - done; |
|
| 232 | + | if count > CHUNK / 8 { |
|
| 233 | + | set count = CHUNK / 8; |
|
| 234 | + | } |
|
| 235 | + | for i in 0..count { |
|
| 236 | + | encode(&mut bytes[..], i * 8, words[done + i], 8); |
|
| 237 | + | } |
|
| 238 | + | try! writeAt(access, offset + done as u64 * 8, &bytes[..count * 8]); |
|
| 239 | + | set done += count; |
|
| 240 | + | } |
|
| 241 | + | } |
|
| 242 | + | ||
| 243 | + | /// Load consecutive little-endian 64-bit words. The address needs no alignment. |
|
| 244 | + | export fn readWordsAt(access: &Access, offset: u64, words: &mut [u64]) throws (abi::Error) { |
|
| 245 | + | let checked = try within(access, offset, words.len as u64 * 8); |
|
| 246 | + | let mut bytes: [u8; CHUNK] = undefined; |
|
| 247 | + | let mut done: u32 = 0; |
|
| 248 | + | while done < words.len { |
|
| 249 | + | let mut count = words.len - done; |
|
| 250 | + | if count > CHUNK / 8 { |
|
| 251 | + | set count = CHUNK / 8; |
|
| 252 | + | } |
|
| 253 | + | try! readAt(access, offset + done as u64 * 8, &mut bytes[..count * 8]); |
|
| 254 | + | for i in 0..count { |
|
| 255 | + | set words[done + i] = decode(&bytes[..], i * 8, 8); |
|
| 256 | + | } |
|
| 257 | + | set done += count; |
|
| 258 | + | } |
|
| 259 | + | } |
|
| 260 | + | ||
| 261 | + | /// Store one little-endian 64-bit value in count consecutive words. |
|
| 262 | + | /// The address needs no alignment. |
|
| 263 | + | export fn fillWordsAt(access: &Access, offset: u64, value: u64, count: u32) throws (abi::Error) { |
|
| 264 | + | let checked = try within(access, offset, count as u64 * 8); |
|
| 265 | + | let mut bytes: [u8; CHUNK] = undefined; |
|
| 266 | + | for i in 0..CHUNK / 8 { |
|
| 267 | + | encode(&mut bytes[..], i * 8, value, 8); |
|
| 268 | + | } |
|
| 269 | + | let mut done: u32 = 0; |
|
| 270 | + | while done < count { |
|
| 271 | + | let mut chunk = count - done; |
|
| 272 | + | if chunk > CHUNK / 8 { |
|
| 273 | + | set chunk = CHUNK / 8; |
|
| 274 | + | } |
|
| 275 | + | try! writeAt(access, offset + done as u64 * 8, &bytes[..chunk * 8]); |
|
| 276 | + | set done += chunk; |
|
| 277 | + | } |
|
| 278 | + | } |
|
| 279 | + | ||
| 280 | + | /// Pool-checked clear. Hold the metadata lock. |
|
| 281 | + | export fn zero(pool: &frames::Pool, address: u64, length: u64) throws (abi::Error) { |
|
| 282 | + | let token = try access(pool, address, length); |
|
| 283 | + | try zeroAt(&token, 0, length); |
|
| 284 | + | } |
|
| 285 | + | ||
| 286 | + | /// Pool-checked copy in. Hold the metadata lock. |
|
| 287 | + | export fn write(pool: &frames::Pool, address: u64, source: &[u8]) throws (abi::Error) { |
|
| 288 | + | let token = try access(pool, address, source.len as u64); |
|
| 289 | + | try writeAt(&token, 0, source); |
|
| 290 | + | } |
|
| 291 | + | ||
| 292 | + | /// Pool-checked copy out. Hold the metadata lock. |
|
| 293 | + | export fn read(pool: &frames::Pool, address: u64, target: &mut [u8]) throws (abi::Error) { |
|
| 294 | + | let token = try access(pool, address, target.len as u64); |
|
| 295 | + | try readAt(&token, 0, target); |
|
| 296 | + | } |
|
| 297 | + | ||
| 298 | + | /// Pool-checked 32-bit store. Hold the metadata lock. |
|
| 299 | + | export fn write32(pool: &frames::Pool, address: u64, value: u32) throws (abi::Error) { |
|
| 300 | + | let token = try access(pool, address, 4); |
|
| 301 | + | try write32At(&token, 0, value); |
|
| 302 | + | } |
|
| 303 | + | ||
| 304 | + | /// Pool-checked word stores. Hold the metadata lock. |
|
| 305 | + | export fn writeWords(pool: &frames::Pool, address: u64, words: &[u64]) throws (abi::Error) { |
|
| 306 | + | let token = try access(pool, address, words.len as u64 * 8); |
|
| 307 | + | try writeWordsAt(&token, 0, words); |
|
| 308 | + | } |
|
| 309 | + | ||
| 310 | + | /// Pool-checked word loads. Hold the metadata lock. |
|
| 311 | + | export fn readWords(pool: &frames::Pool, address: u64, words: &mut [u64]) throws (abi::Error) { |
|
| 312 | + | let token = try access(pool, address, words.len as u64 * 8); |
|
| 313 | + | try readWordsAt(&token, 0, words); |
|
| 314 | + | } |
|
| 315 | + | ||
| 316 | + | /// Pool-checked word fill. Hold the metadata lock. |
|
| 317 | + | export fn fillWords(pool: &frames::Pool, address: u64, value: u64, count: u32) throws (abi::Error) { |
|
| 318 | + | let token = try access(pool, address, count as u64 * 8); |
|
| 319 | + | try fillWordsAt(&token, 0, value, count); |
|
| 320 | + | } |
kernel/kernel/physical.ras
added
+8 -0
| 1 | + | //! Physical address to pointer conversion for the checked memory boundary. |
|
| 2 | + | .text; |
|
| 3 | + | .export @kernel::physical::pointer; |
|
| 4 | + | ||
| 5 | + | // The identity map covers RAM. physical.rad calls this only after checking the |
|
| 6 | + | // target range against allocated frames. |
|
| 7 | + | @kernel::physical::pointer |
|
| 8 | + | ret; |
kernel/kernel/platform.rad
added
+593 -0
| 1 | + | //! Fixed platform resources and reserved-memory exclusion. |
|
| 2 | + | ||
| 3 | + | use super::fdt; |
|
| 4 | + | use super::range; |
|
| 5 | + | use super::limits; |
|
| 6 | + | use std::arch::rv64; |
|
| 7 | + | ||
| 8 | + | /// Required CLINT register window size in bytes. |
|
| 9 | + | export constant CLINT_SIZE: u64 = 0xc000; |
|
| 10 | + | /// CLINT machine time register offset. |
|
| 11 | + | export constant CLINT_CLOCK: u64 = 0xbff8; |
|
| 12 | + | /// First CLINT machine timer compare register offset. |
|
| 13 | + | export constant CLINT_COMPARE: u64 = 0x4000; |
|
| 14 | + | /// Byte stride between CLINT timer compare registers. |
|
| 15 | + | export constant CLINT_COMPARE_STRIDE: u64 = 8; |
|
| 16 | + | /// Byte stride between CLINT software interrupt registers. |
|
| 17 | + | export constant CLINT_SOFTWARE_STRIDE: u64 = 4; |
|
| 18 | + | /// First PLIC context enable bitmap offset. |
|
| 19 | + | export constant PLIC_ENABLES: u64 = 0x2000; |
|
| 20 | + | /// Byte stride between PLIC context enable bitmaps. |
|
| 21 | + | export constant PLIC_ENABLE_STRIDE: u64 = 0x80; |
|
| 22 | + | /// First PLIC context threshold register offset. |
|
| 23 | + | export constant PLIC_CONTEXTS: u64 = 0x200000; |
|
| 24 | + | /// Byte stride between PLIC context register groups. |
|
| 25 | + | export constant PLIC_CONTEXT_STRIDE: u64 = 0x1000; |
|
| 26 | + | /// Claim register offset within a PLIC context. |
|
| 27 | + | export constant PLIC_CLAIM: u64 = 4; |
|
| 28 | + | /// Bytes through a PLIC context claim register. |
|
| 29 | + | export constant PLIC_CONTEXT_SIZE: u64 = PLIC_CLAIM + 4; |
|
| 30 | + | /// Device-tree marker for an unconnected interrupt context. |
|
| 31 | + | constant IRQ_DISCONNECTED: u64 = 0xffffffff; |
|
| 32 | + | /// Bytes in a boot stack tuple with two 64-bit addresses. |
|
| 33 | + | constant STACK_TUPLE_SIZE: u32 = 16; |
|
| 34 | + | ||
| 35 | + | /// Maximum RAM banks in the platform description. |
|
| 36 | + | export constant RAM_BANKS: u32 = 16; |
|
| 37 | + | /// Maximum firmware, image, stack, and device reservations. |
|
| 38 | + | export constant RESERVATIONS: u32 = 64; |
|
| 39 | + | ||
| 40 | + | /// Platform extraction failure. |
|
| 41 | + | export union Error: Copy { |
|
| 42 | + | /// Malformed flattened tree. |
|
| 43 | + | Tree(fdt::Error), |
|
| 44 | + | /// Unsupported or inconsistent platform resource. |
|
| 45 | + | Invalid, |
|
| 46 | + | /// The platform exceeds fixed storage. |
|
| 47 | + | Capacity, |
|
| 48 | + | } |
|
| 49 | + | ||
| 50 | + | /// Device mechanism selected from its compatible property. |
|
| 51 | + | export union Kind: Copy { |
|
| 52 | + | /// Byte-wide 16550 UART. |
|
| 53 | + | Uart, |
|
| 54 | + | /// Per-hart machine timers and software interrupts. |
|
| 55 | + | Clint, |
|
| 56 | + | /// Platform interrupt controller. |
|
| 57 | + | Plic, |
|
| 58 | + | /// Emulator termination register. |
|
| 59 | + | Finish, |
|
| 60 | + | } |
|
| 61 | + | ||
| 62 | + | /// A supported memory-mapped device. |
|
| 63 | + | export record Device: Copy { |
|
| 64 | + | /// Hardware mechanism. |
|
| 65 | + | kind: Kind, |
|
| 66 | + | /// Physical register extent. |
|
| 67 | + | memory: range::Range, |
|
| 68 | + | } |
|
| 69 | + | ||
| 70 | + | /// Validated boot resources. Only array elements below their counts are live. |
|
| 71 | + | export record Platform: Copy { |
|
| 72 | + | /// RAM bank ranges. |
|
| 73 | + | ram: [range::Range; RAM_BANKS], |
|
| 74 | + | /// Number of RAM banks. |
|
| 75 | + | ramCount: u32, |
|
| 76 | + | /// Memory excluded from general allocation. |
|
| 77 | + | reserved: [range::Range; RESERVATIONS], |
|
| 78 | + | /// Number of reserved ranges. |
|
| 79 | + | reservedCount: u32, |
|
| 80 | + | /// Supported MMIO devices in tree order. |
|
| 81 | + | devices: [Device; limits::DEVICES], |
|
| 82 | + | /// Number of MMIO devices. |
|
| 83 | + | deviceCount: u32, |
|
| 84 | + | /// Bit mask of online physical hart IDs. |
|
| 85 | + | harts: u32, |
|
| 86 | + | /// PLIC source count, or zero when no supported controller is present. |
|
| 87 | + | irqSources: u32, |
|
| 88 | + | /// Machine-mode PLIC context index for each online hart. |
|
| 89 | + | irqContexts: [?u32; limits::HARTS], |
|
| 90 | + | /// Timer ticks per second. |
|
| 91 | + | timebase: u32, |
|
| 92 | + | /// Kernel stack ranges, indexed by physical hart ID. |
|
| 93 | + | stacks: [range::Range; limits::HARTS], |
|
| 94 | + | /// Number of stack ranges in the boot contract. |
|
| 95 | + | stackCount: u32, |
|
| 96 | + | } |
|
| 97 | + | ||
| 98 | + | /// Properties retained until a node closes. |
|
| 99 | + | record Node: Copy { |
|
| 100 | + | /// Node name in the input blob. |
|
| 101 | + | name: fdt::Span, |
|
| 102 | + | /// Parent bus address-cell count. |
|
| 103 | + | addressCells: u32, |
|
| 104 | + | /// Parent bus size-cell count. |
|
| 105 | + | sizeCells: u32, |
|
| 106 | + | /// Address-cell count for child nodes. |
|
| 107 | + | childAddress: u32, |
|
| 108 | + | /// Size-cell count for child nodes. |
|
| 109 | + | childSize: u32, |
|
| 110 | + | /// Register tuples, if present. |
|
| 111 | + | registers: ?fdt::Span, |
|
| 112 | + | /// Device compatibility strings, if present. |
|
| 113 | + | compatible: ?fdt::Span, |
|
| 114 | + | /// Device type string, if present. |
|
| 115 | + | deviceType: ?fdt::Span, |
|
| 116 | + | /// Interrupt-controller reference identity, or zero when absent. |
|
| 117 | + | phandle: u32, |
|
| 118 | + | /// Cells in one interrupt specifier. |
|
| 119 | + | interruptCells: u32, |
|
| 120 | + | /// PLIC source count, or zero when absent. |
|
| 121 | + | sources: u32, |
|
| 122 | + | /// PLIC context references and local interrupt numbers. |
|
| 123 | + | interrupts: ?fdt::Span, |
|
| 124 | + | /// Node status permits use. |
|
| 125 | + | enabled: bool, |
|
| 126 | + | /// This node or its parent is the reserved-memory container. |
|
| 127 | + | reserved: bool, |
|
| 128 | + | } |
|
| 129 | + | ||
| 130 | + | /// Read a CPU node's bounded physical hart identifier. |
|
| 131 | + | fn cpu(bytes: &[u8], node: Node) -> u32 throws (Error) { |
|
| 132 | + | let registers = node.registers else { |
|
| 133 | + | throw Error::Invalid; |
|
| 134 | + | }; |
|
| 135 | + | if node.sizeCells <> 0 |
|
| 136 | + | or node.addressCells < 1 |
|
| 137 | + | or node.addressCells > 2 |
|
| 138 | + | or registers.end - registers.start <> node.addressCells * 4 |
|
| 139 | + | { |
|
| 140 | + | throw Error::Invalid; |
|
| 141 | + | } |
|
| 142 | + | let hart = try fdt::integer(bytes, registers.start, node.addressCells * 4) catch err { |
|
| 143 | + | throw Error::Tree(err); |
|
| 144 | + | }; |
|
| 145 | + | if hart >= limits::HARTS as u64 { |
|
| 146 | + | throw Error::Invalid; |
|
| 147 | + | } |
|
| 148 | + | return hart as u32; |
|
| 149 | + | } |
|
| 150 | + | ||
| 151 | + | /// Associate a CPU-local interrupt controller with its parent hart. |
|
| 152 | + | fn controller(bytes: &[u8], node: Node, parent: Node, references: &mut [u32]) throws (Error) { |
|
| 153 | + | let kind = parent.deviceType else { |
|
| 154 | + | throw Error::Invalid; |
|
| 155 | + | }; |
|
| 156 | + | if not string(bytes, kind, &"cpu"[..]) or node.phandle == 0 or node.interruptCells <> 1 { |
|
| 157 | + | throw Error::Invalid; |
|
| 158 | + | } |
|
| 159 | + | let hart = try cpu(bytes, parent); |
|
| 160 | + | if references[hart] <> 0 { |
|
| 161 | + | throw Error::Invalid; |
|
| 162 | + | } |
|
| 163 | + | for reference in &references[..] { |
|
| 164 | + | if reference == node.phandle { |
|
| 165 | + | throw Error::Invalid; |
|
| 166 | + | } |
|
| 167 | + | } |
|
| 168 | + | set references[hart] = node.phandle; |
|
| 169 | + | } |
|
| 170 | + | ||
| 171 | + | /// Resolve PLIC context ordinals after all CPU controller references are known. |
|
| 172 | + | fn plic(bytes: &[u8], node: Node, references: &[u32], platform: &mut Platform) throws (Error) { |
|
| 173 | + | if node.sources == 0 or node.sources > limits::INTERRUPTS or node.interruptCells <> 1 { |
|
| 174 | + | throw Error::Invalid; |
|
| 175 | + | } |
|
| 176 | + | let contexts = node.interrupts else { |
|
| 177 | + | throw Error::Invalid; |
|
| 178 | + | }; |
|
| 179 | + | let length = contexts.end - contexts.start; |
|
| 180 | + | if length == 0 or length % 8 <> 0 or length / 8 > limits::HARTS * 2 { |
|
| 181 | + | throw Error::Invalid; |
|
| 182 | + | } |
|
| 183 | + | let registers = node.registers else { |
|
| 184 | + | throw Error::Invalid; |
|
| 185 | + | }; |
|
| 186 | + | if registers.end - registers.start <> (node.addressCells + node.sizeCells) * 4 { |
|
| 187 | + | throw Error::Invalid; |
|
| 188 | + | } |
|
| 189 | + | let memory = try extent(bytes, registers.start, node.addressCells, node.sizeCells); |
|
| 190 | + | let required = PLIC_CONTEXTS |
|
| 191 | + | + PLIC_CONTEXT_SIZE |
|
| 192 | + | + (length as u64 / 8 - 1) * PLIC_CONTEXT_STRIDE; |
|
| 193 | + | if (memory.start & 3) <> 0 or memory.end - memory.start < required { |
|
| 194 | + | throw Error::Invalid; |
|
| 195 | + | } |
|
| 196 | + | for index in 0..length / 8 { |
|
| 197 | + | let offset = contexts.start + index * 8; |
|
| 198 | + | let reference = try fdt::integer(bytes, offset, 4) catch err { |
|
| 199 | + | throw Error::Tree(err); |
|
| 200 | + | }; |
|
| 201 | + | let cause = try fdt::integer(bytes, offset + 4, 4) catch err { |
|
| 202 | + | throw Error::Tree(err); |
|
| 203 | + | }; |
|
| 204 | + | let mut hart: ?u32 = nil; |
|
| 205 | + | for id in 0..limits::HARTS { |
|
| 206 | + | if references[id] <> 0 and references[id] as u64 == reference { |
|
| 207 | + | set hart = id; |
|
| 208 | + | } |
|
| 209 | + | } |
|
| 210 | + | let id = hart else { |
|
| 211 | + | throw Error::Invalid; |
|
| 212 | + | }; |
|
| 213 | + | if cause == rv64::IRQ_SUPERVISOR_EXTERNAL or cause == IRQ_DISCONNECTED { |
|
| 214 | + | continue; |
|
| 215 | + | } |
|
| 216 | + | if cause <> rv64::IRQ_MACHINE_EXTERNAL { |
|
| 217 | + | throw Error::Invalid; |
|
| 218 | + | } |
|
| 219 | + | if (platform.harts & (1 << id)) == 0 { |
|
| 220 | + | continue; |
|
| 221 | + | } |
|
| 222 | + | if platform.irqContexts[id] <> nil { |
|
| 223 | + | throw Error::Invalid; |
|
| 224 | + | } |
|
| 225 | + | set platform.irqContexts[id] = index; |
|
| 226 | + | } |
|
| 227 | + | for hart in 0..limits::HARTS { |
|
| 228 | + | if (platform.harts & (1 << hart)) <> 0 and platform.irqContexts[hart] == nil { |
|
| 229 | + | throw Error::Invalid; |
|
| 230 | + | } |
|
| 231 | + | } |
|
| 232 | + | set platform.irqSources = node.sources; |
|
| 233 | + | } |
|
| 234 | + | ||
| 235 | + | /// Compare a complete byte span with a name. |
|
| 236 | + | fn equal(bytes: &[u8], span: fdt::Span, value: &[u8]) -> bool { |
|
| 237 | + | if span.end - span.start <> value.len { |
|
| 238 | + | return false; |
|
| 239 | + | } |
|
| 240 | + | for i in 0..value.len { |
|
| 241 | + | if bytes[span.start + i] <> value[i] { |
|
| 242 | + | return false; |
|
| 243 | + | } |
|
| 244 | + | } |
|
| 245 | + | return true; |
|
| 246 | + | } |
|
| 247 | + | ||
| 248 | + | /// Find an exact string in a zero-terminated property string list. |
|
| 249 | + | fn string(bytes: &[u8], span: fdt::Span, value: &[u8]) -> bool { |
|
| 250 | + | let mut start = span.start; |
|
| 251 | + | for i in span.start..span.end { |
|
| 252 | + | if bytes[i] == 0 { |
|
| 253 | + | if equal(bytes, fdt::Span { start, end: i }, value) { |
|
| 254 | + | return true; |
|
| 255 | + | } |
|
| 256 | + | set start = i + 1; |
|
| 257 | + | } |
|
| 258 | + | } |
|
| 259 | + | return false; |
|
| 260 | + | } |
|
| 261 | + | ||
| 262 | + | /// Read a cell count supported by the native 64-bit platform. |
|
| 263 | + | fn scalar(bytes: &[u8], value: fdt::Span) -> u32 throws (Error) { |
|
| 264 | + | if value.end - value.start <> 4 { |
|
| 265 | + | throw Error::Invalid; |
|
| 266 | + | } |
|
| 267 | + | let result = try fdt::integer(bytes, value.start, 4) catch err { |
|
| 268 | + | throw Error::Tree(err); |
|
| 269 | + | }; |
|
| 270 | + | return result as u32; |
|
| 271 | + | } |
|
| 272 | + | ||
| 273 | + | /// Read a nonempty physical range from one register tuple. |
|
| 274 | + | fn extent(bytes: &[u8], at: u32, addressCells: u32, sizeCells: u32) -> range::Range throws (Error) { |
|
| 275 | + | if addressCells < 1 or addressCells > 2 or sizeCells < 1 or sizeCells > 2 { |
|
| 276 | + | throw Error::Invalid; |
|
| 277 | + | } |
|
| 278 | + | let address = try fdt::integer(bytes, at, addressCells * 4) catch err { |
|
| 279 | + | throw Error::Tree(err); |
|
| 280 | + | }; |
|
| 281 | + | let size = try fdt::integer(bytes, at + addressCells * 4, sizeCells * 4) catch err { |
|
| 282 | + | throw Error::Tree(err); |
|
| 283 | + | }; |
|
| 284 | + | let result = range::new(address, size) else { |
|
| 285 | + | throw Error::Invalid; |
|
| 286 | + | }; |
|
| 287 | + | return result; |
|
| 288 | + | } |
|
| 289 | + | ||
| 290 | + | /// Append a reservation. Identical entries from both FDT representations share one slot. |
|
| 291 | + | export fn protect(platform: &mut Platform, value: range::Range) throws (Error) { |
|
| 292 | + | for i in 0..platform.reservedCount { |
|
| 293 | + | if platform.reserved[i] == value { |
|
| 294 | + | return; |
|
| 295 | + | } |
|
| 296 | + | } |
|
| 297 | + | if platform.reservedCount == RESERVATIONS { |
|
| 298 | + | throw Error::Capacity; |
|
| 299 | + | } |
|
| 300 | + | set platform.reserved[platform.reservedCount] = value; |
|
| 301 | + | set platform.reservedCount += 1; |
|
| 302 | + | } |
|
| 303 | + | ||
| 304 | + | /// Test a physical extent for overlap. |
|
| 305 | + | fn overlaps(left: range::Range, right: range::Range) -> bool { |
|
| 306 | + | return left.start < right.end and right.start < left.end; |
|
| 307 | + | } |
|
| 308 | + | ||
| 309 | + | /// Test whether one complete extent belongs to a RAM bank. |
|
| 310 | + | export fn inRam(platform: &Platform, value: range::Range) -> bool { |
|
| 311 | + | for i in 0..platform.ramCount { |
|
| 312 | + | if range::contains(platform.ram[i], value) { |
|
| 313 | + | return true; |
|
| 314 | + | } |
|
| 315 | + | } |
|
| 316 | + | return false; |
|
| 317 | + | } |
|
| 318 | + | ||
| 319 | + | /// Check whether a nonempty physical extent is RAM with no reserved byte. |
|
| 320 | + | export fn available(platform: &Platform, value: range::Range) -> bool { |
|
| 321 | + | if not inRam(platform, value) { |
|
| 322 | + | return false; |
|
| 323 | + | } |
|
| 324 | + | for i in 0..platform.reservedCount { |
|
| 325 | + | if overlaps(platform.reserved[i], value) { |
|
| 326 | + | return false; |
|
| 327 | + | } |
|
| 328 | + | } |
|
| 329 | + | return true; |
|
| 330 | + | } |
|
| 331 | + | ||
| 332 | + | /// Record resources only after all properties of their node are known. |
|
| 333 | + | fn finish(bytes: &[u8], node: Node, platform: &mut Platform) throws (Error) { |
|
| 334 | + | if not node.enabled { |
|
| 335 | + | return; |
|
| 336 | + | } |
|
| 337 | + | let registers = node.registers else { |
|
| 338 | + | return; |
|
| 339 | + | }; |
|
| 340 | + | if let typeName = node.deviceType { |
|
| 341 | + | if string(bytes, typeName, &"cpu"[..]) { |
|
| 342 | + | let hart = try cpu(bytes, node); |
|
| 343 | + | if (platform.harts & (1 << hart)) <> 0 { |
|
| 344 | + | throw Error::Invalid; |
|
| 345 | + | } |
|
| 346 | + | set platform.harts |= 1 << hart; |
|
| 347 | + | return; |
|
| 348 | + | } |
|
| 349 | + | } |
|
| 350 | + | let width = (node.addressCells + node.sizeCells) * 4; |
|
| 351 | + | if width == 0 or (registers.end - registers.start) % width <> 0 { |
|
| 352 | + | throw Error::Invalid; |
|
| 353 | + | } |
|
| 354 | + | let mut at = registers.start; |
|
| 355 | + | while at < registers.end { |
|
| 356 | + | let value = try extent(bytes, at, node.addressCells, node.sizeCells); |
|
| 357 | + | if node.reserved { |
|
| 358 | + | try protect(platform, value); |
|
| 359 | + | } else { |
|
| 360 | + | let mut memory = false; |
|
| 361 | + | if let typeName = node.deviceType { |
|
| 362 | + | set memory = string(bytes, typeName, &"memory"[..]); |
|
| 363 | + | } |
|
| 364 | + | if memory { |
|
| 365 | + | if platform.ramCount == RAM_BANKS { |
|
| 366 | + | throw Error::Capacity; |
|
| 367 | + | } |
|
| 368 | + | for i in 0..platform.ramCount { |
|
| 369 | + | if overlaps(platform.ram[i], value) { |
|
| 370 | + | throw Error::Invalid; |
|
| 371 | + | } |
|
| 372 | + | } |
|
| 373 | + | set platform.ram[platform.ramCount] = value; |
|
| 374 | + | set platform.ramCount += 1; |
|
| 375 | + | } else { |
|
| 376 | + | let mut kind: ?Kind = nil; |
|
| 377 | + | if let names = node.compatible { |
|
| 378 | + | if string(bytes, names, &"ns16550a"[..]) { |
|
| 379 | + | set kind = Kind::Uart; |
|
| 380 | + | } else if string(bytes, names, &"riscv,clint0"[..]) { |
|
| 381 | + | set kind = Kind::Clint; |
|
| 382 | + | } else if string(bytes, names, &"riscv,plic0"[..]) { |
|
| 383 | + | set kind = Kind::Plic; |
|
| 384 | + | } else if string(bytes, names, &"radiant,finish"[..]) { |
|
| 385 | + | set kind = Kind::Finish; |
|
| 386 | + | } |
|
| 387 | + | } |
|
| 388 | + | if let deviceKind = kind { |
|
| 389 | + | if platform.deviceCount == limits::DEVICES { |
|
| 390 | + | throw Error::Capacity; |
|
| 391 | + | } |
|
| 392 | + | set platform.devices[platform.deviceCount] = Device { |
|
| 393 | + | kind: deviceKind, memory: value |
|
| 394 | + | }; |
|
| 395 | + | set platform.deviceCount += 1; |
|
| 396 | + | } |
|
| 397 | + | try protect(platform, value); |
|
| 398 | + | } |
|
| 399 | + | } |
|
| 400 | + | set at += width; |
|
| 401 | + | } |
|
| 402 | + | } |
|
| 403 | + | ||
| 404 | + | /// Extract native platform resources. Discard the output if decoding fails. |
|
| 405 | + | export fn decode(bytes: &[u8], platform: &mut Platform) throws (Error) { |
|
| 406 | + | set platform.ramCount = 0; |
|
| 407 | + | set platform.reservedCount = 0; |
|
| 408 | + | set platform.deviceCount = 0; |
|
| 409 | + | set platform.harts = 0; |
|
| 410 | + | set platform.timebase = 0; |
|
| 411 | + | set platform.stackCount = 0; |
|
| 412 | + | set platform.irqSources = 0; |
|
| 413 | + | for hart in 0..limits::HARTS { |
|
| 414 | + | set platform.irqContexts[hart] = nil; |
|
| 415 | + | } |
|
| 416 | + | let mut references: [u32; limits::HARTS] = [0; limits::HARTS]; |
|
| 417 | + | let mut interruptController: ?Node = nil; |
|
| 418 | + | let tree = try fdt::header(bytes) catch err { |
|
| 419 | + | throw Error::Tree(err); |
|
| 420 | + | }; |
|
| 421 | + | let mut at = tree.reservations.start; |
|
| 422 | + | while at < tree.reservations.end { |
|
| 423 | + | try protect(platform, try extent(bytes, at, 2, 2)); |
|
| 424 | + | set at += fdt::RESERVATION_SIZE; |
|
| 425 | + | } |
|
| 426 | + | let blank = Node { |
|
| 427 | + | name: fdt::Span { start: 0, end: 0 }, |
|
| 428 | + | addressCells: 2, |
|
| 429 | + | sizeCells: 1, |
|
| 430 | + | childAddress: 2, |
|
| 431 | + | childSize: 1, |
|
| 432 | + | registers: nil, |
|
| 433 | + | compatible: nil, |
|
| 434 | + | deviceType: nil, |
|
| 435 | + | phandle: 0, |
|
| 436 | + | interruptCells: 0, |
|
| 437 | + | sources: 0, |
|
| 438 | + | interrupts: nil, |
|
| 439 | + | enabled: true, |
|
| 440 | + | reserved: false, |
|
| 441 | + | }; |
|
| 442 | + | let mut nodes = [blank; fdt::MAX_DEPTH]; |
|
| 443 | + | let mut state = fdt::cursor(tree); |
|
| 444 | + | loop { |
|
| 445 | + | let event = try fdt::next(bytes, tree, &mut state) catch err { |
|
| 446 | + | throw Error::Tree(err); |
|
| 447 | + | }; |
|
| 448 | + | match event { |
|
| 449 | + | case fdt::Event::Begin(name) => { |
|
| 450 | + | let mut addressCells: u32 = 2; |
|
| 451 | + | let mut sizeCells: u32 = 1; |
|
| 452 | + | let mut reserved = false; |
|
| 453 | + | let mut enabled = true; |
|
| 454 | + | if state.depth > 1 { |
|
| 455 | + | let parent = nodes[state.depth - 2]; |
|
| 456 | + | set addressCells = parent.childAddress; |
|
| 457 | + | set sizeCells = parent.childSize; |
|
| 458 | + | set reserved = parent.reserved; |
|
| 459 | + | set enabled = parent.enabled; |
|
| 460 | + | } |
|
| 461 | + | if state.depth == 2 and equal(bytes, name, &"reserved-memory"[..]) { |
|
| 462 | + | set reserved = true; |
|
| 463 | + | } |
|
| 464 | + | set nodes[state.depth - 1] = Node { |
|
| 465 | + | name, |
|
| 466 | + | addressCells, |
|
| 467 | + | sizeCells, |
|
| 468 | + | childAddress: 2, |
|
| 469 | + | childSize: 1, |
|
| 470 | + | registers: nil, |
|
| 471 | + | compatible: nil, |
|
| 472 | + | deviceType: nil, |
|
| 473 | + | phandle: 0, |
|
| 474 | + | interruptCells: 0, |
|
| 475 | + | sources: 0, |
|
| 476 | + | interrupts: nil, |
|
| 477 | + | enabled, |
|
| 478 | + | reserved, |
|
| 479 | + | }; |
|
| 480 | + | } |
|
| 481 | + | case fdt::Event::Property { name, value } => { |
|
| 482 | + | if equal(bytes, name, &"#address-cells"[..]) { |
|
| 483 | + | let count = try scalar(bytes, value); |
|
| 484 | + | if count > 2 { |
|
| 485 | + | throw Error::Invalid; |
|
| 486 | + | } |
|
| 487 | + | set nodes[state.depth - 1].childAddress = count; |
|
| 488 | + | } else if equal(bytes, name, &"#size-cells"[..]) { |
|
| 489 | + | let count = try scalar(bytes, value); |
|
| 490 | + | if count > 2 { |
|
| 491 | + | throw Error::Invalid; |
|
| 492 | + | } |
|
| 493 | + | set nodes[state.depth - 1].childSize = count; |
|
| 494 | + | } else if equal(bytes, name, &"reg"[..]) { |
|
| 495 | + | set nodes[state.depth - 1].registers = value; |
|
| 496 | + | } else if equal(bytes, name, &"compatible"[..]) { |
|
| 497 | + | set nodes[state.depth - 1].compatible = value; |
|
| 498 | + | } else if equal(bytes, name, &"device_type"[..]) { |
|
| 499 | + | set nodes[state.depth - 1].deviceType = value; |
|
| 500 | + | } else if equal(bytes, name, &"phandle"[..]) { |
|
| 501 | + | set nodes[state.depth - 1].phandle = try scalar(bytes, value); |
|
| 502 | + | } else if equal(bytes, name, &"#interrupt-cells"[..]) { |
|
| 503 | + | set nodes[state.depth - 1].interruptCells = try scalar(bytes, value); |
|
| 504 | + | } else if equal(bytes, name, &"riscv,ndev"[..]) { |
|
| 505 | + | set nodes[state.depth - 1].sources = try scalar(bytes, value); |
|
| 506 | + | } else if equal(bytes, name, &"interrupts-extended"[..]) { |
|
| 507 | + | set nodes[state.depth - 1].interrupts = value; |
|
| 508 | + | } else if equal(bytes, name, &"status"[..]) { |
|
| 509 | + | set nodes[state.depth - 1].enabled = nodes[state.depth - 1].enabled |
|
| 510 | + | and (string(bytes, value, &"okay"[..]) or string(bytes, value, &"ok"[..])); |
|
| 511 | + | } else if equal(bytes, name, &"ranges"[..]) and value.start <> value.end { |
|
| 512 | + | throw Error::Invalid; |
|
| 513 | + | } else if state.depth == 2 |
|
| 514 | + | and equal(bytes, nodes[state.depth - 1].name, &"cpus"[..]) |
|
| 515 | + | and equal(bytes, name, &"timebase-frequency"[..]) |
|
| 516 | + | { |
|
| 517 | + | set platform.timebase = try scalar(bytes, value); |
|
| 518 | + | } else if state.depth == 2 |
|
| 519 | + | and equal( |
|
| 520 | + | bytes, nodes[state.depth - 1].name, &"chosen"[..] |
|
| 521 | + | ) |
|
| 522 | + | and equal(bytes, name, &"radiance,kernel-stacks"[..]) |
|
| 523 | + | { |
|
| 524 | + | let size = value.end - value.start; |
|
| 525 | + | if size % STACK_TUPLE_SIZE <> 0 |
|
| 526 | + | or size / STACK_TUPLE_SIZE > limits::HARTS |
|
| 527 | + | { |
|
| 528 | + | throw Error::Invalid; |
|
| 529 | + | } |
|
| 530 | + | set platform.stackCount = size / STACK_TUPLE_SIZE; |
|
| 531 | + | for i in 0..platform.stackCount { |
|
| 532 | + | let stack = try extent( |
|
| 533 | + | bytes, |
|
| 534 | + | value.start |
|
| 535 | + | + i * STACK_TUPLE_SIZE, |
|
| 536 | + | 2, |
|
| 537 | + | 2 |
|
| 538 | + | ); |
|
| 539 | + | if (stack.start & (rv64::STACK_ALIGNMENT as u64 - 1)) |
|
| 540 | + | <> 0 or (stack.end & (rv64::STACK_ALIGNMENT as u64 |
|
| 541 | + | - 1)) <> 0 |
|
| 542 | + | { |
|
| 543 | + | throw Error::Invalid; |
|
| 544 | + | } |
|
| 545 | + | set platform.stacks[i] = stack; |
|
| 546 | + | try protect(platform, stack); |
|
| 547 | + | } |
|
| 548 | + | } |
|
| 549 | + | } |
|
| 550 | + | case fdt::Event::EndNode => { |
|
| 551 | + | let node = nodes[state.depth]; |
|
| 552 | + | if let compatible = node.compatible { |
|
| 553 | + | if string(bytes, compatible, &"riscv,cpu-intc"[..]) { |
|
| 554 | + | if state.depth == 0 { |
|
| 555 | + | throw Error::Invalid; |
|
| 556 | + | } |
|
| 557 | + | try controller(bytes, node, nodes[state.depth - 1], &mut references[..]); |
|
| 558 | + | } |
|
| 559 | + | if node.enabled and string(bytes, compatible, &"riscv,plic0"[..]) { |
|
| 560 | + | if interruptController <> nil { |
|
| 561 | + | throw Error::Invalid; |
|
| 562 | + | } |
|
| 563 | + | set interruptController = node; |
|
| 564 | + | } |
|
| 565 | + | } |
|
| 566 | + | try finish(bytes, node, platform); |
|
| 567 | + | } |
|
| 568 | + | case fdt::Event::End => break, |
|
| 569 | + | } |
|
| 570 | + | } |
|
| 571 | + | if platform.ramCount == 0 or platform.harts == 0 or platform.timebase == 0 { |
|
| 572 | + | throw Error::Invalid; |
|
| 573 | + | } |
|
| 574 | + | if let node = interruptController { |
|
| 575 | + | try plic(bytes, node, &references[..], platform); |
|
| 576 | + | } |
|
| 577 | + | for hart in 0..limits::HARTS { |
|
| 578 | + | if (platform.harts & (1 << hart)) == 0 { |
|
| 579 | + | continue; |
|
| 580 | + | } |
|
| 581 | + | if hart >= platform.stackCount or not inRam(platform, platform.stacks[hart]) { |
|
| 582 | + | throw Error::Invalid; |
|
| 583 | + | } |
|
| 584 | + | for other in 0..hart { |
|
| 585 | + | if (platform.harts & (1 << other)) <> 0 and overlaps( |
|
| 586 | + | platform.stacks[hart], platform.stacks[other] |
|
| 587 | + | ) |
|
| 588 | + | { |
|
| 589 | + | throw Error::Invalid; |
|
| 590 | + | } |
|
| 591 | + | } |
|
| 592 | + | } |
|
| 593 | + | } |
kernel/kernel/plic.rad
added
+169 -0
| 1 | + | //! PLIC register access and bounded external-interrupt delivery. |
|
| 2 | + | use super::abi; |
|
| 3 | + | use super::limits; |
|
| 4 | + | use super::platform; |
|
| 5 | + | use super::registers; |
|
| 6 | + | use super::slots; |
|
| 7 | + | use super::interrupts; |
|
| 8 | + | use super::domains; |
|
| 9 | + | use super::budgets; |
|
| 10 | + | ||
| 11 | + | /// Validated registers for the kernel's machine interrupt receiver. |
|
| 12 | + | export record Mapping: Copy { |
|
| 13 | + | /// Checked controller register extent. |
|
| 14 | + | registers: registers::Window, |
|
| 15 | + | /// Supported nonzero source count. |
|
| 16 | + | count: u32, |
|
| 17 | + | /// PLIC machine context assigned to physical hart zero. |
|
| 18 | + | context: u32, |
|
| 19 | + | } |
|
| 20 | + | ||
| 21 | + | /// Controller state published before secondary harts start. |
|
| 22 | + | static CONTROLLER: ?Mapping = nil; |
|
| 23 | + | ||
| 24 | + | /// Offset of the first per-context enable word. |
|
| 25 | + | constant ENABLES: u64 = platform::PLIC_ENABLES; |
|
| 26 | + | /// Offset of the first context's threshold register, which precedes its claim register. |
|
| 27 | + | constant CONTEXTS: u64 = platform::PLIC_CONTEXTS; |
|
| 28 | + | ||
| 29 | + | /// Select the controller and validate its routed context's register extent. |
|
| 30 | + | export fn mapping(machine: &platform::Platform) -> ?Mapping throws (abi::Error) { |
|
| 31 | + | if machine.irqSources == 0 { |
|
| 32 | + | return nil; |
|
| 33 | + | } |
|
| 34 | + | if machine.irqSources > limits::INTERRUPTS or (machine.harts & 1) == 0 { |
|
| 35 | + | throw abi::Error::InvalidArg; |
|
| 36 | + | } |
|
| 37 | + | let context = machine.irqContexts[0] else { |
|
| 38 | + | throw abi::Error::InvalidArg; |
|
| 39 | + | }; |
|
| 40 | + | if context >= limits::HARTS * 2 { |
|
| 41 | + | throw abi::Error::InvalidArg; |
|
| 42 | + | } |
|
| 43 | + | let mut result: ?Mapping = nil; |
|
| 44 | + | for i in 0..machine.deviceCount { |
|
| 45 | + | let device = machine.devices[i]; |
|
| 46 | + | if device.kind <> platform::Kind::Plic { |
|
| 47 | + | continue; |
|
| 48 | + | } |
|
| 49 | + | let required = CONTEXTS |
|
| 50 | + | + platform::PLIC_CONTEXT_SIZE |
|
| 51 | + | + context as u64 * platform::PLIC_CONTEXT_STRIDE; |
|
| 52 | + | if result <> nil or (device.memory.start & 3) <> 0 { |
|
| 53 | + | throw abi::Error::InvalidArg; |
|
| 54 | + | } |
|
| 55 | + | let window = try registers::open(machine, platform::Kind::Plic, required); |
|
| 56 | + | set result = Mapping { registers: window, count: machine.irqSources, context }; |
|
| 57 | + | } |
|
| 58 | + | if result == nil { |
|
| 59 | + | throw abi::Error::InvalidArg; |
|
| 60 | + | } |
|
| 61 | + | return result; |
|
| 62 | + | } |
|
| 63 | + | ||
| 64 | + | /// Mask all machine contexts and initialize one priority class before dispatch. |
|
| 65 | + | export unsafe fn initialize(machine: &platform::Platform) throws (abi::Error) { |
|
| 66 | + | set CONTROLLER = try mapping(machine); |
|
| 67 | + | let device = CONTROLLER else { |
|
| 68 | + | return; |
|
| 69 | + | }; |
|
| 70 | + | for hart in 0..limits::HARTS { |
|
| 71 | + | let context = machine.irqContexts[hart] else { |
|
| 72 | + | continue; |
|
| 73 | + | }; |
|
| 74 | + | for word in 0..(device.count + 32) / 32 { |
|
| 75 | + | registers::write32( |
|
| 76 | + | device.registers, |
|
| 77 | + | ENABLES |
|
| 78 | + | + context as u64 * platform::PLIC_ENABLE_STRIDE |
|
| 79 | + | + word as u64 * 4, |
|
| 80 | + | 0 |
|
| 81 | + | ); |
|
| 82 | + | } |
|
| 83 | + | registers::write32( |
|
| 84 | + | device.registers, |
|
| 85 | + | CONTEXTS |
|
| 86 | + | + context as u64 * platform::PLIC_CONTEXT_STRIDE, |
|
| 87 | + | 0 |
|
| 88 | + | ); |
|
| 89 | + | } |
|
| 90 | + | for number in 1..device.count + 1 { |
|
| 91 | + | registers::write32(device.registers, number as u64 * 4, 1); |
|
| 92 | + | } |
|
| 93 | + | } |
|
| 94 | + | ||
| 95 | + | /// Change one source enable bit while the metadata lock serializes the word. |
|
| 96 | + | fn enable(device: Mapping, number: u32, enabled: bool) { |
|
| 97 | + | assert number > 0 and number <= device.count; |
|
| 98 | + | let offset = ENABLES |
|
| 99 | + | + device.context as u64 * platform::PLIC_ENABLE_STRIDE |
|
| 100 | + | + (number / 32) as u64 * 4; |
|
| 101 | + | let before = registers::read32(device.registers, offset); |
|
| 102 | + | let bit = 1 << (number % 32); |
|
| 103 | + | let mut after = before & ~bit; |
|
| 104 | + | if enabled { |
|
| 105 | + | set after |= bit; |
|
| 106 | + | } |
|
| 107 | + | if after <> before { |
|
| 108 | + | registers::write32(device.registers, offset, after); |
|
| 109 | + | } |
|
| 110 | + | } |
|
| 111 | + | ||
| 112 | + | /// Mask a source before its exclusive capability is invalidated. |
|
| 113 | + | export fn mask(number: u32) { |
|
| 114 | + | let device = CONTROLLER else { |
|
| 115 | + | return; |
|
| 116 | + | }; |
|
| 117 | + | enable(device, number, false); |
|
| 118 | + | } |
|
| 119 | + | ||
| 120 | + | /// Claim one source, retain its work, complete it while enabled, then mask it. |
|
| 121 | + | export fn claim(irqs: &mut interrupts::Store, hart: u32) { |
|
| 122 | + | let device = CONTROLLER else { |
|
| 123 | + | return; |
|
| 124 | + | }; |
|
| 125 | + | assert hart == 0; |
|
| 126 | + | let claim = CONTEXTS |
|
| 127 | + | + platform::PLIC_CLAIM |
|
| 128 | + | + device.context as u64 * platform::PLIC_CONTEXT_STRIDE; |
|
| 129 | + | let number = registers::read32(device.registers, claim); |
|
| 130 | + | if number == 0 { |
|
| 131 | + | return; |
|
| 132 | + | } |
|
| 133 | + | assert number <= device.count; |
|
| 134 | + | let retained = interrupts::claim(irqs, number); |
|
| 135 | + | registers::write32(device.registers, claim, number); |
|
| 136 | + | enable(device, number, false); |
|
| 137 | + | } |
|
| 138 | + | ||
| 139 | + | /// Deliver retained notifications, rearm consumed sources, and report runnable harts. |
|
| 140 | + | export fn service( |
|
| 141 | + | irqs: &mut interrupts::Store, store: &mut domains::Store, windows: &budgets::Store, now: u64 |
|
| 142 | + | ) -> u32 { |
|
| 143 | + | let device = CONTROLLER else { |
|
| 144 | + | return 0; |
|
| 145 | + | }; |
|
| 146 | + | let mut harts: u32 = 0; |
|
| 147 | + | for number in 1..device.count + 1 { |
|
| 148 | + | let index = number - 1; |
|
| 149 | + | let mut pending = false; |
|
| 150 | + | if irqs.slots[index].state == slots::State::Live { |
|
| 151 | + | set pending = irqs.records[index].state == interrupts::State::Pending; |
|
| 152 | + | } |
|
| 153 | + | let enabled = try interrupts::service(irqs, store, number) catch err { |
|
| 154 | + | // Past the owner checks, only a ring that its receiver corrupted fails the service. |
|
| 155 | + | if err == abi::Error::InvalidArg |
|
| 156 | + | and number <= irqs.count |
|
| 157 | + | and irqs.slots[index].state == slots::State::Live |
|
| 158 | + | { |
|
| 159 | + | set harts |= domains::ringFault(store, irqs.records[index].owner); |
|
| 160 | + | } |
|
| 161 | + | false; |
|
| 162 | + | }; |
|
| 163 | + | enable(device, number, enabled); |
|
| 164 | + | if pending and irqs.records[index].state == interrupts::State::Queued { |
|
| 165 | + | set harts |= budgets::wakeTargets(windows, store, irqs.records[index].owner, now); |
|
| 166 | + | } |
|
| 167 | + | } |
|
| 168 | + | return harts; |
|
| 169 | + | } |
kernel/kernel/range.rad
added
+23 -0
| 1 | + | //! Checked half-open ranges for physical memory and CPU windows. |
|
| 2 | + | ||
| 3 | + | use std::mem; |
|
| 4 | + | /// A nonempty half-open interval. Construct intervals with `new`. |
|
| 5 | + | export record Range: Copy { |
|
| 6 | + | /// First included address or tick. |
|
| 7 | + | start: u64, |
|
| 8 | + | /// First excluded address or tick. |
|
| 9 | + | end: u64, |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Construct a nonempty interval. Reject addition overflow. |
|
| 13 | + | export fn new(start: u64, length: u64) -> ?Range { |
|
| 14 | + | if length == 0 or length > mem::U64_MAX - start { |
|
| 15 | + | return nil; |
|
| 16 | + | } |
|
| 17 | + | return Range { start, end: start + length }; |
|
| 18 | + | } |
|
| 19 | + | ||
| 20 | + | /// Check that the complete nonempty interval is inside the outer interval. |
|
| 21 | + | export fn contains(outer: Range, inner: Range) -> bool { |
|
| 22 | + | return outer.start <= inner.start and inner.start < inner.end and inner.end <= outer.end; |
|
| 23 | + | } |
kernel/kernel/registers.rad
added
+155 -0
| 1 | + | //! Checked access to device registers. |
|
| 2 | + | //! |
|
| 3 | + | //! This is the only kernel module that reads or writes device registers. Every |
|
| 4 | + | //! access goes through a `Window`, an opaque token that only this module can |
|
| 5 | + | //! build, so holding one proves that its range was checked once: |
|
| 6 | + | //! |
|
| 7 | + | //! - it belongs to a device of the requested kind decoded from the platform; |
|
| 8 | + | //! - it does not wrap the address space; |
|
| 9 | + | //! - it does not overlap RAM, and the frame pool only manages RAM, so a window |
|
| 10 | + | //! never aliases memory that the kernel allocates or recycles. |
|
| 11 | + | //! |
|
| 12 | + | //! Each access checks its offset against the window and its address for |
|
| 13 | + | //! natural alignment, then performs one load or store between full I/O |
|
| 14 | + | //! barriers. An access that violates a check is a kernel bug, not a runtime |
|
| 15 | + | //! condition, so it panics instead of reporting an error. Callers derive their |
|
| 16 | + | //! offsets from register layouts whose extent they requested from `open`. |
|
| 17 | + | //! |
|
| 18 | + | //! Device ranges are fixed by the platform for the life of the kernel, so a |
|
| 19 | + | //! `Window` is a Copy value and never needs to be revoked. |
|
| 20 | + | ||
| 21 | + | use super::abi; |
|
| 22 | + | use super::platform; |
|
| 23 | + | ||
| 24 | + | /// Read a byte from a device register between full I/O barriers. |
|
| 25 | + | /// The address must be inside a window and tolerate a byte read. |
|
| 26 | + | unsafe fn load8(address: u64) -> u8; |
|
| 27 | + | /// Read a naturally aligned 32-bit device register between full I/O barriers. |
|
| 28 | + | unsafe fn load32(address: u64) -> u32; |
|
| 29 | + | /// Read a naturally aligned 64-bit device register between full I/O barriers. |
|
| 30 | + | unsafe fn load64(address: u64) -> u64; |
|
| 31 | + | /// Write a byte to a device register between full I/O barriers. |
|
| 32 | + | unsafe fn store8(address: u64, value: u8); |
|
| 33 | + | /// Write a naturally aligned 32-bit device register between full I/O barriers. |
|
| 34 | + | unsafe fn store32(address: u64, value: u32); |
|
| 35 | + | /// Write a naturally aligned 64-bit device register between full I/O barriers. |
|
| 36 | + | unsafe fn store64(address: u64, value: u64); |
|
| 37 | + | ||
| 38 | + | /// A checked device register extent. Offsets in operations are relative to its |
|
| 39 | + | /// first byte. |
|
| 40 | + | export opaque record Window: Copy { |
|
| 41 | + | /// First device byte. |
|
| 42 | + | base: u64, |
|
| 43 | + | /// Number of bytes in the device extent. |
|
| 44 | + | length: u64, |
|
| 45 | + | } |
|
| 46 | + | ||
| 47 | + | /// First physical byte of a window. |
|
| 48 | + | export fn start(window: Window) -> u64 { |
|
| 49 | + | return window.base; |
|
| 50 | + | } |
|
| 51 | + | ||
| 52 | + | /// Number of bytes in a window. |
|
| 53 | + | export fn size(window: Window) -> u64 { |
|
| 54 | + | return window.length; |
|
| 55 | + | } |
|
| 56 | + | ||
| 57 | + | /// Open the first decoded device of a kind, which must span at least `minimum` |
|
| 58 | + | /// bytes (nonzero), must not wrap, and must not overlap RAM. |
|
| 59 | + | export fn open(machine: &platform::Platform, kind: platform::Kind, minimum: u64) -> Window |
|
| 60 | + | throws (abi::Error) |
|
| 61 | + | { |
|
| 62 | + | if minimum == 0 { |
|
| 63 | + | throw abi::Error::InvalidArg; |
|
| 64 | + | } |
|
| 65 | + | for i in 0..machine.deviceCount { |
|
| 66 | + | let device = machine.devices[i]; |
|
| 67 | + | if device.kind <> kind { |
|
| 68 | + | continue; |
|
| 69 | + | } |
|
| 70 | + | let extent = device.memory; |
|
| 71 | + | if extent.end < extent.start or extent.end - extent.start < minimum { |
|
| 72 | + | throw abi::Error::InvalidArg; |
|
| 73 | + | } |
|
| 74 | + | for bank in 0..machine.ramCount { |
|
| 75 | + | let ram = machine.ram[bank]; |
|
| 76 | + | if extent.start < ram.end and ram.start < extent.end { |
|
| 77 | + | throw abi::Error::InvalidArg; |
|
| 78 | + | } |
|
| 79 | + | } |
|
| 80 | + | return Window { base: extent.start, length: extent.end - extent.start }; |
|
| 81 | + | } |
|
| 82 | + | throw abi::Error::InvalidArg; |
|
| 83 | + | } |
|
| 84 | + | ||
| 85 | + | /// Check that `width` bytes at `offset` lie inside the window and the access |
|
| 86 | + | /// address is naturally aligned. The width must be a power of two. |
|
| 87 | + | export fn contains(window: Window, offset: u64, width: u64) -> bool { |
|
| 88 | + | if width > window.length or offset > window.length - width { |
|
| 89 | + | return false; |
|
| 90 | + | } |
|
| 91 | + | return ((window.base + offset) & (width - 1)) == 0; |
|
| 92 | + | } |
|
| 93 | + | ||
| 94 | + | /// Resolve a checked access to its device address. |
|
| 95 | + | fn address(window: Window, offset: u64, width: u64) -> u64 { |
|
| 96 | + | if not contains(window, offset, width) { |
|
| 97 | + | panic "address: register access outside window"; |
|
| 98 | + | } |
|
| 99 | + | return window.base + offset; |
|
| 100 | + | } |
|
| 101 | + | ||
| 102 | + | /// Read one byte register. |
|
| 103 | + | export fn read8(window: Window, offset: u64) -> u8 { |
|
| 104 | + | let target = address(window, offset, 1); |
|
| 105 | + | // SAFETY: address proved the byte lies inside a window, which is a device |
|
| 106 | + | // extent outside RAM. |
|
| 107 | + | unsafe { |
|
| 108 | + | return load8(target); |
|
| 109 | + | } |
|
| 110 | + | } |
|
| 111 | + | ||
| 112 | + | /// Read one naturally aligned 32-bit register. |
|
| 113 | + | export fn read32(window: Window, offset: u64) -> u32 { |
|
| 114 | + | let target = address(window, offset, 4); |
|
| 115 | + | // SAFETY: address proved the aligned word lies inside a device window. |
|
| 116 | + | unsafe { |
|
| 117 | + | return load32(target); |
|
| 118 | + | } |
|
| 119 | + | } |
|
| 120 | + | ||
| 121 | + | /// Read one naturally aligned 64-bit register. |
|
| 122 | + | export fn read64(window: Window, offset: u64) -> u64 { |
|
| 123 | + | let target = address(window, offset, 8); |
|
| 124 | + | // SAFETY: address proved the aligned doubleword lies inside a device window. |
|
| 125 | + | unsafe { |
|
| 126 | + | return load64(target); |
|
| 127 | + | } |
|
| 128 | + | } |
|
| 129 | + | ||
| 130 | + | /// Write one byte register. |
|
| 131 | + | export fn write8(window: Window, offset: u64, value: u8) { |
|
| 132 | + | let target = address(window, offset, 1); |
|
| 133 | + | // SAFETY: address proved the byte lies inside a device window. |
|
| 134 | + | unsafe { |
|
| 135 | + | store8(target, value); |
|
| 136 | + | } |
|
| 137 | + | } |
|
| 138 | + | ||
| 139 | + | /// Write one naturally aligned 32-bit register. |
|
| 140 | + | export fn write32(window: Window, offset: u64, value: u32) { |
|
| 141 | + | let target = address(window, offset, 4); |
|
| 142 | + | // SAFETY: address proved the aligned word lies inside a device window. |
|
| 143 | + | unsafe { |
|
| 144 | + | store32(target, value); |
|
| 145 | + | } |
|
| 146 | + | } |
|
| 147 | + | ||
| 148 | + | /// Write one naturally aligned 64-bit register. |
|
| 149 | + | export fn write64(window: Window, offset: u64, value: u64) { |
|
| 150 | + | let target = address(window, offset, 8); |
|
| 151 | + | // SAFETY: address proved the aligned doubleword lies inside a device window. |
|
| 152 | + | unsafe { |
|
| 153 | + | store64(target, value); |
|
| 154 | + | } |
|
| 155 | + | } |
kernel/kernel/registers.ras
added
+50 -0
| 1 | + | //! Device register loads and stores between full I/O barriers. |
|
| 2 | + | .text; |
|
| 3 | + | .export @kernel::registers::load8; |
|
| 4 | + | .export @kernel::registers::load32; |
|
| 5 | + | .export @kernel::registers::load64; |
|
| 6 | + | .export @kernel::registers::store8; |
|
| 7 | + | .export @kernel::registers::store32; |
|
| 8 | + | .export @kernel::registers::store64; |
|
| 9 | + | ||
| 10 | + | // The checked window contains the byte. |
|
| 11 | + | @kernel::registers::load8 |
|
| 12 | + | fence iorw iorw; |
|
| 13 | + | lbu %a0 0(%a0); |
|
| 14 | + | fence iorw iorw; |
|
| 15 | + | ret; |
|
| 16 | + | ||
| 17 | + | // The checked window contains the aligned word; the result is zero-extended. |
|
| 18 | + | @kernel::registers::load32 |
|
| 19 | + | fence iorw iorw; |
|
| 20 | + | lwu %a0 0(%a0); |
|
| 21 | + | fence iorw iorw; |
|
| 22 | + | ret; |
|
| 23 | + | ||
| 24 | + | // The checked window contains the aligned doubleword. |
|
| 25 | + | @kernel::registers::load64 |
|
| 26 | + | fence iorw iorw; |
|
| 27 | + | ld %a0 0(%a0); |
|
| 28 | + | fence iorw iorw; |
|
| 29 | + | ret; |
|
| 30 | + | ||
| 31 | + | // The checked window contains the byte. |
|
| 32 | + | @kernel::registers::store8 |
|
| 33 | + | fence iorw iorw; |
|
| 34 | + | sb %a1 0(%a0); |
|
| 35 | + | fence iorw iorw; |
|
| 36 | + | ret; |
|
| 37 | + | ||
| 38 | + | // The checked window contains the aligned word. |
|
| 39 | + | @kernel::registers::store32 |
|
| 40 | + | fence iorw iorw; |
|
| 41 | + | sw %a1 0(%a0); |
|
| 42 | + | fence iorw iorw; |
|
| 43 | + | ret; |
|
| 44 | + | ||
| 45 | + | // One RV64 store updates the complete aligned doubleword. |
|
| 46 | + | @kernel::registers::store64 |
|
| 47 | + | fence iorw iorw; |
|
| 48 | + | sd %a1 0(%a0); |
|
| 49 | + | fence iorw iorw; |
|
| 50 | + | ret; |
kernel/kernel/registry.rad
added
+282 -0
| 1 | + | //! Immutable resident packages shared by boot and runtime loading. |
|
| 2 | + | //! |
|
| 3 | + | //! A package slot is written only while reserved, and the descriptor, source |
|
| 4 | + | //! bytes and dependencies are complete before the slot becomes live. A live |
|
| 5 | + | //! slot is never released or rewritten for the rest of the boot, so a published |
|
| 6 | + | //! package is immutable. Only the holder of the loader workspace reserves, |
|
| 7 | + | //! cancels, or publishes slots, and boot does so before other harts start. |
|
| 8 | + | //! Code that reads packages without the metadata lock does so through a |
|
| 9 | + | //! `Published` view captured under the lock: the lock's release and acquire |
|
| 10 | + | //! order a package's publication before any later capture. |
|
| 11 | + | ||
| 12 | + | use std::mem; |
|
| 13 | + | use std::arch::rv64::shared; |
|
| 14 | + | use std::arch::rv64::shared::catalog; |
|
| 15 | + | use super::abi; |
|
| 16 | + | use super::limits; |
|
| 17 | + | use super::slots; |
|
| 18 | + | use super::capability; |
|
| 19 | + | ||
| 20 | + | /// Rights installed on a new Image capability. |
|
| 21 | + | export constant IMAGE_RIGHTS: u16 = abi::READ | abi::EXECUTE | abi::GRANT | abi::TRANSFER; |
|
| 22 | + | ||
| 23 | + | /// Fixed package registry. Callers serialize admission and handle publication. |
|
| 24 | + | export record Store: Copy { |
|
| 25 | + | /// Package generations; published slots remain live until shutdown. |
|
| 26 | + | slots: [slots::Slot; limits::PACKAGES], |
|
| 27 | + | /// Descriptors whose backing storage remains resident until shutdown. |
|
| 28 | + | entries: [catalog::Entry; limits::PACKAGES], |
|
| 29 | + | /// Resolved dependencies in each package's declared order. |
|
| 30 | + | dependencies: [[abi::Ref; limits::PACKAGES]; limits::PACKAGES], |
|
| 31 | + | /// Number of valid resolved dependencies per live package. |
|
| 32 | + | counts: [u32; limits::PACKAGES], |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Initialize an empty registry before loading any native package. |
|
| 36 | + | export fn initialize(store: &mut Store) { |
|
| 37 | + | slots::initialize(&mut store.slots[..]); |
|
| 38 | + | } |
|
| 39 | + | ||
| 40 | + | /// Read-only view of the packages that were published when it was captured. |
|
| 41 | + | /// Opaque, so only `capture` can build one: holding a view proves it was |
|
| 42 | + | /// captured from a real registry. It holds no lock: it stays valid because published packages never |
|
| 43 | + | /// change, |
|
| 44 | + | /// and it exposes no package that was unpublished at capture. |
|
| 45 | + | export opaque record Published: Copy { |
|
| 46 | + | /// Slot generations and states at capture. Only live slots are readable. |
|
| 47 | + | slots: [slots::Slot; limits::PACKAGES], |
|
| 48 | + | /// The registry's descriptor array, read only at slots that were live. |
|
| 49 | + | entries: *unsafe [catalog::Entry], |
|
| 50 | + | } |
|
| 51 | + | ||
| 52 | + | /// Capture the published packages. The caller holds the metadata lock, which |
|
| 53 | + | /// orders every earlier publication before reads through the view. |
|
| 54 | + | export fn capture(store: &Store) -> Published { |
|
| 55 | + | let mut entries: *unsafe [catalog::Entry] = undefined; |
|
| 56 | + | // The pointer only designates the descriptor array, which `describe` reads at published slots. |
|
| 57 | + | unsafe { |
|
| 58 | + | set entries = &store.entries[..] as *unsafe [catalog::Entry]; |
|
| 59 | + | } |
|
| 60 | + | return Published { slots: store.slots, entries }; |
|
| 61 | + | } |
|
| 62 | + | ||
| 63 | + | /// Read the descriptor of a package that was live at capture. |
|
| 64 | + | export fn describe(view: &Published, object: abi::Ref) -> catalog::Entry throws (abi::Error) { |
|
| 65 | + | if not slots::matches(&view.slots[..], object, slots::State::Live) { |
|
| 66 | + | throw abi::Error::BadHandle; |
|
| 67 | + | } |
|
| 68 | + | let mut entry: catalog::Entry = undefined; |
|
| 69 | + | // The slot was live at capture, so the descriptor was complete and never changes. |
|
| 70 | + | unsafe { |
|
| 71 | + | set entry = view.entries[object.index]; |
|
| 72 | + | } |
|
| 73 | + | return entry; |
|
| 74 | + | } |
|
| 75 | + | ||
| 76 | + | /// Read resident metadata of a package that was live at capture. |
|
| 77 | + | export fn lookup(view: &Published, object: abi::Ref) -> shared::Package throws (abi::Error) { |
|
| 78 | + | return (try describe(view, object)).package; |
|
| 79 | + | } |
|
| 80 | + | ||
| 81 | + | /// Find a resident package by its immutable name. |
|
| 82 | + | export fn find(view: &Published, name: &[u8]) -> ?abi::Ref { |
|
| 83 | + | for i in 0..limits::PACKAGES { |
|
| 84 | + | if view.slots[i].state <> slots::State::Live { |
|
| 85 | + | continue; |
|
| 86 | + | } |
|
| 87 | + | let object = abi::Ref { index: i, generation: view.slots[i].generation }; |
|
| 88 | + | if mem::eq((try! describe(view, object)).package.name, &name[..]) { |
|
| 89 | + | return object; |
|
| 90 | + | } |
|
| 91 | + | } |
|
| 92 | + | return nil; |
|
| 93 | + | } |
|
| 94 | + | ||
| 95 | + | /// Reuse identical content and reject conflicting content under a resident name. |
|
| 96 | + | export fn identify(view: &Published, name: &[u8], source: &[u8]) -> ?abi::Ref throws (abi::Error) { |
|
| 97 | + | let object = find(view, name) else { |
|
| 98 | + | return nil; |
|
| 99 | + | }; |
|
| 100 | + | if not mem::eq((try! describe(view, object)).source, &source[..]) { |
|
| 101 | + | throw abi::Error::VerifyFailed; |
|
| 102 | + | } |
|
| 103 | + | return object; |
|
| 104 | + | } |
|
| 105 | + | ||
| 106 | + | /// Reserve the stable package-state slot before compiling its native code. |
|
| 107 | + | export fn reserve(store: &mut Store) -> slots::Reservation throws (abi::Error) { |
|
| 108 | + | return try slots::reserve(&mut store.slots[..]); |
|
| 109 | + | } |
|
| 110 | + | ||
| 111 | + | /// Return unpublished capacity after a failed load. |
|
| 112 | + | export fn cancel(store: &mut Store, reservation: slots::Reservation) { |
|
| 113 | + | try! slots::cancel(&mut store.slots[..], reservation); |
|
| 114 | + | } |
|
| 115 | + | ||
| 116 | + | /// Validated immutable package metadata ready for bounded publication. |
|
| 117 | + | export record Admission: Copy { |
|
| 118 | + | /// Reserved package generation to receive the descriptor. |
|
| 119 | + | object: abi::Ref, |
|
| 120 | + | /// Persistent source and native descriptor. |
|
| 121 | + | entry: catalog::Entry, |
|
| 122 | + | /// Resolved resident dependencies in their declared order. |
|
| 123 | + | dependencies: [abi::Ref; limits::PACKAGES], |
|
| 124 | + | /// Number of initialized dependency references. |
|
| 125 | + | count: u32, |
|
| 126 | + | } |
|
| 127 | + | ||
| 128 | + | /// Resolve admission against immutable packages while holding sole publisher ownership. |
|
| 129 | + | /// The caller keeps that ownership and the descriptor storage through commit or cancellation. |
|
| 130 | + | export fn admit( |
|
| 131 | + | view: &Published, object: abi::Ref, source: *[u8], package: shared::Package |
|
| 132 | + | ) -> Admission throws (abi::Error) { |
|
| 133 | + | if not slots::matches(&view.slots[..], object, slots::State::Reserved) { |
|
| 134 | + | throw abi::Error::BadHandle; |
|
| 135 | + | } |
|
| 136 | + | if try identify(view, &package.name[..], &source[..]) <> nil { |
|
| 137 | + | throw abi::Error::Busy; |
|
| 138 | + | } |
|
| 139 | + | if package.name.len == 0 |
|
| 140 | + | or package.slot <> object.index |
|
| 141 | + | or package.dependencies.len > limits::PACKAGES |
|
| 142 | + | { |
|
| 143 | + | throw abi::Error::VerifyFailed; |
|
| 144 | + | } |
|
| 145 | + | let mut result = Admission { |
|
| 146 | + | object, |
|
| 147 | + | entry: catalog::Entry { source, package }, |
|
| 148 | + | dependencies: undefined, |
|
| 149 | + | count: package.dependencies.len, |
|
| 150 | + | }; |
|
| 151 | + | for dependency, i in package.dependencies { |
|
| 152 | + | let target = find(view, &dependency[..]) else { |
|
| 153 | + | throw abi::Error::VerifyFailed; |
|
| 154 | + | }; |
|
| 155 | + | for prior in &result.dependencies[..i] { |
|
| 156 | + | if prior == target { |
|
| 157 | + | throw abi::Error::VerifyFailed; |
|
| 158 | + | } |
|
| 159 | + | } |
|
| 160 | + | set result.dependencies[i] = target; |
|
| 161 | + | } |
|
| 162 | + | return result; |
|
| 163 | + | } |
|
| 164 | + | ||
| 165 | + | /// Publish admitted metadata under serialization using only bounded reference copies. |
|
| 166 | + | export fn commit( |
|
| 167 | + | store: &mut Store, reservation: slots::Reservation, admission: Admission |
|
| 168 | + | ) -> abi::Ref { |
|
| 169 | + | let object = slots::reference(&reservation); |
|
| 170 | + | assert object == admission.object and slots::matches( |
|
| 171 | + | &store.slots[..], object, slots::State::Reserved |
|
| 172 | + | ); |
|
| 173 | + | assert admission.count <= limits::PACKAGES; |
|
| 174 | + | for i in 0..admission.count { |
|
| 175 | + | let target = admission.dependencies[i]; |
|
| 176 | + | assert slots::matches(&store.slots[..], target, slots::State::Live); |
|
| 177 | + | set store.dependencies[object.index][i] = target; |
|
| 178 | + | } |
|
| 179 | + | set store.counts[object.index] = admission.count; |
|
| 180 | + | set store.entries[object.index] = admission.entry; |
|
| 181 | + | return try! slots::commit(&mut store.slots[..], reservation); |
|
| 182 | + | } |
|
| 183 | + | ||
| 184 | + | /// Publish persistent compiler output after successful admission checks. |
|
| 185 | + | /// All descriptor pointers and source bytes must remain immutable and resident. |
|
| 186 | + | /// Failure cancels the reservation; duplicate content returns its existing slot. |
|
| 187 | + | export fn publish( |
|
| 188 | + | store: &mut Store, reservation: slots::Reservation, source: *[u8], package: shared::Package |
|
| 189 | + | ) -> abi::Ref throws (abi::Error) { |
|
| 190 | + | let view = capture(&*store); |
|
| 191 | + | let existing = try identify(&view, &package.name[..], &source[..]) catch err { |
|
| 192 | + | cancel(store, reservation); |
|
| 193 | + | throw err; |
|
| 194 | + | }; |
|
| 195 | + | if let object = existing { |
|
| 196 | + | cancel(store, reservation); |
|
| 197 | + | return object; |
|
| 198 | + | } |
|
| 199 | + | let object = slots::reference(&reservation); |
|
| 200 | + | let admission = try admit(&view, object, source, package) catch err { |
|
| 201 | + | cancel(store, reservation); |
|
| 202 | + | throw err; |
|
| 203 | + | }; |
|
| 204 | + | return commit(store, reservation, admission); |
|
| 205 | + | } |
|
| 206 | + | ||
| 207 | + | /// Read resident metadata through a generation-bearing package reference. |
|
| 208 | + | export fn get(store: &Store, object: abi::Ref) -> shared::Package throws (abi::Error) { |
|
| 209 | + | if not slots::matches(&store.slots[..], object, slots::State::Live) { |
|
| 210 | + | throw abi::Error::BadHandle; |
|
| 211 | + | } |
|
| 212 | + | return store.entries[object.index].package; |
|
| 213 | + | } |
|
| 214 | + | ||
| 215 | + | /// Resolve one public symbol without searching packages outside the named image. |
|
| 216 | + | export fn exported(store: &Store, object: abi::Ref, name: &[u8]) -> shared::Target |
|
| 217 | + | throws (abi::Error) |
|
| 218 | + | { |
|
| 219 | + | let package = try get(store, object); |
|
| 220 | + | let target = shared::lookup(package.exports, &name[..]) else { |
|
| 221 | + | throw abi::Error::VerifyFailed; |
|
| 222 | + | }; |
|
| 223 | + | return target; |
|
| 224 | + | } |
|
| 225 | + | ||
| 226 | + | /// Collect exports from declared dependencies into caller-owned compiler storage. |
|
| 227 | + | export fn imports(view: &Published, dependencies: &[*[u8]], output: &mut [shared::Symbol]) -> u32 |
|
| 228 | + | throws (abi::Error) |
|
| 229 | + | { |
|
| 230 | + | let mut count: u32 = 0; |
|
| 231 | + | for name in dependencies { |
|
| 232 | + | let object = find(view, &name[..]) else { |
|
| 233 | + | throw abi::Error::VerifyFailed; |
|
| 234 | + | }; |
|
| 235 | + | let package = try! lookup(view, object); |
|
| 236 | + | if package.exports.len > output.len - count { |
|
| 237 | + | throw abi::Error::Exhausted; |
|
| 238 | + | } |
|
| 239 | + | set count += package.exports.len; |
|
| 240 | + | } |
|
| 241 | + | set count = 0; |
|
| 242 | + | for name in dependencies { |
|
| 243 | + | let object = find(view, &name[..]) else panic "imports: resident dependency"; |
|
| 244 | + | let package = try! lookup(view, object); |
|
| 245 | + | for symbol in package.exports { |
|
| 246 | + | set output[count] = symbol; |
|
| 247 | + | set count += 1; |
|
| 248 | + | } |
|
| 249 | + | } |
|
| 250 | + | return count; |
|
| 251 | + | } |
|
| 252 | + | ||
| 253 | + | /// Register a persistent boot catalog in dependency order through normal admission. |
|
| 254 | + | export fn boot(store: &mut Store, catalog: &[catalog::Entry]) throws (abi::Error) { |
|
| 255 | + | for entry in catalog { |
|
| 256 | + | let existing = try identify(&capture(&*store), &entry.package.name[..], &entry.source[..]); |
|
| 257 | + | if existing <> nil { |
|
| 258 | + | continue; |
|
| 259 | + | } |
|
| 260 | + | let reservation = try reserve(store); |
|
| 261 | + | let object = try publish(store, reservation, entry.source, entry.package); |
|
| 262 | + | } |
|
| 263 | + | } |
|
| 264 | + | ||
| 265 | + | /// Install an Image handle for a resident package without duplicating its code. |
|
| 266 | + | export fn install(store: &Store, table: &mut capability::Table, object: abi::Ref) -> abi::Handle |
|
| 267 | + | throws (abi::Error) |
|
| 268 | + | { |
|
| 269 | + | let package = try get(store, object); |
|
| 270 | + | return try capability::install(table, capability::Entry { |
|
| 271 | + | kind: abi::Kind::Image, object, rights: abi::Rights(IMAGE_RIGHTS) |
|
| 272 | + | }); |
|
| 273 | + | } |
|
| 274 | + | ||
| 275 | + | /// Resolve image authority and validate its resident package generation. |
|
| 276 | + | export fn image( |
|
| 277 | + | store: &Store, table: &capability::Table, handle: abi::Handle, rights: abi::Rights |
|
| 278 | + | ) -> abi::Ref throws (abi::Error) { |
|
| 279 | + | let entry = try capability::lookup(table, handle, abi::Kind::Image, rights); |
|
| 280 | + | let package = try get(store, entry.object); |
|
| 281 | + | return entry.object; |
|
| 282 | + | } |
kernel/kernel/remote.rad
added
+324 -0
| 1 | + | //! Fixed request queues and generation-checked completion records. |
|
| 2 | + | use super::abi; |
|
| 3 | + | use super::limits; |
|
| 4 | + | use super::slots; |
|
| 5 | + | ||
| 6 | + | /// A scalar operation retained until its owning hart reaches dispatch. |
|
| 7 | + | export record Call: Copy { |
|
| 8 | + | /// Domain whose capability table supplies the arguments. |
|
| 9 | + | owner: abi::Ref, |
|
| 10 | + | /// Context whose kernel continuation consumes the result. |
|
| 11 | + | context: abi::Ref, |
|
| 12 | + | /// Budget operation to execute with serialized metadata. |
|
| 13 | + | operation: u64, |
|
| 14 | + | /// Original scalar syscall arguments. |
|
| 15 | + | arguments: [u64; 4], |
|
| 16 | + | } |
|
| 17 | + | ||
| 18 | + | /// Result retained with a request's acknowledgement. |
|
| 19 | + | export union Reply: Copy { |
|
| 20 | + | /// Successful scalar return value. |
|
| 21 | + | Value(u64), |
|
| 22 | + | /// Operation failure with no partial commit. |
|
| 23 | + | Error(abi::Error), |
|
| 24 | + | } |
|
| 25 | + | ||
| 26 | + | /// Work performed by the receiving hart before acknowledgement. |
|
| 27 | + | export union Action: Copy { |
|
| 28 | + | /// Refresh dispatch eligibility and its timer boundary. |
|
| 29 | + | Reschedule, |
|
| 30 | + | /// Execute a budget operation after releasing local context ownership. |
|
| 31 | + | Budget(Call), |
|
| 32 | + | /// Refresh domain eligibility and notify the waiting call's context. |
|
| 33 | + | Ready(abi::Ref), |
|
| 34 | + | } |
|
| 35 | + | ||
| 36 | + | /// Request delivery state retained until the source releases it. |
|
| 37 | + | export union State: Copy { |
|
| 38 | + | /// The request is queued for its target. |
|
| 39 | + | Pending, |
|
| 40 | + | /// The target has taken the request. |
|
| 41 | + | Delivered, |
|
| 42 | + | /// The target completed the requested work. |
|
| 43 | + | Acknowledged, |
|
| 44 | + | } |
|
| 45 | + | ||
| 46 | + | /// One request owned by a source hart. |
|
| 47 | + | export record Request: Copy { |
|
| 48 | + | /// Hart that must release the completion record. |
|
| 49 | + | source: u32, |
|
| 50 | + | /// Hart that performs and acknowledges the action. |
|
| 51 | + | target: u32, |
|
| 52 | + | /// Required local action. |
|
| 53 | + | action: Action, |
|
| 54 | + | /// Current delivery state. |
|
| 55 | + | state: State, |
|
| 56 | + | /// Scalar result available after acknowledgement. |
|
| 57 | + | reply: Reply, |
|
| 58 | + | } |
|
| 59 | + | ||
| 60 | + | /// Bounded FIFO whose indices remain within its fixed array. |
|
| 61 | + | export record Queue: Copy { |
|
| 62 | + | /// Generation-bearing request references in delivery order. |
|
| 63 | + | entries: [abi::Ref; limits::REMOTE_PER_HART], |
|
| 64 | + | /// Next entry to consume. |
|
| 65 | + | read: u32, |
|
| 66 | + | /// Next entry to produce. |
|
| 67 | + | write: u32, |
|
| 68 | + | /// Number of occupied entries, including cancelled references. |
|
| 69 | + | count: u32, |
|
| 70 | + | } |
|
| 71 | + | ||
| 72 | + | /// Remote request metadata serialized by the shared kernel lock. |
|
| 73 | + | export record Store: Copy { |
|
| 74 | + | /// Online physical hart mask fixed at boot. |
|
| 75 | + | online: u32, |
|
| 76 | + | /// Request generations retained through acknowledgement. |
|
| 77 | + | slots: [slots::Slot; limits::REMOTE_REQUESTS], |
|
| 78 | + | /// Live request payloads. |
|
| 79 | + | requests: [Request; limits::REMOTE_REQUESTS], |
|
| 80 | + | /// FIFO owned by each receiving hart. |
|
| 81 | + | queues: [Queue; limits::HARTS], |
|
| 82 | + | } |
|
| 83 | + | ||
| 84 | + | /// Delivery identity and immutable routing information. |
|
| 85 | + | export record Delivery: Copy { |
|
| 86 | + | /// Live request generation to acknowledge. |
|
| 87 | + | id: abi::Ref, |
|
| 88 | + | /// Routing, action, and delivered state. |
|
| 89 | + | request: Request, |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// One completion reference for each hart selected by a bounded request batch. |
|
| 93 | + | export record Batch: Copy { |
|
| 94 | + | /// Hart that owns and releases all completion records. |
|
| 95 | + | source: u32, |
|
| 96 | + | /// Target hart mask; only these reference array elements are initialized. |
|
| 97 | + | targets: u32, |
|
| 98 | + | /// Request references indexed by target hart. |
|
| 99 | + | ids: [abi::Ref; limits::HARTS], |
|
| 100 | + | } |
|
| 101 | + | ||
| 102 | + | /// Reserved records whose queue capacity stays protected by the metadata lock. |
|
| 103 | + | export union Reservation: Once { |
|
| 104 | + | /// Batch to publish or cancel before releasing metadata serialization. |
|
| 105 | + | Held(Batch), |
|
| 106 | + | } |
|
| 107 | + | ||
| 108 | + | /// Reserve every target before any request or associated state becomes visible. |
|
| 109 | + | export fn reserve(store: &mut Store, source: u32, targets: u32) -> Reservation throws (abi::Error) { |
|
| 110 | + | try hart(store, source); |
|
| 111 | + | if (targets & ~store.online) <> 0 { |
|
| 112 | + | throw abi::Error::InvalidArg; |
|
| 113 | + | } |
|
| 114 | + | let mut count: u32 = 0; |
|
| 115 | + | for target in 0..limits::HARTS { |
|
| 116 | + | if (targets & (1 << target)) == 0 { |
|
| 117 | + | continue; |
|
| 118 | + | } |
|
| 119 | + | if store.queues[target].count == limits::REMOTE_PER_HART { |
|
| 120 | + | throw abi::Error::Busy; |
|
| 121 | + | } |
|
| 122 | + | set count += 1; |
|
| 123 | + | } |
|
| 124 | + | let mut free: u32 = 0; |
|
| 125 | + | for slot in &store.slots[..] { |
|
| 126 | + | if slot.state == slots::State::Free { |
|
| 127 | + | set free += 1; |
|
| 128 | + | } |
|
| 129 | + | } |
|
| 130 | + | if free < count { |
|
| 131 | + | throw abi::Error::Busy; |
|
| 132 | + | } |
|
| 133 | + | let mut batch = Batch { source, targets, ids: undefined }; |
|
| 134 | + | for target in 0..limits::HARTS { |
|
| 135 | + | if (targets & (1 << target)) == 0 { |
|
| 136 | + | continue; |
|
| 137 | + | } |
|
| 138 | + | let pending = try! slots::reserve(&mut store.slots[..]); |
|
| 139 | + | match pending { |
|
| 140 | + | case slots::Reservation::Held(id) => { |
|
| 141 | + | set batch.ids[target] = id; |
|
| 142 | + | } |
|
| 143 | + | } |
|
| 144 | + | } |
|
| 145 | + | return Reservation::Held(batch); |
|
| 146 | + | } |
|
| 147 | + | ||
| 148 | + | /// Return all unpublished records while the queue-capacity lock remains held. |
|
| 149 | + | export fn cancelReservation(store: &mut Store, reservation: Reservation) { |
|
| 150 | + | match reservation { |
|
| 151 | + | case Reservation::Held(batch) => { |
|
| 152 | + | for target in 0..limits::HARTS { |
|
| 153 | + | if (batch.targets & (1 << target)) == 0 { |
|
| 154 | + | continue; |
|
| 155 | + | } |
|
| 156 | + | try! slots::cancel(&mut store.slots[..], slots::Reservation::Held( |
|
| 157 | + | batch.ids[target] |
|
| 158 | + | )); |
|
| 159 | + | } |
|
| 160 | + | } |
|
| 161 | + | } |
|
| 162 | + | } |
|
| 163 | + | ||
| 164 | + | /// Publish the reserved batch before releasing metadata serialization. |
|
| 165 | + | export fn publish(store: &mut Store, reservation: Reservation, action: Action) -> Batch { |
|
| 166 | + | match reservation { |
|
| 167 | + | case Reservation::Held(batch) => { |
|
| 168 | + | for target in 0..limits::HARTS { |
|
| 169 | + | if (batch.targets & (1 << target)) == 0 { |
|
| 170 | + | continue; |
|
| 171 | + | } |
|
| 172 | + | let id = batch.ids[target]; |
|
| 173 | + | set store.requests[id.index] = Request { |
|
| 174 | + | source: batch.source, |
|
| 175 | + | target, |
|
| 176 | + | action, |
|
| 177 | + | state: State::Pending, |
|
| 178 | + | reply: Reply::Value(0), |
|
| 179 | + | }; |
|
| 180 | + | enqueue(store, target, id); |
|
| 181 | + | let live = try! slots::commit(&mut store.slots[..], slots::Reservation::Held(id)); |
|
| 182 | + | } |
|
| 183 | + | return batch; |
|
| 184 | + | } |
|
| 185 | + | } |
|
| 186 | + | } |
|
| 187 | + | ||
| 188 | + | /// Append a generation reference to a queue with reserved capacity. |
|
| 189 | + | fn enqueue(store: &mut Store, target: u32, id: abi::Ref) { |
|
| 190 | + | assert store.queues[target].count < limits::REMOTE_PER_HART; |
|
| 191 | + | let index = store.queues[target].write; |
|
| 192 | + | set store.queues[target].entries[index] = id; |
|
| 193 | + | set store.queues[target].write = (index + 1) % limits::REMOTE_PER_HART; |
|
| 194 | + | set store.queues[target].count += 1; |
|
| 195 | + | } |
|
| 196 | + | ||
| 197 | + | /// Initialize all request capacity before secondary harts start. |
|
| 198 | + | export fn initialize(store: &mut Store, online: u32) { |
|
| 199 | + | assert online <> 0 and (online >> limits::HARTS) == 0; |
|
| 200 | + | set store.online = online; |
|
| 201 | + | slots::initialize(&mut store.slots[..]); |
|
| 202 | + | for hart in 0..limits::HARTS { |
|
| 203 | + | set store.queues[hart].read = 0; |
|
| 204 | + | set store.queues[hart].write = 0; |
|
| 205 | + | set store.queues[hart].count = 0; |
|
| 206 | + | } |
|
| 207 | + | } |
|
| 208 | + | ||
| 209 | + | /// Check an online hart before indexing its mailbox. |
|
| 210 | + | fn hart(store: &Store, id: u32) throws (abi::Error) { |
|
| 211 | + | if id >= limits::HARTS or (store.online & (1 << id)) == 0 { |
|
| 212 | + | throw abi::Error::InvalidArg; |
|
| 213 | + | } |
|
| 214 | + | } |
|
| 215 | + | ||
| 216 | + | /// Enqueue a request atomically under metadata serialization. |
|
| 217 | + | export fn send(store: &mut Store, source: u32, target: u32, action: Action) -> abi::Ref |
|
| 218 | + | throws (abi::Error) |
|
| 219 | + | { |
|
| 220 | + | try hart(store, source); |
|
| 221 | + | try hart(store, target); |
|
| 222 | + | if store.queues[target].count == limits::REMOTE_PER_HART { |
|
| 223 | + | throw abi::Error::Busy; |
|
| 224 | + | } |
|
| 225 | + | let pending = try slots::reserve(&mut store.slots[..]) catch { |
|
| 226 | + | throw abi::Error::Busy; |
|
| 227 | + | }; |
|
| 228 | + | let id = slots::reference(&pending); |
|
| 229 | + | set store.requests[id.index] = Request { |
|
| 230 | + | source, target, action, state: State::Pending, reply: Reply::Value(0) |
|
| 231 | + | }; |
|
| 232 | + | enqueue(store, target, id); |
|
| 233 | + | return try! slots::commit(&mut store.slots[..], pending); |
|
| 234 | + | } |
|
| 235 | + | ||
| 236 | + | /// Take the next live request, discarding stale or completed queue references. |
|
| 237 | + | export fn take(store: &mut Store, target: u32) -> ?Delivery throws (abi::Error) { |
|
| 238 | + | try hart(store, target); |
|
| 239 | + | for i in 0..limits::REMOTE_PER_HART { |
|
| 240 | + | if store.queues[target].count == 0 { |
|
| 241 | + | return nil; |
|
| 242 | + | } |
|
| 243 | + | let index = store.queues[target].read; |
|
| 244 | + | let id = store.queues[target].entries[index]; |
|
| 245 | + | set store.queues[target].read = (index + 1) % limits::REMOTE_PER_HART; |
|
| 246 | + | set store.queues[target].count -= 1; |
|
| 247 | + | if not slots::matches(&store.slots[..], id, slots::State::Live) { |
|
| 248 | + | continue; |
|
| 249 | + | } |
|
| 250 | + | let request = store.requests[id.index]; |
|
| 251 | + | if request.target <> target or request.state <> State::Pending { |
|
| 252 | + | continue; |
|
| 253 | + | } |
|
| 254 | + | set store.requests[id.index].state = State::Delivered; |
|
| 255 | + | return Delivery { id, request: store.requests[id.index] }; |
|
| 256 | + | } |
|
| 257 | + | return nil; |
|
| 258 | + | } |
|
| 259 | + | ||
| 260 | + | /// Acknowledge one delivered generation exactly once under metadata serialization. |
|
| 261 | + | export fn acknowledge(store: &mut Store, target: u32, id: abi::Ref) -> bool { |
|
| 262 | + | if not slots::matches(&store.slots[..], id, slots::State::Live) { |
|
| 263 | + | return false; |
|
| 264 | + | } |
|
| 265 | + | let request = store.requests[id.index]; |
|
| 266 | + | if request.target <> target or request.state <> State::Delivered { |
|
| 267 | + | return false; |
|
| 268 | + | } |
|
| 269 | + | set store.requests[id.index].state = State::Acknowledged; |
|
| 270 | + | return true; |
|
| 271 | + | } |
|
| 272 | + | ||
| 273 | + | /// Resolve a live request through its issuing hart. |
|
| 274 | + | fn owned(store: &Store, source: u32, id: abi::Ref) -> Request throws (abi::Error) { |
|
| 275 | + | if not slots::matches(&store.slots[..], id, slots::State::Live) { |
|
| 276 | + | throw abi::Error::BadHandle; |
|
| 277 | + | } |
|
| 278 | + | let request = store.requests[id.index]; |
|
| 279 | + | if request.source <> source { |
|
| 280 | + | throw abi::Error::BadHandle; |
|
| 281 | + | } |
|
| 282 | + | return request; |
|
| 283 | + | } |
|
| 284 | + | ||
| 285 | + | /// Test completion while retaining the acknowledgement record. |
|
| 286 | + | export fn completed(store: &Store, source: u32, id: abi::Ref) -> bool throws (abi::Error) { |
|
| 287 | + | return (try owned(store, source, id)).state == State::Acknowledged; |
|
| 288 | + | } |
|
| 289 | + | ||
| 290 | + | /// Store a result before the receiving hart acknowledges its delivery. |
|
| 291 | + | export fn respond(store: &mut Store, target: u32, id: abi::Ref, reply: Reply) { |
|
| 292 | + | assert slots::matches(&store.slots[..], id, slots::State::Live); |
|
| 293 | + | assert store.requests[id.index].target == target |
|
| 294 | + | and store.requests[id.index].state == State::Delivered; |
|
| 295 | + | set store.requests[id.index].reply = reply; |
|
| 296 | + | } |
|
| 297 | + | ||
| 298 | + | /// Read a completed operation's result without releasing its record. |
|
| 299 | + | export fn result(store: &Store, source: u32, id: abi::Ref) -> u64 throws (abi::Error) { |
|
| 300 | + | let request = try owned(store, source, id); |
|
| 301 | + | if request.state <> State::Acknowledged { |
|
| 302 | + | throw abi::Error::Busy; |
|
| 303 | + | } |
|
| 304 | + | match request.reply { |
|
| 305 | + | case Reply::Value(value) => return value, |
|
| 306 | + | case Reply::Error(error) => throw error, |
|
| 307 | + | } |
|
| 308 | + | } |
|
| 309 | + | ||
| 310 | + | /// Release an acknowledged request and advance its generation. |
|
| 311 | + | export fn release(store: &mut Store, source: u32, id: abi::Ref) throws (abi::Error) { |
|
| 312 | + | if (try owned(store, source, id)).state <> State::Acknowledged { |
|
| 313 | + | throw abi::Error::Busy; |
|
| 314 | + | } |
|
| 315 | + | try! slots::release(&mut store.slots[..], id); |
|
| 316 | + | } |
|
| 317 | + | ||
| 318 | + | /// Cancel queued work or discard a completed acknowledgement. |
|
| 319 | + | export fn cancel(store: &mut Store, source: u32, id: abi::Ref) throws (abi::Error) { |
|
| 320 | + | if (try owned(store, source, id)).state == State::Delivered { |
|
| 321 | + | throw abi::Error::Busy; |
|
| 322 | + | } |
|
| 323 | + | try! slots::release(&mut store.slots[..], id); |
|
| 324 | + | } |
kernel/kernel/slots.rad
added
+105 -0
| 1 | + | //! Bounded slot lifetimes and linear reservations. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | use super::limits; |
|
| 5 | + | ||
| 6 | + | /// Slot lifetime state. |
|
| 7 | + | export union State: Copy { |
|
| 8 | + | /// Available for reservation. |
|
| 9 | + | Free, |
|
| 10 | + | /// Owned by an uncommitted reservation. |
|
| 11 | + | Reserved, |
|
| 12 | + | /// Published object. |
|
| 13 | + | Live, |
|
| 14 | + | /// Generation space is exhausted; the slot cannot be reused. |
|
| 15 | + | Retired, |
|
| 16 | + | } |
|
| 17 | + | ||
| 18 | + | /// Metadata stored beside an object table's payload array. |
|
| 19 | + | export record Slot: Copy { |
|
| 20 | + | /// Current nonzero generation. |
|
| 21 | + | generation: u32, |
|
| 22 | + | /// Current lifetime state. |
|
| 23 | + | state: State, |
|
| 24 | + | } |
|
| 25 | + | ||
| 26 | + | /// A reserved slot that must be committed or cancelled in the same table. |
|
| 27 | + | export union Reservation: Once { |
|
| 28 | + | /// Unpublished object slot. |
|
| 29 | + | Held(abi::Ref), |
|
| 30 | + | } |
|
| 31 | + | ||
| 32 | + | /// Initialize fresh metadata. No live references may name this table. |
|
| 33 | + | export fn initialize(slots: &mut [Slot]) { |
|
| 34 | + | for i in 0..slots.len { |
|
| 35 | + | set slots[i] = Slot { generation: 1, state: State::Free }; |
|
| 36 | + | } |
|
| 37 | + | } |
|
| 38 | + | ||
| 39 | + | /// Reserve the first available slot with a bounded scan. |
|
| 40 | + | export fn reserve(slots: &mut [Slot]) -> Reservation throws (abi::Error) { |
|
| 41 | + | for i in 0..slots.len { |
|
| 42 | + | if slots[i].state == State::Free { |
|
| 43 | + | set slots[i].state = State::Reserved; |
|
| 44 | + | return Reservation::Held(abi::Ref { index: i, generation: slots[i].generation }); |
|
| 45 | + | } |
|
| 46 | + | } |
|
| 47 | + | throw abi::Error::Exhausted; |
|
| 48 | + | } |
|
| 49 | + | ||
| 50 | + | /// Check an internal reference against its table and required lifetime state. |
|
| 51 | + | export fn matches(slots: &[Slot], object: abi::Ref, state: State) -> bool { |
|
| 52 | + | return object.index < slots.len |
|
| 53 | + | and slots[object.index].generation == object.generation |
|
| 54 | + | and slots[object.index].state == state; |
|
| 55 | + | } |
|
| 56 | + | ||
| 57 | + | /// Inspect a reservation's unpublished reference without consuming it. |
|
| 58 | + | export fn reference(reservation: &Reservation) -> abi::Ref { |
|
| 59 | + | match reservation { |
|
| 60 | + | case Reservation::Held(object) => return *object, |
|
| 61 | + | } |
|
| 62 | + | } |
|
| 63 | + | ||
| 64 | + | /// Publish a reserved slot. The caller must initialize its payload first. |
|
| 65 | + | export fn commit(slots: &mut [Slot], reservation: Reservation) -> abi::Ref throws (abi::Error) { |
|
| 66 | + | match reservation { |
|
| 67 | + | case Reservation::Held(object) => { |
|
| 68 | + | if not matches(slots, object, State::Reserved) { |
|
| 69 | + | throw abi::Error::BadHandle; |
|
| 70 | + | } |
|
| 71 | + | set slots[object.index].state = State::Live; |
|
| 72 | + | return object; |
|
| 73 | + | } |
|
| 74 | + | } |
|
| 75 | + | } |
|
| 76 | + | ||
| 77 | + | /// Advance the slot generation or retire it permanently. |
|
| 78 | + | fn advance(slot: &mut Slot) { |
|
| 79 | + | if slot.generation == limits::LAST_GENERATION { |
|
| 80 | + | set slot.state = State::Retired; |
|
| 81 | + | } else { |
|
| 82 | + | set slot.generation += 1; |
|
| 83 | + | set slot.state = State::Free; |
|
| 84 | + | } |
|
| 85 | + | } |
|
| 86 | + | ||
| 87 | + | /// Cancel a reservation and invalidate its generation. |
|
| 88 | + | export fn cancel(slots: &mut [Slot], reservation: Reservation) throws (abi::Error) { |
|
| 89 | + | match reservation { |
|
| 90 | + | case Reservation::Held(object) => { |
|
| 91 | + | if not matches(slots, object, State::Reserved) { |
|
| 92 | + | throw abi::Error::BadHandle; |
|
| 93 | + | } |
|
| 94 | + | advance(&mut slots[object.index]); |
|
| 95 | + | } |
|
| 96 | + | } |
|
| 97 | + | } |
|
| 98 | + | ||
| 99 | + | /// Invalidate a live object after all payload-specific teardown is complete. |
|
| 100 | + | export fn release(slots: &mut [Slot], object: abi::Ref) throws (abi::Error) { |
|
| 101 | + | if not matches(slots, object, State::Live) { |
|
| 102 | + | throw abi::Error::BadHandle; |
|
| 103 | + | } |
|
| 104 | + | advance(&mut slots[object.index]); |
|
| 105 | + | } |
kernel/kernel/startup.rad
added
+156 -0
| 1 | + | //! Initial root resources and execution authority. |
|
| 2 | + | use super::state; |
|
| 3 | + | use super::abi; |
|
| 4 | + | use super::platform; |
|
| 5 | + | use super::limits; |
|
| 6 | + | use super::domains; |
|
| 7 | + | use super::pages; |
|
| 8 | + | use super::physical; |
|
| 9 | + | use super::registry; |
|
| 10 | + | use super::devices; |
|
| 11 | + | use super::interrupts; |
|
| 12 | + | use super::budgets; |
|
| 13 | + | use super::dispatch; |
|
| 14 | + | use super::slots; |
|
| 15 | + | use super::capability; |
|
| 16 | + | use std::mem; |
|
| 17 | + | use super::timers; |
|
| 18 | + | ||
| 19 | + | /// Pages allocated for the initial root user stack. |
|
| 20 | + | constant ROOT_STACK_PAGES: u32 = 16; |
|
| 21 | + | ||
| 22 | + | /// Number of 64-bit words in `abi::BootInfo` before its handle array. |
|
| 23 | + | constant HEADER_WORDS: u64 = 10; |
|
| 24 | + | ||
| 25 | + | /// Install root's fixed resource slots and activate its initial context on hart zero. |
|
| 26 | + | /// Boot calls this after global and per-hart initialization, before user dispatch, |
|
| 27 | + | /// with the only access to the shared metadata. |
|
| 28 | + | export fn create( |
|
| 29 | + | machine: &platform::Platform, shared: &mut state::Shared, image: abi::Ref, scheduler: abi::Ref |
|
| 30 | + | ) -> abi::Ref throws (abi::Error) { |
|
| 31 | + | if (machine.harts & 1) == 0 or machine.timebase == 0 { |
|
| 32 | + | throw abi::Error::InvalidArg; |
|
| 33 | + | } |
|
| 34 | + | let root = try domains::bootstrap( |
|
| 35 | + | &mut shared.domains, &mut shared.pages.backings, &shared.registry, image |
|
| 36 | + | ); |
|
| 37 | + | let domain = try! domains::get(&shared.domains, root); |
|
| 38 | + | // The root was just published, and the exclusive metadata means nothing else |
|
| 39 | + | // reaches its memory; see `Domain.memory`. |
|
| 40 | + | unsafe { |
|
| 41 | + | let memory = &mut *domain.memory; |
|
| 42 | + | try populate(machine, shared, &domain, memory, root, image, scheduler); |
|
| 43 | + | } |
|
| 44 | + | return root; |
|
| 45 | + | } |
|
| 46 | + | ||
| 47 | + | /// Fill the root domain's table and boot record, then activate its initial context. |
|
| 48 | + | fn populate( |
|
| 49 | + | machine: &platform::Platform, |
|
| 50 | + | shared: &mut state::Shared, |
|
| 51 | + | domain: &domains::Domain, |
|
| 52 | + | memory: &mut domains::Memory, |
|
| 53 | + | root: abi::Ref, |
|
| 54 | + | image: abi::Ref, |
|
| 55 | + | scheduler: abi::Ref |
|
| 56 | + | ) |
|
| 57 | + | throws (abi::Error) |
|
| 58 | + | { |
|
| 59 | + | let self = try! abi::handle(abi::Kind::Domain, abi::Ref { |
|
| 60 | + | index: 0, generation: memory.table.slots[0].generation |
|
| 61 | + | }); |
|
| 62 | + | let mut clock = abi::Handle(0); |
|
| 63 | + | let mut shutdown = abi::Handle(0); |
|
| 64 | + | for i in 0..shared.devices.count { |
|
| 65 | + | let handle = try devices::install(&shared.devices, &mut memory.table, i); |
|
| 66 | + | if shared.devices.regions[i].kind == platform::Kind::Clint { |
|
| 67 | + | set clock = handle; |
|
| 68 | + | } |
|
| 69 | + | if shared.devices.regions[i].kind == platform::Kind::Finish { |
|
| 70 | + | set shutdown = handle; |
|
| 71 | + | } |
|
| 72 | + | } |
|
| 73 | + | for number in 1..shared.interrupts.count + 1 { |
|
| 74 | + | let handle = try interrupts::seed(&mut shared.interrupts, &mut memory.table, number); |
|
| 75 | + | } |
|
| 76 | + | let now = dispatch::clock(); |
|
| 77 | + | let second = machine.timebase as u64; |
|
| 78 | + | if now >= mem::U64_MAX - second { |
|
| 79 | + | throw abi::Error::InvalidArg; |
|
| 80 | + | } |
|
| 81 | + | // Each hart's authority has no end, because the kernel never creates budget |
|
| 82 | + | // after boot. A scheduler divides this interval for the life of the system. |
|
| 83 | + | let start = now + second; |
|
| 84 | + | let end: u64 = timers::NO_DEADLINE; |
|
| 85 | + | let mut count = 2 + shared.devices.count + shared.interrupts.count; |
|
| 86 | + | for hart in 0..limits::HARTS { |
|
| 87 | + | if (machine.harts & (1 << hart)) == 0 { |
|
| 88 | + | continue; |
|
| 89 | + | } |
|
| 90 | + | let handle = try budgets::seed(&mut shared.budgets, &mut memory.table, hart, start, end); |
|
| 91 | + | if hart == 0 { |
|
| 92 | + | try budgets::bind( |
|
| 93 | + | &mut shared.budgets, |
|
| 94 | + | &shared.domains, |
|
| 95 | + | &memory.table, |
|
| 96 | + | budgets::Binding { budget: handle, domain: self, context: domain.initial }, |
|
| 97 | + | now |
|
| 98 | + | ); |
|
| 99 | + | } |
|
| 100 | + | set count += 1; |
|
| 101 | + | } |
|
| 102 | + | let rootImage = try registry::install(&shared.registry, &mut memory.table, image); |
|
| 103 | + | let schedulerImage = try registry::install(&shared.registry, &mut memory.table, scheduler); |
|
| 104 | + | let stack = try pages::allocate( |
|
| 105 | + | &mut shared.pages, &mut memory.table, self, ROOT_STACK_PAGES |
|
| 106 | + | ); |
|
| 107 | + | let arguments = try pages::allocate(&mut shared.pages, &mut memory.table, self, 1); |
|
| 108 | + | let argumentPage = try! pages::get( |
|
| 109 | + | &shared.pages, |
|
| 110 | + | (try! capability::get( |
|
| 111 | + | &memory.table, arguments |
|
| 112 | + | )).object |
|
| 113 | + | ); |
|
| 114 | + | // The header is written as words, so every field before `handles` must be a u64 or a |
|
| 115 | + | // Handle and the handle array must fill the rest of the record. |
|
| 116 | + | assert abi::BOOT_HANDLES == 2 + limits::DEVICES + limits::INTERRUPTS + limits::HARTS; |
|
| 117 | + | assert @sizeOf(abi::BootInfo) as u64 == (HEADER_WORDS + abi::BOOT_HANDLES as u64) * 8; |
|
| 118 | + | assert @sizeOf(abi::BootInfo) as u64 <= limits::FRAME_SIZE; |
|
| 119 | + | assert count <= abi::BOOT_HANDLES; |
|
| 120 | + | let mut words: [u64; abi::BOOT_HANDLES] = undefined; |
|
| 121 | + | for i in 0..count { |
|
| 122 | + | assert memory.table.slots[i].state == slots::State::Live; |
|
| 123 | + | set words[i] = *try! abi::handle(memory.table.entries[i].kind, abi::Ref { |
|
| 124 | + | index: i, generation: memory.table.slots[i].generation |
|
| 125 | + | }); |
|
| 126 | + | } |
|
| 127 | + | // The argument page is allocated zeroed, so the array past `count` stays zero. |
|
| 128 | + | try! physical::writeWords( |
|
| 129 | + | &shared.pages.backings.pool, |
|
| 130 | + | argumentPage.base, |
|
| 131 | + | &[ |
|
| 132 | + | shared.devices.count as u64, |
|
| 133 | + | shared.interrupts.count as u64, |
|
| 134 | + | machine.harts as u64, |
|
| 135 | + | second, |
|
| 136 | + | *rootImage, |
|
| 137 | + | *schedulerImage, |
|
| 138 | + | *clock, |
|
| 139 | + | *shutdown, |
|
| 140 | + | *arguments, |
|
| 141 | + | count as u64, |
|
| 142 | + | ] |
|
| 143 | + | ); |
|
| 144 | + | try! physical::writeWords( |
|
| 145 | + | &shared.pages.backings.pool, argumentPage.base + HEADER_WORDS * 8, &words[..count] |
|
| 146 | + | ); |
|
| 147 | + | try domains::activate( |
|
| 148 | + | &mut shared.domains, |
|
| 149 | + | &mut shared.pages, |
|
| 150 | + | root, |
|
| 151 | + | memory, |
|
| 152 | + | stack, |
|
| 153 | + | arguments, |
|
| 154 | + | @sizeOf(abi::BootInfo) as u64 |
|
| 155 | + | ); |
|
| 156 | + | } |
kernel/kernel/state.rad
added
+64 -0
| 1 | + | //! Kernel metadata serialized by the metadata lock. |
|
| 2 | + | ||
| 3 | + | use super::limits; |
|
| 4 | + | use super::pages; |
|
| 5 | + | use super::domains; |
|
| 6 | + | use super::budgets; |
|
| 7 | + | use super::registry; |
|
| 8 | + | use super::lifecycle; |
|
| 9 | + | use super::loader; |
|
| 10 | + | use super::timers; |
|
| 11 | + | use super::remote; |
|
| 12 | + | use super::interrupts; |
|
| 13 | + | use super::devices; |
|
| 14 | + | ||
| 15 | + | /// Shared maintenance schedule, serialized with the other metadata. |
|
| 16 | + | export record Maintenance: Copy { |
|
| 17 | + | /// Earliest tick at which a hart may run a maintenance pass. |
|
| 18 | + | next: u64, |
|
| 19 | + | /// Hart with a timer armed no later than `next`, if any. |
|
| 20 | + | hart: ?u32, |
|
| 21 | + | /// Ticks to wait after a due pass before the next one. Grows while passes |
|
| 22 | + | /// make no progress and returns to the configured base otherwise. |
|
| 23 | + | interval: u64, |
|
| 24 | + | } |
|
| 25 | + | ||
| 26 | + | /// Every kernel store that the metadata lock serializes. |
|
| 27 | + | /// |
|
| 28 | + | /// Each field is read and written only while the accessing hart holds the |
|
| 29 | + | /// metadata lock, with one exception: `devices` is written by hart zero during |
|
| 30 | + | /// boot and is read-only afterwards. Hart-local state (dispatch runtimes, trap |
|
| 31 | + | /// anchors), the published platform record, and the lock's own ticket words |
|
| 32 | + | /// are synchronized differently and stay outside this record. |
|
| 33 | + | export record Shared { |
|
| 34 | + | /// Physical frames, page ownership, and backing storage. Lock-serialized. |
|
| 35 | + | pages: pages::Store, |
|
| 36 | + | /// Domain records, execution contexts, and the event store. Lock-serialized. |
|
| 37 | + | domains: domains::Store, |
|
| 38 | + | /// Exclusive CPU intervals. Lock-serialized. |
|
| 39 | + | budgets: budgets::Store, |
|
| 40 | + | /// Resident package registry. Lock-serialized. |
|
| 41 | + | registry: registry::Store, |
|
| 42 | + | /// Calls that retain storage across lock releases. Lock-serialized. |
|
| 43 | + | lifecycle: lifecycle::Calls, |
|
| 44 | + | /// Runtime-loader ownership and resident package liveness. Lock-serialized. |
|
| 45 | + | loader: loader::State, |
|
| 46 | + | /// Per-hart timeouts. Lock-serialized. |
|
| 47 | + | timers: timers::Store, |
|
| 48 | + | /// Cross-hart request queues. Lock-serialized. |
|
| 49 | + | remote: remote::Store, |
|
| 50 | + | /// Global interrupt ownership. Lock-serialized. |
|
| 51 | + | interrupts: interrupts::Store, |
|
| 52 | + | /// Discovered devices. Written by hart zero during boot, read-only afterwards. |
|
| 53 | + | devices: devices::Store, |
|
| 54 | + | /// Maintenance schedule. Lock-serialized. |
|
| 55 | + | maintenance: Maintenance, |
|
| 56 | + | } |
|
| 57 | + | ||
| 58 | + | /// The kernel's lock-serialized metadata. |
|
| 59 | + | /// |
|
| 60 | + | /// Access it only while the accessing hart holds the metadata lock, or on |
|
| 61 | + | /// hart zero during boot before any other hart passes the platform |
|
| 62 | + | /// publication barrier and before dispatch starts. Entry points take the |
|
| 63 | + | /// lock, reach this static once, and pass the stores as `&mut` arguments. |
|
| 64 | + | export unsafe static SHARED: Shared = undefined; |
kernel/kernel/sync.rad
added
+83 -0
| 1 | + | //! RV64 synchronization. |
|
| 2 | + | ||
| 3 | + | use std::mem; |
|
| 4 | + | // The lock's own words. Only `enter` and `leave` reach them. |
|
| 5 | + | ||
| 6 | + | /// Bits retained when a 32-bit lock ticket wraps. |
|
| 7 | + | constant TICKET_MASK: u64 = mem::U32_MAX as u64; |
|
| 8 | + | ||
| 9 | + | /// Next metadata ticket, modulo 2^32. Each hart can hold at most one ticket. |
|
| 10 | + | unsafe static NEXT: u32 = 0; |
|
| 11 | + | /// Metadata ticket permitted to enter the critical section. |
|
| 12 | + | unsafe static SERVING: u32 = 0; |
|
| 13 | + | ||
| 14 | + | /// Interrupt state and ticket retained by one metadata transaction. |
|
| 15 | + | export record Section: Copy { |
|
| 16 | + | /// Original machine interrupt-enable bit. |
|
| 17 | + | interrupts: u64, |
|
| 18 | + | /// Ticket held by the acquiring hart until release. |
|
| 19 | + | ticket: u32, |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | /// Lock ownership that must be released on its acquiring hart. |
|
| 23 | + | export union Guard: Once { |
|
| 24 | + | /// One serialized metadata transaction with machine interrupts masked. |
|
| 25 | + | Held(Section), |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | /// Mask machine interrupts and return the original enable bit. |
|
| 29 | + | /// Callers must pair it with restoreInterrupts to keep critical sections balanced. |
|
| 30 | + | export unsafe fn maskInterrupts() -> u64; |
|
| 31 | + | /// Restore the supplied machine interrupt-enable bit. |
|
| 32 | + | /// The value must come from the matching maskInterrupts call. |
|
| 33 | + | export unsafe fn restoreInterrupts(value: u64); |
|
| 34 | + | ||
| 35 | + | /// Acquire shared metadata in ticket order with local interrupts masked. |
|
| 36 | + | /// Calls must not nest. At most one ticket per online hart can be outstanding. |
|
| 37 | + | export fn enter() -> Guard { |
|
| 38 | + | let mut interrupts: u64 = 0; |
|
| 39 | + | let mut ticket: u32 = 0; |
|
| 40 | + | // The linear Guard returned below is consumed by `leave`, which restores this |
|
| 41 | + | // exact interrupt state, so masking stays balanced. Tickets are granted in order |
|
| 42 | + | // and each hart holds at most one, so SERVING reaches ours exactly once, and the |
|
| 43 | + | // ticket holder is the only writer of SERVING. |
|
| 44 | + | unsafe { |
|
| 45 | + | set interrupts = maskInterrupts(); |
|
| 46 | + | set ticket = nextTicket(&mut NEXT); |
|
| 47 | + | while loadAcquire32(&SERVING) <> ticket {} |
|
| 48 | + | } |
|
| 49 | + | return Guard::Held(Section { interrupts, ticket }); |
|
| 50 | + | } |
|
| 51 | + | ||
| 52 | + | /// Publish metadata and restore the acquiring hart's interrupt state. |
|
| 53 | + | export fn leave(guard: Guard) { |
|
| 54 | + | match guard { |
|
| 55 | + | case Guard::Held(section) => { |
|
| 56 | + | // The ticket holder advances SERVING. The Section holds the interrupt |
|
| 57 | + | // state saved by the matching `enter`. |
|
| 58 | + | unsafe { |
|
| 59 | + | storeRelease32( |
|
| 60 | + | &mut SERVING, |
|
| 61 | + | ((section.ticket as u64 |
|
| 62 | + | + 1) & TICKET_MASK) as u32 |
|
| 63 | + | ); |
|
| 64 | + | restoreInterrupts(section.interrupts); |
|
| 65 | + | } |
|
| 66 | + | } |
|
| 67 | + | } |
|
| 68 | + | } |
|
| 69 | + | ||
| 70 | + | /// Allocate one wrapping ticket from a naturally aligned u32 counter. |
|
| 71 | + | export fn nextTicket(counter: &mut u32) -> u32; |
|
| 72 | + | /// Read a naturally aligned shared word with acquire ordering. |
|
| 73 | + | export fn loadAcquire(value: &u64) -> u64; |
|
| 74 | + | /// Publish a naturally aligned shared word with release ordering. |
|
| 75 | + | export fn storeRelease(value: &mut u64, next: u64); |
|
| 76 | + | /// Read a naturally aligned shared 32-bit word with acquire ordering. |
|
| 77 | + | export fn loadAcquire32(value: &u32) -> u32; |
|
| 78 | + | /// Publish a naturally aligned shared 32-bit word with release ordering. |
|
| 79 | + | export fn storeRelease32(value: &mut u32, next: u32); |
|
| 80 | + | /// Add to a naturally aligned shared word and return its previous value. |
|
| 81 | + | export fn fetchAdd(value: &mut u64, amount: u64) -> u64; |
|
| 82 | + | /// Synchronize local instruction fetch after executable bytes become visible. |
|
| 83 | + | export fn syncInstructions(); |
kernel/kernel/sync.ras
added
+68 -0
| 1 | + | //! RV64 synchronization boundary. Shared words must be naturally aligned. |
|
| 2 | + | // Machine interrupt-enable bit in mstatus. |
|
| 3 | + | .constant SYNC_MIE 8; |
|
| 4 | + | ||
| 5 | + | .text; |
|
| 6 | + | .export @kernel::sync::nextTicket; |
|
| 7 | + | .export @kernel::sync::loadAcquire; |
|
| 8 | + | .export @kernel::sync::storeRelease; |
|
| 9 | + | .export @kernel::sync::loadAcquire32; |
|
| 10 | + | .export @kernel::sync::storeRelease32; |
|
| 11 | + | .export @kernel::sync::fetchAdd; |
|
| 12 | + | .export @kernel::sync::syncInstructions; |
|
| 13 | + | .export @kernel::sync::maskInterrupts; |
|
| 14 | + | .export @kernel::sync::restoreInterrupts; |
|
| 15 | + | ||
| 16 | + | // Preserve the previous interrupt-enable bit while masking machine interrupts. |
|
| 17 | + | @kernel::sync::maskInterrupts |
|
| 18 | + | csrrci %a0 mstatus SYNC_MIE; |
|
| 19 | + | andi %a0 %a0 SYNC_MIE; |
|
| 20 | + | ret; |
|
| 21 | + | ||
| 22 | + | // Restore interrupt delivery while preserving other machine status fields. |
|
| 23 | + | @kernel::sync::restoreInterrupts |
|
| 24 | + | csrci mstatus SYNC_MIE; |
|
| 25 | + | csrs mstatus %a0; |
|
| 26 | + | ret; |
|
| 27 | + | ||
| 28 | + | // Allocate a wrapping ticket and return its zero-extended u32 value. |
|
| 29 | + | @kernel::sync::nextTicket |
|
| 30 | + | li %t0 1; |
|
| 31 | + | amoadd.w.aqrl %a0 %t0 (%a0); |
|
| 32 | + | slli %a0 %a0 32; |
|
| 33 | + | srli %a0 %a0 32; |
|
| 34 | + | ret; |
|
| 35 | + | ||
| 36 | + | // Order subsequent memory accesses after this load. |
|
| 37 | + | @kernel::sync::loadAcquire |
|
| 38 | + | ld %a0 0(%a0); |
|
| 39 | + | fence r rw; |
|
| 40 | + | ret; |
|
| 41 | + | ||
| 42 | + | // Order preceding memory accesses before this store. |
|
| 43 | + | @kernel::sync::storeRelease |
|
| 44 | + | fence rw w; |
|
| 45 | + | sd %a1 0(%a0); |
|
| 46 | + | ret; |
|
| 47 | + | ||
| 48 | + | // Order subsequent memory accesses after a zero-extended word load. |
|
| 49 | + | @kernel::sync::loadAcquire32 |
|
| 50 | + | lwu %a0 0(%a0); |
|
| 51 | + | fence r rw; |
|
| 52 | + | ret; |
|
| 53 | + | ||
| 54 | + | // Order preceding memory accesses before a word store. |
|
| 55 | + | @kernel::sync::storeRelease32 |
|
| 56 | + | fence rw w; |
|
| 57 | + | sw %a1 0(%a0); |
|
| 58 | + | ret; |
|
| 59 | + | ||
| 60 | + | // Atomically add with acquire and release ordering. |
|
| 61 | + | @kernel::sync::fetchAdd |
|
| 62 | + | amoadd.d.aqrl %a0 %a1 (%a0); |
|
| 63 | + | ret; |
|
| 64 | + | ||
| 65 | + | // Refresh the local instruction stream after code publication. |
|
| 66 | + | @kernel::sync::syncInstructions |
|
| 67 | + | fence.i; |
|
| 68 | + | ret; |
kernel/kernel/sys.rad
added
+312 -0
| 1 | + | //! Typed user entry points for the RV64 direct-call ABI. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | ||
| 5 | + | /// Issue one machine call with four scalar arguments. |
|
| 6 | + | @intrinsic fn ecall(operation: u32, a0: i64, a1: i64, a2: i64, a3: i64) -> i64; |
|
| 7 | + | ||
| 8 | + | /// Issue one machine call with four scalar arguments and a buffer. The kernel reads and |
|
| 9 | + | /// writes only the buffer of the caller, which is plain data, never the arguments. |
|
| 10 | + | @intrinsic fn ecallBuffer( |
|
| 11 | + | operation: u32, a0: i64, a1: i64, a2: i64, a3: i64, buffer: &mut opaque |
|
| 12 | + | ) -> i64; |
|
| 13 | + | ||
| 14 | + | /// Reach the memory behind a Page or Events handle as a read-only slice of bytes. The |
|
| 15 | + | /// flag tells whether the access is for writing, and is a constant. |
|
| 16 | + | @intrinsic fn pageSliceRead(handle: u64, write: bool, out: &mut *[u8]) -> i64; |
|
| 17 | + | ||
| 18 | + | /// Reach the memory behind a Page or Events handle as a writable slice of bytes. |
|
| 19 | + | @intrinsic fn pageSliceWrite(handle: u64, write: bool, out: &mut *mut [u8]) -> i64; |
|
| 20 | + | ||
| 21 | + | /// Reach the memory of a Page as a read-only slice of boot records. |
|
| 22 | + | @intrinsic fn pageSliceBoot(handle: u64, write: bool, out: &mut *[abi::BootInfo]) -> i64; |
|
| 23 | + | ||
| 24 | + | /// Reach the memory of a Page as a writable slice of boot records. |
|
| 25 | + | @intrinsic fn pageSliceBootMut(handle: u64, write: bool, out: &mut *mut [abi::BootInfo]) -> i64; |
|
| 26 | + | ||
| 27 | + | /// Reach the memory of a Page as a read-only slice of words. |
|
| 28 | + | @intrinsic fn pageSliceWords(handle: u64, write: bool, out: &mut *[u64]) -> i64; |
|
| 29 | + | ||
| 30 | + | /// Reach the memory of a Page as a writable slice of words. |
|
| 31 | + | @intrinsic fn pageSliceWordsMut(handle: u64, write: bool, out: &mut *mut [u64]) -> i64; |
|
| 32 | + | ||
| 33 | + | /// Translate scalar ABI errors into Radiance throws. |
|
| 34 | + | fn result(value: i64) -> u64 throws (abi::Error) { |
|
| 35 | + | if value >= 0 { |
|
| 36 | + | return value as u64; |
|
| 37 | + | } |
|
| 38 | + | match value { |
|
| 39 | + | case -(abi::Error::BadHandle as i64) => throw abi::Error::BadHandle, |
|
| 40 | + | case -(abi::Error::Denied as i64) => throw abi::Error::Denied, |
|
| 41 | + | case -(abi::Error::OutOfMemory as i64) => throw abi::Error::OutOfMemory, |
|
| 42 | + | case -(abi::Error::InvalidArg as i64) => throw abi::Error::InvalidArg, |
|
| 43 | + | case -(abi::Error::Busy as i64) => throw abi::Error::Busy, |
|
| 44 | + | case -(abi::Error::VerifyFailed as i64) => |
|
| 45 | + | throw abi::Error::VerifyFailed, |
|
| 46 | + | case -(abi::Error::NotPending as i64) => throw abi::Error::NotPending, |
|
| 47 | + | case -(abi::Error::Exhausted as i64) => throw abi::Error::Exhausted, |
|
| 48 | + | else => panic "result: invalid kernel error", |
|
| 49 | + | } |
|
| 50 | + | } |
|
| 51 | + | ||
| 52 | + | /// Copy authority to a domain with optional rights narrowing. |
|
| 53 | + | export fn capabilityGrant(handle: abi::Handle, target: abi::Handle, rights: u64) -> abi::Handle |
|
| 54 | + | throws (abi::Error) |
|
| 55 | + | { |
|
| 56 | + | return abi::Handle(try result(ecall( |
|
| 57 | + | abi::Operation::CapabilityGrant as u32, *handle as i64, *target as i64, rights as i64, 0 |
|
| 58 | + | ))); |
|
| 59 | + | } |
|
| 60 | + | ||
| 61 | + | /// Set a child's lifecycle receiver with Destroy and Wake authority. |
|
| 62 | + | export fn domainReparent(domain: abi::Handle, parent: abi::Handle) throws (abi::Error) { |
|
| 63 | + | try result(ecall(abi::Operation::DomainReparent as u32, *domain as i64, *parent as i64, 0, 0)); |
|
| 64 | + | } |
|
| 65 | + | ||
| 66 | + | /// Load a binary package from a readable Page range. The kernel verifies it. |
|
| 67 | + | export fn imageLoad( |
|
| 68 | + | authority: abi::Handle, source: abi::Handle, offset: u64, length: u64 |
|
| 69 | + | ) -> abi::Handle throws (abi::Error) { |
|
| 70 | + | return abi::Handle(try result(ecall( |
|
| 71 | + | abi::Operation::ImageLoad as u32, |
|
| 72 | + | *authority as i64, |
|
| 73 | + | *source as i64, |
|
| 74 | + | offset as i64, |
|
| 75 | + | length as i64 |
|
| 76 | + | ))); |
|
| 77 | + | } |
|
| 78 | + | ||
| 79 | + | /// Destroy the named domain or the calling domain through the zero sentinel. |
|
| 80 | + | export fn domainDestroy(handle: abi::Handle, flags: u64) throws (abi::Error) { |
|
| 81 | + | try result(ecall(abi::Operation::DomainDestroy as u32, *handle as i64, flags as i64, 0, 0)); |
|
| 82 | + | } |
|
| 83 | + | ||
| 84 | + | /// End the calling domain and report its normal exit status to its parent. |
|
| 85 | + | export fn exit(status: u32) -> ! { |
|
| 86 | + | ecall(abi::Operation::DomainExit as u32, status as i64, 0, 0, 0); |
|
| 87 | + | panic "exit: exit returned"; |
|
| 88 | + | } |
|
| 89 | + | ||
| 90 | + | /// End the calling domain and report an explicit-abort fault to its parent. |
|
| 91 | + | export fn abort() -> ! { |
|
| 92 | + | ecall(abi::Operation::DomainAbort as u32, 0, 0, 0, 0); |
|
| 93 | + | panic "abort: abort returned"; |
|
| 94 | + | } |
|
| 95 | + | ||
| 96 | + | /// Read resident package metadata through a readable Image capability. |
|
| 97 | + | export fn queryImage(handle: abi::Handle) -> abi::ImageInfo throws (abi::Error) { |
|
| 98 | + | let mut info = abi::ImageInfo { id: 0, entry: 0, codeBase: 0, codeBytes: 0, dataBytes: 0 }; |
|
| 99 | + | try result(ecallBuffer(abi::Operation::QueryImage as u32, *handle as i64, 0, 0, 0, &mut info)); |
|
| 100 | + | return info; |
|
| 101 | + | } |
|
| 102 | + | ||
| 103 | + | /// Partition a held CPU window at an interior mtime tick. |
|
| 104 | + | export fn budgetSplit(handle: abi::Handle, at: u64) -> abi::Handle throws (abi::Error) { |
|
| 105 | + | return abi::Handle(try result(ecall( |
|
| 106 | + | abi::Operation::BudgetSplit as u32, *handle as i64, at as i64, 0, 0 |
|
| 107 | + | ))); |
|
| 108 | + | } |
|
| 109 | + | ||
| 110 | + | /// Combine adjacent CPU windows with the same context binding. |
|
| 111 | + | export fn budgetMerge(first: abi::Handle, second: abi::Handle) -> abi::Handle throws (abi::Error) { |
|
| 112 | + | return abi::Handle(try result(ecall( |
|
| 113 | + | abi::Operation::BudgetMerge as u32, *first as i64, *second as i64, 0, 0 |
|
| 114 | + | ))); |
|
| 115 | + | } |
|
| 116 | + | ||
| 117 | + | /// Bind a held CPU window to a context, transferring it to the target domain. |
|
| 118 | + | export fn budgetBind(handle: abi::Handle, domain: abi::Handle, context: u64) -> abi::Handle |
|
| 119 | + | throws (abi::Error) |
|
| 120 | + | { |
|
| 121 | + | return abi::Handle(try result(ecall( |
|
| 122 | + | abi::Operation::BudgetBind as u32, *handle as i64, *domain as i64, context as i64, 0 |
|
| 123 | + | ))); |
|
| 124 | + | } |
|
| 125 | + | ||
| 126 | + | /// Move authority to another domain with optional rights narrowing. |
|
| 127 | + | export fn capabilityTransfer(handle: abi::Handle, target: abi::Handle, rights: u64) -> abi::Handle |
|
| 128 | + | throws (abi::Error) |
|
| 129 | + | { |
|
| 130 | + | return abi::Handle(try result(ecall( |
|
| 131 | + | abi::Operation::CapabilityTransfer as u32, *handle as i64, *target as i64, rights as i64, 0 |
|
| 132 | + | ))); |
|
| 133 | + | } |
|
| 134 | + | ||
| 135 | + | /// Relinquish a handle and its object-specific authority. |
|
| 136 | + | export fn capabilityDrop(handle: abi::Handle) throws (abi::Error) { |
|
| 137 | + | try result(ecall(abi::Operation::CapabilityDrop as u32, *handle as i64, 0, 0, 0)); |
|
| 138 | + | } |
|
| 139 | + | ||
| 140 | + | /// Give the active window's remainder to a target and resume with later authority. |
|
| 141 | + | export fn yield(target: abi::Handle) throws (abi::Error) { |
|
| 142 | + | try result(ecall(abi::Operation::DomainYield as u32, *target as i64, 0, 0, 0)); |
|
| 143 | + | } |
|
| 144 | + | ||
| 145 | + | /// Request a one-shot notification after a positive delay in mtime ticks. |
|
| 146 | + | export fn timeout(delay: u64, token: u32) throws (abi::Error) { |
|
| 147 | + | try result(ecall(abi::Operation::EventTimeout as u32, delay as i64, token as i64, 0, 0)); |
|
| 148 | + | } |
|
| 149 | + | ||
| 150 | + | /// Suspend until a notification is pending and CPU authority permits resumption. |
|
| 151 | + | export fn wait() { |
|
| 152 | + | try! result(ecall(abi::Operation::EventWait as u32, 0, 0, 0, 0)); |
|
| 153 | + | } |
|
| 154 | + | ||
| 155 | + | /// Send a token to a domain with Wake authority without transferring CPU time. |
|
| 156 | + | export fn wakeup(handle: abi::Handle, token: u32) throws (abi::Error) { |
|
| 157 | + | try result(ecall(abi::Operation::EventWakeup as u32, *handle as i64, token as i64, 0, 0)); |
|
| 158 | + | } |
|
| 159 | + | ||
| 160 | + | /// Allocate contiguous zeroed physical frames with self Allocate authority. |
|
| 161 | + | export fn pageAllocate(handle: abi::Handle, count: u64) -> abi::Handle throws (abi::Error) { |
|
| 162 | + | return abi::Handle(try result(ecall( |
|
| 163 | + | abi::Operation::PageAllocate as u32, *handle as i64, count as i64, 0, 0 |
|
| 164 | + | ))); |
|
| 165 | + | } |
|
| 166 | + | ||
| 167 | + | /// Partition an exclusively held Page, returning its right-hand segment. |
|
| 168 | + | export fn pageSplit(handle: abi::Handle, leftCount: u64) -> abi::Handle throws (abi::Error) { |
|
| 169 | + | return abi::Handle(try result(ecall( |
|
| 170 | + | abi::Operation::PageSplit as u32, *handle as i64, leftCount as i64, 0, 0 |
|
| 171 | + | ))); |
|
| 172 | + | } |
|
| 173 | + | ||
| 174 | + | /// Create a pending domain with a private instance of the resident image. |
|
| 175 | + | export fn domainCreate(handle: abi::Handle, image: abi::Handle) -> abi::Handle throws (abi::Error) { |
|
| 176 | + | return abi::Handle(try result(ecall( |
|
| 177 | + | abi::Operation::DomainCreate as u32, *handle as i64, *image as i64, 0, 0 |
|
| 178 | + | ))); |
|
| 179 | + | } |
|
| 180 | + | ||
| 181 | + | /// Activate a pending domain. `stack` is the handle, in the target's table, of a writable |
|
| 182 | + | /// Page that nothing else holds and that was never accessed: the activation consumes it, and |
|
| 183 | + | /// its memory becomes the stack of the initial context. `args` is the handle of a readable |
|
| 184 | + | /// Page of the target, or zero, and `argsSize` bytes of it are the startup arguments. |
|
| 185 | + | export fn domainActivate(handle: abi::Handle, stack: abi::Handle, args: abi::Handle, argsSize: u64) |
|
| 186 | + | throws (abi::Error) |
|
| 187 | + | { |
|
| 188 | + | try result(ecall( |
|
| 189 | + | abi::Operation::DomainActivate as u32, |
|
| 190 | + | *handle as i64, |
|
| 191 | + | *stack as i64, |
|
| 192 | + | *args as i64, |
|
| 193 | + | argsSize as i64 |
|
| 194 | + | )); |
|
| 195 | + | } |
|
| 196 | + | ||
| 197 | + | /// Read Page metadata. An invalid handle is a program error. |
|
| 198 | + | export fn queryPage(handle: abi::Handle) -> abi::PageInfo { |
|
| 199 | + | let mut info = abi::PageInfo { base: 0, count: 0 }; |
|
| 200 | + | try! result(ecallBuffer(abi::Operation::QueryPage as u32, *handle as i64, 0, 0, 0, &mut info)); |
|
| 201 | + | return info; |
|
| 202 | + | } |
|
| 203 | + | ||
| 204 | + | /// Read Device metadata. An invalid handle is a program error. |
|
| 205 | + | export fn queryDevice(handle: abi::Handle) -> abi::DeviceInfo { |
|
| 206 | + | let mut info = abi::DeviceInfo { base: 0, size: 0 }; |
|
| 207 | + | try! result(ecallBuffer( |
|
| 208 | + | abi::Operation::QueryDevice as u32, *handle as i64, 0, 0, 0, &mut info |
|
| 209 | + | )); |
|
| 210 | + | return info; |
|
| 211 | + | } |
|
| 212 | + | ||
| 213 | + | /// Read a physical interrupt source. An invalid handle is a program error. |
|
| 214 | + | export fn queryInterrupt(handle: abi::Handle) -> abi::InterruptInfo { |
|
| 215 | + | let mut info = abi::InterruptInfo { number: 0 }; |
|
| 216 | + | try! result(ecallBuffer( |
|
| 217 | + | abi::Operation::QueryInterrupt as u32, *handle as i64, 0, 0, 0, &mut info |
|
| 218 | + | )); |
|
| 219 | + | return info; |
|
| 220 | + | } |
|
| 221 | + | ||
| 222 | + | /// Read domain identity and lifetime. An invalid handle is a program error. |
|
| 223 | + | export fn queryDomain(handle: abi::Handle) -> abi::DomainInfo { |
|
| 224 | + | let mut info = abi::DomainInfo { id: 0, creator: 0, parent: 0, state: 0 }; |
|
| 225 | + | try! result(ecallBuffer( |
|
| 226 | + | abi::Operation::QueryDomain as u32, *handle as i64, 0, 0, 0, &mut info |
|
| 227 | + | )); |
|
| 228 | + | return info; |
|
| 229 | + | } |
|
| 230 | + | ||
| 231 | + | /// Read event queue capacity. An invalid handle is a program error. |
|
| 232 | + | export fn queryEvents(handle: abi::Handle) -> abi::EventsInfo { |
|
| 233 | + | let mut info = abi::EventsInfo { capacity: 0 }; |
|
| 234 | + | try! result(ecallBuffer( |
|
| 235 | + | abi::Operation::QueryEvents as u32, *handle as i64, 0, 0, 0, &mut info |
|
| 236 | + | )); |
|
| 237 | + | return info; |
|
| 238 | + | } |
|
| 239 | + | ||
| 240 | + | /// Create an additional context with Execute authority over its domain. |
|
| 241 | + | export fn contextCreate(domain: abi::Handle, start: abi::ContextStart) -> u64 throws (abi::Error) { |
|
| 242 | + | let mut copy = start; |
|
| 243 | + | return try result(ecallBuffer( |
|
| 244 | + | abi::Operation::ContextCreate as u32, *domain as i64, 0, 0, 0, &mut copy |
|
| 245 | + | )); |
|
| 246 | + | } |
|
| 247 | + | ||
| 248 | + | /// Reclaim an additional context that no hart is executing. |
|
| 249 | + | export fn contextDestroy(domain: abi::Handle, context: u64) throws (abi::Error) { |
|
| 250 | + | try result(ecall(abi::Operation::ContextDestroy as u32, *domain as i64, context as i64, 0, 0)); |
|
| 251 | + | } |
|
| 252 | + | ||
| 253 | + | /// Read a context. Zero selects the initial context; CURRENT_CONTEXT selects the caller. |
|
| 254 | + | export fn queryContext(domain: abi::Handle, context: u64) -> abi::ContextInfo throws (abi::Error) { |
|
| 255 | + | let mut info = abi::ContextInfo { |
|
| 256 | + | context: 0, domain: 0, stackBase: 0, stackTop: 0, state: 0, hart: 0, budget: 0 |
|
| 257 | + | }; |
|
| 258 | + | try result(ecallBuffer( |
|
| 259 | + | abi::Operation::QueryContext as u32, *domain as i64, context as i64, 0, 0, &mut info |
|
| 260 | + | )); |
|
| 261 | + | return info; |
|
| 262 | + | } |
|
| 263 | + | ||
| 264 | + | /// Read the current extent and binding of a held CPU window. |
|
| 265 | + | export fn queryBudget(handle: abi::Handle) -> abi::BudgetInfo throws (abi::Error) { |
|
| 266 | + | let mut info = abi::BudgetInfo { hart: 0, start: 0, end: 0, context: 0 }; |
|
| 267 | + | try result(ecallBuffer(abi::Operation::QueryBudget as u32, *handle as i64, 0, 0, 0, &mut info)); |
|
| 268 | + | return info; |
|
| 269 | + | } |
|
| 270 | + | ||
| 271 | + | /// Read the bytes of a Page with read authority, or of the caller's own Events handle. |
|
| 272 | + | export fn pageBytes(handle: abi::Handle) -> *[u8] throws (abi::Error) { |
|
| 273 | + | let mut bytes: *[u8] = &[]; |
|
| 274 | + | try result(pageSliceRead(*handle, false, &mut bytes)); |
|
| 275 | + | return bytes; |
|
| 276 | + | } |
|
| 277 | + | ||
| 278 | + | /// Reach the bytes of a Page with read and write authority, or of the caller's own Events |
|
| 279 | + | /// handle, as a writable slice. |
|
| 280 | + | export fn pageBytesMut(handle: abi::Handle) -> *mut [u8] throws (abi::Error) { |
|
| 281 | + | let mut bytes: *mut [u8] = &mut []; |
|
| 282 | + | try result(pageSliceWrite(*handle, true, &mut bytes)); |
|
| 283 | + | return bytes; |
|
| 284 | + | } |
|
| 285 | + | ||
| 286 | + | /// Read the boot record in the startup arguments of the calling domain. |
|
| 287 | + | export fn bootInfo(handle: abi::Handle) -> *[abi::BootInfo] throws (abi::Error) { |
|
| 288 | + | let mut records: *[abi::BootInfo] = &[]; |
|
| 289 | + | try result(pageSliceBoot(*handle, false, &mut records)); |
|
| 290 | + | return records; |
|
| 291 | + | } |
|
| 292 | + | ||
| 293 | + | /// Reach the boot record in the startup arguments of the calling domain for writing. |
|
| 294 | + | export fn bootInfoMut(handle: abi::Handle) -> *mut [abi::BootInfo] throws (abi::Error) { |
|
| 295 | + | let mut records: *mut [abi::BootInfo] = &mut []; |
|
| 296 | + | try result(pageSliceBootMut(*handle, true, &mut records)); |
|
| 297 | + | return records; |
|
| 298 | + | } |
|
| 299 | + | ||
| 300 | + | /// Read the words of a Page with read authority. |
|
| 301 | + | export fn pageWords(handle: abi::Handle) -> *[u64] throws (abi::Error) { |
|
| 302 | + | let mut words: *[u64] = &[]; |
|
| 303 | + | try result(pageSliceWords(*handle, false, &mut words)); |
|
| 304 | + | return words; |
|
| 305 | + | } |
|
| 306 | + | ||
| 307 | + | /// Reach the words of a Page with read and write authority as a writable slice. |
|
| 308 | + | export fn pageWordsMut(handle: abi::Handle) -> *mut [u64] throws (abi::Error) { |
|
| 309 | + | let mut words: *mut [u64] = &mut []; |
|
| 310 | + | try result(pageSliceWordsMut(*handle, true, &mut words)); |
|
| 311 | + | return words; |
|
| 312 | + | } |
kernel/kernel/tests.rad
added
+29 -0
| 1 | + | //! Kernel unit tests. Machine execution tests have separate entry points. |
|
| 2 | + | ||
| 3 | + | export mod range; |
|
| 4 | + | export mod abi; |
|
| 5 | + | export mod slots; |
|
| 6 | + | export mod fdt; |
|
| 7 | + | export mod platform; |
|
| 8 | + | export mod trap; |
|
| 9 | + | export mod capability; |
|
| 10 | + | export mod frames; |
|
| 11 | + | export mod physical; |
|
| 12 | + | export mod registers; |
|
| 13 | + | export mod backing; |
|
| 14 | + | export mod pages; |
|
| 15 | + | export mod transactions; |
|
| 16 | + | export mod events; |
|
| 17 | + | export mod interrupts; |
|
| 18 | + | export mod devices; |
|
| 19 | + | export mod registry; |
|
| 20 | + | export mod loader; |
|
| 21 | + | export mod instances; |
|
| 22 | + | export mod domains; |
|
| 23 | + | export mod lifecycle; |
|
| 24 | + | export mod budgets; |
|
| 25 | + | export mod dispatch; |
|
| 26 | + | export mod calls; |
|
| 27 | + | export mod timers; |
|
| 28 | + | export mod remote; |
|
| 29 | + | export mod boot; |
kernel/kernel/tests/abi.rad
added
+91 -0
| 1 | + | //! Handle bit boundaries and stable ABI values. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use std::lang::il; |
|
| 5 | + | use kernel::abi; |
|
| 6 | + | ||
| 7 | + | /// Check every kind at the index and generation boundaries. |
|
| 8 | + | @test fn handles() throws (testing::TestError) { |
|
| 9 | + | for kind in &[ |
|
| 10 | + | abi::Kind::Page, |
|
| 11 | + | abi::Kind::Interrupt, |
|
| 12 | + | abi::Kind::Device, |
|
| 13 | + | abi::Kind::Events, |
|
| 14 | + | abi::Kind::Domain, |
|
| 15 | + | abi::Kind::Budget, |
|
| 16 | + | abi::Kind::Image, |
|
| 17 | + | ] { |
|
| 18 | + | for index in &[0 as u32, 0xffffff as u32] { |
|
| 19 | + | for generation in &[0 as u32, 1 as u32, 0xffffffff as u32] { |
|
| 20 | + | let object = abi::Ref { index, generation }; |
|
| 21 | + | let value = try! abi::handle(kind, object); |
|
| 22 | + | let decoded = try! abi::decode(value); |
|
| 23 | + | try testing::expect(decoded.kind == kind and decoded.object == object); |
|
| 24 | + | try testing::expect((*value & 0x0f000000) == 0); |
|
| 25 | + | try testing::expect(abi::reference(abi::id(object)) == object); |
|
| 26 | + | } |
|
| 27 | + | } |
|
| 28 | + | } |
|
| 29 | + | let maximum = try! abi::handle(abi::Kind::Image, abi::Ref { |
|
| 30 | + | index: 0xffffff, generation: 0xffffffff |
|
| 31 | + | }); |
|
| 32 | + | try testing::expect(*maximum == 0x7ffffffff0ffffff); |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Reject self, empty/unsupported kinds, reserved bits, and oversized indices. |
|
| 36 | + | @test fn invalidHandles() throws (testing::TestError) { |
|
| 37 | + | let mut failed: u32 = 0; |
|
| 38 | + | for bits in &[ |
|
| 39 | + | 0 as u64, |
|
| 40 | + | 1 as u64, |
|
| 41 | + | 0x8000000000000000, |
|
| 42 | + | 0xf000000000000000, |
|
| 43 | + | 0x1000000001000000, |
|
| 44 | + | 0x1000000002000000, |
|
| 45 | + | 0x1000000004000000, |
|
| 46 | + | 0x1000000008000000, |
|
| 47 | + | ] { |
|
| 48 | + | try abi::decode(abi::Handle(bits)) catch err { |
|
| 49 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 50 | + | set failed += 1; |
|
| 51 | + | }; |
|
| 52 | + | } |
|
| 53 | + | try testing::expect(failed == 8); |
|
| 54 | + | try abi::handle(abi::Kind::Page, abi::Ref { index: 0x1000000, generation: 1 }) catch err { |
|
| 55 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 56 | + | set failed += 1; |
|
| 57 | + | }; |
|
| 58 | + | try testing::expect(failed == 9); |
|
| 59 | + | } |
|
| 60 | + | ||
| 61 | + | /// Check rights validation and the specified error/operation numbers. |
|
| 62 | + | @test fn numbersAndRights() throws (testing::TestError) { |
|
| 63 | + | let all = try! abi::rights(511); |
|
| 64 | + | let read = try! abi::rights(abi::READ as u64); |
|
| 65 | + | try testing::expect(abi::permits(all, read) and not abi::permits(read, all)); |
|
| 66 | + | let mut failed = false; |
|
| 67 | + | try abi::rights(512) catch err { |
|
| 68 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 69 | + | set failed = true; |
|
| 70 | + | }; |
|
| 71 | + | try testing::expect(failed); |
|
| 72 | + | try testing::expect(abi::Operation::CapabilityGrant as u32 == 10); |
|
| 73 | + | try testing::expect(abi::Operation::DomainReparent as u32 == 23); |
|
| 74 | + | try testing::expect(abi::Operation::PageAllocate as u32 == 30); |
|
| 75 | + | try testing::expect(abi::Operation::DomainYield as u32 == 26); |
|
| 76 | + | try testing::expect(abi::Operation::EventWakeup as u32 == 42); |
|
| 77 | + | assert abi::Operation::PageAccess as u32 == 32; |
|
| 78 | + | assert abi::Operation::DeviceAccess as u32 == 90; |
|
| 79 | + | assert abi::Operation::QueryPage as u32 == 80; |
|
| 80 | + | assert abi::Operation::QueryBudget as u32 == 87; |
|
| 81 | + | try testing::expect(abi::Operation::BudgetBind as u32 == 72); |
|
| 82 | + | try testing::expect(abi::Operation::DeviceAccess as u32 == il::DEVICE_ACCESS); |
|
| 83 | + | try testing::expect(abi::Operation::PageAccess as u32 == il::PAGE_ACCESS); |
|
| 84 | + | try testing::expect(abi::Error::BadHandle as u32 == 1 and abi::Error::Exhausted as u32 == 8); |
|
| 85 | + | } |
|
| 86 | + | ||
| 87 | + | /// The Env record has three words. The image builder verifies user packages against this size |
|
| 88 | + | /// (`ENV_SIZE` in kernel/tools/build.rad), and the loader derives it from the type. |
|
| 89 | + | @test fn envSize() throws (testing::TestError) { |
|
| 90 | + | try testing::expect(@sizeOf(abi::Env) == 24); |
|
| 91 | + | } |
kernel/kernel/tests/backing.rad
added
+179 -0
| 1 | + | //! Backing allocations outlive page objects until every exposure ends. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::frames; |
|
| 6 | + | use kernel::backing; |
|
| 7 | + | use kernel::slots; |
|
| 8 | + | ||
| 9 | + | /// Fixed backing-store workspace. |
|
| 10 | + | unsafe static STORE: backing::Store = undefined; |
|
| 11 | + | ||
| 12 | + | /// Initialize two domains and four synthetic physical frames. |
|
| 13 | + | unsafe fn initialize() { |
|
| 14 | + | backing::initialize(&mut STORE); |
|
| 15 | + | try! backing::registerDomain(&mut STORE, abi::Ref { index: 0, generation: 1 }); |
|
| 16 | + | try! backing::registerDomain(&mut STORE, abi::Ref { index: 1, generation: 1 }); |
|
| 17 | + | set STORE.pool.count = 4; |
|
| 18 | + | for i in 0..4 { |
|
| 19 | + | set STORE.pool.addresses[i] = 0x80000000 + i as u64 * 4096; |
|
| 20 | + | set STORE.pool.free[i / 64] |= 1 << (i % 64) as u64; |
|
| 21 | + | } |
|
| 22 | + | } |
|
| 23 | + | ||
| 24 | + | /// Publish one backing record with its initial page and origin exposure. |
|
| 25 | + | unsafe fn allocate() -> abi::Ref { |
|
| 26 | + | let slot = try! slots::reserve(&mut STORE.slots[..]); |
|
| 27 | + | let reservation = try! frames::reserve(&mut STORE.pool, 4); |
|
| 28 | + | let run = frames::commit(reservation); |
|
| 29 | + | return backing::publish(&mut STORE, slot, run, abi::Ref { index: 0, generation: 1 }); |
|
| 30 | + | } |
|
| 31 | + | ||
| 32 | + | /// Publication starts with only the origin domain exposed. |
|
| 33 | + | @test unsafe fn freshExposureBitmap() throws (testing::TestError) { |
|
| 34 | + | set STORE.records[0].exposures[0] = 0xffffffff; |
|
| 35 | + | initialize(); |
|
| 36 | + | let object = allocate(); |
|
| 37 | + | let receiver = abi::Ref { index: 1, generation: 1 }; |
|
| 38 | + | try! backing::expose(&mut STORE, object, receiver); |
|
| 39 | + | assert STORE.records[object.index].exposed == 2; |
|
| 40 | + | } |
|
| 41 | + | ||
| 42 | + | /// Exposure teardown advances in bounded steps and retains shared allocations. |
|
| 43 | + | @test unsafe fn boundedEnd() throws (testing::TestError) { |
|
| 44 | + | initialize(); |
|
| 45 | + | let owner = abi::Ref { index: 0, generation: 1 }; |
|
| 46 | + | let receiver = abi::Ref { index: 1, generation: 1 }; |
|
| 47 | + | let mut objects: [abi::Ref; 3] = undefined; |
|
| 48 | + | for i in 0..3 { |
|
| 49 | + | let slot = try! slots::reserve(&mut STORE.slots[..]); |
|
| 50 | + | let run = frames::commit(try! frames::reserve(&mut STORE.pool, 1)); |
|
| 51 | + | set objects[i] = backing::publish(&mut STORE, slot, run, owner); |
|
| 52 | + | } |
|
| 53 | + | assert STORE.used == 3; |
|
| 54 | + | try! backing::expose(&mut STORE, objects[1], receiver); |
|
| 55 | + | try! backing::releasePage(&mut STORE, objects[0]); |
|
| 56 | + | try! backing::releasePage(&mut STORE, objects[1]); |
|
| 57 | + | let mut invalid = false; |
|
| 58 | + | try backing::endDomainStep(&mut STORE, owner, 0) catch error { |
|
| 59 | + | assert error == abi::Error::InvalidArg; |
|
| 60 | + | set invalid = true; |
|
| 61 | + | }; |
|
| 62 | + | assert invalid and backing::domainLive(&STORE, owner); |
|
| 63 | + | assert not try! backing::endDomainStep(&mut STORE, owner, 1); |
|
| 64 | + | assert not backing::domainLive(&STORE, owner); |
|
| 65 | + | assert STORE.slots[objects[0].index].state == slots::State::Free; |
|
| 66 | + | assert STORE.pool.retiredCount == 1 and not frames::available(&STORE.pool, 0); |
|
| 67 | + | assert STORE.records[objects[1].index].exposed == 2; |
|
| 68 | + | let mut busy = false; |
|
| 69 | + | try backing::registerDomain(&mut STORE, abi::Ref { index: 0, generation: 2 }) catch error { |
|
| 70 | + | assert error == abi::Error::Busy; |
|
| 71 | + | set busy = true; |
|
| 72 | + | }; |
|
| 73 | + | assert busy; |
|
| 74 | + | let mut denied = false; |
|
| 75 | + | try backing::expose(&mut STORE, objects[2], owner) catch error { |
|
| 76 | + | assert error == abi::Error::BadHandle; |
|
| 77 | + | set denied = true; |
|
| 78 | + | }; |
|
| 79 | + | assert denied; |
|
| 80 | + | assert not try! backing::endDomainStep(&mut STORE, owner, 1); |
|
| 81 | + | assert STORE.records[objects[1].index].exposed == 1; |
|
| 82 | + | assert STORE.records[objects[2].index].exposed == 1; |
|
| 83 | + | while not try! backing::endDomainStep(&mut STORE, owner, 64) {} |
|
| 84 | + | assert STORE.records[objects[2].index].exposed == 0 |
|
| 85 | + | and STORE.records[objects[2].index].pages == 1; |
|
| 86 | + | assert STORE.pool.retiredCount == 1; |
|
| 87 | + | assert frames::reclaim(&mut STORE.pool, 1) == 1 and frames::available(&STORE.pool, 0); |
|
| 88 | + | assert not frames::available(&STORE.pool, 1) and not frames::available(&STORE.pool, 2); |
|
| 89 | + | let next = abi::Ref { index: 0, generation: 2 }; |
|
| 90 | + | try! backing::registerDomain(&mut STORE, next); |
|
| 91 | + | assert backing::domainLive(&STORE, next); |
|
| 92 | + | let mut stale = false; |
|
| 93 | + | try backing::endDomainStep(&mut STORE, owner, 64) catch error { |
|
| 94 | + | assert error == abi::Error::BadHandle; |
|
| 95 | + | set stale = true; |
|
| 96 | + | }; |
|
| 97 | + | assert stale and backing::domainLive(&STORE, next); |
|
| 98 | + | try! backing::releasePage(&mut STORE, objects[2]); |
|
| 99 | + | assert frames::available(&STORE.pool, 2); |
|
| 100 | + | while not try! backing::endDomainStep(&mut STORE, receiver, 64) {} |
|
| 101 | + | assert STORE.pool.retiredCount == 1 and not frames::available(&STORE.pool, 1); |
|
| 102 | + | assert frames::reclaim(&mut STORE.pool, 1) == 1 and frames::available(&STORE.pool, 1); |
|
| 103 | + | } |
|
| 104 | + | ||
| 105 | + | /// Dropping all page handles preserves capacity while an exposed domain lives. |
|
| 106 | + | @test unsafe fn exposures() throws (testing::TestError) { |
|
| 107 | + | initialize(); |
|
| 108 | + | let object = allocate(); |
|
| 109 | + | let receiver = abi::Ref { index: 1, generation: 1 }; |
|
| 110 | + | try! backing::expose(&mut STORE, object, receiver); |
|
| 111 | + | try! backing::expose(&mut STORE, object, receiver); |
|
| 112 | + | try testing::expect(STORE.records[object.index].exposed == 2); |
|
| 113 | + | try! backing::releasePage(&mut STORE, object); |
|
| 114 | + | try! backing::endDomain(&mut STORE, abi::Ref { index: 0, generation: 1 }); |
|
| 115 | + | assert not frames::available(&STORE.pool, 0) and slots::matches( |
|
| 116 | + | &STORE.slots[..], object, slots::State::Live |
|
| 117 | + | ); |
|
| 118 | + | try! backing::endDomain(&mut STORE, receiver); |
|
| 119 | + | for i in 0..4 { |
|
| 120 | + | assert frames::available(&STORE.pool, i); |
|
| 121 | + | } |
|
| 122 | + | try testing::expect(not slots::matches(&STORE.slots[..], object, slots::State::Live)); |
|
| 123 | + | } |
|
| 124 | + | ||
| 125 | + | /// Splits keep their common backing alive until every page object is gone. |
|
| 126 | + | @test unsafe fn splitLifetime() throws (testing::TestError) { |
|
| 127 | + | initialize(); |
|
| 128 | + | let object = allocate(); |
|
| 129 | + | try! backing::retainPage(&mut STORE, object); |
|
| 130 | + | try! backing::endDomain(&mut STORE, abi::Ref { index: 0, generation: 1 }); |
|
| 131 | + | try! backing::releasePage(&mut STORE, object); |
|
| 132 | + | assert not frames::available(&STORE.pool, 0) and STORE.records[object.index].pages == 1; |
|
| 133 | + | try! backing::releasePage(&mut STORE, object); |
|
| 134 | + | assert frames::available(&STORE.pool, 0); |
|
| 135 | + | let mut stale = false; |
|
| 136 | + | try backing::releasePage(&mut STORE, object) catch err { |
|
| 137 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 138 | + | set stale = true; |
|
| 139 | + | }; |
|
| 140 | + | try testing::expect(stale); |
|
| 141 | + | } |
|
| 142 | + | ||
| 143 | + | /// An ended domain generation cannot gain exposures through a reused domain slot. |
|
| 144 | + | @test unsafe fn domainGeneration() throws (testing::TestError) { |
|
| 145 | + | initialize(); |
|
| 146 | + | let object = allocate(); |
|
| 147 | + | let old = abi::Ref { index: 1, generation: 1 }; |
|
| 148 | + | try! backing::expose(&mut STORE, object, old); |
|
| 149 | + | try! backing::endDomain(&mut STORE, old); |
|
| 150 | + | try! backing::registerDomain(&mut STORE, abi::Ref { index: 1, generation: 2 }); |
|
| 151 | + | let mut stale = false; |
|
| 152 | + | try backing::expose(&mut STORE, object, old) catch err { |
|
| 153 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 154 | + | set stale = true; |
|
| 155 | + | }; |
|
| 156 | + | try testing::expect(stale and STORE.records[object.index].exposed == 1); |
|
| 157 | + | try! backing::releasePage(&mut STORE, object); |
|
| 158 | + | try! backing::endDomain(&mut STORE, abi::Ref { index: 0, generation: 1 }); |
|
| 159 | + | assert frames::available(&STORE.pool, 0); |
|
| 160 | + | } |
|
| 161 | + | ||
| 162 | + | /// Retained backing metadata cannot create new page authority after the last page ends. |
|
| 163 | + | @test unsafe fn noResurrection() throws (testing::TestError) { |
|
| 164 | + | initialize(); |
|
| 165 | + | let object = allocate(); |
|
| 166 | + | try! backing::releasePage(&mut STORE, object); |
|
| 167 | + | let mut failures: u32 = 0; |
|
| 168 | + | try backing::retainPage(&mut STORE, object) catch err { |
|
| 169 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 170 | + | set failures += 1; |
|
| 171 | + | }; |
|
| 172 | + | try backing::expose(&mut STORE, object, abi::Ref { index: 1, generation: 1 }) catch err { |
|
| 173 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 174 | + | set failures += 1; |
|
| 175 | + | }; |
|
| 176 | + | try testing::expect(failures == 2 and STORE.records[object.index].exposed == 1); |
|
| 177 | + | try! backing::endDomain(&mut STORE, abi::Ref { index: 0, generation: 1 }); |
|
| 178 | + | assert frames::available(&STORE.pool, 0); |
|
| 179 | + | } |
kernel/kernel/tests/boot.rad
added
+26 -0
| 1 | + | //! Firmware input checks and online hart counting at boot. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::boot; |
|
| 5 | + | use kernel::platform; |
|
| 6 | + | ||
| 7 | + | /// The tree size comes from the big-endian total-size word of the header. |
|
| 8 | + | @test fn treeSize() throws (testing::TestError) { |
|
| 9 | + | let mut header: [u8; 40] = [0; 40]; |
|
| 10 | + | set header[4] = 0x00; |
|
| 11 | + | set header[5] = 0x00; |
|
| 12 | + | set header[6] = 0x12; |
|
| 13 | + | set header[7] = 0x34; |
|
| 14 | + | try testing::expect(boot::treeSize(&header[..], 0x80000000) == 0x1234); |
|
| 15 | + | } |
|
| 16 | + | ||
| 17 | + | /// Every set bit of the platform hart mask counts as one online hart. |
|
| 18 | + | @test unsafe fn online() throws (testing::TestError) { |
|
| 19 | + | let mut machine: platform::Platform = undefined; |
|
| 20 | + | set machine.harts = 0; |
|
| 21 | + | try testing::expect(boot::online(&machine) == 0); |
|
| 22 | + | set machine.harts = 0x81; |
|
| 23 | + | try testing::expect(boot::online(&machine) == 2); |
|
| 24 | + | set machine.harts = 0xff; |
|
| 25 | + | try testing::expect(boot::online(&machine) == 8); |
|
| 26 | + | } |
kernel/kernel/tests/budgets.rad
added
+404 -0
| 1 | + | //! Conservation and expiration of exclusive CPU windows. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::capability; |
|
| 6 | + | use kernel::budgets; |
|
| 7 | + | use kernel::slots; |
|
| 8 | + | use kernel::domains; |
|
| 9 | + | use kernel::frames; |
|
| 10 | + | use kernel::instances; |
|
| 11 | + | use kernel::physical; |
|
| 12 | + | use kernel::trap; |
|
| 13 | + | use kernel::range; |
|
| 14 | + | ||
| 15 | + | /// Exclusive CPU-window metadata. |
|
| 16 | + | unsafe static STORE: budgets::Store = undefined; |
|
| 17 | + | /// Capability owner for the test windows. |
|
| 18 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 19 | + | /// Receiving domain's capability table. |
|
| 20 | + | unsafe static TARGET: capability::Table = undefined; |
|
| 21 | + | /// Domain and context lifetimes used for binding admission. |
|
| 22 | + | unsafe static DOMAINS: domains::Store = undefined; |
|
| 23 | + | /// Capability storage for a pending receiver domain. |
|
| 24 | + | unsafe static MEMORY: domains::Memory = undefined; |
|
| 25 | + | /// Pool for the empty instance table; a zero-length token never reads it. |
|
| 26 | + | unsafe static POOL: frames::Pool = undefined; |
|
| 27 | + | ||
| 28 | + | /// Select a context in the receiving domain and move the capability to its table, as the call |
|
| 29 | + | /// boundary does. |
|
| 30 | + | unsafe fn rebind(request: budgets::Binding, now: u64) -> abi::Handle throws (abi::Error) { |
|
| 31 | + | let checked = try budgets::check(&STORE, &DOMAINS, &TABLE, request, now); |
|
| 32 | + | assert budgets::receiver(&checked) == MEMORY.table.owner; |
|
| 33 | + | let entry = try! capability::get(&TABLE, request.budget); |
|
| 34 | + | let result = try budgets::transfer( |
|
| 35 | + | &mut STORE, &mut TABLE, &mut MEMORY.table, request.budget, *entry.rights as u64, now |
|
| 36 | + | ); |
|
| 37 | + | budgets::commit(&mut STORE, checked); |
|
| 38 | + | return result; |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | /// Hand the running budget to a target's initial context, reporting a rejected context as Busy. |
|
| 42 | + | unsafe fn handoff(current: abi::CurrentContextInfo, target: abi::Handle, now: u64) |
|
| 43 | + | throws (abi::Error) |
|
| 44 | + | { |
|
| 45 | + | let request = try budgets::handoffRequest(&STORE, &DOMAINS, &TABLE, current, target, now); |
|
| 46 | + | let moved = try rebind(request, now) catch error { |
|
| 47 | + | if error == abi::Error::InvalidArg { |
|
| 48 | + | throw abi::Error::Busy; |
|
| 49 | + | } |
|
| 50 | + | throw error; |
|
| 51 | + | }; |
|
| 52 | + | } |
|
| 53 | + | ||
| 54 | + | /// Targeted yield moves exactly the active remainder and preserves failures. |
|
| 55 | + | @test unsafe fn targetedYield() throws (testing::TestError) { |
|
| 56 | + | initialize(); |
|
| 57 | + | let target = receiver(); |
|
| 58 | + | let permission = try! capability::get(&TABLE, target); |
|
| 59 | + | let initial = DOMAINS.records[permission.object.index].initial; |
|
| 60 | + | let budget = try! budgets::seed(&mut STORE, &mut TABLE, 0, 10, 100); |
|
| 61 | + | let entry = try! capability::get(&TABLE, budget); |
|
| 62 | + | let source = abi::Ref { index: 7, generation: 1 }; |
|
| 63 | + | set STORE.windows[entry.object.index].context = source; |
|
| 64 | + | let current = abi::CurrentContextInfo { context: abi::id(source), hart: 0, budget: *budget }; |
|
| 65 | + | let before = STORE.windows[entry.object.index]; |
|
| 66 | + | let mut rejected: u32 = 0; |
|
| 67 | + | set DOMAINS.records[permission.object.index].state = domains::Lifecycle::Dead; |
|
| 68 | + | for phase in 0..3 { |
|
| 69 | + | if phase == 1 { |
|
| 70 | + | set DOMAINS.teardown[permission.object.index] = domains::Teardown::Retired; |
|
| 71 | + | } |
|
| 72 | + | if phase == 2 { |
|
| 73 | + | set DOMAINS.slots[permission.object.index].state = slots::State::Free; |
|
| 74 | + | } |
|
| 75 | + | try handoff(current, target, 30) catch error { |
|
| 76 | + | assert error == abi::Error::BadHandle; |
|
| 77 | + | set rejected += 1; |
|
| 78 | + | }; |
|
| 79 | + | assert STORE.windows[entry.object.index] == before; |
|
| 80 | + | assert try! capability::get(&TABLE, budget) == entry; |
|
| 81 | + | } |
|
| 82 | + | set DOMAINS.slots[permission.object.index].state = slots::State::Live; |
|
| 83 | + | set DOMAINS.teardown[permission.object.index] = domains::Teardown::Live; |
|
| 84 | + | set DOMAINS.records[permission.object.index].state = domains::Lifecycle::Pending; |
|
| 85 | + | try handoff(current, target, 30) catch error { |
|
| 86 | + | assert error == abi::Error::Busy; |
|
| 87 | + | set rejected += 1; |
|
| 88 | + | }; |
|
| 89 | + | set DOMAINS.records[permission.object.index].state = domains::Lifecycle::Active; |
|
| 90 | + | set DOMAINS.contexts[initial.index].state = domains::ContextState::Waiting; |
|
| 91 | + | try handoff(current, target, 30) catch error { |
|
| 92 | + | assert error == abi::Error::Busy; |
|
| 93 | + | set rejected += 1; |
|
| 94 | + | }; |
|
| 95 | + | set DOMAINS.contexts[initial.index].state = domains::ContextState::Ready; |
|
| 96 | + | set DOMAINS.contexts[initial.index].hart = 1; |
|
| 97 | + | try handoff(current, target, 30) catch error { |
|
| 98 | + | assert error == abi::Error::Busy; |
|
| 99 | + | set rejected += 1; |
|
| 100 | + | }; |
|
| 101 | + | set DOMAINS.contexts[initial.index].hart = nil; |
|
| 102 | + | try handoff(current, target, 100) catch error { |
|
| 103 | + | assert error == abi::Error::Busy; |
|
| 104 | + | set rejected += 1; |
|
| 105 | + | }; |
|
| 106 | + | let budgetSlot = try! abi::decode(budget); |
|
| 107 | + | set TABLE.entries[budgetSlot.object.index].rights = abi::Rights(abi::READ); |
|
| 108 | + | try handoff(current, target, 30) catch error { |
|
| 109 | + | assert error == abi::Error::Denied; |
|
| 110 | + | set rejected += 1; |
|
| 111 | + | }; |
|
| 112 | + | set TABLE.entries[budgetSlot.object.index].rights = entry.rights; |
|
| 113 | + | let targetSlot = try! abi::decode(target); |
|
| 114 | + | set TABLE.entries[targetSlot.object.index].rights = abi::Rights(abi::READ); |
|
| 115 | + | try handoff(current, target, 30) catch error { |
|
| 116 | + | assert error == abi::Error::Denied; |
|
| 117 | + | set rejected += 1; |
|
| 118 | + | }; |
|
| 119 | + | set TABLE.entries[targetSlot.object.index].rights = permission.rights; |
|
| 120 | + | for i in 0..MEMORY.table.slots.len { |
|
| 121 | + | set MEMORY.table.slots[i].state = slots::State::Retired; |
|
| 122 | + | } |
|
| 123 | + | try handoff(current, target, 30) catch error { |
|
| 124 | + | assert error == abi::Error::Busy; |
|
| 125 | + | set rejected += 1; |
|
| 126 | + | }; |
|
| 127 | + | assert rejected == 10 and STORE.windows[entry.object.index] == before; |
|
| 128 | + | assert try! capability::get(&TABLE, budget) == entry; |
|
| 129 | + | capability::initialize(&mut MEMORY.table, permission.object); |
|
| 130 | + | try! handoff(current, target, 30); |
|
| 131 | + | let after = STORE.windows[entry.object.index]; |
|
| 132 | + | assert after.start == 30 and after.end == 100 and after.hart == 0; |
|
| 133 | + | assert after.owner == permission.object and after.context == initial; |
|
| 134 | + | let moved = try! capability::get(&MEMORY.table, after.handle); |
|
| 135 | + | assert moved.object == entry.object; |
|
| 136 | + | let old = try! abi::decode(budget); |
|
| 137 | + | assert TABLE.slots[old.object.index].state == slots::State::Free; |
|
| 138 | + | } |
|
| 139 | + | ||
| 140 | + | /// Start with one live owner and no issued hart intervals. |
|
| 141 | + | unsafe fn initialize() { |
|
| 142 | + | budgets::initialize(&mut STORE); |
|
| 143 | + | capability::initialize(&mut TABLE, abi::Ref { index: 0, generation: 1 }); |
|
| 144 | + | capability::initialize(&mut TARGET, abi::Ref { index: 1, generation: 1 }); |
|
| 145 | + | } |
|
| 146 | + | ||
| 147 | + | /// Split and merge conserve the unconsumed half-open interval. |
|
| 148 | + | @test unsafe fn conservation() throws (testing::TestError) { |
|
| 149 | + | initialize(); |
|
| 150 | + | let left = try! budgets::seed(&mut STORE, &mut TABLE, 0, 10, 100); |
|
| 151 | + | let right = try! budgets::split(&mut STORE, &mut TABLE, left, 60, 20); |
|
| 152 | + | let a = try! budgets::query(&STORE, &TABLE, left); |
|
| 153 | + | let b = try! budgets::query(&STORE, &TABLE, right); |
|
| 154 | + | try testing::expect(a.start == 20 and a.end == 60 and b.start == 60 and b.end == 100); |
|
| 155 | + | let joined = try! budgets::merge(&mut STORE, &mut TABLE, right, left, 30); |
|
| 156 | + | let result = try! budgets::query(&STORE, &TABLE, joined); |
|
| 157 | + | try testing::expect(joined == right and result.start == 30 and result.end == 100); |
|
| 158 | + | let mut stale = false; |
|
| 159 | + | try budgets::query(&STORE, &TABLE, left) catch err { |
|
| 160 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 161 | + | set stale = true; |
|
| 162 | + | }; |
|
| 163 | + | try testing::expect(stale); |
|
| 164 | + | } |
|
| 165 | + | ||
| 166 | + | /// Expiration cannot recreate consumed time or issue a second initial interval. |
|
| 167 | + | @test unsafe fn expiration() throws (testing::TestError) { |
|
| 168 | + | initialize(); |
|
| 169 | + | let handle = try! budgets::seed(&mut STORE, &mut TABLE, 0, 10, 100); |
|
| 170 | + | let mut failures: u32 = 0; |
|
| 171 | + | try budgets::split(&mut STORE, &mut TABLE, handle, 50, 50) catch err { |
|
| 172 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 173 | + | set failures += 1; |
|
| 174 | + | }; |
|
| 175 | + | try budgets::split(&mut STORE, &mut TABLE, handle, 90, 100) catch err { |
|
| 176 | + | try testing::expect(err == abi::Error::Busy); |
|
| 177 | + | set failures += 1; |
|
| 178 | + | }; |
|
| 179 | + | try testing::expect(not try! budgets::expire(&mut STORE, &mut TABLE, handle, 99)); |
|
| 180 | + | try testing::expect(try! budgets::expire(&mut STORE, &mut TABLE, handle, 100)); |
|
| 181 | + | try budgets::query(&STORE, &TABLE, handle) catch err { |
|
| 182 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 183 | + | set failures += 1; |
|
| 184 | + | }; |
|
| 185 | + | try budgets::seed(&mut STORE, &mut TABLE, 0, 100, 200) catch err { |
|
| 186 | + | try testing::expect(err == abi::Error::Busy); |
|
| 187 | + | set failures += 1; |
|
| 188 | + | }; |
|
| 189 | + | try testing::expect(failures == 4); |
|
| 190 | + | } |
|
| 191 | + | ||
| 192 | + | /// An interval ending at the last tick splits, merges, and stays live like any other. |
|
| 193 | + | @test unsafe fn unbounded() throws (testing::TestError) { |
|
| 194 | + | initialize(); |
|
| 195 | + | let last: u64 = 0xffffffffffffffff; |
|
| 196 | + | let handle = try! budgets::seed(&mut STORE, &mut TABLE, 0, 10, last); |
|
| 197 | + | let mut rejected = false; |
|
| 198 | + | try budgets::split(&mut STORE, &mut TABLE, handle, last, 20) catch err { |
|
| 199 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 200 | + | set rejected = true; |
|
| 201 | + | }; |
|
| 202 | + | try testing::expect(rejected); |
|
| 203 | + | let right = try! budgets::split(&mut STORE, &mut TABLE, handle, last - 1, 20); |
|
| 204 | + | let tail = try! budgets::query(&STORE, &TABLE, right); |
|
| 205 | + | try testing::expect(tail.start == last - 1 and tail.end == last); |
|
| 206 | + | let joined = try! budgets::merge(&mut STORE, &mut TABLE, handle, right, 30); |
|
| 207 | + | let result = try! budgets::query(&STORE, &TABLE, joined); |
|
| 208 | + | try testing::expect(result.start == 30 and result.end == last); |
|
| 209 | + | try testing::expect(not try! budgets::expire(&mut STORE, &mut TABLE, joined, last - 1)); |
|
| 210 | + | } |
|
| 211 | + | ||
| 212 | + | /// A full receiver preserves source authority; successful transfer leaves one owner. |
|
| 213 | + | @test unsafe fn transfer() throws (testing::TestError) { |
|
| 214 | + | initialize(); |
|
| 215 | + | let handle = try! budgets::seed(&mut STORE, &mut TABLE, 0, 10, 100); |
|
| 216 | + | for i in 0..TARGET.slots.len { |
|
| 217 | + | set TARGET.slots[i].state = slots::State::Retired; |
|
| 218 | + | } |
|
| 219 | + | let mut failed = false; |
|
| 220 | + | try budgets::transfer( |
|
| 221 | + | &mut STORE, &mut TABLE, &mut TARGET, handle, abi::READ as u64, 20 |
|
| 222 | + | ) catch err { |
|
| 223 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 224 | + | set failed = true; |
|
| 225 | + | }; |
|
| 226 | + | let unchanged = try! budgets::query(&STORE, &TABLE, handle); |
|
| 227 | + | try testing::expect(failed and unchanged.start == 10 and unchanged.end == 100); |
|
| 228 | + | set TARGET.slots[0].state = slots::State::Free; |
|
| 229 | + | let received = try! budgets::transfer( |
|
| 230 | + | &mut STORE, &mut TABLE, &mut TARGET, handle, abi::READ as u64, 20 |
|
| 231 | + | ); |
|
| 232 | + | let window = try! budgets::query(&STORE, &TARGET, received); |
|
| 233 | + | try testing::expect(window.owner == TARGET.owner and window.start == 20 and window.end == 100); |
|
| 234 | + | let entry = try! capability::get(&TARGET, received); |
|
| 235 | + | try testing::expect(*entry.rights == abi::READ and window.context == nil); |
|
| 236 | + | let mut stale = false; |
|
| 237 | + | try budgets::query(&STORE, &TABLE, handle) catch err { |
|
| 238 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 239 | + | set stale = true; |
|
| 240 | + | }; |
|
| 241 | + | try testing::expect(stale); |
|
| 242 | + | } |
|
| 243 | + | ||
| 244 | + | /// Split exhaustion leaves the original interval intact and merge requires adjacency. |
|
| 245 | + | @test unsafe fn rejection() throws (testing::TestError) { |
|
| 246 | + | initialize(); |
|
| 247 | + | let first = try! budgets::seed(&mut STORE, &mut TABLE, 0, 10, 100); |
|
| 248 | + | for i in 1..STORE.slots.len { |
|
| 249 | + | set STORE.slots[i].state = slots::State::Retired; |
|
| 250 | + | } |
|
| 251 | + | let mut failures: u32 = 0; |
|
| 252 | + | try budgets::split(&mut STORE, &mut TABLE, first, 50, 20) catch err { |
|
| 253 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 254 | + | set failures += 1; |
|
| 255 | + | }; |
|
| 256 | + | let unchanged = try! budgets::query(&STORE, &TABLE, first); |
|
| 257 | + | try testing::expect( |
|
| 258 | + | unchanged.start == 10 |
|
| 259 | + | and unchanged.end == 100 |
|
| 260 | + | and TABLE.slots[1].state == slots::State::Free |
|
| 261 | + | ); |
|
| 262 | + | for i in 1..STORE.slots.len { |
|
| 263 | + | set STORE.slots[i].state = slots::State::Free; |
|
| 264 | + | } |
|
| 265 | + | let middle = try! budgets::split(&mut STORE, &mut TABLE, first, 40, 20); |
|
| 266 | + | let last = try! budgets::split(&mut STORE, &mut TABLE, middle, 70, 20); |
|
| 267 | + | try budgets::merge(&mut STORE, &mut TABLE, first, last, 20) catch err { |
|
| 268 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 269 | + | set failures += 1; |
|
| 270 | + | }; |
|
| 271 | + | let otherHart = try! budgets::seed(&mut STORE, &mut TABLE, 1, 40, 70); |
|
| 272 | + | try budgets::merge(&mut STORE, &mut TABLE, first, otherHart, 20) catch err { |
|
| 273 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 274 | + | set failures += 1; |
|
| 275 | + | }; |
|
| 276 | + | try testing::expect(failures == 3); |
|
| 277 | + | } |
|
| 278 | + | ||
| 279 | + | /// Install a pending receiver and one live context for budget admission tests. |
|
| 280 | + | unsafe fn receiver() -> abi::Handle { |
|
| 281 | + | domains::initialize(&mut DOMAINS); |
|
| 282 | + | let rootSlot = try! slots::reserve(&mut DOMAINS.slots[..]); |
|
| 283 | + | let root = try! slots::commit(&mut DOMAINS.slots[..], rootSlot); |
|
| 284 | + | let childSlot = try! slots::reserve(&mut DOMAINS.slots[..]); |
|
| 285 | + | let child = slots::reference(&childSlot); |
|
| 286 | + | capability::initialize(&mut MEMORY.table, child); |
|
| 287 | + | let contextSlot = try! slots::reserve(&mut DOMAINS.contextSlots[..]); |
|
| 288 | + | let context = slots::reference(&contextSlot); |
|
| 289 | + | set DOMAINS.contexts[context.index] = domains::Context { |
|
| 290 | + | owner: child, |
|
| 291 | + | state: domains::ContextState::Ready, |
|
| 292 | + | hart: nil, |
|
| 293 | + | pending: nil, |
|
| 294 | + | reservation: false, |
|
| 295 | + | kernelFrames: frames::Run { first: 0, count: 0 }, |
|
| 296 | + | kernelStack: range::Range { |
|
| 297 | + | start: 0, end: 0 |
|
| 298 | + | }, |
|
| 299 | + | userStack: range::Range { start: 0, end: 0 }, |
|
| 300 | + | frame: trap::Frame { registers: [0; 32], pc: 4, status: 0x80, cause: 0, value: 0 }, |
|
| 301 | + | }; |
|
| 302 | + | set DOMAINS.records[child.index] = domains::Domain { |
|
| 303 | + | state: domains::Lifecycle::Pending, |
|
| 304 | + | creator: root, |
|
| 305 | + | parent: root, |
|
| 306 | + | image: abi::Ref { index: 0, generation: 1 }, |
|
| 307 | + | initial: context, |
|
| 308 | + | allocation: frames::Run { first: 0, count: 0 }, |
|
| 309 | + | memory: &mut MEMORY, |
|
| 310 | + | graph: frames::Run { first: 0, count: 0 }, |
|
| 311 | + | events: abi::Handle(0), |
|
| 312 | + | }; |
|
| 313 | + | try! slots::commit(&mut DOMAINS.contextSlots[..], contextSlot); |
|
| 314 | + | try! slots::commit(&mut DOMAINS.slots[..], childSlot); |
|
| 315 | + | return try! capability::install(&mut TABLE, capability::Entry { |
|
| 316 | + | kind: abi::Kind::Domain, object: child, rights: abi::Rights(abi::WAKE) |
|
| 317 | + | }); |
|
| 318 | + | } |
|
| 319 | + | ||
| 320 | + | /// One context cannot hold overlapping windows on separate harts. |
|
| 321 | + | @test unsafe fn binding() throws (testing::TestError) { |
|
| 322 | + | initialize(); |
|
| 323 | + | let domain = receiver(); |
|
| 324 | + | let context = abi::Ref { index: 0, generation: 1 }; |
|
| 325 | + | let first = try! budgets::seed(&mut STORE, &mut TABLE, 0, 10, 100); |
|
| 326 | + | let other = try! budgets::seed(&mut STORE, &mut TABLE, 1, 10, 200); |
|
| 327 | + | let future = try! budgets::split(&mut STORE, &mut TABLE, other, 100, 10); |
|
| 328 | + | let received = try! rebind( |
|
| 329 | + | budgets::Binding { budget: first, domain, context }, 10 |
|
| 330 | + | ); |
|
| 331 | + | let entry = try! capability::get(&MEMORY.table, received); |
|
| 332 | + | try testing::expect(not try! budgets::active(&STORE, entry.object, 0, 9)); |
|
| 333 | + | try testing::expect(try! budgets::active(&STORE, entry.object, 0, 10)); |
|
| 334 | + | try testing::expect(not try! budgets::active(&STORE, entry.object, 1, 10)); |
|
| 335 | + | try testing::expect(not try! budgets::active(&STORE, entry.object, 0, 100)); |
|
| 336 | + | let mut overlap = false; |
|
| 337 | + | try rebind( |
|
| 338 | + | budgets::Binding { budget: other, domain, context }, 10 |
|
| 339 | + | ) catch err { |
|
| 340 | + | try testing::expect(err == abi::Error::Busy); |
|
| 341 | + | set overlap = true; |
|
| 342 | + | }; |
|
| 343 | + | let unchanged = try! budgets::query(&STORE, &TABLE, other); |
|
| 344 | + | try testing::expect(overlap and unchanged.context == nil and unchanged.start == 10); |
|
| 345 | + | let next = try! rebind( |
|
| 346 | + | budgets::Binding { budget: future, domain, context }, 10 |
|
| 347 | + | ); |
|
| 348 | + | let following = try! capability::get(&MEMORY.table, next); |
|
| 349 | + | try testing::expect(try! budgets::active(&STORE, following.object, 1, 100)); |
|
| 350 | + | let owner = MEMORY.table.owner; |
|
| 351 | + | let self = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 352 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::WAKE) |
|
| 353 | + | }); |
|
| 354 | + | let same = try! budgets::bind( |
|
| 355 | + | &mut STORE, |
|
| 356 | + | &DOMAINS, |
|
| 357 | + | &MEMORY.table, |
|
| 358 | + | budgets::Binding { budget: received, domain: self, context }, |
|
| 359 | + | 20 |
|
| 360 | + | ); |
|
| 361 | + | let rebound = try! budgets::query(&STORE, &MEMORY.table, same); |
|
| 362 | + | try testing::expect(same == received and rebound.start == 20 and rebound.end == 100); |
|
| 363 | + | } |
|
| 364 | + | ||
| 365 | + | /// Binding validates context generations, receiver capacity, and expiration atomically. |
|
| 366 | + | @test unsafe fn bindingRejection() throws (testing::TestError) { |
|
| 367 | + | initialize(); |
|
| 368 | + | let domain = receiver(); |
|
| 369 | + | let handle = try! budgets::seed(&mut STORE, &mut TABLE, 0, 10, 100); |
|
| 370 | + | let mut request = budgets::Binding { |
|
| 371 | + | budget: handle, |
|
| 372 | + | domain, |
|
| 373 | + | context: abi::Ref { |
|
| 374 | + | index: 0, generation: 2 |
|
| 375 | + | }, |
|
| 376 | + | }; |
|
| 377 | + | let mut failures: u32 = 0; |
|
| 378 | + | try rebind(request, 20) catch err { |
|
| 379 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 380 | + | set failures += 1; |
|
| 381 | + | }; |
|
| 382 | + | set request.context.generation = 1; |
|
| 383 | + | for i in 0..MEMORY.table.slots.len { |
|
| 384 | + | set MEMORY.table.slots[i].state = slots::State::Retired; |
|
| 385 | + | } |
|
| 386 | + | try rebind(request, 20) catch err { |
|
| 387 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 388 | + | set failures += 1; |
|
| 389 | + | }; |
|
| 390 | + | let untouched = try! budgets::query(&STORE, &TABLE, handle); |
|
| 391 | + | try testing::expect(untouched.start == 10 and untouched.context == nil); |
|
| 392 | + | try rebind(request, 100) catch err { |
|
| 393 | + | try testing::expect(err == abi::Error::Busy); |
|
| 394 | + | set failures += 1; |
|
| 395 | + | }; |
|
| 396 | + | set MEMORY.table.slots[0].state = slots::State::Free; |
|
| 397 | + | let domainSlot = try! abi::decode(domain); |
|
| 398 | + | set TABLE.entries[domainSlot.object.index].rights = abi::Rights(0); |
|
| 399 | + | try rebind(request, 20) catch err { |
|
| 400 | + | try testing::expect(err == abi::Error::Denied); |
|
| 401 | + | set failures += 1; |
|
| 402 | + | }; |
|
| 403 | + | try testing::expect(failures == 4); |
|
| 404 | + | } |
kernel/kernel/tests/calls.rad
added
+950 -0
| 1 | + | //! Direct-call scalar results, checked record copies, and architectural replies. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::state; |
|
| 5 | + | use kernel::sync; |
|
| 6 | + | use kernel::abi; |
|
| 7 | + | use kernel::frames; |
|
| 8 | + | use kernel::physical; |
|
| 9 | + | use kernel::access; |
|
| 10 | + | use kernel::calls; |
|
| 11 | + | use kernel::domains; |
|
| 12 | + | use kernel::pages; |
|
| 13 | + | use kernel::backing; |
|
| 14 | + | use kernel::capability; |
|
| 15 | + | use kernel::slots; |
|
| 16 | + | use kernel::registry; |
|
| 17 | + | use kernel::limits; |
|
| 18 | + | use kernel::trap; |
|
| 19 | + | use kernel::budgets; |
|
| 20 | + | use kernel::remote; |
|
| 21 | + | use kernel::interrupts; |
|
| 22 | + | use kernel::events; |
|
| 23 | + | use kernel::devices; |
|
| 24 | + | use kernel::platform; |
|
| 25 | + | use kernel::lifecycle; |
|
| 26 | + | ||
| 27 | + | /// Event storage for administrative destruction targets. |
|
| 28 | + | unsafe static TARGETS: [domains::Memory; 2] = undefined; |
|
| 29 | + | ||
| 30 | + | /// Retained dead-domain records cannot be used or delegated through handles. |
|
| 31 | + | @test unsafe fn deadHandles() throws (testing::TestError) { |
|
| 32 | + | let owner = initialize(); |
|
| 33 | + | let pending = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 34 | + | let object = try! slots::commit(&mut state::SHARED.domains.slots[..], pending); |
|
| 35 | + | try! backing::registerDomain(&mut state::SHARED.pages.backings, object); |
|
| 36 | + | set state::SHARED.domains.records[object.index].state = domains::Lifecycle::Dead; |
|
| 37 | + | set state::SHARED.domains.records[object.index].creator = owner; |
|
| 38 | + | set state::SHARED.domains.records[object.index].parent = nil; |
|
| 39 | + | set state::SHARED.domains.records[object.index].initial = object; |
|
| 40 | + | set state::SHARED.domains.contextSlots[object.index] = |
|
| 41 | + | slots::Slot { generation: object.generation, state: slots::State::Live }; |
|
| 42 | + | set state::SHARED.domains.contexts[object.index].owner = object; |
|
| 43 | + | set state::SHARED.domains.contexts[object.index].state = domains::ContextState::Stopped; |
|
| 44 | + | set state::SHARED.domains.contexts[object.index].hart = nil; |
|
| 45 | + | set state::SHARED.domains.contexts[object.index].userStack = { start: 0, end: 0 }; |
|
| 46 | + | for operation in [84 as u64, 86, 10, 21, 61, 20, 30, 50] { |
|
| 47 | + | let mut kind = abi::Kind::Domain; |
|
| 48 | + | if operation == 84 { |
|
| 49 | + | set kind = abi::Kind::Events; |
|
| 50 | + | } |
|
| 51 | + | let handle = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 52 | + | kind: kind, |
|
| 53 | + | object: object, |
|
| 54 | + | rights: abi::Rights( |
|
| 55 | + | abi::GRANT |
|
| 56 | + | | abi::EXECUTE |
|
| 57 | + | | abi::DESTROY |
|
| 58 | + | | abi::CREATE |
|
| 59 | + | | abi::ALLOCATE |
|
| 60 | + | ), |
|
| 61 | + | }); |
|
| 62 | + | let mut rejected = false; |
|
| 63 | + | let mut argument: u64 = 0; |
|
| 64 | + | if operation == 61 { |
|
| 65 | + | set argument = abi::id(object); |
|
| 66 | + | } |
|
| 67 | + | try calls::invoke(owner, operation, &[*handle, argument, 0, 0], 0) catch error { |
|
| 68 | + | assert error == abi::Error::BadHandle; |
|
| 69 | + | set rejected = true; |
|
| 70 | + | }; |
|
| 71 | + | assert rejected; |
|
| 72 | + | assert (try! capability::get(&MEMORY.table, handle)).object == object; |
|
| 73 | + | } |
|
| 74 | + | } |
|
| 75 | + | ||
| 76 | + | /// Dead and recycled domain references can be dropped without exhausting handles. |
|
| 77 | + | @test unsafe fn dropDeadHandles() throws (testing::TestError) { |
|
| 78 | + | let owner = initialize(); |
|
| 79 | + | for i in 0..limits::HANDLES + 1 { |
|
| 80 | + | let pending = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 81 | + | let object = try! slots::commit(&mut state::SHARED.domains.slots[..], pending); |
|
| 82 | + | set state::SHARED.domains.records[object.index].state = domains::Lifecycle::Dead; |
|
| 83 | + | let handle = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 84 | + | kind: abi::Kind::Domain, object, rights: abi::Rights(0) |
|
| 85 | + | }); |
|
| 86 | + | if i % 2 == 0 { |
|
| 87 | + | try! slots::release(&mut state::SHARED.domains.slots[..], object); |
|
| 88 | + | } |
|
| 89 | + | assert try! calls::invoke(owner, 12, &[*handle, 0, 0, 0], 0) == 0; |
|
| 90 | + | let replacement = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 91 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(0) |
|
| 92 | + | }); |
|
| 93 | + | assert (try! abi::decode(replacement)).object.index == (try! abi::decode( |
|
| 94 | + | handle |
|
| 95 | + | )).object.index; |
|
| 96 | + | let mut rejected = false; |
|
| 97 | + | try calls::invoke(owner, 12, &[*handle, 0, 0, 0], 0) catch error { |
|
| 98 | + | assert error == abi::Error::BadHandle; |
|
| 99 | + | set rejected = true; |
|
| 100 | + | }; |
|
| 101 | + | assert rejected and (try! capability::get(&MEMORY.table, replacement)).object == owner; |
|
| 102 | + | assert try! calls::invoke(owner, 12, &[*replacement, 0, 0, 0], 0) == 0; |
|
| 103 | + | if i % 2 <> 0 { |
|
| 104 | + | try! slots::release(&mut state::SHARED.domains.slots[..], object); |
|
| 105 | + | } |
|
| 106 | + | } |
|
| 107 | + | } |
|
| 108 | + | ||
| 109 | + | /// Administrative destruction checks authority and follows creation ancestry only on request. |
|
| 110 | + | @test unsafe fn administrativeTermination() throws (testing::TestError) { |
|
| 111 | + | for flags in [0 as u64, abi::CASCADE] { |
|
| 112 | + | let owner = initialize(); |
|
| 113 | + | try! events::open(&mut state::SHARED.domains.events, owner, &mut MEMORY.ring); |
|
| 114 | + | let mut objects: [abi::Ref; 2] = undefined; |
|
| 115 | + | for i in 0..2 { |
|
| 116 | + | let pending = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 117 | + | let object = try! slots::commit(&mut state::SHARED.domains.slots[..], pending); |
|
| 118 | + | set objects[i] = object; |
|
| 119 | + | set state::SHARED.domains.records[object.index].state = domains::Lifecycle::Pending; |
|
| 120 | + | set state::SHARED.domains.records[object.index].creator = owner; |
|
| 121 | + | set state::SHARED.domains.records[object.index].parent = owner; |
|
| 122 | + | set state::SHARED.domains.records[object.index].memory = &mut TARGETS[i]; |
|
| 123 | + | try! events::open(&mut state::SHARED.domains.events, object, &mut TARGETS[i].ring); |
|
| 124 | + | set state::SHARED.domains.contextSlots[object.index] = slots::Slot { |
|
| 125 | + | generation: 1, state: slots::State::Live |
|
| 126 | + | }; |
|
| 127 | + | set state::SHARED.domains.contexts[object.index].owner = object; |
|
| 128 | + | set state::SHARED.domains.contexts[object.index].state = domains::ContextState::Ready; |
|
| 129 | + | set state::SHARED.domains.contexts[object.index].hart = 1 + 2 * i; |
|
| 130 | + | } |
|
| 131 | + | set state::SHARED.domains.records[objects[1].index].creator = objects[0]; |
|
| 132 | + | set state::SHARED.domains.records[objects[1].index].parent = objects[0]; |
|
| 133 | + | set state::SHARED.domains.ancestors[objects[1].index][0] = 1 << objects[0].index; |
|
| 134 | + | let handle = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 135 | + | kind: abi::Kind::Domain, object: objects[0], rights: abi::Rights(0) |
|
| 136 | + | }); |
|
| 137 | + | let mut denied = false; |
|
| 138 | + | try calls::termination(owner, 22, &[*handle, flags, 0, 0]) catch error { |
|
| 139 | + | assert error == abi::Error::Denied; |
|
| 140 | + | set denied = true; |
|
| 141 | + | }; |
|
| 142 | + | assert denied and state::SHARED.domains.dead == 0; |
|
| 143 | + | let decoded = try! abi::decode(handle); |
|
| 144 | + | set MEMORY.table.entries[decoded.object.index].rights = abi::Rights(abi::DESTROY); |
|
| 145 | + | let mut invalid = false; |
|
| 146 | + | try calls::termination(owner, 22, &[*handle, 2, 0, 0]) catch error { |
|
| 147 | + | assert error == abi::Error::InvalidArg; |
|
| 148 | + | set invalid = true; |
|
| 149 | + | }; |
|
| 150 | + | assert invalid and state::SHARED.domains.dead == 0; |
|
| 151 | + | let stopped = try! calls::termination(owner, 22, &[*handle, flags, 0, 0]); |
|
| 152 | + | let destroyed = state::SHARED.domains.events.terminals[objects[0].index].event; |
|
| 153 | + | assert destroyed.kind == events::FAULT and destroyed.code == abi::FAULT_DESTROYED; |
|
| 154 | + | if flags == 0 { |
|
| 155 | + | assert stopped == 2 |
|
| 156 | + | and state::SHARED.domains.records[objects[1].index].state |
|
| 157 | + | == domains::Lifecycle::Pending; |
|
| 158 | + | assert state::SHARED.domains.records[objects[1].index].parent == owner; |
|
| 159 | + | } else { |
|
| 160 | + | assert stopped == 10 |
|
| 161 | + | and state::SHARED.domains.records[objects[1].index].state |
|
| 162 | + | == domains::Lifecycle::Dead; |
|
| 163 | + | let descendant = state::SHARED.domains.events.terminals[objects[1].index].event; |
|
| 164 | + | assert descendant.kind == events::FAULT and descendant.code == abi::FAULT_DESTROYED; |
|
| 165 | + | } |
|
| 166 | + | let mut dead = false; |
|
| 167 | + | try calls::termination(owner, 22, &[*handle, 0, 0, 0]) catch error { |
|
| 168 | + | assert error == abi::Error::BadHandle; |
|
| 169 | + | set dead = true; |
|
| 170 | + | }; |
|
| 171 | + | assert dead; |
|
| 172 | + | } |
|
| 173 | + | } |
|
| 174 | + | ||
| 175 | + | /// A retained dead domain reports its Dead state until its identity is released. |
|
| 176 | + | @test unsafe fn deadDomainQuery() throws (testing::TestError) { |
|
| 177 | + | let owner = initialize(); |
|
| 178 | + | let storage = try! allocate(owner, 1); |
|
| 179 | + | let pending = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 180 | + | let object = try! slots::commit(&mut state::SHARED.domains.slots[..], pending); |
|
| 181 | + | set state::SHARED.domains.records[object.index].state = domains::Lifecycle::Dead; |
|
| 182 | + | set state::SHARED.domains.records[object.index].creator = owner; |
|
| 183 | + | set state::SHARED.domains.records[object.index].parent = owner; |
|
| 184 | + | let handle = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 185 | + | kind: abi::Kind::Domain, object, rights: abi::Rights(0) |
|
| 186 | + | }); |
|
| 187 | + | let base = &RAM[OFFSET] as u64; |
|
| 188 | + | for teardown in [domains::Teardown::Live, domains::Teardown::Retired] { |
|
| 189 | + | set state::SHARED.domains.teardown[object.index] = teardown; |
|
| 190 | + | assert try! calls::invoke(owner, 83, &[*handle, base, 32, 0], 0) == 0; |
|
| 191 | + | assert RAM[OFFSET] == abi::id(object) and RAM[OFFSET + 1] == abi::id(owner); |
|
| 192 | + | assert RAM[OFFSET + 2] == abi::id(owner); |
|
| 193 | + | assert RAM[OFFSET + 3] == domains::Lifecycle::Dead as u64; |
|
| 194 | + | } |
|
| 195 | + | try! slots::release(&mut state::SHARED.domains.slots[..], object); |
|
| 196 | + | let mut rejected = false; |
|
| 197 | + | try calls::invoke(owner, 83, &[*handle, base, 32, 0], 0) catch error { |
|
| 198 | + | assert error == abi::Error::BadHandle; |
|
| 199 | + | set rejected = true; |
|
| 200 | + | }; |
|
| 201 | + | assert rejected; |
|
| 202 | + | } |
|
| 203 | + | ||
| 204 | + | /// Destroying the caller through its own Domain handle reports a normal exit. |
|
| 205 | + | @test unsafe fn selfDestroy() throws (testing::TestError) { |
|
| 206 | + | let owner = initialize(); |
|
| 207 | + | try! events::open(&mut state::SHARED.domains.events, owner, &mut MEMORY.ring); |
|
| 208 | + | let handle = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 209 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::DESTROY) |
|
| 210 | + | }); |
|
| 211 | + | let stopped = try! calls::termination(owner, 22, &[*handle, 0, 0, 0]); |
|
| 212 | + | let terminal = state::SHARED.domains.events.terminals[owner.index]; |
|
| 213 | + | assert terminal.event.kind == events::CHILD_EXIT and terminal.event.code == 0; |
|
| 214 | + | } |
|
| 215 | + | ||
| 216 | + | /// Self termination validates arguments and reports the requested terminal kind. |
|
| 217 | + | @test unsafe fn terminationCalls() throws (testing::TestError) { |
|
| 218 | + | for operation in [22 as u64, 25, 24] { |
|
| 219 | + | let owner = initialize(); |
|
| 220 | + | try! events::open(&mut state::SHARED.domains.events, owner, &mut MEMORY.ring); |
|
| 221 | + | let pending = try! slots::reserve(&mut state::SHARED.domains.contextSlots[..]); |
|
| 222 | + | let context = try! slots::commit(&mut state::SHARED.domains.contextSlots[..], pending); |
|
| 223 | + | set state::SHARED.domains.contexts[context.index].owner = owner; |
|
| 224 | + | set state::SHARED.domains.contexts[context.index].state = domains::ContextState::Ready; |
|
| 225 | + | set state::SHARED.domains.contexts[context.index].hart = 2; |
|
| 226 | + | let mut invalid = false; |
|
| 227 | + | try calls::termination(owner, operation, &[0, 0, 1, 0]) catch error { |
|
| 228 | + | assert error == abi::Error::InvalidArg; |
|
| 229 | + | set invalid = true; |
|
| 230 | + | }; |
|
| 231 | + | assert invalid |
|
| 232 | + | and state::SHARED.domains.records[owner.index].state == domains::Lifecycle::Active; |
|
| 233 | + | let mut code: u64 = 0; |
|
| 234 | + | if operation == 24 { |
|
| 235 | + | set code = 0xffffffff; |
|
| 236 | + | } |
|
| 237 | + | assert try! calls::termination(owner, operation, &[code, 0, 0, 0]) == 4; |
|
| 238 | + | let terminal = state::SHARED.domains.events.terminals[owner.index]; |
|
| 239 | + | assert terminal.subject == owner; |
|
| 240 | + | if operation == 25 { |
|
| 241 | + | assert terminal.event.kind == events::FAULT and terminal.event.code == abi::FAULT_ABORT; |
|
| 242 | + | } else { |
|
| 243 | + | assert terminal.event.kind == events::CHILD_EXIT and terminal.event.code == code as u32; |
|
| 244 | + | } |
|
| 245 | + | assert state::SHARED.domains.contexts[context.index].state |
|
| 246 | + | == domains::ContextState::Stopped; |
|
| 247 | + | assert state::SHARED.domains.contexts[context.index].hart == 2; |
|
| 248 | + | } |
|
| 249 | + | let owner = initialize(); |
|
| 250 | + | for arguments in &[[0 as u64, 1, 0, 0], [0, 2, 0, 0]] { |
|
| 251 | + | let mut invalid = false; |
|
| 252 | + | try calls::termination(owner, 22, &arguments[..]) catch error { |
|
| 253 | + | assert error == abi::Error::InvalidArg; |
|
| 254 | + | set invalid = true; |
|
| 255 | + | }; |
|
| 256 | + | assert invalid; |
|
| 257 | + | } |
|
| 258 | + | let mut overflow = false; |
|
| 259 | + | try calls::termination(owner, 24, &[0x100000000, 0, 0, 0]) catch error { |
|
| 260 | + | assert error == abi::Error::InvalidArg; |
|
| 261 | + | set overflow = true; |
|
| 262 | + | }; |
|
| 263 | + | assert overflow; |
|
| 264 | + | } |
|
| 265 | + | ||
| 266 | + | /// Parenting requires both capabilities and preserves creation ancestry. |
|
| 267 | + | @test unsafe fn reparent() throws (testing::TestError) { |
|
| 268 | + | let owner = initialize(); |
|
| 269 | + | let childSlot = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 270 | + | let child = try! slots::commit(&mut state::SHARED.domains.slots[..], childSlot); |
|
| 271 | + | let parentSlot = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 272 | + | let parent = try! slots::commit(&mut state::SHARED.domains.slots[..], parentSlot); |
|
| 273 | + | set state::SHARED.domains.records[child.index].state = domains::Lifecycle::Pending; |
|
| 274 | + | set state::SHARED.domains.records[child.index].creator = owner; |
|
| 275 | + | set state::SHARED.domains.records[child.index].parent = owner; |
|
| 276 | + | set state::SHARED.domains.records[parent.index].state = domains::Lifecycle::Active; |
|
| 277 | + | let childHandle = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 278 | + | kind: abi::Kind::Domain, object: child, rights: abi::Rights(abi::DESTROY) |
|
| 279 | + | }); |
|
| 280 | + | let parentHandle = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 281 | + | kind: abi::Kind::Domain, object: parent, rights: abi::Rights(abi::WAKE) |
|
| 282 | + | }); |
|
| 283 | + | let changed = try! calls::invoke(owner, 23, &[*childHandle, *parentHandle, 0, 0], 0); |
|
| 284 | + | assert changed == 0 and state::SHARED.domains.records[child.index].parent == parent; |
|
| 285 | + | assert state::SHARED.domains.records[child.index].creator == owner; |
|
| 286 | + | let repeated = try! calls::invoke(owner, 23, &[*childHandle, *parentHandle, 0, 0], 0); |
|
| 287 | + | assert repeated == 0; |
|
| 288 | + | let childCap = try! abi::decode(childHandle); |
|
| 289 | + | let parentCap = try! abi::decode(parentHandle); |
|
| 290 | + | let mut denied: u32 = 0; |
|
| 291 | + | for index in &[childCap.object.index, parentCap.object.index] { |
|
| 292 | + | let rights = MEMORY.table.entries[index].rights; |
|
| 293 | + | set MEMORY.table.entries[index].rights = abi::Rights(0); |
|
| 294 | + | try calls::invoke(owner, 23, &[*childHandle, *parentHandle, 0, 0], 0) catch error { |
|
| 295 | + | assert error == abi::Error::Denied; |
|
| 296 | + | set denied += 1; |
|
| 297 | + | }; |
|
| 298 | + | set MEMORY.table.entries[index].rights = rights; |
|
| 299 | + | } |
|
| 300 | + | assert denied == 2 and state::SHARED.domains.records[child.index].parent == parent; |
|
| 301 | + | let mut invalid: u32 = 0; |
|
| 302 | + | for arguments in &[[*childHandle, *parentHandle, 1, 0], [*childHandle, *parentHandle, 0, 1]] { |
|
| 303 | + | try calls::invoke(owner, 23, &arguments[..], 0) catch error { |
|
| 304 | + | assert error == abi::Error::InvalidArg; |
|
| 305 | + | set invalid += 1; |
|
| 306 | + | }; |
|
| 307 | + | } |
|
| 308 | + | assert invalid == 2; |
|
| 309 | + | let mut dead: u32 = 0; |
|
| 310 | + | for object in &[child, parent] { |
|
| 311 | + | set state::SHARED.domains.records[object.index].state = domains::Lifecycle::Dead; |
|
| 312 | + | try calls::invoke(owner, 23, &[*childHandle, *parentHandle, 0, 0], 0) catch error { |
|
| 313 | + | assert error == abi::Error::BadHandle; |
|
| 314 | + | set dead += 1; |
|
| 315 | + | }; |
|
| 316 | + | set state::SHARED.domains.records[object.index].state = domains::Lifecycle::Active; |
|
| 317 | + | } |
|
| 318 | + | assert dead == 2 and state::SHARED.domains.records[child.index].parent == parent; |
|
| 319 | + | try! slots::release(&mut state::SHARED.domains.slots[..], parent); |
|
| 320 | + | try calls::invoke(owner, 23, &[*childHandle, *parentHandle, 0, 0], 0) catch error { |
|
| 321 | + | assert error == abi::Error::BadHandle; |
|
| 322 | + | set dead += 1; |
|
| 323 | + | }; |
|
| 324 | + | assert dead == 3 and state::SHARED.domains.records[child.index].creator == owner; |
|
| 325 | + | } |
|
| 326 | + | ||
| 327 | + | /// Interrupt queries write exactly four bytes at four-byte-aligned addresses. |
|
| 328 | + | @test unsafe fn interruptRecord() throws (testing::TestError) { |
|
| 329 | + | let owner = initialize(); |
|
| 330 | + | interrupts::initialize(&mut state::SHARED.interrupts, 8); |
|
| 331 | + | let handle = try! interrupts::seed(&mut state::SHARED.interrupts, &mut MEMORY.table, 7); |
|
| 332 | + | let page = try! allocate(owner, 1); |
|
| 333 | + | let base = &RAM[OFFSET] as u64; |
|
| 334 | + | set RAM[OFFSET] = 0xaaaaaaaa55555555; |
|
| 335 | + | set RAM[OFFSET + 1] = 0x1122334455667788; |
|
| 336 | + | let result = try! calls::invoke(owner, 82, &[*handle, base + 4, 4, 0], 0); |
|
| 337 | + | assert result == 0 and @sizeOf(abi::InterruptInfo) == 4; |
|
| 338 | + | assert RAM[OFFSET] == 0x0000000755555555 and RAM[OFFSET + 1] == 0x1122334455667788; |
|
| 339 | + | let mut rejected: u32 = 0; |
|
| 340 | + | for args in &[ |
|
| 341 | + | [*handle, base + 2, 4, 0], |
|
| 342 | + | [*handle, base + 4, 8, 0], |
|
| 343 | + | [*handle, base + 4096, 4, 0], |
|
| 344 | + | [*handle, base + 4, 4, 1], |
|
| 345 | + | ] { |
|
| 346 | + | try calls::invoke(owner, 82, &args[..], 0) catch error { |
|
| 347 | + | assert error == abi::Error::InvalidArg; |
|
| 348 | + | set rejected += 1; |
|
| 349 | + | }; |
|
| 350 | + | } |
|
| 351 | + | assert rejected == 4 and RAM[OFFSET] == 0x0000000755555555; |
|
| 352 | + | let boundary = try! calls::invoke(owner, 82, &[*handle, base + 4092, 4, 0], 0); |
|
| 353 | + | assert boundary == 0 and RAM[OFFSET + 511] == 0x0000000700000000; |
|
| 354 | + | let decoded = try! abi::decode(abi::Handle(page)); |
|
| 355 | + | set MEMORY.table.entries[decoded.object.index].rights = abi::Rights(abi::READ); |
|
| 356 | + | try calls::invoke(owner, 82, &[*handle, base + 4, 4, 0], 0) catch error { |
|
| 357 | + | assert error == abi::Error::InvalidArg; |
|
| 358 | + | set rejected += 1; |
|
| 359 | + | }; |
|
| 360 | + | assert rejected == 5; |
|
| 361 | + | } |
|
| 362 | + | ||
| 363 | + | /// A lock proof for single-threaded tests, which cannot mask interrupts. |
|
| 364 | + | fn held() -> sync::Guard { |
|
| 365 | + | return sync::Guard::Held(sync::Section { interrupts: 0, ticket: 0 }); |
|
| 366 | + | } |
|
| 367 | + | ||
| 368 | + | /// Consume a proof made by `held`. |
|
| 369 | + | fn release(guard: sync::Guard) { |
|
| 370 | + | match guard { |
|
| 371 | + | case sync::Guard::Held(section) => {} |
|
| 372 | + | } |
|
| 373 | + | } |
|
| 374 | + | ||
| 375 | + | /// Deferred budget operations recheck authority and expiration at execution time. |
|
| 376 | + | @test unsafe fn deferredBudget() throws (testing::TestError) { |
|
| 377 | + | let owner = initialize(); |
|
| 378 | + | let handle = try! budgets::seed(&mut state::SHARED.budgets, &mut MEMORY.table, 1, 10, 100); |
|
| 379 | + | let entry = try! capability::get(&MEMORY.table, handle); |
|
| 380 | + | let call = remote::Call { |
|
| 381 | + | owner, |
|
| 382 | + | context: owner, |
|
| 383 | + | operation: 70, |
|
| 384 | + | arguments: [ |
|
| 385 | + | *handle, 50, 0, 0 |
|
| 386 | + | ], |
|
| 387 | + | }; |
|
| 388 | + | let guard = held(); |
|
| 389 | + | let wrongHart = calls::remoteBudget(&guard, &mut state::SHARED, call, 0, 20); |
|
| 390 | + | assert wrongHart == remote::Reply::Error(abi::Error::BadHandle); |
|
| 391 | + | remote::initialize(&mut state::SHARED.remote, 3); |
|
| 392 | + | let request = try! remote::send(&mut state::SHARED.remote, 0, 1, remote::Action::Budget(call)); |
|
| 393 | + | let delivery = try! remote::take( |
|
| 394 | + | &mut state::SHARED.remote, 1 |
|
| 395 | + | ) else panic "deferredBudget: missing expired call"; |
|
| 396 | + | assert delivery.id == request; |
|
| 397 | + | let case remote::Action::Budget(queued) = delivery.request.action |
|
| 398 | + | else panic "deferredBudget: queued budget call"; |
|
| 399 | + | let expired = calls::remoteBudget(&guard, &mut state::SHARED, queued, 1, 100); |
|
| 400 | + | assert expired == remote::Reply::Error(abi::Error::Busy); |
|
| 401 | + | remote::respond(&mut state::SHARED.remote, 1, request, expired); |
|
| 402 | + | assert remote::acknowledge(&mut state::SHARED.remote, 1, request); |
|
| 403 | + | assert try! remote::completed(&state::SHARED.remote, 0, request); |
|
| 404 | + | try! remote::release(&mut state::SHARED.remote, 0, request); |
|
| 405 | + | let unchanged = try! budgets::get(&state::SHARED.budgets, entry.object); |
|
| 406 | + | assert unchanged.start == 10 and unchanged.end == 100; |
|
| 407 | + | let reply = calls::remoteBudget(&guard, &mut state::SHARED, call, 1, 20); |
|
| 408 | + | let case remote::Reply::Value(value) = reply else panic "deferredBudget: deferred split"; |
|
| 409 | + | let left = try! budgets::query(&state::SHARED.budgets, &MEMORY.table, handle); |
|
| 410 | + | let right = try! budgets::query(&state::SHARED.budgets, &MEMORY.table, abi::Handle(value)); |
|
| 411 | + | assert left.start == 20 and left.end == 50 and right.start == 50 and right.end == 100; |
|
| 412 | + | let duplicate = calls::remoteBudget(&guard, &mut state::SHARED, call, 1, 25); |
|
| 413 | + | assert duplicate == remote::Reply::Error(abi::Error::InvalidArg); |
|
| 414 | + | let retained = try! budgets::query(&state::SHARED.budgets, &MEMORY.table, handle); |
|
| 415 | + | assert retained.start == 20 and retained.end == 50; |
|
| 416 | + | let drop = remote::Call { owner, context: owner, operation: 12, arguments: [*handle, 0, 0, 0] }; |
|
| 417 | + | assert calls::remoteBudget(&guard, &mut state::SHARED, drop, 1, 25) == remote::Reply::Value(0); |
|
| 418 | + | assert calls::remoteBudget( |
|
| 419 | + | &guard, &mut state::SHARED, call, 1, 25 |
|
| 420 | + | ) == remote::Reply::Error(abi::Error::BadHandle); |
|
| 421 | + | release(guard); |
|
| 422 | + | } |
|
| 423 | + | ||
| 424 | + | /// Mapped physical storage for four pages plus alignment padding. |
|
| 425 | + | static RAM: [u64; 2560] = [0; 2560]; |
|
| 426 | + | /// Kernel-only metadata for the calling domain. |
|
| 427 | + | unsafe static MEMORY: domains::Memory = undefined; |
|
| 428 | + | /// Metadata for a separate capability receiver. |
|
| 429 | + | unsafe static RECEIVER: domains::Memory = undefined; |
|
| 430 | + | /// Mapped storage for the calling domain's empty package-base table, with room to page-align it. |
|
| 431 | + | static BASES: [u64; 1024] = [0; 1024]; |
|
| 432 | + | /// Frame map holding the page of BASES that the table occupies. |
|
| 433 | + | unsafe static TABLE_POOL: frames::Pool = undefined; |
|
| 434 | + | /// Offset to the first aligned physical word. |
|
| 435 | + | static OFFSET: u32 = 0; |
|
| 436 | + | ||
| 437 | + | /// Context slot that runs the calls of the fixture domain. |
|
| 438 | + | static CALLER: abi::Ref = abi::Ref { index: limits::CONTEXTS - 1, generation: 1 }; |
|
| 439 | + | ||
| 440 | + | /// Identity of the fixture's running context, as the hart records it for a call. |
|
| 441 | + | fn running() -> ?abi::CurrentContextInfo { |
|
| 442 | + | return abi::CurrentContextInfo { context: abi::id(CALLER), hart: 0, budget: 0 }; |
|
| 443 | + | } |
|
| 444 | + | ||
| 445 | + | /// Allocate pages through the reservation and publication stages of a call. |
|
| 446 | + | unsafe fn allocate(owner: abi::Ref, count: u64) -> u64 throws (abi::Error) { |
|
| 447 | + | let shared = &mut state::SHARED; |
|
| 448 | + | let domain = try domains::get(&shared.domains, owner); |
|
| 449 | + | let held = try calls::reservePage( |
|
| 450 | + | shared, &domain, &mut MEMORY.table, &[0, count, 0, 0], running() |
|
| 451 | + | ); |
|
| 452 | + | match held { |
|
| 453 | + | case lifecycle::PageHeld::Held(lease, access) => { |
|
| 454 | + | physical::clear(&access); |
|
| 455 | + | return *try calls::publishPage(shared, &mut MEMORY.table, lease, access); |
|
| 456 | + | } |
|
| 457 | + | } |
|
| 458 | + | } |
|
| 459 | + | ||
| 460 | + | /// Create a context through the validation, reservation, and publication stages of a call. |
|
| 461 | + | unsafe fn createContext(owner: abi::Ref, arguments: &[u64]) -> abi::Ref throws (abi::Error) { |
|
| 462 | + | let shared = &mut state::SHARED; |
|
| 463 | + | let domain = try domains::get(&shared.domains, owner); |
|
| 464 | + | let held = try calls::reserveContext(shared, &domain, &mut MEMORY, arguments, running()); |
|
| 465 | + | match held { |
|
| 466 | + | case lifecycle::ContextHeld::Held(lease, access) => { |
|
| 467 | + | physical::clear(&access); |
|
| 468 | + | return try calls::publishContext(shared, owner, lease, access); |
|
| 469 | + | } |
|
| 470 | + | } |
|
| 471 | + | } |
|
| 472 | + | ||
| 473 | + | /// Initialize one active caller with physical allocation authority and a running context. |
|
| 474 | + | unsafe fn initialize() -> abi::Ref { |
|
| 475 | + | domains::initialize(&mut state::SHARED.domains); |
|
| 476 | + | lifecycle::initialize(&mut state::SHARED.lifecycle); |
|
| 477 | + | registry::initialize(&mut state::SHARED.registry); |
|
| 478 | + | budgets::initialize(&mut state::SHARED.budgets); |
|
| 479 | + | slots::initialize(&mut state::SHARED.pages.slots[..]); |
|
| 480 | + | backing::initialize(&mut state::SHARED.pages.backings); |
|
| 481 | + | let base = &RAM[0] as u64; |
|
| 482 | + | let aligned = (base + 4095) & ~4095; |
|
| 483 | + | set OFFSET = ((aligned - base) / 8) as u32; |
|
| 484 | + | let tableBase = (&BASES[0] as u64 + 4095) & ~4095; |
|
| 485 | + | set TABLE_POOL.count = 1; |
|
| 486 | + | for i in 0..TABLE_POOL.free.len { |
|
| 487 | + | set TABLE_POOL.free[i] = 0; |
|
| 488 | + | } |
|
| 489 | + | set TABLE_POOL.addresses[0] = tableBase; |
|
| 490 | + | set state::SHARED.pages.backings.pool.count = 4; |
|
| 491 | + | for i in 0..4 { |
|
| 492 | + | set state::SHARED.pages.backings.pool.addresses[i] = aligned + i as u64 * 4096; |
|
| 493 | + | set state::SHARED.pages.backings.pool.free[i / 64] |= 1 << (i % 64) as u64; |
|
| 494 | + | } |
|
| 495 | + | let pending = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 496 | + | let owner = try! slots::commit(&mut state::SHARED.domains.slots[..], pending); |
|
| 497 | + | try! backing::registerDomain(&mut state::SHARED.pages.backings, owner); |
|
| 498 | + | capability::initialize(&mut MEMORY.table, owner); |
|
| 499 | + | let authority = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 500 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::ALLOCATE) |
|
| 501 | + | }); |
|
| 502 | + | set state::SHARED.domains.records[owner.index] = domains::Domain { |
|
| 503 | + | state: domains::Lifecycle::Active, |
|
| 504 | + | creator: owner, |
|
| 505 | + | parent: nil, |
|
| 506 | + | image: owner, |
|
| 507 | + | initial: owner, |
|
| 508 | + | allocation: { first: 0, count: 0 }, |
|
| 509 | + | memory: &mut MEMORY, |
|
| 510 | + | graph: { first: 0, count: 0 }, |
|
| 511 | + | events: abi::Handle(0), |
|
| 512 | + | }; |
|
| 513 | + | set state::SHARED.domains.tables[owner.index] = try! physical::access( |
|
| 514 | + | &TABLE_POOL, tableBase, limits::PACKAGES as u64 * 8 |
|
| 515 | + | ); |
|
| 516 | + | set state::SHARED.domains.contextSlots[CALLER.index] = slots::Slot { |
|
| 517 | + | generation: 1, state: slots::State::Live |
|
| 518 | + | }; |
|
| 519 | + | set state::SHARED.domains.contexts[CALLER.index].owner = owner; |
|
| 520 | + | set state::SHARED.domains.contexts[CALLER.index].state = domains::ContextState::Ready; |
|
| 521 | + | set state::SHARED.domains.contexts[CALLER.index].hart = nil; |
|
| 522 | + | set state::SHARED.domains.contexts[CALLER.index].pending = nil; |
|
| 523 | + | return owner; |
|
| 524 | + | } |
|
| 525 | + | ||
| 526 | + | /// Failed transfers retain the source; successful moves preserve page exposure. |
|
| 527 | + | @test unsafe fn capabilityCalls() throws (testing::TestError) { |
|
| 528 | + | let owner = initialize(); |
|
| 529 | + | let pending = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 530 | + | let receiver = try! slots::commit(&mut state::SHARED.domains.slots[..], pending); |
|
| 531 | + | try! backing::registerDomain(&mut state::SHARED.pages.backings, receiver); |
|
| 532 | + | capability::initialize(&mut RECEIVER.table, receiver); |
|
| 533 | + | set state::SHARED.domains.records[receiver.index] = state::SHARED.domains.records[owner.index]; |
|
| 534 | + | set state::SHARED.domains.records[receiver.index].memory = &mut RECEIVER; |
|
| 535 | + | let target = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 536 | + | kind: abi::Kind::Domain, object: receiver, rights: abi::Rights(0) |
|
| 537 | + | }); |
|
| 538 | + | let handle = try! allocate(owner, 1); |
|
| 539 | + | let entry = try! capability::get(&MEMORY.table, abi::Handle(handle)); |
|
| 540 | + | let narrowed = try! calls::invoke(owner, 10, &[handle, 0, abi::READ as u64, 0], 0); |
|
| 541 | + | let granted = try! capability::get(&MEMORY.table, abi::Handle(narrowed)); |
|
| 542 | + | try testing::expect( |
|
| 543 | + | *granted.rights == abi::READ |
|
| 544 | + | and state::SHARED.pages.records[entry.object.index].handles == 2 |
|
| 545 | + | ); |
|
| 546 | + | let dropped = try! calls::invoke(owner, 12, &[narrowed, 0, 0, 0], 0); |
|
| 547 | + | try testing::expect( |
|
| 548 | + | dropped == 0 |
|
| 549 | + | and state::SHARED.pages.records[entry.object.index].handles == 1 |
|
| 550 | + | ); |
|
| 551 | + | for i in 0..limits::HANDLES { |
|
| 552 | + | set RECEIVER.table.slots[i].state = slots::State::Retired; |
|
| 553 | + | } |
|
| 554 | + | let mut rejected = false; |
|
| 555 | + | try calls::invoke(owner, 11, &[handle, *target, abi::READ as u64, 0], 0) catch error { |
|
| 556 | + | try testing::expect(error == abi::Error::InvalidArg); |
|
| 557 | + | set rejected = true; |
|
| 558 | + | }; |
|
| 559 | + | try testing::expect( |
|
| 560 | + | rejected and try! capability::get(&MEMORY.table, abi::Handle( |
|
| 561 | + | handle |
|
| 562 | + | )) == entry |
|
| 563 | + | ); |
|
| 564 | + | capability::initialize(&mut RECEIVER.table, receiver); |
|
| 565 | + | let moved = try! calls::invoke(owner, 11, &[handle, *target, abi::READ as u64, 0], 0); |
|
| 566 | + | let received = try! capability::get(&RECEIVER.table, abi::Handle(moved)); |
|
| 567 | + | try testing::expect(received.object == entry.object and *received.rights == abi::READ); |
|
| 568 | + | let page = try! pages::get(&state::SHARED.pages, entry.object); |
|
| 569 | + | try testing::expect( |
|
| 570 | + | page.handles == 1 |
|
| 571 | + | and state::SHARED.pages.backings.records[page.backing.index].exposed == 2 |
|
| 572 | + | ); |
|
| 573 | + | let decoded = try! abi::decode(abi::Handle(handle)); |
|
| 574 | + | try testing::expect(MEMORY.table.slots[decoded.object.index].state == slots::State::Free); |
|
| 575 | + | } |
|
| 576 | + | ||
| 577 | + | /// A transfer needs a live receiver other than the caller; a grant can target the caller. |
|
| 578 | + | @test unsafe fn delegationReceivers() throws (testing::TestError) { |
|
| 579 | + | let owner = initialize(); |
|
| 580 | + | let pending = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 581 | + | let receiver = try! slots::commit(&mut state::SHARED.domains.slots[..], pending); |
|
| 582 | + | try! backing::registerDomain(&mut state::SHARED.pages.backings, receiver); |
|
| 583 | + | capability::initialize(&mut RECEIVER.table, receiver); |
|
| 584 | + | set state::SHARED.domains.records[receiver.index] = state::SHARED.domains.records[owner.index]; |
|
| 585 | + | set state::SHARED.domains.records[receiver.index].memory = &mut RECEIVER; |
|
| 586 | + | let target = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 587 | + | kind: abi::Kind::Domain, object: receiver, rights: abi::Rights(0) |
|
| 588 | + | }); |
|
| 589 | + | let own = try! capability::at(&MEMORY.table, 0, abi::Kind::Domain); |
|
| 590 | + | let handle = try! allocate(owner, 1); |
|
| 591 | + | let entry = try! capability::get(&MEMORY.table, abi::Handle(handle)); |
|
| 592 | + | let read = abi::READ as u64; |
|
| 593 | + | let mut rejected: u32 = 0; |
|
| 594 | + | // A transfer to the caller itself, by handle or by default, leaves the source untouched. |
|
| 595 | + | for args in &[[handle, *own, read, 0], [handle, 0, read, 0]] { |
|
| 596 | + | try calls::invoke(owner, 11, &args[..], 0) catch error { |
|
| 597 | + | try testing::expect(error == abi::Error::InvalidArg); |
|
| 598 | + | set rejected += 1; |
|
| 599 | + | }; |
|
| 600 | + | } |
|
| 601 | + | try testing::expect( |
|
| 602 | + | rejected == 2 and try! capability::get(&MEMORY.table, abi::Handle( |
|
| 603 | + | handle |
|
| 604 | + | )) == entry |
|
| 605 | + | ); |
|
| 606 | + | // A grant to the caller creates a second handle in the caller's table. |
|
| 607 | + | let again = abi::Handle(try! calls::invoke(owner, 10, &[handle, *own, read, 0], 0)); |
|
| 608 | + | try testing::expect( |
|
| 609 | + | again <> abi::Handle(handle) |
|
| 610 | + | and state::SHARED.pages.records[entry.object.index].handles == 2 |
|
| 611 | + | ); |
|
| 612 | + | // A grant to another domain lands in the receiver's table and keeps the source. |
|
| 613 | + | let shared = abi::Handle(try! calls::invoke(owner, 10, &[handle, *target, read, 0], 0)); |
|
| 614 | + | let received = try! capability::get(&RECEIVER.table, shared); |
|
| 615 | + | try testing::expect( |
|
| 616 | + | received.object == entry.object |
|
| 617 | + | and state::SHARED.pages.records[entry.object.index].handles == 3 |
|
| 618 | + | ); |
|
| 619 | + | try testing::expect(try! capability::get(&MEMORY.table, abi::Handle(handle)) == entry); |
|
| 620 | + | // A dead receiver accepts neither a grant nor a transfer. |
|
| 621 | + | set state::SHARED.domains.records[receiver.index].state = domains::Lifecycle::Dead; |
|
| 622 | + | for operation in &[10 as u64, 11 as u64] { |
|
| 623 | + | try calls::invoke(owner, operation, &[handle, *target, read, 0], 0) catch error { |
|
| 624 | + | try testing::expect(error == abi::Error::BadHandle); |
|
| 625 | + | set rejected += 1; |
|
| 626 | + | }; |
|
| 627 | + | } |
|
| 628 | + | try testing::expect( |
|
| 629 | + | rejected == 4 |
|
| 630 | + | and state::SHARED.pages.records[entry.object.index].handles == 3 |
|
| 631 | + | ); |
|
| 632 | + | } |
|
| 633 | + | ||
| 634 | + | /// Events remain installed, and budgets cannot duplicate execution authority. |
|
| 635 | + | @test unsafe fn nondelegableCalls() throws (testing::TestError) { |
|
| 636 | + | let owner = initialize(); |
|
| 637 | + | let queue = try! capability::install(&mut MEMORY.table, capability::Entry { |
|
| 638 | + | kind: abi::Kind::Events, object: owner, rights: abi::Rights(abi::READ | abi::WRITE) |
|
| 639 | + | }); |
|
| 640 | + | set state::SHARED.domains.records[owner.index].events = queue; |
|
| 641 | + | let window = try! budgets::seed(&mut state::SHARED.budgets, &mut MEMORY.table, 0, 10, 20); |
|
| 642 | + | let mut rejected: u32 = 0; |
|
| 643 | + | try calls::invoke(owner, 12, &[*queue, 0, 0, 0], 0) catch error { |
|
| 644 | + | try testing::expect(error == abi::Error::Denied); |
|
| 645 | + | set rejected += 1; |
|
| 646 | + | }; |
|
| 647 | + | try calls::invoke(owner, 10, &[*window, 0, abi::READ as u64, 0], 0) catch error { |
|
| 648 | + | try testing::expect(error == abi::Error::Denied); |
|
| 649 | + | set rejected += 1; |
|
| 650 | + | }; |
|
| 651 | + | let narrowed = try! calls::invoke(owner, 10, &[*queue, 0, abi::READ as u64, 0], 0); |
|
| 652 | + | let dropped = try! calls::invoke(owner, 12, &[narrowed, 0, 0, 0], 0); |
|
| 653 | + | let removed = try! calls::invoke(owner, 12, &[*window, 0, 0, 0], 0); |
|
| 654 | + | try testing::expect(rejected == 2 and dropped == 0 and removed == 0); |
|
| 655 | + | try testing::expect(state::SHARED.budgets.slots[0].state == slots::State::Free); |
|
| 656 | + | let installed = try! capability::get(&MEMORY.table, queue); |
|
| 657 | + | try testing::expect(installed.kind == abi::Kind::Events); |
|
| 658 | + | } |
|
| 659 | + | ||
| 660 | + | /// Device queries copy only metadata into checked caller-owned memory. |
|
| 661 | + | @test unsafe fn deviceRecord() throws (testing::TestError) { |
|
| 662 | + | let owner = initialize(); |
|
| 663 | + | let storage = try! allocate(owner, 1); |
|
| 664 | + | set state::SHARED.devices.count = 1; |
|
| 665 | + | set state::SHARED.devices.regions[0] = devices::Region { |
|
| 666 | + | kind: platform::Kind::Uart, |
|
| 667 | + | info: { base: 0x10000000, size: 256 }, |
|
| 668 | + | rights: abi::Rights(abi::READ), |
|
| 669 | + | }; |
|
| 670 | + | let device = try! devices::install(&state::SHARED.devices, &mut MEMORY.table, 0); |
|
| 671 | + | let base = &RAM[OFFSET] as u64; |
|
| 672 | + | set RAM[OFFSET + 2] = 123; |
|
| 673 | + | assert try! calls::invoke(owner, 81, &[*device, base, 16, 0], 0) == 0; |
|
| 674 | + | assert RAM[OFFSET] == 0x10000000 and RAM[OFFSET + 1] == 256 and RAM[OFFSET + 2] == 123; |
|
| 675 | + | let mut rejected: u32 = 0; |
|
| 676 | + | for args in &[[*device, base + 1, 16, 0], [*device, base, 8, 0], [*device, base, 16, 1]] { |
|
| 677 | + | try calls::invoke(owner, 81, &args[..], 0) catch error { |
|
| 678 | + | assert error == abi::Error::InvalidArg; |
|
| 679 | + | set rejected += 1; |
|
| 680 | + | }; |
|
| 681 | + | } |
|
| 682 | + | assert rejected == 3 and RAM[OFFSET + 2] == 123; |
|
| 683 | + | } |
|
| 684 | + | ||
| 685 | + | /// A query copies exactly its record into writable memory owned by the caller. |
|
| 686 | + | @test unsafe fn records() throws (testing::TestError) { |
|
| 687 | + | let owner = initialize(); |
|
| 688 | + | let handle = try! allocate(owner, 1); |
|
| 689 | + | let base = &RAM[OFFSET] as u64; |
|
| 690 | + | set RAM[OFFSET + 2] = 0x12345678; |
|
| 691 | + | let result = try! calls::invoke(owner, 80, &[handle, base, 16, 0], 0); |
|
| 692 | + | try testing::expect(result == 0 and RAM[OFFSET] == base and RAM[OFFSET + 1] == 1); |
|
| 693 | + | try testing::expect(RAM[OFFSET + 2] == 0x12345678); |
|
| 694 | + | let mut rejected: u32 = 0; |
|
| 695 | + | for args in &[ |
|
| 696 | + | [handle, base + 1, 16, 0], |
|
| 697 | + | [handle, base, 8, 0], |
|
| 698 | + | [handle, base + 4096, 16, 0], |
|
| 699 | + | [handle, base, 16, 1], |
|
| 700 | + | [handle, 0xfffffffffffffff8, 16, 0], |
|
| 701 | + | ] { |
|
| 702 | + | try calls::invoke(owner, 80, &args[..], 0) catch error { |
|
| 703 | + | try testing::expect(error == abi::Error::InvalidArg); |
|
| 704 | + | set rejected += 1; |
|
| 705 | + | }; |
|
| 706 | + | } |
|
| 707 | + | try testing::expect(rejected == 5 and RAM[OFFSET] == base and RAM[OFFSET + 1] == 1); |
|
| 708 | + | let decoded = try! abi::decode(abi::Handle(handle)); |
|
| 709 | + | set MEMORY.table.entries[decoded.object.index].rights = abi::Rights(abi::READ); |
|
| 710 | + | try calls::invoke(owner, 80, &[handle, base, 16, 0], 0) catch error { |
|
| 711 | + | try testing::expect(error == abi::Error::InvalidArg); |
|
| 712 | + | set rejected += 1; |
|
| 713 | + | }; |
|
| 714 | + | try testing::expect(rejected == 6 and RAM[OFFSET + 2] == 0x12345678); |
|
| 715 | + | } |
|
| 716 | + | ||
| 717 | + | /// Count the free frames of the fixture pool. |
|
| 718 | + | unsafe fn free() -> u32 { |
|
| 719 | + | let pool = &state::SHARED.pages.backings.pool; |
|
| 720 | + | let mut count: u32 = 0; |
|
| 721 | + | for i in 0..pool.count { |
|
| 722 | + | if frames::available(pool, i) { |
|
| 723 | + | set count += 1; |
|
| 724 | + | } |
|
| 725 | + | } |
|
| 726 | + | return count; |
|
| 727 | + | } |
|
| 728 | + | ||
| 729 | + | /// A page allocation that cannot reserve frames changes nothing; one whose owner dies |
|
| 730 | + | /// before publication returns its frames and handle. |
|
| 731 | + | @test unsafe fn pageReservation() throws (testing::TestError) { |
|
| 732 | + | let owner = initialize(); |
|
| 733 | + | let before = free(); |
|
| 734 | + | let mut failed = false; |
|
| 735 | + | try allocate(owner, 5) catch error { |
|
| 736 | + | assert error == abi::Error::OutOfMemory; |
|
| 737 | + | set failed = true; |
|
| 738 | + | }; |
|
| 739 | + | assert failed |
|
| 740 | + | and free() == before |
|
| 741 | + | and not state::SHARED.domains.contexts[CALLER.index].reservation; |
|
| 742 | + | let shared = &mut state::SHARED; |
|
| 743 | + | let domain = try! domains::get(&shared.domains, owner); |
|
| 744 | + | let held = try! calls::reservePage( |
|
| 745 | + | shared, &domain, &mut MEMORY.table, &[0, 2, 0, 0], running() |
|
| 746 | + | ); |
|
| 747 | + | assert free() == before - 2 and shared.domains.contexts[CALLER.index].reservation; |
|
| 748 | + | set shared.domains.records[owner.index].state = domains::Lifecycle::Dead; |
|
| 749 | + | let mut cancelled = false; |
|
| 750 | + | match held { |
|
| 751 | + | case lifecycle::PageHeld::Held(lease, access) => { |
|
| 752 | + | physical::clear(&access); |
|
| 753 | + | try calls::publishPage(shared, &mut MEMORY.table, lease, access) catch error { |
|
| 754 | + | assert error == abi::Error::BadHandle; |
|
| 755 | + | set cancelled = true; |
|
| 756 | + | }; |
|
| 757 | + | } |
|
| 758 | + | } |
|
| 759 | + | assert cancelled and not shared.domains.contexts[CALLER.index].reservation; |
|
| 760 | + | while shared.pages.backings.pool.retiredCount > 0 { |
|
| 761 | + | assert frames::reclaim(&mut shared.pages.backings.pool, 256) > 0; |
|
| 762 | + | } |
|
| 763 | + | assert free() == before and MEMORY.table.slots[1].state == slots::State::Free; |
|
| 764 | + | } |
|
| 765 | + | ||
| 766 | + | /// Context calls validate descriptor storage and preserve capacity on failure. |
|
| 767 | + | @test unsafe fn contextBuffers() throws (testing::TestError) { |
|
| 768 | + | let owner = initialize(); |
|
| 769 | + | let page = try! allocate(owner, 1); |
|
| 770 | + | let base = &RAM[OFFSET] as u64; |
|
| 771 | + | let mut rejected: u32 = 0; |
|
| 772 | + | for args in &[[0 as u64, 0, 32, 0], [0, base, 24, 0], [0, base, 32, 1], [0, base + 1, 32, 0]] { |
|
| 773 | + | try createContext(owner, &args[..]) catch error { |
|
| 774 | + | try testing::expect(error == abi::Error::InvalidArg); |
|
| 775 | + | set rejected += 1; |
|
| 776 | + | }; |
|
| 777 | + | } |
|
| 778 | + | try createContext(owner, &[0, base, 32, 0]) catch error { |
|
| 779 | + | try testing::expect(error == abi::Error::Denied); |
|
| 780 | + | set rejected += 1; |
|
| 781 | + | }; |
|
| 782 | + | try calls::invoke(owner, 86, &[0, 0, base, 32], 0) catch error { |
|
| 783 | + | try testing::expect(error == abi::Error::BadHandle); |
|
| 784 | + | set rejected += 1; |
|
| 785 | + | }; |
|
| 786 | + | try testing::expect( |
|
| 787 | + | rejected == 6 |
|
| 788 | + | and not state::SHARED.domains.contexts[CALLER.index].reservation |
|
| 789 | + | ); |
|
| 790 | + | } |
|
| 791 | + | ||
| 792 | + | /// Execute through the unlocked boundary, which single-threaded tests serialize. |
|
| 793 | + | fn unlocked(owner: abi::Ref, operation: u64, arguments: &[u64], now: u64) -> access::Reply |
|
| 794 | + | throws (abi::Error) |
|
| 795 | + | { |
|
| 796 | + | // Unit tests run one hart and never hold the metadata lock. |
|
| 797 | + | unsafe { |
|
| 798 | + | return try calls::invokeReply(owner, operation, arguments, now); |
|
| 799 | + | } |
|
| 800 | + | } |
|
| 801 | + | ||
| 802 | + | /// Invalid infallible queries terminate the caller at the syscall boundary. |
|
| 803 | + | @test unsafe fn invalidQueryAborts() throws (testing::TestError) { |
|
| 804 | + | for operation in [80 as u64, 81, 82, 83, 84] { |
|
| 805 | + | let owner = initialize(); |
|
| 806 | + | try! events::open(&mut state::SHARED.domains.events, owner, &mut MEMORY.ring); |
|
| 807 | + | let mut frame = trap::Frame { |
|
| 808 | + | registers: [0; 32], pc: 0x80000000, status: 0x80, cause: 8, value: 0 |
|
| 809 | + | }; |
|
| 810 | + | set frame.registers[17] = operation; |
|
| 811 | + | calls::handle(owner, &mut frame, 0, 0, unlocked); |
|
| 812 | + | assert state::SHARED.domains.records[owner.index].state == domains::Lifecycle::Dead; |
|
| 813 | + | assert state::SHARED.domains.events.terminals[owner.index].event.code == abi::FAULT_ABORT; |
|
| 814 | + | assert state::SHARED.domains.events.terminals[owner.index].event.kind == events::FAULT; |
|
| 815 | + | } |
|
| 816 | + | } |
|
| 817 | + | ||
| 818 | + | /// An unknown user operation advances PC and returns only a negative a0 error. |
|
| 819 | + | @test unsafe fn trapReply() throws (testing::TestError) { |
|
| 820 | + | let owner = initialize(); |
|
| 821 | + | let mut frame = trap::Frame { |
|
| 822 | + | registers: [123; 32], pc: 0x80000000, status: 0x80, cause: 8, value: 0 |
|
| 823 | + | }; |
|
| 824 | + | set frame.registers[17] = 0xffffffffffffffff; |
|
| 825 | + | calls::handle(owner, &mut frame, 0, 0, unlocked); |
|
| 826 | + | try testing::expect(frame.pc == 0x80000004 and frame.registers[10] == 0xfffffffffffffffc); |
|
| 827 | + | for i in 0..32 { |
|
| 828 | + | if i == 10 or i == 17 { |
|
| 829 | + | continue; |
|
| 830 | + | } |
|
| 831 | + | try testing::expect(frame.registers[i] == 123); |
|
| 832 | + | } |
|
| 833 | + | try testing::expect(frame.registers[17] == 0xffffffffffffffff and frame.status == 0x80); |
|
| 834 | + | } |
|
| 835 | + | ||
| 836 | + | /// Page access gives the region of a Page with the rights of its handle, and marks its backing |
|
| 837 | + | /// as accessed for good. The machine fixtures cover the event ring. |
|
| 838 | + | @test unsafe fn pageAccess() throws (testing::TestError) { |
|
| 839 | + | let owner = initialize(); |
|
| 840 | + | let handle = try! allocate(owner, 2); |
|
| 841 | + | let decoded = try! abi::decode(abi::Handle(handle)); |
|
| 842 | + | let page = try! pages::get( |
|
| 843 | + | &state::SHARED.pages, MEMORY.table.entries[decoded.object.index].object |
|
| 844 | + | ); |
|
| 845 | + | try testing::expect(not try! backing::accessed(&state::SHARED.pages.backings, page.backing)); |
|
| 846 | + | for bad in &[[handle, 2 as u64, 0, 0], [handle, 0, 1, 0], [handle, 0, 0, 1]] { |
|
| 847 | + | try calls::invoke(owner, 32, &bad[..], 0) catch error { |
|
| 848 | + | try testing::expect(error == abi::Error::InvalidArg); |
|
| 849 | + | continue; |
|
| 850 | + | }; |
|
| 851 | + | throw testing::TestError::Failed; |
|
| 852 | + | } |
|
| 853 | + | try testing::expect(not try! backing::accessed(&state::SHARED.pages.backings, page.backing)); |
|
| 854 | + | set MEMORY.table.entries[decoded.object.index].rights = abi::Rights(abi::READ); |
|
| 855 | + | try calls::invoke(owner, 32, &[handle, 1, 0, 0], 0) catch error { |
|
| 856 | + | try testing::expect(error == abi::Error::Denied); |
|
| 857 | + | set MEMORY.table.entries[decoded.object.index].rights = |
|
| 858 | + | abi::Rights(abi::READ | abi::WRITE | abi::GRANT | abi::TRANSFER); |
|
| 859 | + | }; |
|
| 860 | + | try testing::expect(not try! backing::accessed(&state::SHARED.pages.backings, page.backing)); |
|
| 861 | + | set MEMORY.table.entries[decoded.object.index].rights = abi::Rights(abi::READ); |
|
| 862 | + | let read = try! calls::invokeReply(owner, 32, &[handle, 0, 0, 0], 0); |
|
| 863 | + | try testing::expect(read.value == page.base and read.extra == 8192); |
|
| 864 | + | try testing::expect(try! backing::accessed(&state::SHARED.pages.backings, page.backing)); |
|
| 865 | + | set MEMORY.table.entries[decoded.object.index].rights = |
|
| 866 | + | abi::Rights(abi::READ | abi::WRITE | abi::GRANT | abi::TRANSFER); |
|
| 867 | + | let write = try! calls::invokeReply(owner, 32, &[handle, 1, 0, 0], 0); |
|
| 868 | + | try testing::expect(write.value == read.value and write.extra == 8192); |
|
| 869 | + | // A split keeps the flag, because it belongs to the allocation. |
|
| 870 | + | let right = try! calls::invoke(owner, 31, &[handle, 1, 0, 0], 0); |
|
| 871 | + | let rightObject = |
|
| 872 | + | MEMORY.table.entries[(try! abi::decode(abi::Handle(right))).object.index].object; |
|
| 873 | + | let rightPage = try! pages::get(&state::SHARED.pages, rightObject); |
|
| 874 | + | try testing::expect(rightPage.backing == page.backing); |
|
| 875 | + | try testing::expect(try! backing::accessed(&state::SHARED.pages.backings, rightPage.backing)); |
|
| 876 | + | let half = try! calls::invokeReply(owner, 32, &[right, 0, 0, 0], 0); |
|
| 877 | + | try testing::expect(half.value == page.base + 4096 and half.extra == 4096); |
|
| 878 | + | // A handle of another kind is not memory. |
|
| 879 | + | try calls::invoke(owner, 32, &[0, 0, 0, 0], 0) catch error { |
|
| 880 | + | try testing::expect(error == abi::Error::BadHandle); |
|
| 881 | + | }; |
|
| 882 | + | } |
|
| 883 | + | ||
| 884 | + | /// Registers a0 to a3 are never addresses: an operation that writes a record takes the |
|
| 885 | + | /// address and size from a4 and a5, and rejects an operand in a register that it does not use. |
|
| 886 | + | @test unsafe fn bufferRegisters() throws (testing::TestError) { |
|
| 887 | + | let owner = initialize(); |
|
| 888 | + | let handle = try! allocate(owner, 1); |
|
| 889 | + | let base = &RAM[OFFSET] as u64; |
|
| 890 | + | set RAM[OFFSET + 2] = 0x12345678; |
|
| 891 | + | let mut frame = trap::Frame { |
|
| 892 | + | registers: [0; 32], pc: 0x80000000, status: 0x80, cause: 8, value: 0 |
|
| 893 | + | }; |
|
| 894 | + | set frame.registers[17] = 80; |
|
| 895 | + | set frame.registers[10] = handle; |
|
| 896 | + | set frame.registers[14] = base; |
|
| 897 | + | set frame.registers[15] = 16; |
|
| 898 | + | calls::handle(owner, &mut frame, 0, 0, unlocked); |
|
| 899 | + | try testing::expect(frame.registers[10] == 0 and RAM[OFFSET] == base and RAM[OFFSET + 1] == 1); |
|
| 900 | + | // An address in a1, a2 or a3 is rejected and nothing is written. |
|
| 901 | + | set RAM[OFFSET] = 0; |
|
| 902 | + | for register in [11, 12, 13] { |
|
| 903 | + | set frame.registers[register] = base; |
|
| 904 | + | set frame.registers[10] = handle; |
|
| 905 | + | set frame.pc = 0x80000000; |
|
| 906 | + | calls::handle(owner, &mut frame, 0, 0, unlocked); |
|
| 907 | + | try testing::expect(frame.registers[10] == 0xfffffffffffffffc and RAM[OFFSET] == 0); |
|
| 908 | + | set frame.registers[register] = 0; |
|
| 909 | + | } |
|
| 910 | + | // Without a buffer the size does not match the record. |
|
| 911 | + | set frame.registers[10] = handle; |
|
| 912 | + | set frame.registers[14] = 0; |
|
| 913 | + | set frame.registers[15] = 0; |
|
| 914 | + | calls::handle(owner, &mut frame, 0, 0, unlocked); |
|
| 915 | + | try testing::expect(frame.registers[10] == 0xfffffffffffffffc); |
|
| 916 | + | // A page access returns its size in a1, and leaves a1 zero on failure. |
|
| 917 | + | set frame.registers[17] = 32; |
|
| 918 | + | set frame.registers[10] = handle; |
|
| 919 | + | set frame.registers[11] = 1; |
|
| 920 | + | set frame.registers[14] = base; |
|
| 921 | + | calls::handle(owner, &mut frame, 0, 0, unlocked); |
|
| 922 | + | try testing::expect(frame.registers[11] == 4096 and frame.registers[10] <> 0); |
|
| 923 | + | set frame.registers[10] = 0; |
|
| 924 | + | set frame.registers[11] = 1; |
|
| 925 | + | calls::handle(owner, &mut frame, 0, 0, unlocked); |
|
| 926 | + | try testing::expect(frame.registers[11] == 0 and frame.registers[10] == 0xffffffffffffffff); |
|
| 927 | + | } |
|
| 928 | + | ||
| 929 | + | /// A region is not limited in address or size: a page record far above the physical |
|
| 930 | + | /// memory of the build, and larger than 256 MiB, is returned whole in two words. |
|
| 931 | + | @test unsafe fn wideRegion() throws (testing::TestError) { |
|
| 932 | + | let owner = initialize(); |
|
| 933 | + | let handle = try! allocate(owner, 1); |
|
| 934 | + | let decoded = try! abi::decode(abi::Handle(handle)); |
|
| 935 | + | let object = MEMORY.table.entries[decoded.object.index].object; |
|
| 936 | + | set state::SHARED.pages.records[object.index].base = 0x7fff00000000000; |
|
| 937 | + | set state::SHARED.pages.records[object.index].count = 0x3000000; |
|
| 938 | + | let region = try! calls::invokeReply(owner, 32, &[handle, 0, 0, 0], 0); |
|
| 939 | + | try testing::expect(region.value == 0x7fff00000000000 and region.extra == 0x3000000 * 4096); |
|
| 940 | + | let mut frame = trap::Frame { |
|
| 941 | + | registers: [0; 32], pc: 0x80000000, status: 0x80, cause: 8, value: 0 |
|
| 942 | + | }; |
|
| 943 | + | set frame.registers[17] = 32; |
|
| 944 | + | set frame.registers[10] = handle; |
|
| 945 | + | calls::handle(owner, &mut frame, 0, 0, unlocked); |
|
| 946 | + | try testing::expect( |
|
| 947 | + | frame.registers[10] == 0x7fff00000000000 |
|
| 948 | + | and frame.registers[11] == 0x3000000 * 4096 |
|
| 949 | + | ); |
|
| 950 | + | } |
kernel/kernel/tests/capability.rad
added
+211 -0
| 1 | + | //! Capability-table authority, generations, and domain-relative names. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::slots; |
|
| 6 | + | use kernel::limits; |
|
| 7 | + | use kernel::capability; |
|
| 8 | + | ||
| 9 | + | /// Construct one live object's capability metadata. |
|
| 10 | + | fn entry(kind: abi::Kind, index: u32, rights: u16) -> capability::Entry { |
|
| 11 | + | return capability::Entry { |
|
| 12 | + | kind, object: abi::Ref { index, generation: 1 }, rights: abi::Rights(rights) |
|
| 13 | + | }; |
|
| 14 | + | } |
|
| 15 | + | ||
| 16 | + | /// Check a rejected lookup's exact error without accepting successful lookup. |
|
| 17 | + | fn rejected( |
|
| 18 | + | table: &capability::Table, value: abi::Handle, kind: abi::Kind, rights: u16, error: abi::Error |
|
| 19 | + | ) |
|
| 20 | + | throws (testing::TestError) |
|
| 21 | + | { |
|
| 22 | + | let mut failed = false; |
|
| 23 | + | try capability::lookup(table, value, kind, abi::Rights(rights)) catch err { |
|
| 24 | + | try testing::expect(err == error); |
|
| 25 | + | set failed = true; |
|
| 26 | + | }; |
|
| 27 | + | try testing::expect(failed); |
|
| 28 | + | } |
|
| 29 | + | ||
| 30 | + | /// Authoritative metadata controls kind, rights, and object identity. |
|
| 31 | + | @test unsafe fn lookup() throws (testing::TestError) { |
|
| 32 | + | let mut table: capability::Table = undefined; |
|
| 33 | + | capability::initialize(&mut table, abi::Ref { index: 0, generation: 1 }); |
|
| 34 | + | let page = entry(abi::Kind::Page, 9, abi::READ | abi::GRANT); |
|
| 35 | + | let value = try! capability::install(&mut table, page); |
|
| 36 | + | let found = try! capability::lookup(&table, value, abi::Kind::Page, abi::Rights(abi::READ)); |
|
| 37 | + | try testing::expect(found == page); |
|
| 38 | + | let decoded = try! abi::decode(value); |
|
| 39 | + | try testing::expect( |
|
| 40 | + | try! capability::at( |
|
| 41 | + | &table, decoded.object.index, abi::Kind::Page |
|
| 42 | + | ) == value |
|
| 43 | + | ); |
|
| 44 | + | try rejected(&table, value, abi::Kind::Device, 0, abi::Error::BadHandle); |
|
| 45 | + | try rejected(&table, value, abi::Kind::Page, abi::WRITE, abi::Error::Denied); |
|
| 46 | + | let forged = try! abi::handle(abi::Kind::Device, decoded.object); |
|
| 47 | + | try rejected(&table, forged, abi::Kind::Device, 0, abi::Error::BadHandle); |
|
| 48 | + | try rejected( |
|
| 49 | + | &table, abi::Handle(*value | 0x01000000), abi::Kind::Page, 0, abi::Error::BadHandle |
|
| 50 | + | ); |
|
| 51 | + | try rejected(&table, abi::Handle(0), abi::Kind::Domain, 0, abi::Error::BadHandle); |
|
| 52 | + | let outside = try! abi::handle(abi::Kind::Page, abi::Ref { |
|
| 53 | + | index: limits::HANDLES, generation: 1 |
|
| 54 | + | }); |
|
| 55 | + | try rejected(&table, outside, abi::Kind::Page, 0, abi::Error::BadHandle); |
|
| 56 | + | let removed = try! capability::invalidate(&mut table, value); |
|
| 57 | + | try testing::expect(removed == page); |
|
| 58 | + | try rejected(&table, value, abi::Kind::Page, 0, abi::Error::BadHandle); |
|
| 59 | + | let next = try! capability::install(&mut table, page); |
|
| 60 | + | try testing::expect(next <> value); |
|
| 61 | + | try rejected(&table, value, abi::Kind::Page, 0, abi::Error::BadHandle); |
|
| 62 | + | } |
|
| 63 | + | ||
| 64 | + | /// A copied number selects only authority already present in the receiver's table. |
|
| 65 | + | @test unsafe fn domainScope() throws (testing::TestError) { |
|
| 66 | + | let mut left: capability::Table = undefined; |
|
| 67 | + | let mut right: capability::Table = undefined; |
|
| 68 | + | capability::initialize(&mut left, abi::Ref { index: 0, generation: 1 }); |
|
| 69 | + | capability::initialize(&mut right, abi::Ref { index: 1, generation: 1 }); |
|
| 70 | + | let value = try! capability::install(&mut left, entry(abi::Kind::Page, 7, abi::READ)); |
|
| 71 | + | try rejected(&right, value, abi::Kind::Page, 0, abi::Error::BadHandle); |
|
| 72 | + | let local = try! capability::install(&mut right, entry(abi::Kind::Page, 8, abi::WRITE)); |
|
| 73 | + | try testing::expect(value == local); |
|
| 74 | + | try rejected(&right, value, abi::Kind::Page, abi::READ, abi::Error::Denied); |
|
| 75 | + | let actual = try! capability::lookup(&right, value, abi::Kind::Page, abi::Rights(abi::WRITE)); |
|
| 76 | + | try testing::expect(actual.object.index == 8); |
|
| 77 | + | } |
|
| 78 | + | ||
| 79 | + | /// Capacity failures preserve installed entries, and exhausted generations retire. |
|
| 80 | + | @test unsafe fn capacity() throws (testing::TestError) { |
|
| 81 | + | let mut table: capability::Table = undefined; |
|
| 82 | + | capability::initialize(&mut table, abi::Ref { index: 0, generation: 1 }); |
|
| 83 | + | let mut values: [abi::Handle; limits::HANDLES] = undefined; |
|
| 84 | + | for i in 0..limits::HANDLES { |
|
| 85 | + | set values[i] = try! capability::install(&mut table, entry(abi::Kind::Page, i, abi::READ)); |
|
| 86 | + | } |
|
| 87 | + | let mut failed = false; |
|
| 88 | + | try capability::install(&mut table, entry(abi::Kind::Page, 999, abi::READ)) catch err { |
|
| 89 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 90 | + | set failed = true; |
|
| 91 | + | }; |
|
| 92 | + | try testing::expect(failed); |
|
| 93 | + | for i in 0..limits::HANDLES { |
|
| 94 | + | let found = try! capability::get(&table, values[i]); |
|
| 95 | + | try testing::expect(found.object.index == i); |
|
| 96 | + | } |
|
| 97 | + | let old = try! capability::invalidate(&mut table, values[0]); |
|
| 98 | + | set table.slots[0].generation = 0xffffffff; |
|
| 99 | + | let last = try! capability::install(&mut table, old); |
|
| 100 | + | try! capability::invalidate(&mut table, last); |
|
| 101 | + | try testing::expect(table.slots[0].state == slots::State::Retired); |
|
| 102 | + | let mut retired = false; |
|
| 103 | + | try capability::install(&mut table, old) catch err { |
|
| 104 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 105 | + | set retired = true; |
|
| 106 | + | }; |
|
| 107 | + | try testing::expect(retired); |
|
| 108 | + | } |
|
| 109 | + | ||
| 110 | + | /// Zero selects a live self capability; unrelated authority and stale slots do not count. |
|
| 111 | + | @test unsafe fn selfAuthority() throws (testing::TestError) { |
|
| 112 | + | let mut table: capability::Table = undefined; |
|
| 113 | + | capability::initialize(&mut table, abi::Ref { index: 4, generation: 1 }); |
|
| 114 | + | let other = try! capability::install(&mut table, entry( |
|
| 115 | + | abi::Kind::Domain, 5, abi::CREATE | abi::ALLOCATE |
|
| 116 | + | )); |
|
| 117 | + | let page = try! capability::install(&mut table, entry(abi::Kind::Page, 4, abi::ALLOCATE)); |
|
| 118 | + | let oldDomain = capability::Entry { |
|
| 119 | + | kind: abi::Kind::Domain, |
|
| 120 | + | object: abi::Ref { |
|
| 121 | + | index: 4, generation: 2 |
|
| 122 | + | }, |
|
| 123 | + | rights: abi::Rights(abi::ALLOCATE), |
|
| 124 | + | }; |
|
| 125 | + | try! capability::install(&mut table, oldDomain); |
|
| 126 | + | let mut failed = false; |
|
| 127 | + | try capability::authority(&table, abi::Handle(0), abi::Rights(abi::ALLOCATE)) catch err { |
|
| 128 | + | try testing::expect(err == abi::Error::Denied); |
|
| 129 | + | set failed = true; |
|
| 130 | + | }; |
|
| 131 | + | try testing::expect(failed); |
|
| 132 | + | let explicit = try! capability::authority(&table, other, abi::Rights(abi::CREATE)); |
|
| 133 | + | try testing::expect(explicit.object.index == 5); |
|
| 134 | + | let own = try! capability::install(&mut table, entry( |
|
| 135 | + | abi::Kind::Domain, 4, abi::CREATE | abi::ALLOCATE |
|
| 136 | + | )); |
|
| 137 | + | let authorized = try! capability::authority(&table, abi::Handle(0), abi::Rights( |
|
| 138 | + | abi::CREATE | abi::ALLOCATE |
|
| 139 | + | )); |
|
| 140 | + | try testing::expect(authorized.object == table.owner); |
|
| 141 | + | try! capability::invalidate(&mut table, own); |
|
| 142 | + | let mut lost = false; |
|
| 143 | + | try capability::authority(&table, abi::Handle(0), abi::Rights(abi::CREATE)) catch err { |
|
| 144 | + | try testing::expect(err == abi::Error::Denied); |
|
| 145 | + | set lost = true; |
|
| 146 | + | }; |
|
| 147 | + | try testing::expect(lost); |
|
| 148 | + | } |
|
| 149 | + | ||
| 150 | + | /// Invalid installation metadata does not consume a slot or advance its generation. |
|
| 151 | + | @test unsafe fn invalidMetadata() throws (testing::TestError) { |
|
| 152 | + | let mut table: capability::Table = undefined; |
|
| 153 | + | capability::initialize(&mut table, abi::Ref { index: 0, generation: 1 }); |
|
| 154 | + | let mut bad = entry(abi::Kind::Empty, 0, 0); |
|
| 155 | + | let mut failures: u32 = 0; |
|
| 156 | + | for test in 0..3 { |
|
| 157 | + | if test == 1 { |
|
| 158 | + | set bad = entry(abi::Kind::Page, 0, 512); |
|
| 159 | + | } |
|
| 160 | + | if test == 2 { |
|
| 161 | + | set bad = entry(abi::Kind::Page, 0, abi::READ); |
|
| 162 | + | set bad.object.generation = 0; |
|
| 163 | + | } |
|
| 164 | + | try capability::install(&mut table, bad) catch err { |
|
| 165 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 166 | + | set failures += 1; |
|
| 167 | + | }; |
|
| 168 | + | } |
|
| 169 | + | try testing::expect(failures == 3); |
|
| 170 | + | let value = try! capability::install(&mut table, entry(abi::Kind::Page, 0, abi::READ)); |
|
| 171 | + | let decoded = try! abi::decode(value); |
|
| 172 | + | try testing::expect(decoded.object.index == 0 and decoded.object.generation == 1); |
|
| 173 | + | let mut invalid = false; |
|
| 174 | + | try capability::at(&table, 0, abi::Kind::Device) catch err { |
|
| 175 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 176 | + | set invalid = true; |
|
| 177 | + | }; |
|
| 178 | + | try testing::expect(invalid); |
|
| 179 | + | set table.slots[0].state = slots::State::Reserved; |
|
| 180 | + | try rejected(&table, value, abi::Kind::Page, 0, abi::Error::BadHandle); |
|
| 181 | + | let mut pending = false; |
|
| 182 | + | try capability::at(&table, 0, abi::Kind::Page) catch err { |
|
| 183 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 184 | + | set pending = true; |
|
| 185 | + | }; |
|
| 186 | + | try testing::expect(pending); |
|
| 187 | + | } |
|
| 188 | + | ||
| 189 | + | /// Each resource-authority decision uses the complete rights of one live entry. |
|
| 190 | + | @test unsafe fn separateSelfRights() throws (testing::TestError) { |
|
| 191 | + | let mut table: capability::Table = undefined; |
|
| 192 | + | capability::initialize(&mut table, abi::Ref { index: 0, generation: 1 }); |
|
| 193 | + | try! capability::install(&mut table, entry(abi::Kind::Domain, 0, abi::CREATE)); |
|
| 194 | + | let allocate = try! capability::install(&mut table, entry(abi::Kind::Domain, 0, abi::ALLOCATE)); |
|
| 195 | + | let selected = try! capability::authority(&table, abi::Handle(0), abi::Rights(abi::CREATE)); |
|
| 196 | + | try testing::expect(*selected.rights == abi::CREATE); |
|
| 197 | + | let mut denied = false; |
|
| 198 | + | try capability::authority(&table, abi::Handle(0), abi::Rights( |
|
| 199 | + | abi::CREATE | abi::ALLOCATE |
|
| 200 | + | )) catch err { |
|
| 201 | + | try testing::expect(err == abi::Error::Denied); |
|
| 202 | + | set denied = true; |
|
| 203 | + | }; |
|
| 204 | + | try testing::expect(denied); |
|
| 205 | + | let mut missing = false; |
|
| 206 | + | try capability::authority(&table, allocate, abi::Rights(abi::CREATE)) catch err { |
|
| 207 | + | try testing::expect(err == abi::Error::Denied); |
|
| 208 | + | set missing = true; |
|
| 209 | + | }; |
|
| 210 | + | try testing::expect(missing); |
|
| 211 | + | } |
kernel/kernel/tests/devices.rad
added
+163 -0
| 1 | + | //! Device identity, exported regions, and capability delegation. |
|
| 2 | + | use std::testing; |
|
| 3 | + | use kernel::state; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::devices; |
|
| 6 | + | use kernel::platform; |
|
| 7 | + | use kernel::capability; |
|
| 8 | + | use kernel::calls; |
|
| 9 | + | use kernel::domains; |
|
| 10 | + | use kernel::slots; |
|
| 11 | + | ||
| 12 | + | /// Device metadata under test. |
|
| 13 | + | unsafe static STORE: devices::Store = undefined; |
|
| 14 | + | /// Synthetic validated platform regions. |
|
| 15 | + | unsafe static MACHINE: platform::Platform = undefined; |
|
| 16 | + | /// Independent caller and receiver capability storage. |
|
| 17 | + | unsafe static MEMORY: [domains::Memory; 2] = undefined; |
|
| 18 | + | ||
| 19 | + | /// Describe driver devices around kernel-owned interrupt and timer registers. |
|
| 20 | + | unsafe fn initialize() { |
|
| 21 | + | set MACHINE.deviceCount = 4; |
|
| 22 | + | set MACHINE.devices[0] = { |
|
| 23 | + | kind: platform::Kind::Uart, |
|
| 24 | + | memory: { |
|
| 25 | + | start: 0x10000000, end: 0x10000100 |
|
| 26 | + | }, |
|
| 27 | + | }; |
|
| 28 | + | set MACHINE.devices[1] = { |
|
| 29 | + | kind: platform::Kind::Plic, |
|
| 30 | + | memory: { |
|
| 31 | + | start: 0x0c000000, end: 0x10000000 |
|
| 32 | + | }, |
|
| 33 | + | }; |
|
| 34 | + | set MACHINE.devices[2] = { |
|
| 35 | + | kind: platform::Kind::Clint, |
|
| 36 | + | memory: { |
|
| 37 | + | start: 0x02000000, end: 0x02010000 |
|
| 38 | + | }, |
|
| 39 | + | }; |
|
| 40 | + | set MACHINE.devices[3] = { |
|
| 41 | + | kind: platform::Kind::Finish, |
|
| 42 | + | memory: { |
|
| 43 | + | start: 0x10001000, end: 0x10002000 |
|
| 44 | + | }, |
|
| 45 | + | }; |
|
| 46 | + | try! devices::initialize(&mut STORE, &MACHINE); |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | /// Driver regions retain tree order and expose only read-only timer state. |
|
| 50 | + | @test unsafe fn regions() throws (testing::TestError) { |
|
| 51 | + | initialize(); |
|
| 52 | + | assert STORE.count == 3; |
|
| 53 | + | assert STORE.regions[0].kind == platform::Kind::Uart; |
|
| 54 | + | let timer = abi::DeviceInfo { base: 0x0200bff8, size: 8 }; |
|
| 55 | + | assert STORE.regions[1].info == timer; |
|
| 56 | + | assert not abi::permits(STORE.regions[1].rights, abi::Rights(abi::WRITE)); |
|
| 57 | + | assert STORE.regions[2].kind == platform::Kind::Finish; |
|
| 58 | + | let mut rejected: u32 = 0; |
|
| 59 | + | for object in [abi::Ref { index: 3, generation: 1 }, abi::Ref { index: 0, generation: 2 }] { |
|
| 60 | + | try devices::get(&STORE, object) catch error { |
|
| 61 | + | assert error == abi::Error::BadHandle; |
|
| 62 | + | set rejected += 1; |
|
| 63 | + | }; |
|
| 64 | + | } |
|
| 65 | + | assert rejected == 2; |
|
| 66 | + | set MACHINE.devices[2].memory.end = 0x0200bfff; |
|
| 67 | + | try devices::initialize(&mut STORE, &MACHINE) catch error { |
|
| 68 | + | assert error == abi::Error::InvalidArg; |
|
| 69 | + | set rejected += 1; |
|
| 70 | + | }; |
|
| 71 | + | assert rejected == 3 and STORE.count == 0; |
|
| 72 | + | } |
|
| 73 | + | ||
| 74 | + | /// Device authority can move to a surviving domain and can only lose rights. |
|
| 75 | + | @test unsafe fn delegation() throws (testing::TestError) { |
|
| 76 | + | initialize(); |
|
| 77 | + | set state::SHARED.devices = STORE; |
|
| 78 | + | domains::initialize(&mut state::SHARED.domains); |
|
| 79 | + | let mut owners: [abi::Ref; 2] = undefined; |
|
| 80 | + | for i in 0..2 { |
|
| 81 | + | let pending = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 82 | + | let owner = try! slots::commit(&mut state::SHARED.domains.slots[..], pending); |
|
| 83 | + | set owners[i] = owner; |
|
| 84 | + | capability::initialize(&mut MEMORY[i].table, owner); |
|
| 85 | + | set state::SHARED.domains.records[owner.index].state = domains::Lifecycle::Active; |
|
| 86 | + | set state::SHARED.domains.records[owner.index].memory = &mut MEMORY[i]; |
|
| 87 | + | } |
|
| 88 | + | let target = try! capability::install(&mut MEMORY[0].table, capability::Entry { |
|
| 89 | + | kind: abi::Kind::Domain, object: owners[1], rights: abi::Rights(0) |
|
| 90 | + | }); |
|
| 91 | + | let device = try! devices::install(&STORE, &mut MEMORY[0].table, 2); |
|
| 92 | + | let original = try! capability::get(&MEMORY[0].table, device); |
|
| 93 | + | let granted = abi::Handle(try! calls::invoke( |
|
| 94 | + | owners[0], |
|
| 95 | + | 10, |
|
| 96 | + | &[ |
|
| 97 | + | *device, *target, abi::READ as u64, 0 |
|
| 98 | + | ], |
|
| 99 | + | 0 |
|
| 100 | + | )); |
|
| 101 | + | let reader = try! capability::get(&MEMORY[1].table, granted); |
|
| 102 | + | assert reader.object == original.object and reader.rights == abi::Rights(abi::READ); |
|
| 103 | + | let moved = abi::Handle(try! calls::invoke( |
|
| 104 | + | owners[0], |
|
| 105 | + | 11, |
|
| 106 | + | &[ |
|
| 107 | + | *device, *target, *original.rights as u64, 0 |
|
| 108 | + | ], |
|
| 109 | + | 0 |
|
| 110 | + | )); |
|
| 111 | + | assert try! capability::get(&MEMORY[1].table, moved) == original; |
|
| 112 | + | let old = try! abi::decode(device); |
|
| 113 | + | assert MEMORY[0].table.slots[old.object.index].state == slots::State::Free; |
|
| 114 | + | let dropped = try! calls::invoke(owners[1], 12, &[*moved, 0, 0, 0], 0); |
|
| 115 | + | assert dropped == 0; |
|
| 116 | + | assert (try! devices::get(&state::SHARED.devices, reader.object)).info.base == 0x10001000; |
|
| 117 | + | } |
|
| 118 | + | ||
| 119 | + | /// Every register access checks current authority and the complete physical extent. |
|
| 120 | + | @test unsafe fn access() throws (testing::TestError) { |
|
| 121 | + | initialize(); |
|
| 122 | + | capability::initialize(&mut MEMORY[0].table, abi::Ref { index: 0, generation: 1 }); |
|
| 123 | + | let device = try! devices::install(&STORE, &mut MEMORY[0].table, 0); |
|
| 124 | + | for width in [1 as u64, 2, 4, 8] { |
|
| 125 | + | assert try! devices::address( |
|
| 126 | + | &STORE, &MEMORY[0].table, device, 256 - width, width, false |
|
| 127 | + | ) == 0x10000100 - width; |
|
| 128 | + | assert try! devices::address( |
|
| 129 | + | &STORE, &MEMORY[0].table, device, 0, width, true |
|
| 130 | + | ) == 0x10000000; |
|
| 131 | + | } |
|
| 132 | + | let mut rejected: u32 = 0; |
|
| 133 | + | for offset in [1 as u64, 253, 256, 0xffffffffffffffff] { |
|
| 134 | + | try devices::address(&STORE, &MEMORY[0].table, device, offset, 4, false) catch error { |
|
| 135 | + | assert error == abi::Error::InvalidArg; |
|
| 136 | + | set rejected += 1; |
|
| 137 | + | }; |
|
| 138 | + | } |
|
| 139 | + | for width in [0 as u64, 3, 16, 0x100000004] { |
|
| 140 | + | try devices::address(&STORE, &MEMORY[0].table, device, 0, width, false) catch error { |
|
| 141 | + | assert error == abi::Error::InvalidArg; |
|
| 142 | + | set rejected += 1; |
|
| 143 | + | }; |
|
| 144 | + | } |
|
| 145 | + | let clock = try! devices::install(&STORE, &mut MEMORY[0].table, 1); |
|
| 146 | + | try devices::address(&STORE, &MEMORY[0].table, clock, 0, 8, true) catch error { |
|
| 147 | + | assert error == abi::Error::Denied; |
|
| 148 | + | set rejected += 1; |
|
| 149 | + | }; |
|
| 150 | + | try! capability::invalidate(&mut MEMORY[0].table, device); |
|
| 151 | + | let replacement = try! devices::install(&STORE, &mut MEMORY[0].table, 0); |
|
| 152 | + | assert replacement <> device; |
|
| 153 | + | try devices::address(&STORE, &MEMORY[0].table, device, 0, 1, false) catch error { |
|
| 154 | + | assert error == abi::Error::BadHandle; |
|
| 155 | + | set rejected += 1; |
|
| 156 | + | }; |
|
| 157 | + | set STORE.regions[0].info.base = 0x10000001; |
|
| 158 | + | try devices::address(&STORE, &MEMORY[0].table, replacement, 0, 4, false) catch error { |
|
| 159 | + | assert error == abi::Error::InvalidArg; |
|
| 160 | + | set rejected += 1; |
|
| 161 | + | }; |
|
| 162 | + | assert rejected == 11; |
|
| 163 | + | } |
kernel/kernel/tests/dispatch.rad
added
+290 -0
| 1 | + | //! Dispatch selection at CPU-window boundaries and idle gaps. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::slots; |
|
| 6 | + | use kernel::domains; |
|
| 7 | + | use kernel::budgets; |
|
| 8 | + | use kernel::capability; |
|
| 9 | + | use kernel::dispatch; |
|
| 10 | + | use kernel::platform; |
|
| 11 | + | use kernel::range; |
|
| 12 | + | use kernel::registers; |
|
| 13 | + | use kernel::remote; |
|
| 14 | + | use kernel::trap; |
|
| 15 | + | ||
| 16 | + | /// Platform resources used to validate CLINT address selection. |
|
| 17 | + | unsafe static PLATFORM: platform::Platform = undefined; |
|
| 18 | + | ||
| 19 | + | /// Request queues used to complete forwarded calls. |
|
| 20 | + | unsafe static REMOTE: remote::Store = undefined; |
|
| 21 | + | ||
| 22 | + | /// Domain lifetimes and saved contexts used for selection. |
|
| 23 | + | unsafe static DOMAINS: domains::Store = undefined; |
|
| 24 | + | /// Exclusive intervals presented to dispatch. |
|
| 25 | + | unsafe static BUDGETS: budgets::Store = undefined; |
|
| 26 | + | /// Owner of the test CPU capabilities. |
|
| 27 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 28 | + | ||
| 29 | + | /// Install one live context and two separated windows on hart zero. |
|
| 30 | + | unsafe fn initialize() { |
|
| 31 | + | domains::initialize(&mut DOMAINS); |
|
| 32 | + | budgets::initialize(&mut BUDGETS); |
|
| 33 | + | let owner = abi::Ref { index: 0, generation: 1 }; |
|
| 34 | + | capability::initialize(&mut TABLE, owner); |
|
| 35 | + | set DOMAINS.slots[0] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 36 | + | set DOMAINS.records[0].state = domains::Lifecycle::Active; |
|
| 37 | + | set DOMAINS.contextSlots[0] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 38 | + | set DOMAINS.contexts[0].owner = owner; |
|
| 39 | + | set DOMAINS.contexts[0].state = domains::ContextState::Ready; |
|
| 40 | + | set DOMAINS.contexts[0].hart = nil; |
|
| 41 | + | let first = try! budgets::seed(&mut BUDGETS, &mut TABLE, 0, 10, 40); |
|
| 42 | + | let gap = try! budgets::split(&mut BUDGETS, &mut TABLE, first, 20, 0); |
|
| 43 | + | let last = try! budgets::split(&mut BUDGETS, &mut TABLE, gap, 30, 0); |
|
| 44 | + | let a = try! capability::get(&TABLE, first); |
|
| 45 | + | let b = try! capability::get(&TABLE, last); |
|
| 46 | + | set BUDGETS.windows[a.object.index].context = owner; |
|
| 47 | + | set BUDGETS.windows[b.object.index].context = owner; |
|
| 48 | + | } |
|
| 49 | + | ||
| 50 | + | /// Wakeup reaches future windows and excludes expired or stopped contexts. |
|
| 51 | + | @test unsafe fn wakeTargets() throws (testing::TestError) { |
|
| 52 | + | initialize(); |
|
| 53 | + | set DOMAINS.contexts[0].state = domains::ContextState::Waiting; |
|
| 54 | + | assert budgets::wakeTargets(&BUDGETS, &DOMAINS, TABLE.owner, 0) == 1; |
|
| 55 | + | assert budgets::wakeTargets(&BUDGETS, &DOMAINS, TABLE.owner, 25) == 1; |
|
| 56 | + | assert budgets::wakeTargets(&BUDGETS, &DOMAINS, TABLE.owner, 40) == 0; |
|
| 57 | + | set DOMAINS.contexts[0].state = domains::ContextState::Stopped; |
|
| 58 | + | assert budgets::wakeTargets(&BUDGETS, &DOMAINS, TABLE.owner, 0) == 0; |
|
| 59 | + | set DOMAINS.contexts[0].state = domains::ContextState::Ready; |
|
| 60 | + | set DOMAINS.contextSlots[0].generation += 1; |
|
| 61 | + | assert budgets::wakeTargets(&BUDGETS, &DOMAINS, TABLE.owner, 0) == 0; |
|
| 62 | + | } |
|
| 63 | + | ||
| 64 | + | /// Dispatch selects only the half-open interval and wakes at the next runnable start. |
|
| 65 | + | @test unsafe fn boundaries() throws (testing::TestError) { |
|
| 66 | + | initialize(); |
|
| 67 | + | let before = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 9); |
|
| 68 | + | try testing::expect(before.context == nil and before.deadline == 10); |
|
| 69 | + | let first = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 10); |
|
| 70 | + | try testing::expect(first.context == TABLE.owner and first.deadline == 20); |
|
| 71 | + | let gap = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 20); |
|
| 72 | + | try testing::expect(gap.context == nil and gap.deadline == 30); |
|
| 73 | + | let last = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 30); |
|
| 74 | + | try testing::expect(last.context == TABLE.owner and last.deadline == 40); |
|
| 75 | + | let ended = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 40); |
|
| 76 | + | try testing::expect(ended.context == nil and ended.deadline == 0xffffffffffffffff); |
|
| 77 | + | let other = try! dispatch::select(&BUDGETS, &DOMAINS, 1, 10); |
|
| 78 | + | try testing::expect(other.context == nil and other.deadline == 0xffffffffffffffff); |
|
| 79 | + | } |
|
| 80 | + | ||
| 81 | + | /// Every live window on the hart, runnable or not, sets a deadline at its end, |
|
| 82 | + | /// where dispatch retires it. |
|
| 83 | + | @test unsafe fn expiry() throws (testing::TestError) { |
|
| 84 | + | initialize(); |
|
| 85 | + | for i in 0..BUDGETS.windows.len { |
|
| 86 | + | set BUDGETS.windows[i].context = nil; |
|
| 87 | + | } |
|
| 88 | + | let unbound = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 10); |
|
| 89 | + | try testing::expect(unbound.context == nil and unbound.deadline == 20); |
|
| 90 | + | initialize(); |
|
| 91 | + | set DOMAINS.contexts[0].state = domains::ContextState::Waiting; |
|
| 92 | + | let waiting = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 30); |
|
| 93 | + | try testing::expect(waiting.context == nil and waiting.deadline == 40); |
|
| 94 | + | } |
|
| 95 | + | ||
| 96 | + | /// A context becomes eligible only after its previous hart releases ownership. |
|
| 97 | + | @test unsafe fn ownership() throws (testing::TestError) { |
|
| 98 | + | initialize(); |
|
| 99 | + | set DOMAINS.contexts[0].hart = 1; |
|
| 100 | + | let future = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 9); |
|
| 101 | + | assert future.context == nil and future.deadline == 10; |
|
| 102 | + | let occupied = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 10); |
|
| 103 | + | assert occupied.context == nil and occupied.deadline == 20; |
|
| 104 | + | set DOMAINS.contexts[0].hart = nil; |
|
| 105 | + | let free = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 10); |
|
| 106 | + | assert free.context == TABLE.owner and free.deadline == 20; |
|
| 107 | + | set DOMAINS.contexts[0].hart = 0; |
|
| 108 | + | let local = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 10); |
|
| 109 | + | assert local == free; |
|
| 110 | + | } |
|
| 111 | + | ||
| 112 | + | /// Pending domains and stale context generations cannot become runnable through a budget. |
|
| 113 | + | @test unsafe fn lifetimes() throws (testing::TestError) { |
|
| 114 | + | initialize(); |
|
| 115 | + | set DOMAINS.records[0].state = domains::Lifecycle::Pending; |
|
| 116 | + | let pending = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 10); |
|
| 117 | + | try testing::expect(pending.context == nil); |
|
| 118 | + | set DOMAINS.records[0].state = domains::Lifecycle::Active; |
|
| 119 | + | set DOMAINS.contextSlots[0].generation = 2; |
|
| 120 | + | let stale = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 10); |
|
| 121 | + | try testing::expect(stale.context == nil); |
|
| 122 | + | set DOMAINS.contextSlots[0].generation = 1; |
|
| 123 | + | set DOMAINS.contexts[0].state = domains::ContextState::Stopped; |
|
| 124 | + | let stopped = try! dispatch::select(&BUDGETS, &DOMAINS, 0, 10); |
|
| 125 | + | try testing::expect(stopped.context == nil); |
|
| 126 | + | } |
|
| 127 | + | ||
| 128 | + | /// Each online hart has a separate comparison word and shares one clock register. |
|
| 129 | + | @test unsafe fn timerRegisters() throws (testing::TestError) { |
|
| 130 | + | set PLATFORM.harts = 0x81; |
|
| 131 | + | set PLATFORM.deviceCount = 1; |
|
| 132 | + | set PLATFORM.devices[0] = platform::Device { |
|
| 133 | + | kind: platform::Kind::Clint, memory: range::Range { start: 0x2000000, end: 0x2010000 } |
|
| 134 | + | }; |
|
| 135 | + | set PLATFORM.ramCount = 0; |
|
| 136 | + | let first = try! dispatch::timer(&PLATFORM, 0); |
|
| 137 | + | let last = try! dispatch::timer(&PLATFORM, 7); |
|
| 138 | + | assert registers::start(first.window) == 0x2000000 and registers::size(first.window) == 0x10000; |
|
| 139 | + | assert first.software == 0 and last.software == 28; |
|
| 140 | + | try testing::expect(first.window == last.window and first.compare == 0x4000); |
|
| 141 | + | try testing::expect(last.compare == first.compare + 56); |
|
| 142 | + | try testing::expect(registers::contains(last.window, last.compare, 8)); |
|
| 143 | + | try testing::expect(registers::contains(last.window, last.software, 4)); |
|
| 144 | + | try testing::expect(registers::contains(last.window, 0xbff8, 8)); |
|
| 145 | + | let mut failures: u32 = 0; |
|
| 146 | + | try dispatch::timer(&PLATFORM, 1) catch err { |
|
| 147 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 148 | + | set failures += 1; |
|
| 149 | + | }; |
|
| 150 | + | set PLATFORM.devices[0].memory.end = 0x200bff8; |
|
| 151 | + | try dispatch::timer(&PLATFORM, 0) catch err { |
|
| 152 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 153 | + | set failures += 1; |
|
| 154 | + | }; |
|
| 155 | + | try testing::expect(failures == 2); |
|
| 156 | + | } |
|
| 157 | + | ||
| 158 | + | /// Trap callback supplied to the frame-exchange fixture. |
|
| 159 | + | fn handler(frame: &mut trap::Frame, hart: &mut trap::Hart) {} |
|
| 160 | + | ||
| 161 | + | /// Exchange preserves an M-mode continuation while another context uses its own stack. |
|
| 162 | + | @test unsafe fn continuations() throws (testing::TestError) { |
|
| 163 | + | initialize(); |
|
| 164 | + | let mut frame = trap::Frame { registers: [0; 32], pc: 0x40, status: 0x80, cause: 0, value: 0 }; |
|
| 165 | + | set DOMAINS.contexts[0].frame = frame; |
|
| 166 | + | set DOMAINS.contexts[0].kernelStack = range::Range { start: 0x8000, end: 0xc000 }; |
|
| 167 | + | set DOMAINS.contextSlots[1] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 168 | + | set DOMAINS.contexts[1].owner = TABLE.owner; |
|
| 169 | + | set DOMAINS.contexts[1].state = domains::ContextState::Ready; |
|
| 170 | + | set DOMAINS.contexts[1].hart = nil; |
|
| 171 | + | set DOMAINS.contexts[1].frame = frame; |
|
| 172 | + | set DOMAINS.contexts[1].kernelStack = range::Range { start: 0xc000, end: 0x10000 }; |
|
| 173 | + | set BUDGETS.windows[1].context = abi::Ref { index: 1, generation: 1 }; |
|
| 174 | + | let mut idle = frame; |
|
| 175 | + | set idle.pc = 0x100; |
|
| 176 | + | set idle.status = 0x1880; |
|
| 177 | + | set idle.registers[2] = 0x4000; |
|
| 178 | + | let mut state = dispatch::State { current: nil, budget: nil }; |
|
| 179 | + | let config = dispatch::Config { |
|
| 180 | + | hart: 0, idle, stack: range::Range { start: 0x2000, end: 0x4000 }, maintenance: 1 |
|
| 181 | + | }; |
|
| 182 | + | let mut anchor = trap::Hart { |
|
| 183 | + | stackTop: 0x4000, |
|
| 184 | + | stackBottom: 0x2000, |
|
| 185 | + | kernelGp: 0x20000, |
|
| 186 | + | handler, |
|
| 187 | + | savedT0: 0, |
|
| 188 | + | savedT1: 0, |
|
| 189 | + | savedSp: 0, |
|
| 190 | + | dispatchTop: 0x4000, |
|
| 191 | + | dispatchFrame: undefined, |
|
| 192 | + | }; |
|
| 193 | + | let first = try! dispatch::exchange( |
|
| 194 | + | &mut state, &config, &BUDGETS, &mut DOMAINS, &mut frame, &mut anchor, 10 |
|
| 195 | + | ); |
|
| 196 | + | try testing::expect(frame.pc == 0x40 and anchor.stackTop == 0xc000); |
|
| 197 | + | try testing::expect(DOMAINS.contexts[0].hart == 0); |
|
| 198 | + | set frame.pc = 0x88; |
|
| 199 | + | set frame.status = 0x1880; |
|
| 200 | + | set frame.registers[2] = 0xb000; |
|
| 201 | + | set frame.registers[5] = 43; |
|
| 202 | + | try! dispatch::exchange( |
|
| 203 | + | &mut state, &config, &BUDGETS, &mut DOMAINS, &mut frame, &mut anchor, 20 |
|
| 204 | + | ); |
|
| 205 | + | try testing::expect(anchor.stackBottom == 0xc000 and anchor.stackTop == 0x10000); |
|
| 206 | + | try testing::expect(DOMAINS.contexts[0].hart == nil and DOMAINS.contexts[1].hart == 0); |
|
| 207 | + | set frame.pc = 0x77; |
|
| 208 | + | try! dispatch::exchange( |
|
| 209 | + | &mut state, &config, &BUDGETS, &mut DOMAINS, &mut frame, &mut anchor, 30 |
|
| 210 | + | ); |
|
| 211 | + | try testing::expect(frame.pc == 0x88 and frame.status == 0x1880); |
|
| 212 | + | try testing::expect(frame.registers[2] == 0xb000 and frame.registers[5] == 43); |
|
| 213 | + | try testing::expect(DOMAINS.contexts[1].frame.pc == 0x77 and anchor.stackTop == 0xc000); |
|
| 214 | + | let ended = try! dispatch::exchange( |
|
| 215 | + | &mut state, &config, &BUDGETS, &mut DOMAINS, &mut frame, &mut anchor, 40 |
|
| 216 | + | ); |
|
| 217 | + | try testing::expect( |
|
| 218 | + | ended.context == nil |
|
| 219 | + | and frame.pc == idle.pc |
|
| 220 | + | and frame.registers[2] == 0x4000 |
|
| 221 | + | ); |
|
| 222 | + | try testing::expect(anchor.stackTop == 0x4000 and anchor.stackBottom == 0x2000); |
|
| 223 | + | } |
|
| 224 | + | ||
| 225 | + | /// Post a forwarded call for context zero and complete it with the given reply. |
|
| 226 | + | unsafe fn complete(reply: remote::Reply) -> abi::Ref { |
|
| 227 | + | initialize(); |
|
| 228 | + | remote::initialize(&mut REMOTE, 3); |
|
| 229 | + | let context = abi::Ref { index: 0, generation: 1 }; |
|
| 230 | + | let call = remote::Call { owner: TABLE.owner, context, operation: 70, arguments: [0; 4] }; |
|
| 231 | + | let id = try! remote::send(&mut REMOTE, 0, 1, remote::Action::Budget(call)); |
|
| 232 | + | set DOMAINS.contexts[0].pending = id; |
|
| 233 | + | let delivery = try! remote::take(&mut REMOTE, 1) else panic "complete: missing request"; |
|
| 234 | + | remote::respond(&mut REMOTE, 1, id, reply); |
|
| 235 | + | assert remote::acknowledge(&mut REMOTE, 1, id); |
|
| 236 | + | return id; |
|
| 237 | + | } |
|
| 238 | + | ||
| 239 | + | /// A reply is returned once, and its request and context retention are released. |
|
| 240 | + | @test unsafe fn replyRelease() throws (testing::TestError) { |
|
| 241 | + | let id = complete(remote::Reply::Value(9)); |
|
| 242 | + | try testing::expect(try! dispatch::takeReply(&mut DOMAINS, &mut REMOTE, 0, id) == 9); |
|
| 243 | + | try testing::expect(DOMAINS.contexts[0].pending == nil); |
|
| 244 | + | let mut released = false; |
|
| 245 | + | try remote::completed(&REMOTE, 0, id) catch error { |
|
| 246 | + | try testing::expect(error == abi::Error::BadHandle); |
|
| 247 | + | set released = true; |
|
| 248 | + | }; |
|
| 249 | + | try testing::expect(released); |
|
| 250 | + | } |
|
| 251 | + | ||
| 252 | + | /// An error reply is thrown, and the request and context retention are still released. |
|
| 253 | + | @test unsafe fn replyError() throws (testing::TestError) { |
|
| 254 | + | let id = complete(remote::Reply::Error(abi::Error::Denied)); |
|
| 255 | + | let mut thrown = false; |
|
| 256 | + | try dispatch::takeReply(&mut DOMAINS, &mut REMOTE, 0, id) catch error { |
|
| 257 | + | try testing::expect(error == abi::Error::Denied); |
|
| 258 | + | set thrown = true; |
|
| 259 | + | }; |
|
| 260 | + | try testing::expect(thrown); |
|
| 261 | + | try testing::expect(DOMAINS.contexts[0].pending == nil); |
|
| 262 | + | let mut released = false; |
|
| 263 | + | try remote::completed(&REMOTE, 0, id) catch error { |
|
| 264 | + | try testing::expect(error == abi::Error::BadHandle); |
|
| 265 | + | set released = true; |
|
| 266 | + | }; |
|
| 267 | + | try testing::expect(released); |
|
| 268 | + | } |
|
| 269 | + | ||
| 270 | + | /// An unproductive pass doubles the interval up to the cap; progress or no |
|
| 271 | + | /// remaining work returns it to the base. |
|
| 272 | + | @test fn maintenanceBackoff() throws (testing::TestError) { |
|
| 273 | + | let base: u64 = 100; |
|
| 274 | + | let cap = base * dispatch::MAINTENANCE_BACKOFF; |
|
| 275 | + | try testing::expect(dispatch::maintenanceInterval(base, 0, false) == 2 * base); |
|
| 276 | + | try testing::expect(dispatch::maintenanceInterval(base, base, false) == 2 * base); |
|
| 277 | + | try testing::expect(dispatch::maintenanceInterval(base, 2 * base, false) == 4 * base); |
|
| 278 | + | let mut interval = base; |
|
| 279 | + | for i in 0..32 as u32 { |
|
| 280 | + | set interval = dispatch::maintenanceInterval(base, interval, false); |
|
| 281 | + | try testing::expect(interval <= cap); |
|
| 282 | + | } |
|
| 283 | + | try testing::expect(interval == cap); |
|
| 284 | + | try testing::expect(dispatch::maintenanceInterval(base, cap, false) == cap); |
|
| 285 | + | try testing::expect(dispatch::maintenanceInterval(base, cap - 1, false) == cap); |
|
| 286 | + | try testing::expect(dispatch::maintenanceInterval(base, cap, true) == base); |
|
| 287 | + | try testing::expect(dispatch::maintenanceInterval(base, 0, true) == base); |
|
| 288 | + | let huge: u64 = 0xffffffffffffffff / 2; |
|
| 289 | + | try testing::expect(dispatch::maintenanceInterval(huge, huge, false) == 0xffffffffffffffff); |
|
| 290 | + | } |
kernel/kernel/tests/domains.rad
added
+1512 -0
| 1 | + | //! Pending domain creation, initial context state, and creation authority. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use std::arch::rv64::shared; |
|
| 5 | + | use kernel::state; |
|
| 6 | + | use kernel::abi; |
|
| 7 | + | use kernel::slots; |
|
| 8 | + | use kernel::backing; |
|
| 9 | + | use kernel::capability; |
|
| 10 | + | use kernel::registry; |
|
| 11 | + | use kernel::domains; |
|
| 12 | + | use kernel::lifecycle; |
|
| 13 | + | use kernel::loader; |
|
| 14 | + | use kernel::instances; |
|
| 15 | + | use kernel::physical; |
|
| 16 | + | use kernel::limits; |
|
| 17 | + | use kernel::frames; |
|
| 18 | + | use kernel::budgets; |
|
| 19 | + | use kernel::interrupts; |
|
| 20 | + | use kernel::transactions; |
|
| 21 | + | use kernel::pages; |
|
| 22 | + | use kernel::events; |
|
| 23 | + | ||
| 24 | + | /// Exclusive resources released by the domain reaper. |
|
| 25 | + | unsafe static WINDOWS: budgets::Store = undefined; |
|
| 26 | + | /// Interrupt ownership released after hardware masking. |
|
| 27 | + | unsafe static IRQS: interrupts::Store = undefined; |
|
| 28 | + | ||
| 29 | + | /// Caller-owned storage used while a context stack is initialized. |
|
| 30 | + | unsafe static CALLS: lifecycle::Calls = undefined; |
|
| 31 | + | ||
| 32 | + | /// Mapped physical memory for child metadata and package instances. |
|
| 33 | + | static RAM: [u64; 131072] = [0; 131072]; |
|
| 34 | + | /// Domain and context metadata under test. |
|
| 35 | + | unsafe static DOMAINS: domains::Store = undefined; |
|
| 36 | + | /// Physical allocation and exposure state. |
|
| 37 | + | unsafe static BACKINGS: backing::Store = undefined; |
|
| 38 | + | /// Shared resident image registry. |
|
| 39 | + | unsafe static PACKAGES: registry::Store = undefined; |
|
| 40 | + | /// Calling bootstrap domain's capability table. |
|
| 41 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 42 | + | /// Executable image capability in the calling table. |
|
| 43 | + | static IMAGE: abi::Handle = abi::Handle(0); |
|
| 44 | + | /// Page metadata used to check activation authority without reading user memory. |
|
| 45 | + | unsafe static PAGES: pages::Store = undefined; |
|
| 46 | + | /// Event ring of the bootstrap domain, for tests where it receives terminal events. |
|
| 47 | + | unsafe static ROOT: events::Ring = undefined; |
|
| 48 | + | ||
| 49 | + | /// Initialize bootstrap authority and one executable image over mapped frames. |
|
| 50 | + | unsafe fn initialize(rights: u16) { |
|
| 51 | + | domains::initialize(&mut DOMAINS); |
|
| 52 | + | backing::initialize(&mut BACKINGS); |
|
| 53 | + | registry::initialize(&mut PACKAGES); |
|
| 54 | + | let pointer = &RAM[0]; |
|
| 55 | + | let base = pointer as u64; |
|
| 56 | + | let aligned = (base + 4095) & ~4095; |
|
| 57 | + | set BACKINGS.pool.count = (@sizeOf([u64; 131072]) - (aligned - base) as u32) / 4096; |
|
| 58 | + | for i in 0..BACKINGS.pool.count { |
|
| 59 | + | set BACKINGS.pool.addresses[i] = aligned + i as u64 * 4096; |
|
| 60 | + | set BACKINGS.pool.free[i / 64] |= 1 << (i % 64) as u64; |
|
| 61 | + | } |
|
| 62 | + | let pending = try! slots::reserve(&mut DOMAINS.slots[..]); |
|
| 63 | + | let owner = try! slots::commit(&mut DOMAINS.slots[..], pending); |
|
| 64 | + | set DOMAINS.records[owner.index].state = domains::Lifecycle::Active; |
|
| 65 | + | set DOMAINS.records[owner.index].parent = nil; |
|
| 66 | + | set DOMAINS.records[owner.index].creator = owner; |
|
| 67 | + | try! backing::registerDomain(&mut BACKINGS, owner); |
|
| 68 | + | capability::initialize(&mut TABLE, owner); |
|
| 69 | + | let authority = try! capability::install(&mut TABLE, capability::Entry { |
|
| 70 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(rights) |
|
| 71 | + | }); |
|
| 72 | + | let imageSlot = try! registry::reserve(&mut PACKAGES); |
|
| 73 | + | let image = slots::reference(&imageSlot); |
|
| 74 | + | let object = try! registry::publish( |
|
| 75 | + | &mut PACKAGES, |
|
| 76 | + | imageSlot, |
|
| 77 | + | "program", |
|
| 78 | + | shared::Package { |
|
| 79 | + | name: "program", |
|
| 80 | + | dependencies: &[], |
|
| 81 | + | slot: image.index, |
|
| 82 | + | codeAddress: 0x80000000, |
|
| 83 | + | code: &[0x00008067 as u32, 0x00008067], |
|
| 84 | + | exports: &[ |
|
| 85 | + | shared::Symbol { |
|
| 86 | + | name: "program::work", target: shared::Target::Function(0x80000004) |
|
| 87 | + | }, |
|
| 88 | + | shared::Symbol { |
|
| 89 | + | name: "program::entry", target: shared::Target::Function(0x80000000) |
|
| 90 | + | }, |
|
| 91 | + | ], |
|
| 92 | + | entry: 0x80000000, |
|
| 93 | + | template: &[7], |
|
| 94 | + | memory: 8, |
|
| 95 | + | alignment: 8, |
|
| 96 | + | relocations: &[], |
|
| 97 | + | } |
|
| 98 | + | ); |
|
| 99 | + | set IMAGE = try! registry::install(&PACKAGES, &mut TABLE, object); |
|
| 100 | + | } |
|
| 101 | + | ||
| 102 | + | /// Failed root construction restores capacity and permits a fresh attempt. |
|
| 103 | + | @test unsafe fn bootstrapRollback() throws (testing::TestError) { |
|
| 104 | + | initialize(abi::CREATE); |
|
| 105 | + | let image = (try! capability::get(&TABLE, IMAGE)).object; |
|
| 106 | + | domains::initialize(&mut DOMAINS); |
|
| 107 | + | set BACKINGS.domains[0] = 0; |
|
| 108 | + | let capacity = BACKINGS.pool.count; |
|
| 109 | + | for count in [0 as u32, 1, 8, 32] { |
|
| 110 | + | set BACKINGS.pool.count = count; |
|
| 111 | + | let mut failed = false; |
|
| 112 | + | try domains::bootstrap(&mut DOMAINS, &mut BACKINGS, &PACKAGES, image) catch error { |
|
| 113 | + | assert error == abi::Error::OutOfMemory; |
|
| 114 | + | set failed = true; |
|
| 115 | + | }; |
|
| 116 | + | assert failed and DOMAINS.slots[0].state == slots::State::Free; |
|
| 117 | + | assert DOMAINS.contextSlots[0].state == slots::State::Free; |
|
| 118 | + | assert BACKINGS.domains[0] == 0 and DOMAINS.events.queues[0].generation == 0; |
|
| 119 | + | for i in 0..count { |
|
| 120 | + | assert frames::available(&BACKINGS.pool, i); |
|
| 121 | + | } |
|
| 122 | + | } |
|
| 123 | + | set BACKINGS.pool.count = capacity; |
|
| 124 | + | let root = try! domains::bootstrap(&mut DOMAINS, &mut BACKINGS, &PACKAGES, image); |
|
| 125 | + | assert root.index == 0 and root.generation > 1; |
|
| 126 | + | assert (try! domains::get(&DOMAINS, root)).parent == nil; |
|
| 127 | + | } |
|
| 128 | + | ||
| 129 | + | /// Root construction owns slot zero and installs the fixed bootstrap handles. |
|
| 130 | + | @test unsafe fn bootstrap() throws (testing::TestError) { |
|
| 131 | + | initialize(abi::CREATE); |
|
| 132 | + | let image = (try! capability::get(&TABLE, IMAGE)).object; |
|
| 133 | + | domains::initialize(&mut DOMAINS); |
|
| 134 | + | set BACKINGS.domains[0] = 0; |
|
| 135 | + | let root = try! domains::bootstrap(&mut DOMAINS, &mut BACKINGS, &PACKAGES, image); |
|
| 136 | + | assert root.index == 0 and root.generation == 1; |
|
| 137 | + | let domain = try! domains::get(&DOMAINS, root); |
|
| 138 | + | assert domain.creator == root |
|
| 139 | + | and domain.parent == nil |
|
| 140 | + | and domain.state == domains::Lifecycle::Pending; |
|
| 141 | + | assert domain.memory.table.slots[0].state == slots::State::Live; |
|
| 142 | + | let self = domain.memory.table.entries[0]; |
|
| 143 | + | assert self.kind == abi::Kind::Domain and self.object == root; |
|
| 144 | + | assert *self.rights == (domains::MANAGEMENT_RIGHTS | abi::CREATE | abi::ALLOCATE); |
|
| 145 | + | let queue = try! abi::decode(domain.events); |
|
| 146 | + | assert queue.object.index == 1 and queue.kind == abi::Kind::Events; |
|
| 147 | + | assert domain.memory.table.entries[1].object == root; |
|
| 148 | + | assert DOMAINS.contexts[domain.initial.index].frame.pc == 0x80000000; |
|
| 149 | + | assert DOMAINS.contexts[domain.initial.index].frame.registers[3] == instances::address( |
|
| 150 | + | &DOMAINS.tables[root.index] |
|
| 151 | + | ); |
|
| 152 | + | for word in DOMAINS.ancestors[0] { |
|
| 153 | + | assert word == 0; |
|
| 154 | + | } |
|
| 155 | + | let mut busy = false; |
|
| 156 | + | try domains::bootstrap(&mut DOMAINS, &mut BACKINGS, &PACKAGES, image) catch error { |
|
| 157 | + | assert error == abi::Error::Busy; |
|
| 158 | + | set busy = true; |
|
| 159 | + | }; |
|
| 160 | + | assert busy and DOMAINS.slots[1].state == slots::State::Free; |
|
| 161 | + | } |
|
| 162 | + | ||
| 163 | + | /// Interrupted creation returns all private storage after its caller releases its hart. |
|
| 164 | + | @test unsafe fn cancelledCreation() throws (testing::TestError) { |
|
| 165 | + | initialize(abi::CREATE); |
|
| 166 | + | lifecycle::initialize(&mut CALLS); |
|
| 167 | + | let handle = try! domains::create( |
|
| 168 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 169 | + | ); |
|
| 170 | + | let permit = try! capability::get(&TABLE, handle); |
|
| 171 | + | let mut caller = try! domains::get(&DOMAINS, permit.object); |
|
| 172 | + | set DOMAINS.records[permit.object.index].state = domains::Lifecycle::Active; |
|
| 173 | + | let authority = try! capability::install(&mut caller.memory.table, capability::Entry { |
|
| 174 | + | kind: abi::Kind::Domain, object: permit.object, rights: abi::Rights(abi::CREATE) |
|
| 175 | + | }); |
|
| 176 | + | let root = caller.image; |
|
| 177 | + | let image = try! registry::install(&PACKAGES, &mut caller.memory.table, root); |
|
| 178 | + | set PAGES.backings = BACKINGS; |
|
| 179 | + | let pending = try! domains::reserve( |
|
| 180 | + | &mut DOMAINS, &mut PAGES.backings, &PACKAGES, &mut caller.memory.table, authority, image |
|
| 181 | + | ); |
|
| 182 | + | let mut creation: domains::Creation = undefined; |
|
| 183 | + | match &pending { |
|
| 184 | + | case domains::Reservation::Held(value, tokens) => { |
|
| 185 | + | set creation = *value; |
|
| 186 | + | } |
|
| 187 | + | } |
|
| 188 | + | assert DOMAINS.contexts[creation.context.index].owner == creation.domain; |
|
| 189 | + | match lifecycle::holdDomain(&mut CALLS, &mut DOMAINS, caller.initial, pending) { |
|
| 190 | + | case lifecycle::DomainHeld::Held(lease, stranded) => { |
|
| 191 | + | try! domains::prepareCreation( |
|
| 192 | + | ®istry::capture(&PACKAGES), &lifecycle::domainCreation(&CALLS, &lease), &stranded |
|
| 193 | + | ); |
|
| 194 | + | lifecycle::cancelAllocations( |
|
| 195 | + | &mut CALLS, |
|
| 196 | + | &mut DOMAINS, |
|
| 197 | + | &mut PAGES, |
|
| 198 | + | &mut state::SHARED.loader, |
|
| 199 | + | &mut state::SHARED.registry |
|
| 200 | + | ); |
|
| 201 | + | assert DOMAINS.slots[creation.domain.index].state == slots::State::Reserved; |
|
| 202 | + | set DOMAINS.records[permit.object.index].state = domains::Lifecycle::Dead; |
|
| 203 | + | set DOMAINS.contexts[caller.initial.index].state = domains::ContextState::Stopped; |
|
| 204 | + | set DOMAINS.contexts[caller.initial.index].hart = 1; |
|
| 205 | + | lifecycle::cancelAllocations( |
|
| 206 | + | &mut CALLS, |
|
| 207 | + | &mut DOMAINS, |
|
| 208 | + | &mut PAGES, |
|
| 209 | + | &mut state::SHARED.loader, |
|
| 210 | + | &mut state::SHARED.registry |
|
| 211 | + | ); |
|
| 212 | + | assert DOMAINS.contexts[caller.initial.index].reservation; |
|
| 213 | + | assert not frames::available(&PAGES.backings.pool, creation.storage.allocation.first); |
|
| 214 | + | assert lifecycle::leaseContext(lease) == caller.initial; |
|
| 215 | + | set DOMAINS.contexts[caller.initial.index].hart = nil; |
|
| 216 | + | lifecycle::cancelAllocations( |
|
| 217 | + | &mut CALLS, |
|
| 218 | + | &mut DOMAINS, |
|
| 219 | + | &mut PAGES, |
|
| 220 | + | &mut state::SHARED.loader, |
|
| 221 | + | &mut state::SHARED.registry |
|
| 222 | + | ); |
|
| 223 | + | assert not DOMAINS.contexts[caller.initial.index].reservation; |
|
| 224 | + | assert DOMAINS.slots[creation.domain.index].state == slots::State::Free; |
|
| 225 | + | assert DOMAINS.contextSlots[creation.context.index].state == slots::State::Free; |
|
| 226 | + | assert caller.memory.table.slots[creation.handle.index].state == slots::State::Free; |
|
| 227 | + | assert PAGES.backings.pool.retiredCount == 3; |
|
| 228 | + | while PAGES.backings.pool.retiredCount > 0 { |
|
| 229 | + | let reclaimed = frames::reclaim(&mut PAGES.backings.pool, 256); |
|
| 230 | + | assert reclaimed > 0 and reclaimed <= 256; |
|
| 231 | + | } |
|
| 232 | + | for run in [ |
|
| 233 | + | creation.storage.allocation, |
|
| 234 | + | creation.storage.kernelFrames, |
|
| 235 | + | creation.storage.graph.frames, |
|
| 236 | + | ] { |
|
| 237 | + | for i in run.first..run.first + run.count { |
|
| 238 | + | assert frames::available(&PAGES.backings.pool, i); |
|
| 239 | + | } |
|
| 240 | + | } |
|
| 241 | + | } |
|
| 242 | + | } |
|
| 243 | + | set DOMAINS.records[permit.object.index].state = domains::Lifecycle::Active; |
|
| 244 | + | let next = try! domains::reserve( |
|
| 245 | + | &mut DOMAINS, &mut PAGES.backings, &PACKAGES, &mut caller.memory.table, authority, image |
|
| 246 | + | ); |
|
| 247 | + | match lifecycle::holdDomain(&mut CALLS, &mut DOMAINS, caller.initial, next) { |
|
| 248 | + | case lifecycle::DomainHeld::Held(active, access) => { |
|
| 249 | + | try! domains::prepareCreation( |
|
| 250 | + | ®istry::capture(&PACKAGES), &lifecycle::domainCreation(&CALLS, &active), &access |
|
| 251 | + | ); |
|
| 252 | + | let ready = lifecycle::takeDomain(&mut CALLS, &mut DOMAINS, active, access); |
|
| 253 | + | let published = try! domains::publish( |
|
| 254 | + | &mut DOMAINS, &mut PAGES.backings, &mut caller.memory.table, ready |
|
| 255 | + | ); |
|
| 256 | + | let entry = try! capability::get(&caller.memory.table, published); |
|
| 257 | + | assert entry.object.index == creation.domain.index |
|
| 258 | + | and entry.object.generation <> creation.domain.generation; |
|
| 259 | + | assert (try! domains::get(&DOMAINS, entry.object)).state == domains::Lifecycle::Pending; |
|
| 260 | + | assert not DOMAINS.contexts[caller.initial.index].reservation; |
|
| 261 | + | } |
|
| 262 | + | } |
|
| 263 | + | } |
|
| 264 | + | ||
| 265 | + | /// Private storage and terminal identity have separate reclamation boundaries. |
|
| 266 | + | @test unsafe fn reclaimDomain() throws (testing::TestError) { |
|
| 267 | + | initialize(abi::CREATE); |
|
| 268 | + | let parentHandle = try! domains::create( |
|
| 269 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 270 | + | ); |
|
| 271 | + | let handle = try! domains::create( |
|
| 272 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 273 | + | ); |
|
| 274 | + | let entry = try! capability::get(&TABLE, handle); |
|
| 275 | + | let child = try! domains::get(&DOMAINS, entry.object); |
|
| 276 | + | try! domains::reparent(&mut DOMAINS, &TABLE, handle, parentHandle); |
|
| 277 | + | let mut private = child.memory; |
|
| 278 | + | let authority = try! capability::install(&mut private.table, capability::Entry { |
|
| 279 | + | kind: abi::Kind::Domain, object: entry.object, rights: abi::Rights(abi::CREATE) |
|
| 280 | + | }); |
|
| 281 | + | let image = try! registry::install(&PACKAGES, &mut private.table, child.image); |
|
| 282 | + | let descendantHandle = try! domains::create( |
|
| 283 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut private.table, authority, image |
|
| 284 | + | ); |
|
| 285 | + | let descendant = try! capability::get(&private.table, descendantHandle); |
|
| 286 | + | let word = entry.object.index / 32; |
|
| 287 | + | let bit = 1 << (entry.object.index % 32); |
|
| 288 | + | assert (DOMAINS.ancestors[descendant.object.index][word] & bit) <> 0; |
|
| 289 | + | assert not try! domains::quiescent(&DOMAINS, entry.object); |
|
| 290 | + | set DOMAINS.contexts[child.initial.index].hart = 1; |
|
| 291 | + | assert try! domains::terminate(&mut DOMAINS, entry.object, events::CHILD_EXIT, 17) == 2; |
|
| 292 | + | assert not try! domains::quiescent(&DOMAINS, entry.object); |
|
| 293 | + | set DOMAINS.contexts[child.initial.index].hart = nil; |
|
| 294 | + | set DOMAINS.contexts[child.initial.index].pending = abi::Ref { index: 0, generation: 1 }; |
|
| 295 | + | assert not try! domains::quiescent(&DOMAINS, entry.object); |
|
| 296 | + | set DOMAINS.contexts[child.initial.index].pending = nil; |
|
| 297 | + | set DOMAINS.contexts[child.initial.index].reservation = true; |
|
| 298 | + | assert not try! domains::quiescent(&DOMAINS, entry.object); |
|
| 299 | + | set DOMAINS.contexts[child.initial.index].reservation = false; |
|
| 300 | + | let reserved = try! slots::reserve(&mut DOMAINS.contextSlots[..]); |
|
| 301 | + | let context = slots::reference(&reserved); |
|
| 302 | + | set DOMAINS.contexts[context.index].owner = entry.object; |
|
| 303 | + | assert not try! domains::quiescent(&DOMAINS, entry.object); |
|
| 304 | + | let mut busy = false; |
|
| 305 | + | try domains::retireContext(&mut DOMAINS, &mut BACKINGS.pool, entry.object) catch error { |
|
| 306 | + | assert error == abi::Error::Busy; |
|
| 307 | + | set busy = true; |
|
| 308 | + | }; |
|
| 309 | + | assert busy and BACKINGS.pool.retiredCount == 0; |
|
| 310 | + | try! slots::cancel(&mut DOMAINS.contextSlots[..], reserved); |
|
| 311 | + | assert try! domains::quiescent(&DOMAINS, entry.object); |
|
| 312 | + | assert try! domains::retireContext(&mut DOMAINS, &mut BACKINGS.pool, entry.object); |
|
| 313 | + | assert not try! domains::retireContext(&mut DOMAINS, &mut BACKINGS.pool, entry.object); |
|
| 314 | + | let mut handles = false; |
|
| 315 | + | try domains::retireStorage( |
|
| 316 | + | &mut DOMAINS, &mut BACKINGS, entry.object, &child.memory.table |
|
| 317 | + | ) catch error { |
|
| 318 | + | assert error == abi::Error::Busy; |
|
| 319 | + | set handles = true; |
|
| 320 | + | }; |
|
| 321 | + | assert handles; |
|
| 322 | + | let mut memory = child.memory; |
|
| 323 | + | for cap in [child.events, authority, image, descendantHandle] { |
|
| 324 | + | try! capability::invalidate(&mut memory.table, cap); |
|
| 325 | + | } |
|
| 326 | + | let mut exposed = false; |
|
| 327 | + | try domains::retireStorage( |
|
| 328 | + | &mut DOMAINS, &mut BACKINGS, entry.object, &child.memory.table |
|
| 329 | + | ) catch error { |
|
| 330 | + | assert error == abi::Error::Busy; |
|
| 331 | + | set exposed = true; |
|
| 332 | + | }; |
|
| 333 | + | assert exposed; |
|
| 334 | + | while not try! backing::endDomainStep(&mut BACKINGS, entry.object, 64) {} |
|
| 335 | + | assert physical::size(&DOMAINS.tables[entry.object.index]) == limits::PACKAGES as u64 * 8; |
|
| 336 | + | try! domains::retireStorage(&mut DOMAINS, &mut BACKINGS, entry.object, &child.memory.table); |
|
| 337 | + | assert physical::size(&DOMAINS.tables[entry.object.index]) == 0; |
|
| 338 | + | assert BACKINGS.pool.retiredCount == 3; |
|
| 339 | + | let mut stale = false; |
|
| 340 | + | try domains::get(&DOMAINS, entry.object) catch error { |
|
| 341 | + | assert error == abi::Error::BadHandle; |
|
| 342 | + | set stale = true; |
|
| 343 | + | }; |
|
| 344 | + | assert stale and DOMAINS.slots[entry.object.index].state == slots::State::Live; |
|
| 345 | + | while BACKINGS.pool.retiredCount > 0 { |
|
| 346 | + | let reclaimed = frames::reclaim(&mut BACKINGS.pool, 256); |
|
| 347 | + | assert reclaimed > 0 and reclaimed <= 256; |
|
| 348 | + | } |
|
| 349 | + | assert not try! events::acknowledged(&DOMAINS.events, entry.object); |
|
| 350 | + | let mut retained = false; |
|
| 351 | + | try domains::releaseIdentity(&mut DOMAINS, entry.object) catch error { |
|
| 352 | + | assert error == abi::Error::Busy; |
|
| 353 | + | set retained = true; |
|
| 354 | + | }; |
|
| 355 | + | assert retained; |
|
| 356 | + | let parentEntry = try! capability::get(&TABLE, parentHandle); |
|
| 357 | + | let mut parent = try! domains::get(&DOMAINS, parentEntry.object); |
|
| 358 | + | assert try! events::flush( |
|
| 359 | + | &mut DOMAINS.events, parentEntry.object, &mut parent.memory.ring |
|
| 360 | + | ) == 1; |
|
| 361 | + | let event = try! events::pop(&mut parent.memory.ring) |
|
| 362 | + | else panic "reclaimDomain: missing terminal event"; |
|
| 363 | + | assert event.value == abi::id(entry.object) and event.code == 17; |
|
| 364 | + | try! events::refresh(&mut DOMAINS.events, parentEntry.object, &parent.memory.ring); |
|
| 365 | + | try! domains::releaseIdentity(&mut DOMAINS, entry.object); |
|
| 366 | + | assert DOMAINS.slots[entry.object.index].state == slots::State::Free; |
|
| 367 | + | assert (DOMAINS.ancestors[descendant.object.index][word] & bit) == 0; |
|
| 368 | + | let next = try! domains::create( |
|
| 369 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 370 | + | ); |
|
| 371 | + | let replacement = try! capability::get(&TABLE, next); |
|
| 372 | + | assert replacement.object.index == entry.object.index |
|
| 373 | + | and replacement.object.generation <> entry.object.generation; |
|
| 374 | + | assert (try! domains::get(&DOMAINS, replacement.object)).state == domains::Lifecycle::Pending; |
|
| 375 | + | assert try! domains::terminateTree( |
|
| 376 | + | &mut DOMAINS, replacement.object, events::CHILD_EXIT, 0 |
|
| 377 | + | ) == 0; |
|
| 378 | + | assert (try! domains::get(&DOMAINS, descendant.object)).state == domains::Lifecycle::Pending; |
|
| 379 | + | } |
|
| 380 | + | ||
| 381 | + | /// Reaping releases private authority while surviving shared pages retain their frames. |
|
| 382 | + | @test unsafe fn reaper() throws (testing::TestError) { |
|
| 383 | + | initialize(abi::CREATE | abi::ALLOCATE); |
|
| 384 | + | budgets::initialize(&mut WINDOWS); |
|
| 385 | + | interrupts::initialize(&mut IRQS, 8); |
|
| 386 | + | let parentHandle = try! domains::create( |
|
| 387 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 388 | + | ); |
|
| 389 | + | let handle = try! domains::create( |
|
| 390 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 391 | + | ); |
|
| 392 | + | let target = (try! capability::get(&TABLE, handle)).object; |
|
| 393 | + | let receiver = (try! capability::get(&TABLE, parentHandle)).object; |
|
| 394 | + | let mut child = try! domains::get(&DOMAINS, target); |
|
| 395 | + | let mut parent = try! domains::get(&DOMAINS, receiver); |
|
| 396 | + | try! domains::reparent(&mut DOMAINS, &TABLE, handle, parentHandle); |
|
| 397 | + | set PAGES.backings = BACKINGS; |
|
| 398 | + | slots::initialize(&mut PAGES.slots[..]); |
|
| 399 | + | let authority = try! capability::install(&mut child.memory.table, capability::Entry { |
|
| 400 | + | kind: abi::Kind::Domain, object: target, rights: abi::Rights(abi::ALLOCATE) |
|
| 401 | + | }); |
|
| 402 | + | let page = try! pages::allocate(&mut PAGES, &mut child.memory.table, authority, 1); |
|
| 403 | + | let shared = try! pages::grant( |
|
| 404 | + | &mut PAGES, &child.memory.table, &mut parent.memory.table, page, abi::READ as u64 |
|
| 405 | + | ); |
|
| 406 | + | let allocation = |
|
| 407 | + | (try! pages::get( |
|
| 408 | + | &PAGES, |
|
| 409 | + | (try! capability::get( |
|
| 410 | + | &child.memory.table, page |
|
| 411 | + | )).object |
|
| 412 | + | )).backing; |
|
| 413 | + | let run = PAGES.backings.records[allocation.index].run; |
|
| 414 | + | let budget = try! budgets::seed(&mut WINDOWS, &mut child.memory.table, 0, 0, 1000); |
|
| 415 | + | let window = (try! capability::get(&child.memory.table, budget)).object; |
|
| 416 | + | try! interrupts::seed(&mut IRQS, &mut child.memory.table, 7); |
|
| 417 | + | try! capability::install(&mut child.memory.table, capability::Entry { |
|
| 418 | + | kind: abi::Kind::Domain, |
|
| 419 | + | object: abi::Ref { index: 511, generation: 99 }, |
|
| 420 | + | rights: abi::Rights(0), |
|
| 421 | + | }); |
|
| 422 | + | set DOMAINS.contexts[child.initial.index].hart = 0; |
|
| 423 | + | assert try! domains::terminate(&mut DOMAINS, target, events::CHILD_EXIT, 19) == 1; |
|
| 424 | + | assert not lifecycle::reapNext(&mut DOMAINS, &mut PAGES, &mut WINDOWS, &mut IRQS); |
|
| 425 | + | assert DOMAINS.teardown[target.index] == domains::Teardown::Live; |
|
| 426 | + | assert (try! capability::get(&child.memory.table, page)).kind == abi::Kind::Page; |
|
| 427 | + | set DOMAINS.contexts[child.initial.index].hart = nil; |
|
| 428 | + | assert lifecycle::reapNext(&mut DOMAINS, &mut PAGES, &mut WINDOWS, &mut IRQS); |
|
| 429 | + | assert DOMAINS.teardown[target.index] == domains::Teardown::Reaping; |
|
| 430 | + | assert child.memory.table.slots[0].state == slots::State::Free; |
|
| 431 | + | assert (try! capability::get(&child.memory.table, page)).kind == abi::Kind::Page; |
|
| 432 | + | for step in 0..2000 { |
|
| 433 | + | if DOMAINS.teardown[target.index] == domains::Teardown::Retired { |
|
| 434 | + | break; |
|
| 435 | + | } |
|
| 436 | + | assert lifecycle::reapNext(&mut DOMAINS, &mut PAGES, &mut WINDOWS, &mut IRQS); |
|
| 437 | + | } |
|
| 438 | + | assert DOMAINS.teardown[target.index] == domains::Teardown::Retired; |
|
| 439 | + | assert WINDOWS.slots[window.index].state == slots::State::Free; |
|
| 440 | + | assert IRQS.slots[6].state == slots::State::Free; |
|
| 441 | + | assert not lifecycle::reapNext(&mut DOMAINS, &mut PAGES, &mut WINDOWS, &mut IRQS); |
|
| 442 | + | while PAGES.backings.pool.retiredCount > 0 { |
|
| 443 | + | let reclaimed = frames::reclaim(&mut PAGES.backings.pool, 256); |
|
| 444 | + | assert reclaimed > 0 and reclaimed <= 256; |
|
| 445 | + | } |
|
| 446 | + | assert not frames::available(&PAGES.backings.pool, run.first); |
|
| 447 | + | assert PAGES.backings.records[allocation.index].exposed == 1; |
|
| 448 | + | assert (try! pages::get( |
|
| 449 | + | &PAGES, |
|
| 450 | + | (try! capability::get( |
|
| 451 | + | &parent.memory.table, shared |
|
| 452 | + | )).object |
|
| 453 | + | )).count == 1; |
|
| 454 | + | assert try! events::flush(&mut DOMAINS.events, receiver, &mut parent.memory.ring) == 1; |
|
| 455 | + | let event = try! events::pop(&mut parent.memory.ring) |
|
| 456 | + | else panic "reaper: missing reaped child event"; |
|
| 457 | + | assert event.value == abi::id(target) and event.code == 19; |
|
| 458 | + | try! events::refresh(&mut DOMAINS.events, receiver, &parent.memory.ring); |
|
| 459 | + | assert lifecycle::reapNext(&mut DOMAINS, &mut PAGES, &mut WINDOWS, &mut IRQS); |
|
| 460 | + | assert DOMAINS.slots[target.index].state == slots::State::Free; |
|
| 461 | + | assert try! domains::terminate(&mut DOMAINS, receiver, events::CHILD_EXIT, 0) == 0; |
|
| 462 | + | for step in 0..2000 { |
|
| 463 | + | if DOMAINS.slots[receiver.index].state == slots::State::Free { |
|
| 464 | + | break; |
|
| 465 | + | } |
|
| 466 | + | assert lifecycle::reapNext(&mut DOMAINS, &mut PAGES, &mut WINDOWS, &mut IRQS); |
|
| 467 | + | } |
|
| 468 | + | assert DOMAINS.slots[receiver.index].state == slots::State::Free; |
|
| 469 | + | while PAGES.backings.pool.retiredCount > 0 { |
|
| 470 | + | let reclaimed = frames::reclaim(&mut PAGES.backings.pool, 256); |
|
| 471 | + | assert reclaimed > 0 and reclaimed <= 256; |
|
| 472 | + | } |
|
| 473 | + | for i in 0..PAGES.backings.pool.count { |
|
| 474 | + | assert frames::available(&PAGES.backings.pool, i); |
|
| 475 | + | } |
|
| 476 | + | } |
|
| 477 | + | ||
| 478 | + | /// Initial return reports to the current parent and retains its terminal identity. |
|
| 479 | + | @test unsafe fn terminalParent() throws (testing::TestError) { |
|
| 480 | + | initialize(abi::CREATE); |
|
| 481 | + | let first = try! domains::create( |
|
| 482 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 483 | + | ); |
|
| 484 | + | let second = try! domains::create( |
|
| 485 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 486 | + | ); |
|
| 487 | + | let handle = try! domains::create( |
|
| 488 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 489 | + | ); |
|
| 490 | + | let target = try! capability::get(&TABLE, handle); |
|
| 491 | + | let destination = try! capability::get(&TABLE, second); |
|
| 492 | + | try! domains::reparent(&mut DOMAINS, &TABLE, handle, first); |
|
| 493 | + | try! domains::reparent(&mut DOMAINS, &TABLE, handle, second); |
|
| 494 | + | let child = try! domains::get(&DOMAINS, target.object); |
|
| 495 | + | set DOMAINS.contexts[child.initial.index].hart = 3; |
|
| 496 | + | set DOMAINS.contexts[child.initial.index].pending = abi::Ref { index: 7, generation: 1 }; |
|
| 497 | + | let stopped = try! domains::returned(&mut DOMAINS, target.object, child.initial); |
|
| 498 | + | assert stopped == 8 and DOMAINS.contexts[child.initial.index].hart == 3; |
|
| 499 | + | assert DOMAINS.contexts[child.initial.index].pending <> nil; |
|
| 500 | + | let repeated = try! domains::terminate(&mut DOMAINS, target.object, events::FAULT, 99); |
|
| 501 | + | assert repeated == 0; |
|
| 502 | + | assert DOMAINS.records[target.object.index].state == domains::Lifecycle::Dead; |
|
| 503 | + | assert DOMAINS.events.terminals[target.object.index].state == events::State::Pending; |
|
| 504 | + | assert DOMAINS.events.terminals[target.object.index].receiver == destination.object; |
|
| 505 | + | assert DOMAINS.events.queues[target.object.index].generation == 0; |
|
| 506 | + | let mut rejected = false; |
|
| 507 | + | try domains::reparent(&mut DOMAINS, &TABLE, handle, first) catch error { |
|
| 508 | + | assert error == abi::Error::BadHandle; |
|
| 509 | + | set rejected = true; |
|
| 510 | + | }; |
|
| 511 | + | assert rejected and DOMAINS.records[target.object.index].creator == TABLE.owner; |
|
| 512 | + | let mut parent = try! domains::get(&DOMAINS, destination.object); |
|
| 513 | + | assert try! events::flush( |
|
| 514 | + | &mut DOMAINS.events, destination.object, &mut parent.memory.ring |
|
| 515 | + | ) == 1; |
|
| 516 | + | assert not try! events::acknowledged(&DOMAINS.events, target.object); |
|
| 517 | + | let event = try! events::pop(&mut parent.memory.ring) |
|
| 518 | + | else panic "terminalParent: missing child exit"; |
|
| 519 | + | assert event.kind == events::CHILD_EXIT |
|
| 520 | + | and event.code == 0 |
|
| 521 | + | and event.value == abi::id(target.object); |
|
| 522 | + | try! events::refresh(&mut DOMAINS.events, destination.object, &parent.memory.ring); |
|
| 523 | + | assert try! events::acknowledged(&DOMAINS.events, target.object); |
|
| 524 | + | } |
|
| 525 | + | ||
| 526 | + | /// Parent exit moves surviving children, and terminal events that the parent has not read, |
|
| 527 | + | /// to the nearest live ancestor without changing their creator. |
|
| 528 | + | @test unsafe fn parentExit() throws (testing::TestError) { |
|
| 529 | + | initialize(abi::CREATE); |
|
| 530 | + | let grand = TABLE.owner; |
|
| 531 | + | try! events::open(&mut DOMAINS.events, grand, &mut ROOT); |
|
| 532 | + | let parentHandle = try! domains::create( |
|
| 533 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 534 | + | ); |
|
| 535 | + | let childHandle = try! domains::create( |
|
| 536 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 537 | + | ); |
|
| 538 | + | let earlierHandle = try! domains::create( |
|
| 539 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 540 | + | ); |
|
| 541 | + | let parentRef = (try! capability::get(&TABLE, parentHandle)).object; |
|
| 542 | + | let childRef = (try! capability::get(&TABLE, childHandle)).object; |
|
| 543 | + | let earlierRef = (try! capability::get(&TABLE, earlierHandle)).object; |
|
| 544 | + | try! domains::reparent(&mut DOMAINS, &TABLE, childHandle, parentHandle); |
|
| 545 | + | try! domains::reparent(&mut DOMAINS, &TABLE, earlierHandle, parentHandle); |
|
| 546 | + | assert DOMAINS.records[parentRef.index].parent == grand; |
|
| 547 | + | let earlier = try! domains::get(&DOMAINS, earlierRef); |
|
| 548 | + | try! domains::returned(&mut DOMAINS, earlierRef, earlier.initial); |
|
| 549 | + | assert DOMAINS.events.terminals[earlierRef.index].receiver == parentRef; |
|
| 550 | + | let parent = try! domains::get(&DOMAINS, parentRef); |
|
| 551 | + | try! domains::returned(&mut DOMAINS, parentRef, parent.initial); |
|
| 552 | + | let child = try! domains::get(&DOMAINS, childRef); |
|
| 553 | + | assert child.state == domains::Lifecycle::Pending |
|
| 554 | + | and child.parent == grand |
|
| 555 | + | and child.creator == TABLE.owner; |
|
| 556 | + | assert DOMAINS.events.terminals[earlierRef.index].receiver == grand |
|
| 557 | + | and DOMAINS.events.terminals[earlierRef.index].state == events::State::Pending; |
|
| 558 | + | try! domains::returned(&mut DOMAINS, childRef, child.initial); |
|
| 559 | + | assert try! events::flush(&mut DOMAINS.events, grand, &mut ROOT) == 3; |
|
| 560 | + | let mut seen: u32 = 0; |
|
| 561 | + | for i in 0..3 { |
|
| 562 | + | let event = try! events::pop(&mut ROOT) else panic "parentExit: missing terminal event"; |
|
| 563 | + | assert event.kind == events::CHILD_EXIT; |
|
| 564 | + | if event.value == abi::id(parentRef) { |
|
| 565 | + | set seen |= 1; |
|
| 566 | + | } |
|
| 567 | + | if event.value == abi::id(childRef) { |
|
| 568 | + | set seen |= 2; |
|
| 569 | + | } |
|
| 570 | + | if event.value == abi::id(earlierRef) { |
|
| 571 | + | set seen |= 4; |
|
| 572 | + | } |
|
| 573 | + | } |
|
| 574 | + | assert seen == 7; |
|
| 575 | + | } |
|
| 576 | + | ||
| 577 | + | /// Cascade teardown reattaches a survivor past every domain that dies with its parent, and |
|
| 578 | + | /// moves the events of those domains to the same ancestor. |
|
| 579 | + | @test unsafe fn cascadeHeir() throws (testing::TestError) { |
|
| 580 | + | initialize(abi::CREATE); |
|
| 581 | + | let grand = TABLE.owner; |
|
| 582 | + | try! events::open(&mut DOMAINS.events, grand, &mut ROOT); |
|
| 583 | + | let package = try! capability::get(&TABLE, IMAGE); |
|
| 584 | + | let first = descendant(&mut TABLE, package.object); |
|
| 585 | + | let mut ancestor = try! domains::get(&DOMAINS, first); |
|
| 586 | + | let second = descendant(&mut ancestor.memory.table, package.object); |
|
| 587 | + | let survivorHandle = try! domains::create( |
|
| 588 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 589 | + | ); |
|
| 590 | + | let survivor = (try! capability::get(&TABLE, survivorHandle)).object; |
|
| 591 | + | let secondReceiver = try! capability::install(&mut TABLE, capability::Entry { |
|
| 592 | + | kind: abi::Kind::Domain, object: second, rights: abi::Rights(abi::WAKE) |
|
| 593 | + | }); |
|
| 594 | + | try! domains::reparent(&mut DOMAINS, &TABLE, survivorHandle, secondReceiver); |
|
| 595 | + | let harts = try! domains::terminateTree(&mut DOMAINS, first, events::CHILD_EXIT, 7); |
|
| 596 | + | assert DOMAINS.records[second.index].state == domains::Lifecycle::Dead; |
|
| 597 | + | assert DOMAINS.records[survivor.index].state == domains::Lifecycle::Pending |
|
| 598 | + | and DOMAINS.records[survivor.index].parent == grand; |
|
| 599 | + | assert DOMAINS.events.terminals[second.index].receiver == grand |
|
| 600 | + | and DOMAINS.events.terminals[second.index].state == events::State::Pending; |
|
| 601 | + | assert try! events::flush(&mut DOMAINS.events, grand, &mut ROOT) == 2; |
|
| 602 | + | } |
|
| 603 | + | ||
| 604 | + | /// A child that is also its parent's parent, as after the boot handoff from root to the |
|
| 605 | + | /// scheduler, has no ancestor left when the parent exits, and does not become its own parent. |
|
| 606 | + | @test unsafe fn handoffCycle() throws (testing::TestError) { |
|
| 607 | + | initialize(abi::CREATE); |
|
| 608 | + | let parentHandle = try! domains::create( |
|
| 609 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 610 | + | ); |
|
| 611 | + | let childHandle = try! domains::create( |
|
| 612 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 613 | + | ); |
|
| 614 | + | let parentRef = (try! capability::get(&TABLE, parentHandle)).object; |
|
| 615 | + | let childRef = (try! capability::get(&TABLE, childHandle)).object; |
|
| 616 | + | try! domains::reparent(&mut DOMAINS, &TABLE, childHandle, parentHandle); |
|
| 617 | + | try! domains::reparent(&mut DOMAINS, &TABLE, parentHandle, childHandle); |
|
| 618 | + | let parent = try! domains::get(&DOMAINS, parentRef); |
|
| 619 | + | try! domains::returned(&mut DOMAINS, parentRef, parent.initial); |
|
| 620 | + | assert DOMAINS.records[childRef.index].parent == nil; |
|
| 621 | + | assert DOMAINS.events.terminals[parentRef.index].receiver == childRef |
|
| 622 | + | and DOMAINS.events.terminals[parentRef.index].state == events::State::Pending; |
|
| 623 | + | } |
|
| 624 | + | ||
| 625 | + | /// A parent cycle that dies as a whole leaves its survivors with no parent, and events |
|
| 626 | + | /// with no live receiver end undelivered. |
|
| 627 | + | @test unsafe fn parentCycle() throws (testing::TestError) { |
|
| 628 | + | initialize(abi::CREATE); |
|
| 629 | + | let package = try! capability::get(&TABLE, IMAGE); |
|
| 630 | + | let first = descendant(&mut TABLE, package.object); |
|
| 631 | + | let mut ancestor = try! domains::get(&DOMAINS, first); |
|
| 632 | + | let second = descendant(&mut ancestor.memory.table, package.object); |
|
| 633 | + | let survivorHandle = try! domains::create( |
|
| 634 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 635 | + | ); |
|
| 636 | + | let survivor = (try! capability::get(&TABLE, survivorHandle)).object; |
|
| 637 | + | let firstAuthority = try! capability::install(&mut TABLE, capability::Entry { |
|
| 638 | + | kind: abi::Kind::Domain, object: first, rights: abi::Rights(abi::DESTROY) |
|
| 639 | + | }); |
|
| 640 | + | let secondReceiver = try! capability::install(&mut TABLE, capability::Entry { |
|
| 641 | + | kind: abi::Kind::Domain, object: second, rights: abi::Rights(abi::WAKE) |
|
| 642 | + | }); |
|
| 643 | + | try! domains::reparent(&mut DOMAINS, &TABLE, firstAuthority, secondReceiver); |
|
| 644 | + | try! domains::reparent(&mut DOMAINS, &TABLE, survivorHandle, secondReceiver); |
|
| 645 | + | let harts = try! domains::terminateTree(&mut DOMAINS, first, events::CHILD_EXIT, 7); |
|
| 646 | + | assert DOMAINS.records[survivor.index].parent == nil; |
|
| 647 | + | assert try! events::acknowledged(&DOMAINS.events, first); |
|
| 648 | + | assert try! events::acknowledged(&DOMAINS.events, second); |
|
| 649 | + | let child = try! domains::get(&DOMAINS, survivor); |
|
| 650 | + | try! domains::returned(&mut DOMAINS, survivor, child.initial); |
|
| 651 | + | assert try! events::acknowledged(&DOMAINS.events, survivor); |
|
| 652 | + | } |
|
| 653 | + | ||
| 654 | + | /// Create a child through its creator's own authority and resident image. |
|
| 655 | + | unsafe fn descendant(table: &mut capability::Table, image: abi::Ref) -> abi::Ref { |
|
| 656 | + | let owner = table.owner; |
|
| 657 | + | let authority = try! capability::install(table, capability::Entry { |
|
| 658 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::CREATE) |
|
| 659 | + | }); |
|
| 660 | + | let executable = try! registry::install(&PACKAGES, table, image); |
|
| 661 | + | let handle = try! domains::create( |
|
| 662 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, table, authority, executable |
|
| 663 | + | ); |
|
| 664 | + | return (try! capability::get(table, handle)).object; |
|
| 665 | + | } |
|
| 666 | + | ||
| 667 | + | /// Cascade retains creation ancestry after reparenting and an intermediate exit. |
|
| 668 | + | @test unsafe fn cascadeAncestry() throws (testing::TestError) { |
|
| 669 | + | initialize(abi::CREATE); |
|
| 670 | + | let package = try! capability::get(&TABLE, IMAGE); |
|
| 671 | + | let first = descendant(&mut TABLE, package.object); |
|
| 672 | + | let mut ancestor = try! domains::get(&DOMAINS, first); |
|
| 673 | + | let second = descendant(&mut ancestor.memory.table, package.object); |
|
| 674 | + | let mut middle = try! domains::get(&DOMAINS, second); |
|
| 675 | + | let third = descendant(&mut middle.memory.table, package.object); |
|
| 676 | + | let child = try! capability::install(&mut TABLE, capability::Entry { |
|
| 677 | + | kind: abi::Kind::Domain, object: third, rights: abi::Rights(abi::DESTROY) |
|
| 678 | + | }); |
|
| 679 | + | let root = TABLE.owner; |
|
| 680 | + | let receiver = try! capability::install(&mut TABLE, capability::Entry { |
|
| 681 | + | kind: abi::Kind::Domain, object: root, rights: abi::Rights(abi::WAKE) |
|
| 682 | + | }); |
|
| 683 | + | try! domains::reparent(&mut DOMAINS, &TABLE, child, receiver); |
|
| 684 | + | let reserved = try! events::reserve(&mut DOMAINS.events, third); |
|
| 685 | + | let mut busy = false; |
|
| 686 | + | try domains::terminateTree(&mut DOMAINS, first, events::CHILD_EXIT, 7) catch error { |
|
| 687 | + | assert error == abi::Error::Busy; |
|
| 688 | + | set busy = true; |
|
| 689 | + | }; |
|
| 690 | + | assert busy and DOMAINS.records[first.index].state == domains::Lifecycle::Pending; |
|
| 691 | + | assert DOMAINS.records[second.index].state == domains::Lifecycle::Pending; |
|
| 692 | + | assert DOMAINS.events.terminals[first.index].state == events::State::Free; |
|
| 693 | + | events::cancel(&mut DOMAINS.events, reserved); |
|
| 694 | + | let stopped = try! domains::terminate(&mut DOMAINS, second, events::CHILD_EXIT, 12); |
|
| 695 | + | assert stopped == 0 and DOMAINS.records[third.index].state == domains::Lifecycle::Pending; |
|
| 696 | + | assert DOMAINS.records[third.index].parent == TABLE.owner; |
|
| 697 | + | set DOMAINS.contexts[ancestor.initial.index].hart = 1; |
|
| 698 | + | let last = try! domains::get(&DOMAINS, third); |
|
| 699 | + | set DOMAINS.contexts[last.initial.index].hart = 3; |
|
| 700 | + | let harts = try! domains::terminateTree(&mut DOMAINS, first, events::CHILD_EXIT, 7); |
|
| 701 | + | assert harts == 10; |
|
| 702 | + | assert DOMAINS.records[first.index].state == domains::Lifecycle::Dead; |
|
| 703 | + | assert DOMAINS.records[second.index].state == domains::Lifecycle::Dead; |
|
| 704 | + | assert DOMAINS.records[third.index].state == domains::Lifecycle::Dead; |
|
| 705 | + | assert DOMAINS.records[TABLE.owner.index].state == domains::Lifecycle::Active; |
|
| 706 | + | assert DOMAINS.events.terminals[second.index].event.code == 12; |
|
| 707 | + | assert DOMAINS.events.terminals[first.index].event.code == 7; |
|
| 708 | + | assert DOMAINS.events.terminals[third.index].event.kind == events::FAULT; |
|
| 709 | + | assert DOMAINS.events.terminals[third.index].event.code == abi::FAULT_DESTROYED; |
|
| 710 | + | assert DOMAINS.records[third.index].creator == second; |
|
| 711 | + | assert DOMAINS.contexts[last.initial.index].hart == 3; |
|
| 712 | + | } |
|
| 713 | + | ||
| 714 | + | /// Activate the domain that an Execute capability in `TABLE` names, as the call boundary does. |
|
| 715 | + | unsafe fn activate(handle: abi::Handle, stack: abi::Handle, args: abi::Handle, size: u64) |
|
| 716 | + | throws (abi::Error) |
|
| 717 | + | { |
|
| 718 | + | let entry = try capability::lookup(&TABLE, handle, abi::Kind::Domain, abi::Rights( |
|
| 719 | + | abi::EXECUTE |
|
| 720 | + | )); |
|
| 721 | + | let domain = try domains::get(&DOMAINS, entry.object); |
|
| 722 | + | try domains::activate( |
|
| 723 | + | &mut DOMAINS, &mut PAGES, entry.object, &mut *domain.memory, stack, args, size |
|
| 724 | + | ); |
|
| 725 | + | } |
|
| 726 | + | ||
| 727 | + | /// Reserve a context in the domain that an Execute capability in `TABLE` names. |
|
| 728 | + | unsafe fn contextReserve( |
|
| 729 | + | handle: abi::Handle, start: abi::ContextStart |
|
| 730 | + | ) -> domains::ContextReservation throws (abi::Error) { |
|
| 731 | + | let permission = try capability::authority(&TABLE, handle, abi::Rights(abi::EXECUTE)); |
|
| 732 | + | let domain = try domains::get(&DOMAINS, permission.object); |
|
| 733 | + | return try domains::contextReserve( |
|
| 734 | + | &mut DOMAINS, &mut PAGES, &PACKAGES, permission.object, start, &mut domain.memory.table |
|
| 735 | + | ); |
|
| 736 | + | } |
|
| 737 | + | ||
| 738 | + | /// Reserve, clear, and publish a context in the domain that `handle` names. |
|
| 739 | + | unsafe fn contextCreate(handle: abi::Handle, start: abi::ContextStart) -> abi::Ref |
|
| 740 | + | throws (abi::Error) |
|
| 741 | + | { |
|
| 742 | + | let pending = try contextReserve(handle, start); |
|
| 743 | + | domains::contextClear(&pending); |
|
| 744 | + | return domains::contextPublish(&mut DOMAINS, pending); |
|
| 745 | + | } |
|
| 746 | + | ||
| 747 | + | /// Allocate pages in the table of a child domain, which holds allocation authority over itself. |
|
| 748 | + | unsafe fn childPage(child: &domains::Domain, owner: abi::Ref, count: u64) -> abi::Handle { |
|
| 749 | + | let authority = try! capability::install(&mut child.memory.table, capability::Entry { |
|
| 750 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::ALLOCATE) |
|
| 751 | + | }); |
|
| 752 | + | let page = try! pages::allocate(&mut PAGES, &mut child.memory.table, authority, count); |
|
| 753 | + | try! capability::invalidate(&mut child.memory.table, authority); |
|
| 754 | + | return page; |
|
| 755 | + | } |
|
| 756 | + | ||
| 757 | + | /// The Page record behind a handle of the child's table. |
|
| 758 | + | unsafe fn pageOf(child: &domains::Domain, handle: abi::Handle) -> pages::Page { |
|
| 759 | + | return try! pages::get(&PAGES, (try! capability::get(&child.memory.table, handle)).object); |
|
| 760 | + | } |
|
| 761 | + | ||
| 762 | + | /// Suspend a context unless its domain has an unread notification; tell whether events were |
|
| 763 | + | /// published. |
|
| 764 | + | unsafe fn wait(owner: abi::Ref, context: abi::Ref) -> bool throws (abi::Error) { |
|
| 765 | + | let domain = try domains::get(&DOMAINS, owner); |
|
| 766 | + | return try domains::wait(&mut DOMAINS, owner, context, &mut domain.memory.ring); |
|
| 767 | + | } |
|
| 768 | + | ||
| 769 | + | /// Send a wakeup token through a Wake capability in `TABLE`, as the call boundary does. |
|
| 770 | + | unsafe fn wakeup(handle: abi::Handle, token: u32) throws (abi::Error) { |
|
| 771 | + | let permit = try capability::lookup(&TABLE, handle, abi::Kind::Domain, abi::Rights(abi::WAKE)); |
|
| 772 | + | let event = events::Event { |
|
| 773 | + | kind: events::WAKEUP, reserved: 0, code: token, value: abi::id(TABLE.owner) |
|
| 774 | + | }; |
|
| 775 | + | if not try domains::serve(&mut DOMAINS, permit.object, domains::RingRequest::Send(event)) { |
|
| 776 | + | throw abi::Error::Busy; |
|
| 777 | + | } |
|
| 778 | + | } |
|
| 779 | + | ||
| 780 | + | /// Activation validates child authority before publishing its initial state, and consumes |
|
| 781 | + | /// the stack Page only when nothing else holds it. |
|
| 782 | + | @test unsafe fn activation() throws (testing::TestError) { |
|
| 783 | + | initialize(abi::CREATE); |
|
| 784 | + | slots::initialize(&mut PAGES.slots[..]); |
|
| 785 | + | let handle = try! domains::create( |
|
| 786 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 787 | + | ); |
|
| 788 | + | let entry = try! capability::get(&TABLE, handle); |
|
| 789 | + | let mut child = try! domains::get(&DOMAINS, entry.object); |
|
| 790 | + | set PAGES.backings = BACKINGS; |
|
| 791 | + | let stack = childPage(&child, entry.object, 1); |
|
| 792 | + | let args = childPage(&child, entry.object, 1); |
|
| 793 | + | let stackPage = pageOf(&child, stack); |
|
| 794 | + | let argsPage = pageOf(&child, args); |
|
| 795 | + | let mut failures: u32 = 0; |
|
| 796 | + | // A missing or wrong handle, and arguments that the Page cannot hold. |
|
| 797 | + | try activate(handle, abi::Handle(0), abi::Handle(0), 0) catch err { |
|
| 798 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 799 | + | set failures += 1; |
|
| 800 | + | }; |
|
| 801 | + | try activate(handle, stack, abi::Handle(0), 8) catch err { |
|
| 802 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 803 | + | set failures += 1; |
|
| 804 | + | }; |
|
| 805 | + | try activate(handle, stack, args, 4097) catch err { |
|
| 806 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 807 | + | set failures += 1; |
|
| 808 | + | }; |
|
| 809 | + | try activate(handle, stack, abi::Handle(0x7777), 0) catch err { |
|
| 810 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 811 | + | set failures += 1; |
|
| 812 | + | }; |
|
| 813 | + | // The stack is consumed, so arguments cannot name the same handle. |
|
| 814 | + | try activate(handle, stack, stack, 0) catch err { |
|
| 815 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 816 | + | set failures += 1; |
|
| 817 | + | }; |
|
| 818 | + | // A stack must be writable. |
|
| 819 | + | let slot = try! abi::decode(stack); |
|
| 820 | + | set child.memory.table.entries[slot.object.index].rights = abi::Rights(abi::READ); |
|
| 821 | + | try activate(handle, stack, args, 0) catch err { |
|
| 822 | + | try testing::expect(err == abi::Error::Denied); |
|
| 823 | + | set failures += 1; |
|
| 824 | + | }; |
|
| 825 | + | set child.memory.table.entries[slot.object.index].rights = |
|
| 826 | + | abi::Rights(abi::READ | abi::WRITE | abi::GRANT); |
|
| 827 | + | // Another handle to the memory is a reason to refuse it, a grant as much as a sibling. |
|
| 828 | + | let second = try! pages::selfGrant( |
|
| 829 | + | &mut PAGES, &mut child.memory.table, stack, (abi::READ | abi::WRITE) as u64 |
|
| 830 | + | ); |
|
| 831 | + | try activate(handle, stack, args, 0) catch err { |
|
| 832 | + | try testing::expect(err == abi::Error::Busy); |
|
| 833 | + | set failures += 1; |
|
| 834 | + | }; |
|
| 835 | + | try! pages::drop(&mut PAGES, &mut child.memory.table, second); |
|
| 836 | + | // Memory that code has reached as a slice cannot be a stack. |
|
| 837 | + | try! backing::markAccessed(&mut PAGES.backings, stackPage.backing); |
|
| 838 | + | try activate(handle, stack, args, 0) catch err { |
|
| 839 | + | try testing::expect(err == abi::Error::Denied); |
|
| 840 | + | set failures += 1; |
|
| 841 | + | }; |
|
| 842 | + | try testing::expect( |
|
| 843 | + | failures == 8 |
|
| 844 | + | and DOMAINS.records[entry.object.index].state == domains::Lifecycle::Pending |
|
| 845 | + | ); |
|
| 846 | + | try testing::expect(DOMAINS.contexts[child.initial.index].frame.registers[2] == 0); |
|
| 847 | + | // A stack that was never reached works, and the handle is consumed. |
|
| 848 | + | let fresh = childPage(&child, entry.object, 2); |
|
| 849 | + | let freshPage = pageOf(&child, fresh); |
|
| 850 | + | try! activate(handle, fresh, args, 4096); |
|
| 851 | + | try testing::expect(DOMAINS.records[entry.object.index].state == domains::Lifecycle::Active); |
|
| 852 | + | let frame = DOMAINS.contexts[child.initial.index].frame; |
|
| 853 | + | try testing::expect( |
|
| 854 | + | frame.registers[2] == freshPage.base + 8192 |
|
| 855 | + | and frame.registers[4] == freshPage.base |
|
| 856 | + | ); |
|
| 857 | + | try testing::expect(frame.registers[10] == &child.memory.env as u64); |
|
| 858 | + | try testing::expect(child.memory.env.args == *args and child.memory.env.argsSize == 4096); |
|
| 859 | + | try testing::expect(child.memory.env.eventsHandle == *child.events); |
|
| 860 | + | try testing::expect(DOMAINS.contexts[child.initial.index].userStack.start == freshPage.base); |
|
| 861 | + | try testing::expect( |
|
| 862 | + | DOMAINS.contexts[child.initial.index].userStack.end == freshPage.base |
|
| 863 | + | + 8192 |
|
| 864 | + | ); |
|
| 865 | + | try capability::get(&child.memory.table, fresh) catch err { |
|
| 866 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 867 | + | set failures += 1; |
|
| 868 | + | }; |
|
| 869 | + | try testing::expect(failures == 9); |
|
| 870 | + | try activate(handle, stack, args, 0) catch err { |
|
| 871 | + | try testing::expect(err == abi::Error::NotPending); |
|
| 872 | + | set failures += 1; |
|
| 873 | + | }; |
|
| 874 | + | try testing::expect(failures == 10); |
|
| 875 | + | } |
|
| 876 | + | ||
| 877 | + | /// A Page that was split has siblings, so neither half can be a stack. |
|
| 878 | + | @test unsafe fn splitStack() throws (testing::TestError) { |
|
| 879 | + | initialize(abi::CREATE); |
|
| 880 | + | slots::initialize(&mut PAGES.slots[..]); |
|
| 881 | + | let handle = try! domains::create( |
|
| 882 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 883 | + | ); |
|
| 884 | + | let entry = try! capability::get(&TABLE, handle); |
|
| 885 | + | let child = try! domains::get(&DOMAINS, entry.object); |
|
| 886 | + | set PAGES.backings = BACKINGS; |
|
| 887 | + | let left = childPage(&child, entry.object, 2); |
|
| 888 | + | let right = try! pages::split(&mut PAGES, &mut child.memory.table, left, 1); |
|
| 889 | + | let mut busy: u32 = 0; |
|
| 890 | + | try activate(handle, left, abi::Handle(0), 0) catch err { |
|
| 891 | + | try testing::expect(err == abi::Error::Busy); |
|
| 892 | + | set busy += 1; |
|
| 893 | + | }; |
|
| 894 | + | try activate(handle, right, abi::Handle(0), 0) catch err { |
|
| 895 | + | try testing::expect(err == abi::Error::Busy); |
|
| 896 | + | set busy += 1; |
|
| 897 | + | }; |
|
| 898 | + | try testing::expect( |
|
| 899 | + | busy == 2 |
|
| 900 | + | and DOMAINS.records[entry.object.index].state == domains::Lifecycle::Pending |
|
| 901 | + | ); |
|
| 902 | + | // Dropping one half leaves the other with a sibling no more. |
|
| 903 | + | try! pages::drop(&mut PAGES, &mut child.memory.table, left); |
|
| 904 | + | try! activate(handle, right, abi::Handle(0), 0); |
|
| 905 | + | try testing::expect(DOMAINS.records[entry.object.index].state == domains::Lifecycle::Active); |
|
| 906 | + | } |
|
| 907 | + | ||
| 908 | + | /// Additional contexts share private package state and reserve distinct kernel stacks. |
|
| 909 | + | @test unsafe fn additionalContext() throws (testing::TestError) { |
|
| 910 | + | initialize(abi::CREATE); |
|
| 911 | + | slots::initialize(&mut PAGES.slots[..]); |
|
| 912 | + | let handle = try! domains::create( |
|
| 913 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 914 | + | ); |
|
| 915 | + | let authority = try! capability::get(&TABLE, handle); |
|
| 916 | + | let mut child = try! domains::get(&DOMAINS, authority.object); |
|
| 917 | + | // Move the fixture's physical allocation state into its Page store. |
|
| 918 | + | set PAGES.backings = BACKINGS; |
|
| 919 | + | let first = childPage(&child, authority.object, 1); |
|
| 920 | + | let second = childPage(&child, authority.object, 2); |
|
| 921 | + | let third = childPage(&child, authority.object, 1); |
|
| 922 | + | let fourth = childPage(&child, authority.object, 1); |
|
| 923 | + | let secondPage = pageOf(&child, second); |
|
| 924 | + | let start = abi::ContextStart { entry: 0x80000004, stack: *first, args: 7, size: 16 }; |
|
| 925 | + | let later = abi::ContextStart { entry: 0x80000004, stack: *second, args: 7, size: 16 }; |
|
| 926 | + | let count = PAGES.backings.pool.count; |
|
| 927 | + | set PAGES.backings.pool.count = 0; |
|
| 928 | + | let mut exhausted = false; |
|
| 929 | + | try contextCreate(handle, start) catch error { |
|
| 930 | + | try testing::expect(error == abi::Error::OutOfMemory); |
|
| 931 | + | set exhausted = true; |
|
| 932 | + | }; |
|
| 933 | + | try testing::expect( |
|
| 934 | + | exhausted |
|
| 935 | + | and DOMAINS.contextSlots[child.initial.index + 1].state == slots::State::Free |
|
| 936 | + | ); |
|
| 937 | + | // A failed reservation does not consume the stack. |
|
| 938 | + | try! capability::get(&child.memory.table, first); |
|
| 939 | + | set PAGES.backings.pool.count = count; |
|
| 940 | + | let reserved = try! contextReserve(handle, start); |
|
| 941 | + | let index = child.initial.index + 1; |
|
| 942 | + | assert DOMAINS.contextSlots[index].state == slots::State::Reserved; |
|
| 943 | + | // The reservation consumed the stack handle. |
|
| 944 | + | let mut consumed = false; |
|
| 945 | + | try contextCreate(handle, start) catch error { |
|
| 946 | + | assert error == abi::Error::BadHandle; |
|
| 947 | + | set consumed = true; |
|
| 948 | + | }; |
|
| 949 | + | assert consumed; |
|
| 950 | + | let reservedFrames = DOMAINS.contexts[index].kernelFrames; |
|
| 951 | + | domains::contextCancel(&mut DOMAINS, &mut PAGES, reserved); |
|
| 952 | + | assert DOMAINS.contextSlots[index].state == slots::State::Free; |
|
| 953 | + | for i in reservedFrames.first..reservedFrames.first + reservedFrames.count { |
|
| 954 | + | assert frames::available(&PAGES.backings.pool, i); |
|
| 955 | + | } |
|
| 956 | + | let pendingContext = try! contextReserve(handle, later); |
|
| 957 | + | let stackBase = DOMAINS.contexts[index].kernelStack.start; |
|
| 958 | + | let base = &RAM[0] as u64; |
|
| 959 | + | let firstWord = ((stackBase - base) / 8) as u32; |
|
| 960 | + | let lastWord = firstWord + domains::KERNEL_STACK_PAGES * 512 - 1; |
|
| 961 | + | set RAM[firstWord] = 123; |
|
| 962 | + | set RAM[lastWord] = 456; |
|
| 963 | + | lifecycle::initialize(&mut CALLS); |
|
| 964 | + | let mut context: abi::Ref = undefined; |
|
| 965 | + | match lifecycle::holdContext(&mut CALLS, &mut DOMAINS, child.initial, pendingContext) { |
|
| 966 | + | case lifecycle::ContextHeld::Held(lease, access) => { |
|
| 967 | + | assert DOMAINS.contexts[child.initial.index].reservation; |
|
| 968 | + | assert lifecycle::contextTarget(&CALLS, &lease) == authority.object; |
|
| 969 | + | physical::clear(&access); |
|
| 970 | + | assert RAM[firstWord] == 0 and RAM[lastWord] == 0; |
|
| 971 | + | let ready = lifecycle::takeContext(&mut CALLS, &mut DOMAINS, lease, access); |
|
| 972 | + | assert not DOMAINS.contexts[child.initial.index].reservation; |
|
| 973 | + | set context = domains::contextPublish(&mut DOMAINS, ready); |
|
| 974 | + | } |
|
| 975 | + | } |
|
| 976 | + | let created = try! domains::context(&DOMAINS, authority.object, context); |
|
| 977 | + | let initial = DOMAINS.contexts[child.initial.index]; |
|
| 978 | + | try testing::expect(context <> child.initial and created.owner == authority.object); |
|
| 979 | + | try testing::expect(created.frame.registers[3] == initial.frame.registers[3]); |
|
| 980 | + | try testing::expect( |
|
| 981 | + | created.kernelStack.start >= initial.kernelStack.end |
|
| 982 | + | or created.kernelStack.end <= initial.kernelStack.start |
|
| 983 | + | ); |
|
| 984 | + | try testing::expect(created.frame.pc == later.entry); |
|
| 985 | + | try testing::expect( |
|
| 986 | + | created.frame.registers[2] == secondPage.base + 8192 |
|
| 987 | + | and created.frame.registers[4] == secondPage.base |
|
| 988 | + | ); |
|
| 989 | + | try testing::expect( |
|
| 990 | + | created.frame.registers[10] == later.args |
|
| 991 | + | and created.frame.registers[11] == later.size |
|
| 992 | + | ); |
|
| 993 | + | try testing::expect( |
|
| 994 | + | created.userStack.start == secondPage.base |
|
| 995 | + | and created.userStack.end == secondPage.base + 8192 |
|
| 996 | + | ); |
|
| 997 | + | let mut invalid = start; |
|
| 998 | + | set invalid.entry += 4; |
|
| 999 | + | let mut rejected: u32 = 0; |
|
| 1000 | + | try contextCreate(handle, invalid) catch error { |
|
| 1001 | + | try testing::expect(error == abi::Error::InvalidArg); |
|
| 1002 | + | set rejected += 1; |
|
| 1003 | + | }; |
|
| 1004 | + | // The package entry takes a pointer, so it cannot start a context, exported or not. |
|
| 1005 | + | set invalid = start; |
|
| 1006 | + | set invalid.entry = 0x80000000; |
|
| 1007 | + | try contextCreate(handle, invalid) catch error { |
|
| 1008 | + | try testing::expect(error == abi::Error::InvalidArg); |
|
| 1009 | + | set rejected += 1; |
|
| 1010 | + | }; |
|
| 1011 | + | set invalid = start; |
|
| 1012 | + | set invalid.stack = 0x7777; |
|
| 1013 | + | try contextCreate(handle, invalid) catch error { |
|
| 1014 | + | try testing::expect(error == abi::Error::BadHandle); |
|
| 1015 | + | set rejected += 1; |
|
| 1016 | + | }; |
|
| 1017 | + | // The arguments are data and are never checked, whatever they are. |
|
| 1018 | + | set invalid = start; |
|
| 1019 | + | set invalid.stack = *third; |
|
| 1020 | + | set invalid.args = 0xffffffffffffffff; |
|
| 1021 | + | set invalid.size = 0xffffffffffffffff; |
|
| 1022 | + | let wide = try! contextCreate(handle, invalid); |
|
| 1023 | + | try testing::expect(DOMAINS.contexts[wide.index].frame.registers[10] == 0xffffffffffffffff); |
|
| 1024 | + | try testing::expect( |
|
| 1025 | + | rejected == 3 |
|
| 1026 | + | and DOMAINS.contextSlots[context.index + 1].state == slots::State::Live |
|
| 1027 | + | ); |
|
| 1028 | + | try! domains::returned(&mut DOMAINS, authority.object, wide); |
|
| 1029 | + | try! domains::contextDestroy(&mut DOMAINS, &mut PAGES, &TABLE, handle, wide); |
|
| 1030 | + | try testing::expect( |
|
| 1031 | + | DOMAINS.records[authority.object.index].state |
|
| 1032 | + | == domains::Lifecycle::Pending |
|
| 1033 | + | ); |
|
| 1034 | + | set rejected += 3; |
|
| 1035 | + | let otherEntry = authority; |
|
| 1036 | + | try domains::context(&DOMAINS, otherEntry.object, abi::Ref { |
|
| 1037 | + | index: context.index, generation: context.generation + 1 |
|
| 1038 | + | }) catch error { |
|
| 1039 | + | try testing::expect(error == abi::Error::BadHandle); |
|
| 1040 | + | set rejected += 1; |
|
| 1041 | + | }; |
|
| 1042 | + | try domains::context(&DOMAINS, authority.object, abi::Ref { |
|
| 1043 | + | index: context.index, generation: context.generation + 1 |
|
| 1044 | + | }) catch error { |
|
| 1045 | + | try testing::expect(error == abi::Error::BadHandle); |
|
| 1046 | + | set rejected += 1; |
|
| 1047 | + | }; |
|
| 1048 | + | try testing::expect(rejected == 8); |
|
| 1049 | + | try domains::contextDestroy( |
|
| 1050 | + | &mut DOMAINS, &mut PAGES, &TABLE, handle, child.initial |
|
| 1051 | + | ) catch error { |
|
| 1052 | + | try testing::expect(error == abi::Error::Denied); |
|
| 1053 | + | set rejected += 1; |
|
| 1054 | + | }; |
|
| 1055 | + | set DOMAINS.contexts[context.index].hart = 0; |
|
| 1056 | + | try domains::contextDestroy(&mut DOMAINS, &mut PAGES, &TABLE, handle, context) catch error { |
|
| 1057 | + | try testing::expect(error == abi::Error::Busy); |
|
| 1058 | + | set rejected += 1; |
|
| 1059 | + | }; |
|
| 1060 | + | try! domains::returned(&mut DOMAINS, authority.object, context); |
|
| 1061 | + | try testing::expect(DOMAINS.contexts[context.index].state == domains::ContextState::Stopped); |
|
| 1062 | + | try testing::expect( |
|
| 1063 | + | DOMAINS.records[authority.object.index].state |
|
| 1064 | + | == domains::Lifecycle::Pending |
|
| 1065 | + | ); |
|
| 1066 | + | set DOMAINS.contexts[context.index].hart = nil; |
|
| 1067 | + | set DOMAINS.contexts[context.index].pending = abi::Ref { index: 0, generation: 1 }; |
|
| 1068 | + | let mut retained = false; |
|
| 1069 | + | try domains::contextDestroy(&mut DOMAINS, &mut PAGES, &TABLE, handle, context) catch error { |
|
| 1070 | + | assert error == abi::Error::Busy; |
|
| 1071 | + | set retained = true; |
|
| 1072 | + | }; |
|
| 1073 | + | assert retained and DOMAINS.contextSlots[context.index].state == slots::State::Live; |
|
| 1074 | + | for i in created.kernelFrames.first..created.kernelFrames.first + created.kernelFrames.count { |
|
| 1075 | + | assert not frames::available(&PAGES.backings.pool, i); |
|
| 1076 | + | } |
|
| 1077 | + | set DOMAINS.contexts[context.index].pending = nil; |
|
| 1078 | + | set DOMAINS.contexts[context.index].reservation = true; |
|
| 1079 | + | set retained = false; |
|
| 1080 | + | try domains::contextDestroy(&mut DOMAINS, &mut PAGES, &TABLE, handle, context) catch error { |
|
| 1081 | + | assert error == abi::Error::Busy; |
|
| 1082 | + | set retained = true; |
|
| 1083 | + | }; |
|
| 1084 | + | assert retained and DOMAINS.contextSlots[context.index].state == slots::State::Live; |
|
| 1085 | + | set DOMAINS.contexts[context.index].reservation = false; |
|
| 1086 | + | try! domains::contextDestroy(&mut DOMAINS, &mut PAGES, &TABLE, handle, context); |
|
| 1087 | + | for i in created.kernelFrames.first..created.kernelFrames.first + created.kernelFrames.count { |
|
| 1088 | + | assert frames::available(&PAGES.backings.pool, i); |
|
| 1089 | + | } |
|
| 1090 | + | try domains::context(&DOMAINS, authority.object, context) catch error { |
|
| 1091 | + | try testing::expect(error == abi::Error::BadHandle); |
|
| 1092 | + | set rejected += 1; |
|
| 1093 | + | }; |
|
| 1094 | + | let final = abi::ContextStart { entry: 0x80000004, stack: *fourth, args: 0, size: 0 }; |
|
| 1095 | + | let next = try! contextCreate(handle, final); |
|
| 1096 | + | try testing::expect(next.index == context.index and next.generation <> context.generation); |
|
| 1097 | + | try! domains::returned(&mut DOMAINS, authority.object, child.initial); |
|
| 1098 | + | try testing::expect(DOMAINS.records[authority.object.index].state == domains::Lifecycle::Dead); |
|
| 1099 | + | try testing::expect(DOMAINS.contexts[next.index].state == domains::ContextState::Stopped); |
|
| 1100 | + | try domains::contextDestroy(&mut DOMAINS, &mut PAGES, &TABLE, handle, next) catch error { |
|
| 1101 | + | assert error == abi::Error::BadHandle; |
|
| 1102 | + | set rejected += 1; |
|
| 1103 | + | }; |
|
| 1104 | + | assert DOMAINS.contextSlots[next.index].state == slots::State::Live; |
|
| 1105 | + | assert rejected == 12; |
|
| 1106 | + | } |
|
| 1107 | + | ||
| 1108 | + | /// Both serialized wait/wakeup orders preserve notification and runnable state. |
|
| 1109 | + | @test unsafe fn waitWakeup() throws (testing::TestError) { |
|
| 1110 | + | initialize(abi::CREATE); |
|
| 1111 | + | let handle = try! domains::create( |
|
| 1112 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1113 | + | ); |
|
| 1114 | + | let authority = try! capability::get(&TABLE, handle); |
|
| 1115 | + | let mut child = try! domains::get(&DOMAINS, authority.object); |
|
| 1116 | + | set DOMAINS.records[authority.object.index].state = domains::Lifecycle::Active; |
|
| 1117 | + | try! wait(authority.object, child.initial); |
|
| 1118 | + | assert DOMAINS.contexts[child.initial.index].state == domains::ContextState::Waiting; |
|
| 1119 | + | try! wakeup(handle, 17); |
|
| 1120 | + | assert DOMAINS.contexts[child.initial.index].state == domains::ContextState::Ready; |
|
| 1121 | + | try! wait(authority.object, child.initial); |
|
| 1122 | + | assert DOMAINS.contexts[child.initial.index].state == domains::ContextState::Ready; |
|
| 1123 | + | let event = try! events::pop(&mut child.memory.ring) else panic "waitWakeup: wake event"; |
|
| 1124 | + | assert event.kind == events::WAKEUP |
|
| 1125 | + | and event.code == 17 |
|
| 1126 | + | and event.value == abi::id(TABLE.owner); |
|
| 1127 | + | try! wakeup(handle, 23); |
|
| 1128 | + | try! wait(authority.object, child.initial); |
|
| 1129 | + | assert DOMAINS.contexts[child.initial.index].state == domains::ContextState::Ready; |
|
| 1130 | + | let second = try! events::pop(&mut child.memory.ring) |
|
| 1131 | + | else panic "waitWakeup: second wake event"; |
|
| 1132 | + | assert second.code == 23; |
|
| 1133 | + | for i in 0..events::ORDINARY { |
|
| 1134 | + | try! wakeup(handle, i); |
|
| 1135 | + | } |
|
| 1136 | + | let tail = child.memory.ring.tail; |
|
| 1137 | + | let mut rejected: u32 = 0; |
|
| 1138 | + | try wakeup(handle, 999) catch error { |
|
| 1139 | + | assert error == abi::Error::Busy; |
|
| 1140 | + | set rejected += 1; |
|
| 1141 | + | }; |
|
| 1142 | + | assert child.memory.ring.tail == tail; |
|
| 1143 | + | try wakeup(abi::Handle(0), 0) catch error { |
|
| 1144 | + | assert error == abi::Error::BadHandle; |
|
| 1145 | + | set rejected += 1; |
|
| 1146 | + | }; |
|
| 1147 | + | set DOMAINS.records[authority.object.index].state = domains::Lifecycle::Dead; |
|
| 1148 | + | try wakeup(handle, 0) catch error { |
|
| 1149 | + | assert error == abi::Error::BadHandle; |
|
| 1150 | + | set rejected += 1; |
|
| 1151 | + | }; |
|
| 1152 | + | assert rejected == 3; |
|
| 1153 | + | } |
|
| 1154 | + | ||
| 1155 | + | /// Publish a live second context for `owner` in the Ready state. |
|
| 1156 | + | unsafe fn secondContext(owner: abi::Ref) -> abi::Ref { |
|
| 1157 | + | let object = abi::Ref { index: 1, generation: 1 }; |
|
| 1158 | + | assert DOMAINS.contextSlots[object.index].state <> slots::State::Live; |
|
| 1159 | + | set DOMAINS.contextSlots[object.index] = slots::Slot { |
|
| 1160 | + | generation: 1, state: slots::State::Live |
|
| 1161 | + | }; |
|
| 1162 | + | set DOMAINS.contexts[object.index].owner = owner; |
|
| 1163 | + | set DOMAINS.contexts[object.index].state = domains::ContextState::Ready; |
|
| 1164 | + | set DOMAINS.contexts[object.index].hart = nil; |
|
| 1165 | + | set DOMAINS.contexts[object.index].pending = nil; |
|
| 1166 | + | return object; |
|
| 1167 | + | } |
|
| 1168 | + | ||
| 1169 | + | /// Retain a pending terminal event for `subject` to be published to `receiver`. |
|
| 1170 | + | unsafe fn retainTerminal(subject: abi::Ref, receiver: abi::Ref) { |
|
| 1171 | + | let pending = try! events::reserve(&mut DOMAINS.events, subject); |
|
| 1172 | + | events::finish(&mut DOMAINS.events, pending, receiver, events::CHILD_EXIT, 0); |
|
| 1173 | + | } |
|
| 1174 | + | ||
| 1175 | + | /// A Wait that publishes a pending terminal event readies the receiver's other waiting contexts. |
|
| 1176 | + | @test unsafe fn waitTerminalReadiesWaiters() throws (testing::TestError) { |
|
| 1177 | + | initialize(abi::CREATE); |
|
| 1178 | + | let handle = try! domains::create( |
|
| 1179 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1180 | + | ); |
|
| 1181 | + | let authority = try! capability::get(&TABLE, handle); |
|
| 1182 | + | let child = try! domains::get(&DOMAINS, authority.object); |
|
| 1183 | + | set DOMAINS.records[authority.object.index].state = domains::Lifecycle::Active; |
|
| 1184 | + | let other = secondContext(authority.object); |
|
| 1185 | + | try! wait(authority.object, child.initial); |
|
| 1186 | + | assert DOMAINS.contexts[child.initial.index].state == domains::ContextState::Waiting; |
|
| 1187 | + | retainTerminal(abi::Ref { index: 7, generation: 1 }, authority.object); |
|
| 1188 | + | assert try! wait(authority.object, other); |
|
| 1189 | + | assert child.memory.ring.tail == 1 and child.memory.ring.head == 0; |
|
| 1190 | + | assert DOMAINS.contexts[other.index].state == domains::ContextState::Ready; |
|
| 1191 | + | assert DOMAINS.contexts[child.initial.index].state == domains::ContextState::Ready; |
|
| 1192 | + | } |
|
| 1193 | + | ||
| 1194 | + | /// A Wait whose flush publishes nothing leaves other waiters suspended, and the caller waits on an |
|
| 1195 | + | /// empty ring. |
|
| 1196 | + | @test unsafe fn waitWithoutEventKeepsWaiters() throws (testing::TestError) { |
|
| 1197 | + | initialize(abi::CREATE); |
|
| 1198 | + | let handle = try! domains::create( |
|
| 1199 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1200 | + | ); |
|
| 1201 | + | let authority = try! capability::get(&TABLE, handle); |
|
| 1202 | + | let child = try! domains::get(&DOMAINS, authority.object); |
|
| 1203 | + | set DOMAINS.records[authority.object.index].state = domains::Lifecycle::Active; |
|
| 1204 | + | let other = secondContext(authority.object); |
|
| 1205 | + | try! wait(authority.object, child.initial); |
|
| 1206 | + | // A terminal event for another receiver is not published to this ring. |
|
| 1207 | + | retainTerminal(abi::Ref { index: 7, generation: 1 }, abi::Ref { index: 9, generation: 1 }); |
|
| 1208 | + | assert not try! wait(authority.object, other); |
|
| 1209 | + | assert child.memory.ring.tail == 0; |
|
| 1210 | + | assert DOMAINS.contexts[other.index].state == domains::ContextState::Waiting; |
|
| 1211 | + | assert DOMAINS.contexts[child.initial.index].state == domains::ContextState::Waiting; |
|
| 1212 | + | } |
|
| 1213 | + | ||
| 1214 | + | /// Service paths reach a live receiver's ring, and `peer` locates only distinct live domains. |
|
| 1215 | + | @test unsafe fn ringService() throws (testing::TestError) { |
|
| 1216 | + | initialize(abi::CREATE); |
|
| 1217 | + | let handle = try! domains::create( |
|
| 1218 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1219 | + | ); |
|
| 1220 | + | let authority = try! capability::get(&TABLE, handle); |
|
| 1221 | + | let child = try! domains::get(&DOMAINS, authority.object); |
|
| 1222 | + | set DOMAINS.records[authority.object.index].state = domains::Lifecycle::Active; |
|
| 1223 | + | try! wait(authority.object, child.initial); |
|
| 1224 | + | assert DOMAINS.contexts[child.initial.index].state == domains::ContextState::Waiting; |
|
| 1225 | + | let event = events::Event { kind: events::WAKEUP, reserved: 0, code: 5, value: 0 }; |
|
| 1226 | + | assert try! domains::serve(&mut DOMAINS, authority.object, domains::RingRequest::Send(event)); |
|
| 1227 | + | assert DOMAINS.contexts[child.initial.index].state == domains::ContextState::Ready; |
|
| 1228 | + | let ring = &mut child.memory.ring; |
|
| 1229 | + | assert try! domains::notify(&mut DOMAINS, authority.object, ring, event); |
|
| 1230 | + | assert try! domains::serve(&mut DOMAINS, authority.object, domains::RingRequest::Refresh); |
|
| 1231 | + | let first = try! events::pop(&mut child.memory.ring) else panic "ringService: first event"; |
|
| 1232 | + | let second = try! events::pop(&mut child.memory.ring) else panic "ringService: second event"; |
|
| 1233 | + | assert first.code == 5 and second.code == 5; |
|
| 1234 | + | let located = try! domains::peer(&DOMAINS, TABLE.owner, authority.object); |
|
| 1235 | + | assert located as u64 == child.memory as u64; |
|
| 1236 | + | let stale = abi::Ref { |
|
| 1237 | + | index: authority.object.index, generation: authority.object.generation + 1 |
|
| 1238 | + | }; |
|
| 1239 | + | let mut rejected: u32 = 0; |
|
| 1240 | + | try domains::serve(&mut DOMAINS, stale, domains::RingRequest::Refresh) catch error { |
|
| 1241 | + | assert error == abi::Error::BadHandle; |
|
| 1242 | + | set rejected += 1; |
|
| 1243 | + | }; |
|
| 1244 | + | try domains::peer(&DOMAINS, TABLE.owner, stale) catch error { |
|
| 1245 | + | assert error == abi::Error::BadHandle; |
|
| 1246 | + | set rejected += 1; |
|
| 1247 | + | }; |
|
| 1248 | + | set DOMAINS.records[authority.object.index].state = domains::Lifecycle::Dead; |
|
| 1249 | + | try domains::notify(&mut DOMAINS, authority.object, ring, event) catch error { |
|
| 1250 | + | assert error == abi::Error::BadHandle; |
|
| 1251 | + | set rejected += 1; |
|
| 1252 | + | }; |
|
| 1253 | + | try domains::serve(&mut DOMAINS, authority.object, domains::RingRequest::Send( |
|
| 1254 | + | event |
|
| 1255 | + | )) catch error { |
|
| 1256 | + | assert error == abi::Error::BadHandle; |
|
| 1257 | + | set rejected += 1; |
|
| 1258 | + | }; |
|
| 1259 | + | assert rejected == 4; |
|
| 1260 | + | } |
|
| 1261 | + | ||
| 1262 | + | /// Domain reservations retain private capacity across another complete creation. |
|
| 1263 | + | @test unsafe fn stagedCreation() throws (testing::TestError) { |
|
| 1264 | + | initialize(abi::CREATE); |
|
| 1265 | + | let pending = try! domains::reserve( |
|
| 1266 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1267 | + | ); |
|
| 1268 | + | assert DOMAINS.slots[1].state == slots::State::Reserved; |
|
| 1269 | + | assert DOMAINS.contextSlots[0].state == slots::State::Reserved; |
|
| 1270 | + | assert TABLE.slots[2].state == slots::State::Reserved; |
|
| 1271 | + | assert BACKINGS.domains[1] == 0 and DOMAINS.events.queues[1].generation == 0; |
|
| 1272 | + | let other = try! domains::create( |
|
| 1273 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1274 | + | ); |
|
| 1275 | + | try! domains::prepare(®istry::capture(&PACKAGES), &pending); |
|
| 1276 | + | let first = try! domains::publish(&mut DOMAINS, &mut BACKINGS, &mut TABLE, pending); |
|
| 1277 | + | let a = try! capability::get(&TABLE, first); |
|
| 1278 | + | let b = try! capability::get(&TABLE, other); |
|
| 1279 | + | let left = try! domains::get(&DOMAINS, a.object); |
|
| 1280 | + | let right = try! domains::get(&DOMAINS, b.object); |
|
| 1281 | + | assert a.object.index == 1 and b.object.index == 2; |
|
| 1282 | + | assert instances::base( |
|
| 1283 | + | &DOMAINS.tables[a.object.index], 0 |
|
| 1284 | + | ) <> instances::base(&DOMAINS.tables[b.object.index], 0); |
|
| 1285 | + | assert left.state == domains::Lifecycle::Pending and right.state == domains::Lifecycle::Pending; |
|
| 1286 | + | initialize(abi::CREATE); |
|
| 1287 | + | let cancelled = try! domains::reserve( |
|
| 1288 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1289 | + | ); |
|
| 1290 | + | domains::cancel(&mut DOMAINS, &mut BACKINGS, &mut TABLE, cancelled); |
|
| 1291 | + | assert DOMAINS.slots[1].state == slots::State::Free; |
|
| 1292 | + | assert DOMAINS.contextSlots[0].state == slots::State::Free; |
|
| 1293 | + | assert TABLE.slots[2].state == slots::State::Free; |
|
| 1294 | + | for i in 0..BACKINGS.pool.count { |
|
| 1295 | + | assert frames::available(&BACKINGS.pool, i); |
|
| 1296 | + | } |
|
| 1297 | + | set PACKAGES.entries[0].package.relocations = &[ |
|
| 1298 | + | shared::Relocation { |
|
| 1299 | + | offset: 0, count: 1, target: shared::DataRef { slot: 5, offset: 0 } |
|
| 1300 | + | }, |
|
| 1301 | + | ]; |
|
| 1302 | + | let mut invalid = false; |
|
| 1303 | + | try domains::create( |
|
| 1304 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1305 | + | ) catch error { |
|
| 1306 | + | assert error == abi::Error::VerifyFailed; |
|
| 1307 | + | set invalid = true; |
|
| 1308 | + | }; |
|
| 1309 | + | assert invalid and DOMAINS.slots[1].state == slots::State::Free; |
|
| 1310 | + | for i in 0..BACKINGS.pool.count { |
|
| 1311 | + | assert frames::available(&BACKINGS.pool, i); |
|
| 1312 | + | } |
|
| 1313 | + | initialize(abi::CREATE); |
|
| 1314 | + | let abandoned = try! domains::reserve( |
|
| 1315 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1316 | + | ); |
|
| 1317 | + | try! domains::prepare(®istry::capture(&PACKAGES), &abandoned); |
|
| 1318 | + | try! backing::endDomain(&mut BACKINGS, TABLE.owner); |
|
| 1319 | + | let mut dead = false; |
|
| 1320 | + | try domains::publish(&mut DOMAINS, &mut BACKINGS, &mut TABLE, abandoned) catch error { |
|
| 1321 | + | assert error == abi::Error::BadHandle; |
|
| 1322 | + | set dead = true; |
|
| 1323 | + | }; |
|
| 1324 | + | assert dead and DOMAINS.slots[1].state == slots::State::Free; |
|
| 1325 | + | assert TABLE.slots[2].state == slots::State::Free; |
|
| 1326 | + | for i in 0..BACKINGS.pool.count { |
|
| 1327 | + | assert frames::available(&BACKINGS.pool, i); |
|
| 1328 | + | } |
|
| 1329 | + | } |
|
| 1330 | + | ||
| 1331 | + | /// Creation installs only Events in the child and preserves selected authorizing rights. |
|
| 1332 | + | @test unsafe fn pending() throws (testing::TestError) { |
|
| 1333 | + | initialize(abi::CREATE | abi::ALLOCATE); |
|
| 1334 | + | let handle = try! domains::create( |
|
| 1335 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1336 | + | ); |
|
| 1337 | + | let authority = try! capability::get(&TABLE, handle); |
|
| 1338 | + | try testing::expect( |
|
| 1339 | + | *authority.rights == (domains::MANAGEMENT_RIGHTS |
|
| 1340 | + | | abi::CREATE |
|
| 1341 | + | | abi::ALLOCATE) |
|
| 1342 | + | ); |
|
| 1343 | + | let child = try! domains::get(&DOMAINS, authority.object); |
|
| 1344 | + | try testing::expect(child.state == domains::Lifecycle::Pending); |
|
| 1345 | + | try testing::expect(child.creator == TABLE.owner and child.parent == TABLE.owner); |
|
| 1346 | + | try testing::expect(backing::domainLive(&BACKINGS, authority.object)); |
|
| 1347 | + | let events = try! capability::get(&child.memory.table, child.events); |
|
| 1348 | + | try testing::expect(events.kind == abi::Kind::Events and events.object == authority.object); |
|
| 1349 | + | try testing::expect(*events.rights == (abi::READ | abi::WRITE)); |
|
| 1350 | + | try testing::expect(child.memory.table.slots[1].state == slots::State::Free); |
|
| 1351 | + | let context = DOMAINS.contexts[child.initial.index]; |
|
| 1352 | + | try testing::expect(context.owner == authority.object and context.frame.pc == 0x80000000); |
|
| 1353 | + | try testing::expect( |
|
| 1354 | + | context.frame.registers[3] == instances::address( |
|
| 1355 | + | &DOMAINS.tables[authority.object.index] |
|
| 1356 | + | ) |
|
| 1357 | + | ); |
|
| 1358 | + | try testing::expect(context.frame.registers[2] == 0 and context.frame.status == 0x80); |
|
| 1359 | + | try testing::expect(context.kernelFrames.count == domains::KERNEL_STACK_PAGES); |
|
| 1360 | + | try testing::expect( |
|
| 1361 | + | context.kernelStack.end - context.kernelStack.start |
|
| 1362 | + | == domains::KERNEL_STACK_PAGES as u64 * 4096 |
|
| 1363 | + | ); |
|
| 1364 | + | try testing::expect((context.kernelStack.end & 15) == 0); |
|
| 1365 | + | try testing::expect(child.memory.ring.head == 0 and child.memory.ring.tail == 0); |
|
| 1366 | + | try testing::expect(child.memory.ring.mask == 255); |
|
| 1367 | + | } |
|
| 1368 | + | ||
| 1369 | + | /// Check that a failed creation has returned all unpublished resources. |
|
| 1370 | + | unsafe fn failed(expected: abi::Error) throws (testing::TestError) { |
|
| 1371 | + | let mut rejected = false; |
|
| 1372 | + | try domains::create( |
|
| 1373 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1374 | + | ) catch err { |
|
| 1375 | + | try testing::expect(err == expected); |
|
| 1376 | + | set rejected = true; |
|
| 1377 | + | }; |
|
| 1378 | + | try testing::expect(rejected); |
|
| 1379 | + | for i in 0..BACKINGS.pool.count { |
|
| 1380 | + | assert frames::available(&BACKINGS.pool, i); |
|
| 1381 | + | } |
|
| 1382 | + | try testing::expect(DOMAINS.slots[1].state == slots::State::Free); |
|
| 1383 | + | try testing::expect(DOMAINS.contextSlots[0].state == slots::State::Free); |
|
| 1384 | + | try testing::expect(TABLE.slots[2].state == slots::State::Free); |
|
| 1385 | + | try testing::expect(BACKINGS.domains[1] == 0); |
|
| 1386 | + | } |
|
| 1387 | + | ||
| 1388 | + | /// Failure at each memory allocation or event opening returns every reservation. |
|
| 1389 | + | @test unsafe fn rollback() throws (testing::TestError) { |
|
| 1390 | + | initialize(abi::CREATE); |
|
| 1391 | + | let count = BACKINGS.pool.count; |
|
| 1392 | + | set BACKINGS.pool.count = 0; |
|
| 1393 | + | try failed(abi::Error::OutOfMemory); |
|
| 1394 | + | set BACKINGS.pool.count = (@sizeOf(domains::Memory) + 4095) / 4096; |
|
| 1395 | + | try failed(abi::Error::OutOfMemory); |
|
| 1396 | + | set BACKINGS.pool.count += 1; |
|
| 1397 | + | try failed(abi::Error::OutOfMemory); |
|
| 1398 | + | set BACKINGS.pool.count = count; |
|
| 1399 | + | set DOMAINS.events.queues[1].generation = 99; |
|
| 1400 | + | try failed(abi::Error::Busy); |
|
| 1401 | + | try testing::expect(DOMAINS.events.queues[1].generation == 99); |
|
| 1402 | + | set DOMAINS.events.queues[1].generation = 0; |
|
| 1403 | + | let handle = try! domains::create( |
|
| 1404 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1405 | + | ); |
|
| 1406 | + | let entry = try! capability::get(&TABLE, handle); |
|
| 1407 | + | try testing::expect(*entry.rights == (domains::MANAGEMENT_RIGHTS | abi::CREATE)); |
|
| 1408 | + | } |
|
| 1409 | + | ||
| 1410 | + | /// Context exhaustion does not consume a domain slot, handle, or physical frame. |
|
| 1411 | + | @test unsafe fn contextCapacity() throws (testing::TestError) { |
|
| 1412 | + | initialize(abi::CREATE); |
|
| 1413 | + | for i in 0..DOMAINS.contextSlots.len { |
|
| 1414 | + | set DOMAINS.contextSlots[i].state = slots::State::Retired; |
|
| 1415 | + | } |
|
| 1416 | + | let mut rejected = false; |
|
| 1417 | + | try domains::create( |
|
| 1418 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1419 | + | ) catch err { |
|
| 1420 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 1421 | + | set rejected = true; |
|
| 1422 | + | }; |
|
| 1423 | + | try testing::expect(rejected and DOMAINS.slots[1].state == slots::State::Free); |
|
| 1424 | + | try testing::expect(TABLE.slots[2].state == slots::State::Free); |
|
| 1425 | + | for i in 0..BACKINGS.pool.count { |
|
| 1426 | + | assert frames::available(&BACKINGS.pool, i); |
|
| 1427 | + | } |
|
| 1428 | + | } |
|
| 1429 | + | ||
| 1430 | + | /// A creator can grant Create without Allocate and the next creation preserves that choice. |
|
| 1431 | + | @test unsafe fn selectiveAuthority() throws (testing::TestError) { |
|
| 1432 | + | initialize(abi::CREATE | abi::ALLOCATE); |
|
| 1433 | + | let handle = try! domains::create( |
|
| 1434 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1435 | + | ); |
|
| 1436 | + | let entry = try! capability::get(&TABLE, handle); |
|
| 1437 | + | let mut child = try! domains::get(&DOMAINS, entry.object); |
|
| 1438 | + | let owner = TABLE.owner; |
|
| 1439 | + | let grant = try! transactions::reserve( |
|
| 1440 | + | &mut child.memory.table, owner, handle, entry, abi::CREATE as u64, false |
|
| 1441 | + | ); |
|
| 1442 | + | let authority = transactions::grant(&mut child.memory.table, grant); |
|
| 1443 | + | let image = try! capability::get(&TABLE, IMAGE); |
|
| 1444 | + | let imageGrant = try! transactions::reserve( |
|
| 1445 | + | &mut child.memory.table, owner, IMAGE, image, abi::EXECUTE as u64, false |
|
| 1446 | + | ); |
|
| 1447 | + | let childImage = transactions::grant(&mut child.memory.table, imageGrant); |
|
| 1448 | + | let grandchild = try! domains::create( |
|
| 1449 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut child.memory.table, abi::Handle(0), childImage |
|
| 1450 | + | ); |
|
| 1451 | + | let returned = try! capability::get(&child.memory.table, grandchild); |
|
| 1452 | + | try testing::expect(*returned.rights == (domains::MANAGEMENT_RIGHTS | abi::CREATE)); |
|
| 1453 | + | let created = try! domains::get(&DOMAINS, returned.object); |
|
| 1454 | + | try testing::expect(created.creator == entry.object and created.parent == entry.object); |
|
| 1455 | + | let mut denied = false; |
|
| 1456 | + | try capability::authority(&child.memory.table, abi::Handle(0), abi::Rights( |
|
| 1457 | + | abi::ALLOCATE |
|
| 1458 | + | )) catch err { |
|
| 1459 | + | try testing::expect(err == abi::Error::Denied); |
|
| 1460 | + | set denied = true; |
|
| 1461 | + | }; |
|
| 1462 | + | try testing::expect(denied); |
|
| 1463 | + | } |
|
| 1464 | + | ||
| 1465 | + | /// Missing creation rights, stale authority, and non-executable images allocate nothing. |
|
| 1466 | + | @test unsafe fn authority() throws (testing::TestError) { |
|
| 1467 | + | initialize(abi::ALLOCATE); |
|
| 1468 | + | try failed(abi::Error::Denied); |
|
| 1469 | + | initialize(abi::CREATE); |
|
| 1470 | + | set BACKINGS.domains[0] = 0; |
|
| 1471 | + | try failed(abi::Error::BadHandle); |
|
| 1472 | + | initialize(abi::CREATE); |
|
| 1473 | + | set TABLE.entries[1].rights = abi::Rights(abi::READ); |
|
| 1474 | + | try failed(abi::Error::Denied); |
|
| 1475 | + | initialize(abi::CREATE); |
|
| 1476 | + | set PACKAGES.entries[0].package.entry = nil; |
|
| 1477 | + | try failed(abi::Error::VerifyFailed); |
|
| 1478 | + | } |
|
| 1479 | + | ||
| 1480 | + | /// Handle and domain table exhaustion preserve all physical capacity. |
|
| 1481 | + | @test unsafe fn capacity() throws (testing::TestError) { |
|
| 1482 | + | initialize(abi::CREATE); |
|
| 1483 | + | for i in 2..TABLE.slots.len { |
|
| 1484 | + | set TABLE.slots[i].state = slots::State::Retired; |
|
| 1485 | + | } |
|
| 1486 | + | let mut failures: u32 = 0; |
|
| 1487 | + | try domains::create( |
|
| 1488 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1489 | + | ) catch err { |
|
| 1490 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 1491 | + | set failures += 1; |
|
| 1492 | + | }; |
|
| 1493 | + | try testing::expect(DOMAINS.slots[1].state == slots::State::Free); |
|
| 1494 | + | for i in 0..BACKINGS.pool.count { |
|
| 1495 | + | assert frames::available(&BACKINGS.pool, i); |
|
| 1496 | + | } |
|
| 1497 | + | initialize(abi::CREATE); |
|
| 1498 | + | for i in 1..DOMAINS.slots.len { |
|
| 1499 | + | set DOMAINS.slots[i].state = slots::State::Retired; |
|
| 1500 | + | } |
|
| 1501 | + | try domains::create( |
|
| 1502 | + | &mut DOMAINS, &mut BACKINGS, &PACKAGES, &mut TABLE, abi::Handle(0), IMAGE |
|
| 1503 | + | ) catch err { |
|
| 1504 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 1505 | + | set failures += 1; |
|
| 1506 | + | }; |
|
| 1507 | + | try testing::expect(failures == 2 and TABLE.slots[2].state == slots::State::Free); |
|
| 1508 | + | try testing::expect(DOMAINS.contextSlots[0].state == slots::State::Free); |
|
| 1509 | + | for i in 0..BACKINGS.pool.count { |
|
| 1510 | + | assert frames::available(&BACKINGS.pool, i); |
|
| 1511 | + | } |
|
| 1512 | + | } |
kernel/kernel/tests/events.rad
added
+249 -0
| 1 | + | //! Event ring saturation, counter validation, and retained terminal identities. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::events; |
|
| 6 | + | use kernel::sync; |
|
| 7 | + | ||
| 8 | + | /// Kernel event metadata workspace. |
|
| 9 | + | unsafe static STORE: events::Store = undefined; |
|
| 10 | + | /// Consumer-visible ring for the receiver. |
|
| 11 | + | unsafe static RING: events::Ring = undefined; |
|
| 12 | + | /// Separate receiver used to check per-domain IRQ coalescing. |
|
| 13 | + | unsafe static OTHER: events::Ring = undefined; |
|
| 14 | + | ||
| 15 | + | /// Return the receiver's current domain reference. |
|
| 16 | + | fn receiver() -> abi::Ref { |
|
| 17 | + | return abi::Ref { index: 0, generation: 1 }; |
|
| 18 | + | } |
|
| 19 | + | ||
| 20 | + | /// Reset metadata and open one queue. |
|
| 21 | + | unsafe fn initialize() { |
|
| 22 | + | events::initialize(&mut STORE); |
|
| 23 | + | try! events::open(&mut STORE, receiver(), &mut RING); |
|
| 24 | + | } |
|
| 25 | + | ||
| 26 | + | /// Build an ordinary wakeup with a recognizable payload. |
|
| 27 | + | fn wake(value: u64) -> events::Event { |
|
| 28 | + | return events::Event { kind: events::WAKEUP, reserved: 0, code: 17, value }; |
|
| 29 | + | } |
|
| 30 | + | ||
| 31 | + | /// Duplicate IRQ notifications retain one queue entry even at ordinary capacity. |
|
| 32 | + | @test unsafe fn interruptCoalescing() throws (testing::TestError) { |
|
| 33 | + | initialize(); |
|
| 34 | + | let irq = events::Event { kind: events::INTERRUPT, reserved: 0, code: 5, value: 0 }; |
|
| 35 | + | assert try! events::send(&mut STORE, receiver(), &mut RING, irq); |
|
| 36 | + | set RING.data[0].code = 99; |
|
| 37 | + | assert try! events::send(&mut STORE, receiver(), &mut RING, irq); |
|
| 38 | + | assert STORE.queues[0].tail == 1; |
|
| 39 | + | for i in 1..events::ORDINARY { |
|
| 40 | + | assert try! events::send(&mut STORE, receiver(), &mut RING, wake(i as u64)); |
|
| 41 | + | } |
|
| 42 | + | assert try! events::send(&mut STORE, receiver(), &mut RING, irq); |
|
| 43 | + | assert STORE.queues[0].tail == events::ORDINARY; |
|
| 44 | + | assert not try! events::send(&mut STORE, receiver(), &mut RING, events::Event { |
|
| 45 | + | kind: events::INTERRUPT, reserved: 0, code: 6, value: 0 |
|
| 46 | + | }); |
|
| 47 | + | let consumed = try! events::pop(&mut RING) else panic "interruptCoalescing: missing interrupt"; |
|
| 48 | + | assert consumed.code == 99; |
|
| 49 | + | assert try! events::send(&mut STORE, receiver(), &mut RING, irq); |
|
| 50 | + | assert STORE.queues[0].tail == events::ORDINARY + 1; |
|
| 51 | + | } |
|
| 52 | + | ||
| 53 | + | /// Pending IRQ identity is scoped to the receiver and survives counter wrap. |
|
| 54 | + | @test unsafe fn interruptReceivers() throws (testing::TestError) { |
|
| 55 | + | initialize(); |
|
| 56 | + | let other = abi::Ref { index: 1, generation: 1 }; |
|
| 57 | + | try! events::open(&mut STORE, other, &mut OTHER); |
|
| 58 | + | set STORE.queues[0].head = 0xfffffffe; |
|
| 59 | + | set STORE.queues[0].tail = 0xfffffffe; |
|
| 60 | + | set RING.head = 0xfffffffe; |
|
| 61 | + | set RING.tail = 0xfffffffe; |
|
| 62 | + | let irq = events::Event { kind: events::INTERRUPT, reserved: 0, code: 5, value: 0 }; |
|
| 63 | + | assert try! events::send(&mut STORE, receiver(), &mut RING, irq); |
|
| 64 | + | assert try! events::send(&mut STORE, receiver(), &mut RING, events::Event { |
|
| 65 | + | kind: events::INTERRUPT, reserved: 0, code: 6, value: 0 |
|
| 66 | + | }); |
|
| 67 | + | assert try! events::send(&mut STORE, other, &mut OTHER, irq); |
|
| 68 | + | assert try! events::send(&mut STORE, receiver(), &mut RING, irq); |
|
| 69 | + | assert STORE.queues[0].tail == 0 and STORE.queues[1].tail == 1; |
|
| 70 | + | assert events::interruptPending(&STORE, receiver(), 5); |
|
| 71 | + | assert events::interruptPending(&STORE, other, 5); |
|
| 72 | + | let first = try! events::pop(&mut RING) else panic "interruptReceivers: missing wrapped IRQ"; |
|
| 73 | + | assert first.code == 5; |
|
| 74 | + | try! events::refresh(&mut STORE, receiver(), &RING); |
|
| 75 | + | assert not events::interruptPending(&STORE, receiver(), 5); |
|
| 76 | + | assert events::interruptPending(&STORE, receiver(), 6); |
|
| 77 | + | assert events::interruptPending(&STORE, other, 5); |
|
| 78 | + | assert try! events::send(&mut STORE, receiver(), &mut RING, irq); |
|
| 79 | + | assert STORE.queues[0].tail == 1; |
|
| 80 | + | } |
|
| 81 | + | ||
| 82 | + | /// Fill ordinary capacity while preserving the reserved lifecycle capacity. |
|
| 83 | + | @test unsafe fn saturation() throws (testing::TestError) { |
|
| 84 | + | initialize(); |
|
| 85 | + | for i in 0..events::ORDINARY { |
|
| 86 | + | try testing::expect(try! events::send(&mut STORE, receiver(), &mut RING, wake(i as u64))); |
|
| 87 | + | } |
|
| 88 | + | try testing::expect(not try! events::send(&mut STORE, receiver(), &mut RING, wake(999))); |
|
| 89 | + | for i in 0..65 { |
|
| 90 | + | let subject = abi::Ref { index: i + 1, generation: 1 }; |
|
| 91 | + | let pending = try! events::reserve(&mut STORE, subject); |
|
| 92 | + | events::finish(&mut STORE, pending, receiver(), events::CHILD_EXIT, i); |
|
| 93 | + | } |
|
| 94 | + | try testing::expect(try! events::flush(&mut STORE, receiver(), &mut RING) == 64); |
|
| 95 | + | let subject = abi::Ref { index: 65, generation: 1 }; |
|
| 96 | + | try testing::expect(not try! events::acknowledged(&STORE, subject)); |
|
| 97 | + | for i in 0..events::ORDINARY { |
|
| 98 | + | let event = try! events::pop(&mut RING) else panic "saturation: ordinary event"; |
|
| 99 | + | try testing::expect(event == wake(i as u64)); |
|
| 100 | + | } |
|
| 101 | + | try testing::expect(try! events::flush(&mut STORE, receiver(), &mut RING) == 1); |
|
| 102 | + | for i in 0..65 { |
|
| 103 | + | let event = try! events::pop(&mut RING) else panic "saturation: terminal event"; |
|
| 104 | + | try testing::expect(event.kind == events::CHILD_EXIT and event.code == i); |
|
| 105 | + | try testing::expect(event.value == abi::id(abi::Ref { index: i + 1, generation: 1 })); |
|
| 106 | + | } |
|
| 107 | + | try! events::refresh(&mut STORE, receiver(), &RING); |
|
| 108 | + | try testing::expect(try! events::acknowledged(&STORE, subject)); |
|
| 109 | + | try! events::release(&mut STORE, subject); |
|
| 110 | + | let replacement = try! events::reserve(&mut STORE, abi::Ref { index: 65, generation: 2 }); |
|
| 111 | + | events::cancel(&mut STORE, replacement); |
|
| 112 | + | } |
|
| 113 | + | ||
| 114 | + | /// Head and tail arithmetic remains valid across the u32 boundary. |
|
| 115 | + | @test unsafe fn counterWrap() throws (testing::TestError) { |
|
| 116 | + | initialize(); |
|
| 117 | + | set STORE.queues[0].head = 0xfffffffe; |
|
| 118 | + | set STORE.queues[0].tail = 0xfffffffe; |
|
| 119 | + | sync::storeRelease32(&mut RING.head, 0xfffffffe); |
|
| 120 | + | sync::storeRelease32(&mut RING.tail, 0xfffffffe); |
|
| 121 | + | for i in 0..4 { |
|
| 122 | + | try testing::expect(try! events::send(&mut STORE, receiver(), &mut RING, wake(i as u64))); |
|
| 123 | + | } |
|
| 124 | + | for i in 0..4 { |
|
| 125 | + | let event = try! events::pop(&mut RING) else panic "counterWrap: wrapped event"; |
|
| 126 | + | try testing::expect(event == wake(i as u64)); |
|
| 127 | + | } |
|
| 128 | + | try! events::refresh(&mut STORE, receiver(), &RING); |
|
| 129 | + | try testing::expect(STORE.queues[0].head == 2 and STORE.queues[0].tail == 2); |
|
| 130 | + | try testing::expect(try! events::pop(&mut RING) == nil); |
|
| 131 | + | } |
|
| 132 | + | ||
| 133 | + | /// Invalid consumer advances cannot acknowledge or overwrite a pending event. |
|
| 134 | + | @test unsafe fn invalidConsumer() throws (testing::TestError) { |
|
| 135 | + | initialize(); |
|
| 136 | + | try testing::expect(try! events::send(&mut STORE, receiver(), &mut RING, wake(42))); |
|
| 137 | + | for head in &[2 as u32, 0xffffffff] { |
|
| 138 | + | sync::storeRelease32(&mut RING.head, head); |
|
| 139 | + | let mut failed = false; |
|
| 140 | + | try events::refresh(&mut STORE, receiver(), &RING) catch err { |
|
| 141 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 142 | + | set failed = true; |
|
| 143 | + | }; |
|
| 144 | + | try testing::expect(failed and STORE.queues[0].head == 0 and STORE.queues[0].tail == 1); |
|
| 145 | + | } |
|
| 146 | + | sync::storeRelease32(&mut RING.head, 0); |
|
| 147 | + | let event = try! events::pop(&mut RING) else panic "invalidConsumer: retained event"; |
|
| 148 | + | try testing::expect(event == wake(42)); |
|
| 149 | + | } |
|
| 150 | + | ||
| 151 | + | /// A receiver's exit ends both queued and unpublished terminal references. |
|
| 152 | + | @test unsafe fn receiverExit() throws (testing::TestError) { |
|
| 153 | + | initialize(); |
|
| 154 | + | let subject = abi::Ref { index: 1, generation: 7 }; |
|
| 155 | + | let pending = try! events::reserve(&mut STORE, subject); |
|
| 156 | + | events::finish(&mut STORE, pending, receiver(), events::FAULT, 23); |
|
| 157 | + | try testing::expect(try! events::flush(&mut STORE, receiver(), &mut RING) == 1); |
|
| 158 | + | let waiting = abi::Ref { index: 2, generation: 3 }; |
|
| 159 | + | let second = try! events::reserve(&mut STORE, waiting); |
|
| 160 | + | events::finish(&mut STORE, second, receiver(), events::CHILD_EXIT, 0); |
|
| 161 | + | try! events::close(&mut STORE, receiver()); |
|
| 162 | + | try testing::expect(try! events::acknowledged(&STORE, subject)); |
|
| 163 | + | try testing::expect(try! events::acknowledged(&STORE, waiting)); |
|
| 164 | + | try! events::open(&mut STORE, abi::Ref { index: 0, generation: 2 }, &mut RING); |
|
| 165 | + | try testing::expect(try! events::pop(&mut RING) == nil); |
|
| 166 | + | } |
|
| 167 | + | ||
| 168 | + | /// Terminal IDs stay reserved through counter wrap and until producer acknowledgement. |
|
| 169 | + | @test unsafe fn terminalIdentity() throws (testing::TestError) { |
|
| 170 | + | initialize(); |
|
| 171 | + | set STORE.queues[0].head = 0xffffffff; |
|
| 172 | + | set STORE.queues[0].tail = 0xffffffff; |
|
| 173 | + | sync::storeRelease32(&mut RING.head, 0xffffffff); |
|
| 174 | + | sync::storeRelease32(&mut RING.tail, 0xffffffff); |
|
| 175 | + | let subject = abi::Ref { index: 1, generation: 4 }; |
|
| 176 | + | let replacement = abi::Ref { index: 1, generation: 5 }; |
|
| 177 | + | let pending = try! events::reserve(&mut STORE, subject); |
|
| 178 | + | events::finish(&mut STORE, pending, receiver(), events::FAULT, 13); |
|
| 179 | + | try testing::expect(try! events::flush(&mut STORE, receiver(), &mut RING) == 1); |
|
| 180 | + | let event = try! events::pop(&mut RING) else panic "terminalIdentity: terminal wrap"; |
|
| 181 | + | try testing::expect( |
|
| 182 | + | event.kind == events::FAULT |
|
| 183 | + | and event.code == 13 |
|
| 184 | + | and event.value == abi::id(subject) |
|
| 185 | + | ); |
|
| 186 | + | let mut busy: u32 = 0; |
|
| 187 | + | try events::release(&mut STORE, subject) catch err { |
|
| 188 | + | try testing::expect(err == abi::Error::Busy); |
|
| 189 | + | set busy += 1; |
|
| 190 | + | }; |
|
| 191 | + | try reserveAndCancel(replacement) catch err { |
|
| 192 | + | try testing::expect(err == abi::Error::Busy); |
|
| 193 | + | set busy += 1; |
|
| 194 | + | }; |
|
| 195 | + | sync::storeRelease32(&mut RING.head, 1); |
|
| 196 | + | let mut invalid = false; |
|
| 197 | + | try events::refresh(&mut STORE, receiver(), &RING) catch err { |
|
| 198 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 199 | + | set invalid = true; |
|
| 200 | + | }; |
|
| 201 | + | try testing::expect(invalid and busy == 2 and not try! events::acknowledged(&STORE, subject)); |
|
| 202 | + | sync::storeRelease32(&mut RING.head, 0); |
|
| 203 | + | try! events::refresh(&mut STORE, receiver(), &RING); |
|
| 204 | + | try! events::release(&mut STORE, subject); |
|
| 205 | + | let next = try! events::reserve(&mut STORE, replacement); |
|
| 206 | + | events::finish(&mut STORE, next, abi::Ref { index: 0, generation: 0 }, events::CHILD_EXIT, 0); |
|
| 207 | + | try testing::expect(try! events::acknowledged(&STORE, replacement)); |
|
| 208 | + | let mut stale = false; |
|
| 209 | + | try events::acknowledged(&STORE, subject) catch err { |
|
| 210 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 211 | + | set stale = true; |
|
| 212 | + | }; |
|
| 213 | + | try testing::expect(stale); |
|
| 214 | + | } |
|
| 215 | + | ||
| 216 | + | /// Consume a successful reservation probe without committing termination. |
|
| 217 | + | unsafe fn reserveAndCancel(subject: abi::Ref) throws (abi::Error) { |
|
| 218 | + | let pending = try events::reserve(&mut STORE, subject); |
|
| 219 | + | events::cancel(&mut STORE, pending); |
|
| 220 | + | } |
|
| 221 | + | ||
| 222 | + | /// Queue and terminal pools reject invalid generations and restore cancelled capacity. |
|
| 223 | + | @test unsafe fn capacityAndValidation() throws (testing::TestError) { |
|
| 224 | + | initialize(); |
|
| 225 | + | try testing::expect(@sizeOf(events::Event) == 16 and @sizeOf(events::Ring) == 4112); |
|
| 226 | + | for i in 0..512 { |
|
| 227 | + | try! reserveAndCancel(abi::Ref { index: i, generation: 1 }); |
|
| 228 | + | } |
|
| 229 | + | let mut invalid: u32 = 0; |
|
| 230 | + | for subject in &[abi::Ref { index: 512, generation: 1 }, abi::Ref { index: 0, generation: 0 }] { |
|
| 231 | + | try reserveAndCancel(subject) catch err { |
|
| 232 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 233 | + | set invalid += 1; |
|
| 234 | + | }; |
|
| 235 | + | } |
|
| 236 | + | let mut event = wake(0); |
|
| 237 | + | set event.kind = events::FAULT; |
|
| 238 | + | try events::send(&mut STORE, receiver(), &mut RING, event) catch err { |
|
| 239 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 240 | + | set invalid += 1; |
|
| 241 | + | }; |
|
| 242 | + | set event.kind = events::WAKEUP; |
|
| 243 | + | set event.reserved = 1; |
|
| 244 | + | try events::send(&mut STORE, receiver(), &mut RING, event) catch err { |
|
| 245 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 246 | + | set invalid += 1; |
|
| 247 | + | }; |
|
| 248 | + | try testing::expect(invalid == 4 and STORE.queues[0].tail == 0); |
|
| 249 | + | } |
kernel/kernel/tests/fdt.rad
added
+166 -0
| 1 | + | //! Device-tree byte fixtures and structural rejection checks. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::fdt; |
|
| 5 | + | ||
| 6 | + | /// Store a big-endian field in a test fixture. |
|
| 7 | + | fn put(bytes: &mut [u8], offset: u32, width: u32, value: u64) { |
|
| 8 | + | for i in 0..width { |
|
| 9 | + | set bytes[offset + i] = (value >> ((width - i - 1) * 8) as u64) as u8; |
|
| 10 | + | } |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Build one root property and one high-address memory reservation. |
|
| 14 | + | fn fixture(bytes: &mut [u8]) { |
|
| 15 | + | for i in 0..bytes.len { |
|
| 16 | + | set bytes[i] = 0; |
|
| 17 | + | } |
|
| 18 | + | put(bytes, 0, 4, 0xd00dfeed); |
|
| 19 | + | put(bytes, 4, 4, 108); |
|
| 20 | + | put(bytes, 8, 4, 72); |
|
| 21 | + | put(bytes, 12, 4, 104); |
|
| 22 | + | put(bytes, 16, 4, 40); |
|
| 23 | + | put(bytes, 20, 4, 17); |
|
| 24 | + | put(bytes, 24, 4, 16); |
|
| 25 | + | put(bytes, 32, 4, 4); |
|
| 26 | + | put(bytes, 36, 4, 32); |
|
| 27 | + | put(bytes, 40, 8, 0x80000000); |
|
| 28 | + | put(bytes, 48, 8, 4096); |
|
| 29 | + | put(bytes, 72, 4, 1); |
|
| 30 | + | put(bytes, 80, 4, 3); |
|
| 31 | + | put(bytes, 84, 4, 4); |
|
| 32 | + | put(bytes, 88, 4, 0); |
|
| 33 | + | put(bytes, 92, 4, 42); |
|
| 34 | + | put(bytes, 96, 4, 2); |
|
| 35 | + | put(bytes, 100, 4, 9); |
|
| 36 | + | set bytes[104] = 'r'; |
|
| 37 | + | set bytes[105] = 'e'; |
|
| 38 | + | set bytes[106] = 'g'; |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | /// Validate the complete tree, including the final structure token. |
|
| 42 | + | fn validate(bytes: &[u8]) throws (fdt::Error) { |
|
| 43 | + | let tree = try fdt::header(bytes); |
|
| 44 | + | let mut state = fdt::cursor(tree); |
|
| 45 | + | loop { |
|
| 46 | + | match try fdt::next(bytes, tree, &mut state) { |
|
| 47 | + | case fdt::Event::End => return, |
|
| 48 | + | else => {} |
|
| 49 | + | } |
|
| 50 | + | } |
|
| 51 | + | } |
|
| 52 | + | ||
| 53 | + | /// Require malformed input to fail through a bounded decoder error. |
|
| 54 | + | fn rejects(bytes: &[u8]) throws (testing::TestError) { |
|
| 55 | + | let mut failed = false; |
|
| 56 | + | try validate(bytes) catch { |
|
| 57 | + | set failed = true; |
|
| 58 | + | }; |
|
| 59 | + | try testing::expect(failed); |
|
| 60 | + | } |
|
| 61 | + | ||
| 62 | + | /// Decode big-endian ranges and the ordered structure events. |
|
| 63 | + | @test unsafe fn traversal() throws (testing::TestError) { |
|
| 64 | + | let mut bytes: [u8; 108] = undefined; |
|
| 65 | + | fixture(&mut bytes[..]); |
|
| 66 | + | let tree = try! fdt::header(&bytes[..]); |
|
| 67 | + | try testing::expect(tree.reservations.start == 40 and tree.reservations.end == 56); |
|
| 68 | + | try testing::expect(try! fdt::integer(&bytes[..], 40, 8) == 0x80000000); |
|
| 69 | + | let mut state = fdt::cursor(tree); |
|
| 70 | + | match try! fdt::next(&bytes[..], tree, &mut state) { |
|
| 71 | + | case fdt::Event::Begin(name) => try testing::expect(name.start == name.end), |
|
| 72 | + | else => try testing::expect(false), |
|
| 73 | + | } |
|
| 74 | + | match try! fdt::next(&bytes[..], tree, &mut state) { |
|
| 75 | + | case fdt::Event::Property { name, value } => { |
|
| 76 | + | try testing::expect(name.end - name.start == 3 and bytes[name.start] == 'r'); |
|
| 77 | + | try testing::expect(value.end - value.start == 4); |
|
| 78 | + | try testing::expect(try! fdt::integer(&bytes[..], value.start, 4) == 42); |
|
| 79 | + | } |
|
| 80 | + | else => try testing::expect(false), |
|
| 81 | + | } |
|
| 82 | + | match try! fdt::next(&bytes[..], tree, &mut state) { |
|
| 83 | + | case fdt::Event::EndNode => {} |
|
| 84 | + | else => try testing::expect(false), |
|
| 85 | + | } |
|
| 86 | + | match try! fdt::next(&bytes[..], tree, &mut state) { |
|
| 87 | + | case fdt::Event::End => {} |
|
| 88 | + | else => try testing::expect(false), |
|
| 89 | + | } |
|
| 90 | + | try testing::expect(state.finished and state.depth == 0); |
|
| 91 | + | } |
|
| 92 | + | ||
| 93 | + | /// Reject every truncated prefix and invalid block or reservation extents. |
|
| 94 | + | @test unsafe fn malformedHeader() throws (testing::TestError) { |
|
| 95 | + | let mut bytes: [u8; 108] = undefined; |
|
| 96 | + | fixture(&mut bytes[..]); |
|
| 97 | + | for size in 0..bytes.len { |
|
| 98 | + | try rejects(&bytes[..size]); |
|
| 99 | + | } |
|
| 100 | + | for offset in &[0 as u32, 4, 8, 12, 16, 20, 24, 32, 36] { |
|
| 101 | + | fixture(&mut bytes[..]); |
|
| 102 | + | put(&mut bytes[..], offset, 4, 0xffffffff); |
|
| 103 | + | try rejects(&bytes[..]); |
|
| 104 | + | } |
|
| 105 | + | fixture(&mut bytes[..]); |
|
| 106 | + | put(&mut bytes[..], 12, 4, 80); |
|
| 107 | + | try rejects(&bytes[..]); |
|
| 108 | + | fixture(&mut bytes[..]); |
|
| 109 | + | put(&mut bytes[..], 40, 8, 0xfffffffffffff800); |
|
| 110 | + | try rejects(&bytes[..]); |
|
| 111 | + | fixture(&mut bytes[..]); |
|
| 112 | + | put(&mut bytes[..], 48, 8, 0); |
|
| 113 | + | try rejects(&bytes[..]); |
|
| 114 | + | } |
|
| 115 | + | ||
| 116 | + | /// Reject missing names, oversized properties, bad tokens, and unbalanced nodes. |
|
| 117 | + | @test unsafe fn malformedStructure() throws (testing::TestError) { |
|
| 118 | + | let mut bytes: [u8; 108] = undefined; |
|
| 119 | + | for offset in &[72 as u32, 80, 84, 88, 96, 100] { |
|
| 120 | + | fixture(&mut bytes[..]); |
|
| 121 | + | put(&mut bytes[..], offset, 4, 0xffffffff); |
|
| 122 | + | try rejects(&bytes[..]); |
|
| 123 | + | } |
|
| 124 | + | fixture(&mut bytes[..]); |
|
| 125 | + | set bytes[107] = 1; |
|
| 126 | + | try rejects(&bytes[..]); |
|
| 127 | + | fixture(&mut bytes[..]); |
|
| 128 | + | set bytes[77] = 1; |
|
| 129 | + | try rejects(&bytes[..]); |
|
| 130 | + | fixture(&mut bytes[..]); |
|
| 131 | + | put(&mut bytes[..], 96, 4, 4); |
|
| 132 | + | try rejects(&bytes[..]); |
|
| 133 | + | fixture(&mut bytes[..]); |
|
| 134 | + | put(&mut bytes[..], 80, 4, 2); |
|
| 135 | + | try rejects(&bytes[..]); |
|
| 136 | + | } |
|
| 137 | + | ||
| 138 | + | /// Excessive nesting fails before any shift or stack index exceeds its bound. |
|
| 139 | + | @test unsafe fn depthLimit() throws (testing::TestError) { |
|
| 140 | + | let mut bytes: [u8; 512] = undefined; |
|
| 141 | + | fixture(&mut bytes[..]); |
|
| 142 | + | put(&mut bytes[..], 4, 4, 512); |
|
| 143 | + | put(&mut bytes[..], 12, 4, 512); |
|
| 144 | + | put(&mut bytes[..], 32, 4, 0); |
|
| 145 | + | put(&mut bytes[..], 36, 4, 440); |
|
| 146 | + | for i in 72..512 { |
|
| 147 | + | set bytes[i] = 0; |
|
| 148 | + | } |
|
| 149 | + | for i in 0..33 { |
|
| 150 | + | put(&mut bytes[..], 72 + i * 8, 4, 1); |
|
| 151 | + | if i > 0 { |
|
| 152 | + | set bytes[76 + i * 8] = 'x'; |
|
| 153 | + | } |
|
| 154 | + | } |
|
| 155 | + | let tree = try! fdt::header(&bytes[..]); |
|
| 156 | + | let mut state = fdt::cursor(tree); |
|
| 157 | + | for i in 0..32 { |
|
| 158 | + | try! fdt::next(&bytes[..], tree, &mut state); |
|
| 159 | + | } |
|
| 160 | + | let mut failed = false; |
|
| 161 | + | try fdt::next(&bytes[..], tree, &mut state) catch err { |
|
| 162 | + | try testing::expect(err == fdt::Error::Depth); |
|
| 163 | + | set failed = true; |
|
| 164 | + | }; |
|
| 165 | + | try testing::expect(failed); |
|
| 166 | + | } |
kernel/kernel/tests/frames.rad
added
+305 -0
| 1 | + | //! Contiguous frame reservation and reserved-memory exclusion. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::frames; |
|
| 6 | + | use kernel::limits; |
|
| 7 | + | use kernel::platform; |
|
| 8 | + | use kernel::range; |
|
| 9 | + | ||
| 10 | + | /// Frame-map workspace is static because its size exceeds a small kernel stack. |
|
| 11 | + | unsafe static POOL: frames::Pool = undefined; |
|
| 12 | + | ||
| 13 | + | /// Build eight available frames around a reserved two-frame hole. |
|
| 14 | + | unsafe fn initialize() { |
|
| 15 | + | let mut machine: platform::Platform = undefined; |
|
| 16 | + | set machine.ramCount = 1; |
|
| 17 | + | set machine.ram[0] = range::Range { start: 0x80000000, end: 0x8000a000 }; |
|
| 18 | + | set machine.reservedCount = 1; |
|
| 19 | + | set machine.reserved[0] = range::Range { start: 0x80003000, end: 0x80005000 }; |
|
| 20 | + | try! frames::initialize(&mut POOL, &machine); |
|
| 21 | + | } |
|
| 22 | + | ||
| 23 | + | /// A RAM bank at address 0 loses its first page: no frame is at address 0. |
|
| 24 | + | @test unsafe fn skipsAddressZero() throws (testing::TestError) { |
|
| 25 | + | let mut machine: platform::Platform = undefined; |
|
| 26 | + | set machine.ramCount = 1; |
|
| 27 | + | set machine.ram[0] = range::Range { start: 0, end: 0x3000 }; |
|
| 28 | + | set machine.reservedCount = 0; |
|
| 29 | + | try! frames::initialize(&mut POOL, &machine); |
|
| 30 | + | assert POOL.count == 2; |
|
| 31 | + | assert POOL.addresses[0] == 4096 and POOL.addresses[1] == 8192; |
|
| 32 | + | assert frames::find(&POOL, 0) == nil; |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Bitmap allocation agrees with a scalar scan across holes and word boundaries. |
|
| 36 | + | @test unsafe fn bitmapRuns() throws (testing::TestError) { |
|
| 37 | + | let mut machine: platform::Platform = undefined; |
|
| 38 | + | set machine.ramCount = 1; |
|
| 39 | + | set machine.ram[0] = range::Range { start: 0x80000000, end: 0x800c2000 }; |
|
| 40 | + | set machine.reservedCount = 1; |
|
| 41 | + | set machine.reserved[0] = range::Range { start: 0x8001f000, end: 0x80020000 }; |
|
| 42 | + | for pattern in 0..5 { |
|
| 43 | + | for count in [1 as u32, 2, 31, 32, 63, 64, 65, 127, 128, 129, 193, 194] { |
|
| 44 | + | try! frames::initialize(&mut POOL, &machine); |
|
| 45 | + | assert POOL.count == 193; |
|
| 46 | + | for i in 0..POOL.count { |
|
| 47 | + | if pattern > 0 and i % (pattern * 32) == 0 { |
|
| 48 | + | set POOL.free[i / 64] &= ~(1 << (i % 64) as u64); |
|
| 49 | + | } |
|
| 50 | + | } |
|
| 51 | + | // Padding bits are outside the pool even when their values are set. |
|
| 52 | + | set POOL.free[3] |= 0xfffffffffffffffe; |
|
| 53 | + | let before = [POOL.free[0], POOL.free[1], POOL.free[2], POOL.free[3]]; |
|
| 54 | + | let mut expected: ?u32 = nil; |
|
| 55 | + | let mut found: u32 = 0; |
|
| 56 | + | for i in 0..POOL.count { |
|
| 57 | + | if not frames::available(&POOL, i) { |
|
| 58 | + | set found = 0; |
|
| 59 | + | continue; |
|
| 60 | + | } |
|
| 61 | + | if found > 0 and POOL.addresses[i] <> POOL.addresses[i - 1] + 4096 { |
|
| 62 | + | set found = 0; |
|
| 63 | + | } |
|
| 64 | + | set found += 1; |
|
| 65 | + | if found == count { |
|
| 66 | + | set expected = i + 1 - count; |
|
| 67 | + | break; |
|
| 68 | + | } |
|
| 69 | + | } |
|
| 70 | + | let reservation = try frames::reserve(&mut POOL, count) catch error { |
|
| 71 | + | assert expected == nil and error == abi::Error::OutOfMemory; |
|
| 72 | + | for i in 0..4 { |
|
| 73 | + | assert POOL.free[i] == before[i]; |
|
| 74 | + | } |
|
| 75 | + | continue; |
|
| 76 | + | }; |
|
| 77 | + | let first = expected else panic "bitmapRuns: unexpected frame reservation"; |
|
| 78 | + | let run = frames::commit(reservation); |
|
| 79 | + | assert run.first == first and run.count == count; |
|
| 80 | + | for i in 0..POOL.count { |
|
| 81 | + | if i >= first and i - first < count { |
|
| 82 | + | assert not frames::available(&POOL, i); |
|
| 83 | + | } else { |
|
| 84 | + | assert frames::available( |
|
| 85 | + | &POOL, i |
|
| 86 | + | ) == ((before[i / 64] & (1 << (i % 64) as u64)) <> 0); |
|
| 87 | + | } |
|
| 88 | + | } |
|
| 89 | + | try! frames::release(&mut POOL, run); |
|
| 90 | + | for i in 0..4 { |
|
| 91 | + | assert POOL.free[i] == before[i]; |
|
| 92 | + | } |
|
| 93 | + | } |
|
| 94 | + | } |
|
| 95 | + | assert not frames::available(&POOL, POOL.count); |
|
| 96 | + | try! frames::initialize(&mut POOL, &machine); |
|
| 97 | + | let run = frames::commit(try! frames::reserve(&mut POOL, 128)); |
|
| 98 | + | assert run.first == 31; |
|
| 99 | + | let before = [POOL.free[0], POOL.free[1], POOL.free[2], POOL.free[3]]; |
|
| 100 | + | let mut invalid = false; |
|
| 101 | + | try frames::release(&mut POOL, frames::Run { first: run.first, count: 129 }) catch error { |
|
| 102 | + | assert error == abi::Error::BadHandle; |
|
| 103 | + | set invalid = true; |
|
| 104 | + | }; |
|
| 105 | + | assert invalid; |
|
| 106 | + | for i in 0..4 { |
|
| 107 | + | assert POOL.free[i] == before[i]; |
|
| 108 | + | } |
|
| 109 | + | try! frames::release(&mut POOL, run); |
|
| 110 | + | } |
|
| 111 | + | ||
| 112 | + | /// Retired frames stay unavailable until bounded reclamation returns them. |
|
| 113 | + | @test unsafe fn retirement() throws (testing::TestError) { |
|
| 114 | + | initialize(); |
|
| 115 | + | set POOL.retiredHead = limits::FRAMES - 1; |
|
| 116 | + | let left = frames::commit(try! frames::reserve(&mut POOL, 3)); |
|
| 117 | + | let right = frames::commit(try! frames::reserve(&mut POOL, 5)); |
|
| 118 | + | try! frames::retire(&mut POOL, left); |
|
| 119 | + | try! frames::retire(&mut POOL, right); |
|
| 120 | + | assert POOL.retiredCount == 2; |
|
| 121 | + | for i in 0..8 { |
|
| 122 | + | assert not frames::available(&POOL, i); |
|
| 123 | + | } |
|
| 124 | + | assert frames::reclaim(&mut POOL, 0) == 0; |
|
| 125 | + | assert frames::reclaim(&mut POOL, 2) == 2; |
|
| 126 | + | assert frames::available(&POOL, 0) |
|
| 127 | + | and frames::available(&POOL, 1) |
|
| 128 | + | and not frames::available(&POOL, 2); |
|
| 129 | + | let reused = frames::commit(try! frames::reserve(&mut POOL, 2)); |
|
| 130 | + | assert reused.first == 0; |
|
| 131 | + | assert frames::reclaim(&mut POOL, 3) == 3; |
|
| 132 | + | assert POOL.retiredCount == 1 and not frames::available(&POOL, 5); |
|
| 133 | + | assert not frames::available(&POOL, 0) and not frames::available(&POOL, 1); |
|
| 134 | + | try! frames::retire(&mut POOL, reused); |
|
| 135 | + | assert frames::reclaim(&mut POOL, 20) == 5; |
|
| 136 | + | assert POOL.retiredCount == 0; |
|
| 137 | + | for i in 0..8 { |
|
| 138 | + | assert frames::available(&POOL, i); |
|
| 139 | + | } |
|
| 140 | + | assert frames::reclaim(&mut POOL, 20) == 0; |
|
| 141 | + | } |
|
| 142 | + | ||
| 143 | + | /// Adjacent physical frames form runs; a reserved hole cannot be crossed. |
|
| 144 | + | @test unsafe fn fragmentation() throws (testing::TestError) { |
|
| 145 | + | initialize(); |
|
| 146 | + | try testing::expect(POOL.count == 8); |
|
| 147 | + | let left = try! frames::reserve(&mut POOL, 3); |
|
| 148 | + | let leftRun = frames::commit(left); |
|
| 149 | + | let right = try! frames::reserve(&mut POOL, 5); |
|
| 150 | + | let rightRun = frames::commit(right); |
|
| 151 | + | let first = try! frames::extent(&POOL, leftRun); |
|
| 152 | + | let second = try! frames::extent(&POOL, rightRun); |
|
| 153 | + | try testing::expect(first.start == 0x80000000 and first.end == 0x80003000); |
|
| 154 | + | try testing::expect(second.start == 0x80005000 and second.end == 0x8000a000); |
|
| 155 | + | try! frames::release(&mut POOL, leftRun); |
|
| 156 | + | try! frames::release(&mut POOL, rightRun); |
|
| 157 | + | let mut failed = false; |
|
| 158 | + | try reserveAndCancel(6) catch err { |
|
| 159 | + | try testing::expect(err == abi::Error::OutOfMemory); |
|
| 160 | + | set failed = true; |
|
| 161 | + | }; |
|
| 162 | + | try testing::expect(failed); |
|
| 163 | + | } |
|
| 164 | + | ||
| 165 | + | /// Consume a successful probe reservation before returning to its caller. |
|
| 166 | + | unsafe fn reserveAndCancel(count: u32) throws (abi::Error) { |
|
| 167 | + | let token = try frames::reserve(&mut POOL, count); |
|
| 168 | + | try frames::cancel(&mut POOL, token); |
|
| 169 | + | } |
|
| 170 | + | ||
| 171 | + | /// Full exhaustion and cancellation preserve the pool's available capacity. |
|
| 172 | + | @test unsafe fn capacity() throws (testing::TestError) { |
|
| 173 | + | initialize(); |
|
| 174 | + | let mut allocated: [frames::Run; 8] = undefined; |
|
| 175 | + | for i in 0..8 { |
|
| 176 | + | let token = try! frames::reserve(&mut POOL, 1); |
|
| 177 | + | set allocated[i] = frames::commit(token); |
|
| 178 | + | } |
|
| 179 | + | let mut failed = false; |
|
| 180 | + | try reserveAndCancel(1) catch err { |
|
| 181 | + | try testing::expect(err == abi::Error::OutOfMemory); |
|
| 182 | + | set failed = true; |
|
| 183 | + | }; |
|
| 184 | + | try testing::expect(failed); |
|
| 185 | + | for i in 0..8 { |
|
| 186 | + | try! frames::release(&mut POOL, allocated[i]); |
|
| 187 | + | } |
|
| 188 | + | let pending = try! frames::reserve(&mut POOL, 5); |
|
| 189 | + | try! frames::cancel(&mut POOL, pending); |
|
| 190 | + | let again = try! frames::reserve(&mut POOL, 5); |
|
| 191 | + | try! frames::cancel(&mut POOL, again); |
|
| 192 | + | let mut empty = false; |
|
| 193 | + | try reserveAndCancel(0) catch err { |
|
| 194 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 195 | + | set empty = true; |
|
| 196 | + | }; |
|
| 197 | + | try testing::expect(empty); |
|
| 198 | + | } |
|
| 199 | + | ||
| 200 | + | /// A release validates every frame before mutating any occupancy state. |
|
| 201 | + | @test unsafe fn invalidRelease() throws (testing::TestError) { |
|
| 202 | + | initialize(); |
|
| 203 | + | let token = try! frames::reserve(&mut POOL, 2); |
|
| 204 | + | let run = frames::commit(token); |
|
| 205 | + | let mut failed = false; |
|
| 206 | + | try frames::release(&mut POOL, frames::Run { first: run.first, count: 3 }) catch err { |
|
| 207 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 208 | + | set failed = true; |
|
| 209 | + | }; |
|
| 210 | + | try testing::expect(failed); |
|
| 211 | + | try! frames::release(&mut POOL, run); |
|
| 212 | + | let mut duplicate = false; |
|
| 213 | + | try frames::release(&mut POOL, run) catch err { |
|
| 214 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 215 | + | set duplicate = true; |
|
| 216 | + | }; |
|
| 217 | + | try testing::expect(duplicate); |
|
| 218 | + | } |
|
| 219 | + | ||
| 220 | + | /// Only complete, unreserved physical pages can enter the frame pool. |
|
| 221 | + | @test unsafe fn partialPages() throws (testing::TestError) { |
|
| 222 | + | let mut machine: platform::Platform = undefined; |
|
| 223 | + | set machine.ramCount = 1; |
|
| 224 | + | set machine.ram[0] = range::Range { start: 0x80000001, end: 0x80006001 }; |
|
| 225 | + | set machine.reservedCount = 1; |
|
| 226 | + | set machine.reserved[0] = range::Range { start: 0x80002fff, end: 0x80003001 }; |
|
| 227 | + | try! frames::initialize(&mut POOL, &machine); |
|
| 228 | + | try testing::expect(POOL.count == 3); |
|
| 229 | + | let token = try! frames::reserve(&mut POOL, 2); |
|
| 230 | + | let run = frames::commit(token); |
|
| 231 | + | let span = try! frames::extent(&POOL, run); |
|
| 232 | + | try testing::expect(span.start == 0x80004000 and span.end == 0x80006000); |
|
| 233 | + | } |
|
| 234 | + | ||
| 235 | + | /// Physically adjacent banks form a run regardless of device-tree order. |
|
| 236 | + | @test unsafe fn bankOrder() throws (testing::TestError) { |
|
| 237 | + | let mut machine: platform::Platform = undefined; |
|
| 238 | + | set machine.ramCount = 2; |
|
| 239 | + | set machine.reservedCount = 0; |
|
| 240 | + | set machine.ram[0] = range::Range { start: 0x80002000, end: 0x80004000 }; |
|
| 241 | + | set machine.ram[1] = range::Range { start: 0x80000000, end: 0x80002000 }; |
|
| 242 | + | try! frames::initialize(&mut POOL, &machine); |
|
| 243 | + | let token = try! frames::reserve(&mut POOL, 4); |
|
| 244 | + | let run = frames::commit(token); |
|
| 245 | + | let memory = try! frames::extent(&POOL, run); |
|
| 246 | + | try testing::expect(memory.start == 0x80000000 and memory.end == 0x80004000); |
|
| 247 | + | set machine.ram[1].end = 0x80003000; |
|
| 248 | + | let mut failed = false; |
|
| 249 | + | try frames::initialize(&mut POOL, &machine) catch err { |
|
| 250 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 251 | + | set failed = true; |
|
| 252 | + | }; |
|
| 253 | + | try testing::expect(failed and POOL.count == 0); |
|
| 254 | + | } |
|
| 255 | + | ||
| 256 | + | /// The full 65,536-frame profile fits; one extra eligible frame fails cleanly. |
|
| 257 | + | @test unsafe fn maximum() throws (testing::TestError) { |
|
| 258 | + | let mut machine: platform::Platform = undefined; |
|
| 259 | + | set machine.ramCount = 1; |
|
| 260 | + | set machine.reservedCount = 0; |
|
| 261 | + | set machine.ram[0] = range::Range { start: 0x80000000, end: 0x90000000 }; |
|
| 262 | + | try! frames::initialize(&mut POOL, &machine); |
|
| 263 | + | try testing::expect(POOL.count == 65536); |
|
| 264 | + | let token = try! frames::reserve(&mut POOL, 65536); |
|
| 265 | + | let run = frames::commit(token); |
|
| 266 | + | try! frames::release(&mut POOL, run); |
|
| 267 | + | set machine.ram[0].end += 4096; |
|
| 268 | + | let mut failed = false; |
|
| 269 | + | try frames::initialize(&mut POOL, &machine) catch err { |
|
| 270 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 271 | + | set failed = true; |
|
| 272 | + | }; |
|
| 273 | + | try testing::expect(failed and POOL.count == 0); |
|
| 274 | + | set machine.ram[0] = range::Range { start: 0xfffffffffffff001, end: 0xffffffffffffffff }; |
|
| 275 | + | try! frames::initialize(&mut POOL, &machine); |
|
| 276 | + | try testing::expect(POOL.count == 0); |
|
| 277 | + | } |
|
| 278 | + | ||
| 279 | + | /// Sorted reservation skipping agrees with byte-range exclusion across banks and gaps. |
|
| 280 | + | @test unsafe fn reservationSweep() throws (testing::TestError) { |
|
| 281 | + | let mut machine: platform::Platform = undefined; |
|
| 282 | + | set machine.ramCount = 3; |
|
| 283 | + | set machine.ram[0] = range::Range { start: 0x80040000, end: 0x80060000 }; |
|
| 284 | + | set machine.ram[1] = range::Range { start: 0x80000000, end: 0x80010000 }; |
|
| 285 | + | set machine.ram[2] = range::Range { start: 0x80010000, end: 0x80020000 }; |
|
| 286 | + | set machine.reservedCount = 6; |
|
| 287 | + | set machine.reserved[0] = range::Range { start: 0x80050001, end: 0x8005f000 }; |
|
| 288 | + | set machine.reserved[1] = range::Range { start: 0x80001234, end: 0x80004321 }; |
|
| 289 | + | set machine.reserved[2] = range::Range { start: 0x80004000, end: 0x80009001 }; |
|
| 290 | + | set machine.reserved[3] = range::Range { start: 0x7ffff000, end: 0x80000001 }; |
|
| 291 | + | set machine.reserved[4] = range::Range { start: 0x80018000, end: 0x80048000 }; |
|
| 292 | + | set machine.reserved[5] = range::Range { start: 0x80002000, end: 0x80003000 }; |
|
| 293 | + | try! frames::initialize(&mut POOL, &machine); |
|
| 294 | + | let mut count: u32 = 0; |
|
| 295 | + | for i in 0..96 { |
|
| 296 | + | let start = 0x80000000 + i as u64 * 4096; |
|
| 297 | + | if platform::available(&machine, range::Range { start, end: start + 4096 }) { |
|
| 298 | + | assert count < POOL.count |
|
| 299 | + | and POOL.addresses[count] == start |
|
| 300 | + | and frames::available(&POOL, count); |
|
| 301 | + | set count += 1; |
|
| 302 | + | } |
|
| 303 | + | } |
|
| 304 | + | try testing::expect(count == POOL.count); |
|
| 305 | + | } |
kernel/kernel/tests/instances.rad
added
+200 -0
| 1 | + | //! Private package-state graphs over shared resident code. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use std::arch::rv64::shared; |
|
| 5 | + | use kernel::abi; |
|
| 6 | + | use kernel::frames; |
|
| 7 | + | use kernel::slots; |
|
| 8 | + | use kernel::registry; |
|
| 9 | + | use kernel::instances; |
|
| 10 | + | use kernel::physical; |
|
| 11 | + | ||
| 12 | + | /// Mapped storage for independent private graphs. |
|
| 13 | + | static RAM: [u64; 131072] = [0; 131072]; |
|
| 14 | + | /// Physical allocation metadata. |
|
| 15 | + | unsafe static POOL: frames::Pool = undefined; |
|
| 16 | + | /// Base of one package in a test instance's table. |
|
| 17 | + | fn baseOf(built: &instances::Instance, slot: u32) -> u64 { |
|
| 18 | + | match built { |
|
| 19 | + | case instances::Instance::Held(run, table) => return instances::base(table, slot), |
|
| 20 | + | } |
|
| 21 | + | } |
|
| 22 | + | ||
| 23 | + | /// Address of a test instance's table. |
|
| 24 | + | fn addressOf(built: &instances::Instance) -> u64 { |
|
| 25 | + | match built { |
|
| 26 | + | case instances::Instance::Held(run, table) => return instances::address(table), |
|
| 27 | + | } |
|
| 28 | + | } |
|
| 29 | + | ||
| 30 | + | /// Frames of a test instance. |
|
| 31 | + | fn framesOf(built: &instances::Instance) -> frames::Run { |
|
| 32 | + | match built { |
|
| 33 | + | case instances::Instance::Held(run, table) => return *run, |
|
| 34 | + | } |
|
| 35 | + | } |
|
| 36 | + | ||
| 37 | + | /// Size of a test instance's table token. |
|
| 38 | + | fn tableSize(built: &instances::Instance) -> u64 { |
|
| 39 | + | match built { |
|
| 40 | + | case instances::Instance::Held(run, table) => return physical::size(table), |
|
| 41 | + | } |
|
| 42 | + | } |
|
| 43 | + | ||
| 44 | + | /// Immutable package metadata shared by all instances. |
|
| 45 | + | unsafe static PACKAGES: registry::Store = undefined; |
|
| 46 | + | ||
| 47 | + | /// Initialize a frame pool over aligned mapped memory. |
|
| 48 | + | unsafe fn initialize() { |
|
| 49 | + | let pointer = &RAM[0]; |
|
| 50 | + | let base = pointer as u64; |
|
| 51 | + | let aligned = (base + 4095) & ~4095; |
|
| 52 | + | set POOL.count = (@sizeOf([u64; 131072]) - (aligned - base) as u32) / 4096; |
|
| 53 | + | for i in 0..POOL.count { |
|
| 54 | + | set POOL.addresses[i] = aligned + i as u64 * 4096; |
|
| 55 | + | set POOL.free[i / 64] |= 1 << (i % 64) as u64; |
|
| 56 | + | } |
|
| 57 | + | registry::initialize(&mut PACKAGES); |
|
| 58 | + | } |
|
| 59 | + | ||
| 60 | + | /// Publish a package with one private word and optional pointer relocation. |
|
| 61 | + | unsafe fn register( |
|
| 62 | + | name: *[u8], dependencies: *[*[u8]], relocations: *[shared::Relocation] |
|
| 63 | + | ) -> abi::Ref { |
|
| 64 | + | return registerLayout(name, dependencies, relocations, 8, 8); |
|
| 65 | + | } |
|
| 66 | + | ||
| 67 | + | /// Publish explicit private-state bounds for alignment and allocation checks. |
|
| 68 | + | unsafe fn registerLayout( |
|
| 69 | + | name: *[u8], |
|
| 70 | + | dependencies: *[*[u8]], |
|
| 71 | + | relocations: *[shared::Relocation], |
|
| 72 | + | size: u32, |
|
| 73 | + | alignment: u32 |
|
| 74 | + | ) -> abi::Ref { |
|
| 75 | + | let pending = try! registry::reserve(&mut PACKAGES); |
|
| 76 | + | let reference = slots::reference(&pending); |
|
| 77 | + | return try! registry::publish(&mut PACKAGES, pending, name, shared::Package { |
|
| 78 | + | name, |
|
| 79 | + | dependencies, |
|
| 80 | + | slot: reference.index, |
|
| 81 | + | codeAddress: 0x80000000, |
|
| 82 | + | code: &[0x00008067 as u32], |
|
| 83 | + | exports: &[], |
|
| 84 | + | entry: 0x80000000, |
|
| 85 | + | template: &[7, 0, 0, 0, 0, 0, 0, 0], |
|
| 86 | + | memory: size, |
|
| 87 | + | alignment, |
|
| 88 | + | relocations, |
|
| 89 | + | }); |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// Map an instance's private word through the original test allocation. |
|
| 93 | + | unsafe fn word(address: u64) -> *unsafe mut u64 { |
|
| 94 | + | let pointer = &RAM[0]; |
|
| 95 | + | return &mut RAM[((address - pointer as u64) / 8) as u32] as *unsafe mut u64; |
|
| 96 | + | } |
|
| 97 | + | ||
| 98 | + | /// Private initialization retains its graph across other registry and frame transactions. |
|
| 99 | + | @test unsafe fn stagedGraph() throws (testing::TestError) { |
|
| 100 | + | initialize(); |
|
| 101 | + | let root = register("root", &[], &[]); |
|
| 102 | + | let pending = try! instances::reserve(&PACKAGES, &mut POOL, root); |
|
| 103 | + | let later = register("later", &[], &[]); |
|
| 104 | + | let other = try! instances::create(&PACKAGES, &mut POOL, later); |
|
| 105 | + | set *word(baseOf(&other, later.index)) = 99; |
|
| 106 | + | try! instances::initialize(®istry::capture(&PACKAGES), &pending); |
|
| 107 | + | let first = instances::commit(pending); |
|
| 108 | + | assert baseOf(&first, root.index) <> 0 and baseOf(&first, later.index) == 0; |
|
| 109 | + | assert *word(baseOf(&first, root.index)) == 7; |
|
| 110 | + | assert *word(baseOf(&other, later.index)) == 99; |
|
| 111 | + | assert framesOf(&first).first + framesOf(&first).count <= framesOf(&other).first; |
|
| 112 | + | try! frames::release(&mut POOL, framesOf(&first)); |
|
| 113 | + | try! frames::release(&mut POOL, framesOf(&other)); |
|
| 114 | + | let cancelled = try! instances::reserve(&PACKAGES, &mut POOL, root); |
|
| 115 | + | instances::cancel(&mut POOL, cancelled); |
|
| 116 | + | for i in 0..POOL.count { |
|
| 117 | + | assert frames::available(&POOL, i); |
|
| 118 | + | } |
|
| 119 | + | } |
|
| 120 | + | ||
| 121 | + | /// A diamond has one dependency instance per domain and independent relocated pointers. |
|
| 122 | + | @test unsafe fn diamond() throws (testing::TestError) { |
|
| 123 | + | initialize(); |
|
| 124 | + | let base = register("base", &[], &[]); |
|
| 125 | + | unsafe static relocation: [shared::Relocation; 1] = undefined; |
|
| 126 | + | set relocation = [ |
|
| 127 | + | shared::Relocation { |
|
| 128 | + | offset: 0, count: 1, target: shared::DataRef { slot: base.index, offset: 0 } |
|
| 129 | + | }, |
|
| 130 | + | ]; |
|
| 131 | + | let left = register("left", &["base"], &relocation[..]); |
|
| 132 | + | let right = register("right", &["base"], &relocation[..]); |
|
| 133 | + | let root = register("root", &["left", "right"], &[]); |
|
| 134 | + | let first = try! instances::create(&PACKAGES, &mut POOL, root); |
|
| 135 | + | let second = try! instances::create(&PACKAGES, &mut POOL, root); |
|
| 136 | + | try testing::expect(tableSize(&first) == 256 * 8 and tableSize(&second) == 256 * 8); |
|
| 137 | + | try testing::expect(addressOf(&first) <> addressOf(&second)); |
|
| 138 | + | let firstBase = baseOf(&first, base.index); |
|
| 139 | + | let secondBase = baseOf(&second, base.index); |
|
| 140 | + | try testing::expect(firstBase <> secondBase); |
|
| 141 | + | try testing::expect(*word(baseOf(&first, left.index)) == firstBase); |
|
| 142 | + | try testing::expect(*word(baseOf(&first, right.index)) == firstBase); |
|
| 143 | + | try testing::expect(*word(baseOf(&second, left.index)) == secondBase); |
|
| 144 | + | set *word(firstBase) = 99; |
|
| 145 | + | try testing::expect(*word(secondBase) == 7); |
|
| 146 | + | try testing::expect(baseOf(&first, 4) == 0 and baseOf(&second, 4) == 0); |
|
| 147 | + | try! frames::release(&mut POOL, framesOf(&first)); |
|
| 148 | + | try! frames::release(&mut POOL, framesOf(&second)); |
|
| 149 | + | for i in 0..POOL.count { |
|
| 150 | + | assert frames::available(&POOL, i); |
|
| 151 | + | } |
|
| 152 | + | } |
|
| 153 | + | ||
| 154 | + | /// State alignment can exceed a physical frame and zero-fill follows the template. |
|
| 155 | + | @test unsafe fn alignment() throws (testing::TestError) { |
|
| 156 | + | initialize(); |
|
| 157 | + | let root = registerLayout("aligned", &[], &[], 16, 8192); |
|
| 158 | + | let state = try! instances::create(&PACKAGES, &mut POOL, root); |
|
| 159 | + | let stateBase = baseOf(&state, root.index); |
|
| 160 | + | try testing::expect((stateBase & 8191) == 0); |
|
| 161 | + | try testing::expect(*word(stateBase) == 7); |
|
| 162 | + | try testing::expect(*word(stateBase + 8) == 0); |
|
| 163 | + | try! frames::release(&mut POOL, framesOf(&state)); |
|
| 164 | + | } |
|
| 165 | + | ||
| 166 | + | /// Failed allocation and private relocation leave no retained instance frames. |
|
| 167 | + | @test unsafe fn rollback() throws (testing::TestError) { |
|
| 168 | + | initialize(); |
|
| 169 | + | let root = register("valid", &[], &[]); |
|
| 170 | + | let count = POOL.count; |
|
| 171 | + | set POOL.count = 0; |
|
| 172 | + | let mut failures: u32 = 0; |
|
| 173 | + | try instances::create(&PACKAGES, &mut POOL, root) catch err { |
|
| 174 | + | try testing::expect(err == abi::Error::OutOfMemory); |
|
| 175 | + | set failures += 1; |
|
| 176 | + | }; |
|
| 177 | + | set POOL.count = count; |
|
| 178 | + | let bad = register("bad", &[], &[ |
|
| 179 | + | shared::Relocation { |
|
| 180 | + | offset: 0, count: 1, target: shared::DataRef { slot: 5, offset: 0 } |
|
| 181 | + | }, |
|
| 182 | + | ]); |
|
| 183 | + | try instances::create(&PACKAGES, &mut POOL, bad) catch err { |
|
| 184 | + | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 185 | + | set failures += 1; |
|
| 186 | + | }; |
|
| 187 | + | try instances::create(&PACKAGES, &mut POOL, abi::Ref { |
|
| 188 | + | index: root.index, generation: 2 |
|
| 189 | + | }) catch err { |
|
| 190 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 191 | + | set failures += 1; |
|
| 192 | + | }; |
|
| 193 | + | try testing::expect(failures == 3); |
|
| 194 | + | for i in 0..POOL.count { |
|
| 195 | + | assert frames::available(&POOL, i); |
|
| 196 | + | } |
|
| 197 | + | let retry = try! instances::create(&PACKAGES, &mut POOL, root); |
|
| 198 | + | try testing::expect(*word(baseOf(&retry, root.index)) == 7); |
|
| 199 | + | try! frames::release(&mut POOL, framesOf(&retry)); |
|
| 200 | + | } |
kernel/kernel/tests/interrupts.rad
added
+132 -0
| 1 | + | //! Exclusive source ownership, deferred notification, and rearming. |
|
| 2 | + | use std::testing; |
|
| 3 | + | use kernel::abi; |
|
| 4 | + | use kernel::slots; |
|
| 5 | + | use kernel::capability; |
|
| 6 | + | use kernel::domains; |
|
| 7 | + | use kernel::events; |
|
| 8 | + | use kernel::interrupts; |
|
| 9 | + | use kernel::limits; |
|
| 10 | + | ||
| 11 | + | /// Physical source metadata under test. |
|
| 12 | + | unsafe static STORE: interrupts::Store = undefined; |
|
| 13 | + | /// Receiver lifetimes and trusted event metadata. |
|
| 14 | + | unsafe static DOMAINS: domains::Store = undefined; |
|
| 15 | + | /// Source domain capability table and ring. |
|
| 16 | + | unsafe static SOURCE: domains::Memory = undefined; |
|
| 17 | + | /// Target domain capability table and ring. |
|
| 18 | + | unsafe static TARGET: domains::Memory = undefined; |
|
| 19 | + | ||
| 20 | + | /// Initialize two live receivers and eight physical sources. |
|
| 21 | + | unsafe fn initialize() { |
|
| 22 | + | interrupts::initialize(&mut STORE, 8); |
|
| 23 | + | domains::initialize(&mut DOMAINS); |
|
| 24 | + | open(&mut SOURCE); |
|
| 25 | + | open(&mut TARGET); |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | /// Publish one receiver backed by statically retained test memory. |
|
| 29 | + | unsafe fn open(memory: &mut domains::Memory) { |
|
| 30 | + | let pending = try! slots::reserve(&mut DOMAINS.slots[..]); |
|
| 31 | + | let owner = try! slots::commit(&mut DOMAINS.slots[..], pending); |
|
| 32 | + | capability::initialize(&mut memory.table, owner); |
|
| 33 | + | set DOMAINS.records[owner.index].state = domains::Lifecycle::Active; |
|
| 34 | + | set DOMAINS.records[owner.index].memory = &mut *memory as *unsafe mut domains::Memory; |
|
| 35 | + | try! events::open(&mut DOMAINS.events, owner, &mut memory.ring); |
|
| 36 | + | } |
|
| 37 | + | ||
| 38 | + | /// Transfer preserves queued events and moves pending service to the new owner. |
|
| 39 | + | @test unsafe fn ownership() throws (testing::TestError) { |
|
| 40 | + | initialize(); |
|
| 41 | + | let first = try! interrupts::seed(&mut STORE, &mut SOURCE.table, 5); |
|
| 42 | + | assert try! interrupts::query(&STORE, &SOURCE.table, first) == 5; |
|
| 43 | + | assert interrupts::claim(&mut STORE, 5); |
|
| 44 | + | assert not try! interrupts::service(&mut STORE, &mut DOMAINS, 5); |
|
| 45 | + | assert STORE.records[4].state == interrupts::State::Queued; |
|
| 46 | + | let second = try! interrupts::transfer( |
|
| 47 | + | &mut STORE, &mut SOURCE.table, &mut TARGET.table, first, (abi::READ | abi::TRANSFER) as u64 |
|
| 48 | + | ); |
|
| 49 | + | assert STORE.records[4].state == interrupts::State::Pending; |
|
| 50 | + | assert not try! interrupts::service(&mut STORE, &mut DOMAINS, 5); |
|
| 51 | + | assert DOMAINS.events.queues[0].tail == 1 and DOMAINS.events.queues[1].tail == 1; |
|
| 52 | + | let returned = try! interrupts::transfer( |
|
| 53 | + | &mut STORE, &mut TARGET.table, &mut SOURCE.table, second, (abi::READ | abi::TRANSFER) as u64 |
|
| 54 | + | ); |
|
| 55 | + | assert not try! interrupts::service(&mut STORE, &mut DOMAINS, 5); |
|
| 56 | + | assert DOMAINS.events.queues[0].tail == 1; |
|
| 57 | + | let consumed = try! events::pop(&mut SOURCE.ring) else panic "ownership: missing source event"; |
|
| 58 | + | assert consumed.kind == events::INTERRUPT and consumed.code == 5; |
|
| 59 | + | assert try! interrupts::service(&mut STORE, &mut DOMAINS, 5); |
|
| 60 | + | assert STORE.records[4].state == interrupts::State::Idle; |
|
| 61 | + | let old = try! capability::get(&SOURCE.table, returned); |
|
| 62 | + | try! interrupts::drop(&mut STORE, &mut SOURCE.table, returned); |
|
| 63 | + | assert not interrupts::claim(&mut STORE, 5); |
|
| 64 | + | let replacement = try! interrupts::seed(&mut STORE, &mut SOURCE.table, 5); |
|
| 65 | + | let fresh = try! capability::get(&SOURCE.table, replacement); |
|
| 66 | + | assert fresh.object.index == old.object.index |
|
| 67 | + | and fresh.object.generation <> old.object.generation; |
|
| 68 | + | } |
|
| 69 | + | ||
| 70 | + | /// A full event queue retains the source until capacity and consumer progress permit rearming. |
|
| 71 | + | @test unsafe fn saturation() throws (testing::TestError) { |
|
| 72 | + | initialize(); |
|
| 73 | + | let handle = try! interrupts::seed(&mut STORE, &mut SOURCE.table, 3); |
|
| 74 | + | let owner = SOURCE.table.owner; |
|
| 75 | + | for i in 0..events::ORDINARY { |
|
| 76 | + | assert try! events::send(&mut DOMAINS.events, owner, &mut SOURCE.ring, events::Event { |
|
| 77 | + | kind: events::WAKEUP, reserved: 0, code: i, value: 0 |
|
| 78 | + | }); |
|
| 79 | + | } |
|
| 80 | + | assert interrupts::claim(&mut STORE, 3); |
|
| 81 | + | assert not try! interrupts::service(&mut STORE, &mut DOMAINS, 3); |
|
| 82 | + | assert STORE.records[2].state == interrupts::State::Pending; |
|
| 83 | + | assert interrupts::claim(&mut STORE, 3); |
|
| 84 | + | let freed = try! events::pop(&mut SOURCE.ring) else panic "saturation: full ring"; |
|
| 85 | + | assert freed.kind == events::WAKEUP; |
|
| 86 | + | assert not try! interrupts::service(&mut STORE, &mut DOMAINS, 3); |
|
| 87 | + | assert STORE.records[2].state == interrupts::State::Queued; |
|
| 88 | + | for i in 1..events::ORDINARY { |
|
| 89 | + | let event = try! events::pop(&mut SOURCE.ring) else panic "saturation: retained wakeup"; |
|
| 90 | + | assert event.kind == events::WAKEUP; |
|
| 91 | + | } |
|
| 92 | + | assert not try! interrupts::service(&mut STORE, &mut DOMAINS, 3); |
|
| 93 | + | let event = try! events::pop(&mut SOURCE.ring) else panic "saturation: retained interrupt"; |
|
| 94 | + | assert event.code == 3 and event.kind == events::INTERRUPT; |
|
| 95 | + | assert try! interrupts::service(&mut STORE, &mut DOMAINS, 3); |
|
| 96 | + | set DOMAINS.records[owner.index].state = domains::Lifecycle::Dead; |
|
| 97 | + | assert interrupts::claim(&mut STORE, 3); |
|
| 98 | + | assert not try! interrupts::service(&mut STORE, &mut DOMAINS, 3); |
|
| 99 | + | } |
|
| 100 | + | ||
| 101 | + | /// Target exhaustion leaves the source handle and retained notification unchanged. |
|
| 102 | + | @test unsafe fn atomicTransfer() throws (testing::TestError) { |
|
| 103 | + | initialize(); |
|
| 104 | + | let handle = try! interrupts::seed(&mut STORE, &mut SOURCE.table, 7); |
|
| 105 | + | assert interrupts::claim(&mut STORE, 7); |
|
| 106 | + | for i in 0..limits::HANDLES { |
|
| 107 | + | try! capability::install(&mut TARGET.table, capability::Entry { |
|
| 108 | + | kind: abi::Kind::Domain, object: SOURCE.table.owner, rights: abi::Rights(0) |
|
| 109 | + | }); |
|
| 110 | + | } |
|
| 111 | + | let mut full = false; |
|
| 112 | + | try interrupts::transfer( |
|
| 113 | + | &mut STORE, &mut SOURCE.table, &mut TARGET.table, handle, abi::READ as u64 |
|
| 114 | + | ) catch error { |
|
| 115 | + | assert error == abi::Error::InvalidArg; |
|
| 116 | + | set full = true; |
|
| 117 | + | }; |
|
| 118 | + | assert full; |
|
| 119 | + | assert STORE.records[6].owner == SOURCE.table.owner and STORE.records[6].handle == handle; |
|
| 120 | + | assert STORE.records[6].state == interrupts::State::Pending; |
|
| 121 | + | assert try! interrupts::query(&STORE, &SOURCE.table, handle) == 7; |
|
| 122 | + | let mut invalid = false; |
|
| 123 | + | try interrupts::seed(&mut STORE, &mut SOURCE.table, 0) catch error { |
|
| 124 | + | assert error == abi::Error::InvalidArg; |
|
| 125 | + | set invalid = true; |
|
| 126 | + | }; |
|
| 127 | + | assert invalid |
|
| 128 | + | and not interrupts::claim(&mut STORE, 0) |
|
| 129 | + | and not interrupts::claim( |
|
| 130 | + | &mut STORE, 9 |
|
| 131 | + | ); |
|
| 132 | + | } |
kernel/kernel/tests/lifecycle.rad
added
+499 -0
| 1 | + | //! Terminal delivery, receiver eligibility, and retained full-queue events. |
|
| 2 | + | use std::testing; |
|
| 3 | + | use kernel::state; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::slots; |
|
| 6 | + | use kernel::domains; |
|
| 7 | + | use kernel::budgets; |
|
| 8 | + | use kernel::events; |
|
| 9 | + | use kernel::lifecycle; |
|
| 10 | + | use kernel::registry; |
|
| 11 | + | use kernel::loader; |
|
| 12 | + | use kernel::limits; |
|
| 13 | + | use kernel::remote; |
|
| 14 | + | use kernel::pages; |
|
| 15 | + | use kernel::backing; |
|
| 16 | + | use kernel::capability; |
|
| 17 | + | use kernel::frames; |
|
| 18 | + | use kernel::physical; |
|
| 19 | + | use kernel::interrupts; |
|
| 20 | + | ||
| 21 | + | /// Receiver generations, contexts, and terminal events. |
|
| 22 | + | unsafe static STORE: domains::Store = undefined; |
|
| 23 | + | /// CPU authority retained by receivers. |
|
| 24 | + | unsafe static WINDOWS: budgets::Store = undefined; |
|
| 25 | + | /// Queued calls and acknowledgements retained by stopped contexts. |
|
| 26 | + | unsafe static REQUESTS: remote::Store = undefined; |
|
| 27 | + | /// Physical allocation metadata used for interrupted page calls. |
|
| 28 | + | unsafe static MEMORY: pages::Store = undefined; |
|
| 29 | + | /// Interrupt ownership passed to terminal identity reaping. |
|
| 30 | + | unsafe static IRQS: interrupts::Store = undefined; |
|
| 31 | + | /// Mapped frames available to interrupted page calls. |
|
| 32 | + | static RAM: [u64; 4096] = [0; 4096]; |
|
| 33 | + | /// Reservations retained across private initialization. |
|
| 34 | + | unsafe static CALLS: lifecycle::Calls = undefined; |
|
| 35 | + | /// Shared queue storage for the first receiver. |
|
| 36 | + | unsafe static FIRST: domains::Memory = undefined; |
|
| 37 | + | /// Shared queue storage for the second receiver. |
|
| 38 | + | unsafe static SECOND: domains::Memory = undefined; |
|
| 39 | + | ||
| 40 | + | /// Open one receiver with a waiting initial context. |
|
| 41 | + | unsafe fn open(index: u32, memory: &mut domains::Memory) { |
|
| 42 | + | let owner = abi::Ref { index, generation: 1 }; |
|
| 43 | + | set STORE.slots[index] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 44 | + | set STORE.records[index].state = domains::Lifecycle::Active; |
|
| 45 | + | set STORE.records[index].memory = &mut *memory as *unsafe mut domains::Memory; |
|
| 46 | + | set STORE.contextSlots[index] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 47 | + | set STORE.contexts[index].owner = owner; |
|
| 48 | + | set STORE.contexts[index].state = domains::ContextState::Waiting; |
|
| 49 | + | set STORE.contexts[index].hart = nil; |
|
| 50 | + | set STORE.contexts[index].pending = nil; |
|
| 51 | + | try! events::open(&mut STORE.events, owner, &mut memory.ring); |
|
| 52 | + | } |
|
| 53 | + | ||
| 54 | + | /// Initialize two independent receivers and one future window on hart one. |
|
| 55 | + | unsafe fn initialize() { |
|
| 56 | + | domains::initialize(&mut STORE); |
|
| 57 | + | budgets::initialize(&mut WINDOWS); |
|
| 58 | + | remote::initialize(&mut REQUESTS, 3); |
|
| 59 | + | open(0, &mut FIRST); |
|
| 60 | + | open(1, &mut SECOND); |
|
| 61 | + | let owner = abi::Ref { index: 0, generation: 1 }; |
|
| 62 | + | set WINDOWS.slots[0] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 63 | + | set WINDOWS.windows[0] = budgets::Window { |
|
| 64 | + | hart: 1, start: 30, end: 100, owner, handle: abi::Handle(0), context: owner |
|
| 65 | + | }; |
|
| 66 | + | } |
|
| 67 | + | ||
| 68 | + | /// Retain a normal exit for a chosen receiver. |
|
| 69 | + | unsafe fn retain(index: u32, receiver: u32) { |
|
| 70 | + | let pending = try! events::reserve(&mut STORE.events, abi::Ref { index, generation: 1 }); |
|
| 71 | + | events::finish( |
|
| 72 | + | &mut STORE.events, |
|
| 73 | + | pending, |
|
| 74 | + | abi::Ref { index: receiver, generation: 1 }, |
|
| 75 | + | events::CHILD_EXIT, |
|
| 76 | + | index |
|
| 77 | + | ); |
|
| 78 | + | } |
|
| 79 | + | ||
| 80 | + | /// Dead page callers release unpublished capacity only after hart ownership ends. |
|
| 81 | + | @test unsafe fn cancelPage() throws (testing::TestError) { |
|
| 82 | + | initialize(); |
|
| 83 | + | lifecycle::initialize(&mut CALLS); |
|
| 84 | + | backing::initialize(&mut MEMORY.backings); |
|
| 85 | + | slots::initialize(&mut MEMORY.slots[..]); |
|
| 86 | + | let owner = abi::Ref { index: 0, generation: 1 }; |
|
| 87 | + | try! backing::registerDomain(&mut MEMORY.backings, owner); |
|
| 88 | + | capability::initialize(&mut FIRST.table, owner); |
|
| 89 | + | let authority = try! capability::install(&mut FIRST.table, capability::Entry { |
|
| 90 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::ALLOCATE) |
|
| 91 | + | }); |
|
| 92 | + | let base = (&RAM[0] as u64 + 4095) & ~4095; |
|
| 93 | + | set MEMORY.backings.pool.count = 2; |
|
| 94 | + | for i in 0..2 { |
|
| 95 | + | set MEMORY.backings.pool.addresses[i] = base + i as u64 * 4096; |
|
| 96 | + | set MEMORY.backings.pool.free[i / 64] |= 1 << (i % 64) as u64; |
|
| 97 | + | } |
|
| 98 | + | let reservation = try! pages::reserve(&mut MEMORY, &mut FIRST.table, authority, 2); |
|
| 99 | + | match lifecycle::holdPage(&mut CALLS, &mut STORE, owner, reservation) { |
|
| 100 | + | case lifecycle::PageHeld::Held(held, stranded) => { |
|
| 101 | + | assert STORE.contexts[0].reservation; |
|
| 102 | + | physical::clear(&stranded); |
|
| 103 | + | lifecycle::cancelAllocations( |
|
| 104 | + | &mut CALLS, |
|
| 105 | + | &mut STORE, |
|
| 106 | + | &mut MEMORY, |
|
| 107 | + | &mut state::SHARED.loader, |
|
| 108 | + | &mut state::SHARED.registry |
|
| 109 | + | ); |
|
| 110 | + | assert not frames::available(&MEMORY.backings.pool, 0) |
|
| 111 | + | and not frames::available(&MEMORY.backings.pool, 1); |
|
| 112 | + | set STORE.records[0].state = domains::Lifecycle::Dead; |
|
| 113 | + | set STORE.contexts[0].state = domains::ContextState::Stopped; |
|
| 114 | + | set STORE.contexts[0].hart = 0; |
|
| 115 | + | lifecycle::cancelAllocations( |
|
| 116 | + | &mut CALLS, |
|
| 117 | + | &mut STORE, |
|
| 118 | + | &mut MEMORY, |
|
| 119 | + | &mut state::SHARED.loader, |
|
| 120 | + | &mut state::SHARED.registry |
|
| 121 | + | ); |
|
| 122 | + | assert not frames::available(&MEMORY.backings.pool, 0); |
|
| 123 | + | // Model the end of the stopped continuation before hart release. |
|
| 124 | + | assert lifecycle::leaseContext(held) == owner; |
|
| 125 | + | set STORE.contexts[0].hart = nil; |
|
| 126 | + | lifecycle::cancelAllocations( |
|
| 127 | + | &mut CALLS, |
|
| 128 | + | &mut STORE, |
|
| 129 | + | &mut MEMORY, |
|
| 130 | + | &mut state::SHARED.loader, |
|
| 131 | + | &mut state::SHARED.registry |
|
| 132 | + | ); |
|
| 133 | + | assert MEMORY.backings.pool.retiredCount == 1 |
|
| 134 | + | and not frames::available(&MEMORY.backings.pool, 0); |
|
| 135 | + | assert frames::reclaim(&mut MEMORY.backings.pool, 256) == 2; |
|
| 136 | + | assert frames::available(&MEMORY.backings.pool, 0) |
|
| 137 | + | and frames::available(&MEMORY.backings.pool, 1); |
|
| 138 | + | assert MEMORY.slots[0].state == slots::State::Free; |
|
| 139 | + | assert MEMORY.backings.slots[0].state == slots::State::Free; |
|
| 140 | + | assert FIRST.table.slots[1].state == slots::State::Free and CALLS.active[0] == 0; |
|
| 141 | + | assert not STORE.contexts[0].reservation; |
|
| 142 | + | } |
|
| 143 | + | } |
|
| 144 | + | set STORE.records[0].state = domains::Lifecycle::Active; |
|
| 145 | + | let next = try! pages::reserve(&mut MEMORY, &mut FIRST.table, authority, 1); |
|
| 146 | + | match lifecycle::holdPage(&mut CALLS, &mut STORE, owner, next) { |
|
| 147 | + | case lifecycle::PageHeld::Held(lease, access) => { |
|
| 148 | + | physical::clear(&access); |
|
| 149 | + | let ready = lifecycle::takePage(&mut CALLS, &mut STORE, lease, access); |
|
| 150 | + | let handle = pages::publish(&mut MEMORY, &mut FIRST.table, ready); |
|
| 151 | + | assert CALLS.active[0] == 0; |
|
| 152 | + | let entry = try! capability::get(&FIRST.table, handle); |
|
| 153 | + | assert (try! pages::get(&MEMORY, entry.object)).count == 1; |
|
| 154 | + | } |
|
| 155 | + | } |
|
| 156 | + | } |
|
| 157 | + | ||
| 158 | + | /// A token pairs only with the allocation it was derived for, so a lease cannot take back another |
|
| 159 | + | /// call's token. |
|
| 160 | + | @test unsafe fn tokenPairing() throws (testing::TestError) { |
|
| 161 | + | initialize(); |
|
| 162 | + | backing::initialize(&mut MEMORY.backings); |
|
| 163 | + | slots::initialize(&mut MEMORY.slots[..]); |
|
| 164 | + | let owner = abi::Ref { index: 0, generation: 1 }; |
|
| 165 | + | try! backing::registerDomain(&mut MEMORY.backings, owner); |
|
| 166 | + | capability::initialize(&mut FIRST.table, owner); |
|
| 167 | + | let authority = try! capability::install(&mut FIRST.table, capability::Entry { |
|
| 168 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::ALLOCATE) |
|
| 169 | + | }); |
|
| 170 | + | let base = (&RAM[0] as u64 + 4095) & ~4095; |
|
| 171 | + | set MEMORY.backings.pool.count = 3; |
|
| 172 | + | for i in 0..3 { |
|
| 173 | + | set MEMORY.backings.pool.addresses[i] = base + i as u64 * 4096; |
|
| 174 | + | set MEMORY.backings.pool.free[i / 64] |= 1 << (i % 64) as u64; |
|
| 175 | + | } |
|
| 176 | + | let single = try! pages::reserve(&mut MEMORY, &mut FIRST.table, authority, 1); |
|
| 177 | + | let double = try! pages::reserve(&mut MEMORY, &mut FIRST.table, authority, 2); |
|
| 178 | + | match &single { |
|
| 179 | + | case pages::Reservation::Held(one, oneAccess) => match &double { |
|
| 180 | + | case pages::Reservation::Held(two, twoAccess) => { |
|
| 181 | + | assert pages::covers(one, oneAccess) and pages::covers(two, twoAccess); |
|
| 182 | + | assert not pages::covers(one, twoAccess) and not pages::covers(two, oneAccess); |
|
| 183 | + | } |
|
| 184 | + | }, |
|
| 185 | + | } |
|
| 186 | + | pages::cancel(&mut MEMORY, &mut FIRST.table, double); |
|
| 187 | + | pages::cancel(&mut MEMORY, &mut FIRST.table, single); |
|
| 188 | + | } |
|
| 189 | + | ||
| 190 | + | /// One cancellation pass retires at most one caller's private allocation. |
|
| 191 | + | @test unsafe fn cancellationBatch() throws (testing::TestError) { |
|
| 192 | + | initialize(); |
|
| 193 | + | lifecycle::initialize(&mut CALLS); |
|
| 194 | + | backing::initialize(&mut MEMORY.backings); |
|
| 195 | + | slots::initialize(&mut MEMORY.slots[..]); |
|
| 196 | + | let owner = abi::Ref { index: 0, generation: 1 }; |
|
| 197 | + | let second = abi::Ref { index: 1, generation: 1 }; |
|
| 198 | + | set STORE.contexts[1].owner = owner; |
|
| 199 | + | try! backing::registerDomain(&mut MEMORY.backings, owner); |
|
| 200 | + | capability::initialize(&mut FIRST.table, owner); |
|
| 201 | + | let authority = try! capability::install(&mut FIRST.table, capability::Entry { |
|
| 202 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::ALLOCATE) |
|
| 203 | + | }); |
|
| 204 | + | let base = (&RAM[0] as u64 + 4095) & ~4095; |
|
| 205 | + | set MEMORY.backings.pool.count = 2; |
|
| 206 | + | for i in 0..2 { |
|
| 207 | + | set MEMORY.backings.pool.addresses[i] = base + i as u64 * 4096; |
|
| 208 | + | set MEMORY.backings.pool.free[i / 64] |= 1 << (i % 64) as u64; |
|
| 209 | + | } |
|
| 210 | + | let firstPage = try! pages::reserve(&mut MEMORY, &mut FIRST.table, authority, 1); |
|
| 211 | + | match lifecycle::holdPage(&mut CALLS, &mut STORE, owner, firstPage) { |
|
| 212 | + | case lifecycle::PageHeld::Held(first, firstAccess) => { |
|
| 213 | + | let secondPage = try! pages::reserve(&mut MEMORY, &mut FIRST.table, authority, 1); |
|
| 214 | + | match lifecycle::holdPage(&mut CALLS, &mut STORE, second, secondPage) { |
|
| 215 | + | case lifecycle::PageHeld::Held(other, otherAccess) => { |
|
| 216 | + | assert lifecycle::leaseContext(first) == owner; |
|
| 217 | + | assert lifecycle::leaseContext(other) == second; |
|
| 218 | + | set STORE.records[0].state = domains::Lifecycle::Dead; |
|
| 219 | + | set STORE.contexts[0].state = domains::ContextState::Stopped; |
|
| 220 | + | set STORE.contexts[1].state = domains::ContextState::Stopped; |
|
| 221 | + | assert lifecycle::cancelAllocations( |
|
| 222 | + | &mut CALLS, |
|
| 223 | + | &mut STORE, |
|
| 224 | + | &mut MEMORY, |
|
| 225 | + | &mut state::SHARED.loader, |
|
| 226 | + | &mut state::SHARED.registry |
|
| 227 | + | ); |
|
| 228 | + | assert CALLS.active[0] == 2 and MEMORY.backings.pool.retiredCount == 1; |
|
| 229 | + | assert not STORE.contexts[0].reservation and STORE.contexts[1].reservation; |
|
| 230 | + | assert lifecycle::cancelAllocations( |
|
| 231 | + | &mut CALLS, |
|
| 232 | + | &mut STORE, |
|
| 233 | + | &mut MEMORY, |
|
| 234 | + | &mut state::SHARED.loader, |
|
| 235 | + | &mut state::SHARED.registry |
|
| 236 | + | ); |
|
| 237 | + | assert CALLS.active[0] == 0 and MEMORY.backings.pool.retiredCount == 2; |
|
| 238 | + | assert not lifecycle::cancelAllocations( |
|
| 239 | + | &mut CALLS, |
|
| 240 | + | &mut STORE, |
|
| 241 | + | &mut MEMORY, |
|
| 242 | + | &mut state::SHARED.loader, |
|
| 243 | + | &mut state::SHARED.registry |
|
| 244 | + | ); |
|
| 245 | + | assert not frames::available(&MEMORY.backings.pool, 0) |
|
| 246 | + | and not frames::available(&MEMORY.backings.pool, 1); |
|
| 247 | + | assert frames::reclaim(&mut MEMORY.backings.pool, 1) == 1; |
|
| 248 | + | assert frames::reclaim(&mut MEMORY.backings.pool, 1) == 1; |
|
| 249 | + | assert MEMORY.backings.pool.retiredCount == 0; |
|
| 250 | + | } |
|
| 251 | + | } |
|
| 252 | + | } |
|
| 253 | + | } |
|
| 254 | + | } |
|
| 255 | + | ||
| 256 | + | /// A context call belongs to its caller even when its target domain dies. |
|
| 257 | + | @test unsafe fn cancelContext() throws (testing::TestError) { |
|
| 258 | + | initialize(); |
|
| 259 | + | lifecycle::initialize(&mut CALLS); |
|
| 260 | + | backing::initialize(&mut MEMORY.backings); |
|
| 261 | + | let caller = abi::Ref { index: 0, generation: 1 }; |
|
| 262 | + | let target = abi::Ref { index: 1, generation: 1 }; |
|
| 263 | + | let base = (&RAM[0] as u64 + 4095) & ~4095; |
|
| 264 | + | set MEMORY.backings.pool.count = 1; |
|
| 265 | + | set MEMORY.backings.pool.addresses[0] = base; |
|
| 266 | + | set MEMORY.backings.pool.free[0] |= 1; |
|
| 267 | + | let slot = try! slots::reserve(&mut STORE.contextSlots[..]); |
|
| 268 | + | let object = slots::reference(&slot); |
|
| 269 | + | let held = try! frames::reserve(&mut MEMORY.backings.pool, 1); |
|
| 270 | + | let access = physical::reserved(&MEMORY.backings.pool, &held); |
|
| 271 | + | let run = frames::commit(held); |
|
| 272 | + | set STORE.contexts[object.index].owner = target; |
|
| 273 | + | set STORE.contexts[object.index].kernelFrames = run; |
|
| 274 | + | set STORE.contexts[object.index].hart = nil; |
|
| 275 | + | match slot { |
|
| 276 | + | case slots::Reservation::Held(id) => { |
|
| 277 | + | assert id == object; |
|
| 278 | + | } |
|
| 279 | + | } |
|
| 280 | + | let pending = domains::ContextReservation::Held( |
|
| 281 | + | domains::ContextAllocation { |
|
| 282 | + | object, owner: target, frames: run, base |
|
| 283 | + | }, |
|
| 284 | + | access |
|
| 285 | + | ); |
|
| 286 | + | match lifecycle::holdContext(&mut CALLS, &mut STORE, caller, pending) { |
|
| 287 | + | case lifecycle::ContextHeld::Held(lease, stranded) => { |
|
| 288 | + | assert lifecycle::contextTarget(&CALLS, &lease) == target; |
|
| 289 | + | physical::clear(&stranded); |
|
| 290 | + | set STORE.records[1].state = domains::Lifecycle::Dead; |
|
| 291 | + | lifecycle::cancelAllocations( |
|
| 292 | + | &mut CALLS, |
|
| 293 | + | &mut STORE, |
|
| 294 | + | &mut MEMORY, |
|
| 295 | + | &mut state::SHARED.loader, |
|
| 296 | + | &mut state::SHARED.registry |
|
| 297 | + | ); |
|
| 298 | + | assert STORE.contexts[0].reservation and not frames::available( |
|
| 299 | + | &MEMORY.backings.pool, 0 |
|
| 300 | + | ); |
|
| 301 | + | set STORE.records[0].state = domains::Lifecycle::Dead; |
|
| 302 | + | set STORE.contexts[0].state = domains::ContextState::Stopped; |
|
| 303 | + | set STORE.contexts[0].hart = 1; |
|
| 304 | + | lifecycle::cancelAllocations( |
|
| 305 | + | &mut CALLS, |
|
| 306 | + | &mut STORE, |
|
| 307 | + | &mut MEMORY, |
|
| 308 | + | &mut state::SHARED.loader, |
|
| 309 | + | &mut state::SHARED.registry |
|
| 310 | + | ); |
|
| 311 | + | assert STORE.contextSlots[object.index].state == slots::State::Reserved; |
|
| 312 | + | assert lifecycle::leaseContext(lease) == caller; |
|
| 313 | + | set STORE.contexts[0].hart = nil; |
|
| 314 | + | lifecycle::cancelAllocations( |
|
| 315 | + | &mut CALLS, |
|
| 316 | + | &mut STORE, |
|
| 317 | + | &mut MEMORY, |
|
| 318 | + | &mut state::SHARED.loader, |
|
| 319 | + | &mut state::SHARED.registry |
|
| 320 | + | ); |
|
| 321 | + | assert MEMORY.backings.pool.retiredCount == 1 |
|
| 322 | + | and not frames::available(&MEMORY.backings.pool, 0); |
|
| 323 | + | assert frames::reclaim(&mut MEMORY.backings.pool, 256) == 1; |
|
| 324 | + | assert frames::available(&MEMORY.backings.pool, 0); |
|
| 325 | + | assert STORE.contextSlots[object.index].state == slots::State::Free; |
|
| 326 | + | assert not STORE.contexts[0].reservation and CALLS.active[0] == 0; |
|
| 327 | + | } |
|
| 328 | + | } |
|
| 329 | + | } |
|
| 330 | + | ||
| 331 | + | /// Dead contexts retain requests until hart release and completion of taken work. |
|
| 332 | + | @test unsafe fn cancelRequests() throws (testing::TestError) { |
|
| 333 | + | initialize(); |
|
| 334 | + | let context = abi::Ref { index: 0, generation: 1 }; |
|
| 335 | + | let pending = try! remote::send(&mut REQUESTS, 0, 1, remote::Action::Ready(context)); |
|
| 336 | + | let other = try! remote::send(&mut REQUESTS, 0, 1, remote::Action::Ready(context)); |
|
| 337 | + | let delivered = try! remote::take(&mut REQUESTS, 1) |
|
| 338 | + | else panic "cancelRequests: missing readiness request"; |
|
| 339 | + | assert delivered.id == pending; |
|
| 340 | + | set STORE.contexts[0].pending = other; |
|
| 341 | + | lifecycle::cancelRequests(&mut STORE, &mut REQUESTS); |
|
| 342 | + | assert REQUESTS.slots[other.index].state == slots::State::Live; |
|
| 343 | + | set STORE.records[0].state = domains::Lifecycle::Dead; |
|
| 344 | + | set STORE.contexts[0].state = domains::ContextState::Stopped; |
|
| 345 | + | set STORE.contexts[0].hart = 0; |
|
| 346 | + | lifecycle::cancelRequests(&mut STORE, &mut REQUESTS); |
|
| 347 | + | assert REQUESTS.slots[other.index].state == slots::State::Live; |
|
| 348 | + | set STORE.contexts[0].hart = nil; |
|
| 349 | + | lifecycle::cancelRequests(&mut STORE, &mut REQUESTS); |
|
| 350 | + | assert REQUESTS.slots[other.index].state == slots::State::Free; |
|
| 351 | + | assert REQUESTS.slots[pending.index].state == slots::State::Live; |
|
| 352 | + | assert STORE.contexts[0].pending == pending; |
|
| 353 | + | assert remote::acknowledge(&mut REQUESTS, 1, pending); |
|
| 354 | + | lifecycle::cancelRequests(&mut STORE, &mut REQUESTS); |
|
| 355 | + | assert STORE.contexts[0].pending == nil |
|
| 356 | + | and REQUESTS.slots[pending.index].state == slots::State::Free; |
|
| 357 | + | assert try! remote::take(&mut REQUESTS, 1) == nil; |
|
| 358 | + | assert REQUESTS.queues[1].count == 0; |
|
| 359 | + | let budget = try! remote::send(&mut REQUESTS, 0, 1, remote::Action::Budget(remote::Call { |
|
| 360 | + | owner: context, context, operation: 70, arguments: [0; 4] |
|
| 361 | + | })); |
|
| 362 | + | set STORE.contexts[0].pending = budget; |
|
| 363 | + | let work = try! remote::take(&mut REQUESTS, 1) |
|
| 364 | + | else panic "cancelRequests: missing budget request"; |
|
| 365 | + | remote::respond(&mut REQUESTS, 1, work.id, remote::Reply::Error(abi::Error::BadHandle)); |
|
| 366 | + | assert remote::acknowledge(&mut REQUESTS, 1, work.id); |
|
| 367 | + | lifecycle::cancelRequests(&mut STORE, &mut REQUESTS); |
|
| 368 | + | assert STORE.contexts[0].pending == nil |
|
| 369 | + | and REQUESTS.slots[budget.index].state == slots::State::Free; |
|
| 370 | + | } |
|
| 371 | + | ||
| 372 | + | /// Closing multiple receivers validates all generations before changing any queue. |
|
| 373 | + | @test unsafe fn closeReceivers() throws (testing::TestError) { |
|
| 374 | + | initialize(); |
|
| 375 | + | retain(2, 0); |
|
| 376 | + | retain(3, 1); |
|
| 377 | + | let mut generations: [u32; limits::DOMAINS] = [0; limits::DOMAINS]; |
|
| 378 | + | set generations[0] = 1; |
|
| 379 | + | set generations[1] = 2; |
|
| 380 | + | let mut rejected = false; |
|
| 381 | + | try events::closeSet(&mut STORE.events, &generations[..]) catch error { |
|
| 382 | + | assert error == abi::Error::BadHandle; |
|
| 383 | + | set rejected = true; |
|
| 384 | + | }; |
|
| 385 | + | assert rejected |
|
| 386 | + | and STORE.events.queues[0].generation == 1 |
|
| 387 | + | and STORE.events.queues[1].generation == 1; |
|
| 388 | + | assert STORE.events.pending == 2; |
|
| 389 | + | set generations[1] = 1; |
|
| 390 | + | try! events::closeSet(&mut STORE.events, &generations[..]); |
|
| 391 | + | assert STORE.events.pending == 0; |
|
| 392 | + | assert STORE.events.queues[0].generation == 0 and STORE.events.queues[1].generation == 0; |
|
| 393 | + | assert try! events::acknowledged(&STORE.events, abi::Ref { index: 2, generation: 1 }); |
|
| 394 | + | assert try! events::acknowledged(&STORE.events, abi::Ref { index: 3, generation: 1 }); |
|
| 395 | + | } |
|
| 396 | + | ||
| 397 | + | /// Unpublished events of a terminating receiver move to a live receiver, and end when there |
|
| 398 | + | /// is none. A published event counts as delivered and ends when its receiver closes. |
|
| 399 | + | @test unsafe fn redirectTerminals() throws (testing::TestError) { |
|
| 400 | + | initialize(); |
|
| 401 | + | retain(2, 0); |
|
| 402 | + | retain(3, 0); |
|
| 403 | + | retain(4, 0); |
|
| 404 | + | let published = abi::Ref { index: 3, generation: 1 }; |
|
| 405 | + | assert try! events::deliver(&mut STORE.events, published, &mut FIRST.ring); |
|
| 406 | + | assert STORE.events.pending == 2 and STORE.events.queued == 1; |
|
| 407 | + | let heir = abi::Ref { index: 1, generation: 1 }; |
|
| 408 | + | events::redirect(&mut STORE.events, abi::Ref { index: 2, generation: 1 }, heir); |
|
| 409 | + | events::redirect(&mut STORE.events, abi::Ref { index: 4, generation: 1 }, nil); |
|
| 410 | + | assert STORE.events.pending == 1; |
|
| 411 | + | assert STORE.events.terminals[2].receiver == heir |
|
| 412 | + | and STORE.events.terminals[2].state == events::State::Pending; |
|
| 413 | + | assert try! events::acknowledged(&STORE.events, abi::Ref { index: 4, generation: 1 }); |
|
| 414 | + | try! events::close(&mut STORE.events, abi::Ref { index: 0, generation: 1 }); |
|
| 415 | + | assert try! events::acknowledged(&STORE.events, published); |
|
| 416 | + | assert try! events::flush(&mut STORE.events, heir, &mut SECOND.ring) == 1; |
|
| 417 | + | assert STORE.events.pending == 0 and STORE.events.queued == 1; |
|
| 418 | + | } |
|
| 419 | + | ||
| 420 | + | /// Events ready both receivers but signal only existing CPU authority. |
|
| 421 | + | @test unsafe fn receivers() throws (testing::TestError) { |
|
| 422 | + | initialize(); |
|
| 423 | + | retain(2, 0); |
|
| 424 | + | retain(3, 1); |
|
| 425 | + | assert STORE.events.pending == 2; |
|
| 426 | + | assert lifecycle::service(&mut STORE, &WINDOWS, 20) == 2; |
|
| 427 | + | assert STORE.events.pending == 0; |
|
| 428 | + | assert STORE.contexts[0].state == domains::ContextState::Ready; |
|
| 429 | + | assert STORE.contexts[1].state == domains::ContextState::Ready; |
|
| 430 | + | assert lifecycle::service(&mut STORE, &WINDOWS, 20) == 0; |
|
| 431 | + | assert STORE.events.queues[0].tail == 1 and STORE.events.queues[1].tail == 1; |
|
| 432 | + | let first = try! events::pop(&mut FIRST.ring) else panic "receivers: first exit"; |
|
| 433 | + | let second = try! events::pop(&mut SECOND.ring) else panic "receivers: second exit"; |
|
| 434 | + | assert first.kind == events::CHILD_EXIT and first.code == 2; |
|
| 435 | + | assert second.kind == events::CHILD_EXIT and second.code == 3; |
|
| 436 | + | retain(4, 0); |
|
| 437 | + | assert lifecycle::service(&mut STORE, &WINDOWS, 100) == 0; |
|
| 438 | + | assert STORE.events.terminals[4].state == events::State::Queued; |
|
| 439 | + | } |
|
| 440 | + | ||
| 441 | + | /// Maintenance acknowledges the last queued exit while its receiver keeps running. |
|
| 442 | + | @test unsafe fn queuedAcknowledgement() throws (testing::TestError) { |
|
| 443 | + | initialize(); |
|
| 444 | + | let subject = abi::Ref { index: 2, generation: 1 }; |
|
| 445 | + | set STORE.slots[2] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 446 | + | set STORE.records[2].state = domains::Lifecycle::Dead; |
|
| 447 | + | set STORE.teardown[2] = domains::Teardown::Retired; |
|
| 448 | + | set STORE.dead = 1; |
|
| 449 | + | retain(2, 0); |
|
| 450 | + | assert lifecycle::service(&mut STORE, &WINDOWS, 20) == 2; |
|
| 451 | + | assert STORE.events.pending == 0 and STORE.events.terminals[2].state == events::State::Queued; |
|
| 452 | + | assert STORE.events.queued == 1; |
|
| 453 | + | assert not lifecycle::reapNext(&mut STORE, &mut MEMORY, &mut WINDOWS, &mut IRQS); |
|
| 454 | + | assert lifecycle::service(&mut STORE, &WINDOWS, 21) == 0; |
|
| 455 | + | assert STORE.events.terminals[2].state == events::State::Queued; |
|
| 456 | + | let event = try! events::pop(&mut FIRST.ring) |
|
| 457 | + | else panic "queuedAcknowledgement: terminal event"; |
|
| 458 | + | assert event.value == abi::id(subject); |
|
| 459 | + | assert STORE.contexts[0].state == domains::ContextState::Ready; |
|
| 460 | + | assert lifecycle::service(&mut STORE, &WINDOWS, 22) == 0; |
|
| 461 | + | assert try! events::acknowledged(&STORE.events, subject); |
|
| 462 | + | assert STORE.events.queued == 0; |
|
| 463 | + | assert lifecycle::reapNext(&mut STORE, &mut MEMORY, &mut WINDOWS, &mut IRQS); |
|
| 464 | + | assert STORE.dead == 0 and STORE.slots[2].state == slots::State::Free; |
|
| 465 | + | assert STORE.events.terminals[2].state == events::State::Free; |
|
| 466 | + | } |
|
| 467 | + | ||
| 468 | + | /// Full receivers retain terminal events while other receivers make progress. |
|
| 469 | + | @test unsafe fn saturation() throws (testing::TestError) { |
|
| 470 | + | initialize(); |
|
| 471 | + | let receiver = abi::Ref { index: 0, generation: 1 }; |
|
| 472 | + | for i in 0..events::ORDINARY { |
|
| 473 | + | assert try! events::send(&mut STORE.events, receiver, &mut FIRST.ring, events::Event { |
|
| 474 | + | kind: events::WAKEUP, reserved: 0, code: i, value: 0 |
|
| 475 | + | }); |
|
| 476 | + | } |
|
| 477 | + | for i in 64..128 { |
|
| 478 | + | retain(i, 0); |
|
| 479 | + | } |
|
| 480 | + | assert try! events::flush(&mut STORE.events, receiver, &mut FIRST.ring) == 64; |
|
| 481 | + | retain(2, 0); |
|
| 482 | + | retain(3, 1); |
|
| 483 | + | assert lifecycle::service(&mut STORE, &WINDOWS, 20) == 0; |
|
| 484 | + | assert STORE.events.terminals[2].state == events::State::Pending; |
|
| 485 | + | assert STORE.events.pending == 1; |
|
| 486 | + | assert STORE.events.terminals[3].state == events::State::Queued; |
|
| 487 | + | let consumed = try! events::pop(&mut FIRST.ring) else panic "saturation: full queue"; |
|
| 488 | + | assert consumed.kind == events::WAKEUP; |
|
| 489 | + | assert lifecycle::service(&mut STORE, &WINDOWS, 20) == 2; |
|
| 490 | + | assert STORE.events.terminals[2].state == events::State::Queued; |
|
| 491 | + | assert STORE.events.queues[0].tail == events::CAPACITY + 1; |
|
| 492 | + | retain(4, 0); |
|
| 493 | + | assert STORE.events.pending == 1; |
|
| 494 | + | try! events::close(&mut STORE.events, receiver); |
|
| 495 | + | assert STORE.events.pending == 0; |
|
| 496 | + | assert STORE.events.queued == 1; |
|
| 497 | + | try! events::close(&mut STORE.events, abi::Ref { index: 1, generation: 1 }); |
|
| 498 | + | assert STORE.events.queued == 0; |
|
| 499 | + | } |
kernel/kernel/tests/loader.rad
added
+506 -0
| 1 | + | //! Verified binary loading, resident snapshots, and resource rollback. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use std::arch::rv64::shared; |
|
| 5 | + | use std::lang::alloc; |
|
| 6 | + | use std::lang::il; |
|
| 7 | + | use std::lang::il::binary; |
|
| 8 | + | use std::lang::il::binary::program; |
|
| 9 | + | use kernel::abi; |
|
| 10 | + | use kernel::limits; |
|
| 11 | + | use kernel::slots; |
|
| 12 | + | use kernel::pages; |
|
| 13 | + | use kernel::frames; |
|
| 14 | + | use kernel::backing; |
|
| 15 | + | use kernel::capability; |
|
| 16 | + | use kernel::registry; |
|
| 17 | + | use kernel::loader; |
|
| 18 | + | use kernel::domains; |
|
| 19 | + | use kernel::lifecycle; |
|
| 20 | + | ||
| 21 | + | /// Caller metadata used for interrupted loader calls. |
|
| 22 | + | unsafe static DOMAINS: domains::Store = undefined; |
|
| 23 | + | /// Caller-owned private reservations. |
|
| 24 | + | unsafe static CALLS: lifecycle::Calls = undefined; |
|
| 25 | + | /// Capability storage retained until the stopped caller is reclaimed. |
|
| 26 | + | unsafe static MEMORY: domains::Memory = undefined; |
|
| 27 | + | ||
| 28 | + | /// Mapped frames for temporary compiler storage and persistent native output. |
|
| 29 | + | static RAM: [u64; 18874368] = [0; 18874368]; |
|
| 30 | + | /// Physical frame and page metadata. |
|
| 31 | + | unsafe static PAGES: pages::Store = undefined; |
|
| 32 | + | /// Resident package registry. |
|
| 33 | + | unsafe static PACKAGES: registry::Store = undefined; |
|
| 34 | + | /// Loader ownership and resident-frame accounting. |
|
| 35 | + | unsafe static LOADER: loader::State = undefined; |
|
| 36 | + | /// Calling domain's capability table. |
|
| 37 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 38 | + | /// Source page allocated by the calling domain. |
|
| 39 | + | static SOURCE: abi::Handle = abi::Handle(0); |
|
| 40 | + | /// Byte offset of the first aligned test frame. |
|
| 41 | + | static OFFSET: u32 = 0; |
|
| 42 | + | ||
| 43 | + | /// Initialize a live domain and one source page over actual mapped memory. |
|
| 44 | + | unsafe fn initialize() { |
|
| 45 | + | slots::initialize(&mut PAGES.slots[..]); |
|
| 46 | + | backing::initialize(&mut PAGES.backings); |
|
| 47 | + | registry::initialize(&mut PACKAGES); |
|
| 48 | + | loader::initialize(&mut LOADER); |
|
| 49 | + | let pointer = &RAM[0]; |
|
| 50 | + | let base = pointer as u64; |
|
| 51 | + | let aligned = (base + 4095) & ~4095; |
|
| 52 | + | set OFFSET = (aligned - base) as u32; |
|
| 53 | + | set PAGES.backings.pool.count = (@sizeOf([u64; 18874368]) - OFFSET) / 4096; |
|
| 54 | + | for i in 0..PAGES.backings.pool.count { |
|
| 55 | + | set PAGES.backings.pool.addresses[i] = aligned + i as u64 * 4096; |
|
| 56 | + | set PAGES.backings.pool.free[i / 64] |= 1 << (i % 64) as u64; |
|
| 57 | + | } |
|
| 58 | + | let owner = abi::Ref { index: 0, generation: 1 }; |
|
| 59 | + | capability::initialize(&mut TABLE, owner); |
|
| 60 | + | try! backing::registerDomain(&mut PAGES.backings, owner); |
|
| 61 | + | let authority = try! capability::install(&mut TABLE, capability::Entry { |
|
| 62 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::ALLOCATE) |
|
| 63 | + | }); |
|
| 64 | + | set SOURCE = try! pages::allocate(&mut PAGES, &mut TABLE, abi::Handle(0), 1); |
|
| 65 | + | } |
|
| 66 | + | ||
| 67 | + | /// IL storage for the generated test function. |
|
| 68 | + | unsafe static CODE: [u8; 8192] = undefined; |
|
| 69 | + | ||
| 70 | + | /// Encode one small function into the source page, optionally naming an absent export. |
|
| 71 | + | unsafe fn input(value: i64, badExport: bool) -> loader::Request { |
|
| 72 | + | return inputWithDependencies(value, badExport, &[]); |
|
| 73 | + | } |
|
| 74 | + | ||
| 75 | + | /// Encode declared dependencies with a constant-return entry function. |
|
| 76 | + | unsafe fn inputWithDependencies( |
|
| 77 | + | value: i64, badExport: bool, dependencies: *[*[u8]] |
|
| 78 | + | ) -> loader::Request { |
|
| 79 | + | let mut arena = alloc::new(&mut CODE[..]); |
|
| 80 | + | let mut instructions: *mut [il::Instr] = &mut []; |
|
| 81 | + | try! instructions.append(il::Instr::Ret { val: il::Val::Imm(value) }, &mut arena); |
|
| 82 | + | let mut blocks: *mut [il::Block] = &mut []; |
|
| 83 | + | try! blocks.append( |
|
| 84 | + | il::Block { |
|
| 85 | + | label: il::BlockLabel { base: "entry", suffix: nil }, |
|
| 86 | + | params: &[], |
|
| 87 | + | instrs: instructions, |
|
| 88 | + | locs: &[], |
|
| 89 | + | preds: &[], |
|
| 90 | + | loopDepth: 0, |
|
| 91 | + | }, |
|
| 92 | + | &mut arena |
|
| 93 | + | ); |
|
| 94 | + | let mut functions: *mut [il::Fn] = &mut []; |
|
| 95 | + | try! functions.append( |
|
| 96 | + | il::Fn { |
|
| 97 | + | name: "loaded::main", |
|
| 98 | + | params: &[], |
|
| 99 | + | returnType: il::ValType::Data(il::Type::W64), |
|
| 100 | + | isExtern: false, |
|
| 101 | + | isLeaf: true, |
|
| 102 | + | blocks, |
|
| 103 | + | vals: &[], |
|
| 104 | + | cases: &[], |
|
| 105 | + | }, |
|
| 106 | + | &mut arena |
|
| 107 | + | ); |
|
| 108 | + | let mut list: *mut [*il::Fn] = &mut []; |
|
| 109 | + | try! list.append(&mut functions[0], &mut arena); |
|
| 110 | + | let mut name: *[u8] = "loaded::main"; |
|
| 111 | + | if badExport { |
|
| 112 | + | set name = "missing"; |
|
| 113 | + | } |
|
| 114 | + | let mut exports: *mut [binary::Export] = &mut []; |
|
| 115 | + | try! exports.append(binary::Export { name, kind: binary::ExportKind::Function }, &mut arena); |
|
| 116 | + | let package = binary::Package { |
|
| 117 | + | symbols: &["loaded", "loaded::main", "entry", "missing"], |
|
| 118 | + | name: "loaded", |
|
| 119 | + | dependencies, |
|
| 120 | + | exports, |
|
| 121 | + | entry: "loaded::main", |
|
| 122 | + | program: il::Program { data: &[], fns: list, layouts: &[], signatures: &[] }, |
|
| 123 | + | }; |
|
| 124 | + | let bytes = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 18874368])); |
|
| 125 | + | let mut output = &mut bytes[OFFSET..OFFSET + 4096] as *mut [u8]; |
|
| 126 | + | let length = try! program::encode(&mut output, &package); |
|
| 127 | + | return loader::Request { |
|
| 128 | + | authority: abi::Handle(0), source: SOURCE, offset: 0, length: length as u64 |
|
| 129 | + | }; |
|
| 130 | + | } |
|
| 131 | + | ||
| 132 | + | /// Count available frames to detect leaked temporary or unpublished output storage. |
|
| 133 | + | unsafe fn free() -> u32 { |
|
| 134 | + | let mut count: u32 = 0; |
|
| 135 | + | for i in 0..PAGES.backings.pool.count { |
|
| 136 | + | if frames::available(&PAGES.backings.pool, i) { |
|
| 137 | + | set count += 1; |
|
| 138 | + | } |
|
| 139 | + | } |
|
| 140 | + | return count; |
|
| 141 | + | } |
|
| 142 | + | ||
| 143 | + | /// Dead loaders retain private resources until hart release, then permit a retry. |
|
| 144 | + | @test unsafe fn cancelledCall() throws (testing::TestError) { |
|
| 145 | + | for stage in 0..3 { |
|
| 146 | + | initialize(); |
|
| 147 | + | domains::initialize(&mut DOMAINS); |
|
| 148 | + | lifecycle::initialize(&mut CALLS); |
|
| 149 | + | let request = input(43, false); |
|
| 150 | + | let before = free(); |
|
| 151 | + | let owner = TABLE.owner; |
|
| 152 | + | set MEMORY.table = TABLE; |
|
| 153 | + | set DOMAINS.slots[0] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 154 | + | set DOMAINS.records[0].state = domains::Lifecycle::Active; |
|
| 155 | + | set DOMAINS.records[0].memory = &mut MEMORY; |
|
| 156 | + | set DOMAINS.contextSlots[0] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 157 | + | set DOMAINS.contexts[0].owner = owner; |
|
| 158 | + | set DOMAINS.contexts[0].state = domains::ContextState::Ready; |
|
| 159 | + | set DOMAINS.contexts[0].hart = nil; |
|
| 160 | + | let pending = try! loader::reserve(&mut LOADER, &mut PAGES, &mut MEMORY.table, request); |
|
| 161 | + | match lifecycle::holdLoad(&mut CALLS, &mut DOMAINS, owner, pending) { |
|
| 162 | + | case lifecycle::LoadHeld::Held(lease, held) => { |
|
| 163 | + | let mut tokens = held; |
|
| 164 | + | if stage > 0 { |
|
| 165 | + | let output = try! lifecycle::reserveOutput( |
|
| 166 | + | &mut CALLS, &lease, &mut PAGES, &mut PACKAGES |
|
| 167 | + | ); |
|
| 168 | + | set tokens = lifecycle::withOutput(tokens, output); |
|
| 169 | + | } |
|
| 170 | + | if stage == 2 { |
|
| 171 | + | let retained = lifecycle::load(&CALLS, &lease); |
|
| 172 | + | let decoded = try! lifecycle::decodeLoad(&retained, &tokens, &lease); |
|
| 173 | + | let compiled = try! lifecycle::compileLoad( |
|
| 174 | + | ®istry::capture(&PACKAGES), &retained, &tokens, &lease, &decoded |
|
| 175 | + | ); |
|
| 176 | + | assert compiled.code > 0; |
|
| 177 | + | } |
|
| 178 | + | lifecycle::cancelAllocations( |
|
| 179 | + | &mut CALLS, &mut DOMAINS, &mut PAGES, &mut LOADER, &mut PACKAGES |
|
| 180 | + | ); |
|
| 181 | + | assert LOADER.owner <> nil and DOMAINS.contexts[0].reservation; |
|
| 182 | + | set DOMAINS.records[0].state = domains::Lifecycle::Dead; |
|
| 183 | + | set DOMAINS.contexts[0].state = domains::ContextState::Stopped; |
|
| 184 | + | set DOMAINS.contexts[0].hart = 1; |
|
| 185 | + | lifecycle::cancelAllocations( |
|
| 186 | + | &mut CALLS, &mut DOMAINS, &mut PAGES, &mut LOADER, &mut PACKAGES |
|
| 187 | + | ); |
|
| 188 | + | assert LOADER.owner <> nil and free() < before; |
|
| 189 | + | assert lifecycle::leaseContext(lease) == owner; |
|
| 190 | + | set DOMAINS.contexts[0].hart = nil; |
|
| 191 | + | lifecycle::cancelAllocations( |
|
| 192 | + | &mut CALLS, &mut DOMAINS, &mut PAGES, &mut LOADER, &mut PACKAGES |
|
| 193 | + | ); |
|
| 194 | + | assert LOADER.owner == nil and not DOMAINS.contexts[0].reservation; |
|
| 195 | + | assert free() < before and PAGES.backings.pool.retiredCount > 0; |
|
| 196 | + | while PAGES.backings.pool.retiredCount > 0 { |
|
| 197 | + | let reclaimed = frames::reclaim(&mut PAGES.backings.pool, 256); |
|
| 198 | + | assert reclaimed > 0 and reclaimed <= 256; |
|
| 199 | + | } |
|
| 200 | + | assert free() == before and MEMORY.table.slots[2].state == slots::State::Free; |
|
| 201 | + | assert PACKAGES.slots[0].state == slots::State::Free and not LOADER.live[0]; |
|
| 202 | + | } |
|
| 203 | + | } |
|
| 204 | + | set DOMAINS.records[0].state = domains::Lifecycle::Active; |
|
| 205 | + | let retry = try! loader::reserve(&mut LOADER, &mut PAGES, &mut MEMORY.table, request); |
|
| 206 | + | match lifecycle::holdLoad(&mut CALLS, &mut DOMAINS, owner, retry) { |
|
| 207 | + | case lifecycle::LoadHeld::Held(active, retryAccess) => { |
|
| 208 | + | let retryOutput = try! lifecycle::reserveOutput( |
|
| 209 | + | &mut CALLS, &active, &mut PAGES, &mut PACKAGES |
|
| 210 | + | ); |
|
| 211 | + | let joined = lifecycle::withOutput(retryAccess, retryOutput); |
|
| 212 | + | let retained = lifecycle::load(&CALLS, &active); |
|
| 213 | + | let decoded = try! lifecycle::decodeLoad(&retained, &joined, &active); |
|
| 214 | + | let compiled = try! lifecycle::compileLoad( |
|
| 215 | + | ®istry::capture(&PACKAGES), &retained, &joined, &active, &decoded |
|
| 216 | + | ); |
|
| 217 | + | let handle = try! lifecycle::finishLoad( |
|
| 218 | + | &mut CALLS, |
|
| 219 | + | &mut DOMAINS, |
|
| 220 | + | &mut PAGES, |
|
| 221 | + | &mut LOADER, |
|
| 222 | + | &mut PACKAGES, |
|
| 223 | + | &mut MEMORY.table, |
|
| 224 | + | active, |
|
| 225 | + | lifecycle::LoadResult::Compiled(compiled, joined) |
|
| 226 | + | ); |
|
| 227 | + | let object = try! registry::image(&PACKAGES, &MEMORY.table, handle, abi::Rights(0)); |
|
| 228 | + | assert object.index == 0 and not DOMAINS.contexts[0].reservation; |
|
| 229 | + | let available = free(); |
|
| 230 | + | let duplicate = try! loader::reserve( |
|
| 231 | + | &mut LOADER, &mut PAGES, &mut MEMORY.table, request |
|
| 232 | + | ); |
|
| 233 | + | match lifecycle::holdLoad(&mut CALLS, &mut DOMAINS, owner, duplicate) { |
|
| 234 | + | case lifecycle::LoadHeld::Held(reuse, reuseAccess) => { |
|
| 235 | + | let source = lifecycle::load(&CALLS, &reuse); |
|
| 236 | + | let program = try! lifecycle::decodeLoad(&source, &reuseAccess, &reuse); |
|
| 237 | + | assert try! lifecycle::identifyLoad( |
|
| 238 | + | ®istry::capture(&PACKAGES), &source, &reuseAccess, &reuse, &program |
|
| 239 | + | ) == object; |
|
| 240 | + | let same = try! lifecycle::finishLoad( |
|
| 241 | + | &mut CALLS, |
|
| 242 | + | &mut DOMAINS, |
|
| 243 | + | &mut PAGES, |
|
| 244 | + | &mut LOADER, |
|
| 245 | + | &mut PACKAGES, |
|
| 246 | + | &mut MEMORY.table, |
|
| 247 | + | reuse, |
|
| 248 | + | lifecycle::LoadResult::Existing(object, reuseAccess) |
|
| 249 | + | ); |
|
| 250 | + | assert try! registry::image(&PACKAGES, &MEMORY.table, same, abi::Rights( |
|
| 251 | + | 0 |
|
| 252 | + | )) == object; |
|
| 253 | + | assert free() == available and LOADER.owner == nil; |
|
| 254 | + | } |
|
| 255 | + | } |
|
| 256 | + | } |
|
| 257 | + | } |
|
| 258 | + | } |
|
| 259 | + | } |
|
| 260 | + | ||
| 261 | + | /// Reserved input stays private across allocation, source mutation, and contention. |
|
| 262 | + | @test unsafe fn stagedInput() throws (testing::TestError) { |
|
| 263 | + | initialize(); |
|
| 264 | + | let request = input(43, false); |
|
| 265 | + | let before = free(); |
|
| 266 | + | let pending = try! loader::reserve(&mut LOADER, &mut PAGES, &mut TABLE, request); |
|
| 267 | + | assert LOADER.owner <> nil and TABLE.slots[2].state == slots::State::Reserved; |
|
| 268 | + | let other = try! pages::allocate(&mut PAGES, &mut TABLE, abi::Handle(0), 1); |
|
| 269 | + | let decoded = try! loader::decode(&pending); |
|
| 270 | + | assert decoded.program.fns.len == 1 and decoded.name.len == 6; |
|
| 271 | + | let replacement = input(99, false); |
|
| 272 | + | assert decoded.program.fns[0].blocks[0].instrs[0] == il::Instr::Ret { val: il::Val::Imm(43) }; |
|
| 273 | + | let mut busy = false; |
|
| 274 | + | try loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request) catch error { |
|
| 275 | + | assert error == abi::Error::Busy; |
|
| 276 | + | set busy = true; |
|
| 277 | + | }; |
|
| 278 | + | assert busy; |
|
| 279 | + | loader::cancel(&mut LOADER, &mut PAGES, &mut TABLE, pending); |
|
| 280 | + | assert LOADER.owner == nil and TABLE.slots[2].state == slots::State::Free; |
|
| 281 | + | assert free() + 1 == before; |
|
| 282 | + | assert (try! pages::get(&PAGES, (try! capability::get(&TABLE, other)).object)).count == 1; |
|
| 283 | + | } |
|
| 284 | + | ||
| 285 | + | /// Compiled output stays private until publication and cancellation restores capacity. |
|
| 286 | + | @test unsafe fn stagedOutput() throws (testing::TestError) { |
|
| 287 | + | initialize(); |
|
| 288 | + | let request = input(43, false); |
|
| 289 | + | let before = free(); |
|
| 290 | + | let pending = try! loader::reserve(&mut LOADER, &mut PAGES, &mut TABLE, request); |
|
| 291 | + | let decoded = try! loader::decode(&pending); |
|
| 292 | + | let output = try! loader::reserveOutput(&mut PAGES.backings.pool, &mut PACKAGES); |
|
| 293 | + | assert PACKAGES.slots[0].state == slots::State::Reserved and not LOADER.live[0]; |
|
| 294 | + | let other = try! pages::allocate(&mut PAGES, &mut TABLE, abi::Handle(0), 1); |
|
| 295 | + | let used = try! loader::compile(®istry::capture(&PACKAGES), &pending, &decoded, &output); |
|
| 296 | + | assert used.code > 0 and used.metadata > 0; |
|
| 297 | + | assert PACKAGES.slots[0].state == slots::State::Reserved and not LOADER.live[0]; |
|
| 298 | + | loader::cancelOutput(&mut PAGES.backings.pool, &mut PACKAGES, output); |
|
| 299 | + | loader::cancel(&mut LOADER, &mut PAGES, &mut TABLE, pending); |
|
| 300 | + | assert free() + 1 == before and LOADER.owner == nil; |
|
| 301 | + | assert PACKAGES.slots[0].state == slots::State::Free; |
|
| 302 | + | let handle = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request); |
|
| 303 | + | assert (try! registry::image(&PACKAGES, &TABLE, handle, abi::Rights(0))).index == 0; |
|
| 304 | + | } |
|
| 305 | + | ||
| 306 | + | /// A caller whose backing lifetime ended cannot receive a completed Image handle. |
|
| 307 | + | @test unsafe fn completionLifetime() throws (testing::TestError) { |
|
| 308 | + | initialize(); |
|
| 309 | + | let request = input(43, false); |
|
| 310 | + | let handle = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request); |
|
| 311 | + | let object = try! registry::image(&PACKAGES, &TABLE, handle, abi::Rights(0)); |
|
| 312 | + | let before = free(); |
|
| 313 | + | let pending = try! loader::reserve(&mut LOADER, &mut PAGES, &mut TABLE, request); |
|
| 314 | + | try! backing::endDomain(&mut PAGES.backings, TABLE.owner); |
|
| 315 | + | let mut dead = false; |
|
| 316 | + | try loader::finish(&mut LOADER, &mut PAGES, &mut TABLE, pending, object) catch error { |
|
| 317 | + | assert error == abi::Error::BadHandle; |
|
| 318 | + | set dead = true; |
|
| 319 | + | }; |
|
| 320 | + | assert dead and LOADER.owner == nil and free() == before; |
|
| 321 | + | assert TABLE.slots[3].state == slots::State::Free; |
|
| 322 | + | assert slots::matches(&PACKAGES.slots[..], object, slots::State::Live); |
|
| 323 | + | } |
|
| 324 | + | ||
| 325 | + | /// Resident source bytes survive mutation of the source page and workspace release. |
|
| 326 | + | @test unsafe fn residentSnapshot() throws (testing::TestError) { |
|
| 327 | + | initialize(); |
|
| 328 | + | let request = input(43, false); |
|
| 329 | + | let before = free(); |
|
| 330 | + | let handle = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request); |
|
| 331 | + | let object = try! registry::image(&PACKAGES, &TABLE, handle, abi::Rights(abi::EXECUTE)); |
|
| 332 | + | let package = try! registry::get(&PACKAGES, object); |
|
| 333 | + | try testing::expect(package.code.len > 0 and package.entry <> nil); |
|
| 334 | + | let retained = LOADER.resident[object.index]; |
|
| 335 | + | try testing::expect(free() + retained.code.count + retained.metadata.count == before); |
|
| 336 | + | try testing::expect(LOADER.owner == nil); |
|
| 337 | + | let bytes = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 18874368])); |
|
| 338 | + | let magic = bytes[OFFSET]; |
|
| 339 | + | set bytes[OFFSET] = 0; |
|
| 340 | + | try testing::expect(PACKAGES.entries[object.index].source[0] == magic); |
|
| 341 | + | set bytes[OFFSET] = magic; |
|
| 342 | + | let duplicate = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request); |
|
| 343 | + | try testing::expect(duplicate <> handle); |
|
| 344 | + | try testing::expect( |
|
| 345 | + | try! registry::image(&PACKAGES, &TABLE, duplicate, abi::Rights( |
|
| 346 | + | 0 |
|
| 347 | + | )) == object |
|
| 348 | + | ); |
|
| 349 | + | try testing::expect(free() + retained.code.count + retained.metadata.count == before); |
|
| 350 | + | } |
|
| 351 | + | ||
| 352 | + | /// Decode and linking failures return all unpublished frames and capability capacity. |
|
| 353 | + | @test unsafe fn rollback() throws (testing::TestError) { |
|
| 354 | + | initialize(); |
|
| 355 | + | let mut request = input(43, false); |
|
| 356 | + | let before = free(); |
|
| 357 | + | set request.length = 3; |
|
| 358 | + | let mut failures: u32 = 0; |
|
| 359 | + | try loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request) catch err { |
|
| 360 | + | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 361 | + | set failures += 1; |
|
| 362 | + | }; |
|
| 363 | + | set request = input(43, true); |
|
| 364 | + | try loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request) catch err { |
|
| 365 | + | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 366 | + | set failures += 1; |
|
| 367 | + | }; |
|
| 368 | + | try testing::expect(failures == 2 and free() == before and LOADER.owner == nil); |
|
| 369 | + | try testing::expect( |
|
| 370 | + | TABLE.slots[2].state == slots::State::Free |
|
| 371 | + | and PACKAGES.slots[0].state == slots::State::Free |
|
| 372 | + | ); |
|
| 373 | + | let valid = input(43, false); |
|
| 374 | + | let handle = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, valid); |
|
| 375 | + | let object = try! registry::image(&PACKAGES, &TABLE, handle, abi::Rights(0)); |
|
| 376 | + | try testing::expect(object.index == 0); |
|
| 377 | + | } |
|
| 378 | + | ||
| 379 | + | /// Contention and workspace allocation failure preserve source authority and clear workspace |
|
| 380 | + | /// occupancy. |
|
| 381 | + | @test unsafe fn workspaceOwnership() throws (testing::TestError) { |
|
| 382 | + | initialize(); |
|
| 383 | + | let request = input(43, false); |
|
| 384 | + | let before = free(); |
|
| 385 | + | let lease = try! loader::acquire(&mut LOADER, &mut PAGES.backings.pool, TABLE.owner); |
|
| 386 | + | let mut busy = false; |
|
| 387 | + | try loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request) catch err { |
|
| 388 | + | set busy = err == abi::Error::Busy; |
|
| 389 | + | }; |
|
| 390 | + | let held = LOADER.owner == TABLE.owner; |
|
| 391 | + | loader::release(&mut LOADER, &mut PAGES.backings.pool, lease); |
|
| 392 | + | try testing::expect(busy and held); |
|
| 393 | + | try testing::expect(free() == before); |
|
| 394 | + | let count = PAGES.backings.pool.count; |
|
| 395 | + | set PAGES.backings.pool.count = 32; |
|
| 396 | + | let mut exhausted = false; |
|
| 397 | + | try loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request) catch err { |
|
| 398 | + | try testing::expect(err == abi::Error::OutOfMemory); |
|
| 399 | + | set exhausted = true; |
|
| 400 | + | }; |
|
| 401 | + | set PAGES.backings.pool.count = count; |
|
| 402 | + | try testing::expect(exhausted and free() == before and LOADER.owner == nil); |
|
| 403 | + | try testing::expect(TABLE.slots[2].state == slots::State::Free); |
|
| 404 | + | } |
|
| 405 | + | ||
| 406 | + | /// Output allocation failures cancel registry admission and return compiler frames. |
|
| 407 | + | @test unsafe fn outputAllocationRollback() throws (testing::TestError) { |
|
| 408 | + | initialize(); |
|
| 409 | + | let request = input(43, false); |
|
| 410 | + | let before = free(); |
|
| 411 | + | let workspacePages = (@sizeOf(loader::Workspace) + 4095) / 4096; |
|
| 412 | + | for extra in &[0 as u32, 2048] { |
|
| 413 | + | let reservation = try! frames::reserve( |
|
| 414 | + | &mut PAGES.backings.pool, before - workspacePages - extra |
|
| 415 | + | ); |
|
| 416 | + | let held = frames::commit(reservation); |
|
| 417 | + | let available = free(); |
|
| 418 | + | let mut failed = false; |
|
| 419 | + | try loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request) catch err { |
|
| 420 | + | try testing::expect(err == abi::Error::OutOfMemory); |
|
| 421 | + | set failed = true; |
|
| 422 | + | }; |
|
| 423 | + | try testing::expect(failed and free() == available and LOADER.owner == nil); |
|
| 424 | + | try testing::expect(TABLE.slots[2].state == slots::State::Free); |
|
| 425 | + | try testing::expect(PACKAGES.slots[0].state == slots::State::Free); |
|
| 426 | + | try! frames::release(&mut PAGES.backings.pool, held); |
|
| 427 | + | try testing::expect(free() == before); |
|
| 428 | + | } |
|
| 429 | + | let handle = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request); |
|
| 430 | + | let object = try! registry::image(&PACKAGES, &TABLE, handle, abi::Rights(0)); |
|
| 431 | + | try testing::expect(object.index == 0); |
|
| 432 | + | } |
|
| 433 | + | ||
| 434 | + | /// Check that a load of a package with `dependencies` fails and leaves no trace. |
|
| 435 | + | unsafe fn rejectDependencies(dependencies: *[*[u8]], before: u32, resident: abi::Ref) |
|
| 436 | + | throws (testing::TestError) |
|
| 437 | + | { |
|
| 438 | + | let request = inputWithDependencies(43, false, dependencies); |
|
| 439 | + | let mut failed = false; |
|
| 440 | + | try loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request) catch err { |
|
| 441 | + | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 442 | + | set failed = true; |
|
| 443 | + | }; |
|
| 444 | + | try testing::expect(failed and free() == before and LOADER.owner == nil); |
|
| 445 | + | try testing::expect(TABLE.slots[2].state == slots::State::Free); |
|
| 446 | + | try testing::expect(PACKAGES.slots[1].state == slots::State::Free and not LOADER.live[1]); |
|
| 447 | + | try testing::expect(slots::matches(&PACKAGES.slots[..], resident, slots::State::Live)); |
|
| 448 | + | } |
|
| 449 | + | ||
| 450 | + | /// A package with dependencies is rejected before compilation, even when its dependency is |
|
| 451 | + | /// resident, and returns all frames and capability capacity. |
|
| 452 | + | @test unsafe fn dependenciesRejected() throws (testing::TestError) { |
|
| 453 | + | initialize(); |
|
| 454 | + | let pending = try! registry::reserve(&mut PACKAGES); |
|
| 455 | + | let reference = slots::reference(&pending); |
|
| 456 | + | let dependency = shared::Package { |
|
| 457 | + | name: "missing", |
|
| 458 | + | dependencies: &[], |
|
| 459 | + | slot: reference.index, |
|
| 460 | + | codeAddress: 0, |
|
| 461 | + | code: &[], |
|
| 462 | + | exports: &[], |
|
| 463 | + | entry: nil, |
|
| 464 | + | template: &[], |
|
| 465 | + | memory: 0, |
|
| 466 | + | alignment: 8, |
|
| 467 | + | relocations: &[], |
|
| 468 | + | }; |
|
| 469 | + | let resident = try! registry::publish(&mut PACKAGES, pending, "dependency", dependency); |
|
| 470 | + | let before = free(); |
|
| 471 | + | try rejectDependencies(&["missing"], before, resident); |
|
| 472 | + | try rejectDependencies(&["missing", "missing"], before, resident); |
|
| 473 | + | let valid = input(43, false); |
|
| 474 | + | let handle = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, valid); |
|
| 475 | + | let object = try! registry::image(&PACKAGES, &TABLE, handle, abi::Rights(0)); |
|
| 476 | + | try testing::expect(object.index == 1); |
|
| 477 | + | } |
|
| 478 | + | ||
| 479 | + | /// Generated code leaves no stale bytes behind it in its final executable frame. |
|
| 480 | + | @test unsafe fn codeTailCleared() throws (testing::TestError) { |
|
| 481 | + | initialize(); |
|
| 482 | + | let request = input(43, false); |
|
| 483 | + | let pending = try! loader::reserve(&mut LOADER, &mut PAGES, &mut TABLE, request); |
|
| 484 | + | let decoded = try! loader::decode(&pending); |
|
| 485 | + | let output = try! loader::reserveOutput(&mut PAGES.backings.pool, &mut PACKAGES); |
|
| 486 | + | match &output { |
|
| 487 | + | case loader::OutputReservation::Held(target, access) => { |
|
| 488 | + | let ram = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 18874368])); |
|
| 489 | + | let start = (target.codeBase - &RAM[0] as u64) as u32; |
|
| 490 | + | let stale = &mut ram[start..start + 4096]; |
|
| 491 | + | for i in 0..stale.len { |
|
| 492 | + | set stale[i] = 0xa5; |
|
| 493 | + | } |
|
| 494 | + | let used = try! loader::compile( |
|
| 495 | + | ®istry::capture(&PACKAGES), &pending, &decoded, &output |
|
| 496 | + | ); |
|
| 497 | + | assert used.code == 1; |
|
| 498 | + | assert stale[0] <> 0xa5; |
|
| 499 | + | for i in 4032..stale.len { |
|
| 500 | + | assert stale[i] == 0; |
|
| 501 | + | } |
|
| 502 | + | } |
|
| 503 | + | } |
|
| 504 | + | loader::cancelOutput(&mut PAGES.backings.pool, &mut PACKAGES, output); |
|
| 505 | + | loader::cancel(&mut LOADER, &mut PAGES, &mut TABLE, pending); |
|
| 506 | + | } |
kernel/kernel/tests/pages.rad
added
+457 -0
| 1 | + | //! Page allocation, zeroing, split atomicity, and retained-pointer lifetimes. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::frames; |
|
| 6 | + | use kernel::capability; |
|
| 7 | + | use kernel::pages; |
|
| 8 | + | use kernel::backing; |
|
| 9 | + | use kernel::slots; |
|
| 10 | + | use kernel::limits; |
|
| 11 | + | ||
| 12 | + | /// Page and allocation metadata workspace. |
|
| 13 | + | unsafe static STORE: pages::Store = undefined; |
|
| 14 | + | /// Test domain's capability table. |
|
| 15 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 16 | + | /// Receiver table for page capability transactions. |
|
| 17 | + | unsafe static TARGET: capability::Table = undefined; |
|
| 18 | + | /// Mapped bytes with room for page alignment and eight full pages. |
|
| 19 | + | static RAM: [u8; 36864] = [0; 36864]; |
|
| 20 | + | /// Byte offset to the first page-aligned test frame. |
|
| 21 | + | static OFFSET: u32 = 0; |
|
| 22 | + | ||
| 23 | + | /// Initialize metadata over mapped hosted memory, without synthetic addresses. |
|
| 24 | + | unsafe fn initialize() { |
|
| 25 | + | for i in 0..RAM.len { |
|
| 26 | + | set RAM[i] = 0xa5; |
|
| 27 | + | } |
|
| 28 | + | let pointer: *u8 = &RAM[0]; |
|
| 29 | + | let address = pointer as u64; |
|
| 30 | + | let aligned = (address + 4095) & ~4095; |
|
| 31 | + | set OFFSET = (aligned - address) as u32; |
|
| 32 | + | slots::initialize(&mut STORE.slots[..]); |
|
| 33 | + | backing::initialize(&mut STORE.backings); |
|
| 34 | + | set STORE.backings.pool.count = 8; |
|
| 35 | + | for i in 0..8 { |
|
| 36 | + | set STORE.backings.pool.addresses[i] = aligned + i as u64 * 4096; |
|
| 37 | + | set STORE.backings.pool.free[i / 64] |= 1 << (i % 64) as u64; |
|
| 38 | + | } |
|
| 39 | + | let owner = abi::Ref { index: 0, generation: 1 }; |
|
| 40 | + | capability::initialize(&mut TABLE, owner); |
|
| 41 | + | try! backing::registerDomain(&mut STORE.backings, owner); |
|
| 42 | + | let authority = try! capability::install(&mut TABLE, capability::Entry { |
|
| 43 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::ALLOCATE) |
|
| 44 | + | }); |
|
| 45 | + | } |
|
| 46 | + | ||
| 47 | + | /// Drop the test's handle and its object reference in the same serialized operation. |
|
| 48 | + | unsafe fn drop(handle: abi::Handle) { |
|
| 49 | + | try! pages::drop(&mut STORE, &mut TABLE, handle); |
|
| 50 | + | } |
|
| 51 | + | ||
| 52 | + | /// Reserved frames stay private while another allocation changes shared metadata. |
|
| 53 | + | @test unsafe fn stagedAllocation() throws (testing::TestError) { |
|
| 54 | + | initialize(); |
|
| 55 | + | let pending = try! pages::reserve(&mut STORE, &mut TABLE, abi::Handle(0), 2); |
|
| 56 | + | assert STORE.slots[0].state == slots::State::Reserved; |
|
| 57 | + | assert TABLE.slots[1].state == slots::State::Reserved; |
|
| 58 | + | assert not frames::available(&STORE.backings.pool, 0) |
|
| 59 | + | and not frames::available(&STORE.backings.pool, 1); |
|
| 60 | + | assert RAM[OFFSET] == 0xa5; |
|
| 61 | + | let other = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 6); |
|
| 62 | + | set RAM[OFFSET + 8192] = 0x7b; |
|
| 63 | + | pages::clear(&pending); |
|
| 64 | + | for i in OFFSET..OFFSET + 8192 { |
|
| 65 | + | assert RAM[i] == 0; |
|
| 66 | + | } |
|
| 67 | + | assert RAM[OFFSET + 8192] == 0x7b; |
|
| 68 | + | let handle = pages::publish(&mut STORE, &mut TABLE, pending); |
|
| 69 | + | let entry = try! capability::get(&TABLE, handle); |
|
| 70 | + | let page = try! pages::get(&STORE, entry.object); |
|
| 71 | + | assert page.count == 2 and page.origin == TABLE.owner; |
|
| 72 | + | drop(handle); |
|
| 73 | + | drop(other); |
|
| 74 | + | try! backing::endDomain(&mut STORE.backings, TABLE.owner); |
|
| 75 | + | for i in 0..8 { |
|
| 76 | + | assert frames::available(&STORE.backings.pool, i); |
|
| 77 | + | } |
|
| 78 | + | } |
|
| 79 | + | ||
| 80 | + | /// Cancelling unpublished storage returns all capacity without exposing its bytes. |
|
| 81 | + | @test unsafe fn cancelAllocation() throws (testing::TestError) { |
|
| 82 | + | initialize(); |
|
| 83 | + | let pending = try! pages::reserve(&mut STORE, &mut TABLE, abi::Handle(0), 8); |
|
| 84 | + | pages::cancel(&mut STORE, &mut TABLE, pending); |
|
| 85 | + | assert STORE.slots[0].state == slots::State::Free; |
|
| 86 | + | assert STORE.backings.slots[0].state == slots::State::Free; |
|
| 87 | + | assert TABLE.slots[1].state == slots::State::Free; |
|
| 88 | + | for i in 0..8 { |
|
| 89 | + | assert frames::available(&STORE.backings.pool, i); |
|
| 90 | + | } |
|
| 91 | + | assert RAM[OFFSET] == 0xa5; |
|
| 92 | + | let full = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 8); |
|
| 93 | + | let entry = try! capability::get(&TABLE, full); |
|
| 94 | + | assert (try! pages::get(&STORE, entry.object)).count == 8; |
|
| 95 | + | } |
|
| 96 | + | ||
| 97 | + | /// Transfers keep both domains exposed without duplicating page handles. |
|
| 98 | + | @test unsafe fn transferExposure() throws (testing::TestError) { |
|
| 99 | + | initialize(); |
|
| 100 | + | let receiver = abi::Ref { index: 1, generation: 1 }; |
|
| 101 | + | capability::initialize(&mut TARGET, receiver); |
|
| 102 | + | try! backing::registerDomain(&mut STORE.backings, receiver); |
|
| 103 | + | let original = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 8); |
|
| 104 | + | let entry = try! capability::get(&TABLE, original); |
|
| 105 | + | let moved = try! pages::transfer( |
|
| 106 | + | &mut STORE, &mut TABLE, &mut TARGET, original, abi::READ as u64 |
|
| 107 | + | ); |
|
| 108 | + | let page = try! pages::get(&STORE, entry.object); |
|
| 109 | + | try testing::expect( |
|
| 110 | + | page.handles == 1 |
|
| 111 | + | and STORE.backings.records[page.backing.index].exposed == 2 |
|
| 112 | + | ); |
|
| 113 | + | let actual = try! capability::get(&TARGET, moved); |
|
| 114 | + | try testing::expect(*actual.rights == abi::READ); |
|
| 115 | + | try! pages::drop(&mut STORE, &mut TARGET, moved); |
|
| 116 | + | try! backing::endDomain(&mut STORE.backings, receiver); |
|
| 117 | + | assert not frames::available(&STORE.backings.pool, 0); |
|
| 118 | + | try! backing::endDomain(&mut STORE.backings, TABLE.owner); |
|
| 119 | + | assert frames::available(&STORE.backings.pool, 0); |
|
| 120 | + | } |
|
| 121 | + | ||
| 122 | + | /// Grants acquire references only after capacity and receiver checks succeed. |
|
| 123 | + | @test unsafe fn grantRollback() throws (testing::TestError) { |
|
| 124 | + | initialize(); |
|
| 125 | + | let receiver = abi::Ref { index: 1, generation: 1 }; |
|
| 126 | + | capability::initialize(&mut TARGET, receiver); |
|
| 127 | + | let original = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 2); |
|
| 128 | + | let entry = try! capability::get(&TABLE, original); |
|
| 129 | + | let page = try! pages::get(&STORE, entry.object); |
|
| 130 | + | let mut dead = false; |
|
| 131 | + | try pages::grant(&mut STORE, &TABLE, &mut TARGET, original, abi::READ as u64) catch err { |
|
| 132 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 133 | + | set dead = true; |
|
| 134 | + | }; |
|
| 135 | + | try testing::expect(dead and TARGET.slots[0].state == slots::State::Free); |
|
| 136 | + | let mut rejected = false; |
|
| 137 | + | try pages::transfer(&mut STORE, &mut TABLE, &mut TARGET, original, abi::READ as u64) catch err { |
|
| 138 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 139 | + | set rejected = true; |
|
| 140 | + | }; |
|
| 141 | + | try testing::expect(rejected and TARGET.slots[0].state == slots::State::Free); |
|
| 142 | + | try testing::expect(try! capability::get(&TABLE, original) == entry); |
|
| 143 | + | try! backing::registerDomain(&mut STORE.backings, receiver); |
|
| 144 | + | for i in 0..limits::HANDLES { |
|
| 145 | + | let filler = try! capability::install(&mut TARGET, capability::Entry { |
|
| 146 | + | kind: abi::Kind::Domain, object: receiver, rights: abi::Rights(0) |
|
| 147 | + | }); |
|
| 148 | + | } |
|
| 149 | + | let mut full: u32 = 0; |
|
| 150 | + | try pages::grant(&mut STORE, &TABLE, &mut TARGET, original, abi::READ as u64) catch err { |
|
| 151 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 152 | + | set full += 1; |
|
| 153 | + | }; |
|
| 154 | + | try pages::transfer(&mut STORE, &mut TABLE, &mut TARGET, original, abi::READ as u64) catch err { |
|
| 155 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 156 | + | set full += 1; |
|
| 157 | + | }; |
|
| 158 | + | try testing::expect(full == 2 and STORE.records[entry.object.index].handles == 1); |
|
| 159 | + | try testing::expect(STORE.backings.records[page.backing.index].exposed == 1); |
|
| 160 | + | try testing::expect(try! capability::get(&TABLE, original) == entry); |
|
| 161 | + | let filler = try! capability::at(&TARGET, 0, abi::Kind::Domain); |
|
| 162 | + | try! capability::invalidate(&mut TARGET, filler); |
|
| 163 | + | let granted = try! pages::grant(&mut STORE, &TABLE, &mut TARGET, original, abi::READ as u64); |
|
| 164 | + | try testing::expect(STORE.records[entry.object.index].handles == 2); |
|
| 165 | + | try testing::expect(STORE.backings.records[page.backing.index].exposed == 2); |
|
| 166 | + | drop(original); |
|
| 167 | + | try! pages::drop(&mut STORE, &mut TARGET, granted); |
|
| 168 | + | try! backing::endDomain(&mut STORE.backings, TABLE.owner); |
|
| 169 | + | assert not frames::available(&STORE.backings.pool, 0); |
|
| 170 | + | try! backing::endDomain(&mut STORE.backings, receiver); |
|
| 171 | + | assert frames::available(&STORE.backings.pool, 0); |
|
| 172 | + | } |
|
| 173 | + | ||
| 174 | + | /// Self-attenuation adds one reference, preserves authority, and prevents splitting. |
|
| 175 | + | @test unsafe fn selfAttenuation() throws (testing::TestError) { |
|
| 176 | + | initialize(); |
|
| 177 | + | let original = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 2); |
|
| 178 | + | let copy = try! pages::selfGrant(&mut STORE, &mut TABLE, original, abi::READ as u64); |
|
| 179 | + | let entry = try! capability::get(&TABLE, copy); |
|
| 180 | + | try testing::expect( |
|
| 181 | + | *entry.rights == abi::READ |
|
| 182 | + | and STORE.records[entry.object.index].handles == 2 |
|
| 183 | + | ); |
|
| 184 | + | let narrowed = try! pages::selfGrant(&mut STORE, &mut TABLE, copy, abi::WRITE as u64); |
|
| 185 | + | let none = try! capability::get(&TABLE, narrowed); |
|
| 186 | + | try testing::expect(*none.rights == 0); |
|
| 187 | + | drop(narrowed); |
|
| 188 | + | drop(copy); |
|
| 189 | + | let right = try! pages::split(&mut STORE, &mut TABLE, original, 1); |
|
| 190 | + | drop(original); |
|
| 191 | + | drop(right); |
|
| 192 | + | try! backing::endDomain(&mut STORE.backings, TABLE.owner); |
|
| 193 | + | assert frames::available(&STORE.backings.pool, 0) and frames::available( |
|
| 194 | + | &STORE.backings.pool, 1 |
|
| 195 | + | ); |
|
| 196 | + | } |
|
| 197 | + | ||
| 198 | + | /// New allocations are zeroed completely and carry the documented default rights. |
|
| 199 | + | @test unsafe fn allocation() throws (testing::TestError) { |
|
| 200 | + | initialize(); |
|
| 201 | + | let handle = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 2); |
|
| 202 | + | let entry = try! capability::get(&TABLE, handle); |
|
| 203 | + | try testing::expect(*entry.rights == (abi::READ | abi::WRITE | abi::GRANT | abi::TRANSFER)); |
|
| 204 | + | let page = try! pages::get(&STORE, entry.object); |
|
| 205 | + | try testing::expect(page.count == 2 and page.origin == TABLE.owner and page.handles == 1); |
|
| 206 | + | for i in OFFSET..OFFSET + 8192 { |
|
| 207 | + | try testing::expect(RAM[i] == 0); |
|
| 208 | + | } |
|
| 209 | + | try testing::expect(RAM[OFFSET + 8192] == 0xa5); |
|
| 210 | + | let mut invalid = false; |
|
| 211 | + | try pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 0) catch err { |
|
| 212 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 213 | + | set invalid = true; |
|
| 214 | + | }; |
|
| 215 | + | try testing::expect(invalid); |
|
| 216 | + | for count in &[65537 as u64, 0xffffffffffffffff] { |
|
| 217 | + | let mut exhausted = false; |
|
| 218 | + | try pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), count) catch err { |
|
| 219 | + | try testing::expect(err == abi::Error::OutOfMemory); |
|
| 220 | + | set exhausted = true; |
|
| 221 | + | }; |
|
| 222 | + | assert exhausted and frames::available(&STORE.backings.pool, 2); |
|
| 223 | + | } |
|
| 224 | + | } |
|
| 225 | + | ||
| 226 | + | /// Splitting preserves contents and backing identity while changing only page extents. |
|
| 227 | + | @test unsafe fn split() throws (testing::TestError) { |
|
| 228 | + | initialize(); |
|
| 229 | + | let handle = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 4); |
|
| 230 | + | set RAM[OFFSET + 8192] = 73; |
|
| 231 | + | let original = try! capability::get(&TABLE, handle); |
|
| 232 | + | for count in &[0 as u64, 4, 5, 0xffffffffffffffff] { |
|
| 233 | + | let mut failed = false; |
|
| 234 | + | try pages::split(&mut STORE, &mut TABLE, handle, count) catch err { |
|
| 235 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 236 | + | set failed = true; |
|
| 237 | + | }; |
|
| 238 | + | try testing::expect(failed); |
|
| 239 | + | } |
|
| 240 | + | let rightHandle = try! pages::split(&mut STORE, &mut TABLE, handle, 2); |
|
| 241 | + | let rightEntry = try! capability::get(&TABLE, rightHandle); |
|
| 242 | + | let left = try! pages::get(&STORE, original.object); |
|
| 243 | + | let right = try! pages::get(&STORE, rightEntry.object); |
|
| 244 | + | try testing::expect(left.count == 2 and right.count == 2 and right.base == left.base + 8192); |
|
| 245 | + | try testing::expect(left.backing == right.backing and left.origin == right.origin); |
|
| 246 | + | try testing::expect(original.rights == rightEntry.rights and RAM[OFFSET + 8192] == 73); |
|
| 247 | + | try testing::expect(STORE.backings.records[left.backing.index].pages == 2); |
|
| 248 | + | try! pages::retain(&mut STORE, original.object, TABLE.owner); |
|
| 249 | + | let mut shared = false; |
|
| 250 | + | try pages::split(&mut STORE, &mut TABLE, handle, 1) catch err { |
|
| 251 | + | try testing::expect(err == abi::Error::Busy); |
|
| 252 | + | set shared = true; |
|
| 253 | + | }; |
|
| 254 | + | try testing::expect(shared); |
|
| 255 | + | try! pages::release(&mut STORE, original.object); |
|
| 256 | + | } |
|
| 257 | + | ||
| 258 | + | /// Neither handle drop nor origin exit can release frames exposed to another live domain. |
|
| 259 | + | @test unsafe fn retainedPointers() throws (testing::TestError) { |
|
| 260 | + | initialize(); |
|
| 261 | + | let receiver = abi::Ref { index: 1, generation: 1 }; |
|
| 262 | + | try! backing::registerDomain(&mut STORE.backings, receiver); |
|
| 263 | + | let handle = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 8); |
|
| 264 | + | let entry = try! capability::get(&TABLE, handle); |
|
| 265 | + | try! pages::retain(&mut STORE, entry.object, receiver); |
|
| 266 | + | set RAM[OFFSET] = 99; |
|
| 267 | + | drop(handle); |
|
| 268 | + | try! pages::release(&mut STORE, entry.object); |
|
| 269 | + | try! backing::endDomain(&mut STORE.backings, TABLE.owner); |
|
| 270 | + | assert not frames::available(&STORE.backings.pool, 0) and RAM[OFFSET] == 99; |
|
| 271 | + | try! backing::endDomain(&mut STORE.backings, receiver); |
|
| 272 | + | assert frames::available(&STORE.backings.pool, 0); |
|
| 273 | + | let owner = abi::Ref { index: 0, generation: 2 }; |
|
| 274 | + | capability::initialize(&mut TABLE, owner); |
|
| 275 | + | try! backing::registerDomain(&mut STORE.backings, owner); |
|
| 276 | + | let authority = try! capability::install(&mut TABLE, capability::Entry { |
|
| 277 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(abi::ALLOCATE) |
|
| 278 | + | }); |
|
| 279 | + | try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 8); |
|
| 280 | + | try testing::expect(RAM[OFFSET] == 0); |
|
| 281 | + | } |
|
| 282 | + | ||
| 283 | + | /// A full capability table prevents allocation and splitting before payload mutation. |
|
| 284 | + | @test unsafe fn fullTable() throws (testing::TestError) { |
|
| 285 | + | initialize(); |
|
| 286 | + | let handle = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 4); |
|
| 287 | + | let entry = try! capability::get(&TABLE, handle); |
|
| 288 | + | let owner = TABLE.owner; |
|
| 289 | + | for i in 2..limits::HANDLES { |
|
| 290 | + | let filler = try! capability::install(&mut TABLE, capability::Entry { |
|
| 291 | + | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(0) |
|
| 292 | + | }); |
|
| 293 | + | } |
|
| 294 | + | let mut failed: u32 = 0; |
|
| 295 | + | try pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 1) catch err { |
|
| 296 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 297 | + | set failed += 1; |
|
| 298 | + | }; |
|
| 299 | + | try pages::split(&mut STORE, &mut TABLE, handle, 2) catch err { |
|
| 300 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 301 | + | set failed += 1; |
|
| 302 | + | }; |
|
| 303 | + | let page = try! pages::get(&STORE, entry.object); |
|
| 304 | + | assert failed == 2 and page.count == 4 and frames::available(&STORE.backings.pool, 4); |
|
| 305 | + | } |
|
| 306 | + | ||
| 307 | + | /// Failure at each storage stage releases all earlier reservations. |
|
| 308 | + | @test unsafe fn rollback() throws (testing::TestError) { |
|
| 309 | + | initialize(); |
|
| 310 | + | let full = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 8); |
|
| 311 | + | let mut failed = false; |
|
| 312 | + | try pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 1) catch err { |
|
| 313 | + | try testing::expect(err == abi::Error::OutOfMemory); |
|
| 314 | + | set failed = true; |
|
| 315 | + | }; |
|
| 316 | + | try testing::expect(failed and TABLE.slots[2].state == slots::State::Free); |
|
| 317 | + | try testing::expect( |
|
| 318 | + | STORE.slots[1].state == slots::State::Free |
|
| 319 | + | and STORE.backings.slots[1].state == slots::State::Free |
|
| 320 | + | ); |
|
| 321 | + | for stage in 0..2 { |
|
| 322 | + | initialize(); |
|
| 323 | + | if stage == 0 { |
|
| 324 | + | for i in 0..limits::ALLOCATIONS { |
|
| 325 | + | set STORE.backings.slots[i].state = slots::State::Retired; |
|
| 326 | + | } |
|
| 327 | + | } else { |
|
| 328 | + | for i in 0..limits::PAGES { |
|
| 329 | + | set STORE.slots[i].state = slots::State::Retired; |
|
| 330 | + | } |
|
| 331 | + | } |
|
| 332 | + | let mut exhausted = false; |
|
| 333 | + | try pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 1) catch err { |
|
| 334 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 335 | + | set exhausted = true; |
|
| 336 | + | }; |
|
| 337 | + | assert exhausted |
|
| 338 | + | and TABLE.slots[1].state == slots::State::Free |
|
| 339 | + | and frames::available(&STORE.backings.pool, 0); |
|
| 340 | + | if stage == 0 { |
|
| 341 | + | try testing::expect(STORE.slots[0].state == slots::State::Free); |
|
| 342 | + | } |
|
| 343 | + | } |
|
| 344 | + | } |
|
| 345 | + | ||
| 346 | + | /// A Domain handle with Allocate rights must name the caller to allocate RAM. |
|
| 347 | + | @test unsafe fn allocationAuthority() throws (testing::TestError) { |
|
| 348 | + | initialize(); |
|
| 349 | + | let other = try! capability::install(&mut TABLE, capability::Entry { |
|
| 350 | + | kind: abi::Kind::Domain, |
|
| 351 | + | object: abi::Ref { index: 1, generation: 1 }, |
|
| 352 | + | rights: abi::Rights(abi::ALLOCATE), |
|
| 353 | + | }); |
|
| 354 | + | let mut denied = false; |
|
| 355 | + | try pages::allocate(&mut STORE, &mut TABLE, other, 1) catch err { |
|
| 356 | + | try testing::expect(err == abi::Error::Denied); |
|
| 357 | + | set denied = true; |
|
| 358 | + | }; |
|
| 359 | + | assert denied |
|
| 360 | + | and frames::available(&STORE.backings.pool, 0) |
|
| 361 | + | and TABLE.slots[2].state == slots::State::Free; |
|
| 362 | + | } |
|
| 363 | + | ||
| 364 | + | /// Buffer authority covers complete ranges and rejects overflow and stale objects. |
|
| 365 | + | @test unsafe fn bufferAuthority() throws (testing::TestError) { |
|
| 366 | + | initialize(); |
|
| 367 | + | let handle = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 2); |
|
| 368 | + | let entry = try! capability::get(&TABLE, handle); |
|
| 369 | + | let page = try! pages::get(&STORE, entry.object); |
|
| 370 | + | try testing::expect(pages::accessible( |
|
| 371 | + | &STORE, &TABLE, page.base, 8192, abi::Rights(abi::READ | abi::WRITE) |
|
| 372 | + | )); |
|
| 373 | + | try testing::expect(pages::accessible( |
|
| 374 | + | &STORE, &TABLE, page.base + 8191, 1, abi::Rights(abi::WRITE) |
|
| 375 | + | )); |
|
| 376 | + | try testing::expect(not pages::accessible(&STORE, &TABLE, page.base + 8192, 1, abi::Rights( |
|
| 377 | + | abi::READ |
|
| 378 | + | ))); |
|
| 379 | + | try testing::expect(not pages::accessible(&STORE, &TABLE, page.base - 1, 2, abi::Rights( |
|
| 380 | + | abi::READ |
|
| 381 | + | ))); |
|
| 382 | + | try testing::expect(not pages::accessible( |
|
| 383 | + | &STORE, &TABLE, page.base, 0xffffffffffffffff, abi::Rights(abi::READ) |
|
| 384 | + | )); |
|
| 385 | + | let right = try! pages::split(&mut STORE, &mut TABLE, handle, 1); |
|
| 386 | + | try testing::expect(pages::accessible( |
|
| 387 | + | &STORE, &TABLE, page.base, 8192, abi::Rights(abi::WRITE) |
|
| 388 | + | )); |
|
| 389 | + | let rightSlot = try! abi::decode(right); |
|
| 390 | + | set TABLE.entries[rightSlot.object.index].rights = abi::Rights(abi::READ); |
|
| 391 | + | try testing::expect(not pages::accessible(&STORE, &TABLE, page.base, 8192, abi::Rights( |
|
| 392 | + | abi::WRITE |
|
| 393 | + | ))); |
|
| 394 | + | try testing::expect(pages::accessible(&STORE, &TABLE, page.base, 8192, abi::Rights(abi::READ))); |
|
| 395 | + | set TABLE.entries[rightSlot.object.index].object.generation += 1; |
|
| 396 | + | try testing::expect(not pages::accessible(&STORE, &TABLE, page.base, 8192, abi::Rights( |
|
| 397 | + | abi::READ |
|
| 398 | + | ))); |
|
| 399 | + | } |
|
| 400 | + | ||
| 401 | + | /// A stack consumes its handle and keeps its frames until the domain that is exposed to them ends. |
|
| 402 | + | @test unsafe fn stackConsumption() throws (testing::TestError) { |
|
| 403 | + | initialize(); |
|
| 404 | + | let stack = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 2); |
|
| 405 | + | let entry = try! capability::get(&TABLE, stack); |
|
| 406 | + | let page = try! pages::get(&STORE, entry.object); |
|
| 407 | + | let extent = try! pages::consumeStack(&mut STORE, &mut TABLE, stack); |
|
| 408 | + | assert extent.start == page.base and extent.end == page.base + 8192; |
|
| 409 | + | // The handle and the Page are gone, and the frames are held by the exposure of the owner. |
|
| 410 | + | try capability::get(&TABLE, stack) catch error { |
|
| 411 | + | assert error == abi::Error::BadHandle; |
|
| 412 | + | }; |
|
| 413 | + | assert STORE.slots[entry.object.index].state == slots::State::Free; |
|
| 414 | + | assert not frames::available(&STORE.backings.pool, 0) |
|
| 415 | + | and not frames::available(&STORE.backings.pool, 1); |
|
| 416 | + | try! backing::endDomain(&mut STORE.backings, TABLE.owner); |
|
| 417 | + | assert frames::available(&STORE.backings.pool, 0) and frames::available( |
|
| 418 | + | &STORE.backings.pool, 1 |
|
| 419 | + | ); |
|
| 420 | + | } |
|
| 421 | + | ||
| 422 | + | /// A stack Page with a second handle, a split sibling, or earlier access is refused whole. |
|
| 423 | + | @test unsafe fn stackRefusals() throws (testing::TestError) { |
|
| 424 | + | initialize(); |
|
| 425 | + | let shared = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 1); |
|
| 426 | + | let second = try! pages::selfGrant( |
|
| 427 | + | &mut STORE, &mut TABLE, shared, (abi::READ | abi::WRITE) as u64 |
|
| 428 | + | ); |
|
| 429 | + | let mut refused: u32 = 0; |
|
| 430 | + | try pages::consumeStack(&mut STORE, &mut TABLE, shared) catch error { |
|
| 431 | + | assert error == abi::Error::Busy; |
|
| 432 | + | set refused += 1; |
|
| 433 | + | }; |
|
| 434 | + | drop(second); |
|
| 435 | + | let accessed = try! capability::get(&TABLE, shared); |
|
| 436 | + | let page = try! pages::get(&STORE, accessed.object); |
|
| 437 | + | try! backing::markAccessed(&mut STORE.backings, page.backing); |
|
| 438 | + | try pages::consumeStack(&mut STORE, &mut TABLE, shared) catch error { |
|
| 439 | + | assert error == abi::Error::Denied; |
|
| 440 | + | set refused += 1; |
|
| 441 | + | }; |
|
| 442 | + | let pair = try! pages::allocate(&mut STORE, &mut TABLE, abi::Handle(0), 2); |
|
| 443 | + | let right = try! pages::split(&mut STORE, &mut TABLE, pair, 1); |
|
| 444 | + | try pages::consumeStack(&mut STORE, &mut TABLE, pair) catch error { |
|
| 445 | + | assert error == abi::Error::Busy; |
|
| 446 | + | set refused += 1; |
|
| 447 | + | }; |
|
| 448 | + | try pages::consumeStack(&mut STORE, &mut TABLE, right) catch error { |
|
| 449 | + | assert error == abi::Error::Busy; |
|
| 450 | + | set refused += 1; |
|
| 451 | + | }; |
|
| 452 | + | assert refused == 4; |
|
| 453 | + | // Nothing was consumed. |
|
| 454 | + | try! capability::get(&TABLE, shared); |
|
| 455 | + | try! capability::get(&TABLE, pair); |
|
| 456 | + | try! capability::get(&TABLE, right); |
|
| 457 | + | } |
kernel/kernel/tests/physical.rad
added
+436 -0
| 1 | + | //! Physical-memory boundary checks against a pool over mapped hosted memory. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::frames; |
|
| 6 | + | use kernel::physical; |
|
| 7 | + | ||
| 8 | + | /// Mapped bytes with room for page alignment and five frames. |
|
| 9 | + | static RAM: [u8; 24576] = [0; 24576]; |
|
| 10 | + | /// Frame metadata: four adjacent frames, then a mapped-address gap. |
|
| 11 | + | unsafe static POOL: frames::Pool = undefined; |
|
| 12 | + | /// First page-aligned test address. |
|
| 13 | + | static BASE: u64 = 0; |
|
| 14 | + | ||
| 15 | + | /// Frames 0 and 1 are allocated, frame 2 is free, frames 3 and 4 are allocated. |
|
| 16 | + | /// Frame 4 is not adjacent to frame 3 and is never dereferenced. |
|
| 17 | + | unsafe fn initialize() { |
|
| 18 | + | for i in 0..RAM.len { |
|
| 19 | + | set RAM[i] = 0xa5; |
|
| 20 | + | } |
|
| 21 | + | let pointer: *u8 = &RAM[0]; |
|
| 22 | + | let aligned = (pointer as u64 + 4095) & ~4095; |
|
| 23 | + | set BASE = aligned; |
|
| 24 | + | set POOL.count = 5; |
|
| 25 | + | for i in 0..1024 { |
|
| 26 | + | set POOL.free[i] = 0; |
|
| 27 | + | } |
|
| 28 | + | for i in 0..4 { |
|
| 29 | + | set POOL.addresses[i] = aligned + i as u64 * 4096; |
|
| 30 | + | } |
|
| 31 | + | set POOL.addresses[4] = aligned + 8 * 4096; |
|
| 32 | + | set POOL.free[0] = 1 << 2; |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Offset of a mapped test address inside RAM. |
|
| 36 | + | unsafe fn offset(address: u64) -> u32 { |
|
| 37 | + | let pointer: *u8 = &RAM[0]; |
|
| 38 | + | return (address - pointer as u64) as u32; |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | /// Run every operation on one range and count the ones rejected with InvalidArg. |
|
| 42 | + | unsafe fn rejected(address: u64, length: u32) -> u32 { |
|
| 43 | + | let mut count: u32 = 0; |
|
| 44 | + | let mut bytes: [u8; 8192] = undefined; |
|
| 45 | + | let mut words: [u64; 1024] = undefined; |
|
| 46 | + | for i in 0..bytes.len { |
|
| 47 | + | set bytes[i] = 1; |
|
| 48 | + | } |
|
| 49 | + | for i in 0..words.len { |
|
| 50 | + | set words[i] = 1; |
|
| 51 | + | } |
|
| 52 | + | try! physical::zero( |
|
| 53 | + | &POOL, address, length as u64 |
|
| 54 | + | ) catch err { |
|
| 55 | + | if err == abi::Error::InvalidArg { |
|
| 56 | + | set count += 1; |
|
| 57 | + | } |
|
| 58 | + | }; |
|
| 59 | + | try! physical::write( |
|
| 60 | + | &POOL, address, &bytes[..length] |
|
| 61 | + | ) catch err { |
|
| 62 | + | if err == abi::Error::InvalidArg { |
|
| 63 | + | set count += 1; |
|
| 64 | + | } |
|
| 65 | + | }; |
|
| 66 | + | try! physical::read( |
|
| 67 | + | &POOL, address, &mut bytes[..length] |
|
| 68 | + | ) catch err { |
|
| 69 | + | if err == abi::Error::InvalidArg { |
|
| 70 | + | set count += 1; |
|
| 71 | + | } |
|
| 72 | + | }; |
|
| 73 | + | try! physical::writeWords( |
|
| 74 | + | &POOL, address, &words[..length / 8] |
|
| 75 | + | ) catch err { |
|
| 76 | + | if err == abi::Error::InvalidArg { |
|
| 77 | + | set count += 1; |
|
| 78 | + | } |
|
| 79 | + | }; |
|
| 80 | + | try! physical::readWords( |
|
| 81 | + | &POOL, address, &mut words[..length / 8] |
|
| 82 | + | ) catch err { |
|
| 83 | + | if err == abi::Error::InvalidArg { |
|
| 84 | + | set count += 1; |
|
| 85 | + | } |
|
| 86 | + | }; |
|
| 87 | + | try! physical::fillWords( |
|
| 88 | + | &POOL, address, 9, length / 8 |
|
| 89 | + | ) catch err { |
|
| 90 | + | if err == abi::Error::InvalidArg { |
|
| 91 | + | set count += 1; |
|
| 92 | + | } |
|
| 93 | + | }; |
|
| 94 | + | return count; |
|
| 95 | + | } |
|
| 96 | + | ||
| 97 | + | /// Every mapped byte is still the initial pattern. |
|
| 98 | + | unsafe fn untouched() -> bool { |
|
| 99 | + | for i in 0..RAM.len { |
|
| 100 | + | if RAM[i] <> 0xa5 { |
|
| 101 | + | return false; |
|
| 102 | + | } |
|
| 103 | + | } |
|
| 104 | + | return true; |
|
| 105 | + | } |
|
| 106 | + | ||
| 107 | + | /// Ranges that wrap the address space are rejected before any access. |
|
| 108 | + | @test unsafe fn rejectsWrapping() throws (testing::TestError) { |
|
| 109 | + | initialize(); |
|
| 110 | + | try testing::expect(rejected(0xfffffffffffff000, 0x2000) == 6); |
|
| 111 | + | try testing::expect(rejected(0xffffffffffffffff, 8) == 6); |
|
| 112 | + | try testing::expect(rejected(BASE, 0) == 0); |
|
| 113 | + | try testing::expect(untouched()); |
|
| 114 | + | } |
|
| 115 | + | ||
| 116 | + | /// The empty token has no bytes: only zero-length operations succeed, and it cannot be widened. |
|
| 117 | + | @test unsafe fn emptyToken() throws (testing::TestError) { |
|
| 118 | + | initialize(); |
|
| 119 | + | let none = physical::empty(); |
|
| 120 | + | try testing::expect(physical::size(&none) == 0 and physical::start(&none) == 0); |
|
| 121 | + | try! physical::zeroAt(&none, 0, 0); |
|
| 122 | + | let mut empty: [u8; 0] = []; |
|
| 123 | + | try! physical::writeAt(&none, 0, &empty[..]); |
|
| 124 | + | try! physical::readAt(&none, 0, &mut empty[..]); |
|
| 125 | + | let mut failed: u32 = 0; |
|
| 126 | + | try physical::zeroAt(&none, 0, 8) catch err { |
|
| 127 | + | if err == abi::Error::InvalidArg { |
|
| 128 | + | set failed += 1; |
|
| 129 | + | } |
|
| 130 | + | }; |
|
| 131 | + | try physical::narrow(none, 0, 1) catch err { |
|
| 132 | + | if err == abi::Error::InvalidArg { |
|
| 133 | + | set failed += 1; |
|
| 134 | + | } |
|
| 135 | + | }; |
|
| 136 | + | try testing::expect(failed == 2); |
|
| 137 | + | try testing::expect(untouched()); |
|
| 138 | + | } |
|
| 139 | + | ||
| 140 | + | /// Ranges outside the pool, including the bytes next to it, are rejected. |
|
| 141 | + | @test unsafe fn rejectsOutsidePool() throws (testing::TestError) { |
|
| 142 | + | initialize(); |
|
| 143 | + | try testing::expect(rejected(0, 8) == 6); |
|
| 144 | + | try testing::expect(rejected(BASE - 8, 8) == 6); |
|
| 145 | + | try testing::expect(rejected(BASE - 8, 16) == 6); |
|
| 146 | + | try testing::expect(rejected(BASE + 5 * 4096, 8) == 6); |
|
| 147 | + | try testing::expect(rejected(BASE + 8 * 4096 + 4088, 16) == 6); |
|
| 148 | + | try testing::expect(untouched()); |
|
| 149 | + | } |
|
| 150 | + | ||
| 151 | + | /// A range that reaches a free frame is rejected whole, even when it starts in an allocated frame. |
|
| 152 | + | @test unsafe fn rejectsFreeFrames() throws (testing::TestError) { |
|
| 153 | + | initialize(); |
|
| 154 | + | try testing::expect(rejected(BASE + 2 * 4096, 8) == 6); |
|
| 155 | + | try testing::expect(rejected(BASE + 4096 + 4088, 16) == 6); |
|
| 156 | + | try testing::expect(rejected(BASE + 2 * 4096 - 8, 4096 + 16) == 6); |
|
| 157 | + | try testing::expect(rejected(BASE + 4096, 2 * 4096) == 6); |
|
| 158 | + | try testing::expect(untouched()); |
|
| 159 | + | set POOL.free[0] |= 1 << 1; |
|
| 160 | + | try testing::expect(rejected(BASE, 2 * 4096) == 6); |
|
| 161 | + | try testing::expect(untouched()); |
|
| 162 | + | } |
|
| 163 | + | ||
| 164 | + | /// A range spanning a gap in the frame map is rejected even when both ends are allocated frames. |
|
| 165 | + | @test unsafe fn rejectsMapGap() throws (testing::TestError) { |
|
| 166 | + | initialize(); |
|
| 167 | + | set POOL.free[0] = 0; |
|
| 168 | + | try testing::expect(rejected(BASE + 3 * 4096 + 4088, 16) == 6); |
|
| 169 | + | try testing::expect(rejected(BASE + 3 * 4096, 2 * 4096) == 6); |
|
| 170 | + | try testing::expect(untouched()); |
|
| 171 | + | } |
|
| 172 | + | ||
| 173 | + | /// Ranges that reach below, above, or beside the pool never touch the bytes outside it. |
|
| 174 | + | @test unsafe fn partialOverlap() throws (testing::TestError) { |
|
| 175 | + | initialize(); |
|
| 176 | + | set POOL.addresses[0] = BASE + 4096; |
|
| 177 | + | set POOL.addresses[1] = BASE + 2 * 4096; |
|
| 178 | + | set POOL.addresses[2] = BASE + 3 * 4096; |
|
| 179 | + | set POOL.addresses[3] = BASE + 4 * 4096; |
|
| 180 | + | set POOL.free[0] = 1 << 2; |
|
| 181 | + | try testing::expect(rejected(BASE + 4096 - 8, 16) == 6); |
|
| 182 | + | try testing::expect(rejected(BASE, 4096 + 8) == 6); |
|
| 183 | + | try testing::expect(untouched()); |
|
| 184 | + | } |
|
| 185 | + | ||
| 186 | + | /// Byte operations accept allocated frames and stay inside the requested range. |
|
| 187 | + | @test unsafe fn byteRoundTrip() throws (testing::TestError) { |
|
| 188 | + | initialize(); |
|
| 189 | + | try! physical::zero(&POOL, BASE + 4090, 12); |
|
| 190 | + | let start = offset(BASE); |
|
| 191 | + | for i in 0..RAM.len { |
|
| 192 | + | let inside = i >= start + 4090 and i < start + 4102; |
|
| 193 | + | if inside { |
|
| 194 | + | try testing::expect(RAM[i] == 0); |
|
| 195 | + | } else { |
|
| 196 | + | try testing::expect(RAM[i] == 0xa5); |
|
| 197 | + | } |
|
| 198 | + | } |
|
| 199 | + | let data = [1 as u8, 2, 3, 4, 5, 6, 7, 8, 9]; |
|
| 200 | + | try! physical::write(&POOL, BASE + 4093, &data[..]); |
|
| 201 | + | let mut back: [u8; 9] = [0; 9]; |
|
| 202 | + | try! physical::read(&POOL, BASE + 4093, &mut back[..]); |
|
| 203 | + | for i in 0..9 { |
|
| 204 | + | try testing::expect(back[i] == i as u8 + 1); |
|
| 205 | + | } |
|
| 206 | + | try testing::expect(RAM[start + 4092] == 0 and RAM[start + 4102] == 0xa5); |
|
| 207 | + | try! physical::zero(&POOL, BASE, 8192); |
|
| 208 | + | for i in 0..8192 { |
|
| 209 | + | try testing::expect(RAM[start + i] == 0); |
|
| 210 | + | } |
|
| 211 | + | try testing::expect(RAM[start + 8192] == 0xa5); |
|
| 212 | + | } |
|
| 213 | + | ||
| 214 | + | /// Word operations use little-endian storage with no alignment requirement. |
|
| 215 | + | @test unsafe fn wordRoundTrip() throws (testing::TestError) { |
|
| 216 | + | initialize(); |
|
| 217 | + | let start = offset(BASE); |
|
| 218 | + | try! physical::write32(&POOL, BASE + 5, 0x04030201); |
|
| 219 | + | try testing::expect( |
|
| 220 | + | RAM[start + 4] == 0xa5 |
|
| 221 | + | and RAM[start + 5] == 1 |
|
| 222 | + | and RAM[start + 8] == 4 |
|
| 223 | + | and RAM[start + 9] == 0xa5 |
|
| 224 | + | ); |
|
| 225 | + | let words = [0x0102030405060708 as u64, 0xffffffffffffffff, 0]; |
|
| 226 | + | try! physical::writeWords(&POOL, BASE + 16, &words[..]); |
|
| 227 | + | try testing::expect( |
|
| 228 | + | RAM[start + 16] == 8 |
|
| 229 | + | and RAM[start + 23] == 1 |
|
| 230 | + | and RAM[start + 24] == 0xff |
|
| 231 | + | and RAM[start + 40] == 0xa5 |
|
| 232 | + | ); |
|
| 233 | + | let mut back: [u64; 3] = [7; 3]; |
|
| 234 | + | try! physical::readWords(&POOL, BASE + 16, &mut back[..]); |
|
| 235 | + | try testing::expect(back[0] == words[0] and back[1] == words[1] and back[2] == 0); |
|
| 236 | + | try! physical::fillWords(&POOL, BASE + 100, 0x1122334455667788, 300); |
|
| 237 | + | let mut filled: [u64; 300] = undefined; |
|
| 238 | + | try! physical::readWords(&POOL, BASE + 100, &mut filled[..]); |
|
| 239 | + | for i in 0..300 { |
|
| 240 | + | try testing::expect(filled[i] == 0x1122334455667788); |
|
| 241 | + | } |
|
| 242 | + | try testing::expect(RAM[start + 99] == 0xa5 and RAM[start + 100 + 2400] == 0xa5); |
|
| 243 | + | let mut large: [u64; 200] = undefined; |
|
| 244 | + | for i in 0..200 { |
|
| 245 | + | set large[i] = 3; |
|
| 246 | + | } |
|
| 247 | + | try! physical::writeWords(&POOL, BASE + 4096 + 2000, &large[..]); |
|
| 248 | + | try! physical::readWords(&POOL, BASE + 4096 + 2000, &mut large[..]); |
|
| 249 | + | try testing::expect(large[199] == 3); |
|
| 250 | + | } |
|
| 251 | + | ||
| 252 | + | /// A range ending exactly at the last byte of an allocated frame is accepted, and one byte more is |
|
| 253 | + | /// not. |
|
| 254 | + | @test unsafe fn frameBoundary() throws (testing::TestError) { |
|
| 255 | + | initialize(); |
|
| 256 | + | try! physical::zero(&POOL, BASE + 4096 - 8, 8); |
|
| 257 | + | try! physical::zero(&POOL, BASE + 4096 - 8, 16); |
|
| 258 | + | try testing::expect(rejected(BASE + 2 * 4096 - 8, 16) == 6); |
|
| 259 | + | try! physical::zero(&POOL, BASE + 4 * 4096 - 8, 8); |
|
| 260 | + | } |
|
| 261 | + | ||
| 262 | + | /// A token derived from a reservation covers exactly its frames and needs no pool access. |
|
| 263 | + | @test unsafe fn reservedToken() throws (testing::TestError) { |
|
| 264 | + | initialize(); |
|
| 265 | + | let held = try! frames::reserve(&mut POOL, 1); |
|
| 266 | + | let token = physical::reserved(&POOL, &held); |
|
| 267 | + | let run = frames::commit(held); |
|
| 268 | + | try testing::expect(physical::size(&token) == 4096); |
|
| 269 | + | // Concurrent pool mutation elsewhere cannot matter: the pool is not consulted. |
|
| 270 | + | set POOL.free[0] = 0xff; |
|
| 271 | + | let start = offset(BASE) + 2 * 4096; |
|
| 272 | + | try! physical::zeroAt(&token, 0, 4096); |
|
| 273 | + | for i in 0..4096 { |
|
| 274 | + | try testing::expect(RAM[start + i] == 0); |
|
| 275 | + | } |
|
| 276 | + | try testing::expect(RAM[start - 1] == 0xa5 and RAM[start + 4096] == 0xa5); |
|
| 277 | + | let data = [7 as u8, 8, 9]; |
|
| 278 | + | try! physical::writeAt(&token, 4093, &data[..]); |
|
| 279 | + | let mut back: [u8; 3] = [0; 3]; |
|
| 280 | + | try! physical::readAt(&token, 4093, &mut back[..]); |
|
| 281 | + | try testing::expect(back[0] == 7 and back[2] == 9 and RAM[start + 4096] == 0xa5); |
|
| 282 | + | try! physical::writeWordsAt(&token, 8, &[0x0102030405060708 as u64]); |
|
| 283 | + | try! physical::write32At(&token, 16, 0x0a0b0c0d); |
|
| 284 | + | try! physical::fillWordsAt(&token, 24, 5, 3); |
|
| 285 | + | try testing::expect(RAM[start + 8] == 8 and RAM[start + 16] == 0x0d and RAM[start + 24] == 5); |
|
| 286 | + | let mut failures: u32 = 0; |
|
| 287 | + | try physical::zeroAt(&token, 4095, 2) catch err { |
|
| 288 | + | if err == abi::Error::InvalidArg { |
|
| 289 | + | set failures += 1; |
|
| 290 | + | } |
|
| 291 | + | }; |
|
| 292 | + | try physical::writeAt(&token, 4097, &data[..0]) catch err { |
|
| 293 | + | if err == abi::Error::InvalidArg { |
|
| 294 | + | set failures += 1; |
|
| 295 | + | } |
|
| 296 | + | }; |
|
| 297 | + | try physical::readAt( |
|
| 298 | + | &token, 0xffffffffffffffff, &mut back[..] |
|
| 299 | + | ) catch err { |
|
| 300 | + | if err == abi::Error::InvalidArg { |
|
| 301 | + | set failures += 1; |
|
| 302 | + | } |
|
| 303 | + | }; |
|
| 304 | + | try physical::fillWordsAt(&token, 4088, 1, 2) catch err { |
|
| 305 | + | if err == abi::Error::InvalidArg { |
|
| 306 | + | set failures += 1; |
|
| 307 | + | } |
|
| 308 | + | }; |
|
| 309 | + | try physical::writeWordsAt(&token, 0xfffffffffffffff8, &[1 as u64]) catch err { |
|
| 310 | + | if err == abi::Error::InvalidArg { |
|
| 311 | + | set failures += 1; |
|
| 312 | + | } |
|
| 313 | + | }; |
|
| 314 | + | try testing::expect(failures == 5 and RAM[start + 4096] == 0xa5); |
|
| 315 | + | try testing::expect(run.count == 1); |
|
| 316 | + | } |
|
| 317 | + | ||
| 318 | + | /// A pool-checked token is bounded to the checked range, and offsets cannot leave it. |
|
| 319 | + | @test unsafe fn checkedToken() throws (testing::TestError) { |
|
| 320 | + | initialize(); |
|
| 321 | + | let token = try! physical::access(&POOL, BASE + 100, 50); |
|
| 322 | + | try testing::expect(physical::size(&token) == 50); |
|
| 323 | + | try! physical::zeroAt(&token, 10, 40); |
|
| 324 | + | let start = offset(BASE) + 100; |
|
| 325 | + | try testing::expect( |
|
| 326 | + | RAM[start + 9] == 0xa5 |
|
| 327 | + | and RAM[start + 10] == 0 |
|
| 328 | + | and RAM[start + 49] == 0 |
|
| 329 | + | and RAM[start + 50] == 0xa5 |
|
| 330 | + | ); |
|
| 331 | + | let mut failures: u32 = 0; |
|
| 332 | + | try physical::zeroAt(&token, 10, 41) catch err { |
|
| 333 | + | if err == abi::Error::InvalidArg { |
|
| 334 | + | set failures += 1; |
|
| 335 | + | } |
|
| 336 | + | }; |
|
| 337 | + | try physical::zeroAt(&token, 51, 0) catch err { |
|
| 338 | + | if err == abi::Error::InvalidArg { |
|
| 339 | + | set failures += 1; |
|
| 340 | + | } |
|
| 341 | + | }; |
|
| 342 | + | try physical::access(&POOL, BASE + 2 * 4096, 1) catch err { |
|
| 343 | + | if err == abi::Error::InvalidArg { |
|
| 344 | + | set failures += 1; |
|
| 345 | + | } |
|
| 346 | + | }; |
|
| 347 | + | try testing::expect(failures == 3); |
|
| 348 | + | } |
|
| 349 | + | ||
| 350 | + | /// A fresh 50-byte token at an unaligned address, since `narrow` consumes its parent. |
|
| 351 | + | unsafe fn parent() -> physical::Access { |
|
| 352 | + | return try! physical::access(&POOL, BASE + 100, 50); |
|
| 353 | + | } |
|
| 354 | + | ||
| 355 | + | /// A narrowed token keeps the parent's start plus the offset, and rejects ranges past the parent's |
|
| 356 | + | /// end. |
|
| 357 | + | @test unsafe fn narrowToken() throws (testing::TestError) { |
|
| 358 | + | initialize(); |
|
| 359 | + | let inner = try! physical::narrow(parent(), 10, 30); |
|
| 360 | + | try testing::expect(physical::start(&inner) == BASE + 110 and physical::size(&inner) == 30); |
|
| 361 | + | let whole = try! physical::narrow(parent(), 0, 50); |
|
| 362 | + | try testing::expect(physical::start(&whole) == BASE + 100 and physical::size(&whole) == 50); |
|
| 363 | + | let empty = try! physical::narrow(parent(), 50, 0); |
|
| 364 | + | try testing::expect(physical::start(&empty) == BASE + 150 and physical::size(&empty) == 0); |
|
| 365 | + | let nested = try! physical::narrow(inner, 5, 25); |
|
| 366 | + | try testing::expect(physical::start(&nested) == BASE + 115 and physical::size(&nested) == 25); |
|
| 367 | + | let mut failures: u32 = 0; |
|
| 368 | + | try physical::narrow(parent(), 10, 41) catch err { |
|
| 369 | + | if err == abi::Error::InvalidArg { |
|
| 370 | + | set failures += 1; |
|
| 371 | + | } |
|
| 372 | + | }; |
|
| 373 | + | try physical::narrow(parent(), 51, 0) catch err { |
|
| 374 | + | if err == abi::Error::InvalidArg { |
|
| 375 | + | set failures += 1; |
|
| 376 | + | } |
|
| 377 | + | }; |
|
| 378 | + | try physical::narrow(parent(), 0, 51) catch err { |
|
| 379 | + | if err == abi::Error::InvalidArg { |
|
| 380 | + | set failures += 1; |
|
| 381 | + | } |
|
| 382 | + | }; |
|
| 383 | + | try physical::narrow( |
|
| 384 | + | parent(), 0xffffffffffffffff, 2 |
|
| 385 | + | ) catch err { |
|
| 386 | + | if err == abi::Error::InvalidArg { |
|
| 387 | + | set failures += 1; |
|
| 388 | + | } |
|
| 389 | + | }; |
|
| 390 | + | try physical::narrow( |
|
| 391 | + | parent(), 2, 0xffffffffffffffff |
|
| 392 | + | ) catch err { |
|
| 393 | + | if err == abi::Error::InvalidArg { |
|
| 394 | + | set failures += 1; |
|
| 395 | + | } |
|
| 396 | + | }; |
|
| 397 | + | try physical::narrow( |
|
| 398 | + | try! physical::narrow(parent(), 10, 30), 0, 31 |
|
| 399 | + | ) catch err { |
|
| 400 | + | if err == abi::Error::InvalidArg { |
|
| 401 | + | set failures += 1; |
|
| 402 | + | } |
|
| 403 | + | }; |
|
| 404 | + | try testing::expect(failures == 6); |
|
| 405 | + | } |
|
| 406 | + | ||
| 407 | + | /// A word load reads what a word store wrote and rejects ranges that straddle the end. |
|
| 408 | + | @test unsafe fn readWordAt() throws (testing::TestError) { |
|
| 409 | + | initialize(); |
|
| 410 | + | let token = try! physical::access(&POOL, BASE + 100, 24); |
|
| 411 | + | try! physical::writeWordsAt(&token, 0, &[0x0102030405060708 as u64, 0xfffffffffffffffe, 5]); |
|
| 412 | + | try testing::expect(try! physical::read64At(&token, 0) == 0x0102030405060708); |
|
| 413 | + | try testing::expect(try! physical::read64At(&token, 8) == 0xfffffffffffffffe); |
|
| 414 | + | try testing::expect(try! physical::read64At(&token, 16) == 5); |
|
| 415 | + | try! physical::writeAt(&token, 3, &[0x11 as u8, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]); |
|
| 416 | + | try testing::expect(try! physical::read64At(&token, 3) == 0x8877665544332211); |
|
| 417 | + | let mut failures: u32 = 0; |
|
| 418 | + | try physical::read64At(&token, 17) catch err { |
|
| 419 | + | if err == abi::Error::InvalidArg { |
|
| 420 | + | set failures += 1; |
|
| 421 | + | } |
|
| 422 | + | }; |
|
| 423 | + | try physical::read64At(&token, 24) catch err { |
|
| 424 | + | if err == abi::Error::InvalidArg { |
|
| 425 | + | set failures += 1; |
|
| 426 | + | } |
|
| 427 | + | }; |
|
| 428 | + | try physical::read64At( |
|
| 429 | + | &token, 0xffffffffffffffff |
|
| 430 | + | ) catch err { |
|
| 431 | + | if err == abi::Error::InvalidArg { |
|
| 432 | + | set failures += 1; |
|
| 433 | + | } |
|
| 434 | + | }; |
|
| 435 | + | try testing::expect(failures == 3); |
|
| 436 | + | } |
kernel/kernel/tests/platform.rad
added
+282 -0
| 1 | + | //! Platform resource discovery and reserved-memory exclusion fixtures. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::platform; |
|
| 5 | + | use kernel::range; |
|
| 6 | + | use kernel::plic; |
|
| 7 | + | use kernel::registers; |
|
| 8 | + | use kernel::abi; |
|
| 9 | + | ||
| 10 | + | /// Fixed FDT construction workspace with separate structure and string regions. |
|
| 11 | + | record Fixture: Copy { |
|
| 12 | + | /// Entire blob. |
|
| 13 | + | bytes: [u8; 4096], |
|
| 14 | + | /// Next structure byte. |
|
| 15 | + | offset: u32, |
|
| 16 | + | /// Next property-name byte. |
|
| 17 | + | strings: u32, |
|
| 18 | + | /// Stack range value offset for malformed-input checks. |
|
| 19 | + | stack: u32, |
|
| 20 | + | /// PLIC context-list payload for mapping rejection checks. |
|
| 21 | + | contexts: u32, |
|
| 22 | + | /// PLIC source-count payload for capacity checks. |
|
| 23 | + | sources: u32, |
|
| 24 | + | /// PLIC register extent payload for bounds checks. |
|
| 25 | + | plic: u32, |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | /// Write a big-endian fixture field. |
|
| 29 | + | fn put(f: &mut Fixture, offset: u32, width: u32, value: u64) { |
|
| 30 | + | for i in 0..width { |
|
| 31 | + | set f.bytes[offset + i] = (value >> ((width - i - 1) * 8) as u64) as u8; |
|
| 32 | + | } |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Append one structure token. |
|
| 36 | + | fn word(f: &mut Fixture, value: u32) { |
|
| 37 | + | let offset = f.offset; |
|
| 38 | + | put(f, offset, 4, value as u64); |
|
| 39 | + | set f.offset += 4; |
|
| 40 | + | } |
|
| 41 | + | ||
| 42 | + | /// Append a node name and its terminator. |
|
| 43 | + | fn begin(f: &mut Fixture, name: &[u8]) { |
|
| 44 | + | word(f, 1); |
|
| 45 | + | for i in 0..name.len { |
|
| 46 | + | set f.bytes[f.offset + i] = name[i]; |
|
| 47 | + | } |
|
| 48 | + | set f.offset = (f.offset + name.len + 4) & ~3; |
|
| 49 | + | } |
|
| 50 | + | ||
| 51 | + | /// Append a property header and allocate its zeroed payload. |
|
| 52 | + | fn property(f: &mut Fixture, name: &[u8], size: u32) -> u32 { |
|
| 53 | + | let nameOffset = f.strings - 3072; |
|
| 54 | + | word(f, 3); |
|
| 55 | + | word(f, size); |
|
| 56 | + | word(f, nameOffset); |
|
| 57 | + | for i in 0..name.len { |
|
| 58 | + | set f.bytes[f.strings + i] = name[i]; |
|
| 59 | + | } |
|
| 60 | + | set f.strings += name.len + 1; |
|
| 61 | + | let offset = f.offset; |
|
| 62 | + | set f.offset = (f.offset + size + 3) & ~3; |
|
| 63 | + | return offset; |
|
| 64 | + | } |
|
| 65 | + | ||
| 66 | + | /// Append a one-cell property. |
|
| 67 | + | fn scalar(f: &mut Fixture, name: &[u8], value: u32) { |
|
| 68 | + | let offset = property(f, name, 4); |
|
| 69 | + | put(f, offset, 4, value as u64); |
|
| 70 | + | } |
|
| 71 | + | ||
| 72 | + | /// Append a zero-terminated string property. |
|
| 73 | + | fn string(f: &mut Fixture, name: &[u8], value: &[u8]) { |
|
| 74 | + | let offset = property(f, name, value.len + 1); |
|
| 75 | + | for i in 0..value.len { |
|
| 76 | + | set f.bytes[offset + i] = value[i]; |
|
| 77 | + | } |
|
| 78 | + | } |
|
| 79 | + | ||
| 80 | + | /// Append a two-address-cell, two-size-cell range. |
|
| 81 | + | fn region(f: &mut Fixture, name: &[u8], start: u64, size: u64) -> u32 { |
|
| 82 | + | let offset = property(f, name, 16); |
|
| 83 | + | put(f, offset, 8, start); |
|
| 84 | + | put(f, offset + 8, 8, size); |
|
| 85 | + | return offset; |
|
| 86 | + | } |
|
| 87 | + | ||
| 88 | + | /// Construct the supported one-hart boot contract. |
|
| 89 | + | fn build(f: &mut Fixture) { |
|
| 90 | + | construct(f, false); |
|
| 91 | + | } |
|
| 92 | + | ||
| 93 | + | /// Construct a boot contract with optional supervisor and machine PLIC contexts. |
|
| 94 | + | fn construct(f: &mut Fixture, interrupts: bool) { |
|
| 95 | + | for i in 0..f.bytes.len { |
|
| 96 | + | set f.bytes[i] = 0; |
|
| 97 | + | } |
|
| 98 | + | set f.offset = 56; |
|
| 99 | + | set f.strings = 3072; |
|
| 100 | + | begin(f, &""[..]); |
|
| 101 | + | scalar(f, &"#address-cells"[..], 2); |
|
| 102 | + | scalar(f, &"#size-cells"[..], 2); |
|
| 103 | + | begin(f, &"chosen"[..]); |
|
| 104 | + | set f.stack = region(f, &"radiance,kernel-stacks"[..], 0x80008000, 0x2000); |
|
| 105 | + | word(f, 2); |
|
| 106 | + | begin(f, &"memory@80000000"[..]); |
|
| 107 | + | string(f, &"device_type"[..], &"memory"[..]); |
|
| 108 | + | let ram = region(f, &"reg"[..], 0x80000000, 0x10000); |
|
| 109 | + | word(f, 2); |
|
| 110 | + | begin(f, &"cpus"[..]); |
|
| 111 | + | scalar(f, &"#address-cells"[..], 1); |
|
| 112 | + | scalar(f, &"#size-cells"[..], 0); |
|
| 113 | + | scalar(f, &"timebase-frequency"[..], 10000000); |
|
| 114 | + | begin(f, &"cpu@0"[..]); |
|
| 115 | + | string(f, &"device_type"[..], &"cpu"[..]); |
|
| 116 | + | scalar(f, &"reg"[..], 0); |
|
| 117 | + | if interrupts { |
|
| 118 | + | begin(f, &"interrupt-controller"[..]); |
|
| 119 | + | string(f, &"compatible"[..], &"riscv,cpu-intc"[..]); |
|
| 120 | + | scalar(f, &"phandle"[..], 17); |
|
| 121 | + | scalar(f, &"#interrupt-cells"[..], 1); |
|
| 122 | + | let marker = property(f, &"interrupt-controller"[..], 0); |
|
| 123 | + | word(f, 2); |
|
| 124 | + | } |
|
| 125 | + | word(f, 2); |
|
| 126 | + | word(f, 2); |
|
| 127 | + | begin(f, &"reserved-memory"[..]); |
|
| 128 | + | scalar(f, &"#address-cells"[..], 2); |
|
| 129 | + | scalar(f, &"#size-cells"[..], 2); |
|
| 130 | + | let ranges = property(f, &"ranges"[..], 0); |
|
| 131 | + | begin(f, &"image@80002000"[..]); |
|
| 132 | + | let image = region(f, &"reg"[..], 0x80002000, 0x3000); |
|
| 133 | + | word(f, 2); |
|
| 134 | + | word(f, 2); |
|
| 135 | + | begin(f, &"soc"[..]); |
|
| 136 | + | scalar(f, &"#address-cells"[..], 2); |
|
| 137 | + | scalar(f, &"#size-cells"[..], 2); |
|
| 138 | + | let bus = property(f, &"ranges"[..], 0); |
|
| 139 | + | begin(f, &"uart@10000000"[..]); |
|
| 140 | + | string(f, &"compatible"[..], &"ns16550a"[..]); |
|
| 141 | + | let uart = region(f, &"reg"[..], 0x10000000, 0x100); |
|
| 142 | + | word(f, 2); |
|
| 143 | + | if interrupts { |
|
| 144 | + | begin(f, &"plic@c000000"[..]); |
|
| 145 | + | string(f, &"compatible"[..], &"riscv,plic0"[..]); |
|
| 146 | + | set f.plic = region(f, &"reg"[..], 0xc000000, 0x4000000); |
|
| 147 | + | scalar(f, &"#interrupt-cells"[..], 1); |
|
| 148 | + | let sources = property(f, &"riscv,ndev"[..], 4); |
|
| 149 | + | set f.sources = sources; |
|
| 150 | + | put(f, sources, 4, 31); |
|
| 151 | + | let contexts = property(f, &"interrupts-extended"[..], 16); |
|
| 152 | + | set f.contexts = contexts; |
|
| 153 | + | put(f, contexts, 4, 17); |
|
| 154 | + | put(f, contexts + 4, 4, 9); |
|
| 155 | + | put(f, contexts + 8, 4, 17); |
|
| 156 | + | put(f, contexts + 12, 4, 11); |
|
| 157 | + | word(f, 2); |
|
| 158 | + | } |
|
| 159 | + | word(f, 2); |
|
| 160 | + | word(f, 2); |
|
| 161 | + | word(f, 9); |
|
| 162 | + | let total = f.strings; |
|
| 163 | + | let size = f.offset - 56; |
|
| 164 | + | put(f, 0, 4, 0xd00dfeed); |
|
| 165 | + | put(f, 4, 4, total as u64); |
|
| 166 | + | put(f, 8, 4, 56); |
|
| 167 | + | put(f, 12, 4, 3072); |
|
| 168 | + | put(f, 16, 4, 40); |
|
| 169 | + | put(f, 20, 4, 17); |
|
| 170 | + | put(f, 24, 4, 16); |
|
| 171 | + | put(f, 32, 4, (total - 3072) as u64); |
|
| 172 | + | put(f, 36, 4, size as u64); |
|
| 173 | + | } |
|
| 174 | + | ||
| 175 | + | /// PLIC machine contexts follow reference-list order rather than physical hart ID. |
|
| 176 | + | @test unsafe fn interruptContexts() throws (testing::TestError) { |
|
| 177 | + | let mut f: Fixture = undefined; |
|
| 178 | + | construct(&mut f, true); |
|
| 179 | + | let mut p: platform::Platform = undefined; |
|
| 180 | + | try! platform::decode(&f.bytes[..f.strings], &mut p); |
|
| 181 | + | assert p.irqSources == 31 and p.irqContexts[0] == 1; |
|
| 182 | + | assert p.deviceCount == 2 and p.devices[1].kind == platform::Kind::Plic; |
|
| 183 | + | let initialContexts = f.contexts; |
|
| 184 | + | put(&mut f, initialContexts + 4, 4, 0xffffffff); |
|
| 185 | + | try! platform::decode(&f.bytes[..f.strings], &mut p); |
|
| 186 | + | assert p.irqContexts[0] == 1; |
|
| 187 | + | for change in 0..7 { |
|
| 188 | + | construct(&mut f, true); |
|
| 189 | + | let sources = f.sources; |
|
| 190 | + | let contexts = f.contexts; |
|
| 191 | + | let registers = f.plic; |
|
| 192 | + | match change { |
|
| 193 | + | case 0 => put(&mut f, sources, 4, 0), |
|
| 194 | + | case 1 => put(&mut f, sources, 4, 129), |
|
| 195 | + | case 2 => put(&mut f, contexts + 8, 4, 99), |
|
| 196 | + | case 3 => put(&mut f, contexts + 4, 4, 11), |
|
| 197 | + | case 4 => put(&mut f, contexts + 12, 4, 9), |
|
| 198 | + | case 5 => put(&mut f, registers + 8, 8, 0x200000), |
|
| 199 | + | else => put(&mut f, registers, 8, 0xc000002), |
|
| 200 | + | } |
|
| 201 | + | let mut rejected = false; |
|
| 202 | + | try platform::decode(&f.bytes[..f.strings], &mut p) catch error { |
|
| 203 | + | assert error == platform::Error::Invalid; |
|
| 204 | + | set rejected = true; |
|
| 205 | + | }; |
|
| 206 | + | assert rejected; |
|
| 207 | + | } |
|
| 208 | + | } |
|
| 209 | + | ||
| 210 | + | /// PLIC register selection uses the decoded machine context and rejects ambiguous mappings. |
|
| 211 | + | @test unsafe fn plicMapping() throws (testing::TestError) { |
|
| 212 | + | let mut f: Fixture = undefined; |
|
| 213 | + | construct(&mut f, true); |
|
| 214 | + | let mut p: platform::Platform = undefined; |
|
| 215 | + | try! platform::decode(&f.bytes[..f.strings], &mut p); |
|
| 216 | + | let selected = try! plic::mapping(&p) else panic "plicMapping: missing PLIC"; |
|
| 217 | + | assert registers::start(selected.registers) == 0xc000000 |
|
| 218 | + | and selected.count == 31 |
|
| 219 | + | and selected.context == 1; |
|
| 220 | + | set p.irqContexts[0] = 16; |
|
| 221 | + | let mut rejected = false; |
|
| 222 | + | try plic::mapping(&p) catch error { |
|
| 223 | + | assert error == abi::Error::InvalidArg; |
|
| 224 | + | set rejected = true; |
|
| 225 | + | }; |
|
| 226 | + | assert rejected; |
|
| 227 | + | set p.irqContexts[0] = 1; |
|
| 228 | + | set p.devices[2] = p.devices[1]; |
|
| 229 | + | set p.deviceCount = 3; |
|
| 230 | + | let mut duplicate = false; |
|
| 231 | + | try plic::mapping(&p) catch error { |
|
| 232 | + | assert error == abi::Error::InvalidArg; |
|
| 233 | + | set duplicate = true; |
|
| 234 | + | }; |
|
| 235 | + | assert duplicate; |
|
| 236 | + | set p.irqSources = 0; |
|
| 237 | + | assert try! plic::mapping(&p) == nil; |
|
| 238 | + | } |
|
| 239 | + | ||
| 240 | + | /// Resources and their reservations come from the tree rather than fixed addresses. |
|
| 241 | + | @test unsafe fn discovery() throws (testing::TestError) { |
|
| 242 | + | let mut f: Fixture = undefined; |
|
| 243 | + | build(&mut f); |
|
| 244 | + | let mut p: platform::Platform = undefined; |
|
| 245 | + | try! platform::decode(&f.bytes[..f.strings], &mut p); |
|
| 246 | + | try testing::expect(p.ramCount == 1 and p.harts == 1 and p.timebase == 10000000); |
|
| 247 | + | try testing::expect(p.deviceCount == 1 and p.devices[0].kind == platform::Kind::Uart); |
|
| 248 | + | try testing::expect(p.devices[0].memory.start == 0x10000000); |
|
| 249 | + | try testing::expect(p.stackCount == 1 and p.stacks[0].end == 0x8000a000); |
|
| 250 | + | try testing::expect(p.reservedCount == 3); |
|
| 251 | + | for i in 0..16 { |
|
| 252 | + | let page = range::new(0x80000000 + i as u64 * 4096, 4096) |
|
| 253 | + | else panic "discovery: valid page"; |
|
| 254 | + | let free = i < 2 or (i >= 5 and i < 8) or i >= 10; |
|
| 255 | + | try testing::expect(platform::available(&p, page) == free); |
|
| 256 | + | } |
|
| 257 | + | try testing::expect(not platform::available(&p, range::Range { |
|
| 258 | + | start: 0x7ffff000, end: 0x80001000 |
|
| 259 | + | })); |
|
| 260 | + | try testing::expect(not platform::available(&p, range::Range { |
|
| 261 | + | start: 0x80001000, end: 0x80003000 |
|
| 262 | + | })); |
|
| 263 | + | try testing::expect(not platform::available(&p, range::Range { |
|
| 264 | + | start: 0x80010000, end: 0x80011000 |
|
| 265 | + | })); |
|
| 266 | + | } |
|
| 267 | + | ||
| 268 | + | /// Misaligned, overflowing, and non-RAM kernel stacks cannot become boot state. |
|
| 269 | + | @test unsafe fn invalidStacks() throws (testing::TestError) { |
|
| 270 | + | let mut f: Fixture = undefined; |
|
| 271 | + | let mut p: platform::Platform = undefined; |
|
| 272 | + | for start in &[0x80008001 as u64, 0x70000000, 0xfffffffffffff000] { |
|
| 273 | + | build(&mut f); |
|
| 274 | + | let stack = f.stack; |
|
| 275 | + | put(&mut f, stack, 8, start); |
|
| 276 | + | let mut failed = false; |
|
| 277 | + | try platform::decode(&f.bytes[..f.strings], &mut p) catch { |
|
| 278 | + | set failed = true; |
|
| 279 | + | }; |
|
| 280 | + | try testing::expect(failed); |
|
| 281 | + | } |
|
| 282 | + | } |
kernel/kernel/tests/range.rad
added
+27 -0
| 1 | + | //! Bounds and overflow checks for kernel intervals. |
|
| 2 | + | ||
| 3 | + | use kernel::range; |
|
| 4 | + | use std::testing; |
|
| 5 | + | ||
| 6 | + | /// Check empty intervals and overflow at the address-space boundary. |
|
| 7 | + | @test fn construction() throws (testing::TestError) { |
|
| 8 | + | try testing::expect(range::new(0, 0) == nil); |
|
| 9 | + | try testing::expect(range::new(0xffffffffffffffff, 1) == nil); |
|
| 10 | + | try testing::expect(range::new(1, 0xffffffffffffffff) == nil); |
|
| 11 | + | let span = range::new(0, 0xffffffffffffffff) else { |
|
| 12 | + | throw testing::TestError::Failed; |
|
| 13 | + | }; |
|
| 14 | + | try testing::expect(span.start == 0); |
|
| 15 | + | try testing::expect(span.end == 0xffffffffffffffff); |
|
| 16 | + | } |
|
| 17 | + | ||
| 18 | + | /// Check exact bounds, partial overlap, and the excluded upper bound. |
|
| 19 | + | @test fn containment() throws (testing::TestError) { |
|
| 20 | + | let outer = range::Range { start: 4096, end: 8192 }; |
|
| 21 | + | try testing::expect(range::contains(outer, outer)); |
|
| 22 | + | try testing::expect(range::contains(outer, range::Range { start: 4097, end: 8191 })); |
|
| 23 | + | try testing::expect(not range::contains(outer, range::Range { start: 4095, end: 4097 })); |
|
| 24 | + | try testing::expect(not range::contains(outer, range::Range { start: 8191, end: 8193 })); |
|
| 25 | + | try testing::expect(not range::contains(outer, range::Range { start: 8192, end: 8193 })); |
|
| 26 | + | try testing::expect(not range::contains(outer, range::Range { start: 4096, end: 4096 })); |
|
| 27 | + | } |
kernel/kernel/tests/registers.rad
added
+117 -0
| 1 | + | //! Register windows: construction from decoded devices and access checks. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::platform; |
|
| 6 | + | use kernel::registers; |
|
| 7 | + | ||
| 8 | + | /// Synthetic platform with one RAM bank and one device of each mapped kind. |
|
| 9 | + | unsafe static MACHINE: platform::Platform = undefined; |
|
| 10 | + | ||
| 11 | + | /// Describe a UART, CLINT, and PLIC beside one RAM bank. |
|
| 12 | + | unsafe fn initialize() { |
|
| 13 | + | set MACHINE.ramCount = 1; |
|
| 14 | + | set MACHINE.ram[0] = { start: 0x80000000, end: 0x90000000 }; |
|
| 15 | + | set MACHINE.deviceCount = 3; |
|
| 16 | + | set MACHINE.devices[0] = { |
|
| 17 | + | kind: platform::Kind::Uart, |
|
| 18 | + | memory: { |
|
| 19 | + | start: 0x10000000, end: 0x10000100 |
|
| 20 | + | }, |
|
| 21 | + | }; |
|
| 22 | + | set MACHINE.devices[1] = { |
|
| 23 | + | kind: platform::Kind::Clint, |
|
| 24 | + | memory: { |
|
| 25 | + | start: 0x02000000, end: 0x02010000 |
|
| 26 | + | }, |
|
| 27 | + | }; |
|
| 28 | + | set MACHINE.devices[2] = { |
|
| 29 | + | kind: platform::Kind::Plic, |
|
| 30 | + | memory: { |
|
| 31 | + | start: 0x0c000000, end: 0x10000000 |
|
| 32 | + | }, |
|
| 33 | + | }; |
|
| 34 | + | } |
|
| 35 | + | ||
| 36 | + | /// Count rejections of an open request. |
|
| 37 | + | unsafe fn rejected(kind: platform::Kind, minimum: u64) -> bool { |
|
| 38 | + | try registers::open(&MACHINE, kind, minimum) catch error { |
|
| 39 | + | assert error == abi::Error::InvalidArg; |
|
| 40 | + | return true; |
|
| 41 | + | }; |
|
| 42 | + | return false; |
|
| 43 | + | } |
|
| 44 | + | ||
| 45 | + | /// A window covers the decoded device extent of the requested kind. |
|
| 46 | + | @test unsafe fn open() throws (testing::TestError) { |
|
| 47 | + | initialize(); |
|
| 48 | + | let uart = try! registers::open(&MACHINE, platform::Kind::Uart, 6); |
|
| 49 | + | assert registers::start(uart) == 0x10000000 and registers::size(uart) == 0x100; |
|
| 50 | + | let clint = try! registers::open(&MACHINE, platform::Kind::Clint, 0xc000); |
|
| 51 | + | assert registers::start(clint) == 0x02000000 and registers::size(clint) == 0x10000; |
|
| 52 | + | let plic = try! registers::open(&MACHINE, platform::Kind::Plic, 0x201000); |
|
| 53 | + | assert registers::start(plic) == 0x0c000000; |
|
| 54 | + | } |
|
| 55 | + | ||
| 56 | + | /// Requests fail for a missing kind, a short extent, or a zero minimum. |
|
| 57 | + | @test unsafe fn openRejectsMismatch() throws (testing::TestError) { |
|
| 58 | + | initialize(); |
|
| 59 | + | assert rejected(platform::Kind::Finish, 1); |
|
| 60 | + | assert rejected(platform::Kind::Uart, 0x101); |
|
| 61 | + | assert rejected(platform::Kind::Uart, 0); |
|
| 62 | + | set MACHINE.deviceCount = 0; |
|
| 63 | + | assert rejected(platform::Kind::Uart, 1); |
|
| 64 | + | } |
|
| 65 | + | ||
| 66 | + | /// A device range that wraps the address space or runs backwards is rejected. |
|
| 67 | + | @test unsafe fn openRejectsWrapping() throws (testing::TestError) { |
|
| 68 | + | initialize(); |
|
| 69 | + | set MACHINE.devices[0].memory = { start: 0xfffffffffffffff0, end: 0x10 }; |
|
| 70 | + | assert rejected(platform::Kind::Uart, 1); |
|
| 71 | + | set MACHINE.devices[0].memory = { start: 0x10000100, end: 0x10000000 }; |
|
| 72 | + | assert rejected(platform::Kind::Uart, 1); |
|
| 73 | + | } |
|
| 74 | + | ||
| 75 | + | /// A device range that touches any RAM byte is rejected, adjacent ranges are not. |
|
| 76 | + | @test unsafe fn openRejectsRam() throws (testing::TestError) { |
|
| 77 | + | initialize(); |
|
| 78 | + | set MACHINE.devices[0].memory = { start: 0x7fffff00, end: 0x80000000 }; |
|
| 79 | + | assert not rejected(platform::Kind::Uart, 1); |
|
| 80 | + | set MACHINE.devices[0].memory = { start: 0x7fffff00, end: 0x80000001 }; |
|
| 81 | + | assert rejected(platform::Kind::Uart, 1); |
|
| 82 | + | set MACHINE.devices[0].memory = { start: 0x8fffff00, end: 0x90000100 }; |
|
| 83 | + | assert rejected(platform::Kind::Uart, 1); |
|
| 84 | + | set MACHINE.devices[0].memory = { start: 0x90000000, end: 0x90000100 }; |
|
| 85 | + | assert not rejected(platform::Kind::Uart, 1); |
|
| 86 | + | set MACHINE.devices[0].memory = { start: 0x70000000, end: 0xa0000000 }; |
|
| 87 | + | assert rejected(platform::Kind::Uart, 1); |
|
| 88 | + | set MACHINE.ram[0] = { start: 0x70000000, end: 0xa0000000 }; |
|
| 89 | + | set MACHINE.devices[0].memory = { start: 0x80000000, end: 0x80000100 }; |
|
| 90 | + | assert rejected(platform::Kind::Uart, 1); |
|
| 91 | + | } |
|
| 92 | + | ||
| 93 | + | /// Offsets must keep the whole access inside the window. |
|
| 94 | + | @test unsafe fn bounds() throws (testing::TestError) { |
|
| 95 | + | initialize(); |
|
| 96 | + | let uart = try! registers::open(&MACHINE, platform::Kind::Uart, 6); |
|
| 97 | + | assert registers::contains(uart, 0, 1) and registers::contains(uart, 0xff, 1); |
|
| 98 | + | assert not registers::contains(uart, 0x100, 1); |
|
| 99 | + | assert registers::contains(uart, 0xfc, 4) and not registers::contains(uart, 0xfd, 4); |
|
| 100 | + | assert registers::contains(uart, 0xf8, 8) and not registers::contains(uart, 0xfc, 8); |
|
| 101 | + | assert not registers::contains(uart, 0xffffffffffffffff, 1); |
|
| 102 | + | assert not registers::contains(uart, 0xfffffffffffffffc, 8); |
|
| 103 | + | assert not registers::contains(uart, 0, 0x101); |
|
| 104 | + | } |
|
| 105 | + | ||
| 106 | + | /// Accesses need natural alignment of the device address, not only of the offset. |
|
| 107 | + | @test unsafe fn alignment() throws (testing::TestError) { |
|
| 108 | + | initialize(); |
|
| 109 | + | let clint = try! registers::open(&MACHINE, platform::Kind::Clint, 0xc000); |
|
| 110 | + | assert registers::contains(clint, 0xbff8, 8) and registers::contains(clint, 4, 4); |
|
| 111 | + | assert not registers::contains(clint, 4, 8) and not registers::contains(clint, 2, 4); |
|
| 112 | + | assert registers::contains(clint, 3, 1); |
|
| 113 | + | set MACHINE.devices[1].memory = { start: 0x02000004, end: 0x02010004 }; |
|
| 114 | + | let shifted = try! registers::open(&MACHINE, platform::Kind::Clint, 0xc000); |
|
| 115 | + | assert registers::contains(shifted, 0, 4) and not registers::contains(shifted, 0, 8); |
|
| 116 | + | assert registers::contains(shifted, 4, 8) and not registers::contains(shifted, 2, 4); |
|
| 117 | + | } |
kernel/kernel/tests/registry.rad
added
+234 -0
| 1 | + | //! Resident package identity, dependency admission, and image capabilities. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use std::arch::rv64::shared; |
|
| 5 | + | use std::arch::rv64::shared::catalog; |
|
| 6 | + | use kernel::abi; |
|
| 7 | + | use kernel::slots; |
|
| 8 | + | use kernel::capability; |
|
| 9 | + | use kernel::registry; |
|
| 10 | + | ||
| 11 | + | /// Resident registry metadata workspace. |
|
| 12 | + | unsafe static STORE: registry::Store = undefined; |
|
| 13 | + | /// Persistent distinct names for complete registry exhaustion. |
|
| 14 | + | unsafe static NAMES: [[u8; 4]; 256] = undefined; |
|
| 15 | + | ||
| 16 | + | /// Construct persistent compiled metadata for registry contract tests. |
|
| 17 | + | fn package(name: *[u8], dependencies: *[*[u8]], slot: u32) -> shared::Package { |
|
| 18 | + | return shared::Package { |
|
| 19 | + | name, |
|
| 20 | + | dependencies, |
|
| 21 | + | slot, |
|
| 22 | + | codeAddress: 0x80000000 + slot as u64 * 4096, |
|
| 23 | + | code: &[0x00008067 as u32], |
|
| 24 | + | exports: &[], |
|
| 25 | + | entry: nil, |
|
| 26 | + | template: &[], |
|
| 27 | + | memory: 0, |
|
| 28 | + | alignment: 8, |
|
| 29 | + | relocations: &[], |
|
| 30 | + | }; |
|
| 31 | + | } |
|
| 32 | + | ||
| 33 | + | /// Admit persistent package bytes and metadata through a reserved registry slot. |
|
| 34 | + | unsafe fn register(name: *[u8], dependencies: *[*[u8]], bytes: *[u8]) -> abi::Ref |
|
| 35 | + | throws (abi::Error) |
|
| 36 | + | { |
|
| 37 | + | let pending = try registry::reserve(&mut STORE); |
|
| 38 | + | let slot = slots::reference(&pending); |
|
| 39 | + | return try registry::publish(&mut STORE, pending, bytes, package( |
|
| 40 | + | name, dependencies, slot.index |
|
| 41 | + | )); |
|
| 42 | + | } |
|
| 43 | + | ||
| 44 | + | /// Admission resolves dependencies before its reserved package becomes visible. |
|
| 45 | + | @test unsafe fn stagedAdmission() throws (testing::TestError) { |
|
| 46 | + | registry::initialize(&mut STORE); |
|
| 47 | + | let base = try! register("base", &[], "base bytes"); |
|
| 48 | + | let pending = try! registry::reserve(&mut STORE); |
|
| 49 | + | let object = slots::reference(&pending); |
|
| 50 | + | let admitted = try! registry::admit(®istry::capture(&STORE), object, "child bytes", package( |
|
| 51 | + | "child", &["base"], object.index |
|
| 52 | + | )); |
|
| 53 | + | assert STORE.slots[object.index].state == slots::State::Reserved; |
|
| 54 | + | assert registry::find(®istry::capture(&STORE), &"child"[..]) == nil; |
|
| 55 | + | assert admitted.dependencies[0] == base; |
|
| 56 | + | let published = registry::commit(&mut STORE, pending, admitted); |
|
| 57 | + | assert published == object and STORE.counts[object.index] == 1; |
|
| 58 | + | assert STORE.dependencies[object.index][0] == base; |
|
| 59 | + | let rejected = try! registry::reserve(&mut STORE); |
|
| 60 | + | let slot = slots::reference(&rejected); |
|
| 61 | + | let mut duplicate = false; |
|
| 62 | + | try registry::admit(®istry::capture(&STORE), slot, "bad bytes", package( |
|
| 63 | + | "bad", |
|
| 64 | + | &[ |
|
| 65 | + | "base", "base" |
|
| 66 | + | ], |
|
| 67 | + | slot.index |
|
| 68 | + | )) catch error { |
|
| 69 | + | assert error == abi::Error::VerifyFailed; |
|
| 70 | + | set duplicate = true; |
|
| 71 | + | }; |
|
| 72 | + | assert duplicate and STORE.slots[slot.index].state == slots::State::Reserved; |
|
| 73 | + | registry::cancel(&mut STORE, rejected); |
|
| 74 | + | assert STORE.slots[slot.index].state == slots::State::Free; |
|
| 75 | + | } |
|
| 76 | + | ||
| 77 | + | /// Identical bytes reuse one package; a name cannot identify different content. |
|
| 78 | + | @test unsafe fn identity() throws (testing::TestError) { |
|
| 79 | + | registry::initialize(&mut STORE); |
|
| 80 | + | let first = try! register("a", &[], "binary a"); |
|
| 81 | + | let second = try! register("a", &[], "binary a"); |
|
| 82 | + | try testing::expect(first == second and STORE.slots[1].state == slots::State::Free); |
|
| 83 | + | let found = try! registry::identify(®istry::capture(&STORE), &"a"[..], &"binary a"[..]); |
|
| 84 | + | try testing::expect(found == first); |
|
| 85 | + | let mut conflict = false; |
|
| 86 | + | try register("a", &[], "different") catch err { |
|
| 87 | + | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 88 | + | set conflict = true; |
|
| 89 | + | }; |
|
| 90 | + | try testing::expect(conflict and STORE.slots[1].state == slots::State::Free); |
|
| 91 | + | } |
|
| 92 | + | ||
| 93 | + | /// Dependencies must already be resident and retain shared identity in a diamond. |
|
| 94 | + | @test unsafe fn dependencyGraph() throws (testing::TestError) { |
|
| 95 | + | registry::initialize(&mut STORE); |
|
| 96 | + | let mut missing = false; |
|
| 97 | + | try register("left", &["base"], "left") catch err { |
|
| 98 | + | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 99 | + | set missing = true; |
|
| 100 | + | }; |
|
| 101 | + | try testing::expect(missing and STORE.slots[0].state == slots::State::Free); |
|
| 102 | + | let base = try! register("base", &[], "base"); |
|
| 103 | + | let left = try! register("left", &["base"], "left"); |
|
| 104 | + | let right = try! register("right", &["base"], "right"); |
|
| 105 | + | let root = try! register("root", &["left", "right"], "root"); |
|
| 106 | + | try testing::expect( |
|
| 107 | + | STORE.dependencies[left.index][0] == base |
|
| 108 | + | and STORE.dependencies[right.index][0] == base |
|
| 109 | + | ); |
|
| 110 | + | try testing::expect( |
|
| 111 | + | STORE.dependencies[root.index][0] == left |
|
| 112 | + | and STORE.dependencies[root.index][1] == right |
|
| 113 | + | ); |
|
| 114 | + | } |
|
| 115 | + | ||
| 116 | + | /// Image handles name resident packages and dropping a handle does not unload code. |
|
| 117 | + | @test unsafe fn images() throws (testing::TestError) { |
|
| 118 | + | registry::initialize(&mut STORE); |
|
| 119 | + | let object = try! register("image", &[], "image"); |
|
| 120 | + | let mut table: capability::Table = undefined; |
|
| 121 | + | capability::initialize(&mut table, abi::Ref { index: 0, generation: 1 }); |
|
| 122 | + | let handle = try! registry::install(&STORE, &mut table, object); |
|
| 123 | + | let found = try! registry::image(&STORE, &table, handle, abi::Rights(abi::EXECUTE)); |
|
| 124 | + | try testing::expect(found == object); |
|
| 125 | + | try! capability::invalidate(&mut table, handle); |
|
| 126 | + | let resident = try! registry::get(&STORE, object); |
|
| 127 | + | try testing::expect(resident.slot == object.index); |
|
| 128 | + | let mut stale = false; |
|
| 129 | + | try registry::image(&STORE, &table, handle, abi::Rights(0)) catch err { |
|
| 130 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 131 | + | set stale = true; |
|
| 132 | + | }; |
|
| 133 | + | try testing::expect(stale); |
|
| 134 | + | } |
|
| 135 | + | ||
| 136 | + | /// Boot admission uses the same identity and dependency records as runtime publication. |
|
| 137 | + | @test unsafe fn bootCatalog() throws (testing::TestError) { |
|
| 138 | + | registry::initialize(&mut STORE); |
|
| 139 | + | let catalog = [ |
|
| 140 | + | catalog::Entry { source: "base", package: package("base", &[], 0) }, |
|
| 141 | + | catalog::Entry { source: "app", package: package("app", &["base"], 1) }, |
|
| 142 | + | ]; |
|
| 143 | + | try! registry::boot(&mut STORE, &catalog[..]); |
|
| 144 | + | try! registry::boot(&mut STORE, &catalog[..]); |
|
| 145 | + | try testing::expect(STORE.counts[1] == 1 and STORE.dependencies[1][0].index == 0); |
|
| 146 | + | try testing::expect(STORE.slots[2].state == slots::State::Free); |
|
| 147 | + | } |
|
| 148 | + | ||
| 149 | + | /// Export lookup and dependency import collection preserve code and private-data targets. |
|
| 150 | + | @test unsafe fn exports() throws (testing::TestError) { |
|
| 151 | + | registry::initialize(&mut STORE); |
|
| 152 | + | let pending = try! registry::reserve(&mut STORE); |
|
| 153 | + | let mut native = package("base", &[], 0); |
|
| 154 | + | set native.exports = &[ |
|
| 155 | + | shared::Symbol { name: "base::run", target: shared::Target::Function(0x80000000) }, |
|
| 156 | + | shared::Symbol { |
|
| 157 | + | name: "base::state", |
|
| 158 | + | target: shared::Target::Data(shared::DataRef { slot: 0, offset: 8 }), |
|
| 159 | + | }, |
|
| 160 | + | ]; |
|
| 161 | + | let object = try! registry::publish(&mut STORE, pending, "base", native); |
|
| 162 | + | let target = try! registry::exported(&STORE, object, &"base::run"[..]); |
|
| 163 | + | try testing::expect(target == shared::Target::Function(0x80000000)); |
|
| 164 | + | let mut output: [shared::Symbol; 2] = undefined; |
|
| 165 | + | let count = try! registry::imports(®istry::capture(&STORE), &["base"], &mut output[..]); |
|
| 166 | + | try testing::expect( |
|
| 167 | + | count == 2 |
|
| 168 | + | and output[1].target == shared::Target::Data(shared::DataRef { slot: 0, offset: 8 }) |
|
| 169 | + | ); |
|
| 170 | + | let saved = output[0]; |
|
| 171 | + | let mut failures: u32 = 0; |
|
| 172 | + | try registry::imports(®istry::capture(&STORE), &["base"], &mut output[..1]) catch err { |
|
| 173 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 174 | + | set failures += 1; |
|
| 175 | + | }; |
|
| 176 | + | try registry::exported(&STORE, object, &"base::private"[..]) catch err { |
|
| 177 | + | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 178 | + | set failures += 1; |
|
| 179 | + | }; |
|
| 180 | + | try registry::imports(®istry::capture(&STORE), &["absent"], &mut output[..]) catch err { |
|
| 181 | + | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 182 | + | set failures += 1; |
|
| 183 | + | }; |
|
| 184 | + | try testing::expect(failures == 3 and output[0] == saved); |
|
| 185 | + | } |
|
| 186 | + | ||
| 187 | + | /// Full registry exhaustion leaves existing packages available for duplicate lookup. |
|
| 188 | + | @test unsafe fn capacity() throws (testing::TestError) { |
|
| 189 | + | registry::initialize(&mut STORE); |
|
| 190 | + | for i in 0..256 { |
|
| 191 | + | set NAMES[i] = [ |
|
| 192 | + | 112 as u8, (48 + i / 100) as u8, (48 + i / 10 % 10) as u8, (48 + i % 10) as u8 |
|
| 193 | + | ]; |
|
| 194 | + | let object = try! register(&NAMES[i][..], &[], &NAMES[i][..]); |
|
| 195 | + | try testing::expect(object.index == i); |
|
| 196 | + | } |
|
| 197 | + | let mut full = false; |
|
| 198 | + | try register("extra", &[], "extra") catch err { |
|
| 199 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 200 | + | set full = true; |
|
| 201 | + | }; |
|
| 202 | + | try testing::expect(full); |
|
| 203 | + | let found = try! registry::identify( |
|
| 204 | + | ®istry::capture(&STORE), &NAMES[255][..], &NAMES[255][..] |
|
| 205 | + | ) else panic "capacity: resident package"; |
|
| 206 | + | try testing::expect(found.index == 255); |
|
| 207 | + | } |
|
| 208 | + | ||
| 209 | + | /// A captured view exposes only the packages published at capture, and its reads |
|
| 210 | + | /// are unaffected by later changes to the registry. |
|
| 211 | + | @test unsafe fn publishedView() throws (testing::TestError) { |
|
| 212 | + | registry::initialize(&mut STORE); |
|
| 213 | + | let base = try! register("base", &[], "base bytes"); |
|
| 214 | + | let pending = try! registry::reserve(&mut STORE); |
|
| 215 | + | let reserved = slots::reference(&pending); |
|
| 216 | + | let view = registry::capture(&STORE); |
|
| 217 | + | let later = try! register("later", &[], "later bytes"); |
|
| 218 | + | assert registry::find(&view, &"base"[..]) == base; |
|
| 219 | + | assert registry::find(&view, &"later"[..]) == nil; |
|
| 220 | + | assert (try! registry::lookup(&view, base)).name.len == 4; |
|
| 221 | + | let mut rejected: u32 = 0; |
|
| 222 | + | try registry::lookup(&view, later) catch error { |
|
| 223 | + | assert error == abi::Error::BadHandle; |
|
| 224 | + | set rejected += 1; |
|
| 225 | + | }; |
|
| 226 | + | try registry::describe(&view, reserved) catch error { |
|
| 227 | + | assert error == abi::Error::BadHandle; |
|
| 228 | + | set rejected += 1; |
|
| 229 | + | }; |
|
| 230 | + | assert rejected == 2; |
|
| 231 | + | let fresh = registry::capture(&STORE); |
|
| 232 | + | assert registry::find(&fresh, &"later"[..]) == later; |
|
| 233 | + | registry::cancel(&mut STORE, pending); |
|
| 234 | + | } |
kernel/kernel/tests/remote.rad
added
+226 -0
| 1 | + | //! Bounded request queues, acknowledgement identity, and capacity recovery. |
|
| 2 | + | use std::testing; |
|
| 3 | + | use kernel::abi; |
|
| 4 | + | use kernel::remote; |
|
| 5 | + | use kernel::limits; |
|
| 6 | + | use kernel::slots; |
|
| 7 | + | ||
| 8 | + | /// Serialized request state shared by two test harts. |
|
| 9 | + | unsafe static STORE: remote::Store = undefined; |
|
| 10 | + | ||
| 11 | + | /// Requests retain source ownership until an explicit acknowledgement is released. |
|
| 12 | + | @test unsafe fn acknowledgement() throws (testing::TestError) { |
|
| 13 | + | remote::initialize(&mut STORE, 3); |
|
| 14 | + | let id = try! remote::send(&mut STORE, 0, 1, remote::Action::Reschedule); |
|
| 15 | + | assert not try! remote::completed(&STORE, 0, id); |
|
| 16 | + | let delivery = try! remote::take(&mut STORE, 1) else panic "acknowledgement: missing request"; |
|
| 17 | + | assert delivery.id == id and delivery.request.source == 0; |
|
| 18 | + | assert not remote::acknowledge(&mut STORE, 0, id); |
|
| 19 | + | assert remote::acknowledge(&mut STORE, 1, id); |
|
| 20 | + | assert not remote::acknowledge(&mut STORE, 1, id); |
|
| 21 | + | assert try! remote::completed(&STORE, 0, id); |
|
| 22 | + | let mut denied = false; |
|
| 23 | + | try remote::release(&mut STORE, 1, id) catch error { |
|
| 24 | + | assert error == abi::Error::BadHandle; |
|
| 25 | + | set denied = true; |
|
| 26 | + | }; |
|
| 27 | + | assert denied; |
|
| 28 | + | try! remote::release(&mut STORE, 0, id); |
|
| 29 | + | assert not remote::acknowledge(&mut STORE, 1, id); |
|
| 30 | + | } |
|
| 31 | + | ||
| 32 | + | /// Cancellation leaves a harmless stale queue entry until the receiver drains it. |
|
| 33 | + | @test unsafe fn saturation() throws (testing::TestError) { |
|
| 34 | + | remote::initialize(&mut STORE, 3); |
|
| 35 | + | let mut ids: [abi::Ref; 16] = undefined; |
|
| 36 | + | for i in 0..16 { |
|
| 37 | + | set ids[i] = try! remote::send(&mut STORE, 0, 1, remote::Action::Reschedule); |
|
| 38 | + | } |
|
| 39 | + | let mut full = false; |
|
| 40 | + | try remote::send(&mut STORE, 0, 1, remote::Action::Reschedule) catch error { |
|
| 41 | + | assert error == abi::Error::Busy; |
|
| 42 | + | set full = true; |
|
| 43 | + | }; |
|
| 44 | + | assert full; |
|
| 45 | + | try! remote::cancel(&mut STORE, 0, ids[0]); |
|
| 46 | + | for i in 1..16 { |
|
| 47 | + | let delivery = try! remote::take(&mut STORE, 1) |
|
| 48 | + | else panic "saturation: missing queued request"; |
|
| 49 | + | assert delivery.id == ids[i]; |
|
| 50 | + | assert remote::acknowledge(&mut STORE, 1, delivery.id); |
|
| 51 | + | try! remote::release(&mut STORE, 0, delivery.id); |
|
| 52 | + | } |
|
| 53 | + | assert try! remote::take(&mut STORE, 1) == nil; |
|
| 54 | + | for i in 0..40 { |
|
| 55 | + | let id = try! remote::send(&mut STORE, 0, 1, remote::Action::Reschedule); |
|
| 56 | + | let delivery = try! remote::take(&mut STORE, 1) else panic "saturation: request reuse"; |
|
| 57 | + | assert delivery.id == id and id <> ids[0]; |
|
| 58 | + | assert not remote::acknowledge(&mut STORE, 1, ids[0]); |
|
| 59 | + | assert remote::acknowledge(&mut STORE, 1, id); |
|
| 60 | + | try! remote::release(&mut STORE, 0, id); |
|
| 61 | + | } |
|
| 62 | + | } |
|
| 63 | + | ||
| 64 | + | /// Completed records retain capacity until their source releases them. |
|
| 65 | + | @test unsafe fn retainedCapacity() throws (testing::TestError) { |
|
| 66 | + | remote::initialize(&mut STORE, 3); |
|
| 67 | + | let mut ids: [abi::Ref; limits::REMOTE_REQUESTS] = undefined; |
|
| 68 | + | for i in 0..limits::REMOTE_REQUESTS { |
|
| 69 | + | let id = try! remote::send(&mut STORE, 0, 1, remote::Action::Reschedule); |
|
| 70 | + | set ids[i] = id; |
|
| 71 | + | let delivery = try! remote::take(&mut STORE, 1) |
|
| 72 | + | else panic "retainedCapacity: missing request"; |
|
| 73 | + | assert delivery.id == id; |
|
| 74 | + | assert remote::acknowledge(&mut STORE, 1, id); |
|
| 75 | + | } |
|
| 76 | + | let mut full = false; |
|
| 77 | + | try remote::send(&mut STORE, 0, 1, remote::Action::Reschedule) catch error { |
|
| 78 | + | assert error == abi::Error::Busy; |
|
| 79 | + | set full = true; |
|
| 80 | + | }; |
|
| 81 | + | assert full; |
|
| 82 | + | for id in &ids[..] { |
|
| 83 | + | try! remote::release(&mut STORE, 0, id); |
|
| 84 | + | } |
|
| 85 | + | let id = try! remote::send(&mut STORE, 0, 1, remote::Action::Reschedule); |
|
| 86 | + | try! remote::cancel(&mut STORE, 0, id); |
|
| 87 | + | } |
|
| 88 | + | ||
| 89 | + | /// A cancelled queue reference cannot deliver a reused slot generation. |
|
| 90 | + | @test unsafe fn cancelledGeneration() throws (testing::TestError) { |
|
| 91 | + | remote::initialize(&mut STORE, 3); |
|
| 92 | + | let stale = try! remote::send(&mut STORE, 0, 1, remote::Action::Reschedule); |
|
| 93 | + | try! remote::cancel(&mut STORE, 0, stale); |
|
| 94 | + | let action = remote::Action::Ready(stale); |
|
| 95 | + | let live = try! remote::send(&mut STORE, 0, 1, action); |
|
| 96 | + | assert live.index == stale.index and live <> stale; |
|
| 97 | + | let delivery = try! remote::take(&mut STORE, 1) |
|
| 98 | + | else panic "cancelledGeneration: missing reused request"; |
|
| 99 | + | assert delivery.id == live and delivery.request.action == action; |
|
| 100 | + | assert try! remote::take(&mut STORE, 1) == nil; |
|
| 101 | + | assert not remote::acknowledge(&mut STORE, 1, stale); |
|
| 102 | + | let mut cancelDenied = false; |
|
| 103 | + | try remote::cancel(&mut STORE, 0, live) catch error { |
|
| 104 | + | assert error == abi::Error::Busy; |
|
| 105 | + | set cancelDenied = true; |
|
| 106 | + | }; |
|
| 107 | + | assert cancelDenied; |
|
| 108 | + | let mut releaseDenied = false; |
|
| 109 | + | try remote::release(&mut STORE, 0, live) catch error { |
|
| 110 | + | assert error == abi::Error::Busy; |
|
| 111 | + | set releaseDenied = true; |
|
| 112 | + | }; |
|
| 113 | + | assert releaseDenied; |
|
| 114 | + | assert remote::acknowledge(&mut STORE, 1, live); |
|
| 115 | + | try! remote::release(&mut STORE, 0, live); |
|
| 116 | + | } |
|
| 117 | + | ||
| 118 | + | /// Invalid routes and premature release preserve all queued work. |
|
| 119 | + | @test unsafe fn invalidRoute() throws (testing::TestError) { |
|
| 120 | + | remote::initialize(&mut STORE, 1); |
|
| 121 | + | for target in &[1, limits::HARTS] { |
|
| 122 | + | let mut denied = false; |
|
| 123 | + | try remote::send(&mut STORE, 0, target, remote::Action::Reschedule) catch error { |
|
| 124 | + | assert error == abi::Error::InvalidArg; |
|
| 125 | + | set denied = true; |
|
| 126 | + | }; |
|
| 127 | + | assert denied; |
|
| 128 | + | } |
|
| 129 | + | let mut sourceDenied = false; |
|
| 130 | + | try remote::send(&mut STORE, 1, 0, remote::Action::Reschedule) catch error { |
|
| 131 | + | assert error == abi::Error::InvalidArg; |
|
| 132 | + | set sourceDenied = true; |
|
| 133 | + | }; |
|
| 134 | + | assert sourceDenied; |
|
| 135 | + | let id = try! remote::send(&mut STORE, 0, 0, remote::Action::Reschedule); |
|
| 136 | + | let mut releaseDenied = false; |
|
| 137 | + | try remote::release(&mut STORE, 0, id) catch error { |
|
| 138 | + | assert error == abi::Error::Busy; |
|
| 139 | + | set releaseDenied = true; |
|
| 140 | + | }; |
|
| 141 | + | assert releaseDenied; |
|
| 142 | + | assert not remote::acknowledge(&mut STORE, 0, id); |
|
| 143 | + | let delivery = try! remote::take(&mut STORE, 0) |
|
| 144 | + | else panic "invalidRoute: missing local request"; |
|
| 145 | + | assert delivery.id == id; |
|
| 146 | + | assert remote::acknowledge(&mut STORE, 0, id); |
|
| 147 | + | try! remote::release(&mut STORE, 0, id); |
|
| 148 | + | assert try! remote::take(&mut STORE, 0) == nil; |
|
| 149 | + | } |
|
| 150 | + | ||
| 151 | + | /// Replies become observable only after the target acknowledges completion. |
|
| 152 | + | @test unsafe fn replies() throws (testing::TestError) { |
|
| 153 | + | remote::initialize(&mut STORE, 3); |
|
| 154 | + | let id = try! remote::send(&mut STORE, 0, 1, remote::Action::Reschedule); |
|
| 155 | + | let delivery = try! remote::take(&mut STORE, 1) else panic "replies: missing reply request"; |
|
| 156 | + | assert delivery.id == id; |
|
| 157 | + | remote::respond(&mut STORE, 1, id, remote::Reply::Value(42)); |
|
| 158 | + | let mut pending = false; |
|
| 159 | + | try remote::result(&STORE, 0, id) catch error { |
|
| 160 | + | assert error == abi::Error::Busy; |
|
| 161 | + | set pending = true; |
|
| 162 | + | }; |
|
| 163 | + | assert pending; |
|
| 164 | + | assert remote::acknowledge(&mut STORE, 1, id); |
|
| 165 | + | assert try! remote::result(&STORE, 0, id) == 42; |
|
| 166 | + | try! remote::release(&mut STORE, 0, id); |
|
| 167 | + | let failed = try! remote::send(&mut STORE, 0, 1, remote::Action::Reschedule); |
|
| 168 | + | let taken = try! remote::take(&mut STORE, 1) else panic "replies: missing failure request"; |
|
| 169 | + | assert taken.id == failed; |
|
| 170 | + | remote::respond(&mut STORE, 1, failed, remote::Reply::Error(abi::Error::BadHandle)); |
|
| 171 | + | assert remote::acknowledge(&mut STORE, 1, failed); |
|
| 172 | + | let mut rejected = false; |
|
| 173 | + | try remote::result(&STORE, 0, failed) catch error { |
|
| 174 | + | assert error == abi::Error::BadHandle; |
|
| 175 | + | set rejected = true; |
|
| 176 | + | }; |
|
| 177 | + | assert rejected; |
|
| 178 | + | try! remote::release(&mut STORE, 0, failed); |
|
| 179 | + | } |
|
| 180 | + | ||
| 181 | + | /// Batch failure and cancellation leave every mailbox unchanged. |
|
| 182 | + | @test unsafe fn batchReservation() throws (testing::TestError) { |
|
| 183 | + | remote::initialize(&mut STORE, 7); |
|
| 184 | + | for i in 0..limits::REMOTE_PER_HART { |
|
| 185 | + | let id = try! remote::send(&mut STORE, 0, 2, remote::Action::Reschedule); |
|
| 186 | + | } |
|
| 187 | + | assert batchFull() and STORE.queues[1].count == 0; |
|
| 188 | + | let pending = try! remote::reserve(&mut STORE, 0, 2); |
|
| 189 | + | assert STORE.queues[1].count == 0; |
|
| 190 | + | remote::cancelReservation(&mut STORE, pending); |
|
| 191 | + | assert STORE.queues[1].count == 0; |
|
| 192 | + | let mut live: u32 = 0; |
|
| 193 | + | for slot in &STORE.slots[..] { |
|
| 194 | + | assert slot.state <> slots::State::Reserved; |
|
| 195 | + | if slot.state == slots::State::Live { |
|
| 196 | + | set live += 1; |
|
| 197 | + | } |
|
| 198 | + | } |
|
| 199 | + | assert live == limits::REMOTE_PER_HART; |
|
| 200 | + | for i in 0..limits::REMOTE_PER_HART { |
|
| 201 | + | let delivery = try! remote::take(&mut STORE, 2) |
|
| 202 | + | else panic "batchReservation: full mailbox lost request"; |
|
| 203 | + | assert remote::acknowledge(&mut STORE, 2, delivery.id); |
|
| 204 | + | try! remote::release(&mut STORE, 0, delivery.id); |
|
| 205 | + | } |
|
| 206 | + | let reserved = try! remote::reserve(&mut STORE, 0, 6); |
|
| 207 | + | let batch = remote::publish(&mut STORE, reserved, remote::Action::Reschedule); |
|
| 208 | + | assert batch.source == 0 and batch.targets == 6; |
|
| 209 | + | for target in 1..3 { |
|
| 210 | + | let delivery = try! remote::take(&mut STORE, target) |
|
| 211 | + | else panic "batchReservation: missing batch request"; |
|
| 212 | + | assert delivery.id == batch.ids[target]; |
|
| 213 | + | assert remote::acknowledge(&mut STORE, target, delivery.id); |
|
| 214 | + | try! remote::release(&mut STORE, 0, delivery.id); |
|
| 215 | + | } |
|
| 216 | + | } |
|
| 217 | + | ||
| 218 | + | /// Cancel an unexpected reservation so every linear result is consumed. |
|
| 219 | + | unsafe fn batchFull() -> bool { |
|
| 220 | + | let pending = try remote::reserve(&mut STORE, 0, 6) catch error { |
|
| 221 | + | assert error == abi::Error::Busy; |
|
| 222 | + | return true; |
|
| 223 | + | }; |
|
| 224 | + | remote::cancelReservation(&mut STORE, pending); |
|
| 225 | + | return false; |
|
| 226 | + | } |
kernel/kernel/tests/slots.rad
added
+72 -0
| 1 | + | //! Slot reservation, publication, cancellation, and generation retirement. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::slots; |
|
| 6 | + | ||
| 7 | + | /// Reservations occupy capacity before publication and must be consumed once. |
|
| 8 | + | @test unsafe fn lifetime() throws (testing::TestError) { |
|
| 9 | + | let mut table: [slots::Slot; 2] = undefined; |
|
| 10 | + | slots::initialize(&mut table[..]); |
|
| 11 | + | let first = try! slots::reserve(&mut table[..]); |
|
| 12 | + | let pending = slots::reference(&first); |
|
| 13 | + | let reserved = slots::matches(&table[..], pending, slots::State::Reserved); |
|
| 14 | + | let second = try! slots::reserve(&mut table[..]); |
|
| 15 | + | let left = try! slots::commit(&mut table[..], first); |
|
| 16 | + | let right = try! slots::commit(&mut table[..], second); |
|
| 17 | + | try testing::expect(reserved and left.index == 0 and right.index == 1); |
|
| 18 | + | let mut exhausted = false; |
|
| 19 | + | try reserveAndCancel(&mut table[..]) catch err { |
|
| 20 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 21 | + | set exhausted = true; |
|
| 22 | + | }; |
|
| 23 | + | try testing::expect(exhausted); |
|
| 24 | + | try! slots::release(&mut table[..], left); |
|
| 25 | + | try testing::expect(not slots::matches(&table[..], left, slots::State::Live)); |
|
| 26 | + | let replacement = try! slots::reserve(&mut table[..]); |
|
| 27 | + | let next = try! slots::commit(&mut table[..], replacement); |
|
| 28 | + | try testing::expect(next.index == left.index and next.generation == left.generation + 1); |
|
| 29 | + | let mut stale = false; |
|
| 30 | + | try slots::release(&mut table[..], left) catch err { |
|
| 31 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 32 | + | set stale = true; |
|
| 33 | + | }; |
|
| 34 | + | try testing::expect(stale and slots::matches(&table[..], next, slots::State::Live)); |
|
| 35 | + | } |
|
| 36 | + | ||
| 37 | + | /// Cancellation invalidates references and exhausted generations never wrap. |
|
| 38 | + | @test unsafe fn cancellationAndRetirement() throws (testing::TestError) { |
|
| 39 | + | let mut table: [slots::Slot; 1] = undefined; |
|
| 40 | + | slots::initialize(&mut table[..]); |
|
| 41 | + | let token = try! slots::reserve(&mut table[..]); |
|
| 42 | + | let old = slots::reference(&token); |
|
| 43 | + | try! slots::cancel(&mut table[..], token); |
|
| 44 | + | try testing::expect( |
|
| 45 | + | table[0].state == slots::State::Free |
|
| 46 | + | and table[0].generation == old.generation + 1 |
|
| 47 | + | ); |
|
| 48 | + | set table[0].generation = 0xffffffff; |
|
| 49 | + | let last = try! slots::reserve(&mut table[..]); |
|
| 50 | + | let object = try! slots::commit(&mut table[..], last); |
|
| 51 | + | try! slots::release(&mut table[..], object); |
|
| 52 | + | try testing::expect( |
|
| 53 | + | table[0].state == slots::State::Retired |
|
| 54 | + | and table[0].generation == 0xffffffff |
|
| 55 | + | ); |
|
| 56 | + | let mut failed = false; |
|
| 57 | + | try reserveAndCancel(&mut table[..]) catch err { |
|
| 58 | + | try testing::expect(err == abi::Error::Exhausted); |
|
| 59 | + | set failed = true; |
|
| 60 | + | }; |
|
| 61 | + | try testing::expect(failed); |
|
| 62 | + | set table[0].state = slots::State::Free; |
|
| 63 | + | let cancelled = try! slots::reserve(&mut table[..]); |
|
| 64 | + | try! slots::cancel(&mut table[..], cancelled); |
|
| 65 | + | try testing::expect(table[0].state == slots::State::Retired); |
|
| 66 | + | } |
|
| 67 | + | ||
| 68 | + | /// Consume a reservation when a capacity check unexpectedly succeeds. |
|
| 69 | + | fn reserveAndCancel(table: &mut [slots::Slot]) throws (abi::Error) { |
|
| 70 | + | let token = try slots::reserve(table); |
|
| 71 | + | try slots::cancel(table, token); |
|
| 72 | + | } |
kernel/kernel/tests/timers.rad
added
+156 -0
| 1 | + | //! Timeout boundaries, fixed capacity, and retained delivery. |
|
| 2 | + | use std::testing; |
|
| 3 | + | use kernel::abi; |
|
| 4 | + | use kernel::slots; |
|
| 5 | + | use kernel::domains; |
|
| 6 | + | use kernel::events; |
|
| 7 | + | use kernel::timers; |
|
| 8 | + | use kernel::limits; |
|
| 9 | + | use kernel::budgets; |
|
| 10 | + | ||
| 11 | + | /// Timer records under test. |
|
| 12 | + | unsafe static TIMERS: timers::Store = undefined; |
|
| 13 | + | /// Domain metadata and trusted notification state. |
|
| 14 | + | unsafe static DOMAINS: domains::Store = undefined; |
|
| 15 | + | /// CPU authority used to identify remote notification targets. |
|
| 16 | + | unsafe static BUDGETS: budgets::Store = undefined; |
|
| 17 | + | /// Receiver ring and private metadata. |
|
| 18 | + | unsafe static MEMORY: domains::Memory = undefined; |
|
| 19 | + | /// Ring and metadata of a second, well-behaved receiver. |
|
| 20 | + | unsafe static OTHER: domains::Memory = undefined; |
|
| 21 | + | ||
| 22 | + | /// Open an active domain queue without assigning CPU authority. |
|
| 23 | + | unsafe fn initialize() -> abi::Ref { |
|
| 24 | + | timers::initialize(&mut TIMERS); |
|
| 25 | + | budgets::initialize(&mut BUDGETS); |
|
| 26 | + | domains::initialize(&mut DOMAINS); |
|
| 27 | + | let pending = try! slots::reserve(&mut DOMAINS.slots[..]); |
|
| 28 | + | let owner = try! slots::commit(&mut DOMAINS.slots[..], pending); |
|
| 29 | + | set DOMAINS.records[owner.index].state = domains::Lifecycle::Active; |
|
| 30 | + | set DOMAINS.records[owner.index].memory = &mut MEMORY; |
|
| 31 | + | try! events::open(&mut DOMAINS.events, owner, &mut MEMORY.ring); |
|
| 32 | + | return owner; |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// A deferred timeout retries on its owning hart after its receiver waits elsewhere. |
|
| 36 | + | @test unsafe fn remoteReceiver() throws (testing::TestError) { |
|
| 37 | + | let owner = initialize(); |
|
| 38 | + | set DOMAINS.contextSlots[0] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 39 | + | let context = abi::Ref { index: 0, generation: 1 }; |
|
| 40 | + | set DOMAINS.contexts[0].owner = owner; |
|
| 41 | + | set DOMAINS.contexts[0].state = domains::ContextState::Waiting; |
|
| 42 | + | set BUDGETS.slots[0] = slots::Slot { generation: 1, state: slots::State::Live }; |
|
| 43 | + | set BUDGETS.windows[0] = budgets::Window { |
|
| 44 | + | hart: 1, start: 10, end: 100, owner, handle: abi::Handle(0), context |
|
| 45 | + | }; |
|
| 46 | + | try! timers::request(&mut TIMERS, &DOMAINS, owner, 0, 0, 15, 17); |
|
| 47 | + | for i in 0..events::ORDINARY { |
|
| 48 | + | assert try! domains::serve(&mut DOMAINS, owner, domains::RingRequest::Send(events::Event { |
|
| 49 | + | kind: events::WAKEUP, reserved: 0, code: i, value: 0 |
|
| 50 | + | })); |
|
| 51 | + | } |
|
| 52 | + | let deferred = try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 0, 15); |
|
| 53 | + | assert deferred.pending and deferred.harts == 0; |
|
| 54 | + | for i in 0..events::ORDINARY { |
|
| 55 | + | let event = try! events::pop(&mut MEMORY.ring) else panic "remoteReceiver: wake"; |
|
| 56 | + | } |
|
| 57 | + | set DOMAINS.contexts[0].state = domains::ContextState::Waiting; |
|
| 58 | + | let remote = try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 1, 15); |
|
| 59 | + | assert not remote.pending and remote.harts == 0; |
|
| 60 | + | let result = try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 0, 15); |
|
| 61 | + | assert result.harts == 2 and result.deadline == 0xffffffffffffffff and not result.pending; |
|
| 62 | + | assert DOMAINS.contexts[0].state == domains::ContextState::Ready; |
|
| 63 | + | let again = try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 0, 16); |
|
| 64 | + | assert again.harts == 0; |
|
| 65 | + | } |
|
| 66 | + | ||
| 67 | + | /// A timeout is delivered once, at its deadline, on its registered hart. |
|
| 68 | + | @test unsafe fn boundaries() throws (testing::TestError) { |
|
| 69 | + | let owner = initialize(); |
|
| 70 | + | try! timers::request(&mut TIMERS, &DOMAINS, owner, 1, 10, 5, 17); |
|
| 71 | + | assert (try! timers::service( |
|
| 72 | + | &mut TIMERS, &mut DOMAINS, &BUDGETS, 0, 15 |
|
| 73 | + | )).deadline == 0xffffffffffffffff; |
|
| 74 | + | assert (try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 1, 14)).deadline == 15; |
|
| 75 | + | assert try! events::pop(&mut MEMORY.ring) == nil; |
|
| 76 | + | let next = try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 1, 15); |
|
| 77 | + | assert next.deadline == 0xffffffffffffffff; |
|
| 78 | + | let event = try! events::pop(&mut MEMORY.ring) else panic "boundaries: timeout"; |
|
| 79 | + | assert event.kind == events::TIMEOUT and event.code == 17 and event.value == 0; |
|
| 80 | + | let again = try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 1, 16); |
|
| 81 | + | assert try! events::pop(&mut MEMORY.ring) == nil; |
|
| 82 | + | } |
|
| 83 | + | ||
| 84 | + | /// Full queues retain due timeouts without requesting another immediate interrupt. |
|
| 85 | + | @test unsafe fn saturation() throws (testing::TestError) { |
|
| 86 | + | let owner = initialize(); |
|
| 87 | + | for i in 0..events::ORDINARY { |
|
| 88 | + | assert try! domains::serve(&mut DOMAINS, owner, domains::RingRequest::Send(events::Event { |
|
| 89 | + | kind: events::WAKEUP, reserved: 0, code: i, value: 0 |
|
| 90 | + | })); |
|
| 91 | + | } |
|
| 92 | + | for i in 0..limits::TIMEOUTS_PER_HART { |
|
| 93 | + | try! timers::request(&mut TIMERS, &DOMAINS, owner, 0, 0, 1, i); |
|
| 94 | + | } |
|
| 95 | + | let mut full = false; |
|
| 96 | + | try timers::request(&mut TIMERS, &DOMAINS, owner, 0, 0, 1, 99) catch error { |
|
| 97 | + | assert error == abi::Error::Busy; |
|
| 98 | + | set full = true; |
|
| 99 | + | }; |
|
| 100 | + | assert full; |
|
| 101 | + | let deferred = try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 0, 1); |
|
| 102 | + | assert deferred.pending and deferred.deadline == 0xffffffffffffffff; |
|
| 103 | + | assert (try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 0, 2)).pending; |
|
| 104 | + | for i in 0..events::ORDINARY { |
|
| 105 | + | let event = try! events::pop(&mut MEMORY.ring) else panic "saturation: wake"; |
|
| 106 | + | } |
|
| 107 | + | let next = try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 0, 2); |
|
| 108 | + | assert not next.pending; |
|
| 109 | + | for i in 0..limits::TIMEOUTS_PER_HART { |
|
| 110 | + | let event = try! events::pop(&mut MEMORY.ring) else panic "saturation: retained timeout"; |
|
| 111 | + | assert event.kind == events::TIMEOUT and event.code == i; |
|
| 112 | + | } |
|
| 113 | + | try! timers::request(&mut TIMERS, &DOMAINS, owner, 0, 2, 1, 100); |
|
| 114 | + | } |
|
| 115 | + | ||
| 116 | + | /// Invalid delays fail, and dead receivers release timer capacity. |
|
| 117 | + | @test unsafe fn lifetime() throws (testing::TestError) { |
|
| 118 | + | let owner = initialize(); |
|
| 119 | + | let mut rejected: u32 = 0; |
|
| 120 | + | try timers::request(&mut TIMERS, &DOMAINS, owner, 0, 0, 0, 1) catch error { |
|
| 121 | + | assert error == abi::Error::InvalidArg; |
|
| 122 | + | set rejected += 1; |
|
| 123 | + | }; |
|
| 124 | + | try timers::request(&mut TIMERS, &DOMAINS, owner, 0, 0xffffffffffffffff, 1, 1) catch error { |
|
| 125 | + | assert error == abi::Error::InvalidArg; |
|
| 126 | + | set rejected += 1; |
|
| 127 | + | }; |
|
| 128 | + | try! timers::request(&mut TIMERS, &DOMAINS, owner, 0, 0, 5, 1); |
|
| 129 | + | set DOMAINS.records[owner.index].state = domains::Lifecycle::Dead; |
|
| 130 | + | let next = try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 0, 1); |
|
| 131 | + | assert next.deadline == 0xffffffffffffffff and rejected == 2; |
|
| 132 | + | assert TIMERS.entries[0][0].owner == nil; |
|
| 133 | + | } |
|
| 134 | + | ||
| 135 | + | /// A receiver that corrupts its ring counters faults without stopping delivery to other receivers. |
|
| 136 | + | @test unsafe fn corruptRing() throws (testing::TestError) { |
|
| 137 | + | let owner = initialize(); |
|
| 138 | + | let pending = try! slots::reserve(&mut DOMAINS.slots[..]); |
|
| 139 | + | let other = try! slots::commit(&mut DOMAINS.slots[..], pending); |
|
| 140 | + | set DOMAINS.records[other.index].state = domains::Lifecycle::Active; |
|
| 141 | + | set DOMAINS.records[other.index].memory = &mut OTHER; |
|
| 142 | + | try! events::open(&mut DOMAINS.events, other, &mut OTHER.ring); |
|
| 143 | + | try! timers::request(&mut TIMERS, &DOMAINS, owner, 0, 0, 5, 1); |
|
| 144 | + | try! timers::request(&mut TIMERS, &DOMAINS, other, 0, 0, 5, 2); |
|
| 145 | + | // The consumer counter moves past the producer counter. |
|
| 146 | + | set MEMORY.ring.head = 0x80000000; |
|
| 147 | + | let result = try! timers::service(&mut TIMERS, &mut DOMAINS, &BUDGETS, 0, 5); |
|
| 148 | + | assert DOMAINS.records[owner.index].state == domains::Lifecycle::Dead; |
|
| 149 | + | assert DOMAINS.events.terminals[owner.index].event.kind == events::FAULT; |
|
| 150 | + | assert DOMAINS.events.terminals[owner.index].event.code == abi::FAULT_RING; |
|
| 151 | + | assert TIMERS.entries[0][0].owner == nil and TIMERS.entries[0][1].owner == nil; |
|
| 152 | + | assert not result.pending; |
|
| 153 | + | assert DOMAINS.records[other.index].state == domains::Lifecycle::Active; |
|
| 154 | + | let event = try! events::pop(&mut OTHER.ring) else panic "corruptRing: timeout"; |
|
| 155 | + | assert event.kind == events::TIMEOUT and event.code == 2; |
|
| 156 | + | } |
kernel/kernel/tests/transactions.rad
added
+195 -0
| 1 | + | //! Atomic capability publication and delegation rules. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::limits; |
|
| 6 | + | use kernel::slots; |
|
| 7 | + | use kernel::capability; |
|
| 8 | + | use kernel::transactions; |
|
| 9 | + | ||
| 10 | + | /// Sender table used by serialized transactions. |
|
| 11 | + | unsafe static SOURCE: capability::Table = undefined; |
|
| 12 | + | /// Receiver table used by serialized transactions. |
|
| 13 | + | unsafe static TARGET: capability::Table = undefined; |
|
| 14 | + | ||
| 15 | + | /// Initialize two independent domain tables. |
|
| 16 | + | unsafe fn initialize() { |
|
| 17 | + | capability::initialize(&mut SOURCE, abi::Ref { index: 0, generation: 1 }); |
|
| 18 | + | capability::initialize(&mut TARGET, abi::Ref { index: 1, generation: 1 }); |
|
| 19 | + | } |
|
| 20 | + | ||
| 21 | + | /// Install test authority whose object lifetime belongs to the caller. |
|
| 22 | + | unsafe fn install(kind: abi::Kind, rights: u16) -> abi::Handle { |
|
| 23 | + | return try! capability::install(&mut SOURCE, capability::Entry { |
|
| 24 | + | kind, object: abi::Ref { index: 7, generation: 1 }, rights: abi::Rights(rights) |
|
| 25 | + | }); |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | /// Transfer failure preserves source authority; successful publication invalidates it. |
|
| 29 | + | @test unsafe fn atomicTransfer() throws (testing::TestError) { |
|
| 30 | + | initialize(); |
|
| 31 | + | let source = install(abi::Kind::Budget, abi::READ | abi::TRANSFER); |
|
| 32 | + | let entry = try! capability::get(&SOURCE, source); |
|
| 33 | + | for i in 0..limits::HANDLES { |
|
| 34 | + | try! capability::install(&mut TARGET, entry); |
|
| 35 | + | } |
|
| 36 | + | let mut full = false; |
|
| 37 | + | try move(source) catch err { |
|
| 38 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 39 | + | set full = true; |
|
| 40 | + | }; |
|
| 41 | + | try testing::expect(full and try! capability::get(&SOURCE, source) == entry); |
|
| 42 | + | let first = try! capability::at(&TARGET, 0, abi::Kind::Budget); |
|
| 43 | + | let removed = try! capability::invalidate(&mut TARGET, first); |
|
| 44 | + | let moved = try! move(source); |
|
| 45 | + | let actual = try! capability::get(&TARGET, moved); |
|
| 46 | + | try testing::expect(actual.object == entry.object and *actual.rights == abi::READ); |
|
| 47 | + | let mut stale = false; |
|
| 48 | + | try capability::get(&SOURCE, source) catch err { |
|
| 49 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 50 | + | set stale = true; |
|
| 51 | + | }; |
|
| 52 | + | try testing::expect(stale); |
|
| 53 | + | } |
|
| 54 | + | ||
| 55 | + | /// Complete a transfer after any required object-specific ownership update. |
|
| 56 | + | unsafe fn move(source: abi::Handle) -> abi::Handle throws (abi::Error) { |
|
| 57 | + | let entry = try capability::get(&SOURCE, source); |
|
| 58 | + | let owner = SOURCE.owner; |
|
| 59 | + | let pending = try transactions::reserve( |
|
| 60 | + | &mut TARGET, owner, source, entry, abi::READ as u64, true |
|
| 61 | + | ); |
|
| 62 | + | return transactions::transfer(&mut SOURCE, &mut TARGET, pending); |
|
| 63 | + | } |
|
| 64 | + | ||
| 65 | + | /// Self-grants narrow rights without requiring Grant; budgets remain exclusive. |
|
| 66 | + | @test unsafe fn selfGrant() throws (testing::TestError) { |
|
| 67 | + | initialize(); |
|
| 68 | + | let source = install(abi::Kind::Events, abi::READ | abi::WRITE); |
|
| 69 | + | let entry = try! capability::get(&SOURCE, source); |
|
| 70 | + | let owner = SOURCE.owner; |
|
| 71 | + | let pending = try! transactions::reserve( |
|
| 72 | + | &mut SOURCE, owner, source, entry, abi::READ as u64, false |
|
| 73 | + | ); |
|
| 74 | + | let copy = transactions::grant(&mut SOURCE, pending); |
|
| 75 | + | let narrowed = try! capability::get(&SOURCE, copy); |
|
| 76 | + | try testing::expect(*narrowed.rights == abi::READ); |
|
| 77 | + | try testing::expect(try! capability::get(&SOURCE, source) == entry); |
|
| 78 | + | let budget = install(abi::Kind::Budget, abi::GRANT | abi::TRANSFER); |
|
| 79 | + | let budgetEntry = try! capability::get(&SOURCE, budget); |
|
| 80 | + | let mut denied = false; |
|
| 81 | + | try probe(owner, budget, budgetEntry, false) catch err { |
|
| 82 | + | try testing::expect(err == abi::Error::Denied); |
|
| 83 | + | set denied = true; |
|
| 84 | + | }; |
|
| 85 | + | try testing::expect(denied); |
|
| 86 | + | } |
|
| 87 | + | ||
| 88 | + | /// Reserve and cancel a policy probe without leaving an outstanding token. |
|
| 89 | + | unsafe fn probe(owner: abi::Ref, handle: abi::Handle, entry: capability::Entry, moving: bool) |
|
| 90 | + | throws (abi::Error) |
|
| 91 | + | { |
|
| 92 | + | let pending = try transactions::reserve( |
|
| 93 | + | &mut TARGET, owner, handle, entry, abi::RIGHTS_MASK as u64, moving |
|
| 94 | + | ); |
|
| 95 | + | transactions::cancel(&mut TARGET, pending); |
|
| 96 | + | } |
|
| 97 | + | ||
| 98 | + | /// Nondelegable kinds and missing rights fail before consuming target capacity. |
|
| 99 | + | @test unsafe fn delegation() throws (testing::TestError) { |
|
| 100 | + | initialize(); |
|
| 101 | + | let mut failures: u32 = 0; |
|
| 102 | + | for kind in &[abi::Kind::Events, abi::Kind::Interrupt, abi::Kind::Budget, abi::Kind::Page] { |
|
| 103 | + | let source = install(kind, 0); |
|
| 104 | + | let entry = try! capability::get(&SOURCE, source); |
|
| 105 | + | try probe(SOURCE.owner, source, entry, false) catch err { |
|
| 106 | + | try testing::expect(err == abi::Error::Denied); |
|
| 107 | + | set failures += 1; |
|
| 108 | + | }; |
|
| 109 | + | } |
|
| 110 | + | let events = install(abi::Kind::Events, abi::TRANSFER); |
|
| 111 | + | let entry = try! capability::get(&SOURCE, events); |
|
| 112 | + | try probe(SOURCE.owner, events, entry, true) catch err { |
|
| 113 | + | try testing::expect(err == abi::Error::Denied); |
|
| 114 | + | set failures += 1; |
|
| 115 | + | }; |
|
| 116 | + | try testing::expect(failures == 5 and TARGET.slots[0].generation == 1); |
|
| 117 | + | let interrupt = install(abi::Kind::Interrupt, abi::TRANSFER); |
|
| 118 | + | let moved = try! move(interrupt); |
|
| 119 | + | let actual = try! capability::get(&TARGET, moved); |
|
| 120 | + | try testing::expect(actual.kind == abi::Kind::Interrupt); |
|
| 121 | + | } |
|
| 122 | + | ||
| 123 | + | /// Cancelled object work leaves no published capability and restores capacity. |
|
| 124 | + | @test unsafe fn cancellation() throws (testing::TestError) { |
|
| 125 | + | initialize(); |
|
| 126 | + | let source = install(abi::Kind::Page, abi::GRANT | abi::READ); |
|
| 127 | + | let entry = try! capability::get(&SOURCE, source); |
|
| 128 | + | let owner = SOURCE.owner; |
|
| 129 | + | let pending = try! transactions::reserve( |
|
| 130 | + | &mut TARGET, owner, source, entry, abi::READ as u64, false |
|
| 131 | + | ); |
|
| 132 | + | let reserved = TARGET.slots[0].state == slots::State::Reserved; |
|
| 133 | + | transactions::cancel(&mut TARGET, pending); |
|
| 134 | + | try testing::expect(reserved); |
|
| 135 | + | try testing::expect(TARGET.slots[0].state == slots::State::Free); |
|
| 136 | + | try testing::expect(try! capability::get(&SOURCE, source) == entry); |
|
| 137 | + | } |
|
| 138 | + | ||
| 139 | + | /// Invalid masks, self-transfers, and budget self-grants preserve table state. |
|
| 140 | + | @test unsafe fn policyBoundaries() throws (testing::TestError) { |
|
| 141 | + | initialize(); |
|
| 142 | + | let source = install(abi::Kind::Budget, abi::GRANT | abi::TRANSFER); |
|
| 143 | + | let entry = try! capability::get(&SOURCE, source); |
|
| 144 | + | let mut failures: u32 = 0; |
|
| 145 | + | try selfProbe(source, entry, 0, true) catch err { |
|
| 146 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 147 | + | set failures += 1; |
|
| 148 | + | }; |
|
| 149 | + | try selfProbe(source, entry, 0, false) catch err { |
|
| 150 | + | try testing::expect(err == abi::Error::Denied); |
|
| 151 | + | set failures += 1; |
|
| 152 | + | }; |
|
| 153 | + | try selfProbe(source, entry, 0xffffffffffffffff, false) catch err { |
|
| 154 | + | try testing::expect(err == abi::Error::InvalidArg); |
|
| 155 | + | set failures += 1; |
|
| 156 | + | }; |
|
| 157 | + | try testing::expect(failures == 3 and SOURCE.slots[1].generation == 1); |
|
| 158 | + | try testing::expect(try! capability::get(&SOURCE, source) == entry); |
|
| 159 | + | } |
|
| 160 | + | ||
| 161 | + | /// Consume a successful self-directed policy probe without publishing it. |
|
| 162 | + | unsafe fn selfProbe(handle: abi::Handle, entry: capability::Entry, rights: u64, moving: bool) |
|
| 163 | + | throws (abi::Error) |
|
| 164 | + | { |
|
| 165 | + | let owner = SOURCE.owner; |
|
| 166 | + | let pending = try transactions::reserve(&mut SOURCE, owner, handle, entry, rights, moving); |
|
| 167 | + | transactions::cancel(&mut SOURCE, pending); |
|
| 168 | + | } |
|
| 169 | + | ||
| 170 | + | /// Only the installed Events handle is protected from drop; attenuated copies can end. |
|
| 171 | + | @test unsafe fn installedEvents() throws (testing::TestError) { |
|
| 172 | + | initialize(); |
|
| 173 | + | let installed = install(abi::Kind::Events, abi::READ | abi::WRITE); |
|
| 174 | + | let entry = try! capability::get(&SOURCE, installed); |
|
| 175 | + | let owner = SOURCE.owner; |
|
| 176 | + | let pending = try! transactions::reserve( |
|
| 177 | + | &mut SOURCE, owner, installed, entry, abi::READ as u64, false |
|
| 178 | + | ); |
|
| 179 | + | let copy = transactions::grant(&mut SOURCE, pending); |
|
| 180 | + | let mut denied = false; |
|
| 181 | + | try transactions::dropping(&SOURCE, installed, installed) catch err { |
|
| 182 | + | try testing::expect(err == abi::Error::Denied); |
|
| 183 | + | set denied = true; |
|
| 184 | + | }; |
|
| 185 | + | try testing::expect(denied); |
|
| 186 | + | let allowed = try! transactions::dropping(&SOURCE, copy, installed); |
|
| 187 | + | let removed = try! capability::invalidate(&mut SOURCE, copy); |
|
| 188 | + | try testing::expect(removed == allowed and try! capability::get(&SOURCE, installed) == entry); |
|
| 189 | + | let mut stale = false; |
|
| 190 | + | try transactions::dropping(&SOURCE, copy, installed) catch err { |
|
| 191 | + | try testing::expect(err == abi::Error::BadHandle); |
|
| 192 | + | set stale = true; |
|
| 193 | + | }; |
|
| 194 | + | try testing::expect(stale); |
|
| 195 | + | } |
kernel/kernel/tests/trap.rad
added
+46 -0
| 1 | + | //! Trap classification, frame layout, and resume-address boundaries. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use kernel::trap; |
|
| 5 | + | ||
| 6 | + | /// The assembly frame includes x0-x31 and all four trap CSRs. |
|
| 7 | + | @test fn layout() throws (testing::TestError) { |
|
| 8 | + | try testing::expect(@sizeOf(trap::Frame) == 288); |
|
| 9 | + | assert @sizeOf(trap::Hart) == 352; |
|
| 10 | + | try testing::expect(trap::FRAME_SIZE % 16 == 0); |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Interrupts retain their source identity; exceptions retain their fault class. |
|
| 14 | + | @test fn classification() throws (testing::TestError) { |
|
| 15 | + | try testing::expect(trap::classify(0x8000000000000003) == trap::Cause::Software); |
|
| 16 | + | try testing::expect(trap::classify(0x8000000000000007) == trap::Cause::Timer); |
|
| 17 | + | try testing::expect(trap::classify(0x800000000000000b) == trap::Cause::External); |
|
| 18 | + | try testing::expect(trap::classify(8) == trap::Cause::UserCall); |
|
| 19 | + | try testing::expect(trap::classify(11) == trap::Cause::MachineCall); |
|
| 20 | + | try testing::expect(trap::classify(2) == trap::Cause::IllegalInstruction); |
|
| 21 | + | try testing::expect(trap::classify(3) == trap::Cause::Breakpoint); |
|
| 22 | + | for code in &[0 as u64, 1, 4, 5, 6, 7, 12, 13, 15] { |
|
| 23 | + | try testing::expect(trap::classify(code) == trap::Cause::MemoryFault); |
|
| 24 | + | } |
|
| 25 | + | try testing::expect(trap::classify(0x8000000000000008) == trap::Cause::Unknown); |
|
| 26 | + | try testing::expect(trap::classify(9) == trap::Cause::Unknown); |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | /// Only an aligned ecall resume address can advance by one RV64 instruction. |
|
| 30 | + | @test unsafe fn resumeAddress() throws (testing::TestError) { |
|
| 31 | + | let mut frame: trap::Frame = undefined; |
|
| 32 | + | set frame.pc = 0x80010000; |
|
| 33 | + | set frame.cause = 8; |
|
| 34 | + | set frame.status = 0; |
|
| 35 | + | try testing::expect(trap::fromUser(&frame)); |
|
| 36 | + | try testing::expect(trap::advanceCall(&mut frame) and frame.pc == 0x80010004); |
|
| 37 | + | set frame.status = 0x1800; |
|
| 38 | + | try testing::expect(not trap::fromUser(&frame)); |
|
| 39 | + | set frame.cause = 0x8000000000000007; |
|
| 40 | + | try testing::expect(not trap::advanceCall(&mut frame) and frame.pc == 0x80010004); |
|
| 41 | + | set frame.cause = 11; |
|
| 42 | + | set frame.pc = 0xfffffffffffffffc; |
|
| 43 | + | try testing::expect(not trap::advanceCall(&mut frame) and frame.pc == 0xfffffffffffffffc); |
|
| 44 | + | set frame.pc = 2; |
|
| 45 | + | try testing::expect(not trap::advanceCall(&mut frame) and frame.pc == 2); |
|
| 46 | + | } |
kernel/kernel/timers.rad
added
+120 -0
| 1 | + | //! Fixed per-hart timeout storage and lossless deferred notification. |
|
| 2 | + | use super::abi; |
|
| 3 | + | use super::limits; |
|
| 4 | + | use super::slots; |
|
| 5 | + | use super::domains; |
|
| 6 | + | use super::events; |
|
| 7 | + | use super::budgets; |
|
| 8 | + | use std::mem; |
|
| 9 | + | ||
| 10 | + | /// Deadline value when no timer is armed. |
|
| 11 | + | export constant NO_DEADLINE: u64 = mem::U64_MAX; |
|
| 12 | + | ||
| 13 | + | /// One armed timeout or retained due notification. |
|
| 14 | + | export record Entry: Copy { |
|
| 15 | + | /// Receiver generation, or nil for free capacity. |
|
| 16 | + | owner: ?abi::Ref, |
|
| 17 | + | /// Earliest permitted delivery time in mtime ticks. |
|
| 18 | + | deadline: u64, |
|
| 19 | + | /// User token echoed in the timeout event. |
|
| 20 | + | token: u32, |
|
| 21 | + | } |
|
| 22 | + | ||
| 23 | + | /// Fixed timeout slots partitioned by the hart that accepted each request. |
|
| 24 | + | export record Store: Copy { |
|
| 25 | + | /// Armed or pending entries owned by each physical hart. |
|
| 26 | + | entries: [[Entry; limits::TIMEOUTS_PER_HART]; limits::HARTS], |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | /// Clear all timeout capacity before secondary harts start. |
|
| 30 | + | export fn initialize(store: &mut Store) { |
|
| 31 | + | for hart in 0..limits::HARTS { |
|
| 32 | + | for i in 0..limits::TIMEOUTS_PER_HART { |
|
| 33 | + | set store.entries[hart][i].owner = nil; |
|
| 34 | + | } |
|
| 35 | + | } |
|
| 36 | + | } |
|
| 37 | + | ||
| 38 | + | /// Reserve one timeout without blocking or assigning CPU authority. |
|
| 39 | + | export fn request( |
|
| 40 | + | store: &mut Store, |
|
| 41 | + | receivers: &domains::Store, |
|
| 42 | + | owner: abi::Ref, |
|
| 43 | + | hart: u32, |
|
| 44 | + | now: u64, |
|
| 45 | + | delay: u64, |
|
| 46 | + | token: u32 |
|
| 47 | + | ) throws (abi::Error) { |
|
| 48 | + | if hart >= limits::HARTS or delay == 0 or delay > mem::U64_MAX - now { |
|
| 49 | + | throw abi::Error::InvalidArg; |
|
| 50 | + | } |
|
| 51 | + | let domain = try domains::get(receivers, owner); |
|
| 52 | + | if domain.state <> domains::Lifecycle::Active { |
|
| 53 | + | throw abi::Error::BadHandle; |
|
| 54 | + | } |
|
| 55 | + | for i in 0..limits::TIMEOUTS_PER_HART { |
|
| 56 | + | if store.entries[hart][i].owner <> nil { |
|
| 57 | + | continue; |
|
| 58 | + | } |
|
| 59 | + | set store.entries[hart][i] = Entry { owner, deadline: now + delay, token }; |
|
| 60 | + | return; |
|
| 61 | + | } |
|
| 62 | + | throw abi::Error::Busy; |
|
| 63 | + | } |
|
| 64 | + | ||
| 65 | + | /// Next timer boundary and dispatch notifications from delivered events. |
|
| 66 | + | export record Result: Copy { |
|
| 67 | + | /// Earliest future timeout, or the maximum tick when none is armed. |
|
| 68 | + | deadline: u64, |
|
| 69 | + | /// Harts with retained receiver authority that must refresh dispatch. |
|
| 70 | + | harts: u32, |
|
| 71 | + | /// Due notifications that require a scheduled maintenance retry. |
|
| 72 | + | pending: bool, |
|
| 73 | + | } |
|
| 74 | + | ||
| 75 | + | /// Deliver due entries and report the next boundary and affected dispatch harts. |
|
| 76 | + | /// Full queues retain due entries and request a scheduled maintenance retry. A receiver whose ring |
|
| 77 | + | /// fails validation is faulted and its entry dropped. |
|
| 78 | + | export fn service( |
|
| 79 | + | store: &mut Store, receivers: &mut domains::Store, windows: &budgets::Store, hart: u32, now: u64 |
|
| 80 | + | ) -> Result throws (abi::Error) { |
|
| 81 | + | if hart >= limits::HARTS { |
|
| 82 | + | throw abi::Error::InvalidArg; |
|
| 83 | + | } |
|
| 84 | + | let mut result = Result { deadline: NO_DEADLINE, harts: 0, pending: false }; |
|
| 85 | + | for i in 0..limits::TIMEOUTS_PER_HART { |
|
| 86 | + | let entry = store.entries[hart][i]; |
|
| 87 | + | let owner = entry.owner else { |
|
| 88 | + | continue; |
|
| 89 | + | }; |
|
| 90 | + | if not slots::matches(&receivers.slots[..], owner, slots::State::Live) |
|
| 91 | + | or receivers.records[owner.index].state == domains::Lifecycle::Dead |
|
| 92 | + | { |
|
| 93 | + | set store.entries[hart][i].owner = nil; |
|
| 94 | + | continue; |
|
| 95 | + | } |
|
| 96 | + | if entry.deadline > now { |
|
| 97 | + | if entry.deadline < result.deadline { |
|
| 98 | + | set result.deadline = entry.deadline; |
|
| 99 | + | } |
|
| 100 | + | continue; |
|
| 101 | + | } |
|
| 102 | + | let sent = try domains::serve(receivers, owner, domains::RingRequest::Send(events::Event { |
|
| 103 | + | kind: events::TIMEOUT, reserved: 0, code: entry.token, value: 0 |
|
| 104 | + | })) catch err { |
|
| 105 | + | // Only the receiver can corrupt its ring, so the fault is the receiver's own. |
|
| 106 | + | if err == abi::Error::InvalidArg { |
|
| 107 | + | set result.harts |= domains::ringFault(receivers, owner); |
|
| 108 | + | } |
|
| 109 | + | set store.entries[hart][i].owner = nil; |
|
| 110 | + | continue; |
|
| 111 | + | }; |
|
| 112 | + | if sent { |
|
| 113 | + | set store.entries[hart][i].owner = nil; |
|
| 114 | + | set result.harts |= budgets::wakeTargets(windows, receivers, owner, now); |
|
| 115 | + | } else { |
|
| 116 | + | set result.pending = true; |
|
| 117 | + | } |
|
| 118 | + | } |
|
| 119 | + | return result; |
|
| 120 | + | } |
kernel/kernel/transactions.rad
added
+144 -0
| 1 | + | //! Capability reservations held within one serialized object transaction. |
|
| 2 | + | ||
| 3 | + | use super::abi; |
|
| 4 | + | use super::slots; |
|
| 5 | + | use super::capability; |
|
| 6 | + | ||
| 7 | + | /// Metadata for unpublished authority and its source. |
|
| 8 | + | export record Pending: Copy { |
|
| 9 | + | /// Destination domain generation. |
|
| 10 | + | target: abi::Ref, |
|
| 11 | + | /// Reserved slot in the destination table. |
|
| 12 | + | slot: abi::Ref, |
|
| 13 | + | /// Source domain generation. |
|
| 14 | + | owner: abi::Ref, |
|
| 15 | + | /// Source capability to invalidate on transfer. |
|
| 16 | + | source: abi::Handle, |
|
| 17 | + | /// Validated source entry before attenuation. |
|
| 18 | + | original: capability::Entry, |
|
| 19 | + | /// Attenuated entry to publish after object work succeeds. |
|
| 20 | + | entry: capability::Entry, |
|
| 21 | + | /// True when publication also consumes the source capability. |
|
| 22 | + | moving: bool, |
|
| 23 | + | } |
|
| 24 | + | ||
| 25 | + | /// Unpublished capacity that must be committed or cancelled in the same table. |
|
| 26 | + | export union Reservation: Once { |
|
| 27 | + | /// One destination slot and the authority it will receive. |
|
| 28 | + | Held(Pending), |
|
| 29 | + | } |
|
| 30 | + | ||
| 31 | + | /// Reserve target capacity after delegation checks and rights validation. |
|
| 32 | + | /// The caller supplies a live source entry and keeps both tables serialized |
|
| 33 | + | /// through object validation, lifetime changes, and commit or cancellation. |
|
| 34 | + | export fn reserve( |
|
| 35 | + | target: &mut capability::Table, |
|
| 36 | + | owner: abi::Ref, |
|
| 37 | + | source: abi::Handle, |
|
| 38 | + | original: capability::Entry, |
|
| 39 | + | rights: u64, |
|
| 40 | + | moving: bool |
|
| 41 | + | ) -> Reservation throws (abi::Error) { |
|
| 42 | + | let requested = try abi::rights(rights); |
|
| 43 | + | let own = owner == target.owner; |
|
| 44 | + | if moving { |
|
| 45 | + | if own { |
|
| 46 | + | throw abi::Error::InvalidArg; |
|
| 47 | + | } |
|
| 48 | + | if original.kind == abi::Kind::Events or not abi::permits(original.rights, abi::Rights( |
|
| 49 | + | abi::TRANSFER |
|
| 50 | + | )) |
|
| 51 | + | { |
|
| 52 | + | throw abi::Error::Denied; |
|
| 53 | + | } |
|
| 54 | + | } else { |
|
| 55 | + | if original.kind == abi::Kind::Interrupt or original.kind == abi::Kind::Budget { |
|
| 56 | + | throw abi::Error::Denied; |
|
| 57 | + | } |
|
| 58 | + | if not own and (original.kind == abi::Kind::Events or not abi::permits( |
|
| 59 | + | original.rights, abi::Rights(abi::GRANT) |
|
| 60 | + | )) |
|
| 61 | + | { |
|
| 62 | + | throw abi::Error::Denied; |
|
| 63 | + | } |
|
| 64 | + | } |
|
| 65 | + | let reservation = try slots::reserve(&mut target.slots[..]) catch err { |
|
| 66 | + | throw abi::Error::InvalidArg; |
|
| 67 | + | }; |
|
| 68 | + | match reservation { |
|
| 69 | + | case slots::Reservation::Held(slot) => |
|
| 70 | + | return Reservation::Held(Pending { |
|
| 71 | + | target: target.owner, |
|
| 72 | + | slot, |
|
| 73 | + | owner, |
|
| 74 | + | source, |
|
| 75 | + | original, |
|
| 76 | + | entry: capability::Entry { |
|
| 77 | + | kind: original.kind, |
|
| 78 | + | object: original.object, |
|
| 79 | + | rights: abi::Rights(*original.rights & *requested), |
|
| 80 | + | }, |
|
| 81 | + | moving, |
|
| 82 | + | }), |
|
| 83 | + | } |
|
| 84 | + | } |
|
| 85 | + | ||
| 86 | + | /// Check the reserved destination before any publication or cancellation. |
|
| 87 | + | fn require(target: &capability::Table, pending: &Pending) { |
|
| 88 | + | assert target.owner == pending.target; |
|
| 89 | + | assert slots::matches(&target.slots[..], pending.slot, slots::State::Reserved); |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// Restore target capacity after object validation or lifetime acquisition fails. |
|
| 93 | + | export fn cancel(target: &mut capability::Table, reservation: Reservation) { |
|
| 94 | + | match reservation { |
|
| 95 | + | case Reservation::Held(pending) => { |
|
| 96 | + | require(target, &pending); |
|
| 97 | + | try! slots::cancel(&mut target.slots[..], slots::Reservation::Held(pending.slot)); |
|
| 98 | + | } |
|
| 99 | + | } |
|
| 100 | + | } |
|
| 101 | + | ||
| 102 | + | /// Publish a grant after acquiring its object reference and lifetime exposure. |
|
| 103 | + | export fn grant(target: &mut capability::Table, reservation: Reservation) -> abi::Handle { |
|
| 104 | + | match reservation { |
|
| 105 | + | case Reservation::Held(pending) => { |
|
| 106 | + | require(target, &pending); |
|
| 107 | + | assert not pending.moving; |
|
| 108 | + | return capability::publish( |
|
| 109 | + | target, slots::Reservation::Held(pending.slot), pending.entry |
|
| 110 | + | ); |
|
| 111 | + | } |
|
| 112 | + | } |
|
| 113 | + | } |
|
| 114 | + | ||
| 115 | + | /// Publish a transfer after object ownership and exposure updates succeed. |
|
| 116 | + | /// The caller holds serialization over both tables for the complete transaction. |
|
| 117 | + | export fn transfer( |
|
| 118 | + | source: &mut capability::Table, target: &mut capability::Table, reservation: Reservation |
|
| 119 | + | ) -> abi::Handle { |
|
| 120 | + | match reservation { |
|
| 121 | + | case Reservation::Held(pending) => { |
|
| 122 | + | require(target, &pending); |
|
| 123 | + | assert pending.moving and source.owner == pending.owner; |
|
| 124 | + | assert try! capability::get(source, pending.source) == pending.original; |
|
| 125 | + | let result = capability::publish( |
|
| 126 | + | target, slots::Reservation::Held(pending.slot), pending.entry |
|
| 127 | + | ); |
|
| 128 | + | try! capability::invalidate(source, pending.source); |
|
| 129 | + | return result; |
|
| 130 | + | } |
|
| 131 | + | } |
|
| 132 | + | } |
|
| 133 | + | ||
| 134 | + | /// Validate drop authority before object-specific lifetime release. |
|
| 135 | + | /// installedEvents is the domain's original queue handle, or zero before installation. |
|
| 136 | + | export fn dropping( |
|
| 137 | + | table: &capability::Table, handle: abi::Handle, installedEvents: abi::Handle |
|
| 138 | + | ) -> capability::Entry throws (abi::Error) { |
|
| 139 | + | let entry = try capability::get(table, handle); |
|
| 140 | + | if handle == installedEvents { |
|
| 141 | + | throw abi::Error::Denied; |
|
| 142 | + | } |
|
| 143 | + | return entry; |
|
| 144 | + | } |
kernel/kernel/trap.rad
added
+121 -0
| 1 | + | //! Integer trap contexts and exception classification. |
|
| 2 | + | ||
| 3 | + | use std::mem; |
|
| 4 | + | use std::arch::rv64; |
|
| 5 | + | /// Frame bytes reserved by trap entry. This is a multiple of stack alignment. |
|
| 6 | + | export constant FRAME_SIZE: u32 = 288; |
|
| 7 | + | ||
| 8 | + | /// Saved architectural state. Assembly offsets follow field declaration order. |
|
| 9 | + | export record Frame: Copy { |
|
| 10 | + | /// Integer registers indexed by architectural register number. x0 is zero. |
|
| 11 | + | registers: [u64; rv64::NUM_REGISTERS], |
|
| 12 | + | /// Instruction address captured from mepc. |
|
| 13 | + | pc: u64, |
|
| 14 | + | /// Trap-entry mstatus, including previous privilege and interrupt state. |
|
| 15 | + | status: u64, |
|
| 16 | + | /// Interrupt bit and exception code captured from mcause. |
|
| 17 | + | cause: u64, |
|
| 18 | + | /// Fault address or instruction bits captured from mtval. |
|
| 19 | + | value: u64, |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | /// Per-hart trap entry state named by mscratch. Entry scratch fields are private |
|
| 23 | + | /// to the assembly prologue while machine interrupts are disabled. |
|
| 24 | + | export record Hart: Copy { |
|
| 25 | + | /// Exclusive upper bound of the kernel stack. |
|
| 26 | + | stackTop: u64, |
|
| 27 | + | /// First valid kernel stack byte. |
|
| 28 | + | stackBottom: u64, |
|
| 29 | + | /// Package-state base used by kernel code. |
|
| 30 | + | kernelGp: u64, |
|
| 31 | + | /// Handler called with a complete frame and this hart's state. |
|
| 32 | + | handler: unsafe fn(&mut Frame, &mut Hart), |
|
| 33 | + | /// Temporary preservation of x5 during entry. |
|
| 34 | + | savedT0: u64, |
|
| 35 | + | /// Temporary preservation of x6 during entry. |
|
| 36 | + | savedT1: u64, |
|
| 37 | + | /// Interrupted stack pointer, before stack selection. |
|
| 38 | + | savedSp: u64, |
|
| 39 | + | /// Firmware stack top used while releasing and selecting context ownership. |
|
| 40 | + | dispatchTop: u64, |
|
| 41 | + | /// Hart-owned frame used after the interrupted context's stack is released. |
|
| 42 | + | dispatchFrame: Frame, |
|
| 43 | + | } |
|
| 44 | + | ||
| 45 | + | /// Trap classes consumed by kernel dispatch. |
|
| 46 | + | export union Cause: Copy { |
|
| 47 | + | /// Machine software interrupt. |
|
| 48 | + | Software, |
|
| 49 | + | /// Machine timer interrupt. |
|
| 50 | + | Timer, |
|
| 51 | + | /// Machine external interrupt. |
|
| 52 | + | External, |
|
| 53 | + | /// Environment call from user mode. |
|
| 54 | + | UserCall, |
|
| 55 | + | /// Environment call from machine mode. |
|
| 56 | + | MachineCall, |
|
| 57 | + | /// Unsupported or privileged instruction. |
|
| 58 | + | IllegalInstruction, |
|
| 59 | + | /// Breakpoint instruction. |
|
| 60 | + | Breakpoint, |
|
| 61 | + | /// Instruction, load, or store address/access fault. |
|
| 62 | + | MemoryFault, |
|
| 63 | + | /// Unhandled architectural cause. |
|
| 64 | + | Unknown, |
|
| 65 | + | } |
|
| 66 | + | ||
| 67 | + | /// Classify mcause without discarding its interrupt bit. |
|
| 68 | + | export fn classify(cause: u64) -> Cause { |
|
| 69 | + | match cause { |
|
| 70 | + | case rv64::MCAUSE_MACHINE_SOFTWARE => return Cause::Software, |
|
| 71 | + | case rv64::MCAUSE_MACHINE_TIMER => return Cause::Timer, |
|
| 72 | + | case rv64::MCAUSE_MACHINE_EXTERNAL => return Cause::External, |
|
| 73 | + | case rv64::MCAUSE_USER_CALL => return Cause::UserCall, |
|
| 74 | + | case rv64::MCAUSE_MACHINE_CALL => return Cause::MachineCall, |
|
| 75 | + | case rv64::MCAUSE_ILLEGAL_INSTRUCTION => |
|
| 76 | + | return Cause::IllegalInstruction, |
|
| 77 | + | case rv64::MCAUSE_BREAKPOINT => return Cause::Breakpoint, |
|
| 78 | + | case |
|
| 79 | + | rv64::MCAUSE_INSTRUCTION_MISALIGNED, |
|
| 80 | + | rv64::MCAUSE_INSTRUCTION_ACCESS, |
|
| 81 | + | rv64::MCAUSE_LOAD_MISALIGNED, |
|
| 82 | + | rv64::MCAUSE_LOAD_ACCESS, |
|
| 83 | + | rv64::MCAUSE_STORE_MISALIGNED, |
|
| 84 | + | rv64::MCAUSE_STORE_ACCESS, |
|
| 85 | + | rv64::MCAUSE_INSTRUCTION_PAGE, |
|
| 86 | + | rv64::MCAUSE_LOAD_PAGE, |
|
| 87 | + | rv64::MCAUSE_STORE_PAGE => return Cause::MemoryFault, |
|
| 88 | + | else => return Cause::Unknown, |
|
| 89 | + | } |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// Check the privilege captured in the frame's MPP field. |
|
| 93 | + | export fn fromUser(frame: &Frame) -> bool { |
|
| 94 | + | return (frame.status & rv64::MSTATUS_MPP_MASK) == 0; |
|
| 95 | + | } |
|
| 96 | + | ||
| 97 | + | /// Advance a completed ecall. Leave interrupts, faults, and invalid PCs unchanged. |
|
| 98 | + | export fn advanceCall(frame: &mut Frame) -> bool { |
|
| 99 | + | if frame.cause <> rv64::MCAUSE_USER_CALL |
|
| 100 | + | and frame.cause <> rv64::MCAUSE_MACHINE_CALL |
|
| 101 | + | { |
|
| 102 | + | return false; |
|
| 103 | + | } |
|
| 104 | + | if (frame.pc & (rv64::INSTR_SIZE as u64 - 1)) <> 0 |
|
| 105 | + | or frame.pc > mem::U64_MAX - rv64::INSTR_SIZE as u64 |
|
| 106 | + | { |
|
| 107 | + | return false; |
|
| 108 | + | } |
|
| 109 | + | set frame.pc += rv64::INSTR_SIZE as u64; |
|
| 110 | + | return true; |
|
| 111 | + | } |
|
| 112 | + | ||
| 113 | + | /// Install trap entry for the current hart with machine interrupts disabled. |
|
| 114 | + | /// The Hart must stay valid for the program and belong to the executing hart. |
|
| 115 | + | export unsafe fn install(hart: *mut Hart); |
|
| 116 | + | /// Restore a complete integer context through mret. The current mscratch must |
|
| 117 | + | /// name an installed Hart, and the frame must have machine interrupts disabled. |
|
| 118 | + | export unsafe fn resume(frame: *Frame) -> !; |
|
| 119 | + | ||
| 120 | + | /// Disable machine interrupts and stop the current hart. |
|
| 121 | + | export fn halt() -> !; |
kernel/kernel/trap.ras
added
+179 -0
| 1 | + | //! RV64 integer trap entry. mscratch names the current hart's kernel state. |
|
| 2 | + | // Machine interrupt-enable bit in mstatus. |
|
| 3 | + | .constant TRAP_MIE 8; |
|
| 4 | + | // Mask that clears the machine interrupt-enable bit. |
|
| 5 | + | .constant TRAP_MIE_CLEAR -TRAP_MIE - 1; |
|
| 6 | + | // Bit offset of the previous privilege field. |
|
| 7 | + | .constant TRAP_MPP_SHIFT 11; |
|
| 8 | + | // Mask of the previous privilege field after shifting. |
|
| 9 | + | .constant TRAP_MPP_MASK 3; |
|
| 10 | + | // Previous privilege value for machine mode. |
|
| 11 | + | .constant TRAP_MPP_MACHINE 3; |
|
| 12 | + | // Mask for the required 16-byte stack alignment. |
|
| 13 | + | .constant TRAP_STACK_MASK 15; |
|
| 14 | + | // Bytes in the saved Frame record. |
|
| 15 | + | .constant TRAP_FRAME_SIZE 288; |
|
| 16 | + | // Hart stackTop field offset. |
|
| 17 | + | .constant TRAP_HART_STACK_TOP 0; |
|
| 18 | + | // Hart stackBottom field offset. |
|
| 19 | + | .constant TRAP_HART_STACK_BOTTOM 8; |
|
| 20 | + | // Hart kernelGp field offset. |
|
| 21 | + | .constant TRAP_HART_GP 16; |
|
| 22 | + | // Hart handler field offset. |
|
| 23 | + | .constant TRAP_HART_HANDLER 24; |
|
| 24 | + | // Hart savedT0 field offset. |
|
| 25 | + | .constant TRAP_HART_T0 32; |
|
| 26 | + | // Hart savedT1 field offset. |
|
| 27 | + | .constant TRAP_HART_T1 40; |
|
| 28 | + | // Hart savedSp field offset. |
|
| 29 | + | .constant TRAP_HART_SP 48; |
|
| 30 | + | // Frame pc field offset. |
|
| 31 | + | .constant TRAP_FRAME_PC 256; |
|
| 32 | + | // Frame status field offset. |
|
| 33 | + | .constant TRAP_FRAME_STATUS 264; |
|
| 34 | + | // Frame cause field offset. |
|
| 35 | + | .constant TRAP_FRAME_CAUSE 272; |
|
| 36 | + | // Frame value field offset. |
|
| 37 | + | .constant TRAP_FRAME_VALUE 280; |
|
| 38 | + | ||
| 39 | + | .text; |
|
| 40 | + | .export @kernel::trap::install; |
|
| 41 | + | .export @kernel::trap::resume; |
|
| 42 | + | .export @kernel::trap::vector; |
|
| 43 | + | .export @kernel::trap::halt; |
|
| 44 | + | ||
| 45 | + | // Install a direct trap vector and its per-hart stack anchor. |
|
| 46 | + | @kernel::trap::install |
|
| 47 | + | csrw mie %zero; |
|
| 48 | + | csrci mstatus TRAP_MIE; |
|
| 49 | + | csrw mscratch %a0; |
|
| 50 | + | la %t0 @kernel::trap::vector; |
|
| 51 | + | csrw mtvec %t0; |
|
| 52 | + | ret; |
|
| 53 | + | ||
| 54 | + | // Hardware has cleared MIE. Preserve entry scratch registers before stack selection. |
|
| 55 | + | @kernel::trap::vector |
|
| 56 | + | csrrw %tp mscratch %tp; |
|
| 57 | + | sd %t0 TRAP_HART_T0(%tp); |
|
| 58 | + | sd %t1 TRAP_HART_T1(%tp); |
|
| 59 | + | sd %sp TRAP_HART_SP(%tp); |
|
| 60 | + | csrr %t0 mstatus; |
|
| 61 | + | srli %t0 %t0 TRAP_MPP_SHIFT; |
|
| 62 | + | andi %t0 %t0 TRAP_MPP_MASK; |
|
| 63 | + | li %t1 TRAP_MPP_MACHINE; |
|
| 64 | + | beq %t0 %t1 @machineStack; |
|
| 65 | + | ld %sp TRAP_HART_STACK_TOP(%tp); |
|
| 66 | + | @machineStack |
|
| 67 | + | andi %t0 %sp TRAP_STACK_MASK; |
|
| 68 | + | bnez %t0 @stackFault; |
|
| 69 | + | ld %t0 TRAP_HART_STACK_TOP(%tp); |
|
| 70 | + | bltu %t0 %sp @stackFault; |
|
| 71 | + | addi %t1 %sp -TRAP_FRAME_SIZE; |
|
| 72 | + | bltu %sp %t1 @stackFault; |
|
| 73 | + | ld %t0 TRAP_HART_STACK_BOTTOM(%tp); |
|
| 74 | + | bltu %t1 %t0 @stackFault; |
|
| 75 | + | mv %sp %t1; |
|
| 76 | + | sd %zero 0(%sp); |
|
| 77 | + | // SC clears the hart reservation. The saved x0 slot accepts a zero store. |
|
| 78 | + | sc.d %zero %zero (%sp); |
|
| 79 | + | sd %ra 8(%sp); |
|
| 80 | + | sd %gp 24(%sp); |
|
| 81 | + | sd %t2 56(%sp); |
|
| 82 | + | sd %s0 64(%sp); |
|
| 83 | + | sd %s1 72(%sp); |
|
| 84 | + | sd %a0 80(%sp); |
|
| 85 | + | sd %a1 88(%sp); |
|
| 86 | + | sd %a2 96(%sp); |
|
| 87 | + | sd %a3 104(%sp); |
|
| 88 | + | sd %a4 112(%sp); |
|
| 89 | + | sd %a5 120(%sp); |
|
| 90 | + | sd %a6 128(%sp); |
|
| 91 | + | sd %a7 136(%sp); |
|
| 92 | + | sd %s2 144(%sp); |
|
| 93 | + | sd %s3 152(%sp); |
|
| 94 | + | sd %s4 160(%sp); |
|
| 95 | + | sd %s5 168(%sp); |
|
| 96 | + | sd %s6 176(%sp); |
|
| 97 | + | sd %s7 184(%sp); |
|
| 98 | + | sd %s8 192(%sp); |
|
| 99 | + | sd %s9 200(%sp); |
|
| 100 | + | sd %s10 208(%sp); |
|
| 101 | + | sd %s11 216(%sp); |
|
| 102 | + | sd %t3 224(%sp); |
|
| 103 | + | sd %t4 232(%sp); |
|
| 104 | + | sd %t5 240(%sp); |
|
| 105 | + | sd %t6 248(%sp); |
|
| 106 | + | ld %t0 TRAP_HART_SP(%tp); |
|
| 107 | + | sd %t0 16(%sp); |
|
| 108 | + | csrr %t0 mscratch; |
|
| 109 | + | sd %t0 32(%sp); |
|
| 110 | + | ld %t0 TRAP_HART_T0(%tp); |
|
| 111 | + | sd %t0 40(%sp); |
|
| 112 | + | ld %t0 TRAP_HART_T1(%tp); |
|
| 113 | + | sd %t0 48(%sp); |
|
| 114 | + | csrr %t0 mepc; |
|
| 115 | + | sd %t0 TRAP_FRAME_PC(%sp); |
|
| 116 | + | csrr %t0 mstatus; |
|
| 117 | + | sd %t0 TRAP_FRAME_STATUS(%sp); |
|
| 118 | + | csrr %t0 mcause; |
|
| 119 | + | sd %t0 TRAP_FRAME_CAUSE(%sp); |
|
| 120 | + | csrr %t0 mtval; |
|
| 121 | + | sd %t0 TRAP_FRAME_VALUE(%sp); |
|
| 122 | + | csrw mscratch %tp; |
|
| 123 | + | ld %gp TRAP_HART_GP(%tp); |
|
| 124 | + | mv %a0 %sp; |
|
| 125 | + | mv %a1 %tp; |
|
| 126 | + | ld %t0 TRAP_HART_HANDLER(%tp); |
|
| 127 | + | jalr %ra %t0 0; |
|
| 128 | + | mv %a0 %sp; |
|
| 129 | + | ||
| 130 | + | // Interrupts remain disabled until mret restores the saved MPIE state. |
|
| 131 | + | @kernel::trap::resume |
|
| 132 | + | csrci mstatus TRAP_MIE; |
|
| 133 | + | mv %sp %a0; |
|
| 134 | + | // A resumed frame must not use a reservation from another execution context. |
|
| 135 | + | sc.d %zero %zero (%sp); |
|
| 136 | + | ld %t0 TRAP_FRAME_PC(%sp); |
|
| 137 | + | csrw mepc %t0; |
|
| 138 | + | ld %t0 TRAP_FRAME_STATUS(%sp); |
|
| 139 | + | andi %t0 %t0 TRAP_MIE_CLEAR; |
|
| 140 | + | csrw mstatus %t0; |
|
| 141 | + | ld %ra 8(%sp); |
|
| 142 | + | ld %gp 24(%sp); |
|
| 143 | + | ld %tp 32(%sp); |
|
| 144 | + | ld %t0 40(%sp); |
|
| 145 | + | ld %t1 48(%sp); |
|
| 146 | + | ld %t2 56(%sp); |
|
| 147 | + | ld %s0 64(%sp); |
|
| 148 | + | ld %s1 72(%sp); |
|
| 149 | + | ld %a0 80(%sp); |
|
| 150 | + | ld %a1 88(%sp); |
|
| 151 | + | ld %a2 96(%sp); |
|
| 152 | + | ld %a3 104(%sp); |
|
| 153 | + | ld %a4 112(%sp); |
|
| 154 | + | ld %a5 120(%sp); |
|
| 155 | + | ld %a6 128(%sp); |
|
| 156 | + | ld %a7 136(%sp); |
|
| 157 | + | ld %s2 144(%sp); |
|
| 158 | + | ld %s3 152(%sp); |
|
| 159 | + | ld %s4 160(%sp); |
|
| 160 | + | ld %s5 168(%sp); |
|
| 161 | + | ld %s6 176(%sp); |
|
| 162 | + | ld %s7 184(%sp); |
|
| 163 | + | ld %s8 192(%sp); |
|
| 164 | + | ld %s9 200(%sp); |
|
| 165 | + | ld %s10 208(%sp); |
|
| 166 | + | ld %s11 216(%sp); |
|
| 167 | + | ld %t3 224(%sp); |
|
| 168 | + | ld %t4 232(%sp); |
|
| 169 | + | ld %t5 240(%sp); |
|
| 170 | + | ld %t6 248(%sp); |
|
| 171 | + | ld %sp 16(%sp); |
|
| 172 | + | mret; |
|
| 173 | + | ||
| 174 | + | // A frame must never extend outside the reserved kernel stack. |
|
| 175 | + | @stackFault |
|
| 176 | + | @kernel::trap::halt |
|
| 177 | + | csrw mie %zero; |
|
| 178 | + | wfi; |
|
| 179 | + | j @stackFault; |
kernel/mmio.rad
added
+62 -0
| 1 | + | //! Fixed-width register access through live Device capabilities. |
|
| 2 | + | use super::abi; |
|
| 3 | + | ||
| 4 | + | /// Compiler primitive for one checked 8-bit register read. |
|
| 5 | + | @intrinsic fn deviceRead8(handle: u64, offset: u64) -> u8; |
|
| 6 | + | /// Compiler primitive for one checked 8-bit register write. |
|
| 7 | + | @intrinsic fn deviceWrite8(handle: u64, offset: u64, value: u8); |
|
| 8 | + | ||
| 9 | + | /// Read an aligned 8-bit register at a byte offset within the device. |
|
| 10 | + | export fn read8(handle: abi::Handle, offset: u64) -> u8 { |
|
| 11 | + | return deviceRead8(*handle, offset); |
|
| 12 | + | } |
|
| 13 | + | ||
| 14 | + | /// Write an aligned 8-bit register at a byte offset within the device. |
|
| 15 | + | export fn write8(handle: abi::Handle, offset: u64, value: u8) { |
|
| 16 | + | deviceWrite8(*handle, offset, value); |
|
| 17 | + | } |
|
| 18 | + | ||
| 19 | + | /// Compiler primitive for one checked 16-bit register read. |
|
| 20 | + | @intrinsic fn deviceRead16(handle: u64, offset: u64) -> u16; |
|
| 21 | + | /// Compiler primitive for one checked 16-bit register write. |
|
| 22 | + | @intrinsic fn deviceWrite16(handle: u64, offset: u64, value: u16); |
|
| 23 | + | ||
| 24 | + | /// Read an aligned 16-bit register at a byte offset within the device. |
|
| 25 | + | export fn read16(handle: abi::Handle, offset: u64) -> u16 { |
|
| 26 | + | return deviceRead16(*handle, offset); |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | /// Write an aligned 16-bit register at a byte offset within the device. |
|
| 30 | + | export fn write16(handle: abi::Handle, offset: u64, value: u16) { |
|
| 31 | + | deviceWrite16(*handle, offset, value); |
|
| 32 | + | } |
|
| 33 | + | ||
| 34 | + | /// Compiler primitive for one checked 32-bit register read. |
|
| 35 | + | @intrinsic fn deviceRead32(handle: u64, offset: u64) -> u32; |
|
| 36 | + | /// Compiler primitive for one checked 32-bit register write. |
|
| 37 | + | @intrinsic fn deviceWrite32(handle: u64, offset: u64, value: u32); |
|
| 38 | + | ||
| 39 | + | /// Read an aligned 32-bit register at a byte offset within the device. |
|
| 40 | + | export fn read32(handle: abi::Handle, offset: u64) -> u32 { |
|
| 41 | + | return deviceRead32(*handle, offset); |
|
| 42 | + | } |
|
| 43 | + | ||
| 44 | + | /// Write an aligned 32-bit register at a byte offset within the device. |
|
| 45 | + | export fn write32(handle: abi::Handle, offset: u64, value: u32) { |
|
| 46 | + | deviceWrite32(*handle, offset, value); |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | /// Compiler primitive for one checked 64-bit register read. |
|
| 50 | + | @intrinsic fn deviceRead64(handle: u64, offset: u64) -> u64; |
|
| 51 | + | /// Compiler primitive for one checked 64-bit register write. |
|
| 52 | + | @intrinsic fn deviceWrite64(handle: u64, offset: u64, value: u64); |
|
| 53 | + | ||
| 54 | + | /// Read an aligned 64-bit register at a byte offset within the device. |
|
| 55 | + | export fn read64(handle: abi::Handle, offset: u64) -> u64 { |
|
| 56 | + | return deviceRead64(*handle, offset); |
|
| 57 | + | } |
|
| 58 | + | ||
| 59 | + | /// Write an aligned 64-bit register at a byte offset within the device. |
|
| 60 | + | export fn write64(handle: abi::Handle, offset: u64, value: u64) { |
|
| 61 | + | deviceWrite64(*handle, offset, value); |
|
| 62 | + | } |
kernel/tools/build.rad
added
+353 -0
| 1 | + | //! Build a native kernel image with shared packages and a persistent boot catalog. |
|
| 2 | + | ||
| 3 | + | use std::sys; |
|
| 4 | + | use std::io; |
|
| 5 | + | use std::mem; |
|
| 6 | + | use std::sys::unix; |
|
| 7 | + | use std::lang::alloc; |
|
| 8 | + | use std::lang::strings; |
|
| 9 | + | use std::lang::il::binary; |
|
| 10 | + | use std::lang::il::binary::program; |
|
| 11 | + | use std::lang::il::verify; |
|
| 12 | + | use std::lang::gen::data; |
|
| 13 | + | use std::lang::gen::labels; |
|
| 14 | + | use std::collections::dict; |
|
| 15 | + | use std::arch::rv64; |
|
| 16 | + | use std::arch::rv64::asm; |
|
| 17 | + | use std::arch::rv64::emit; |
|
| 18 | + | use std::arch::rv64::encode; |
|
| 19 | + | use std::arch::rv64::image; |
|
| 20 | + | use std::arch::rv64::shared; |
|
| 21 | + | use std::arch::rv64::shared::catalog; |
|
| 22 | + | use std::lang::gen::regalloc::liveness; |
|
| 23 | + | ||
| 24 | + | /// Byte alignment of boot package code and data segments. |
|
| 25 | + | constant CODE_ALIGNMENT: u32 = 4096; |
|
| 26 | + | /// Bytes reserved for the boot entry trampoline. |
|
| 27 | + | constant TRAMPOLINE_BYTES: u32 = 4096; |
|
| 28 | + | ||
| 29 | + | /// Number of elements reserved for code storage. |
|
| 30 | + | constant CODE_CAPACITY: u32 = 16777216; |
|
| 31 | + | /// Number of elements reserved for scratch storage. |
|
| 32 | + | constant SCRATCH_CAPACITY: u32 = 16777216; |
|
| 33 | + | /// Number of elements reserved for decode storage. |
|
| 34 | + | constant DECODE_CAPACITY: u32 = 67108864; |
|
| 35 | + | /// Number of elements reserved for input storage. |
|
| 36 | + | constant INPUT_CAPACITY: u32 = 8388608; |
|
| 37 | + | /// Number of elements reserved for source storage. |
|
| 38 | + | constant SOURCE_CAPACITY: u32 = 65536; |
|
| 39 | + | /// Number of elements reserved for assembly storage. |
|
| 40 | + | constant ASSEMBLY_CAPACITY: u32 = 4194304; |
|
| 41 | + | /// Number of elements reserved for text storage. |
|
| 42 | + | constant TEXT_CAPACITY: u32 = 16384; |
|
| 43 | + | /// Number of elements reserved for data storage. |
|
| 44 | + | constant DATA_CAPACITY: u32 = 4096; |
|
| 45 | + | /// Number of elements reserved for symbols storage. |
|
| 46 | + | constant SYMBOLS_CAPACITY: u32 = 4096; |
|
| 47 | + | /// Number of elements reserved for exports storage. |
|
| 48 | + | constant EXPORTS_CAPACITY: u32 = 4096; |
|
| 49 | + | /// Number of elements reserved for imports storage. |
|
| 50 | + | constant IMPORTS_CAPACITY: u32 = 16384; |
|
| 51 | + | /// Number of elements reserved for relocs storage. |
|
| 52 | + | constant RELOCS_CAPACITY: u32 = 4096; |
|
| 53 | + | /// Number of elements reserved for templates storage. |
|
| 54 | + | constant TEMPLATES_CAPACITY: u32 = 1048576; |
|
| 55 | + | /// Number of elements reserved for ro storage. |
|
| 56 | + | constant RO_CAPACITY: u32 = 16777216; |
|
| 57 | + | /// Number of elements reserved for rw storage. |
|
| 58 | + | constant RW_CAPACITY: u32 = 33554432; |
|
| 59 | + | /// Number of elements reserved for native storage. |
|
| 60 | + | constant NATIVE_CAPACITY: u32 = 4194304; |
|
| 61 | + | ||
| 62 | + | /// Native entry page, above the platform firmware data. |
|
| 63 | + | constant CODE_ADDRESS: u64 = 0x81000000; |
|
| 64 | + | /// Size in bytes of the kernel's `Env` record, which a package entry may receive. |
|
| 65 | + | constant ENV_SIZE: u32 = 24; |
|
| 66 | + | /// Maximum shared packages in the boot image. |
|
| 67 | + | constant PACKAGES: u32 = 4; |
|
| 68 | + | /// Package code arenas retained until image output completes. |
|
| 69 | + | unsafe static CODE: [[u8; CODE_CAPACITY]; PACKAGES] = undefined; |
|
| 70 | + | /// Reusable function workspace. |
|
| 71 | + | static SCRATCH: [u8; SCRATCH_CAPACITY] = [0; SCRATCH_CAPACITY]; |
|
| 72 | + | /// Persistent decoded RIL storage. |
|
| 73 | + | static DECODE: [u8; DECODE_CAPACITY] = [0; DECODE_CAPACITY]; |
|
| 74 | + | /// Exact package bytes retained in the native catalog. |
|
| 75 | + | unsafe static INPUT: [[u8; INPUT_CAPACITY]; PACKAGES] = undefined; |
|
| 76 | + | /// Combined startup and boundary assembly source. |
|
| 77 | + | static SOURCE: [u8; SOURCE_CAPACITY] = [0; SOURCE_CAPACITY]; |
|
| 78 | + | /// Assembler workspace. |
|
| 79 | + | static ASSEMBLY: [u8; ASSEMBLY_CAPACITY] = [0; ASSEMBLY_CAPACITY]; |
|
| 80 | + | /// Assembled startup words. |
|
| 81 | + | static TEXT: [u32; TEXT_CAPACITY] = [0; TEXT_CAPACITY]; |
|
| 82 | + | /// Assembly identifiers retained through package linking. |
|
| 83 | + | unsafe static NAMES: [*[u8]; strings::TABLE_SIZE] = undefined; |
|
| 84 | + | /// Per-package data layout workspaces. |
|
| 85 | + | unsafe static DATA: [[data::DataSym; DATA_CAPACITY]; PACKAGES] = undefined; |
|
| 86 | + | /// Local function and data definitions. |
|
| 87 | + | unsafe static SYMBOLS: [[shared::Symbol; SYMBOLS_CAPACITY]; PACKAGES] = |
|
| 88 | + | undefined; |
|
| 89 | + | /// Public package symbols. |
|
| 90 | + | unsafe static EXPORTS: [[shared::Symbol; EXPORTS_CAPACITY]; PACKAGES] = |
|
| 91 | + | undefined; |
|
| 92 | + | /// Exports from dependencies compiled before the current package. |
|
| 93 | + | unsafe static IMPORTS: [shared::Symbol; IMPORTS_CAPACITY] = undefined; |
|
| 94 | + | /// Private-state relocation records. |
|
| 95 | + | unsafe static RELOCS: [[shared::Relocation; RELOCS_CAPACITY]; PACKAGES] = |
|
| 96 | + | undefined; |
|
| 97 | + | /// Initialized package templates. |
|
| 98 | + | unsafe static TEMPLATES: [[u8; TEMPLATES_CAPACITY]; PACKAGES] = undefined; |
|
| 99 | + | /// Read-only catalog and retained package payloads. |
|
| 100 | + | static RO: [u8; RO_CAPACITY] = [0; RO_CAPACITY]; |
|
| 101 | + | /// Kernel state table and initialized private package graph. |
|
| 102 | + | static RW: [u8; RW_CAPACITY] = [0; RW_CAPACITY]; |
|
| 103 | + | /// Contiguous native code segment, including entry trampoline and alignment padding. |
|
| 104 | + | static NATIVE: [u32; NATIVE_CAPACITY] = [0; NATIVE_CAPACITY]; |
|
| 105 | + | ||
| 106 | + | /// Round an extent up to a power-of-two alignment. |
|
| 107 | + | fn aligned(value: u64, alignment: u32) -> u64 { |
|
| 108 | + | assert alignment > 0 and (alignment & (alignment - 1)) == 0; |
|
| 109 | + | assert value <= mem::U64_MAX - alignment as u64 + 1; |
|
| 110 | + | return (value + alignment as u64 - 1) & ~(alignment as u64 - 1); |
|
| 111 | + | } |
|
| 112 | + | ||
| 113 | + | /// Print a bounded shared-backend failure class. |
|
| 114 | + | fn report(error: shared::Error) { |
|
| 115 | + | match error { |
|
| 116 | + | case shared::Error::Codegen(_) => io::printLn("native build: code generation failed"), |
|
| 117 | + | case shared::Error::Capacity => io::printLn("native build: output capacity exhausted"), |
|
| 118 | + | case shared::Error::Symbol => io::printLn("native build: unresolved or duplicate symbol"), |
|
| 119 | + | case shared::Error::Range => io::printLn("native build: address out of range"), |
|
| 120 | + | case shared::Error::Alignment => io::printLn("native build: invalid alignment"), |
|
| 121 | + | case shared::Error::Instance => io::printLn("native build: missing package instance"), |
|
| 122 | + | } |
|
| 123 | + | } |
|
| 124 | + | ||
| 125 | + | /// Compile one boot package into resident code and private-state metadata. |
|
| 126 | + | unsafe fn compile( |
|
| 127 | + | input: &binary::Package, |
|
| 128 | + | slot: u32, |
|
| 129 | + | address: u64, |
|
| 130 | + | imports: &[shared::Symbol], |
|
| 131 | + | assembly: &asm::Program, |
|
| 132 | + | stack: rv64::StackCheck |
|
| 133 | + | ) -> shared::Package throws (shared::Error) { |
|
| 134 | + | let arena = alloc::new(&mut CODE[slot][..]); |
|
| 135 | + | let mut scratch = alloc::new(&mut SCRATCH[..]); |
|
| 136 | + | return try shared::compileAssembly( |
|
| 137 | + | input, |
|
| 138 | + | assembly, |
|
| 139 | + | slot, |
|
| 140 | + | address, |
|
| 141 | + | imports, |
|
| 142 | + | shared::Storage { |
|
| 143 | + | data: &mut DATA[slot][..], |
|
| 144 | + | symbols: &mut SYMBOLS[slot][..], |
|
| 145 | + | exports: &mut EXPORTS[slot][..], |
|
| 146 | + | template: &mut TEMPLATES[slot][..], |
|
| 147 | + | relocations: &mut RELOCS[slot][..], |
|
| 148 | + | stack: stack, |
|
| 149 | + | }, |
|
| 150 | + | arena, |
|
| 151 | + | &mut scratch |
|
| 152 | + | ); |
|
| 153 | + | } |
|
| 154 | + | ||
| 155 | + | /// Emit a trampoline that preserves firmware arguments and supplies the native catalog. |
|
| 156 | + | unsafe fn trampoline(kernel: u64, ro: u64, rw: u64, count: u32) { |
|
| 157 | + | let mut arena = alloc::new(&mut SCRATCH[..]); |
|
| 158 | + | let mut blockOffsets: *mut [i32] = &mut []; |
|
| 159 | + | try! blockOffsets.resize(labels::MAX_BLOCKS_PER_FN, 0, &mut arena); |
|
| 160 | + | let mut funcEntries: *mut [dict::Entry] = &mut []; |
|
| 161 | + | try! funcEntries.resize( |
|
| 162 | + | labels::FUNC_TABLE_SIZE, dict::Entry { key: &[], value: 0 }, &mut arena |
|
| 163 | + | ); |
|
| 164 | + | let mut e = try! emit::emitter(arena, false, labels::fresh(blockOffsets, funcEntries)); |
|
| 165 | + | try! emit::loadImm(&mut e, rv64::GP, rw as i64); |
|
| 166 | + | try! emit::loadImm(&mut e, rv64::A3, ro as i64); |
|
| 167 | + | try! emit::loadImm(&mut e, rv64::A4, count as i64); |
|
| 168 | + | try! emit::loadImm(&mut e, rv64::T0, kernel as i64); |
|
| 169 | + | try! emit::emit(&mut e, encode::jalr(rv64::ZERO, rv64::T0, 0)); |
|
| 170 | + | assert e.code.len <= TRAMPOLINE_BYTES / rv64::INSTR_SIZE as u32; |
|
| 171 | + | for word, i in &e.code[..] { |
|
| 172 | + | set NATIVE[i] = word; |
|
| 173 | + | } |
|
| 174 | + | } |
|
| 175 | + | ||
| 176 | + | /// Link shared boot packages, retain their catalog, and instantiate the kernel graph. |
|
| 177 | + | @default unsafe fn main(env: *sys::Env) -> i32 { |
|
| 178 | + | assert env.args.len >= 5 and env.args.len <= PACKAGES + 3; |
|
| 179 | + | let count = env.args.len - 3; |
|
| 180 | + | let mut decoder = alloc::new(&mut DECODE[..]); |
|
| 181 | + | let libraryBytesLength = unix::readFile(env.args[1], &mut INPUT[0][..]) else panic "main: std RIL"; |
|
| 182 | + | let libraryBytes = &INPUT[0][..libraryBytesLength]; |
|
| 183 | + | let libraryInput = try! program::decode(libraryBytes, &mut decoder, binary::Limits { |
|
| 184 | + | registers: liveness::MAX_SSA_REGS, blocks: labels::MAX_BLOCKS_PER_FN |
|
| 185 | + | }); |
|
| 186 | + | let kernelBytesLength = unix::readFile(env.args[2], &mut INPUT[1][..]) else panic "main: kernel RIL"; |
|
| 187 | + | let kernelBytes = &INPUT[1][..kernelBytesLength]; |
|
| 188 | + | let kernelInput = try! program::decode(kernelBytes, &mut decoder, binary::Limits { |
|
| 189 | + | registers: liveness::MAX_SSA_REGS, blocks: labels::MAX_BLOCKS_PER_FN |
|
| 190 | + | }); |
|
| 191 | + | assert mem::eq(libraryInput.name, "std") and mem::eq(kernelInput.name, "kernel"); |
|
| 192 | + | assert libraryInput.dependencies.len == 0; |
|
| 193 | + | for dependency in kernelInput.dependencies { |
|
| 194 | + | assert mem::eq(dependency, "std"); |
|
| 195 | + | } |
|
| 196 | + | let sourceLength = unix::readFile(env.args[3], &mut SOURCE[..]) else panic "main: kernel assembly"; |
|
| 197 | + | let source = &SOURCE[..sourceLength]; |
|
| 198 | + | let mut assemblyArena = alloc::new(&mut ASSEMBLY[..]); |
|
| 199 | + | let empty: *mut [u8] = &mut []; |
|
| 200 | + | let mut names = strings::pool(&mut NAMES[..]); |
|
| 201 | + | let assembly = try asm::assemble( |
|
| 202 | + | asm::scanner::SourceKind::String, |
|
| 203 | + | source, |
|
| 204 | + | &mut TEXT[..], |
|
| 205 | + | empty, |
|
| 206 | + | &mut assemblyArena, |
|
| 207 | + | &mut names, |
|
| 208 | + | 0 |
|
| 209 | + | ) catch error { |
|
| 210 | + | asm::printError(error, &env.args[3][..], source); |
|
| 211 | + | return 1; |
|
| 212 | + | }; |
|
| 213 | + | let library = try compile( |
|
| 214 | + | &libraryInput, |
|
| 215 | + | 0, |
|
| 216 | + | CODE_ADDRESS |
|
| 217 | + | + TRAMPOLINE_BYTES as u64, |
|
| 218 | + | &[], |
|
| 219 | + | &asm::Program { text: &[], data: &[], symbols: &[], externalFixups: &[] }, |
|
| 220 | + | rv64::StackCheck::Off |
|
| 221 | + | ) catch error { |
|
| 222 | + | report(error); |
|
| 223 | + | return 1; |
|
| 224 | + | }; |
|
| 225 | + | let kernelAddress = aligned( |
|
| 226 | + | library.codeAddress |
|
| 227 | + | + library.code.len as u64 * rv64::INSTR_SIZE as u64, |
|
| 228 | + | CODE_ALIGNMENT |
|
| 229 | + | ); |
|
| 230 | + | let kernel = try compile( |
|
| 231 | + | &kernelInput, 1, kernelAddress, library.exports, &assembly, rv64::StackCheck::Off |
|
| 232 | + | ) catch error { |
|
| 233 | + | report(error); |
|
| 234 | + | return 1; |
|
| 235 | + | }; |
|
| 236 | + | let mut catalogEntries: [catalog::Entry; PACKAGES] = undefined; |
|
| 237 | + | set catalogEntries[0] = catalog::Entry { source: libraryBytes, package: library }; |
|
| 238 | + | set catalogEntries[1] = catalog::Entry { source: kernelBytes, package: kernel }; |
|
| 239 | + | let mut codeEnd = kernel.codeAddress |
|
| 240 | + | + kernel.code.len as u64 * rv64::INSTR_SIZE as u64; |
|
| 241 | + | for slot in 2..count { |
|
| 242 | + | let bytesLength = unix::readFile( |
|
| 243 | + | env.args[slot + 3], &mut INPUT[slot][..] |
|
| 244 | + | ) else panic "main: boot package RIL"; |
|
| 245 | + | let bytes = &INPUT[slot][..bytesLength]; |
|
| 246 | + | let mut input = try! program::decode(bytes, &mut decoder, binary::Limits { |
|
| 247 | + | registers: liveness::MAX_SSA_REGS, blocks: labels::MAX_BLOCKS_PER_FN |
|
| 248 | + | }); |
|
| 249 | + | // User packages run in U-mode, so every one must verify; there is no way around this. |
|
| 250 | + | let mut verifier = alloc::new(&mut SCRATCH[..]); |
|
| 251 | + | try verify::verifyPackage( |
|
| 252 | + | &mut input, shared::verifierConfig(ENV_SIZE), &mut verifier, &mut decoder |
|
| 253 | + | ) catch error { |
|
| 254 | + | io::print("native build: boot package fails verification: "); |
|
| 255 | + | io::print(verify::ruleName(error.rule)); |
|
| 256 | + | io::print(" "); |
|
| 257 | + | match error.place { |
|
| 258 | + | case verify::Place::Fn(i) => { |
|
| 259 | + | io::print(" fn "); |
|
| 260 | + | io::print(input.program.fns[i].name); |
|
| 261 | + | } |
|
| 262 | + | else => {} |
|
| 263 | + | } |
|
| 264 | + | io::print(" in "); |
|
| 265 | + | io::printLn(input.name); |
|
| 266 | + | return 1; |
|
| 267 | + | }; |
|
| 268 | + | let mut imported: u32 = 0; |
|
| 269 | + | for previous in 0..slot { |
|
| 270 | + | for symbol in catalogEntries[previous].package.exports { |
|
| 271 | + | assert imported < IMPORTS.len; |
|
| 272 | + | set IMPORTS[imported] = symbol; |
|
| 273 | + | set imported += 1; |
|
| 274 | + | } |
|
| 275 | + | } |
|
| 276 | + | let package = try compile( |
|
| 277 | + | &input, |
|
| 278 | + | slot, |
|
| 279 | + | aligned( |
|
| 280 | + | codeEnd, CODE_ALIGNMENT |
|
| 281 | + | ), |
|
| 282 | + | &IMPORTS[..imported], |
|
| 283 | + | &asm::Program { text: &[], data: &[], symbols: &[], externalFixups: &[] }, |
|
| 284 | + | rv64::StackCheck::TpLimit |
|
| 285 | + | ) catch error { |
|
| 286 | + | report(error); |
|
| 287 | + | return 1; |
|
| 288 | + | }; |
|
| 289 | + | set catalogEntries[slot] = catalog::Entry { source: bytes, package }; |
|
| 290 | + | set codeEnd = package.codeAddress |
|
| 291 | + | + package.code.len as u64 * rv64::INSTR_SIZE as u64; |
|
| 292 | + | } |
|
| 293 | + | let codeSize = (codeEnd - CODE_ADDRESS) as u32; |
|
| 294 | + | assert codeEnd - CODE_ADDRESS <= @sizeOf([u32; NATIVE_CAPACITY]) as u64; |
|
| 295 | + | let roAddress = aligned(codeEnd, CODE_ALIGNMENT); |
|
| 296 | + | let roSize = try catalog::pack(&catalogEntries[..count], roAddress, &mut RO[..]) catch error { |
|
| 297 | + | report(error); |
|
| 298 | + | return 1; |
|
| 299 | + | }; |
|
| 300 | + | let rwAddress = aligned(roAddress + roSize as u64, CODE_ALIGNMENT); |
|
| 301 | + | let mut bases: [u64; shared::MAX_PACKAGES] = [0; shared::MAX_PACKAGES]; |
|
| 302 | + | let mut rwSize: u32 = @sizeOf([u64; shared::MAX_PACKAGES]); |
|
| 303 | + | for entry in &catalogEntries[..2] { |
|
| 304 | + | let at = aligned(rwAddress + rwSize as u64, entry.package.alignment); |
|
| 305 | + | assert at - rwAddress <= RW.len as u64 |
|
| 306 | + | and entry.package.memory <= RW.len - (at - rwAddress) as u32; |
|
| 307 | + | set bases[entry.package.slot] = at; |
|
| 308 | + | set rwSize = (at - rwAddress) as u32 + entry.package.memory; |
|
| 309 | + | } |
|
| 310 | + | try! mem::copy(&mut RW[..@sizeOf([u64; shared::MAX_PACKAGES])], @sliceOf( |
|
| 311 | + | &bases[0] as *unsafe u8, @sizeOf([u64; shared::MAX_PACKAGES]) |
|
| 312 | + | )); |
|
| 313 | + | for entry in &catalogEntries[..2] { |
|
| 314 | + | let at = (bases[entry.package.slot] - rwAddress) as u32; |
|
| 315 | + | try shared::instantiate( |
|
| 316 | + | &entry.package, &bases[..], &mut RW[at..at + entry.package.memory] |
|
| 317 | + | ) catch error { |
|
| 318 | + | report(error); |
|
| 319 | + | return 1; |
|
| 320 | + | }; |
|
| 321 | + | } |
|
| 322 | + | for i in 0..codeSize / 4 { |
|
| 323 | + | set NATIVE[i] = encode::nop(); |
|
| 324 | + | } |
|
| 325 | + | for entry in &catalogEntries[..count] { |
|
| 326 | + | let at = ((entry.package.codeAddress - CODE_ADDRESS) / 4) as u32; |
|
| 327 | + | for word, i in entry.package.code { |
|
| 328 | + | set NATIVE[at + i] = word; |
|
| 329 | + | } |
|
| 330 | + | } |
|
| 331 | + | trampoline(kernelAddress, roAddress, rwAddress, count); |
|
| 332 | + | let header = try! image::header(image::Layout { |
|
| 333 | + | code: image::Segment { address: CODE_ADDRESS, size: codeSize }, |
|
| 334 | + | roData: image::Segment { address: roAddress, size: roSize }, |
|
| 335 | + | rwData: image::Segment { address: rwAddress, size: rwSize }, |
|
| 336 | + | rwTail: 0, |
|
| 337 | + | }); |
|
| 338 | + | let fd = unix::openOpts( |
|
| 339 | + | env.args[4], |
|
| 340 | + | unix::OpenFlags(*unix::O_WRONLY | *unix::O_CREAT | *unix::O_TRUNC), |
|
| 341 | + | unix::FILE_MODE |
|
| 342 | + | ); |
|
| 343 | + | assert fd >= 0; |
|
| 344 | + | let written = unix::writeAll(fd, &header[..]) |
|
| 345 | + | and unix::writeAll(fd, @sliceOf( |
|
| 346 | + | &NATIVE[0] as *u8, codeSize |
|
| 347 | + | )) |
|
| 348 | + | and unix::writeAll(fd, &RO[..roSize]) |
|
| 349 | + | and unix::writeAll(fd, &RW[..rwSize]); |
|
| 350 | + | let closed = unix::close(fd) == 0; |
|
| 351 | + | assert written and closed; |
|
| 352 | + | return 0; |
|
| 353 | + | } |
lib/std/lang/il.rad
+2 -2
| 743 | 743 | ||
| 744 | 744 | /// Compiler-generated metadata call for one MMIO access. |
|
| 745 | 745 | /// a0 is the handle, a1 the offset, and a2 the byte width plus bit 8 for a write. |
|
| 746 | 746 | /// a3 holds a write value and is preserved. Success returns the address in a0. |
|
| 747 | 747 | /// Any failure terminates the caller before the user-mode register access. |
|
| 748 | - | export constant DEVICE_ACCESS: u32 = 74; |
|
| 748 | + | export constant DEVICE_ACCESS: u32 = 90; |
|
| 749 | 749 | ||
| 750 | 750 | /// Kernel operation behind `page.slice`. a0 is a Page or Events handle and a1 is nonzero for |
|
| 751 | 751 | /// writing. Success returns the base address in a0 and the size in bytes in a1. Failure |
|
| 752 | 752 | /// returns a negative error in a0. |
|
| 753 | - | export constant PAGE_ACCESS: u32 = 75; |
|
| 753 | + | export constant PAGE_ACCESS: u32 = 32; |
|
| 754 | 754 | ||
| 755 | 755 | /// Get the destination register of an instruction, if any. |
|
| 756 | 756 | export fn instrDst(instr: Instr) -> ?Reg { |
|
| 757 | 757 | match instr { |
|
| 758 | 758 | case Instr::Reserve { dst, .. } => return dst, |
test/Makefile
+88 -4
| 1 | - | # Compiler and standard library test suites. Run from the repository root. |
|
| 1 | + | # Test suites and machine fixture matrix. Run from the repository root. |
|
| 2 | 2 | # JOBS controls workers inside test scripts. |
|
| 3 | 3 | JOBS ?= 1 |
|
| 4 | 4 | export JOBS |
|
| 5 | - | TESTS := seed-test std-test fmt-test bin-test module-test \ |
|
| 6 | - | native-test shared-test verify-test |
|
| 5 | + | TEST_HARTS := 1 2 8 |
|
| 6 | + | SMP_TESTS := $(addprefix smp-test-,$(TEST_HARTS)) |
|
| 7 | + | CYCLES_TESTS := $(addprefix cycles-test-,$(TEST_HARTS)) |
|
| 8 | + | TESTS := seed-test std-test fmt-test bin-test module-test \ |
|
| 9 | + | native-test shared-test sync-test verify-test kernel-test \ |
|
| 10 | + | kernel-boot-test trap-test loader-test dispatch-test $(SMP_TESTS) \ |
|
| 11 | + | termination-test runtime-test bootstrap-test scheduling-test mmio-test \ |
|
| 12 | + | $(CYCLES_TESTS) |
|
| 7 | 13 | ||
| 8 | 14 | test: $(TESTS) |
|
| 9 | 15 | .PHONY: test $(TESTS) smp-test cycles-test clean-std-test clean-bin-test |
|
| 10 | 16 | ||
| 11 | 17 | # Hosted test tools share one compiler recipe. |
|
| 12 | 18 | # Runner binary: the lowering IL checker. |
|
| 13 | 19 | TEST_TOOL_SRC := test/runner.rad test/native/build.rad \ |
|
| 14 | - | test/shared/build.rad \ |
|
| 20 | + | test/shared/build.rad test/sync/build.rad test/loader/hostile.rad \ |
|
| 15 | 21 | test/verify/check.rad |
|
| 16 | 22 | TEST_TOOLS := $(TEST_TOOL_SRC:.rad=.rv64) |
|
| 17 | 23 | ||
| 18 | 24 | $(TEST_TOOLS): %.rv64: %.rad $(STD_LIB) $(RAD_BIN) test/Makefile |
|
| 19 | 25 | @$(RADIANCE) $(STD) -pkg tool -mod $< -entry tool -o $@ |
| 41 | 47 | # The source formatter keeps the AST of every standard library and compiler |
|
| 42 | 48 | # source, and formats its own output to itself. |
|
| 43 | 49 | fmt-test: $(RAD_BIN) test/fmt/run |
|
| 44 | 50 | @sh test/fmt/run |
|
| 45 | 51 | ||
| 52 | + | kernel-test: $(KERNEL_TEST) |
|
| 53 | + | @$(HOSTED) $(KERNEL_TEST) |
|
| 54 | + | @sh test/slots/run |
|
| 55 | + | ||
| 56 | + | kernel-boot-test: $(BIN_DIR)/kernel.rv64 |
|
| 57 | + | @sh test/boot/run |
|
| 58 | + | ||
| 46 | 59 | # Native image execution through the configured machine-capable emulator. |
|
| 47 | 60 | native-test: $(BIN_DIR)/native.fixture.rv64 |
|
| 48 | 61 | @$${RAD_EMULATOR:-emulator} -machine -max-steps=1000000 -run $< |
|
| 49 | 62 | ||
| 50 | 63 | $(BIN_DIR)/native.fixture.rv64: test/native/build.rv64 test/Makefile |
| 53 | 66 | # Shared package execution |
|
| 54 | 67 | shared-test: test/shared/build.rv64 |
|
| 55 | 68 | @sh test/shared/run |
|
| 56 | 69 | @sh test/shared/errors/run |
|
| 57 | 70 | ||
| 71 | + | # Kernel synchronization machine tests |
|
| 72 | + | sync-test: test/sync/build.rv64 |
|
| 73 | + | @sh test/sync/run |
|
| 74 | + | ||
| 58 | 75 | # Binary Tests |
|
| 59 | 76 | ||
| 60 | 77 | # Only tests with `//! returns:` are compiled to binaries and executed. |
|
| 61 | 78 | BIN_TEST_SRC := $(shell grep -l '^//! returns:' \ |
|
| 62 | 79 | $(wildcard test/tests/*.rad test/tests/*.ras)) |
| 102 | 119 | @rm -f $(TEST_TOOLS) $(BIN_TEST_BIN) test/compiler/discovery.rv64 \ |
|
| 103 | 120 | $(wildcard test/tests/*.rv64.debug test/tests/*.rv64.s) \ |
|
| 104 | 121 | $(TEST_TOOLS:.rv64=.rv64.debug) $(TEST_TOOLS:.rv64=.rv64.s) \ |
|
| 105 | 122 | $(TEST_TOOLS:.rv64=.rv64.o) |
|
| 106 | 123 | ||
| 124 | + | # Integer trap contexts and nested machine interrupts. |
|
| 125 | + | trap-test: test/sync/build.rv64 |
|
| 126 | + | @sh test/trap/run |
|
| 127 | + | ||
| 128 | + | # Runtime package compilation and execution through the production kernel. |
|
| 129 | + | # Generator of the hostile packages that the loader must reject. |
|
| 130 | + | loader-test: $(BIN_DIR)/kernel.build.rv64 test/loader/hostile.rv64 |
|
| 131 | + | @sh test/loader/run |
|
| 132 | + | ||
| 133 | + | # Machine fixtures build one kernel image each; every hart count then runs |
|
| 134 | + | # the same image as its own target. |
|
| 135 | + | FIXTURE_DEPS := $(RAD_BIN) $(BIN_DIR)/kernel.build.rv64 \ |
|
| 136 | + | test/kernel/build test/kernel/embed test/kernel/fixture.rad \ |
|
| 137 | + | test/kernel.rad test/Makefile kernel/mmio.rad $(KERNEL_MODULES) $(KERNEL_ASM) |
|
| 138 | + | DISPATCH_IMAGES := $(BIN_DIR)/dispatch.rv64 $(BIN_DIR)/smp.rv64 \ |
|
| 139 | + | $(BIN_DIR)/termination.rv64 $(BIN_DIR)/mmio.rv64 |
|
| 140 | + | ||
| 141 | + | # Dispatch fixtures are built by test/dispatch/build from test/NAME. |
|
| 142 | + | $(DISPATCH_IMAGES): $(BIN_DIR)/%.rv64: test/%/spin.rad test/%/machine.ras \ |
|
| 143 | + | test/%/kernel/dispatchcheck.rad test/dispatch/build $(FIXTURE_DEPS) |
|
| 144 | + | @sh test/dispatch/build $* $@ |
|
| 145 | + | ||
| 146 | + | $(BIN_DIR)/termination.rv64: test/termination/cases.rad |
|
| 147 | + | $(BIN_DIR)/mmio.rv64: test/mmio/cases.rad |
|
| 148 | + | $(BIN_DIR)/smp.rv64: test/smp/kernel/invariants.rad test/smp/cases.rad |
|
| 149 | + | ||
| 150 | + | # Native preemption across user code, idle, and retained machine work. |
|
| 151 | + | dispatch-test: $(BIN_DIR)/dispatch.rv64 |
|
| 152 | + | @sh test/dispatch/run dispatch $< 1 |
|
| 153 | + | ||
| 154 | + | # Concurrent user contexts and metadata transactions on every online hart. |
|
| 155 | + | smp-test: $(SMP_TESTS) |
|
| 156 | + | $(SMP_TESTS): smp-test-%: $(BIN_DIR)/smp.rv64 |
|
| 157 | + | @sh test/dispatch/run smp $< $* 7 |
|
| 158 | + | ||
| 159 | + | # User exit, abort, and CPU faults preserve the surviving parent's execution. |
|
| 160 | + | termination-test: $(BIN_DIR)/termination.rv64 |
|
| 161 | + | @sh test/dispatch/run termination $< 1 |
|
| 162 | + | ||
| 163 | + | # Scheduled compilation, cancellation, retry, and shared executable publication. |
|
| 164 | + | runtime-test: $(BIN_DIR)/kernel.build.rv64 |
|
| 165 | + | @sh test/runtime/run |
|
| 166 | + | ||
| 167 | + | # Root lifetime and resource authority across the boot-package handoff. |
|
| 168 | + | bootstrap-test: $(BIN_DIR)/kernel.build.rv64 |
|
| 169 | + | @sh test/bootstrap/run |
|
| 170 | + | ||
| 171 | + | # Fixture root and scheduler packages execute authority changes in user mode. |
|
| 172 | + | scheduling-test: $(BIN_DIR)/kernel.rv64 |
|
| 173 | + | @sh test/scheduling/run |
|
| 174 | + | ||
| 175 | + | # Checked user-mode register access, including rejection before device effects. |
|
| 176 | + | mmio-test: $(BIN_DIR)/mmio.rv64 |
|
| 177 | + | @sh test/dispatch/run mmio $< 1 |
|
| 178 | + | ||
| 179 | + | # Dependency, controller, and worker loading under every hart count. |
|
| 180 | + | $(BIN_DIR)/cycles.rv64: test/cycles/build test/cycles/spin.rad \ |
|
| 181 | + | test/cycles/job.rad test/cycles/support.rad test/cycles/machine.ras \ |
|
| 182 | + | test/cycles/kernel/dispatchcheck.rad test/smp/kernel/invariants.rad \ |
|
| 183 | + | $(FIXTURE_DEPS) |
|
| 184 | + | @sh test/cycles/build $@ |
|
| 185 | + | ||
| 186 | + | cycles-test: $(CYCLES_TESTS) |
|
| 187 | + | $(CYCLES_TESTS): cycles-test-%: $(BIN_DIR)/cycles.rv64 |
|
| 188 | + | @$${RAD_EMULATOR:-emulator} -machine -harts=$* -memory-size=262144 \ |
|
| 189 | + | -max-steps=10000000000 -run $< |
|
| 190 | + | ||
| 107 | 191 | # The verifier accepts or rejects the binary RIL of each fixture as |
|
| 108 | 192 | # test/verify/expected says. |
|
| 109 | 193 | verify-test: test/verify/check.rv64 |
|
| 110 | 194 | @sh test/verify/run |
|
| 111 | 195 |
test/boot/machine.ras
added
+10 -0
| 1 | + | //! Complete only after every hart passes the production boot initialization. |
|
| 2 | + | .text; |
|
| 3 | + | call @kernel::boot::initialize; |
|
| 4 | + | beqz %a0 @wait; |
|
| 5 | + | li %t0 0x10001000; |
|
| 6 | + | li %t1 0x5555; |
|
| 7 | + | sw %t1 0(%t0); |
|
| 8 | + | @wait |
|
| 9 | + | wfi; |
|
| 10 | + | j @wait; |
test/boot/run
added
+48 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Boot the kernel through its production initialization on each supported hart count. |
|
| 3 | + | set -eu |
|
| 4 | + | emulator=${RAD_EMULATOR:-emulator} |
|
| 5 | + | work=$(mktemp -d) |
|
| 6 | + | trap 'rm -rf "$work"' EXIT HUP INT TERM |
|
| 7 | + | cat test/boot/machine.ras kernel/kernel/*.ras > "$work/boot.ras" |
|
| 8 | + | scripts/hosted bin/kernel.build.rv64 -- bin/std.ril bin/kernel.ril "$work/boot.ras" "$work/boot.rv64" |
|
| 9 | + | for harts in 1 2 8; do |
|
| 10 | + | # The deterministic emulator shares its tick budget across all harts. |
|
| 11 | + | steps=$((10000000 * harts)) |
|
| 12 | + | if ! "$emulator" -machine -max-steps="$steps" -harts="$harts" \ |
|
| 13 | + | -run "$work/boot.rv64" > "$work/log" 2>&1 \ |
|
| 14 | + | || ! grep -q '^kernel: platform ready$' "$work/log" \ |
|
| 15 | + | || ! grep -q '^kernel: boot catalog ready$' "$work/log"; then |
|
| 16 | + | cat "$work/log" >&2 |
|
| 17 | + | exit 1 |
|
| 18 | + | fi |
|
| 19 | + | printf 'kernel boot: %s harts passed\n' "$harts" |
|
| 20 | + | done |
|
| 21 | + | status=0 |
|
| 22 | + | "$emulator" -machine -harts=8 -max-steps=80000000 -run bin/kernel.rv64 > "$work/log" 2>&1 || status=$? |
|
| 23 | + | if [ "$status" -ne 2 ] || [ "$(grep -c 'wfi=1 mcause=0x0' "$work/log")" -ne 8 ]; then |
|
| 24 | + | cat "$work/log" >&2 |
|
| 25 | + | exit 1 |
|
| 26 | + | fi |
|
| 27 | + | printf 'kernel startup: all eight harts reached machine idle without traps\n' |
|
| 28 | + | cat test/boot/trap.ras kernel/kernel/*.ras > "$work/trap.ras" |
|
| 29 | + | scripts/hosted bin/kernel.build.rv64 -- bin/std.ril bin/kernel.ril "$work/trap.ras" "$work/trap.rv64" |
|
| 30 | + | status=0 |
|
| 31 | + | "$emulator" -machine -harts=2 -max-steps=20000000 -run "$work/trap.rv64" > "$work/log" 2>&1 || status=$? |
|
| 32 | + | if [ "$status" -ne 2 ] || [ "$(grep -c '^kernel: unexpected trap$' "$work/log")" -ne 1 ] \ |
|
| 33 | + | || [ "$(grep -c 'wfi=1' "$work/log")" -ne 2 ]; then |
|
| 34 | + | cat "$work/log" >&2 |
|
| 35 | + | exit 1 |
|
| 36 | + | fi |
|
| 37 | + | printf 'kernel trap: shared kernel state restored from the hart anchor\n' |
|
| 38 | + | ||
| 39 | + | # Assembly failures report the input location and return a compiler error status. |
|
| 40 | + | printf '.text;\ninvalid;\n' > "$work/invalid.ras" |
|
| 41 | + | status=0 |
|
| 42 | + | scripts/hosted bin/kernel.build.rv64 -- bin/std.ril bin/kernel.ril \ |
|
| 43 | + | "$work/invalid.ras" "$work/invalid.rv64" > "$work/invalid.log" 2>&1 || status=$? |
|
| 44 | + | if [ "$status" -ne 1 ] || ! grep -Fq \ |
|
| 45 | + | "$work/invalid.ras:2:1: error: unknown instruction" "$work/invalid.log"; then |
|
| 46 | + | cat "$work/invalid.log" >&2 |
|
| 47 | + | exit 1 |
|
| 48 | + | fi |
test/boot/trap.ras
added
+12 -0
| 1 | + | //! Enter a kernel handler after an interrupted context changes gp. |
|
| 2 | + | .text; |
|
| 3 | + | call @kernel::boot::initialize; |
|
| 4 | + | beqz %a0 @wait; |
|
| 5 | + | li %gp 123; |
|
| 6 | + | ecall; |
|
| 7 | + | li %t0 0x10001000; |
|
| 8 | + | li %t1 0x13333; |
|
| 9 | + | sw %t1 0(%t0); |
|
| 10 | + | @wait |
|
| 11 | + | wfi; |
|
| 12 | + | j @wait; |
test/bootstrap/kernel/dispatchcheck.rad
added
+114 -0
| 1 | + | //! Native root construction, exit, and continued resource authority. |
|
| 2 | + | use kernel::fixture; |
|
| 3 | + | use kernel::state; |
|
| 4 | + | use kernel::abi; |
|
| 5 | + | use kernel::boot; |
|
| 6 | + | use kernel::registry; |
|
| 7 | + | use kernel::domains; |
|
| 8 | + | use kernel::capability; |
|
| 9 | + | use kernel::pages; |
|
| 10 | + | use kernel::budgets; |
|
| 11 | + | use kernel::dispatch; |
|
| 12 | + | use kernel::calls; |
|
| 13 | + | use kernel::events; |
|
| 14 | + | use kernel::sync; |
|
| 15 | + | use kernel::startup; |
|
| 16 | + | ||
| 17 | + | /// Duration of the root execution window before the surviving checker. |
|
| 18 | + | constant ROOT_TICKS: u64 = 10000000; |
|
| 19 | + | ||
| 20 | + | // State retained across setup and verification is initialized before |
|
| 21 | + | // dispatch starts. The scheduled checker reads it after the user workload. |
|
| 22 | + | /// Initial root identity retained by the terminal event. |
|
| 23 | + | unsafe static ROOT: abi::Ref = undefined; |
|
| 24 | + | /// Surviving controller domain with inherited resource authority. |
|
| 25 | + | unsafe static CONTROLLER: abi::Ref = undefined; |
|
| 26 | + | /// Image capability installed in the controller's own table. |
|
| 27 | + | unsafe static IMAGE: abi::Handle = undefined; |
|
| 28 | + | /// Current kernel package-state table. |
|
| 29 | + | fn kernelGp() -> u64; |
|
| 30 | + | /// Controller entry address. |
|
| 31 | + | fn completion() -> u64; |
|
| 32 | + | /// Signal successful completion to the emulator. |
|
| 33 | + | fn finish(); |
|
| 34 | + | /// Map an assembly-compatible physical byte address. |
|
| 35 | + | fn memory(address: u64) -> *mut u8; |
|
| 36 | + | ||
| 37 | + | /// Construct root from the boot catalog and give its child a later CPU window. |
|
| 38 | + | export unsafe fn setup() { |
|
| 39 | + | let image = registry::find(®istry::capture(&state::SHARED.registry), |
|
| 40 | + | &"loaded"[..]) else panic "setup: missing boot user package"; |
|
| 41 | + | let service = registry::find(®istry::capture(&state::SHARED.registry), |
|
| 42 | + | &"service"[..]) else panic "setup: missing boot service package"; |
|
| 43 | + | set ROOT = try! startup::create(&boot::PLATFORM, &mut state::SHARED, |
|
| 44 | + | image, service); |
|
| 45 | + | assert ROOT.index == 0; |
|
| 46 | + | let mut root = try! domains::get(&state::SHARED.domains, ROOT); |
|
| 47 | + | let self = try! abi::handle(abi::Kind::Domain, abi::Ref { index: 0, |
|
| 48 | + | generation: root.memory.table.slots[0].generation }); |
|
| 49 | + | assert (try! abi::decode(root.events)).object.index == 1; |
|
| 50 | + | let executable = try! registry::install(&state::SHARED.registry, |
|
| 51 | + | &mut root.memory.table, image); |
|
| 52 | + | let childHandle = try! domains::create(&mut state::SHARED.domains, |
|
| 53 | + | &mut state::SHARED.pages.backings, &state::SHARED.registry, |
|
| 54 | + | &mut root.memory.table, self, executable); |
|
| 55 | + | set CONTROLLER = (try! capability::get(&root.memory.table, |
|
| 56 | + | childHandle)).object; |
|
| 57 | + | let mut child = try! domains::get(&state::SHARED.domains, CONTROLLER); |
|
| 58 | + | let inherited = domains::MANAGEMENT_RIGHTS | abi::CREATE | abi::ALLOCATE; |
|
| 59 | + | let authority = try! calls::invoke(ROOT, |
|
| 60 | + | abi::Operation::CapabilityGrant as u32, &[*childHandle, *childHandle, |
|
| 61 | + | inherited as u64, 0], 0); |
|
| 62 | + | assert service.index == 3 and image.index == 2; |
|
| 63 | + | set IMAGE = try! registry::install(&state::SHARED.registry, |
|
| 64 | + | &mut child.memory.table, service); |
|
| 65 | + | try! domains::reparent(&mut state::SHARED.domains, &root.memory.table, |
|
| 66 | + | self, childHandle); |
|
| 67 | + | fixture::machine(CONTROLLER, completion(), kernelGp()); |
|
| 68 | + | let now = dispatch::clock(); |
|
| 69 | + | let index = 2 + state::SHARED.devices.count + |
|
| 70 | + | state::SHARED.interrupts.count; |
|
| 71 | + | let budget = try! abi::handle(abi::Kind::Budget, abi::Ref { index, |
|
| 72 | + | generation: root.memory.table.slots[index].generation }); |
|
| 73 | + | let window = try! budgets::query(&state::SHARED.budgets, |
|
| 74 | + | &root.memory.table, budget); |
|
| 75 | + | let start = window.start; |
|
| 76 | + | let later = try! budgets::split(&mut state::SHARED.budgets, |
|
| 77 | + | &mut root.memory.table, budget, start + ROOT_TICKS, now); |
|
| 78 | + | let rootContext = root.initial; |
|
| 79 | + | let childContext = child.initial; |
|
| 80 | + | try! calls::rebind(&mut state::SHARED, |
|
| 81 | + | &mut root.memory.table, budgets::Binding { budget, domain: self, |
|
| 82 | + | context: rootContext }, now); |
|
| 83 | + | try! calls::rebind(&mut state::SHARED, |
|
| 84 | + | &mut root.memory.table, budgets::Binding { budget: later, |
|
| 85 | + | domain: childHandle, context: childContext }, now); |
|
| 86 | + | } |
|
| 87 | + | ||
| 88 | + | /// Root exit leaves the survivor able to allocate and create new domains. |
|
| 89 | + | export unsafe fn verify() { |
|
| 90 | + | let guard = sync::enter(); |
|
| 91 | + | let mut child = try! domains::get(&state::SHARED.domains, CONTROLLER); |
|
| 92 | + | let terminal = try! events::pop(&mut child.memory.ring) |
|
| 93 | + | else panic "verify: missing root exit"; |
|
| 94 | + | assert terminal.kind == events::CHILD_EXIT and terminal.code == 0 |
|
| 95 | + | and terminal.value == abi::id(ROOT); |
|
| 96 | + | assert child.state == domains::Lifecycle::Active and child.parent == nil; |
|
| 97 | + | try! events::refresh(&mut state::SHARED.domains.events, CONTROLLER, |
|
| 98 | + | &child.memory.ring); |
|
| 99 | + | sync::leave(guard); |
|
| 100 | + | let page = abi::Handle(try! calls::synchronized(CONTROLLER, |
|
| 101 | + | abi::Operation::PageAllocate as u32, &[0, 1, 0, 0], dispatch::clock())); |
|
| 102 | + | let domain = abi::Handle(try! calls::synchronized(CONTROLLER, |
|
| 103 | + | abi::Operation::DomainCreate as u32, &[0, *IMAGE, 0, 0], |
|
| 104 | + | dispatch::clock())); |
|
| 105 | + | let checking = sync::enter(); |
|
| 106 | + | let allocated = try! capability::get(&child.memory.table, page); |
|
| 107 | + | assert (try! pages::get(&state::SHARED.pages, allocated.object)).count == 1; |
|
| 108 | + | let object = (try! capability::get(&child.memory.table, domain)).object; |
|
| 109 | + | let created = try! domains::get(&state::SHARED.domains, object); |
|
| 110 | + | assert created.creator == CONTROLLER and created.parent == CONTROLLER; |
|
| 111 | + | assert created.state == domains::Lifecycle::Pending; |
|
| 112 | + | sync::leave(checking); |
|
| 113 | + | finish(); |
|
| 114 | + | } |
test/bootstrap/machine.ras
added
+25 -0
| 1 | + | //! Boot one hart and check user termination through production dispatch. |
|
| 2 | + | .text; |
|
| 3 | + | call @kernel::boot::initialize; |
|
| 4 | + | call @kernel::dispatchcheck::setup; |
|
| 5 | + | call @kernel::boot::run; |
|
| 6 | + | .export @kernel::dispatchcheck::memory; |
|
| 7 | + | .export @kernel::dispatchcheck::kernelGp; |
|
| 8 | + | .export @kernel::dispatchcheck::completion; |
|
| 9 | + | .export @kernel::dispatchcheck::finish; |
|
| 10 | + | @kernel::dispatchcheck::memory |
|
| 11 | + | ret; |
|
| 12 | + | @kernel::dispatchcheck::kernelGp |
|
| 13 | + | mv %a0 %gp; |
|
| 14 | + | ret; |
|
| 15 | + | @kernel::dispatchcheck::completion |
|
| 16 | + | la %a0 @complete; |
|
| 17 | + | ret; |
|
| 18 | + | @complete |
|
| 19 | + | call @kernel::dispatchcheck::verify; |
|
| 20 | + | ebreak; |
|
| 21 | + | @kernel::dispatchcheck::finish |
|
| 22 | + | li %t0 0x10001000; |
|
| 23 | + | li %t1 0x5555; |
|
| 24 | + | sw %t1 0(%t0); |
|
| 25 | + | ebreak; |
test/bootstrap/root.rad
added
+38 -0
| 1 | + | //! Validate the root startup bundle from user mode. |
|
| 2 | + | export mod abi; |
|
| 3 | + | export mod sys; |
|
| 4 | + | ||
| 5 | + | /// Check root identity, resource ordering, and image authority before returning. |
|
| 6 | + | @default fn main(env: *abi::Env) { |
|
| 7 | + | assert env.argsSize == @sizeOf(abi::BootInfo) as u64; |
|
| 8 | + | let records = try! sys::bootInfo(abi::Handle(env.args)); |
|
| 9 | + | assert records.len == 1; |
|
| 10 | + | let info = records[0]; |
|
| 11 | + | let self = sys::queryDomain(abi::Handle(info.handles[0])); |
|
| 12 | + | assert (self.id & 0xffffffff) == 0 and self.creator == self.id; |
|
| 13 | + | assert info.handles[1] == env.eventsHandle and info.timebase > 0; |
|
| 14 | + | let mut harts: u64 = 0; |
|
| 15 | + | for hart in 0..8 { if (info.harts & (1 << hart as u64)) <> 0 { |
|
| 16 | + | set harts += 1; |
|
| 17 | + | } } |
|
| 18 | + | assert info.count == 2 + info.devices + info.interrupts + harts; |
|
| 19 | + | for i in info.count as u32..abi::BOOT_HANDLES { |
|
| 20 | + | assert info.handles[i] == 0; |
|
| 21 | + | } |
|
| 22 | + | for i in 0..info.devices as u32 { |
|
| 23 | + | let device = sys::queryDevice(abi::Handle(info.handles[2 + i])); |
|
| 24 | + | assert device.size > 0; |
|
| 25 | + | } |
|
| 26 | + | for i in 0..info.interrupts as u32 { |
|
| 27 | + | let irq = sys::queryInterrupt(abi::Handle(info.handles[2 + info.devices as u32 + i])); |
|
| 28 | + | assert irq.number == i + 1; |
|
| 29 | + | } |
|
| 30 | + | assert sys::queryDevice(info.clock).size == 8; |
|
| 31 | + | assert sys::queryDevice(info.shutdown).size >= 4; |
|
| 32 | + | assert (try! sys::queryImage(info.rootImage)).entry <> 0; |
|
| 33 | + | assert (try! sys::queryImage(info.schedulerImage)).entry <> 0; |
|
| 34 | + | let current = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)); |
|
| 35 | + | let first = 2 + info.devices as u32 + info.interrupts as u32; |
|
| 36 | + | assert current.hart == 0 and current.budget == info.handles[first]; |
|
| 37 | + | assert (try! sys::queryBudget(abi::Handle(info.handles[first]))).hart == 0; |
|
| 38 | + | } |
test/bootstrap/run
added
+16 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Construct root from a boot package and retain authority after root exits. |
|
| 3 | + | set -eu |
|
| 4 | + | emulator=${RAD_EMULATOR:-emulator} |
|
| 5 | + | work=$(mktemp -d) |
|
| 6 | + | trap 'rm -rf "$work"' EXIT HUP INT TERM |
|
| 7 | + | scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" \ |
|
| 8 | + | -pkg loaded -mod test/bootstrap/root.rad \ |
|
| 9 | + | -mod abi=kernel/kernel/abi.rad -mod sys=kernel/kernel/sys.rad \ |
|
| 10 | + | -entry loaded -ril "$work" |
|
| 11 | + | scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" \ |
|
| 12 | + | -pkg service -mod test/bootstrap/service.rad -entry service -ril "$work" |
|
| 13 | + | sh test/kernel/build bootstrap "$work" "$work/bootstrap.rv64" \ |
|
| 14 | + | "$work/loaded.ril" "$work/service.ril" |
|
| 15 | + | "$emulator" -machine -harts=1 -memory-size=262144 -max-steps=2000000000 -run "$work/bootstrap.rv64" |
|
| 16 | + | printf 'bootstrap: root exit preserves allocation and creation authority\n' |
test/bootstrap/service.rad
added
+6 -0
| 1 | + | //! Independent boot package used for creation after root exit. |
|
| 2 | + | ||
| 3 | + | /// Return a small status from the service entry. |
|
| 4 | + | @default fn main() -> u64 { |
|
| 5 | + | return 7; |
|
| 6 | + | } |
test/cycles/build
added
+23 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Build the controller and worker images loaded after dispatch starts. |
|
| 3 | + | # Usage: build image |
|
| 4 | + | set -eu |
|
| 5 | + | image=$1 |
|
| 6 | + | # Stage beside the image so the final move is atomic. |
|
| 7 | + | work=$(mktemp -d "$(dirname "$image")/.build.XXXXXX") |
|
| 8 | + | trap 'rm -rf "$work"' EXIT HUP INT TERM |
|
| 9 | + | for package in spin job; do |
|
| 10 | + | set -- -pkg "$package" -mod "test/cycles/$package.rad" \ |
|
| 11 | + | -mod support=test/cycles/support.rad \ |
|
| 12 | + | -mod abi=kernel/kernel/abi.rad -mod sys=kernel/kernel/sys.rad |
|
| 13 | + | if [ "$package" = spin ]; then |
|
| 14 | + | set -- "$@" -mod mmio=kernel/mmio.rad |
|
| 15 | + | fi |
|
| 16 | + | scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" "$@" -entry "$package" -ril "$work" |
|
| 17 | + | done |
|
| 18 | + | printf '//! Binary inputs for repeated domain lifecycles.\n' > "$work/inputs.rad" |
|
| 19 | + | for package in spin job; do |
|
| 20 | + | sh test/kernel/embed "$package" "$work/$package.ril" >> "$work/inputs.rad" |
|
| 21 | + | done |
|
| 22 | + | sh test/kernel/build cycles "$work" "$work/cycles.rv64" |
|
| 23 | + | mv "$work/cycles.rv64" "$image" |
test/cycles/job.rad
added
+13 -0
| 1 | + | //! A private worker instance that exits normally or raises a CPU fault. |
|
| 2 | + | export mod support; |
|
| 3 | + | export mod abi; |
|
| 4 | + | export mod sys; |
|
| 5 | + | ||
| 6 | + | /// Check private library state before the selected terminal operation. |
|
| 7 | + | @default unsafe fn main(env: *abi::Env) { |
|
| 8 | + | assert support::advance() == 1; |
|
| 9 | + | if env.argsSize == 0 { |
|
| 10 | + | sys::exit(9); |
|
| 11 | + | } |
|
| 12 | + | assert false; |
|
| 13 | + | } |
test/cycles/kernel/dispatchcheck.rad
added
+297 -0
| 1 | + | //! Concurrent private package graphs and complete worker resource recovery. |
|
| 2 | + | export mod inputs; |
|
| 3 | + | export mod invariants; |
|
| 4 | + | use kernel::calls; |
|
| 5 | + | use kernel::fixture; |
|
| 6 | + | use kernel::limits; |
|
| 7 | + | use kernel::state; |
|
| 8 | + | use kernel::abi; |
|
| 9 | + | use kernel::frames; |
|
| 10 | + | use kernel::slots; |
|
| 11 | + | use kernel::pages; |
|
| 12 | + | use kernel::capability; |
|
| 13 | + | use kernel::registry; |
|
| 14 | + | use kernel::domains; |
|
| 15 | + | use kernel::budgets; |
|
| 16 | + | use kernel::dispatch; |
|
| 17 | + | use kernel::boot; |
|
| 18 | + | use kernel::sync; |
|
| 19 | + | use kernel::remote; |
|
| 20 | + | use kernel::events; |
|
| 21 | + | use kernel::devices; |
|
| 22 | + | use kernel::platform; |
|
| 23 | + | ||
| 24 | + | /// Delay before controllers start, scaled by online hart count. |
|
| 25 | + | constant START_DELAY: u64 = 1000000; |
|
| 26 | + | ||
| 27 | + | /// Duration reserved per worker, scaled by online hart count. |
|
| 28 | + | constant WORKER_TICKS: u64 = 10000000; |
|
| 29 | + | ||
| 30 | + | /// Duration reserved per controller, scaled by online hart count. |
|
| 31 | + | constant CONTROLLER_TICKS: u64 = 800000000; |
|
| 32 | + | ||
| 33 | + | /// Maximum maintenance requests before recovery must finish. |
|
| 34 | + | constant RECLAIM_ATTEMPTS: u32 = 100000; |
|
| 35 | + | ||
| 36 | + | /// Maximum frame or slot entries inspected in one metadata section. |
|
| 37 | + | constant FRAME_BATCH: u32 = 128; |
|
| 38 | + | ||
| 39 | + | // Setup initializes retained state for online harts before releasing READY. |
|
| 40 | + | // Other harts must acquire READY before they read this state. |
|
| 41 | + | /// Bootstrap resource capabilities. |
|
| 42 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 43 | + | /// Surviving event consumer. |
|
| 44 | + | unsafe static PARENT: abi::Ref = undefined; |
|
| 45 | + | /// Controller identities indexed by hart. |
|
| 46 | + | unsafe static CHILDREN: [fixture::Actor; limits::HARTS] = undefined; |
|
| 47 | + | /// Bootstrap-owned argument page addresses indexed by hart. |
|
| 48 | + | unsafe static ARGUMENTS: [u64; limits::HARTS] = [0; limits::HARTS]; |
|
| 49 | + | /// Free frames before controller creation. |
|
| 50 | + | unsafe static FREE: u32 = 0; |
|
| 51 | + | /// Reusable page, backing, domain, and context slots before controller |
|
| 52 | + | /// creation. |
|
| 53 | + | unsafe static SLOTS: [u32; 4] = [0; 4]; |
|
| 54 | + | /// Fixture publication barrier. |
|
| 55 | + | static READY: u64 = 0; |
|
| 56 | + | /// Validated physical page mapping. |
|
| 57 | + | fn memory(address: u64) -> *mut u8; |
|
| 58 | + | /// Current kernel package-state table. |
|
| 59 | + | fn kernelGp() -> u64; |
|
| 60 | + | /// Native checker entry. |
|
| 61 | + | fn completion() -> u64; |
|
| 62 | + | /// Write one diagnostic byte. |
|
| 63 | + | fn put(byte: u8); |
|
| 64 | + | /// Finish the native fixture. |
|
| 65 | + | fn finish(); |
|
| 66 | + | /// Sleep until this hart's software interrupt is pending. Interrupts stay |
|
| 67 | + | /// globally masked, so the interrupt wakes the hart without trapping. |
|
| 68 | + | fn park(); |
|
| 69 | + | ||
| 70 | + | /// Count reusable frames in short metadata sections. |
|
| 71 | + | unsafe fn freeFrames() -> u32 { |
|
| 72 | + | let mut count: u32 = 0; |
|
| 73 | + | let mut first: u32 = 0; |
|
| 74 | + | while first < state::SHARED.pages.backings.pool.count { |
|
| 75 | + | let mut end = first + FRAME_BATCH; |
|
| 76 | + | if end > state::SHARED.pages.backings.pool.count { |
|
| 77 | + | set end = state::SHARED.pages.backings.pool.count; |
|
| 78 | + | } |
|
| 79 | + | let guard = sync::enter(); |
|
| 80 | + | for i in first..end { |
|
| 81 | + | if frames::available(&state::SHARED.pages.backings.pool, i) { |
|
| 82 | + | set count += 1; |
|
| 83 | + | } |
|
| 84 | + | } |
|
| 85 | + | sync::leave(guard); |
|
| 86 | + | set first = end; |
|
| 87 | + | } |
|
| 88 | + | return count; |
|
| 89 | + | } |
|
| 90 | + | ||
| 91 | + | /// Count reusable object slots after all allocation transactions have stopped. |
|
| 92 | + | fn freeSlots(entries: &[slots::Slot]) -> u32 { |
|
| 93 | + | let mut count: u32 = 0; |
|
| 94 | + | let mut first: u32 = 0; |
|
| 95 | + | while first < entries.len { |
|
| 96 | + | let mut end = first + FRAME_BATCH; |
|
| 97 | + | if end > entries.len { |
|
| 98 | + | set end = entries.len; |
|
| 99 | + | } |
|
| 100 | + | let guard = sync::enter(); |
|
| 101 | + | for i in first..end { |
|
| 102 | + | assert entries[i].state <> slots::State::Reserved; |
|
| 103 | + | if entries[i].state == slots::State::Free { |
|
| 104 | + | set count += 1; |
|
| 105 | + | } |
|
| 106 | + | } |
|
| 107 | + | sync::leave(guard); |
|
| 108 | + | set first = end; |
|
| 109 | + | } |
|
| 110 | + | return count; |
|
| 111 | + | } |
|
| 112 | + | ||
| 113 | + | /// Read object capacity at the quiescent boundaries of the workload. |
|
| 114 | + | unsafe fn capacity() -> [u32; 4] { |
|
| 115 | + | return [freeSlots(&state::SHARED.pages.slots[..]), |
|
| 116 | + | freeSlots(&state::SHARED.pages.backings.slots[..]), |
|
| 117 | + | freeSlots(&state::SHARED.domains.slots[..]), |
|
| 118 | + | freeSlots(&state::SHARED.domains.contextSlots[..])]; |
|
| 119 | + | } |
|
| 120 | + | ||
| 121 | + | /// Publish fixture state before any hart enters dispatch. |
|
| 122 | + | export unsafe fn start(last: bool) { |
|
| 123 | + | if last { |
|
| 124 | + | setup(); |
|
| 125 | + | sync::storeRelease(&mut READY, 1); |
|
| 126 | + | // Every online hart finished dispatch::initialize before arriving. |
|
| 127 | + | for hart in 0..limits::HARTS { |
|
| 128 | + | if hart <> dispatch::hart() and (boot::PLATFORM.harts & (1 << hart)) |
|
| 129 | + | <> 0 { |
|
| 130 | + | dispatch::signal(hart); |
|
| 131 | + | } |
|
| 132 | + | } |
|
| 133 | + | } |
|
| 134 | + | while sync::loadAcquire(&READY) == 0 { |
|
| 135 | + | park(); |
|
| 136 | + | } |
|
| 137 | + | } |
|
| 138 | + | ||
| 139 | + | /// Prepare one controller per hart and a later surviving checker. |
|
| 140 | + | unsafe fn setup() { |
|
| 141 | + | let authority = fixture::bootstrap(&mut TABLE); |
|
| 142 | + | let image = fixture::load(&mut TABLE, authority, &inputs::spin[..]); |
|
| 143 | + | let source = fixture::input(&mut TABLE, authority, &inputs::job[..]); |
|
| 144 | + | let parentHandle = try! domains::create(&mut state::SHARED.domains, |
|
| 145 | + | &mut state::SHARED.pages.backings, &state::SHARED.registry, |
|
| 146 | + | &mut TABLE, authority, image); |
|
| 147 | + | set PARENT = (try! capability::get(&TABLE, parentHandle)).object; |
|
| 148 | + | let parent = try! domains::get(&state::SHARED.domains, PARENT); |
|
| 149 | + | fixture::machine(PARENT, completion(), kernelGp()); |
|
| 150 | + | let mut arguments: [abi::Handle; limits::HARTS] = undefined; |
|
| 151 | + | let mut count: u64 = 0; |
|
| 152 | + | for hart in 0..limits::HARTS { |
|
| 153 | + | if (boot::PLATFORM.harts & (1 << hart)) == 0 { |
|
| 154 | + | continue; |
|
| 155 | + | } |
|
| 156 | + | set count += 1; |
|
| 157 | + | set arguments[hart] = try! pages::allocate(&mut state::SHARED.pages, |
|
| 158 | + | &mut TABLE, authority, 1); |
|
| 159 | + | set ARGUMENTS[hart] = (try! pages::get(&state::SHARED.pages, |
|
| 160 | + | (try! capability::get(&TABLE, arguments[hart])).object)).base; |
|
| 161 | + | } |
|
| 162 | + | set FREE = freeFrames(); |
|
| 163 | + | set SLOTS = capacity(); |
|
| 164 | + | for hart in 0..limits::HARTS { |
|
| 165 | + | if (boot::PLATFORM.harts & (1 << hart)) == 0 { |
|
| 166 | + | continue; |
|
| 167 | + | } |
|
| 168 | + | let actor = fixture::create(&mut TABLE, authority, image); |
|
| 169 | + | let handle = actor.handle; |
|
| 170 | + | let object = actor.domain; |
|
| 171 | + | let mut child = try! domains::get(&state::SHARED.domains, object); |
|
| 172 | + | set CHILDREN[hart] = actor; |
|
| 173 | + | try! domains::reparent(&mut state::SHARED.domains, &TABLE, handle, |
|
| 174 | + | parentHandle); |
|
| 175 | + | let self = try! capability::install(&mut child.memory.table, |
|
| 176 | + | capability::Entry { |
|
| 177 | + | kind: abi::Kind::Domain, object, |
|
| 178 | + | rights: abi::Rights(abi::CREATE | abi::ALLOCATE), |
|
| 179 | + | }); |
|
| 180 | + | let stack = try! pages::allocate(&mut state::SHARED.pages, |
|
| 181 | + | &mut child.memory.table, self, 4); |
|
| 182 | + | let args = @sliceOf(memory(ARGUMENTS[hart]) as *mut u64, 6); |
|
| 183 | + | set args[0] = *(try! pages::grant(&mut state::SHARED.pages, |
|
| 184 | + | &TABLE, &mut child.memory.table, source, abi::READ as u64)); |
|
| 185 | + | set args[1] = inputs::job.len as u64; |
|
| 186 | + | for i in 0..state::SHARED.devices.count { |
|
| 187 | + | if state::SHARED.devices.regions[i].kind == platform::Kind::Clint { |
|
| 188 | + | set args[2] = *(try! devices::install(&state::SHARED.devices, |
|
| 189 | + | &mut child.memory.table, i)); |
|
| 190 | + | } |
|
| 191 | + | } |
|
| 192 | + | assert args[2] <> 0; |
|
| 193 | + | let page = try! pages::grant(&mut state::SHARED.pages, &TABLE, |
|
| 194 | + | &mut child.memory.table, arguments[hart], |
|
| 195 | + | (abi::READ | abi::WRITE | abi::GRANT) as u64); |
|
| 196 | + | set args[3] = *page; |
|
| 197 | + | set args[4] = WORKER_TICKS * count; |
|
| 198 | + | try! fixture::activate(&TABLE, handle, stack, page, 56); |
|
| 199 | + | } |
|
| 200 | + | for byte in "cycles controllers ready\n" { |
|
| 201 | + | put(byte); |
|
| 202 | + | } |
|
| 203 | + | let now = dispatch::clock(); |
|
| 204 | + | let start = now + START_DELAY * count; |
|
| 205 | + | for hart in 0..limits::HARTS { |
|
| 206 | + | if (boot::PLATFORM.harts & (1 << hart)) == 0 { |
|
| 207 | + | continue; |
|
| 208 | + | } |
|
| 209 | + | let window = try! budgets::seed(&mut state::SHARED.budgets, |
|
| 210 | + | &mut TABLE, hart, start, 0xffffffffffffffff); |
|
| 211 | + | let tail = try! budgets::split(&mut state::SHARED.budgets, |
|
| 212 | + | &mut TABLE, window, start + CONTROLLER_TICKS * count, now); |
|
| 213 | + | try! calls::rebind(&mut state::SHARED, &mut TABLE, |
|
| 214 | + | budgets::Binding { budget: window, domain: CHILDREN[hart].handle, |
|
| 215 | + | context: CHILDREN[hart].context }, now); |
|
| 216 | + | if hart == 0 { |
|
| 217 | + | let final = try! calls::rebind(&mut state::SHARED, &mut TABLE, |
|
| 218 | + | budgets::Binding { budget: tail, domain: parentHandle, |
|
| 219 | + | context: parent.initial }, now); |
|
| 220 | + | } |
|
| 221 | + | } |
|
| 222 | + | } |
|
| 223 | + | ||
| 224 | + | /// Check all terminal records, shared code identity, and transient recovery. |
|
| 225 | + | /// Only the surviving checker runs these phases after the worker windows end. |
|
| 226 | + | export unsafe fn verify() { |
|
| 227 | + | let retained = checkWorkers(); |
|
| 228 | + | checkRecovery(retained); |
|
| 229 | + | invariants::check(&TABLE); |
|
| 230 | + | for byte in "cycles final recovery and invariants passed\n" { |
|
| 231 | + | put(byte); |
|
| 232 | + | } |
|
| 233 | + | finish(); |
|
| 234 | + | } |
|
| 235 | + | ||
| 236 | + | /// Check terminal records and package reuse under the metadata lock. |
|
| 237 | + | unsafe fn checkWorkers() -> u32 { |
|
| 238 | + | for byte in "cycles checking completion\n" { |
|
| 239 | + | put(byte); |
|
| 240 | + | } |
|
| 241 | + | let guard = sync::enter(); |
|
| 242 | + | let mut parent = try! domains::get(&state::SHARED.domains, PARENT); |
|
| 243 | + | let job = registry::find(®istry::capture(&state::SHARED.registry), |
|
| 244 | + | &"job"[..]) else panic "checkWorkers: worker package required"; |
|
| 245 | + | let package = try! registry::get(&state::SHARED.registry, job); |
|
| 246 | + | let mut seen: u32 = 0; |
|
| 247 | + | while let event = try! events::pop(&mut parent.memory.ring) { |
|
| 248 | + | assert event.kind == events::CHILD_EXIT and event.code == 68; |
|
| 249 | + | let mut found = false; |
|
| 250 | + | for hart in 0..limits::HARTS { |
|
| 251 | + | if (boot::PLATFORM.harts & (1 << hart)) == 0 { |
|
| 252 | + | continue; |
|
| 253 | + | } |
|
| 254 | + | if event.value == abi::id(CHILDREN[hart].domain) { |
|
| 255 | + | assert (seen & (1 << hart)) == 0; |
|
| 256 | + | set seen |= 1 << hart; |
|
| 257 | + | set found = true; |
|
| 258 | + | let args = @sliceOf(memory(ARGUMENTS[hart]) as *mut u64, 7); |
|
| 259 | + | assert args[5] == package.codeAddress and args[6] == 4; |
|
| 260 | + | } |
|
| 261 | + | } |
|
| 262 | + | assert found; |
|
| 263 | + | } |
|
| 264 | + | assert seen == boot::PLATFORM.harts; |
|
| 265 | + | try! events::refresh(&mut state::SHARED.domains.events, PARENT, |
|
| 266 | + | &parent.memory.ring); |
|
| 267 | + | assert state::SHARED.loader.live[job.index] and state::SHARED.loader.owner |
|
| 268 | + | == nil; |
|
| 269 | + | let retained = state::SHARED.loader.resident[job.index].code.count |
|
| 270 | + | + state::SHARED.loader.resident[job.index].metadata.count; |
|
| 271 | + | sync::leave(guard); |
|
| 272 | + | return retained; |
|
| 273 | + | } |
|
| 274 | + | ||
| 275 | + | /// Reap stopped domains, then compare frame and object capacity at quiescence. |
|
| 276 | + | unsafe fn checkRecovery(retained: u32) { |
|
| 277 | + | let mut done = false; |
|
| 278 | + | for attempt in 0..RECLAIM_ATTEMPTS { |
|
| 279 | + | let guard = sync::enter(); |
|
| 280 | + | set done = state::SHARED.domains.dead == 0 |
|
| 281 | + | and state::SHARED.pages.backings.pool.retiredCount == 0; |
|
| 282 | + | if done { |
|
| 283 | + | sync::leave(guard); |
|
| 284 | + | break; |
|
| 285 | + | } |
|
| 286 | + | let request = try! dispatch::request(&mut state::SHARED.remote, 0, |
|
| 287 | + | remote::Action::Reschedule); |
|
| 288 | + | sync::leave(guard); |
|
| 289 | + | assert try! dispatch::awaitRequest(0, request) == 0; |
|
| 290 | + | } |
|
| 291 | + | assert done; |
|
| 292 | + | assert freeFrames() + retained == FREE; |
|
| 293 | + | let recovered = capacity(); |
|
| 294 | + | for i in 0..4 { |
|
| 295 | + | assert recovered[i] == SLOTS[i]; |
|
| 296 | + | } |
|
| 297 | + | } |
test/cycles/machine.ras
added
+36 -0
| 1 | + | //! Start all initialized harts after the fixture publishes its shared domains. |
|
| 2 | + | .text; |
|
| 3 | + | call @kernel::boot::initialize; |
|
| 4 | + | call @kernel::dispatchcheck::start; |
|
| 5 | + | call @kernel::boot::run; |
|
| 6 | + | ebreak; |
|
| 7 | + | .export @kernel::dispatchcheck::memory; |
|
| 8 | + | .export @kernel::dispatchcheck::kernelGp; |
|
| 9 | + | .export @kernel::dispatchcheck::completion; |
|
| 10 | + | .export @kernel::dispatchcheck::finish; |
|
| 11 | + | .export @kernel::dispatchcheck::put; |
|
| 12 | + | .export @kernel::dispatchcheck::park; |
|
| 13 | + | @kernel::dispatchcheck::memory |
|
| 14 | + | ret; |
|
| 15 | + | @kernel::dispatchcheck::kernelGp |
|
| 16 | + | mv %a0 %gp; |
|
| 17 | + | ret; |
|
| 18 | + | @kernel::dispatchcheck::completion |
|
| 19 | + | la %a0 @complete; |
|
| 20 | + | ret; |
|
| 21 | + | @complete |
|
| 22 | + | call @kernel::dispatchcheck::verify; |
|
| 23 | + | ebreak; |
|
| 24 | + | @kernel::dispatchcheck::put |
|
| 25 | + | li %t0 0x10000000; |
|
| 26 | + | sb %a0 0(%t0); |
|
| 27 | + | ret; |
|
| 28 | + | @kernel::dispatchcheck::park |
|
| 29 | + | csrsi mie 8; |
|
| 30 | + | wfi; |
|
| 31 | + | ret; |
|
| 32 | + | @kernel::dispatchcheck::finish |
|
| 33 | + | li %t0 0x10001000; |
|
| 34 | + | li %t1 0x5555; |
|
| 35 | + | sw %t1 0(%t0); |
|
| 36 | + | ebreak; |
test/cycles/spin.rad
added
+109 -0
| 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 | + | } |
|
| 100 | + | else { |
|
| 101 | + | assert event.kind == 3 and event.code == 3; |
|
| 102 | + | } |
|
| 103 | + | memoryFence(); |
|
| 104 | + | set ring.head = head + 1; |
|
| 105 | + | try! sys::capabilityDrop(image); |
|
| 106 | + | set args[6] = turn as u64 + 1; |
|
| 107 | + | } |
|
| 108 | + | sys::exit(68); |
|
| 109 | + | } |
test/cycles/support.rad
added
+9 -0
| 1 | + | //! Shared library code with private state in each domain graph. |
|
| 2 | + | /// Per-instance invocation count. |
|
| 3 | + | unsafe static COUNT: u64 = 0; |
|
| 4 | + | ||
| 5 | + | /// Advance this domain's private library state. |
|
| 6 | + | export unsafe fn advance() -> u64 { |
|
| 7 | + | set COUNT += 1; |
|
| 8 | + | return COUNT; |
|
| 9 | + | } |
test/dispatch/build
added
+22 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Build a production dispatch fixture image. |
|
| 3 | + | # Usage: build fixture image |
|
| 4 | + | set -eu |
|
| 5 | + | fixture=$1 |
|
| 6 | + | image=$2 |
|
| 7 | + | # Stage beside the image so the final move is atomic. |
|
| 8 | + | work=$(mktemp -d "$(dirname "$image")/.build.XXXXXX") |
|
| 9 | + | trap 'rm -rf "$work"' EXIT HUP INT TERM |
|
| 10 | + | set -- -pkg spin -mod "test/$fixture/spin.rad" \ |
|
| 11 | + | -mod abi=kernel/kernel/abi.rad -mod sys=kernel/kernel/sys.rad |
|
| 12 | + | if [ -f "test/$fixture/cases.rad" ]; then |
|
| 13 | + | set -- "$@" -mod "cases=test/$fixture/cases.rad" |
|
| 14 | + | fi |
|
| 15 | + | if [ "$fixture" = mmio ]; then |
|
| 16 | + | set -- "$@" -mod mmio=kernel/mmio.rad |
|
| 17 | + | fi |
|
| 18 | + | scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" "$@" -entry spin -ril "$work" |
|
| 19 | + | printf '//! Binary inputs for the dispatch fixture.\n' > "$work/inputs.rad" |
|
| 20 | + | sh test/kernel/embed INPUT "$work/spin.ril" >> "$work/inputs.rad" |
|
| 21 | + | sh test/kernel/build "$fixture" "$work" "$work/dispatch.rv64" |
|
| 22 | + | mv "$work/dispatch.rv64" "$image" |
test/dispatch/kernel/dispatchcheck.rad
added
+247 -0
| 1 | + | //! Native timer preemption across user execution, idle, and machine |
|
| 2 | + | //! continuations. |
|
| 3 | + | export mod inputs; |
|
| 4 | + | use std::arch::rv64::shared; |
|
| 5 | + | use std::arch::rv64; |
|
| 6 | + | use kernel::calls; |
|
| 7 | + | use kernel::fixture; |
|
| 8 | + | use kernel::state; |
|
| 9 | + | use kernel::abi; |
|
| 10 | + | use kernel::pages; |
|
| 11 | + | use kernel::capability; |
|
| 12 | + | use kernel::registry; |
|
| 13 | + | use kernel::instances; |
|
| 14 | + | use kernel::domains; |
|
| 15 | + | use kernel::budgets; |
|
| 16 | + | use kernel::dispatch; |
|
| 17 | + | use kernel::boot; |
|
| 18 | + | use kernel::registers; |
|
| 19 | + | use kernel::events; |
|
| 20 | + | use kernel::sync; |
|
| 21 | + | ||
| 22 | + | /// Number of scheduled fixture actors. |
|
| 23 | + | constant COUNT: u32 = 3; |
|
| 24 | + | /// Index of the user-mode workload. |
|
| 25 | + | constant USER: u32 = 0; |
|
| 26 | + | /// Index of the retained machine-mode workload. |
|
| 27 | + | constant WORKER: u32 = 1; |
|
| 28 | + | /// Index of the final machine checker. |
|
| 29 | + | constant CHECKER: u32 = 2; |
|
| 30 | + | ||
| 31 | + | /// Delay before user dispatch, in machine timer ticks. |
|
| 32 | + | constant START_DELAY: u64 = 1000000; |
|
| 33 | + | ||
| 34 | + | /// Duration of a dispatch span, large enough for user setup calls. |
|
| 35 | + | constant WINDOW_TICKS: u64 = 32000000; |
|
| 36 | + | ||
| 37 | + | // State retained across setup and verification is initialized before |
|
| 38 | + | // dispatch starts. The scheduled checker reads it after the user workload. |
|
| 39 | + | /// Bootstrap authority used to create fixture domains and CPU windows. |
|
| 40 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 41 | + | /// Domain handles for budget binding and initial contexts for the user, |
|
| 42 | + | /// machine worker, and completion check. |
|
| 43 | + | unsafe static ACTORS: [fixture::Actor; COUNT] = undefined; |
|
| 44 | + | /// View a validated allocated source page. |
|
| 45 | + | fn memory(address: u64) -> *mut u8; |
|
| 46 | + | /// Current kernel package-state base. |
|
| 47 | + | fn kernelGp() -> u64; |
|
| 48 | + | /// Native entry for retained M-mode work. |
|
| 49 | + | fn worker() -> u64; |
|
| 50 | + | /// Native entry for the final assertion callback. |
|
| 51 | + | fn completion() -> u64; |
|
| 52 | + | /// Complete the machine fixture with a passing status. |
|
| 53 | + | fn finish(); |
|
| 54 | + | /// Complete the fixture with a distinct failed invariant code. |
|
| 55 | + | fn failed(code: u64); |
|
| 56 | + | ||
| 57 | + | /// Give the user domain its stack and its arguments Page, which names a Page |
|
| 58 | + | /// for queries, the package, the self Domain and the completion checker, and |
|
| 59 | + | /// activate it. The future window is added to the arguments later. Return the |
|
| 60 | + | /// arguments. |
|
| 61 | + | unsafe fn startUser(authority: abi::Handle, user: &mut domains::Domain, |
|
| 62 | + | executable: abi::Handle, selfAuthority: abi::Handle) -> *mut [u8] |
|
| 63 | + | { |
|
| 64 | + | let moved = fixture::stack(&mut TABLE, authority, |
|
| 65 | + | &mut user.memory.table); |
|
| 66 | + | let startup = try! pages::allocate(&mut state::SHARED.pages, |
|
| 67 | + | &mut TABLE, authority, 1); |
|
| 68 | + | let granted = try! pages::grant(&mut state::SHARED.pages, &TABLE, |
|
| 69 | + | &mut user.memory.table, startup, abi::READ as u64); |
|
| 70 | + | let stackCap = try! capability::get(&TABLE, startup); |
|
| 71 | + | let stackPage = try! pages::get(&state::SHARED.pages, stackCap.object); |
|
| 72 | + | let checkerContext = ACTORS[CHECKER].context; |
|
| 73 | + | let checkerOwner = |
|
| 74 | + | state::SHARED.domains.contexts[checkerContext.index].owner; |
|
| 75 | + | let parent = try! capability::install(&mut user.memory.table, |
|
| 76 | + | capability::Entry { |
|
| 77 | + | kind: abi::Kind::Domain, object: checkerOwner, |
|
| 78 | + | rights: abi::Rights(abi::WAKE), |
|
| 79 | + | }); |
|
| 80 | + | let args = @sliceOf(memory(stackPage.base), 40); |
|
| 81 | + | let words = [*granted, *executable, *selfAuthority]; |
|
| 82 | + | for word in 0..COUNT { |
|
| 83 | + | for byte in 0..fixture::WORD_BYTES { |
|
| 84 | + | set args[word * 8 + byte] = (words[word] >> (byte as u64 * 8)) as |
|
| 85 | + | u8; |
|
| 86 | + | } |
|
| 87 | + | } |
|
| 88 | + | for byte in 0..fixture::WORD_BYTES { |
|
| 89 | + | set args[32 + byte] = (*parent >> (byte as u64 * 8)) as u8; |
|
| 90 | + | } |
|
| 91 | + | try! fixture::activate(&TABLE, ACTORS[USER].handle, moved, granted, 40); |
|
| 92 | + | return args; |
|
| 93 | + | } |
|
| 94 | + | ||
| 95 | + | /// Prepare real private domains before enabling dispatch on hart zero. |
|
| 96 | + | export unsafe fn setup() { |
|
| 97 | + | let authority = fixture::bootstrap(&mut TABLE); |
|
| 98 | + | let image = fixture::load(&mut TABLE, authority, &inputs::INPUT[..]); |
|
| 99 | + | for i in 0..COUNT { |
|
| 100 | + | set ACTORS[i] = fixture::create(&mut TABLE, authority, image); |
|
| 101 | + | let actor = ACTORS[i]; |
|
| 102 | + | if i <> USER { |
|
| 103 | + | fixture::machine(actor.domain, |
|
| 104 | + | worker() if i == WORKER else completion(), kernelGp()); |
|
| 105 | + | } |
|
| 106 | + | } |
|
| 107 | + | let cap = try! capability::get(&TABLE, ACTORS[USER].handle); |
|
| 108 | + | let mut user = try! domains::get(&state::SHARED.domains, cap.object); |
|
| 109 | + | let selfAuthority = try! capability::install(&mut user.memory.table, |
|
| 110 | + | capability::Entry { |
|
| 111 | + | kind: abi::Kind::Domain, object: cap.object, |
|
| 112 | + | rights: abi::Rights(abi::CREATE | abi::ALLOCATE | abi::EXECUTE | |
|
| 113 | + | abi::WAKE | abi::DESTROY), |
|
| 114 | + | }); |
|
| 115 | + | let root = user.image; |
|
| 116 | + | let executable = try! registry::install(&state::SHARED.registry, |
|
| 117 | + | &mut user.memory.table, root); |
|
| 118 | + | let args = startUser(authority, &mut user, executable, selfAuthority); |
|
| 119 | + | let imageRef = try! registry::image(&state::SHARED.registry, &TABLE, |
|
| 120 | + | image, abi::Rights(abi::EXECUTE)); |
|
| 121 | + | let target = try! registry::exported(&state::SHARED.registry, imageRef, |
|
| 122 | + | &"spin::count"[..]); |
|
| 123 | + | let case shared::Target::Data(data) = target else panic "setup: counter"; |
|
| 124 | + | let timer = try! dispatch::timer(&boot::PLATFORM, 0); |
|
| 125 | + | let clock = dispatch::now(timer); |
|
| 126 | + | let start = clock + START_DELAY; |
|
| 127 | + | // The user must finish its setup calls inside the first span, so the span |
|
| 128 | + | // is large relative to the kernel work of those calls. `spin::worker` arms |
|
| 129 | + | // its timeout for a quarter of the span. |
|
| 130 | + | let span: u64 = WINDOW_TICKS; |
|
| 131 | + | let workerContext = ACTORS[WORKER].context; |
|
| 132 | + | let frame = &mut state::SHARED.domains.contexts[workerContext.index].frame; |
|
| 133 | + | set frame.registers[*rv64::A0 as u32] = |
|
| 134 | + | instances::base(&state::SHARED.domains.tables[cap.object.index], |
|
| 135 | + | data.slot) + data.offset as u64; |
|
| 136 | + | set frame.registers[*rv64::A1 as u32] = registers::start(timer.window) + |
|
| 137 | + | dispatch::CLOCK; |
|
| 138 | + | set frame.registers[*rv64::A2 as u32] = start + 3 * span; |
|
| 139 | + | set frame.registers[*rv64::A3 as u32] = start + 4 * span; |
|
| 140 | + | let mut windows: [abi::Handle; 6] = undefined; |
|
| 141 | + | let mut remaining = try! budgets::seed(&mut state::SHARED.budgets, |
|
| 142 | + | &mut TABLE, 0, start, start + 6 * span); |
|
| 143 | + | for i in 0..6 { |
|
| 144 | + | set windows[i] = remaining; |
|
| 145 | + | if i < 5 { |
|
| 146 | + | set remaining = try! budgets::split(&mut state::SHARED.budgets, |
|
| 147 | + | &mut TABLE, remaining, start + (i as u64 + 1) * span, clock); |
|
| 148 | + | } |
|
| 149 | + | } |
|
| 150 | + | let owners = &[USER, USER, WORKER, USER, WORKER, CHECKER]; |
|
| 151 | + | for i in 0..6 { |
|
| 152 | + | // Window 1 is an idle gap. Dispatch retires expired windows through |
|
| 153 | + | // their owner's domain table, which the bootstrap table is not, so it |
|
| 154 | + | // is dropped. |
|
| 155 | + | if i == 1 { |
|
| 156 | + | try! budgets::drop(&mut state::SHARED.budgets, &mut TABLE, |
|
| 157 | + | windows[i]); |
|
| 158 | + | continue; |
|
| 159 | + | } |
|
| 160 | + | let owner = owners[i]; |
|
| 161 | + | let handle = try! calls::rebind(&mut state::SHARED, &mut TABLE, |
|
| 162 | + | budgets::Binding { budget: windows[i], |
|
| 163 | + | domain: ACTORS[owner].handle, |
|
| 164 | + | context: ACTORS[owner].context }, clock); |
|
| 165 | + | if i == 3 { |
|
| 166 | + | for byte in 0..fixture::WORD_BYTES { |
|
| 167 | + | set args[24 + byte] = (*handle >> (byte as u64 * 8)) as u8; |
|
| 168 | + | } |
|
| 169 | + | } |
|
| 170 | + | } |
|
| 171 | + | } |
|
| 172 | + | ||
| 173 | + | /// Check state captured by production timer handling after both resumptions. |
|
| 174 | + | export unsafe fn verify() { |
|
| 175 | + | let userContext = ACTORS[USER].context; |
|
| 176 | + | let checkerContext = ACTORS[CHECKER].context; |
|
| 177 | + | let userOwner = state::SHARED.domains.contexts[userContext.index].owner; |
|
| 178 | + | let checkerOwner = |
|
| 179 | + | state::SHARED.domains.contexts[checkerContext.index].owner; |
|
| 180 | + | let table = &state::SHARED.domains.tables[userOwner.index]; |
|
| 181 | + | let user = state::SHARED.domains.contexts[userContext.index].frame; |
|
| 182 | + | let machine = |
|
| 183 | + | state::SHARED.domains.contexts[ACTORS[WORKER].context.index].frame; |
|
| 184 | + | assert (user.status & fixture::MACHINE_PRIVILEGE) == 0 and |
|
| 185 | + | (machine.status & fixture::MACHINE_PRIVILEGE) == |
|
| 186 | + | fixture::MACHINE_PRIVILEGE; |
|
| 187 | + | assert machine.registers[*rv64::S0 as u32] > 0 and |
|
| 188 | + | machine.registers[*rv64::S1 as u32] == 1 and machine.registers[*rv64::S2 |
|
| 189 | + | as u32] > 0; |
|
| 190 | + | let mut domain = try! domains::get(&state::SHARED.domains, userOwner); |
|
| 191 | + | let target = try! registry::exported(&state::SHARED.registry, domain.image, |
|
| 192 | + | &"spin::extraCount"[..]); |
|
| 193 | + | let case shared::Target::Data(data) = target |
|
| 194 | + | else panic "verify: extra counter"; |
|
| 195 | + | let bytes = @sliceOf(memory(instances::base( table, |
|
| 196 | + | data.slot) + data.offset as u64), 8); |
|
| 197 | + | let mut progress: u64 = 0; |
|
| 198 | + | for byte in 0..fixture::WORD_BYTES { |
|
| 199 | + | set progress |= bytes[byte] as u64 << (byte as u64 * 8); |
|
| 200 | + | } |
|
| 201 | + | if progress == 0 { |
|
| 202 | + | failed(6); |
|
| 203 | + | } |
|
| 204 | + | let checked = try! registry::exported(&state::SHARED.registry, |
|
| 205 | + | domain.image, &"spin::destroyed"[..]); |
|
| 206 | + | let case shared::Target::Data(flag) = checked |
|
| 207 | + | else panic "verify: destruction flag"; |
|
| 208 | + | if *memory(instances::base( table, flag.slot) + flag.offset as u64) <> 1 { |
|
| 209 | + | failed(7); |
|
| 210 | + | } |
|
| 211 | + | let child = try! registry::exported(&state::SHARED.registry, domain.image, |
|
| 212 | + | &"spin::childId"[..]); |
|
| 213 | + | let case shared::Target::Data(identity) = child |
|
| 214 | + | else panic "verify: child identity"; |
|
| 215 | + | let idBytes = @sliceOf(memory(instances::base( table, |
|
| 216 | + | identity.slot) + identity.offset as u64), 8); |
|
| 217 | + | let mut childId: u64 = 0; |
|
| 218 | + | for byte in 0..fixture::WORD_BYTES { |
|
| 219 | + | set childId |= idBytes[byte] as u64 << (byte as u64 * 8); |
|
| 220 | + | } |
|
| 221 | + | let owner = checkerOwner; |
|
| 222 | + | let guard = sync::enter(); |
|
| 223 | + | let mut checker = try! domains::get(&state::SHARED.domains, owner); |
|
| 224 | + | assert try! events::flush(&mut state::SHARED.domains.events, owner, |
|
| 225 | + | &mut checker.memory.ring) == 0; |
|
| 226 | + | assert not try! events::acknowledged(&state::SHARED.domains.events, |
|
| 227 | + | abi::reference(childId)); |
|
| 228 | + | let exited = try! events::pop(&mut checker.memory.ring) |
|
| 229 | + | else panic "verify: child exit"; |
|
| 230 | + | assert exited.kind == events::CHILD_EXIT and exited.code == 0 and |
|
| 231 | + | exited.value == childId; |
|
| 232 | + | try! events::refresh(&mut state::SHARED.domains.events, owner, |
|
| 233 | + | &checker.memory.ring); |
|
| 234 | + | assert try! events::acknowledged(&state::SHARED.domains.events, |
|
| 235 | + | abi::reference(childId)); |
|
| 236 | + | sync::leave(guard); |
|
| 237 | + | let wake = try! events::pop(&mut domain.memory.ring) |
|
| 238 | + | else panic "verify: wake token" |
|
| 239 | + | ; |
|
| 240 | + | assert wake.kind == events::WAKEUP and wake.code == 123; |
|
| 241 | + | let timeout = try! events::pop(&mut domain.memory.ring) |
|
| 242 | + | else panic "verify: timeout token"; |
|
| 243 | + | assert timeout.kind == events::TIMEOUT and timeout.code == 77 and |
|
| 244 | + | timeout.value == 0; |
|
| 245 | + | assert try! events::pop(&mut domain.memory.ring) == nil; |
|
| 246 | + | finish(); |
|
| 247 | + | } |
test/dispatch/machine.ras
added
+81 -0
| 1 | + | //! Exercise real timer-driven switching on hart zero. |
|
| 2 | + | // Machine completion device address. |
|
| 3 | + | .constant DISPATCH_TEST_FINISH_ADDRESS 0x10001000; |
|
| 4 | + | // Successful machine completion value. |
|
| 5 | + | .constant DISPATCH_TEST_FINISH_PASS 0x5555; |
|
| 6 | + | // Failure status code in the completion register. |
|
| 7 | + | .constant DISPATCH_TEST_FINISH_FAILURE_CODE 0x3333; |
|
| 8 | + | // Bit offset of the completion status field. |
|
| 9 | + | .constant DISPATCH_TEST_STATUS_SHIFT 16; |
|
| 10 | + | ||
| 11 | + | .text; |
|
| 12 | + | call @kernel::boot::initialize; |
|
| 13 | + | call @kernel::dispatchcheck::setup; |
|
| 14 | + | call @kernel::boot::run; |
|
| 15 | + | @fail |
|
| 16 | + | li %a0 1; |
|
| 17 | + | j @failure; |
|
| 18 | + | @late |
|
| 19 | + | li %a0 2; |
|
| 20 | + | j @failure; |
|
| 21 | + | @noProgress |
|
| 22 | + | li %a0 3; |
|
| 23 | + | j @failure; |
|
| 24 | + | @noResumption |
|
| 25 | + | li %a0 4; |
|
| 26 | + | j @failure; |
|
| 27 | + | @stackCorruption |
|
| 28 | + | li %a0 5; |
|
| 29 | + | @failure |
|
| 30 | + | li %t0 DISPATCH_TEST_FINISH_ADDRESS; |
|
| 31 | + | slli %t1 %a0 DISPATCH_TEST_STATUS_SHIFT; |
|
| 32 | + | li %t2 DISPATCH_TEST_FINISH_FAILURE_CODE; |
|
| 33 | + | or %t1 %t1 %t2; |
|
| 34 | + | sw %t1 0(%t0); |
|
| 35 | + | .export @kernel::dispatchcheck::memory; |
|
| 36 | + | .export @kernel::dispatchcheck::kernelGp; |
|
| 37 | + | .export @kernel::dispatchcheck::worker; |
|
| 38 | + | .export @kernel::dispatchcheck::completion; |
|
| 39 | + | .export @kernel::dispatchcheck::finish; |
|
| 40 | + | .export @kernel::dispatchcheck::failed; |
|
| 41 | + | @kernel::dispatchcheck::failed |
|
| 42 | + | j @failure; |
|
| 43 | + | @kernel::dispatchcheck::memory |
|
| 44 | + | ret; |
|
| 45 | + | @kernel::dispatchcheck::kernelGp |
|
| 46 | + | mv %a0 %gp; |
|
| 47 | + | ret; |
|
| 48 | + | @kernel::dispatchcheck::worker |
|
| 49 | + | la %a0 @machineWork; |
|
| 50 | + | ret; |
|
| 51 | + | @kernel::dispatchcheck::completion |
|
| 52 | + | la %a0 @complete; |
|
| 53 | + | ret; |
|
| 54 | + | @machineWork |
|
| 55 | + | ld %t0 0(%a1); |
|
| 56 | + | bgeu %t0 %a2 @late; |
|
| 57 | + | ld %s2 0(%a0); |
|
| 58 | + | beqz %s2 @noProgress; |
|
| 59 | + | addi %sp %sp -16; |
|
| 60 | + | li %s3 1234567; |
|
| 61 | + | sd %s3 0(%sp); |
|
| 62 | + | li %s0 0; |
|
| 63 | + | li %s1 0; |
|
| 64 | + | @workLoop |
|
| 65 | + | addi %s0 %s0 1; |
|
| 66 | + | ld %t0 0(%sp); |
|
| 67 | + | bne %t0 %s3 @stackCorruption; |
|
| 68 | + | ld %t0 0(%a1); |
|
| 69 | + | bltu %t0 %a3 @workLoop; |
|
| 70 | + | ld %t0 0(%a0); |
|
| 71 | + | bgeu %s2 %t0 @noResumption; |
|
| 72 | + | li %s1 1; |
|
| 73 | + | j @workLoop; |
|
| 74 | + | @complete |
|
| 75 | + | call @kernel::dispatchcheck::verify; |
|
| 76 | + | j @fail; |
|
| 77 | + | @kernel::dispatchcheck::finish |
|
| 78 | + | li %t0 DISPATCH_TEST_FINISH_ADDRESS; |
|
| 79 | + | li %t1 DISPATCH_TEST_FINISH_PASS; |
|
| 80 | + | sw %t1 0(%t0); |
|
| 81 | + | j @fail; |
test/dispatch/run
added
+15 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Preempt and resume U-mode and M-mode work through production dispatch. |
|
| 3 | + | # Usage: run fixture image harts [irq] |
|
| 4 | + | set -eu |
|
| 5 | + | emulator=${RAD_EMULATOR:-emulator} |
|
| 6 | + | fixture=$1 |
|
| 7 | + | image=$2 |
|
| 8 | + | harts=$3 |
|
| 9 | + | irq=${4:-0} |
|
| 10 | + | set -- |
|
| 11 | + | if [ "$irq" -ne 0 ]; then |
|
| 12 | + | set -- "-irq=$irq" "-irq-at=1" |
|
| 13 | + | fi |
|
| 14 | + | "$emulator" -machine -harts="$harts" -memory-size=262144 -max-steps=2000000000 "$@" -run "$image" |
|
| 15 | + | printf '%s: %s-hart execution passed\n' "$fixture" "$harts" |
test/dispatch/spin.rad
added
+222 -0
| 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(number: u32, a0: i64, a1: i64, a2: i64, a3: i64, buffer: &mut opaque) |
|
| 7 | + | -> i64; |
|
| 8 | + | ||
| 9 | + | /// Query a Page into a buffer that is smaller than the record. |
|
| 10 | + | fn shortQuery(handle: i64) -> i64 { |
|
| 11 | + | let mut one: [u64; 1] = [0; 1]; |
|
| 12 | + | return ecallBuffer(80, handle, 0, 0, 0, &mut one); |
|
| 13 | + | } |
|
| 14 | + | /// Private query output used to check writable package-state authority. |
|
| 15 | + | unsafe static INFO: [u64; 2] = [0; 2]; |
|
| 16 | + | /// Private progress counter observed by the machine fixture. |
|
| 17 | + | export unsafe static count: u64 = 0; |
|
| 18 | + | /// Additional-context progress in the same private package state. |
|
| 19 | + | export unsafe static extraCount: u64 = 0; |
|
| 20 | + | /// Successful user-side return and destruction checks. |
|
| 21 | + | export unsafe static destroyed: u64 = 0; |
|
| 22 | + | /// Generation-bearing identity of the child that returns normally. |
|
| 23 | + | export unsafe static childId: u64 = 0; |
|
| 24 | + | /// Local self-Domain authority shared by both user contexts. |
|
| 25 | + | unsafe static SELF: abi::Handle = abi::Handle(0); |
|
| 26 | + | ||
| 27 | + | /// Update shared private state and return from an additional context. |
|
| 28 | + | export unsafe fn worker(args: u64, size: u64) { |
|
| 29 | + | assert args == 0 and size == 0; |
|
| 30 | + | let current = try! sys::queryContext(SELF, abi::CURRENT_CONTEXT); |
|
| 31 | + | let initial = try! sys::queryContext(SELF, 0); |
|
| 32 | + | assert current.context <> initial.context and current.budget <> 0; |
|
| 33 | + | assert initial.budget == 0; |
|
| 34 | + | set extraCount += 1; |
|
| 35 | + | // A quarter of the span of the fixture's budget windows. |
|
| 36 | + | try! sys::timeout(8000000, 77); |
|
| 37 | + | sys::wait(); |
|
| 38 | + | set extraCount += 1; |
|
| 39 | + | set count += 1; |
|
| 40 | + | } |
|
| 41 | + | /// Reach a page and the event ring as slices: the rights of the handle decide the access. |
|
| 42 | + | fn pageSlices(domain: abi::Handle, events: abi::Handle, allocated: abi::Handle) { |
|
| 43 | + | // The memory of a page is reached as a slice, with the rights of the handle. |
|
| 44 | + | let writable = try! sys::pageBytesMut(allocated); |
|
| 45 | + | assert writable.len == 4096; |
|
| 46 | + | set writable[0] = 7; |
|
| 47 | + | set writable[4095] = 9; |
|
| 48 | + | let narrowed = try! sys::capabilityGrant(allocated, abi::Handle(0), abi::READ as u64); |
|
| 49 | + | let seen = try! sys::pageBytes(narrowed); |
|
| 50 | + | assert seen.len == 4096 and seen[0] == 7 and seen[4095] == 9; |
|
| 51 | + | let mut rejected = false; |
|
| 52 | + | try sys::pageBytesMut(narrowed) catch error { |
|
| 53 | + | assert error == abi::Error::Denied; |
|
| 54 | + | set rejected = true; |
|
| 55 | + | }; |
|
| 56 | + | assert rejected; |
|
| 57 | + | set rejected = false; |
|
| 58 | + | try sys::pageBytes(domain) catch error { |
|
| 59 | + | assert error == abi::Error::BadHandle; |
|
| 60 | + | set rejected = true; |
|
| 61 | + | }; |
|
| 62 | + | assert rejected; |
|
| 63 | + | let ring = try! sys::pageBytesMut(events); |
|
| 64 | + | assert ring.len > 0; |
|
| 65 | + | try! sys::capabilityDrop(narrowed); |
|
| 66 | + | } |
|
| 67 | + | ||
| 68 | + | /// Check Page, Events, Domain and Image queries and Page lifecycle calls. |
|
| 69 | + | unsafe fn basics(query: abi::Handle, domainHandle: abi::Handle, events: abi::Handle, |
|
| 70 | + | package: abi::Handle) { |
|
| 71 | + | let info = sys::queryPage(query); |
|
| 72 | + | assert info.base <> 0 and info.count == 1; |
|
| 73 | + | let mut rejected = false; |
|
| 74 | + | try sys::pageAllocate(abi::Handle(0), 0) catch error { |
|
| 75 | + | assert error == abi::Error::InvalidArg; |
|
| 76 | + | set rejected = true; |
|
| 77 | + | }; |
|
| 78 | + | assert rejected; |
|
| 79 | + | let allocated = try! sys::pageAllocate(abi::Handle(0), 1); |
|
| 80 | + | assert sys::queryPage(allocated).count == 1; |
|
| 81 | + | pageSlices(domainHandle, events, allocated); |
|
| 82 | + | let narrowed = try! sys::capabilityGrant(allocated, abi::Handle(0), abi::READ as u64); |
|
| 83 | + | try! sys::capabilityDrop(narrowed); |
|
| 84 | + | try! sys::capabilityDrop(allocated); |
|
| 85 | + | assert sys::queryEvents(events).capacity == 256; |
|
| 86 | + | let domain = sys::queryDomain(domainHandle); |
|
| 87 | + | assert domain.id <> 0 and domain.state == 1; |
|
| 88 | + | let image = try! sys::queryImage(package); |
|
| 89 | + | assert image.entry <> 0 and image.codeBase <> 0 and image.codeBytes > 0 and image.dataBytes > 0; |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// Split the future window into the windows of the extra context and the child, and bind them. |
|
| 93 | + | unsafe fn partition(future: abi::Handle, domainHandle: abi::Handle, extra: u64, |
|
| 94 | + | child: abi::Handle) { |
|
| 95 | + | let later = try! sys::queryBudget(future); |
|
| 96 | + | let tail = try! sys::budgetSplit(future, later.start + (later.end - later.start) / 2); |
|
| 97 | + | let childWindow = try! sys::budgetSplit(tail, later.start + (later.end - later.start) * 5 / 8); |
|
| 98 | + | let resumption = try! sys::budgetSplit(childWindow, |
|
| 99 | + | later.start + (later.end - later.start) * 11 / 16); |
|
| 100 | + | let awakened = try! sys::budgetSplit(resumption, |
|
| 101 | + | later.start + (later.end - later.start) * 3 / 4); |
|
| 102 | + | try! sys::budgetSplit(awakened, |
|
| 103 | + | later.start + (later.end - later.start) * 13 / 16); |
|
| 104 | + | let bound = try! sys::budgetBind(tail, domainHandle, extra); |
|
| 105 | + | try! sys::budgetBind(awakened, SELF, extra); |
|
| 106 | + | let initial = try! sys::queryContext(child, 0); |
|
| 107 | + | try! sys::budgetBind(childWindow, child, initial.context); |
|
| 108 | + | } |
|
| 109 | + | ||
| 110 | + | /// Check the error boundary of raw calls and buffers. |
|
| 111 | + | unsafe fn rawCalls(handle: i64) { |
|
| 112 | + | let mut local: [u64; 2] = [0; 2]; |
|
| 113 | + | assert ecallBuffer(80, handle, 0, 0, 0, &mut local) == 0; |
|
| 114 | + | assert local[0] <> 0 and local[1] == 1; |
|
| 115 | + | assert ecallBuffer(80, handle, 0, 0, 0, &mut INFO) == 0; |
|
| 116 | + | assert INFO[0] == local[0] and INFO[1] == local[1]; |
|
| 117 | + | assert shortQuery(handle) == -4; |
|
| 118 | + | assert ecall(80, handle, 0, 0, 0) == -4; |
|
| 119 | + | assert ecallBuffer(80, handle, 4096, 0, 0, &mut local) == -4; |
|
| 120 | + | assert ecall(0xffffffff, 0, 0, 0, 0) == -4; |
|
| 121 | + | } |
|
| 122 | + | ||
| 123 | + | /// Check direct calls and handoff, then exercise independently bound contexts. |
|
| 124 | + | @default unsafe fn main(env: *abi::Env) { |
|
| 125 | + | if env.argsSize == 0 { |
|
| 126 | + | try! sys::timeout(10000, 88); |
|
| 127 | + | sys::wait(); |
|
| 128 | + | return; |
|
| 129 | + | } |
|
| 130 | + | assert env.argsSize == 40; |
|
| 131 | + | // The arguments Page holds a Page capability, the resident package, the self-Domain |
|
| 132 | + | // capability, the future window, and the completion checker. |
|
| 133 | + | let startup = try! sys::pageWords(abi::Handle(env.args)); |
|
| 134 | + | let query = startup[0]; |
|
| 135 | + | let package = abi::Handle(startup[1]); |
|
| 136 | + | let domainHandle = abi::Handle(startup[2]); |
|
| 137 | + | let futureHandle = abi::Handle(startup[3]); |
|
| 138 | + | let parentHandle = abi::Handle(startup[4]); |
|
| 139 | + | let events = abi::Handle(env.eventsHandle); |
|
| 140 | + | set SELF = domainHandle; |
|
| 141 | + | let handle = query as i64; |
|
| 142 | + | basics(abi::Handle(query), domainHandle, events, package); |
|
| 143 | + | let domain = sys::queryDomain(domainHandle); |
|
| 144 | + | let mut rejected = false; |
|
| 145 | + | let child = try! sys::domainCreate(abi::Handle(0), package); |
|
| 146 | + | set childId = sys::queryDomain(child).id; |
|
| 147 | + | try! sys::domainReparent(child, parentHandle); |
|
| 148 | + | let stack = try! sys::pageAllocate(abi::Handle(0), 1); |
|
| 149 | + | let childStack = try! sys::capabilityTransfer(stack, child, (abi::READ | abi::WRITE) as u64); |
|
| 150 | + | try! sys::domainActivate(child, childStack, abi::Handle(0), 0); |
|
| 151 | + | assert sys::queryDomain(child).state == 1; |
|
| 152 | + | set rejected = false; |
|
| 153 | + | try sys::domainActivate(child, childStack, abi::Handle(0), 0) catch error { |
|
| 154 | + | assert error == abi::Error::NotPending; |
|
| 155 | + | set rejected = true; |
|
| 156 | + | }; |
|
| 157 | + | assert rejected; |
|
| 158 | + | set rejected = false; |
|
| 159 | + | try sys::imageLoad(abi::Handle(0), abi::Handle(query), 0, 0) catch error { |
|
| 160 | + | assert error == abi::Error::InvalidArg; |
|
| 161 | + | set rejected = true; |
|
| 162 | + | }; |
|
| 163 | + | assert rejected; |
|
| 164 | + | let current = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)); |
|
| 165 | + | assert current.context <> 0 and current.hart == 0 and current.budget <> 0; |
|
| 166 | + | let initial = try! sys::queryContext(domainHandle, 0); |
|
| 167 | + | assert initial.context == current.context and initial.budget == 0; |
|
| 168 | + | set rejected = false; |
|
| 169 | + | try sys::queryContext(child, abi::CURRENT_CONTEXT) catch error { |
|
| 170 | + | assert error == abi::Error::InvalidArg; |
|
| 171 | + | set rejected = true; |
|
| 172 | + | }; |
|
| 173 | + | assert rejected; |
|
| 174 | + | let window = try! sys::queryBudget(abi::Handle(current.budget)); |
|
| 175 | + | assert window.context == current.context and window.hart == current.hart |
|
| 176 | + | and window.start < window.end; |
|
| 177 | + | let extraStack = try! sys::pageAllocate(abi::Handle(0), 1); |
|
| 178 | + | let extraMemory = sys::queryPage(extraStack); |
|
| 179 | + | let extra = try! sys::contextCreate(domainHandle, abi::ContextStart { |
|
| 180 | + | entry: worker as u64, stack: *extraStack, args: 0, size: 0, |
|
| 181 | + | }); |
|
| 182 | + | let context = try! sys::queryContext(domainHandle, extra); |
|
| 183 | + | assert context.context == extra and context.domain == domain.id; |
|
| 184 | + | assert context.stackBase == extraMemory.base and context.stackTop == extraMemory.base + 4096; |
|
| 185 | + | let future = futureHandle; |
|
| 186 | + | partition(future, domainHandle, extra, child); |
|
| 187 | + | rawCalls(handle); |
|
| 188 | + | set rejected = false; |
|
| 189 | + | try sys::yield(SELF) catch error { |
|
| 190 | + | assert error == abi::Error::Busy; |
|
| 191 | + | set rejected = true; |
|
| 192 | + | }; |
|
| 193 | + | let after = try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT); |
|
| 194 | + | assert rejected and after.budget == current.budget; |
|
| 195 | + | set count = 1; |
|
| 196 | + | try! sys::yield(child); |
|
| 197 | + | let continued = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)); |
|
| 198 | + | assert continued.context == current.context and continued.budget == *future; |
|
| 199 | + | while extraCount == 0 { |
|
| 200 | + | set count += 1; |
|
| 201 | + | } |
|
| 202 | + | let waiting = try! sys::queryContext(SELF, extra); |
|
| 203 | + | assert waiting.state == 2 and waiting.hart == 0xffffffffffffffff; |
|
| 204 | + | try! sys::wakeup(SELF, 123); |
|
| 205 | + | assert extraCount == 1 or extraCount == 2; |
|
| 206 | + | while extraCount == 1 { |
|
| 207 | + | set count += 1; |
|
| 208 | + | } |
|
| 209 | + | let stopped = try! sys::queryContext(domainHandle, extra); |
|
| 210 | + | assert stopped.state == 1 and stopped.hart == 0xffffffffffffffff; |
|
| 211 | + | try! sys::contextDestroy(domainHandle, extra); |
|
| 212 | + | set rejected = false; |
|
| 213 | + | try sys::queryContext(domainHandle, extra) catch error { |
|
| 214 | + | assert error == abi::Error::BadHandle; |
|
| 215 | + | set rejected = true; |
|
| 216 | + | }; |
|
| 217 | + | assert rejected; |
|
| 218 | + | set destroyed = 1; |
|
| 219 | + | while true { |
|
| 220 | + | set count += 1; |
|
| 221 | + | } |
|
| 222 | + | } |
test/kernel.rad
added
+39 -0
| 1 | + | //! Kernel modules and machine-test entry points. |
|
| 2 | + | ||
| 3 | + | export mod fdt; |
|
| 4 | + | export mod abi; |
|
| 5 | + | export mod limits; |
|
| 6 | + | export mod slots; |
|
| 7 | + | export mod range; |
|
| 8 | + | export mod sync; |
|
| 9 | + | export mod platform; |
|
| 10 | + | export mod trap; |
|
| 11 | + | export mod capability; |
|
| 12 | + | export mod transactions; |
|
| 13 | + | export mod events; |
|
| 14 | + | export mod interrupts; |
|
| 15 | + | export mod devices; |
|
| 16 | + | export mod registers; |
|
| 17 | + | export mod plic; |
|
| 18 | + | export mod registry; |
|
| 19 | + | export mod frames; |
|
| 20 | + | export mod physical; |
|
| 21 | + | export mod backing; |
|
| 22 | + | export mod pages; |
|
| 23 | + | export mod loader; |
|
| 24 | + | export mod instances; |
|
| 25 | + | export mod domains; |
|
| 26 | + | export mod lifecycle; |
|
| 27 | + | export mod budgets; |
|
| 28 | + | export mod dispatch; |
|
| 29 | + | export mod timers; |
|
| 30 | + | export mod remote; |
|
| 31 | + | export mod state; |
|
| 32 | + | export mod locked; |
|
| 33 | + | export mod access; |
|
| 34 | + | export mod calls; |
|
| 35 | + | export mod sys; |
|
| 36 | + | export mod boot; |
|
| 37 | + | export mod startup; |
|
| 38 | + | export mod fixture; |
|
| 39 | + | export mod dispatchcheck; |
test/kernel/build
added
+36 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Build a machine-test fixture from explicitly mapped kernel modules. |
|
| 3 | + | # Usage: sh test/kernel/build fixture work image [boot-packages...] |
|
| 4 | + | set -eu |
|
| 5 | + | fixture=$1 |
|
| 6 | + | work=$2 |
|
| 7 | + | image=$3 |
|
| 8 | + | shift 3 |
|
| 9 | + | ( |
|
| 10 | + | set -- -pkg std |
|
| 11 | + | while IFS= read -r source; do set -- "$@" -mod "$source"; done < std.lib |
|
| 12 | + | set -- "$@" -pkg kernel -mod test/kernel.rad |
|
| 13 | + | for source in kernel/kernel/*.rad; do |
|
| 14 | + | name=${source##*/} |
|
| 15 | + | name=${name%.rad} |
|
| 16 | + | [ "$name" != tests ] || continue |
|
| 17 | + | set -- "$@" -mod "$name=$source" |
|
| 18 | + | done |
|
| 19 | + | set -- "$@" -mod fixture=test/kernel/fixture.rad |
|
| 20 | + | set -- "$@" -mod "dispatchcheck=test/$fixture/kernel/dispatchcheck.rad" |
|
| 21 | + | if [ -f "test/$fixture/cases.rad" ]; then |
|
| 22 | + | set -- "$@" -mod "dispatchcheck/cases=test/$fixture/cases.rad" |
|
| 23 | + | fi |
|
| 24 | + | if [ -f "$work/inputs.rad" ]; then |
|
| 25 | + | set -- "$@" -mod "dispatchcheck/inputs=$work/inputs.rad" |
|
| 26 | + | fi |
|
| 27 | + | case $fixture in |
|
| 28 | + | smp|cycles) |
|
| 29 | + | set -- "$@" -mod dispatchcheck/invariants=test/smp/kernel/invariants.rad |
|
| 30 | + | ;; |
|
| 31 | + | esac |
|
| 32 | + | scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" "$@" -entry kernel -ril "$work" |
|
| 33 | + | ) |
|
| 34 | + | cat "test/$fixture/machine.ras" kernel/kernel/*.ras > "$work/machine.ras" |
|
| 35 | + | scripts/hosted bin/kernel.build.rv64 -- "$work/std.ril" "$work/kernel.ril" \ |
|
| 36 | + | "$work/machine.ras" "$image" "$@" |
test/kernel/embed
added
+13 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Write a Radiance byte-array declaration for a binary fixture. |
|
| 3 | + | # Usage: embed name binary-file |
|
| 4 | + | set -eu |
|
| 5 | + | name=$1 |
|
| 6 | + | source=$2 |
|
| 7 | + | length=$(wc -c < "$source") |
|
| 8 | + | bytes=$(od -An -v -tu1 "$source") |
|
| 9 | + | printf '\n/// Binary RIL bytes for %s.\nexport static %s: [u8; %s] = [\n' \ |
|
| 10 | + | "$name" "$name" "$length" |
|
| 11 | + | printf '%s\n' "$bytes" | |
|
| 12 | + | awk '{ for (i = 1; i <= NF; i++) printf "%s,", $i; print "" }' |
|
| 13 | + | printf '];\n' |
test/kernel/fixture.rad
added
+137 -0
| 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::limits; |
|
| 9 | + | use kernel::loader; |
|
| 10 | + | use kernel::pages; |
|
| 11 | + | use kernel::physical; |
|
| 12 | + | use kernel::slots; |
|
| 13 | + | use kernel::capability; |
|
| 14 | + | use kernel::domains; |
|
| 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 and interrupt-enable state restored by mret. |
|
| 24 | + | export constant MACHINE_STATUS: u64 = MACHINE_PRIVILEGE | 0x80; |
|
| 25 | + | ||
| 26 | + | /// A pending domain and its initial execution context. |
|
| 27 | + | export record Actor: Copy { |
|
| 28 | + | /// Execute capability in the fixture's bootstrap table. |
|
| 29 | + | handle: abi::Handle, |
|
| 30 | + | /// Generation-bearing domain identity. |
|
| 31 | + | domain: abi::Ref, |
|
| 32 | + | /// Initial context identity. |
|
| 33 | + | context: abi::Ref, |
|
| 34 | + | } |
|
| 35 | + | ||
| 36 | + | /// Initialize bootstrap authority before any hart can access fixture resources. |
|
| 37 | + | export unsafe fn bootstrap(table: &mut capability::Table) -> abi::Handle { |
|
| 38 | + | let pending = try! slots::reserve(&mut state::SHARED.domains.slots[..]); |
|
| 39 | + | let owner = try! slots::commit(&mut state::SHARED.domains.slots[..], |
|
| 40 | + | pending); |
|
| 41 | + | capability::initialize(table, owner); |
|
| 42 | + | try! backing::registerDomain(&mut state::SHARED.pages.backings, owner); |
|
| 43 | + | return try! capability::install(table, capability::Entry { |
|
| 44 | + | kind: abi::Kind::Domain, object: owner, |
|
| 45 | + | rights: abi::Rights(abi::CREATE | abi::ALLOCATE), |
|
| 46 | + | }); |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | /// Copy package bytes to a fresh bootstrap-owned page allocation. The caller |
|
| 50 | + | /// must have exclusive access to shared page metadata. |
|
| 51 | + | export unsafe fn input(table: &mut capability::Table, authority: abi::Handle, |
|
| 52 | + | bytes: &[u8]) -> abi::Handle |
|
| 53 | + | { |
|
| 54 | + | let handle = try! pages::allocate(&mut state::SHARED.pages, table, |
|
| 55 | + | authority, |
|
| 56 | + | (bytes.len as u64 + limits::FRAME_SIZE - 1) / limits::FRAME_SIZE); |
|
| 57 | + | let page = try! pages::get(&state::SHARED.pages, |
|
| 58 | + | (try! capability::get(table, handle)).object); |
|
| 59 | + | try! physical::write(&state::SHARED.pages.backings.pool, page.base, bytes); |
|
| 60 | + | return handle; |
|
| 61 | + | } |
|
| 62 | + | ||
| 63 | + | /// Load a fixture package before dispatch starts. |
|
| 64 | + | export unsafe fn load(table: &mut capability::Table, authority: abi::Handle, |
|
| 65 | + | bytes: &[u8]) -> abi::Handle |
|
| 66 | + | { |
|
| 67 | + | let source = input(table, authority, bytes); |
|
| 68 | + | return try! loader::load(&mut state::SHARED.loader, |
|
| 69 | + | &mut state::SHARED.pages, &mut state::SHARED.registry, table, |
|
| 70 | + | loader::Request { authority, source, offset: 0, |
|
| 71 | + | length: bytes.len as u64 }); |
|
| 72 | + | } |
|
| 73 | + | ||
| 74 | + | /// Create a pending fixture actor before dispatch starts. |
|
| 75 | + | export unsafe fn create(table: &mut capability::Table, authority: abi::Handle, |
|
| 76 | + | image: abi::Handle) -> Actor { |
|
| 77 | + | let handle = try! domains::create(&mut state::SHARED.domains, |
|
| 78 | + | &mut state::SHARED.pages.backings, &state::SHARED.registry, table, |
|
| 79 | + | authority, image); |
|
| 80 | + | let domain = (try! capability::get(table, handle)).object; |
|
| 81 | + | let initial = (try! domains::get(&state::SHARED.domains, domain)).initial; |
|
| 82 | + | return Actor { handle, domain, context: initial }; |
|
| 83 | + | } |
|
| 84 | + | ||
| 85 | + | /// Prepare a machine-mode checker or worker before dispatch starts. |
|
| 86 | + | export unsafe fn machine(domain: abi::Ref, entry: u64, gp: u64) { |
|
| 87 | + | let object = try! domains::get(&state::SHARED.domains, domain); |
|
| 88 | + | set state::SHARED.domains.records[domain.index].state = |
|
| 89 | + | domains::Lifecycle::Active; |
|
| 90 | + | let context = &mut state::SHARED.domains.contexts[object.initial.index]; |
|
| 91 | + | set context.frame.pc = entry; |
|
| 92 | + | set context.frame.status = MACHINE_STATUS; |
|
| 93 | + | set context.frame.registers[*rv64::SP as u32] = context.kernelStack.end; |
|
| 94 | + | set context.frame.registers[*rv64::GP as u32] = gp; |
|
| 95 | + | } |
|
| 96 | + | ||
| 97 | + | /// Bind disjoint child windows and a final checker window on hart zero. The |
|
| 98 | + | /// caller runs this before dispatch starts. Times use machine timer ticks. |
|
| 99 | + | export unsafe fn schedule(table: &mut capability::Table, actors: &[Actor], |
|
| 100 | + | checker: Actor, start: u64, span: u64, now: u64) |
|
| 101 | + | { |
|
| 102 | + | let mut remaining = try! budgets::seed(&mut state::SHARED.budgets, table, |
|
| 103 | + | 0, start, start + (actors.len as u64 + 1) * span); |
|
| 104 | + | for actor, i in actors { |
|
| 105 | + | let next = try! budgets::split(&mut state::SHARED.budgets, table, |
|
| 106 | + | remaining, start + (i as u64 + 1) * span, now); |
|
| 107 | + | let bound = try! calls::rebind(&mut state::SHARED, table, |
|
| 108 | + | budgets::Binding { budget: remaining, domain: actor.handle, |
|
| 109 | + | context: actor.context }, now); |
|
| 110 | + | set remaining = next; |
|
| 111 | + | } |
|
| 112 | + | let bound = try! calls::rebind(&mut state::SHARED, table, |
|
| 113 | + | budgets::Binding { budget: remaining, domain: checker.handle, |
|
| 114 | + | context: checker.context }, now); |
|
| 115 | + | } |
|
| 116 | + | ||
| 117 | + | /// Allocate a writable stack in a child table before dispatch starts. |
|
| 118 | + | export unsafe fn stack(table: &mut capability::Table, authority: abi::Handle, |
|
| 119 | + | child: &mut capability::Table) -> abi::Handle |
|
| 120 | + | { |
|
| 121 | + | let page = try! pages::allocate(&mut state::SHARED.pages, table, authority, |
|
| 122 | + | 1); |
|
| 123 | + | return try! pages::transfer(&mut state::SHARED.pages, table, child, page, |
|
| 124 | + | (abi::READ | abi::WRITE) as u64); |
|
| 125 | + | } |
|
| 126 | + | ||
| 127 | + | /// Activate the domain that an Execute capability in `table` names. The caller |
|
| 128 | + | /// must have exclusive access to the shared domain and page stores. |
|
| 129 | + | export unsafe fn activate(table: &capability::Table, handle: abi::Handle, |
|
| 130 | + | stack: abi::Handle, args: abi::Handle, size: u64) throws (abi::Error) |
|
| 131 | + | { |
|
| 132 | + | let entry = try capability::lookup(table, handle, abi::Kind::Domain, |
|
| 133 | + | abi::Rights(abi::EXECUTE)); |
|
| 134 | + | let domain = try domains::get(&state::SHARED.domains, entry.object); |
|
| 135 | + | try domains::activate(&mut state::SHARED.domains, &mut state::SHARED.pages, |
|
| 136 | + | entry.object, &mut *domain.memory, stack, args, size); |
|
| 137 | + | } |
test/loader/hostile.rad
added
+181 -0
| 1 | + | //! Generate binary RIL packages that the kernel loader must reject. |
|
| 2 | + | //! |
|
| 3 | + | //! The language cannot express these programs, so each one is built as typed IL and |
|
| 4 | + | //! encoded. Every package is small and well formed for the decoder, and breaks exactly one |
|
| 5 | + | //! rule of the verifier. The tool writes `<name>.ril` for each package into the directory |
|
| 6 | + | //! given as the first argument. |
|
| 7 | + | ||
| 8 | + | use std::sys; |
|
| 9 | + | use std::sys::unix; |
|
| 10 | + | use std::lang::alloc; |
|
| 11 | + | use std::lang::il; |
|
| 12 | + | use std::lang::il::binary; |
|
| 13 | + | use std::lang::il::binary::program; |
|
| 14 | + | ||
| 15 | + | /// Layout of eight plain bytes. |
|
| 16 | + | constant PLAIN: u32 = 0; |
|
| 17 | + | /// Layout of a nullable owned pointer slot. |
|
| 18 | + | constant CELL: u32 = 1; |
|
| 19 | + | /// Layout of an owned writable slice slot of plain words. |
|
| 20 | + | constant SLICE: u32 = 2; |
|
| 21 | + | ||
| 22 | + | /// Storage for the IL of one package. |
|
| 23 | + | static BUILD: [u8; 65536] = [0; 65536]; |
|
| 24 | + | /// Storage for the encoding of one package. |
|
| 25 | + | static OUT: [u8; 65536] = [0; 65536]; |
|
| 26 | + | ||
| 27 | + | /// Name of the entry function of every package. |
|
| 28 | + | constant ENTRY: *[u8] = "hostile::main"; |
|
| 29 | + | ||
| 30 | + | /// Plain data of eight bytes. |
|
| 31 | + | fn word() -> il::ValType { |
|
| 32 | + | return il::ValType::Data(il::Type::W64); |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Register `n` as a value. |
|
| 36 | + | fn reg(n: u32) -> il::Val { |
|
| 37 | + | return il::Val::Reg(il::Reg { n }); |
|
| 38 | + | } |
|
| 39 | + | ||
| 40 | + | /// A slot list of one slot. |
|
| 41 | + | fn only(arena: &mut alloc::Arena, slot: il::Slot) -> *[il::Slot] { |
|
| 42 | + | let mut list: *mut [il::Slot] = &mut []; |
|
| 43 | + | try! list.append(slot, arena); |
|
| 44 | + | return &list[..]; |
|
| 45 | + | } |
|
| 46 | + | ||
| 47 | + | /// Layout table shared by the packages. |
|
| 48 | + | fn layouts(arena: &mut alloc::Arena) -> *mut [il::Layout] { |
|
| 49 | + | let cell = il::PtrType { layout: PLAIN, own: true, mutable: true, nullable: true }; |
|
| 50 | + | let mut table: *mut [il::Layout] = &mut []; |
|
| 51 | + | try! table.append(il::Layout { size: 8, alignment: 8, slots: &[] }, arena); |
|
| 52 | + | let pointer = only(arena, il::Slot { offset: 0, kind: il::SlotKind::Ptr(cell) }); |
|
| 53 | + | try! table.append(il::Layout { size: 8, alignment: 8, slots: pointer }, arena); |
|
| 54 | + | let words = il::PtrType { layout: PLAIN, own: true, mutable: true, nullable: true }; |
|
| 55 | + | let slice = only(arena, il::Slot { offset: 0, kind: il::SlotKind::Slice(words) }); |
|
| 56 | + | try! table.append(il::Layout { size: 16, alignment: 8, slots: slice }, arena); |
|
| 57 | + | return table; |
|
| 58 | + | } |
|
| 59 | + | ||
| 60 | + | /// A function of one block. |
|
| 61 | + | fn function(arena: &mut alloc::Arena, name: *[u8], instrs: &[il::Instr], isExtern: bool) -> il::Fn { |
|
| 62 | + | let mut body: *mut [il::Instr] = &mut []; |
|
| 63 | + | try! body.extend(instrs, arena); |
|
| 64 | + | let mut blocks: *mut [il::Block] = &mut []; |
|
| 65 | + | if not isExtern { |
|
| 66 | + | try! blocks.append(il::Block { |
|
| 67 | + | label: il::BlockLabel { base: "b", suffix: nil }, params: &[], instrs: &body[..], |
|
| 68 | + | locs: &[], preds: &[], loopDepth: 0, |
|
| 69 | + | }, arena); |
|
| 70 | + | } |
|
| 71 | + | return il::Fn { |
|
| 72 | + | name, params: &[], returnType: word(), isExtern, isLeaf: false, |
|
| 73 | + | blocks: &blocks[..], vals: &[], cases: &[], |
|
| 74 | + | }; |
|
| 75 | + | } |
|
| 76 | + | ||
| 77 | + | /// Instruction `ret 0`. |
|
| 78 | + | fn done() -> il::Instr { |
|
| 79 | + | return il::Instr::Ret { val: il::Val::Imm(0) }; |
|
| 80 | + | } |
|
| 81 | + | ||
| 82 | + | /// Instruction that reserves a frame object of `layout` in register `dst`. |
|
| 83 | + | fn reserve(dst: u32, layout: u32) -> il::Instr { |
|
| 84 | + | return il::Instr::Reserve { dst: il::Reg { n: dst }, layout }; |
|
| 85 | + | } |
|
| 86 | + | ||
| 87 | + | /// Instruction that stores the 64-bit `src` at `offset` of the object in register `dst`. |
|
| 88 | + | fn store64(src: il::Val, dst: u32, offset: i32) -> il::Instr { |
|
| 89 | + | return il::Instr::Store { typ: il::Type::W64, src, dst: il::Reg { n: dst }, offset }; |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// Instruction that loads 64 bits at `offset` of the object in register `src`. |
|
| 93 | + | fn load64(dst: u32, src: u32, offset: i32) -> il::Instr { |
|
| 94 | + | return il::Instr::Load { typ: il::Type::W64, dst: il::Reg { n: dst }, src: il::Reg { n: src }, |
|
| 95 | + | offset }; |
|
| 96 | + | } |
|
| 97 | + | ||
| 98 | + | /// Write one package to `<directory>/<name>.ril`. |
|
| 99 | + | unsafe fn emit(directory: *[u8], name: *[u8], dependencies: *[*[u8]], data: &[il::Data], |
|
| 100 | + | instrs: &[il::Instr], |
|
| 101 | + | externs: bool, symbols: *[*[u8]]) |
|
| 102 | + | { |
|
| 103 | + | let mut arena = alloc::new(&mut BUILD[..]); |
|
| 104 | + | let mut list: *mut [*il::Fn] = &mut []; |
|
| 105 | + | let mut main: *mut [il::Fn] = &mut []; |
|
| 106 | + | try! main.append(function(&mut arena, ENTRY, instrs, false), &mut arena); |
|
| 107 | + | try! list.append(&main[0], &mut arena); |
|
| 108 | + | if externs { |
|
| 109 | + | let mut escape: *mut [il::Fn] = &mut []; |
|
| 110 | + | try! escape.append(function(&mut arena, "hostile::escape", &[], true), &mut arena); |
|
| 111 | + | try! list.append(&escape[0], &mut arena); |
|
| 112 | + | } |
|
| 113 | + | let mut tables: *mut [il::Data] = &mut []; |
|
| 114 | + | try! tables.extend(data, &mut arena); |
|
| 115 | + | let table = layouts(&mut arena); |
|
| 116 | + | let package = binary::Package { |
|
| 117 | + | symbols, name: "hostile", dependencies, exports: &[], entry: ENTRY, |
|
| 118 | + | program: il::Program { |
|
| 119 | + | data: &tables[..], fns: &list[..], layouts: &table[..], signatures: &[], |
|
| 120 | + | }, |
|
| 121 | + | }; |
|
| 122 | + | let mut output: *mut [u8] = &mut OUT[..]; |
|
| 123 | + | let length = try! program::encode(&mut output, &package); |
|
| 124 | + | let mut path: *mut [u8] = &mut []; |
|
| 125 | + | try! path.extend(directory, &mut arena); |
|
| 126 | + | try! path.extend("/", &mut arena); |
|
| 127 | + | try! path.extend(name, &mut arena); |
|
| 128 | + | try! path.extend(".ril", &mut arena); |
|
| 129 | + | // The system call takes a terminated string. |
|
| 130 | + | try! path.append(0, &mut arena); |
|
| 131 | + | assert unix::writeFile(&path[..], &OUT[..length]); |
|
| 132 | + | } |
|
| 133 | + | ||
| 134 | + | /// Generate every hostile package into the directory in the first argument. |
|
| 135 | + | @default unsafe fn main(env: *sys::Env) -> i32 { |
|
| 136 | + | assert env.args.len == 2; |
|
| 137 | + | let directory = env.args[1]; |
|
| 138 | + | let plain: *[*[u8]] = &["hostile", ENTRY]; |
|
| 139 | + | let none: *[*[u8]] = &[]; |
|
| 140 | + | // A word of data is dereferenced as if it were a pointer. |
|
| 141 | + | emit(directory, "forged", none, &[], &[ |
|
| 142 | + | il::Instr::Copy { dst: il::Reg { n: 0 }, val: il::Val::Imm(0x80000000) }, |
|
| 143 | + | load64(1, 0, 0), done(), |
|
| 144 | + | ], false, plain); |
|
| 145 | + | // An integer is stored over a pointer slot of static data. |
|
| 146 | + | let window = il::Data { |
|
| 147 | + | name: "hostile::window", size: 8, alignment: 8, layout: CELL, readOnly: false, |
|
| 148 | + | isZeroInit: true, values: &[], |
|
| 149 | + | }; |
|
| 150 | + | emit(directory, "overwrite", none, &[window], &[ |
|
| 151 | + | il::Instr::Copy { dst: il::Reg { n: 0 }, val: il::Val::DataSym("hostile::window") }, |
|
| 152 | + | store64(il::Val::Imm(0x80000000), 0, 0), done(), |
|
| 153 | + | ], false, &["hostile", ENTRY, "hostile::window"]); |
|
| 154 | + | // The address of a frame object is stored into the memory behind a Page. |
|
| 155 | + | emit(directory, "leak", none, &[], &[ |
|
| 156 | + | reserve(0, PLAIN), reserve(1, SLICE), |
|
| 157 | + | il::Instr::PageSlice { |
|
| 158 | + | err: il::Reg { n: 2 }, slot: il::Reg { n: 1 }, handle: il::Val::Imm(1), write: true, |
|
| 159 | + | layout: PLAIN, |
|
| 160 | + | }, |
|
| 161 | + | il::Instr::SliceElem { dst: il::Reg { n: 3 }, slice: il::Reg { n: 1 }, |
|
| 162 | + | index: il::Val::Imm(0), layout: PLAIN }, |
|
| 163 | + | il::Instr::StorePtr { src: reg(0), dst: il::Reg { n: 3 }, offset: 0 }, done(), |
|
| 164 | + | ], false, plain); |
|
| 165 | + | // The package refers to code that it does not define. |
|
| 166 | + | emit(directory, "extern", none, &[], &[done()], true, &["hostile", ENTRY, "hostile::escape"]); |
|
| 167 | + | // The package needs another package. |
|
| 168 | + | emit(directory, "dependent", &["support"], &[], &[done()], false, &["hostile", ENTRY, |
|
| 169 | + | "support"]); |
|
| 170 | + | // The kernel would write a call result over a pointer slot. |
|
| 171 | + | emit(directory, "buffer", none, &[], &[ |
|
| 172 | + | reserve(0, CELL), |
|
| 173 | + | il::Instr::Ecall { |
|
| 174 | + | dst: il::Reg { n: 1 }, num: il::Val::Imm(44), a0: il::Val::Imm(1), a1: il::Val::Imm(0), |
|
| 175 | + | a2: il::Val::Imm(0), a3: il::Val::Imm(0), buf: il::EcallBuf { ptr: il::Reg { n: 0 }, |
|
| 176 | + | size: 8 }, |
|
| 177 | + | }, |
|
| 178 | + | done(), |
|
| 179 | + | ], false, plain); |
|
| 180 | + | return 0; |
|
| 181 | + | } |
test/loader/kernel/dispatchcheck.rad
added
+113 -0
| 1 | + | //! Execute runtime-loaded native code after releasing its compiler workspace. |
|
| 2 | + | export mod inputs; |
|
| 3 | + | ||
| 4 | + | use std::mem; |
|
| 5 | + | use kernel::fixture; |
|
| 6 | + | use kernel::limits; |
|
| 7 | + | use kernel::state; |
|
| 8 | + | use kernel::abi; |
|
| 9 | + | use kernel::capability; |
|
| 10 | + | use kernel::pages; |
|
| 11 | + | use kernel::registry; |
|
| 12 | + | use kernel::loader; |
|
| 13 | + | use kernel::instances; |
|
| 14 | + | use kernel::domains; |
|
| 15 | + | use kernel::frames; |
|
| 16 | + | ||
| 17 | + | /// Capability storage for the loading domain. |
|
| 18 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 19 | + | ||
| 20 | + | /// View a validated source Page as mapped bytes. |
|
| 21 | + | fn memory(address: u64) -> *mut u8; |
|
| 22 | + | ||
| 23 | + | /// Call a published entry with one instance's package-state table. |
|
| 24 | + | fn invoke(address: u64, table: u64) -> i64; |
|
| 25 | + | ||
| 26 | + | /// Count the frames that are free. |
|
| 27 | + | unsafe fn freeFrames() -> u32 { |
|
| 28 | + | let mut count: u32 = 0; |
|
| 29 | + | for i in 0..state::SHARED.pages.backings.pool.count { |
|
| 30 | + | if frames::available(&state::SHARED.pages.backings.pool, i) { |
|
| 31 | + | set count += 1; |
|
| 32 | + | } |
|
| 33 | + | } |
|
| 34 | + | return count; |
|
| 35 | + | } |
|
| 36 | + | ||
| 37 | + | /// Copy a package into the source Page and load it, expecting the verifier to |
|
| 38 | + | /// reject it. The load leaves no frames, handles or loader ownership behind. |
|
| 39 | + | unsafe fn rejected(authority: abi::Handle, source: abi::Handle, bytes: &[u8]) { |
|
| 40 | + | let cap = try! capability::lookup(&TABLE, source, abi::Kind::Page, |
|
| 41 | + | abi::Rights(abi::WRITE)); |
|
| 42 | + | let page = try! pages::get(&state::SHARED.pages, cap.object); |
|
| 43 | + | let window = @sliceOf(memory(page.base), limits::FRAME_SIZE); |
|
| 44 | + | let length = try! mem::copy(&mut window[..], bytes); |
|
| 45 | + | let before = freeFrames(); |
|
| 46 | + | let mut failed = false; |
|
| 47 | + | try loader::load(&mut state::SHARED.loader, &mut state::SHARED.pages, |
|
| 48 | + | &mut state::SHARED.registry, &mut TABLE, loader::Request { authority, |
|
| 49 | + | source, offset: 0, length: length as u64 }) |
|
| 50 | + | catch error { |
|
| 51 | + | assert error == abi::Error::VerifyFailed; |
|
| 52 | + | set failed = true; |
|
| 53 | + | }; |
|
| 54 | + | assert failed and state::SHARED.loader.owner == nil; |
|
| 55 | + | assert freeFrames() == before; |
|
| 56 | + | } |
|
| 57 | + | ||
| 58 | + | /// Load the hosted comparison package and execute its retained native |
|
| 59 | + | /// instructions. |
|
| 60 | + | export unsafe fn run() { |
|
| 61 | + | let authority = fixture::bootstrap(&mut TABLE); |
|
| 62 | + | let source = try! pages::allocate(&mut state::SHARED.pages, &mut TABLE, |
|
| 63 | + | authority, 1); |
|
| 64 | + | let cap = try! capability::lookup(&TABLE, source, abi::Kind::Page, |
|
| 65 | + | abi::Rights(abi::READ)); |
|
| 66 | + | let page = try! pages::get(&state::SHARED.pages, cap.object); |
|
| 67 | + | let bytes = @sliceOf(memory(page.base), limits::FRAME_SIZE); |
|
| 68 | + | let length = try! mem::copy(&mut bytes[..], &inputs::INPUT[..]); |
|
| 69 | + | let handle = try! loader::load(&mut state::SHARED.loader, |
|
| 70 | + | &mut state::SHARED.pages, &mut state::SHARED.registry, &mut TABLE, |
|
| 71 | + | loader::Request { authority, source, offset: 0, length: length as u64 |
|
| 72 | + | }); |
|
| 73 | + | assert state::SHARED.loader.owner == nil; |
|
| 74 | + | let object = try! registry::image(&state::SHARED.registry, &TABLE, handle, |
|
| 75 | + | abi::Rights(abi::EXECUTE)); |
|
| 76 | + | let package = try! registry::get(&state::SHARED.registry, object); |
|
| 77 | + | let entry = package.entry else panic "run: loaded entry"; |
|
| 78 | + | for i in 0..length { |
|
| 79 | + | set bytes[i] = 0; |
|
| 80 | + | } |
|
| 81 | + | assert package.relocations.len > 0; |
|
| 82 | + | let firstHandle = try! domains::create(&mut state::SHARED.domains, |
|
| 83 | + | &mut state::SHARED.pages.backings, &state::SHARED.registry, &mut TABLE, |
|
| 84 | + | authority, handle); |
|
| 85 | + | let secondHandle = try! domains::create(&mut state::SHARED.domains, |
|
| 86 | + | &mut state::SHARED.pages.backings, &state::SHARED.registry, &mut TABLE, |
|
| 87 | + | authority, handle); |
|
| 88 | + | let firstEntry = try! capability::get(&TABLE, firstHandle); |
|
| 89 | + | let secondEntry = try! capability::get(&TABLE, secondHandle); |
|
| 90 | + | let first = try! domains::get(&state::SHARED.domains, firstEntry.object); |
|
| 91 | + | let second = try! domains::get(&state::SHARED.domains, secondEntry.object); |
|
| 92 | + | assert first.state == domains::Lifecycle::Pending |
|
| 93 | + | and second.state == domains::Lifecycle::Pending; |
|
| 94 | + | assert state::SHARED.domains.contexts[first.initial.index].frame.pc == |
|
| 95 | + | entry; |
|
| 96 | + | assert state::SHARED.domains.contexts[second.initial.index].frame.pc == |
|
| 97 | + | entry; |
|
| 98 | + | assert first.memory.ring.mask == 255 and second.memory.ring.mask == 255; |
|
| 99 | + | let tables = &state::SHARED.domains.tables; |
|
| 100 | + | let firstTable = instances::address(&tables[firstEntry.object.index]); |
|
| 101 | + | let secondTable = instances::address(&tables[secondEntry.object.index]); |
|
| 102 | + | assert invoke(entry, firstTable) == 43; |
|
| 103 | + | assert invoke(entry, firstTable) == 85; |
|
| 104 | + | assert invoke(entry, secondTable) == 43; |
|
| 105 | + | // Packages that break one rule of the verifier each: none is compiled or |
|
| 106 | + | // published. |
|
| 107 | + | rejected(authority, source, &inputs::HOSTILE_FORGED[..]); |
|
| 108 | + | rejected(authority, source, &inputs::HOSTILE_OVERWRITE[..]); |
|
| 109 | + | rejected(authority, source, &inputs::HOSTILE_LEAK[..]); |
|
| 110 | + | rejected(authority, source, &inputs::HOSTILE_EXTERN[..]); |
|
| 111 | + | rejected(authority, source, &inputs::HOSTILE_DEPENDENT[..]); |
|
| 112 | + | rejected(authority, source, &inputs::HOSTILE_BUFFER[..]); |
|
| 113 | + | } |
test/loader/loaded.rad
added
+17 -0
| 1 | + | //! A package executed by both hosted compilation and the kernel loader. |
|
| 2 | + | ||
| 3 | + | /// Counter owned by each package instance. |
|
| 4 | + | unsafe static counter: i64 = 1; |
|
| 5 | + | /// Read-only table of slices into this instance's initialized data, which the loader relocates. |
|
| 6 | + | constant FACTORS: [*[u8]; 2] = ["6", "7"]; |
|
| 7 | + | ||
| 8 | + | /// Compute a result through a package-local call. |
|
| 9 | + | unsafe fn calculate(value: i64) -> i64 { |
|
| 10 | + | set counter += value * (FACTORS[0][0] as i64 - 48); |
|
| 11 | + | return counter; |
|
| 12 | + | } |
|
| 13 | + | ||
| 14 | + | /// Return the result checked by both execution paths. |
|
| 15 | + | @default unsafe fn main() -> i32 { |
|
| 16 | + | return calculate(7) as i32; |
|
| 17 | + | } |
test/loader/machine.ras
added
+30 -0
| 1 | + | //! Run the loader fixture after all harts complete production initialization. |
|
| 2 | + | .text; |
|
| 3 | + | call @kernel::boot::initialize; |
|
| 4 | + | beqz %a0 @wait; |
|
| 5 | + | call @kernel::dispatchcheck::run; |
|
| 6 | + | li %t0 0x10001000; |
|
| 7 | + | li %t1 0x5555; |
|
| 8 | + | sw %t1 0(%t0); |
|
| 9 | + | @wait |
|
| 10 | + | wfi; |
|
| 11 | + | j @wait; |
|
| 12 | + | ||
| 13 | + | .export @kernel::dispatchcheck::memory; |
|
| 14 | + | .export @kernel::dispatchcheck::invoke; |
|
| 15 | + | ||
| 16 | + | // The caller supplies an address within its validated source Page. |
|
| 17 | + | @kernel::dispatchcheck::memory |
|
| 18 | + | ret; |
|
| 19 | + | ||
| 20 | + | // Preserve kernel state while calling one private instance of shared code. |
|
| 21 | + | @kernel::dispatchcheck::invoke |
|
| 22 | + | addi %sp %sp -16; |
|
| 23 | + | sd %ra 8(%sp); |
|
| 24 | + | sd %gp 0(%sp); |
|
| 25 | + | mv %gp %a1; |
|
| 26 | + | jalr %ra %a0 0; |
|
| 27 | + | ld %gp 0(%sp); |
|
| 28 | + | ld %ra 8(%sp); |
|
| 29 | + | addi %sp %sp 16; |
|
| 30 | + | ret; |
test/loader/run
added
+31 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Build a kernel fixture and call code produced by its runtime loader. |
|
| 3 | + | set -eu |
|
| 4 | + | emulator=${RAD_EMULATOR:-emulator} |
|
| 5 | + | work=$(mktemp -d) |
|
| 6 | + | trap 'rm -rf "$work"' EXIT HUP INT TERM |
|
| 7 | + | scripts/hosted bin/radiance.rv64.dev -pkg loaded -mod test/loader/loaded.rad -entry loaded -ril "$work" |
|
| 8 | + | scripts/hosted bin/radiance.rv64.dev -pkg loaded -mod test/loader/loaded.rad -entry loaded -o "$work/hosted.rv64" |
|
| 9 | + | status=0 |
|
| 10 | + | "$emulator" -run "$work/hosted.rv64" || status=$? |
|
| 11 | + | test "$status" -eq 43 |
|
| 12 | + | printf '//! Binary packages used by the loader fixture.\n' > "$work/inputs.rad" |
|
| 13 | + | sh test/kernel/embed INPUT "$work/loaded.ril" >> "$work/inputs.rad" |
|
| 14 | + | # Hostile packages, generated as typed IL because the language cannot express them. |
|
| 15 | + | mkdir "$work/hostile" |
|
| 16 | + | "$emulator" -run test/loader/hostile.rv64 -- "$work/hostile" |
|
| 17 | + | for name in forged overwrite leak extern dependent buffer; do |
|
| 18 | + | sh test/kernel/embed "HOSTILE_$(echo "$name" | tr a-z A-Z)" \ |
|
| 19 | + | "$work/hostile/$name.ril" >> "$work/inputs.rad" |
|
| 20 | + | done |
|
| 21 | + | sh test/kernel/build loader "$work" "$work/loader.rv64" |
|
| 22 | + | "$emulator" -machine -memory-size=262144 -max-steps=200000000 -run "$work/loader.rv64" |
|
| 23 | + | # The image builder verifies user packages as the loader does, and writes no image for a hostile one. |
|
| 24 | + | status=0 |
|
| 25 | + | scripts/hosted bin/kernel.build.rv64 \ |
|
| 26 | + | -- "$work/std.ril" "$work/kernel.ril" "$work/machine.ras" "$work/rejected.rv64" "$work/hostile/forged.ril" \ |
|
| 27 | + | > "$work/rejected.log" 2>&1 || status=$? |
|
| 28 | + | test "$status" -eq 1 |
|
| 29 | + | grep -q 'boot package fails verification: Type' "$work/rejected.log" |
|
| 30 | + | test ! -e "$work/rejected.rv64" |
|
| 31 | + | printf 'runtime loader: hosted and loaded entries agree, and hostile packages are rejected\n' |
test/mmio/cases.rad
added
+30 -0
| 1 | + | //! Device-access scenarios shared with the kernel checker. |
|
| 2 | + | ||
| 3 | + | /// Register operation and capability configuration selected by the parent. |
|
| 4 | + | export union Scenario: Copy { |
|
| 5 | + | /// Read and write all supported register widths. |
|
| 6 | + | Widths = 0, |
|
| 7 | + | /// Access through the absent handle. |
|
| 8 | + | MissingHandle = 1, |
|
| 9 | + | /// Write through read-only authority. |
|
| 10 | + | ReadOnly = 2, |
|
| 11 | + | /// Read through write-only authority. |
|
| 12 | + | WriteOnly = 3, |
|
| 13 | + | /// Write at an address that is not naturally aligned. |
|
| 14 | + | Misaligned = 4, |
|
| 15 | + | /// Read beyond the device region. |
|
| 16 | + | OutOfBounds = 5, |
|
| 17 | + | /// Access through a dropped capability. |
|
| 18 | + | DroppedHandle = 6, |
|
| 19 | + | /// Use a page capability as a device. |
|
| 20 | + | WrongKind = 7, |
|
| 21 | + | /// Overflow the device address calculation. |
|
| 22 | + | AddressOverflow = 8, |
|
| 23 | + | } |
|
| 24 | + | ||
| 25 | + | /// Status returned after all valid register accesses pass. |
|
| 26 | + | export constant EXIT_STATUS: u32 = 61; |
|
| 27 | + | /// Emulator success value used to expose an unauthorized device write. |
|
| 28 | + | export constant FINISH_VALUE: u32 = 0x5555; |
|
| 29 | + | /// Byte offset just beyond the synthetic device page. |
|
| 30 | + | export constant REGION_SIZE: u64 = 4096; |
test/mmio/kernel/dispatchcheck.rad
added
+162 -0
| 1 | + | //! Native fixed-width MMIO and authority rejection before register access. |
|
| 2 | + | export mod inputs; |
|
| 3 | + | export mod cases; |
|
| 4 | + | use kernel::fixture; |
|
| 5 | + | use kernel::state; |
|
| 6 | + | use kernel::abi; |
|
| 7 | + | use kernel::pages; |
|
| 8 | + | use kernel::capability; |
|
| 9 | + | use kernel::domains; |
|
| 10 | + | use kernel::dispatch; |
|
| 11 | + | use kernel::boot; |
|
| 12 | + | use kernel::events; |
|
| 13 | + | use kernel::sync; |
|
| 14 | + | use kernel::devices; |
|
| 15 | + | use kernel::platform; |
|
| 16 | + | ||
| 17 | + | /// Number of child scenarios, in CPU-window order. |
|
| 18 | + | constant COUNT: u32 = 9; |
|
| 19 | + | /// Delay before the first child window, in machine timer ticks. |
|
| 20 | + | constant START_DELAY: u64 = 1000000; |
|
| 21 | + | /// Duration of each child and checker window, in machine timer ticks. |
|
| 22 | + | constant WINDOW_TICKS: u64 = 2000000; |
|
| 23 | + | ||
| 24 | + | /// User actions and capability configurations in CPU-window order. |
|
| 25 | + | /// The expected lifecycle event is CHILD_EXIT with EXIT_STATUS for Widths, |
|
| 26 | + | /// and FAULT with FAULT_ABORT for each rejected access. |
|
| 27 | + | static SCENARIOS: [cases::Scenario; COUNT] = [ |
|
| 28 | + | cases::Scenario::Widths, |
|
| 29 | + | cases::Scenario::MissingHandle, |
|
| 30 | + | cases::Scenario::ReadOnly, |
|
| 31 | + | cases::Scenario::WriteOnly, |
|
| 32 | + | cases::Scenario::Misaligned, |
|
| 33 | + | cases::Scenario::OutOfBounds, |
|
| 34 | + | cases::Scenario::DroppedHandle, |
|
| 35 | + | cases::Scenario::WrongKind, |
|
| 36 | + | cases::Scenario::AddressOverflow, |
|
| 37 | + | ]; |
|
| 38 | + | ||
| 39 | + | // State retained across setup and verification is initialized before |
|
| 40 | + | // dispatch starts. The scheduled checker reads it after the user workload. |
|
| 41 | + | /// Bootstrap creation, allocation, and budget authority. |
|
| 42 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 43 | + | /// Child domain identities retained by terminal events. |
|
| 44 | + | unsafe static CHILDREN: [fixture::Actor; COUNT] = undefined; |
|
| 45 | + | /// Surviving event receiver and native completion checker. |
|
| 46 | + | unsafe static PARENT: fixture::Actor = undefined; |
|
| 47 | + | /// Register storage exposed only through a synthetic Device capability. |
|
| 48 | + | static REGISTERS: |
|
| 49 | + | [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(abi::READ | abi::WRITE | abi::GRANT | |
|
| 73 | + | abi::TRANSFER), |
|
| 74 | + | }; |
|
| 75 | + | set state::SHARED.devices.count += 1; |
|
| 76 | + | let mut clockDevice: u32 = 0; |
|
| 77 | + | let mut finish: u32 = 0; |
|
| 78 | + | for i in 0..scratch { |
|
| 79 | + | if state::SHARED.devices.regions[i].kind == platform::Kind::Clint { |
|
| 80 | + | set clockDevice = i; |
|
| 81 | + | } |
|
| 82 | + | if state::SHARED.devices.regions[i].kind == platform::Kind::Finish { |
|
| 83 | + | set finish = i; |
|
| 84 | + | } |
|
| 85 | + | } |
|
| 86 | + | for i in 0..COUNT { |
|
| 87 | + | let actor = fixture::create(&mut TABLE, authority, image); |
|
| 88 | + | let handle = actor.handle; |
|
| 89 | + | let object = actor.domain; |
|
| 90 | + | let mut child = try! domains::get(&state::SHARED.domains, object); |
|
| 91 | + | let scenario = SCENARIOS[i]; |
|
| 92 | + | set CHILDREN[i] = actor; |
|
| 93 | + | try! domains::reparent(&mut state::SHARED.domains, &TABLE, handle, |
|
| 94 | + | parentHandle); |
|
| 95 | + | let moved = fixture::stack(&mut TABLE, authority, |
|
| 96 | + | &mut child.memory.table); |
|
| 97 | + | let page = try! pages::allocate(&mut state::SHARED.pages, |
|
| 98 | + | &mut TABLE, authority, 1); |
|
| 99 | + | let granted = try! pages::grant(&mut state::SHARED.pages, &TABLE, |
|
| 100 | + | &mut child.memory.table, page, abi::READ as u64); |
|
| 101 | + | let storage = try! pages::get(&state::SHARED.pages, |
|
| 102 | + | (try! capability::get(&TABLE, page)).object); |
|
| 103 | + | let mut region = scratch; |
|
| 104 | + | if scenario == cases::Scenario::ReadOnly or |
|
| 105 | + | scenario == cases::Scenario::DroppedHandle |
|
| 106 | + | { |
|
| 107 | + | set region = finish; |
|
| 108 | + | } |
|
| 109 | + | let mut device = try! devices::install(&state::SHARED.devices, |
|
| 110 | + | &mut child.memory.table, region); |
|
| 111 | + | let slot = (try! abi::decode(device)).object.index; |
|
| 112 | + | if scenario == cases::Scenario::ReadOnly { |
|
| 113 | + | set child.memory.table.entries[slot].rights = |
|
| 114 | + | abi::Rights(abi::READ); |
|
| 115 | + | } |
|
| 116 | + | if scenario == cases::Scenario::WriteOnly { |
|
| 117 | + | set child.memory.table.entries[slot].rights = |
|
| 118 | + | abi::Rights(abi::WRITE); |
|
| 119 | + | } |
|
| 120 | + | if scenario == cases::Scenario::MissingHandle { |
|
| 121 | + | set device = abi::Handle(0); |
|
| 122 | + | } |
|
| 123 | + | if scenario == cases::Scenario::WrongKind { |
|
| 124 | + | set device = granted; |
|
| 125 | + | } |
|
| 126 | + | let timerHandle = try! devices::install(&state::SHARED.devices, |
|
| 127 | + | &mut child.memory.table, clockDevice); |
|
| 128 | + | let args = @sliceOf(memory(storage.base) as *mut u64, 3); |
|
| 129 | + | set args[0] = *device; |
|
| 130 | + | set args[1] = *timerHandle; |
|
| 131 | + | set args[2] = scenario as u64; |
|
| 132 | + | try! fixture::activate(&TABLE, handle, moved, granted, 24); |
|
| 133 | + | } |
|
| 134 | + | let timer = try! dispatch::timer(&boot::PLATFORM, 0); |
|
| 135 | + | let clock = dispatch::now(timer); |
|
| 136 | + | fixture::schedule(&mut TABLE, &CHILDREN[..], PARENT, |
|
| 137 | + | clock + START_DELAY, WINDOW_TICKS, clock); |
|
| 138 | + | } |
|
| 139 | + | ||
| 140 | + | /// Check each terminal kind, status, and generation while the parent continues |
|
| 141 | + | /// to run. |
|
| 142 | + | export unsafe fn verify() { |
|
| 143 | + | let guard = sync::enter(); |
|
| 144 | + | let mut parent = try! domains::get(&state::SHARED.domains, PARENT.domain); |
|
| 145 | + | for scenario, i in &SCENARIOS[..] { |
|
| 146 | + | let actor = CHILDREN[i]; |
|
| 147 | + | let event = try! events::pop(&mut parent.memory.ring) |
|
| 148 | + | else panic "verify: missing termination event"; |
|
| 149 | + | assert event.value == abi::id(actor.domain); |
|
| 150 | + | let valid = scenario == cases::Scenario::Widths; |
|
| 151 | + | let kind = events::CHILD_EXIT if valid else events::FAULT; |
|
| 152 | + | let code = cases::EXIT_STATUS if valid else abi::FAULT_ABORT; |
|
| 153 | + | assert event.kind == kind and event.code == code; |
|
| 154 | + | assert state::SHARED.domains.records[actor.domain.index].state == |
|
| 155 | + | domains::Lifecycle::Dead; |
|
| 156 | + | } |
|
| 157 | + | try! events::refresh(&mut state::SHARED.domains.events, PARENT.domain, |
|
| 158 | + | &parent.memory.ring); |
|
| 159 | + | assert try! events::pop(&mut parent.memory.ring) == nil; |
|
| 160 | + | sync::leave(guard); |
|
| 161 | + | finish(); |
|
| 162 | + | } |
test/mmio/machine.ras
added
+25 -0
| 1 | + | //! Boot one hart and check user termination through production dispatch. |
|
| 2 | + | .text; |
|
| 3 | + | call @kernel::boot::initialize; |
|
| 4 | + | call @kernel::dispatchcheck::setup; |
|
| 5 | + | call @kernel::boot::run; |
|
| 6 | + | .export @kernel::dispatchcheck::memory; |
|
| 7 | + | .export @kernel::dispatchcheck::kernelGp; |
|
| 8 | + | .export @kernel::dispatchcheck::completion; |
|
| 9 | + | .export @kernel::dispatchcheck::finish; |
|
| 10 | + | @kernel::dispatchcheck::memory |
|
| 11 | + | ret; |
|
| 12 | + | @kernel::dispatchcheck::kernelGp |
|
| 13 | + | mv %a0 %gp; |
|
| 14 | + | ret; |
|
| 15 | + | @kernel::dispatchcheck::completion |
|
| 16 | + | la %a0 @complete; |
|
| 17 | + | ret; |
|
| 18 | + | @complete |
|
| 19 | + | call @kernel::dispatchcheck::verify; |
|
| 20 | + | ebreak; |
|
| 21 | + | @kernel::dispatchcheck::finish |
|
| 22 | + | li %t0 0x10001000; |
|
| 23 | + | li %t1 0x5555; |
|
| 24 | + | sw %t1 0(%t0); |
|
| 25 | + | ebreak; |
test/mmio/spin.rad
added
+53 -0
| 1 | + | //! User-mode register widths and rejected MMIO authority. |
|
| 2 | + | export mod cases; |
|
| 3 | + | export mod abi; |
|
| 4 | + | export mod sys; |
|
| 5 | + | export mod mmio; |
|
| 6 | + | ||
| 7 | + | /// Execute each width, or require a kernel fault before an unauthorized access. |
|
| 8 | + | @default fn main(env: *abi::Env) { |
|
| 9 | + | // The arguments Page holds the device handle under test, the read-only |
|
| 10 | + | // machine timer handle, and the number of the test case, all installed by |
|
| 11 | + | // the test parent. |
|
| 12 | + | let words = try! sys::pageWords(abi::Handle(env.args)); |
|
| 13 | + | let device = abi::Handle(words[0]); |
|
| 14 | + | let clock = abi::Handle(words[1]); |
|
| 15 | + | let scenario = words[2]; |
|
| 16 | + | if scenario == cases::Scenario::Widths as u64 { |
|
| 17 | + | let before = mmio::read64(clock, 0); |
|
| 18 | + | mmio::write8(device, 0, 0x81); |
|
| 19 | + | mmio::write16(device, 2, 0x8123); |
|
| 20 | + | mmio::write32(device, 4, 0x81234567); |
|
| 21 | + | mmio::write64(device, 8, 0x8123456789abcdef); |
|
| 22 | + | assert mmio::read8(device, 0) == 0x81; |
|
| 23 | + | assert mmio::read16(device, 2) == 0x8123; |
|
| 24 | + | assert mmio::read32(device, 4) == 0x81234567; |
|
| 25 | + | assert mmio::read64(device, 8) == 0x8123456789abcdef; |
|
| 26 | + | assert mmio::read32(clock, 0) as u64 >= before; |
|
| 27 | + | sys::exit(cases::EXIT_STATUS); |
|
| 28 | + | } |
|
| 29 | + | if scenario == cases::Scenario::MissingHandle as u64 or |
|
| 30 | + | scenario == cases::Scenario::ReadOnly as u64 { |
|
| 31 | + | mmio::write32(device, 0, cases::FINISH_VALUE); |
|
| 32 | + | } |
|
| 33 | + | if scenario == cases::Scenario::WriteOnly as u64 { |
|
| 34 | + | let value = mmio::read8(device, 0); |
|
| 35 | + | } |
|
| 36 | + | if scenario == cases::Scenario::Misaligned as u64 { |
|
| 37 | + | mmio::write32(device, 1, 1); |
|
| 38 | + | } |
|
| 39 | + | if scenario == cases::Scenario::OutOfBounds as u64 { |
|
| 40 | + | let value = mmio::read8(device, cases::REGION_SIZE); |
|
| 41 | + | } |
|
| 42 | + | if scenario == cases::Scenario::DroppedHandle as u64 { |
|
| 43 | + | try! sys::capabilityDrop(device); |
|
| 44 | + | mmio::write32(device, 0, cases::FINISH_VALUE); |
|
| 45 | + | } |
|
| 46 | + | if scenario == cases::Scenario::WrongKind as u64 { |
|
| 47 | + | let value = mmio::read8(device, 0); |
|
| 48 | + | } |
|
| 49 | + | if scenario == cases::Scenario::AddressOverflow as u64 { |
|
| 50 | + | mmio::write64(device, 0xfffffffffffffff8, 1); |
|
| 51 | + | } |
|
| 52 | + | sys::exit(99); |
|
| 53 | + | } |
test/runtime/cases.rad
added
+25 -0
| 1 | + | //! Actor roles and progress fields shared by runtime loading checks. |
|
| 2 | + | ||
| 3 | + | // Roles selected by the parent in the startup page. |
|
| 4 | + | /// First compiler call, interrupted and cancelled by the controller. |
|
| 5 | + | export constant FIRST_LOADER: u32 = 0; |
|
| 6 | + | /// Compiler call that is interrupted, resumed, and then repeated. |
|
| 7 | + | export constant RETRY_LOADER: u32 = 1; |
|
| 8 | + | /// User context that runs while the retry loader owns compilation. |
|
| 9 | + | export constant WITNESS: u32 = 2; |
|
| 10 | + | /// Machine context that verifies cancellation and publication. |
|
| 11 | + | export constant CONTROLLER: u32 = 3; |
|
| 12 | + | ||
| 13 | + | /// Number of user and machine actors in the fixture. |
|
| 14 | + | export constant ACTORS: u32 = 4; |
|
| 15 | + | /// Progress value written immediately before a compiler call. |
|
| 16 | + | export constant STARTED: u64 = 1; |
|
| 17 | + | /// Progress value written after both compiler calls complete. |
|
| 18 | + | export constant COMPLETE: u64 = 2; |
|
| 19 | + | /// Shared progress word that holds the published package identity. |
|
| 20 | + | export constant IMAGE_ID: u32 = 3; |
|
| 21 | + | ||
| 22 | + | /// Progress prefix for an error from the first image load. |
|
| 23 | + | export constant LOAD_ERROR_BASE: u64 = 100; |
|
| 24 | + | /// Progress prefix for an error from the repeated image load. |
|
| 25 | + | export constant REPEAT_ERROR_BASE: u64 = 200; |
test/runtime/kernel/dispatchcheck.rad
added
+334 -0
| 1 | + | //! Runtime compiler ownership across scheduled windows and domain destruction. |
|
| 2 | + | export mod inputs; |
|
| 3 | + | export mod cases; |
|
| 4 | + | use kernel::fixture; |
|
| 5 | + | use kernel::state; |
|
| 6 | + | use kernel::abi; |
|
| 7 | + | use kernel::frames; |
|
| 8 | + | use kernel::slots; |
|
| 9 | + | use kernel::pages; |
|
| 10 | + | use kernel::capability; |
|
| 11 | + | use kernel::registry; |
|
| 12 | + | use kernel::loader; |
|
| 13 | + | use kernel::instances; |
|
| 14 | + | use kernel::domains; |
|
| 15 | + | use kernel::budgets; |
|
| 16 | + | use kernel::dispatch; |
|
| 17 | + | use kernel::sync; |
|
| 18 | + | use kernel::calls; |
|
| 19 | + | use kernel::lifecycle; |
|
| 20 | + | use kernel::remote; |
|
| 21 | + | ||
| 22 | + | /// Delay before the first user window, in timer ticks. |
|
| 23 | + | constant START_DELAY: u64 = 1000000; |
|
| 24 | + | /// End of the first interrupted load, relative to the first window start. |
|
| 25 | + | constant FIRST_WINDOW_END: u64 = 14000000; |
|
| 26 | + | /// End of the controller's cancellation window. |
|
| 27 | + | constant CANCEL_WINDOW_END: u64 = 50000000; |
|
| 28 | + | /// End of the retry loader's first window. |
|
| 29 | + | constant RETRY_WINDOW_END: u64 = 60000000; |
|
| 30 | + | /// End of the witness window. |
|
| 31 | + | constant WITNESS_WINDOW_END: u64 = 62000000; |
|
| 32 | + | /// End of the retry loader's final window. |
|
| 33 | + | constant FINAL_WINDOW_END: u64 = 300000000; |
|
| 34 | + | /// Duration of the final machine verification window. |
|
| 35 | + | constant CHECK_WINDOW: u64 = 10000000; |
|
| 36 | + | /// Maximum maintenance requests while reclaiming cancelled compiler storage. |
|
| 37 | + | constant RECLAIM_ATTEMPTS: u32 = 10000; |
|
| 38 | + | /// Maximum frames inspected in one metadata critical section. |
|
| 39 | + | constant FRAME_BATCH: u32 = 256; |
|
| 40 | + | ||
| 41 | + | /// Actor and end tick of one scheduled execution window. |
|
| 42 | + | record Window: Copy { |
|
| 43 | + | /// Actor that receives the window. |
|
| 44 | + | actor: u32, |
|
| 45 | + | /// Exclusive end relative to the schedule start. |
|
| 46 | + | end: u64, |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | // State retained across setup and verification is initialized before |
|
| 50 | + | // dispatch starts. The scheduled checker reads it after the user workload. |
|
| 51 | + | /// Bootstrap resource capabilities. |
|
| 52 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 53 | + | /// First loader, retry loader, witness, and controller domain handles, |
|
| 54 | + | /// generation-bearing domain identities, and initial context identities. |
|
| 55 | + | unsafe static ACTORS: [fixture::Actor; cases::ACTORS] = undefined; |
|
| 56 | + | /// Controller capability for the first loader. |
|
| 57 | + | unsafe static VICTIM: abi::Handle = undefined; |
|
| 58 | + | /// Controller capability for the binary input page. |
|
| 59 | + | unsafe static SOURCE: abi::Handle = undefined; |
|
| 60 | + | /// Physical address of shared user progress and package identity. |
|
| 61 | + | unsafe static PROGRESS: u64 = undefined; |
|
| 62 | + | /// Start of the controller's final execution window. |
|
| 63 | + | unsafe static FINAL: u64 = 0; |
|
| 64 | + | /// Write one diagnostic byte to the fixture UART. |
|
| 65 | + | fn put(byte: u8); |
|
| 66 | + | /// Write a fixed-width hexadecimal diagnostic value. |
|
| 67 | + | fn number(value: u64) { |
|
| 68 | + | let digits = "0123456789abcdef"; |
|
| 69 | + | for i in 0..16 { |
|
| 70 | + | put(digits[((value >> ((15 - i) as u64 * 4)) & 15) as u32]); |
|
| 71 | + | } |
|
| 72 | + | } |
|
| 73 | + | ||
| 74 | + | /// Map a validated physical page. |
|
| 75 | + | fn memory(address: u64) -> *mut u8; |
|
| 76 | + | /// Current kernel package-state base. |
|
| 77 | + | fn kernelGp() -> u64; |
|
| 78 | + | /// Native controller entry address. |
|
| 79 | + | fn completion() -> u64; |
|
| 80 | + | /// Execute published code with one private package-state table. |
|
| 81 | + | fn invoke(address: u64, table: u64) -> i64; |
|
| 82 | + | /// Signal native fixture completion. |
|
| 83 | + | fn finish(); |
|
| 84 | + | ||
| 85 | + | /// Install user actors and disjoint windows that interrupt two compiler calls. |
|
| 86 | + | export unsafe fn setup() { |
|
| 87 | + | let authority = fixture::bootstrap(&mut TABLE); |
|
| 88 | + | let image = fixture::load(&mut TABLE, authority, &inputs::INPUT[..]); |
|
| 89 | + | let payload = fixture::input(&mut TABLE, authority, &inputs::PAYLOAD[..]); |
|
| 90 | + | let progress = try! pages::allocate(&mut state::SHARED.pages, |
|
| 91 | + | &mut TABLE, authority, 1); |
|
| 92 | + | let shared = try! pages::get(&state::SHARED.pages, |
|
| 93 | + | (try! capability::get(&TABLE, progress)).object); |
|
| 94 | + | set PROGRESS = shared.base; |
|
| 95 | + | for i in 0..cases::ACTORS { |
|
| 96 | + | let actor = fixture::create(&mut TABLE, authority, image); |
|
| 97 | + | let handle = actor.handle; |
|
| 98 | + | let object = actor.domain; |
|
| 99 | + | let mut child = try! domains::get(&state::SHARED.domains, object); |
|
| 100 | + | let mut rights = abi::ALLOCATE; |
|
| 101 | + | if i == cases::CONTROLLER { |
|
| 102 | + | set rights |= abi::CREATE; |
|
| 103 | + | } |
|
| 104 | + | try! capability::install(&mut child.memory.table, |
|
| 105 | + | capability::Entry { |
|
| 106 | + | kind: abi::Kind::Domain, object, rights: abi::Rights(rights), |
|
| 107 | + | }); |
|
| 108 | + | set ACTORS[i] = actor; |
|
| 109 | + | let readable = try! pages::grant(&mut state::SHARED.pages, &TABLE, |
|
| 110 | + | &mut child.memory.table, payload, abi::READ as u64); |
|
| 111 | + | let writable = try! pages::grant(&mut state::SHARED.pages, &TABLE, |
|
| 112 | + | &mut child.memory.table, progress, (abi::READ | abi::WRITE) as u64); |
|
| 113 | + | if i == cases::CONTROLLER { |
|
| 114 | + | set SOURCE = readable; |
|
| 115 | + | set VICTIM = try! capability::install( |
|
| 116 | + | &mut child.memory.table, capability::Entry { |
|
| 117 | + | kind: abi::Kind::Domain, |
|
| 118 | + | object: ACTORS[cases::FIRST_LOADER].domain, |
|
| 119 | + | rights: abi::Rights(abi::DESTROY), |
|
| 120 | + | }); |
|
| 121 | + | fixture::machine(object, completion(), kernelGp()); |
|
| 122 | + | continue; |
|
| 123 | + | } |
|
| 124 | + | let moved = fixture::stack(&mut TABLE, authority, |
|
| 125 | + | &mut child.memory.table); |
|
| 126 | + | let startup = try! pages::allocate(&mut state::SHARED.pages, |
|
| 127 | + | &mut TABLE, authority, 1); |
|
| 128 | + | let granted = try! pages::grant(&mut state::SHARED.pages, &TABLE, |
|
| 129 | + | &mut child.memory.table, startup, abi::READ as u64); |
|
| 130 | + | let storage = try! pages::get(&state::SHARED.pages, |
|
| 131 | + | (try! capability::get(&TABLE, startup)).object); |
|
| 132 | + | let args = @sliceOf(memory(storage.base) as *mut u64, 4); |
|
| 133 | + | set args[0] = *readable; |
|
| 134 | + | set args[1] = inputs::PAYLOAD.len as u64; |
|
| 135 | + | set args[2] = *writable; |
|
| 136 | + | set args[3] = i as u64; |
|
| 137 | + | try! fixture::activate(&TABLE, handle, moved, granted, 32); |
|
| 138 | + | } |
|
| 139 | + | for byte in "runtime actors ready\n" { |
|
| 140 | + | put(byte); |
|
| 141 | + | } |
|
| 142 | + | let now = dispatch::clock(); |
|
| 143 | + | let start = now + START_DELAY; |
|
| 144 | + | set FINAL = start + FINAL_WINDOW_END; |
|
| 145 | + | let mut remaining = try! budgets::seed(&mut state::SHARED.budgets, |
|
| 146 | + | &mut TABLE, 0, start, FINAL + CHECK_WINDOW); |
|
| 147 | + | let windows = [ |
|
| 148 | + | Window { actor: cases::FIRST_LOADER, end: FIRST_WINDOW_END }, |
|
| 149 | + | Window { actor: cases::CONTROLLER, end: CANCEL_WINDOW_END }, |
|
| 150 | + | Window { actor: cases::RETRY_LOADER, end: RETRY_WINDOW_END }, |
|
| 151 | + | Window { actor: cases::WITNESS, end: WITNESS_WINDOW_END }, |
|
| 152 | + | Window { actor: cases::RETRY_LOADER, end: FINAL_WINDOW_END }, |
|
| 153 | + | ]; |
|
| 154 | + | for window in &windows[..] { |
|
| 155 | + | let next = try! budgets::split(&mut state::SHARED.budgets, |
|
| 156 | + | &mut TABLE, remaining, start + window.end, now); |
|
| 157 | + | let actor = window.actor; |
|
| 158 | + | let bound = try! calls::rebind(&mut state::SHARED, &mut TABLE, |
|
| 159 | + | budgets::Binding { budget: remaining, |
|
| 160 | + | domain: ACTORS[actor].handle, |
|
| 161 | + | context: ACTORS[actor].context }, now); |
|
| 162 | + | set remaining = next; |
|
| 163 | + | } |
|
| 164 | + | let bound = try! calls::rebind(&mut state::SHARED, &mut TABLE, |
|
| 165 | + | budgets::Binding { budget: remaining, |
|
| 166 | + | domain: ACTORS[cases::CONTROLLER].handle, |
|
| 167 | + | context: ACTORS[cases::CONTROLLER].context }, now); |
|
| 168 | + | } |
|
| 169 | + | ||
| 170 | + | /// Verify cancellation, competing admission, progress, and resident code reuse. |
|
| 171 | + | /// This entry runs only in the controller's reserved machine windows. |
|
| 172 | + | export unsafe fn verify() { |
|
| 173 | + | let loading = checkInterruptedLoad(); |
|
| 174 | + | checkCompetingLoad(); |
|
| 175 | + | cancelLoad(&loading); |
|
| 176 | + | while dispatch::clock() < FINAL { |
|
| 177 | + | } |
|
| 178 | + | checkPublishedLoad(); |
|
| 179 | + | finish(); |
|
| 180 | + | } |
|
| 181 | + | ||
| 182 | + | /// Snapshot the stopped compiler continuation under the metadata lock. |
|
| 183 | + | unsafe fn checkInterruptedLoad() -> lifecycle::Loading { |
|
| 184 | + | let progress = @sliceOf(memory(PROGRESS) as *mut u64, cases::ACTORS); |
|
| 185 | + | let guard = sync::enter(); |
|
| 186 | + | let first = ACTORS[cases::FIRST_LOADER].context; |
|
| 187 | + | for byte in "runtime first progress and owner: " { |
|
| 188 | + | put(byte); |
|
| 189 | + | } |
|
| 190 | + | number(progress[cases::FIRST_LOADER]); |
|
| 191 | + | put(32); |
|
| 192 | + | if let holder = state::SHARED.loader.owner { |
|
| 193 | + | number(abi::id(holder)); |
|
| 194 | + | } |
|
| 195 | + | put(10); |
|
| 196 | + | assert progress[cases::FIRST_LOADER] == cases::STARTED and |
|
| 197 | + | state::SHARED.loader.owner == ACTORS[cases::FIRST_LOADER].domain; |
|
| 198 | + | assert state::SHARED.domains.contexts[first.index].reservation; |
|
| 199 | + | let mode = state::SHARED.domains.contexts[first.index].frame.status & |
|
| 200 | + | fixture::MACHINE_PRIVILEGE; |
|
| 201 | + | assert mode == fixture::MACHINE_PRIVILEGE; |
|
| 202 | + | let allocation = state::SHARED.lifecycle.records[first.index].allocation; |
|
| 203 | + | let mut loading: lifecycle::Loading = undefined; |
|
| 204 | + | match allocation { |
|
| 205 | + | case lifecycle::Allocation::Load(value) => { |
|
| 206 | + | set loading = value; |
|
| 207 | + | }, |
|
| 208 | + | else => panic "checkInterruptedLoad: loader reservation required", |
|
| 209 | + | } |
|
| 210 | + | let output = |
|
| 211 | + | loading.output else panic "checkInterruptedLoad: output required"; |
|
| 212 | + | sync::leave(guard); |
|
| 213 | + | return loading; |
|
| 214 | + | } |
|
| 215 | + | ||
| 216 | + | /// A separate controller call must fail while the stopped compiler owns |
|
| 217 | + | /// loading. |
|
| 218 | + | unsafe fn checkCompetingLoad() { |
|
| 219 | + | let mut busy = false; |
|
| 220 | + | try calls::synchronized(ACTORS[cases::CONTROLLER].domain, |
|
| 221 | + | abi::Operation::ImageLoad as u32, &[0, *SOURCE, 0, |
|
| 222 | + | inputs::PAYLOAD.len as u64], dispatch::clock()) catch error { |
|
| 223 | + | assert error == abi::Error::Busy; |
|
| 224 | + | set busy = true; |
|
| 225 | + | }; |
|
| 226 | + | assert busy; |
|
| 227 | + | for byte in "runtime competing load rejected\n" { |
|
| 228 | + | put(byte); |
|
| 229 | + | } |
|
| 230 | + | } |
|
| 231 | + | ||
| 232 | + | /// Destroy the stopped loader and verify unpublished output and frame recovery. |
|
| 233 | + | /// Each request and inspection uses its own bounded metadata critical section. |
|
| 234 | + | unsafe fn cancelLoad(loading: &lifecycle::Loading) { |
|
| 235 | + | let first = ACTORS[cases::FIRST_LOADER].context; |
|
| 236 | + | let output = loading.output else panic "cancelLoad: output required"; |
|
| 237 | + | let destroyed = try! calls::synchronized(ACTORS[cases::CONTROLLER].domain, |
|
| 238 | + | abi::Operation::DomainDestroy as u32, &[*VICTIM, 0, 0, 0], |
|
| 239 | + | dispatch::clock()); |
|
| 240 | + | assert destroyed == 0; |
|
| 241 | + | let checking = sync::enter(); |
|
| 242 | + | let request = try! dispatch::request(&mut state::SHARED.remote, 0, |
|
| 243 | + | remote::Action::Reschedule); |
|
| 244 | + | sync::leave(checking); |
|
| 245 | + | let result = try! dispatch::awaitRequest(0, request); |
|
| 246 | + | assert result == 0; |
|
| 247 | + | let cancelled = sync::enter(); |
|
| 248 | + | assert state::SHARED.loader.owner == nil; |
|
| 249 | + | let active = state::SHARED.lifecycle.active[ |
|
| 250 | + | first.index / fixture::CONTEXT_BITS]; |
|
| 251 | + | let mask = (1 as u32) << (first.index % fixture::CONTEXT_BITS); |
|
| 252 | + | assert (active & mask) == 0; |
|
| 253 | + | assert state::SHARED.registry.slots[output.object.index].state == |
|
| 254 | + | slots::State::Free; |
|
| 255 | + | sync::leave(cancelled); |
|
| 256 | + | let mut reclaimed = false; |
|
| 257 | + | for attempt in 0..RECLAIM_ATTEMPTS { |
|
| 258 | + | let guard = sync::enter(); |
|
| 259 | + | set reclaimed = state::SHARED.pages.backings.pool.retiredCount == 0; |
|
| 260 | + | if reclaimed { |
|
| 261 | + | sync::leave(guard); |
|
| 262 | + | break; |
|
| 263 | + | } |
|
| 264 | + | let request = try! dispatch::request(&mut state::SHARED.remote, 0, |
|
| 265 | + | remote::Action::Reschedule); |
|
| 266 | + | sync::leave(guard); |
|
| 267 | + | let result = try! dispatch::awaitRequest(0, request); |
|
| 268 | + | assert result == 0; |
|
| 269 | + | } |
|
| 270 | + | assert reclaimed; |
|
| 271 | + | for run in [loader::workspace(&loading.input), output.code, output.metadata] |
|
| 272 | + | { |
|
| 273 | + | let mut offset: u32 = 0; |
|
| 274 | + | while offset < run.count { |
|
| 275 | + | let mut count = run.count - offset; |
|
| 276 | + | if count > FRAME_BATCH { |
|
| 277 | + | set count = FRAME_BATCH; |
|
| 278 | + | } |
|
| 279 | + | let recovered = sync::enter(); |
|
| 280 | + | for i in run.first + offset..run.first + offset + count { |
|
| 281 | + | assert frames::available(&state::SHARED.pages.backings.pool, i); |
|
| 282 | + | } |
|
| 283 | + | sync::leave(recovered); |
|
| 284 | + | set offset += count; |
|
| 285 | + | } |
|
| 286 | + | } |
|
| 287 | + | for byte in "runtime cancellation passed\n" { |
|
| 288 | + | put(byte); |
|
| 289 | + | } |
|
| 290 | + | } |
|
| 291 | + | ||
| 292 | + | /// Verify retry progress, resident identity, and execution in a fresh instance. |
|
| 293 | + | unsafe fn checkPublishedLoad() { |
|
| 294 | + | let progress = @sliceOf(memory(PROGRESS) as *mut u64, cases::ACTORS); |
|
| 295 | + | let complete = sync::enter(); |
|
| 296 | + | for byte in "runtime final progress: " { |
|
| 297 | + | put(byte); |
|
| 298 | + | } |
|
| 299 | + | for i in 0..cases::ACTORS { |
|
| 300 | + | number(progress[i]); |
|
| 301 | + | put(32); |
|
| 302 | + | } |
|
| 303 | + | put(10); |
|
| 304 | + | assert progress[cases::FIRST_LOADER] == cases::STARTED; |
|
| 305 | + | assert progress[cases::RETRY_LOADER] == cases::COMPLETE; |
|
| 306 | + | assert progress[cases::WITNESS] == cases::STARTED; |
|
| 307 | + | assert state::SHARED.loader.owner == nil; |
|
| 308 | + | let package = try! registry::get(&state::SHARED.registry, |
|
| 309 | + | abi::reference(progress[cases::IMAGE_ID])); |
|
| 310 | + | let entry = package.entry |
|
| 311 | + | else panic "checkPublishedLoad: published entry required"; |
|
| 312 | + | let mut controller = try! domains::get(&state::SHARED.domains, |
|
| 313 | + | ACTORS[cases::CONTROLLER].domain); |
|
| 314 | + | let image = try! registry::install(&state::SHARED.registry, |
|
| 315 | + | &mut controller.memory.table, |
|
| 316 | + | abi::reference(progress[cases::IMAGE_ID])); |
|
| 317 | + | sync::leave(complete); |
|
| 318 | + | let handle = |
|
| 319 | + | abi::Handle(try! calls::synchronized(ACTORS[cases::CONTROLLER].domain, |
|
| 320 | + | abi::Operation::DomainCreate as u32, &[0, *image, 0, 0], |
|
| 321 | + | dispatch::clock())); |
|
| 322 | + | let created = sync::enter(); |
|
| 323 | + | let object = (try! capability::get(&controller.memory.table, |
|
| 324 | + | handle)).object; |
|
| 325 | + | let domain = try! domains::get(&state::SHARED.domains, object); |
|
| 326 | + | sync::leave(created); |
|
| 327 | + | assert invoke(entry, |
|
| 328 | + | instances::address(&state::SHARED.domains.tables[object.index])) == 43; |
|
| 329 | + | assert invoke(entry, |
|
| 330 | + | instances::address(&state::SHARED.domains.tables[object.index])) == 85; |
|
| 331 | + | for byte in "runtime retry published executable code\n" { |
|
| 332 | + | put(byte); |
|
| 333 | + | } |
|
| 334 | + | } |
test/runtime/machine.ras
added
+44 -0
| 1 | + | //! Boot one hart and check user termination through production dispatch. |
|
| 2 | + | .text; |
|
| 3 | + | call @kernel::boot::initialize; |
|
| 4 | + | call @kernel::dispatchcheck::setup; |
|
| 5 | + | call @kernel::boot::run; |
|
| 6 | + | .export @kernel::dispatchcheck::memory; |
|
| 7 | + | .export @kernel::dispatchcheck::kernelGp; |
|
| 8 | + | .export @kernel::dispatchcheck::completion; |
|
| 9 | + | .export @kernel::dispatchcheck::finish; |
|
| 10 | + | @kernel::dispatchcheck::memory |
|
| 11 | + | ret; |
|
| 12 | + | @kernel::dispatchcheck::kernelGp |
|
| 13 | + | mv %a0 %gp; |
|
| 14 | + | ret; |
|
| 15 | + | @kernel::dispatchcheck::completion |
|
| 16 | + | la %a0 @complete; |
|
| 17 | + | ret; |
|
| 18 | + | @complete |
|
| 19 | + | call @kernel::dispatchcheck::verify; |
|
| 20 | + | ebreak; |
|
| 21 | + | @kernel::dispatchcheck::finish |
|
| 22 | + | li %t0 0x10001000; |
|
| 23 | + | li %t1 0x5555; |
|
| 24 | + | sw %t1 0(%t0); |
|
| 25 | + | ebreak; |
|
| 26 | + | ||
| 27 | + | .export @kernel::dispatchcheck::put; |
|
| 28 | + | @kernel::dispatchcheck::put |
|
| 29 | + | li %t0 0x10000000; |
|
| 30 | + | sb %a0 0(%t0); |
|
| 31 | + | ret; |
|
| 32 | + | ||
| 33 | + | .export @kernel::dispatchcheck::invoke; |
|
| 34 | + | // Preserve kernel state while calling one private instance of shared code. |
|
| 35 | + | @kernel::dispatchcheck::invoke |
|
| 36 | + | addi %sp %sp -16; |
|
| 37 | + | sd %ra 8(%sp); |
|
| 38 | + | sd %gp 0(%sp); |
|
| 39 | + | mv %gp %a1; |
|
| 40 | + | jalr %ra %a0 0; |
|
| 41 | + | ld %gp 0(%sp); |
|
| 42 | + | ld %ra 8(%sp); |
|
| 43 | + | addi %sp %sp 16; |
|
| 44 | + | ret; |
test/runtime/run
added
+24 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Compile, cancel, and retry a package through scheduled user calls. |
|
| 3 | + | set -eu |
|
| 4 | + | emulator=${RAD_EMULATOR:-emulator} |
|
| 5 | + | work=$(mktemp -d) |
|
| 6 | + | trap 'rm -rf "$work"' EXIT HUP INT TERM |
|
| 7 | + | scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" \ |
|
| 8 | + | -pkg spin -mod test/runtime/spin.rad \ |
|
| 9 | + | -mod cases=test/runtime/cases.rad \ |
|
| 10 | + | -mod abi=kernel/kernel/abi.rad -mod sys=kernel/kernel/sys.rad -entry spin -ril "$work" |
|
| 11 | + | cp test/loader/loaded.rad "$work/loaded.rad" |
|
| 12 | + | index=0 |
|
| 13 | + | while [ "$index" -lt 128 ]; do |
|
| 14 | + | printf '\n/// Compute a distinct exported value.\nexport fn value%s(input: u64) -> u64 { return input + %s; }\n' "$index" "$index" >> "$work/loaded.rad" |
|
| 15 | + | index=$((index + 1)) |
|
| 16 | + | done |
|
| 17 | + | scripts/hosted bin/radiance.rv64.dev \ |
|
| 18 | + | -pkg loaded -mod "$work/loaded.rad" -entry loaded -ril "$work" |
|
| 19 | + | printf '//! Binary inputs for scheduled loading.\n' > "$work/inputs.rad" |
|
| 20 | + | sh test/kernel/embed INPUT "$work/spin.ril" >> "$work/inputs.rad" |
|
| 21 | + | sh test/kernel/embed PAYLOAD "$work/loaded.ril" >> "$work/inputs.rad" |
|
| 22 | + | sh test/kernel/build runtime "$work" "$work/dispatch.rv64" |
|
| 23 | + | "$emulator" -machine -harts=1 -memory-size=262144 -max-steps=2000000000 -run "$work/dispatch.rv64" |
|
| 24 | + | printf 'runtime: scheduled cancellation, retry, and publication passed\n' |
test/runtime/spin.rad
added
+42 -0
| 1 | + | //! Scheduled runtime compilation and an independent window witness. |
|
| 2 | + | export mod cases; |
|
| 3 | + | export mod abi; |
|
| 4 | + | export mod sys; |
|
| 5 | + | ||
| 6 | + | /// Compile a package twice or record progress during another domain's load. |
|
| 7 | + | @default fn main(env: *abi::Env) { |
|
| 8 | + | // The arguments Page holds the binary package capability, its byte count, |
|
| 9 | + | // the shared progress Page capability, and the actor index selected by the |
|
| 10 | + | // parent. |
|
| 11 | + | let words = try! sys::pageWords(abi::Handle(env.args)); |
|
| 12 | + | let source = abi::Handle(words[0]); |
|
| 13 | + | let length = words[1]; |
|
| 14 | + | let progress = try! sys::pageWordsMut(abi::Handle(words[2])); |
|
| 15 | + | let actor = words[3] as u32; |
|
| 16 | + | if actor == cases::WITNESS { |
|
| 17 | + | assert progress[cases::RETRY_LOADER] == cases::STARTED; |
|
| 18 | + | let mut busy = false; |
|
| 19 | + | try sys::imageLoad(abi::Handle(0), source, 0, length) catch error { |
|
| 20 | + | assert error == abi::Error::Busy; |
|
| 21 | + | set busy = true; |
|
| 22 | + | }; |
|
| 23 | + | assert busy; |
|
| 24 | + | set progress[cases::WITNESS] = cases::STARTED; |
|
| 25 | + | return; |
|
| 26 | + | } |
|
| 27 | + | set progress[actor] = cases::STARTED; |
|
| 28 | + | let image = try sys::imageLoad(abi::Handle(0), source, 0, length) catch |
|
| 29 | + | error { |
|
| 30 | + | set progress[actor] = cases::LOAD_ERROR_BASE + error as u64; |
|
| 31 | + | return; |
|
| 32 | + | }; |
|
| 33 | + | let info = try! sys::queryImage(image); |
|
| 34 | + | let repeated = try sys::imageLoad(abi::Handle(0), source, 0, length) catch |
|
| 35 | + | error { |
|
| 36 | + | set progress[actor] = cases::REPEAT_ERROR_BASE + error as u64; |
|
| 37 | + | return; |
|
| 38 | + | }; |
|
| 39 | + | assert (try! sys::queryImage(repeated)).id == info.id; |
|
| 40 | + | set progress[cases::IMAGE_ID] = info.id; |
|
| 41 | + | set progress[actor] = cases::COMPLETE; |
|
| 42 | + | } |
test/scheduling/root.rad
added
+58 -0
| 1 | + | //! Root resource handoff for user-space scheduling tests. |
|
| 2 | + | export mod abi; |
|
| 3 | + | export mod sys; |
|
| 4 | + | ||
| 5 | + | /// Transfer root's resource bundle and parent duties before normal exit. |
|
| 6 | + | @default fn main(env: *abi::Env) -> ! { |
|
| 7 | + | assert env.argsSize == @sizeOf(abi::BootInfo) as u64; |
|
| 8 | + | let args = abi::Handle(env.args); |
|
| 9 | + | // The boot record is in the Page that the startup arguments name. Root updates it in |
|
| 10 | + | // place, and moves the Page to the scheduler as its arguments. |
|
| 11 | + | let records = try! sys::bootInfoMut(args); |
|
| 12 | + | assert records.len == 1; |
|
| 13 | + | let mut info = records[0]; |
|
| 14 | + | let self = abi::Handle(info.handles[0]); |
|
| 15 | + | let scheduler = try! sys::domainCreate(self, info.schedulerImage); |
|
| 16 | + | let context = try! sys::queryContext(scheduler, 0); |
|
| 17 | + | let authority = try! sys::capabilityGrant(scheduler, scheduler, abi::RIGHTS_MASK as u64); |
|
| 18 | + | let stack = try! sys::pageAllocate(self, 16); |
|
| 19 | + | let sharedStack = try! sys::capabilityTransfer(stack, scheduler, abi::RIGHTS_MASK as u64); |
|
| 20 | + | let clock = info.clock; |
|
| 21 | + | let shutdown = info.shutdown; |
|
| 22 | + | let budgets = 2 + info.devices as u32 + info.interrupts as u32; |
|
| 23 | + | for i in 2..budgets { |
|
| 24 | + | let old = abi::Handle(info.handles[i]); |
|
| 25 | + | let moved = try! sys::capabilityTransfer(old, scheduler, abi::RIGHTS_MASK as u64); |
|
| 26 | + | set info.handles[i] = *moved; |
|
| 27 | + | if old == clock { |
|
| 28 | + | set info.clock = moved; |
|
| 29 | + | } |
|
| 30 | + | if old == shutdown { |
|
| 31 | + | set info.shutdown = moved; |
|
| 32 | + | } |
|
| 33 | + | } |
|
| 34 | + | for i in budgets..info.count as u32 { |
|
| 35 | + | let budget = abi::Handle(info.handles[i]); |
|
| 36 | + | let window = try! sys::queryBudget(budget); |
|
| 37 | + | if window.hart == 0 { |
|
| 38 | + | let future = try! sys::budgetSplit(budget, window.start + info.timebase * 2); |
|
| 39 | + | set info.handles[i] = *try! sys::budgetBind(future, scheduler, context.context); |
|
| 40 | + | } else { |
|
| 41 | + | set info.handles[i] = *try! sys::capabilityTransfer(budget, scheduler, |
|
| 42 | + | abi::RIGHTS_MASK as u64); |
|
| 43 | + | } |
|
| 44 | + | } |
|
| 45 | + | set info.handles[0] = *authority; |
|
| 46 | + | set info.handles[1] = 0; |
|
| 47 | + | set info.rootImage = try! sys::capabilityTransfer(info.rootImage, scheduler, |
|
| 48 | + | abi::RIGHTS_MASK as u64); |
|
| 49 | + | set info.schedulerImage = try! sys::capabilityTransfer(info.schedulerImage, scheduler, |
|
| 50 | + | abi::RIGHTS_MASK as u64); |
|
| 51 | + | assert info.arguments == args; |
|
| 52 | + | let moved = try! sys::capabilityTransfer(args, scheduler, abi::RIGHTS_MASK as u64); |
|
| 53 | + | set info.arguments = moved; |
|
| 54 | + | try! sys::domainReparent(self, scheduler); |
|
| 55 | + | set records[0] = info; |
|
| 56 | + | try! sys::domainActivate(scheduler, sharedStack, moved, @sizeOf(abi::BootInfo) as u64); |
|
| 57 | + | sys::exit(0); |
|
| 58 | + | } |
test/scheduling/run
added
+22 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Execute root handoff, user-space yields, and window expiry in fixture packages. |
|
| 3 | + | set -eu |
|
| 4 | + | emulator=${RAD_EMULATOR:-emulator} |
|
| 5 | + | harts=${1:-1 2 8} |
|
| 6 | + | work=$(mktemp -d) |
|
| 7 | + | trap 'rm -rf "$work"' EXIT HUP INT TERM |
|
| 8 | + | for package in root scheduler; do |
|
| 9 | + | set -- -pkg "$package" -mod "test/scheduling/$package.rad" \ |
|
| 10 | + | -mod abi=kernel/kernel/abi.rad -mod sys=kernel/kernel/sys.rad |
|
| 11 | + | if [ "$package" = scheduler ]; then |
|
| 12 | + | set -- "$@" -mod mmio=kernel/mmio.rad |
|
| 13 | + | fi |
|
| 14 | + | scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" "$@" -entry "$package" -ril "$work" |
|
| 15 | + | done |
|
| 16 | + | cat kernel/kernel/*.ras > "$work/scheduling.ras" |
|
| 17 | + | scripts/hosted bin/kernel.build.rv64 -- bin/std.ril bin/kernel.ril \ |
|
| 18 | + | "$work/scheduling.ras" "$work/scheduling.rv64" "$work/root.ril" "$work/scheduler.ril" |
|
| 19 | + | for count in $harts; do |
|
| 20 | + | "$emulator" -machine -harts="$count" -memory-size=262144 -max-steps=2000000000 -run "$work/scheduling.rv64" |
|
| 21 | + | printf 'scheduling: %s-hart root handoff, four user-space yields, 513 retired windows, and checked shutdown passed\n' "$count" |
|
| 22 | + | done |
test/scheduling/scheduler.rad
added
+60 -0
| 1 | + | //! User-space scheduling and CPU-window retirement checks. |
|
| 2 | + | export mod abi; |
|
| 3 | + | export mod sys; |
|
| 4 | + | export mod mmio; |
|
| 5 | + | ||
| 6 | + | /// Completion value for a successful scheduling test. |
|
| 7 | + | constant FINISH_PASS: u32 = 0x5555; |
|
| 8 | + | ||
| 9 | + | /// Run a worker, or distribute four successive CPU windows from the boot bundle and |
|
| 10 | + | /// then let more windows expire than the kernel has budget objects. |
|
| 11 | + | @default fn main(env: *abi::Env) -> ! { |
|
| 12 | + | if env.argsSize == 0 { |
|
| 13 | + | sys::exit(7); |
|
| 14 | + | } |
|
| 15 | + | assert env.argsSize == @sizeOf(abi::BootInfo) as u64; |
|
| 16 | + | let records = try! sys::bootInfo(abi::Handle(env.args)); |
|
| 17 | + | assert records.len == 1; |
|
| 18 | + | let info = records[0]; |
|
| 19 | + | let self = abi::Handle(info.handles[0]); |
|
| 20 | + | let started = mmio::read64(info.clock, 0); |
|
| 21 | + | assert sys::queryDomain(self).state == 1; |
|
| 22 | + | // Boot authority has no end, so the scheduler can run for the life of the system. |
|
| 23 | + | let initial = try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT); |
|
| 24 | + | let boot = try! sys::queryBudget(abi::Handle(initial.budget)); |
|
| 25 | + | assert boot.end == 0xffffffffffffffff; |
|
| 26 | + | for turn in 0 .. 4 { |
|
| 27 | + | let current = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)); |
|
| 28 | + | let budget = abi::Handle(current.budget); |
|
| 29 | + | let window = try! sys::queryBudget(budget); |
|
| 30 | + | let later = try! sys::budgetSplit(budget, window.start + info.timebase); |
|
| 31 | + | let worker = try! sys::domainCreate(self, info.schedulerImage); |
|
| 32 | + | let stack = try! sys::pageAllocate(self, 16); |
|
| 33 | + | let transferred = try! sys::capabilityTransfer(stack, worker, abi::RIGHTS_MASK as u64); |
|
| 34 | + | // A worker gets no arguments and exits before reading its Env's argument fields. |
|
| 35 | + | try! sys::domainActivate(worker, transferred, abi::Handle(0), 0); |
|
| 36 | + | try! sys::yield(worker); |
|
| 37 | + | assert (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)).budget == *later; |
|
| 38 | + | } |
|
| 39 | + | // The kernel retires each window when it ends and leaves its handle stale, so |
|
| 40 | + | // more successive windows than its 512 budget objects run to completion. |
|
| 41 | + | for turn in 0 .. 513 { |
|
| 42 | + | let ending = abi::Handle((try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)).budget); |
|
| 43 | + | // One millisecond outlasts the split call, so the split point is still ahead. |
|
| 44 | + | let end = mmio::read64(info.clock, 0) + info.timebase / 1000; |
|
| 45 | + | let next = try! sys::budgetSplit(ending, end); |
|
| 46 | + | while (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)).budget <> *next {} |
|
| 47 | + | let mut stale = false; |
|
| 48 | + | try sys::queryBudget(ending) catch err { |
|
| 49 | + | assert err == abi::Error::BadHandle; |
|
| 50 | + | set stale = true; |
|
| 51 | + | }; |
|
| 52 | + | assert stale; |
|
| 53 | + | } |
|
| 54 | + | assert mmio::read64(info.clock, 0) > started; |
|
| 55 | + | if info.shutdown <> abi::Handle(0) { |
|
| 56 | + | mmio::write32(info.shutdown, 0, FINISH_PASS); |
|
| 57 | + | panic "main: shutdown returned"; |
|
| 58 | + | } |
|
| 59 | + | sys::exit(0); |
|
| 60 | + | } |
test/slots/abandon.rad
added
+11 -0
| 1 | + | //! Compile-failure fixture for an unconsumed reservation. |
|
| 2 | + | ||
| 3 | + | use kernel::slots; |
|
| 4 | + | ||
| 5 | + | /// A reservation must be consumed before its scope ends. |
|
| 6 | + | @default unsafe fn main() -> u32 { |
|
| 7 | + | let mut table: [slots::Slot; 1] = undefined; |
|
| 8 | + | slots::initialize(&mut table[..]); |
|
| 9 | + | let token = try! slots::reserve(&mut table[..]); |
|
| 10 | + | return 0; |
|
| 11 | + | } |
test/slots/duplicate.rad
added
+13 -0
| 1 | + | //! Compile-failure fixture for repeated reservation consumption. |
|
| 2 | + | ||
| 3 | + | use kernel::slots; |
|
| 4 | + | ||
| 5 | + | /// A reservation cannot be cancelled twice. |
|
| 6 | + | @default unsafe fn main() -> u32 { |
|
| 7 | + | let mut table: [slots::Slot; 1] = undefined; |
|
| 8 | + | slots::initialize(&mut table[..]); |
|
| 9 | + | let token = try! slots::reserve(&mut table[..]); |
|
| 10 | + | try! slots::cancel(&mut table[..], token); |
|
| 11 | + | try! slots::cancel(&mut table[..], token); |
|
| 12 | + | return 0; |
|
| 13 | + | } |
test/slots/kernel.rad
added
+4 -0
| 1 | + | //! Kernel modules used by reservation rejection fixtures. |
|
| 2 | + | export mod slots; |
|
| 3 | + | export mod abi; |
|
| 4 | + | export mod limits; |
test/slots/run
added
+23 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Check that reservation misuse fails during compilation. |
|
| 3 | + | set -eu |
|
| 4 | + | work=$(mktemp -d) |
|
| 5 | + | trap 'rm -rf "$work"' EXIT HUP INT TERM |
|
| 6 | + | for name in duplicate abandon; do |
|
| 7 | + | case "$name" in |
|
| 8 | + | duplicate) diagnostic='linear value used after consumption' ;; |
|
| 9 | + | abandon) diagnostic='linear value is not consumed' ;; |
|
| 10 | + | esac |
|
| 11 | + | status=0 |
|
| 12 | + | scripts/hosted bin/radiance.rv64.dev \ |
|
| 13 | + | -pkg kernel -mod test/slots/kernel.rad \ |
|
| 14 | + | -mod slots=kernel/kernel/slots.rad -mod abi=kernel/kernel/abi.rad \ |
|
| 15 | + | -mod limits=kernel/kernel/limits.rad \ |
|
| 16 | + | -pkg "$name" -mod "test/slots/$name.rad" -entry "$name" \ |
|
| 17 | + | -o "$work/test.rv64" > "$work/log" 2>&1 || status=$? |
|
| 18 | + | if [ "$status" -ne 1 ] || ! grep -q "$diagnostic" "$work/log"; then |
|
| 19 | + | cat "$work/log" >&2 |
|
| 20 | + | exit 1 |
|
| 21 | + | fi |
|
| 22 | + | printf 'reservation rejection: %s passed\n' "$name" |
|
| 23 | + | done |
test/smp/cases.rad
added
+40 -0
| 1 | + | //! Shared constants and progress stages for concurrent kernel checks. |
|
| 2 | + | ||
| 3 | + | /// Pages in the allocation interrupted by the cancellation checker. |
|
| 4 | + | export constant ALLOCATION_PAGES: u32 = 4096; |
|
| 5 | + | /// Completed allocation transactions per user context. |
|
| 6 | + | export constant TRANSACTIONS: u32 = 16; |
|
| 7 | + | /// Physical interrupt source injected by the machine runner. |
|
| 8 | + | export constant IRQ_SOURCE: u32 = 7; |
|
| 9 | + | /// Token delivered by the remote wakeup check. |
|
| 10 | + | export constant WAKE_TOKEN: u32 = 0x55; |
|
| 11 | + | /// Token delivered by the remote timeout check. |
|
| 12 | + | export constant TIMEOUT_TOKEN: u32 = 0x66; |
|
| 13 | + | /// Token delivered while a level interrupt remains asserted. |
|
| 14 | + | export constant STORM_TOKEN: u32 = 0x77; |
|
| 15 | + | ||
| 16 | + | /// Handshake between the idle user context and the machine checker. |
|
| 17 | + | export union WakeStage: Copy { |
|
| 18 | + | /// User is about to wait for an explicit wakeup. |
|
| 19 | + | BeforeWakeWait = 2, |
|
| 20 | + | /// User resumed after its explicit wakeup. |
|
| 21 | + | WakeResumed = 3, |
|
| 22 | + | /// Checker permits the user to start its timeout wait. |
|
| 23 | + | AllowTimeout = 4, |
|
| 24 | + | /// User is about to wait for the timeout. |
|
| 25 | + | BeforeTimeoutWait = 5, |
|
| 26 | + | /// User resumed after its timeout. |
|
| 27 | + | TimeoutResumed = 6, |
|
| 28 | + | /// Checker permits the user to wait for terminal events. |
|
| 29 | + | AllowTerminalWait = 7, |
|
| 30 | + | /// User is about to wait for terminal events. |
|
| 31 | + | BeforeTerminalWait = 8, |
|
| 32 | + | /// User resumed after a terminal event. |
|
| 33 | + | TerminalResumed = 9, |
|
| 34 | + | /// Checker permits the user to start its large allocation. |
|
| 35 | + | AllowAllocation = 10, |
|
| 36 | + | /// User is about to allocate pages. |
|
| 37 | + | BeforeAllocation = 11, |
|
| 38 | + | /// Allocation completed without interruption. |
|
| 39 | + | AllocationComplete = 12, |
|
| 40 | + | } |
test/smp/kernel/dispatchcheck.rad
added
+941 -0
| 1 | + | //! One domain executing separate user contexts on every online hart. |
|
| 2 | + | export mod inputs; |
|
| 3 | + | export mod cases; |
|
| 4 | + | export mod invariants; |
|
| 5 | + | use std::arch::rv64::shared; |
|
| 6 | + | use std::arch::rv64; |
|
| 7 | + | use kernel::fixture; |
|
| 8 | + | use kernel::limits; |
|
| 9 | + | use kernel::state; |
|
| 10 | + | use kernel::abi; |
|
| 11 | + | use kernel::frames; |
|
| 12 | + | use kernel::slots; |
|
| 13 | + | use kernel::pages; |
|
| 14 | + | use kernel::capability; |
|
| 15 | + | use kernel::registry; |
|
| 16 | + | use kernel::instances; |
|
| 17 | + | use kernel::domains; |
|
| 18 | + | use kernel::budgets; |
|
| 19 | + | use kernel::dispatch; |
|
| 20 | + | use kernel::boot; |
|
| 21 | + | use kernel::sync; |
|
| 22 | + | use kernel::remote; |
|
| 23 | + | use kernel::calls; |
|
| 24 | + | use kernel::events; |
|
| 25 | + | use kernel::timers; |
|
| 26 | + | use kernel::interrupts; |
|
| 27 | + | use kernel::plic; |
|
| 28 | + | use kernel::registers; |
|
| 29 | + | use kernel::lifecycle; |
|
| 30 | + | ||
| 31 | + | /// Delay before concurrent user windows, in timer ticks. |
|
| 32 | + | constant START_DELAY: u64 = 1000000; |
|
| 33 | + | ||
| 34 | + | /// Base work and migration duration, scaled by the square of hart count. |
|
| 35 | + | constant WORK_TICKS: u64 = 10000000; |
|
| 36 | + | ||
| 37 | + | /// Reserved duration for final machine checks. |
|
| 38 | + | constant CHECK_TICKS: u64 = 10000000000; |
|
| 39 | + | ||
| 40 | + | /// Maximum bounded polls for remote progress. |
|
| 41 | + | constant POLL_ATTEMPTS: u32 = 100000; |
|
| 42 | + | ||
| 43 | + | /// Maximum polls for a level interrupt to reach a saturated event queue. |
|
| 44 | + | constant IRQ_PENDING_ATTEMPTS: u32 = 10000; |
|
| 45 | + | /// Maximum consumed events while checking interrupt saturation and timeout. |
|
| 46 | + | constant IRQ_EVENTS: u32 = 1000; |
|
| 47 | + | /// Maximum maintenance requests after a cancelled page allocation. |
|
| 48 | + | constant RECLAIM_ATTEMPTS: u32 = 1000; |
|
| 49 | + | /// Level-triggered UART source asserted during the storm check. |
|
| 50 | + | constant UART_IRQ: u32 = 10; |
|
| 51 | + | /// Minimum repeated UART events needed to check progress during a storm. |
|
| 52 | + | constant STORM_EVENTS: u32 = 16; |
|
| 53 | + | /// Byte offset of the first PLIC context enable word. |
|
| 54 | + | constant PLIC_ENABLES: u64 = 0x2000; |
|
| 55 | + | /// Byte stride between PLIC context enable words. |
|
| 56 | + | constant PLIC_ENABLE_STRIDE: u64 = 0x80; |
|
| 57 | + | /// Timer ticks before the remote idle-context timeout. |
|
| 58 | + | constant REMOTE_TIMEOUT: u64 = 100000; |
|
| 59 | + | ||
| 60 | + | /// Timeout while the fixture holds a level interrupt asserted. |
|
| 61 | + | constant IRQ_TIMEOUT: u64 = 2000000; |
|
| 62 | + | ||
| 63 | + | // Setup initializes retained state for online harts before releasing READY. |
|
| 64 | + | // Other harts must acquire READY before they read this state. |
|
| 65 | + | /// Bootstrap resource authority. |
|
| 66 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 67 | + | /// Shared user domain generation. |
|
| 68 | + | unsafe static USER: abi::Ref = undefined; |
|
| 69 | + | /// Pending domain in the user's creation subtree. |
|
| 70 | + | unsafe static DESCENDANT: abi::Ref = undefined; |
|
| 71 | + | /// Per-hart user execution contexts. |
|
| 72 | + | unsafe static CONTEXTS: [abi::Ref; limits::HARTS] = undefined; |
|
| 73 | + | /// Mapped progress counters in the user's private state. |
|
| 74 | + | unsafe static COUNTERS: u64 = 0; |
|
| 75 | + | /// Per-context migration observations in the user's private state. |
|
| 76 | + | unsafe static MIGRATIONS: u64 = 0; |
|
| 77 | + | /// Shared handshake for the independently activated context on hart one. |
|
| 78 | + | unsafe static WAKE_STATE: u64 = 0; |
|
| 79 | + | /// Bootstrap management capability for the pending idle-wakeup domain. |
|
| 80 | + | unsafe static IDLE_HANDLE: abi::Handle = undefined; |
|
| 81 | + | /// Context whose Wait call must release the remote hart. |
|
| 82 | + | unsafe static IDLE_CONTEXT: abi::Ref = undefined; |
|
| 83 | + | /// Stack Page handle in the idle-wakeup domain's table. |
|
| 84 | + | unsafe static IDLE_STACK: u64 = 0; |
|
| 85 | + | /// Generation of the idle-wakeup domain. |
|
| 86 | + | unsafe static IDLE_DOMAIN: abi::Ref = undefined; |
|
| 87 | + | /// Completion checker's live domain generation. |
|
| 88 | + | unsafe static CHECKER: abi::Ref = undefined; |
|
| 89 | + | /// Unbound window that hart zero retires before the checker starts. |
|
| 90 | + | unsafe static EXPIRED: abi::Handle = undefined; |
|
| 91 | + | /// Checker's authority to activate and notify the idle-wakeup domain. |
|
| 92 | + | unsafe static RECEIVER: abi::Handle = undefined; |
|
| 93 | + | /// Unbound authority reserved for the idle-wakeup check on hart one. |
|
| 94 | + | unsafe static IDLE_WINDOW: ?abi::Handle = nil; |
|
| 95 | + | /// Release/acquire barrier after fixture publication. |
|
| 96 | + | static READY: u64 = 0; |
|
| 97 | + | /// Validated physical memory mapping. |
|
| 98 | + | fn memory(address: u64) -> *mut u8; |
|
| 99 | + | /// Current kernel package-state table. |
|
| 100 | + | fn kernelGp() -> u64; |
|
| 101 | + | /// M-mode entry of the completion checker. |
|
| 102 | + | fn completion() -> u64; |
|
| 103 | + | /// Write one diagnostic byte to the fixture UART. |
|
| 104 | + | fn put(byte: u8); |
|
| 105 | + | /// Set the fixture UART interrupt-enable register. |
|
| 106 | + | fn uartInterrupt(value: u8); |
|
| 107 | + | /// Finish the machine test successfully. |
|
| 108 | + | fn finish(); |
|
| 109 | + | /// Sleep until this hart's software interrupt is pending. Interrupts stay |
|
| 110 | + | /// globally masked, so the interrupt wakes the hart without trapping. |
|
| 111 | + | fn park(); |
|
| 112 | + | ||
| 113 | + | /// Publish fixture state on the last initialized hart and wait on every hart. |
|
| 114 | + | export unsafe fn start(last: bool) { |
|
| 115 | + | if last { |
|
| 116 | + | setup(); |
|
| 117 | + | sync::storeRelease(&mut READY, 1); |
|
| 118 | + | // Every online hart finished dispatch::initialize before arriving. |
|
| 119 | + | for hart in 0..limits::HARTS { |
|
| 120 | + | if hart <> dispatch::hart() and (boot::PLATFORM.harts & (1 << hart)) |
|
| 121 | + | <> 0 { |
|
| 122 | + | dispatch::signal(hart); |
|
| 123 | + | } |
|
| 124 | + | } |
|
| 125 | + | } |
|
| 126 | + | while sync::loadAcquire(&READY) == 0 { |
|
| 127 | + | park(); |
|
| 128 | + | } |
|
| 129 | + | } |
|
| 130 | + | ||
| 131 | + | /// Create one context per online hart before any dispatch can access the |
|
| 132 | + | /// fixture. |
|
| 133 | + | unsafe fn setup() { |
|
| 134 | + | let authority = fixture::bootstrap(&mut TABLE); |
|
| 135 | + | let image = fixture::load(&mut TABLE, authority, &inputs::INPUT[..]); |
|
| 136 | + | let handle = try! domains::create(&mut state::SHARED.domains, |
|
| 137 | + | &mut state::SHARED.pages.backings, &state::SHARED.registry, |
|
| 138 | + | &mut TABLE, authority, image); |
|
| 139 | + | let cap = try! capability::get(&TABLE, handle); |
|
| 140 | + | set USER = cap.object; |
|
| 141 | + | createDescendant(); |
|
| 142 | + | let mut user = try! domains::get(&state::SHARED.domains, USER); |
|
| 143 | + | let self = try! capability::install(&mut user.memory.table, |
|
| 144 | + | capability::Entry { |
|
| 145 | + | kind: abi::Kind::Domain, object: USER, rights: |
|
| 146 | + | abi::Rights(abi::ALLOCATE | abi::WAKE), |
|
| 147 | + | }); |
|
| 148 | + | assert boot::PLATFORM.irqSources == 31; |
|
| 149 | + | let irq = try! interrupts::seed(&mut state::SHARED.interrupts, |
|
| 150 | + | &mut user.memory.table, cases::IRQ_SOURCE); |
|
| 151 | + | sync::storeRelease(memory(dataAddress(USER, |
|
| 152 | + | &"spin::interrupt"[..])) as *mut u64, *irq); |
|
| 153 | + | let target = try! registry::exported(&state::SHARED.registry, user.image, |
|
| 154 | + | &"spin::worker"[..]); |
|
| 155 | + | let case shared::Target::Function(entry) = target |
|
| 156 | + | else panic "setup: worker entry"; |
|
| 157 | + | set COUNTERS = dataAddress(USER, &"spin::count"[..]); |
|
| 158 | + | set MIGRATIONS = dataAddress(USER, &"spin::migrations"[..]); |
|
| 159 | + | let notification = @sliceOf(memory(dataAddress(USER, |
|
| 160 | + | &"spin::notification"[..])), 8); |
|
| 161 | + | for i in 0..fixture::WORD_BYTES { |
|
| 162 | + | set notification[i] = ((*self) >> (i as u64 * 8)) as u8; |
|
| 163 | + | } |
|
| 164 | + | for hart in 0..limits::HARTS { |
|
| 165 | + | if (boot::PLATFORM.harts & (1 << hart)) == 0 { |
|
| 166 | + | continue; |
|
| 167 | + | } |
|
| 168 | + | let moved = fixture::stack(&mut TABLE, authority, |
|
| 169 | + | &mut user.memory.table); |
|
| 170 | + | if hart == 0 { |
|
| 171 | + | try! fixture::activate(&TABLE, handle, moved, abi::Handle(0), 0); |
|
| 172 | + | set CONTEXTS[hart] = user.initial; |
|
| 173 | + | } else { |
|
| 174 | + | set CONTEXTS[hart] = |
|
| 175 | + | try! domains::contextCreate(&mut state::SHARED.domains, |
|
| 176 | + | &mut state::SHARED.pages, &state::SHARED.registry, USER, |
|
| 177 | + | abi::ContextStart { entry, stack: *moved, args: hart as u64, |
|
| 178 | + | size: 8 }, &mut user.memory.table); |
|
| 179 | + | } |
|
| 180 | + | } |
|
| 181 | + | let checkerHandle = try! domains::create(&mut state::SHARED.domains, |
|
| 182 | + | &mut state::SHARED.pages.backings, &state::SHARED.registry, |
|
| 183 | + | &mut TABLE, authority, image); |
|
| 184 | + | let checkerCap = try! capability::get(&TABLE, checkerHandle); |
|
| 185 | + | let mut checker = try! domains::get(&state::SHARED.domains, |
|
| 186 | + | checkerCap.object); |
|
| 187 | + | set CHECKER = checkerCap.object; |
|
| 188 | + | fixture::machine(checkerCap.object, completion(), kernelGp()); |
|
| 189 | + | idleSetup(authority, image); |
|
| 190 | + | let clock = dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0)); |
|
| 191 | + | let start = clock + START_DELAY; |
|
| 192 | + | let mut harts: u64 = 0; |
|
| 193 | + | for hart in 0..limits::HARTS { |
|
| 194 | + | if (boot::PLATFORM.harts & (1 << hart)) <> 0 { |
|
| 195 | + | set harts += 1; |
|
| 196 | + | } |
|
| 197 | + | } |
|
| 198 | + | let end = start + WORK_TICKS * harts * harts; |
|
| 199 | + | let migrationEnd = end + WORK_TICKS * harts * harts; |
|
| 200 | + | let windowBase = dataAddress(USER, &"spin::windows"[..]); |
|
| 201 | + | for hart in 0..limits::HARTS { |
|
| 202 | + | if (boot::PLATFORM.harts & (1 << hart)) == 0 { |
|
| 203 | + | continue; |
|
| 204 | + | } |
|
| 205 | + | let begin = clock if hart == 0 else start; |
|
| 206 | + | let mut budget = try! budgets::seed(&mut state::SHARED.budgets, |
|
| 207 | + | &mut TABLE, hart, begin, 0xffffffffffffffff); |
|
| 208 | + | if hart == 0 { |
|
| 209 | + | let next = try! budgets::split(&mut state::SHARED.budgets, |
|
| 210 | + | &mut TABLE, budget, start, clock); |
|
| 211 | + | set EXPIRED = try! budgets::transfer(&mut state::SHARED.budgets, |
|
| 212 | + | &mut TABLE, &mut checker.memory.table, budget, |
|
| 213 | + | budgets::DEFAULT_RIGHTS as u64, clock); |
|
| 214 | + | set budget = next; |
|
| 215 | + | } |
|
| 216 | + | let final = try! budgets::split(&mut state::SHARED.budgets, |
|
| 217 | + | &mut TABLE, budget, end, clock); |
|
| 218 | + | let checking = try! budgets::split(&mut state::SHARED.budgets, |
|
| 219 | + | &mut TABLE, final, migrationEnd, clock); |
|
| 220 | + | let future = try! budgets::split(&mut state::SHARED.budgets, |
|
| 221 | + | &mut TABLE, checking, migrationEnd + CHECK_TICKS, clock); |
|
| 222 | + | let transferred = try! budgets::transfer(&mut state::SHARED.budgets, |
|
| 223 | + | &mut TABLE, &mut user.memory.table, future, |
|
| 224 | + | budgets::DEFAULT_RIGHTS as u64, clock); |
|
| 225 | + | let caller = (hart as u64 + harts - 1) % harts; |
|
| 226 | + | let output = @sliceOf(memory(windowBase + caller * 8), 8); |
|
| 227 | + | for i in 0..fixture::WORD_BYTES { |
|
| 228 | + | set output[i] = ((*transferred) >> (i as u64 * 8)) as u8; |
|
| 229 | + | } |
|
| 230 | + | let bound = try! calls::rebind(&mut state::SHARED, &mut TABLE, |
|
| 231 | + | budgets::Binding { budget, domain: handle, |
|
| 232 | + | context: CONTEXTS[hart] }, clock); |
|
| 233 | + | let rotated = ((hart as u64 + 1) % harts) as u32; |
|
| 234 | + | let moved = try! calls::rebind(&mut state::SHARED, &mut TABLE, |
|
| 235 | + | budgets::Binding { budget: final, domain: handle, |
|
| 236 | + | context: CONTEXTS[rotated] }, clock); |
|
| 237 | + | if hart == 0 { |
|
| 238 | + | let bound = try! calls::rebind(&mut state::SHARED, &mut TABLE, |
|
| 239 | + | budgets::Binding { budget: checking, domain: checkerHandle, |
|
| 240 | + | context: checker.initial }, clock); |
|
| 241 | + | } |
|
| 242 | + | if hart == 1 { |
|
| 243 | + | set IDLE_WINDOW = checking; |
|
| 244 | + | } |
|
| 245 | + | } |
|
| 246 | + | } |
|
| 247 | + | ||
| 248 | + | /// Create an actual pending descendant before secondary harts enter dispatch. |
|
| 249 | + | unsafe fn createDescendant() { |
|
| 250 | + | let mut user = try! domains::get(&state::SHARED.domains, USER); |
|
| 251 | + | let root = user.image; |
|
| 252 | + | let authority = try! capability::install(&mut user.memory.table, |
|
| 253 | + | capability::Entry { |
|
| 254 | + | kind: abi::Kind::Domain, object: USER, rights: |
|
| 255 | + | abi::Rights(abi::CREATE), |
|
| 256 | + | }); |
|
| 257 | + | let image = try! registry::install(&state::SHARED.registry, |
|
| 258 | + | &mut user.memory.table, root); |
|
| 259 | + | let handle = try! domains::create(&mut state::SHARED.domains, |
|
| 260 | + | &mut state::SHARED.pages.backings, &state::SHARED.registry, |
|
| 261 | + | &mut user.memory.table, authority, image); |
|
| 262 | + | set DESCENDANT = (try! capability::get(&user.memory.table, handle)).object; |
|
| 263 | + | } |
|
| 264 | + | ||
| 265 | + | /// Resolve one exported private-data address in the fixture's user instance. |
|
| 266 | + | unsafe fn dataAddress(owner: abi::Ref, name: &[u8]) -> u64 { |
|
| 267 | + | let user = try! domains::get(&state::SHARED.domains, owner); |
|
| 268 | + | let target = try! registry::exported(&state::SHARED.registry, user.image, |
|
| 269 | + | name); |
|
| 270 | + | let case shared::Target::Data(data) = |
|
| 271 | + | target else panic "dataAddress: fixture data export"; |
|
| 272 | + | return instances::base(&state::SHARED.domains.tables[owner.index], |
|
| 273 | + | data.slot) + data.offset as u64; |
|
| 274 | + | } |
|
| 275 | + | ||
| 276 | + | /// Prepare a pending domain and its stack for activation on an idle hart. |
|
| 277 | + | unsafe fn idleSetup(authority: abi::Handle, image: abi::Handle) { |
|
| 278 | + | set IDLE_HANDLE = try! domains::create(&mut state::SHARED.domains, |
|
| 279 | + | &mut state::SHARED.pages.backings, &state::SHARED.registry, |
|
| 280 | + | &mut TABLE, authority, image); |
|
| 281 | + | let permit = try! capability::get(&TABLE, IDLE_HANDLE); |
|
| 282 | + | set IDLE_DOMAIN = permit.object; |
|
| 283 | + | let mut domain = try! domains::get(&state::SHARED.domains, IDLE_DOMAIN); |
|
| 284 | + | let allocation = try! capability::install(&mut domain.memory.table, |
|
| 285 | + | capability::Entry { |
|
| 286 | + | kind: abi::Kind::Domain, object: IDLE_DOMAIN, rights: |
|
| 287 | + | abi::Rights(abi::ALLOCATE), |
|
| 288 | + | }); |
|
| 289 | + | set IDLE_CONTEXT = domain.initial; |
|
| 290 | + | set WAKE_STATE = dataAddress(IDLE_DOMAIN, &"spin::wakeState"[..]); |
|
| 291 | + | sync::storeRelease(memory(dataAddress(IDLE_DOMAIN, |
|
| 292 | + | &"spin::idle"[..])) as *mut u64, 1); |
|
| 293 | + | let moved = fixture::stack(&mut TABLE, authority, |
|
| 294 | + | &mut domain.memory.table); |
|
| 295 | + | set IDLE_STACK = *moved; |
|
| 296 | + | let mut checker = try! domains::get(&state::SHARED.domains, CHECKER); |
|
| 297 | + | set RECEIVER = try! capability::install(&mut checker.memory.table, |
|
| 298 | + | capability::Entry { |
|
| 299 | + | kind: abi::Kind::Domain, object: IDLE_DOMAIN, |
|
| 300 | + | rights: abi::Rights(abi::WAKE | abi::EXECUTE | abi::DESTROY), |
|
| 301 | + | }); |
|
| 302 | + | } |
|
| 303 | + | ||
| 304 | + | /// Check shared progress and context isolation after the user windows finish. |
|
| 305 | + | /// Only the scheduled checker runs these phases. Kernel metadata is locked by |
|
| 306 | + | /// each phase; user progress words use acquire loads. |
|
| 307 | + | export unsafe fn verify() { |
|
| 308 | + | checkUserContexts(); |
|
| 309 | + | checkAcknowledgements(); |
|
| 310 | + | expiredCall(); |
|
| 311 | + | if (boot::PLATFORM.harts & 2) <> 0 { |
|
| 312 | + | idleWakeup(); |
|
| 313 | + | } |
|
| 314 | + | interruptOwnership(); |
|
| 315 | + | interruptStorm(); |
|
| 316 | + | if (boot::PLATFORM.harts & 2) <> 0 { |
|
| 317 | + | terminalWakeup(); |
|
| 318 | + | cancelAllocation(); |
|
| 319 | + | reclaimedDomains(); |
|
| 320 | + | } |
|
| 321 | + | invariants::check(&TABLE); |
|
| 322 | + | for byte in "smp final ownership and interval invariants passed\n" { |
|
| 323 | + | put(byte); |
|
| 324 | + | } |
|
| 325 | + | finish(); |
|
| 326 | + | } |
|
| 327 | + | ||
| 328 | + | /// Check saved contexts, migration destinations, and all per-hart progress. |
|
| 329 | + | unsafe fn checkUserContexts() { |
|
| 330 | + | let guard = sync::enter(); |
|
| 331 | + | let user = try! domains::get(&state::SHARED.domains, USER); |
|
| 332 | + | let mut counts: [u64; limits::HARTS] = [0; limits::HARTS]; |
|
| 333 | + | let mut notifications: u32 = 0; |
|
| 334 | + | for hart in 0..limits::HARTS { |
|
| 335 | + | if (boot::PLATFORM.harts & (1 << hart)) <> 0 { |
|
| 336 | + | set notifications += 1; |
|
| 337 | + | } |
|
| 338 | + | } |
|
| 339 | + | for hart in 0..limits::HARTS { |
|
| 340 | + | if (boot::PLATFORM.harts & (1 << hart)) == 0 { |
|
| 341 | + | continue; |
|
| 342 | + | } |
|
| 343 | + | let value = sync::loadAcquire(memory(COUNTERS + |
|
| 344 | + | hart as u64 * 8) as *u64); |
|
| 345 | + | set counts[hart] = value; |
|
| 346 | + | let context = try! domains::context(&state::SHARED.domains, USER, |
|
| 347 | + | CONTEXTS[hart]); |
|
| 348 | + | assert context.owner == USER; |
|
| 349 | + | let gp = context.frame.registers[*rv64::GP as u32]; |
|
| 350 | + | let sp = context.frame.registers[*rv64::SP as u32]; |
|
| 351 | + | let mode = context.frame.status & fixture::MACHINE_PRIVILEGE; |
|
| 352 | + | assert mode == 0 or mode == fixture::MACHINE_PRIVILEGE; |
|
| 353 | + | if mode == fixture::MACHINE_PRIVILEGE { |
|
| 354 | + | assert gp == kernelGp(); |
|
| 355 | + | assert sp >= context.kernelStack.start |
|
| 356 | + | and sp <= context.kernelStack.end; |
|
| 357 | + | } else { |
|
| 358 | + | assert gp == |
|
| 359 | + | instances::address( &state::SHARED.domains.tables[USER.index]); |
|
| 360 | + | } |
|
| 361 | + | let destination = (hart + notifications - 1) % notifications; |
|
| 362 | + | assert context.hart == nil or context.hart == destination; |
|
| 363 | + | assert sync::loadAcquire(memory(MIGRATIONS + |
|
| 364 | + | hart as u64 * 8) as *u64) == destination as u64 |
|
| 365 | + | + 1; |
|
| 366 | + | assert context.pending == nil; |
|
| 367 | + | } |
|
| 368 | + | assert state::SHARED.domains.events.queues[USER.index].tail == |
|
| 369 | + | notifications + 1; |
|
| 370 | + | assert user.memory.ring.data[0].kind == events::INTERRUPT |
|
| 371 | + | and user.memory.ring.data[0].code == cases::IRQ_SOURCE; |
|
| 372 | + | sync::leave(guard); |
|
| 373 | + | for byte in "smp completed transactions:" { |
|
| 374 | + | put(byte); |
|
| 375 | + | } |
|
| 376 | + | for hart in 0..limits::HARTS { |
|
| 377 | + | if (boot::PLATFORM.harts & (1 << hart)) == 0 { |
|
| 378 | + | continue; |
|
| 379 | + | } |
|
| 380 | + | put(32); |
|
| 381 | + | number(counts[hart]); |
|
| 382 | + | } |
|
| 383 | + | put(10); |
|
| 384 | + | for hart in 0..limits::HARTS { |
|
| 385 | + | if (boot::PLATFORM.harts & (1 << hart)) <> 0 { |
|
| 386 | + | assert counts[hart] == cases::TRANSACTIONS as u64; |
|
| 387 | + | } |
|
| 388 | + | } |
|
| 389 | + | } |
|
| 390 | + | ||
| 391 | + | /// Post one request per online hart and check acknowledgement ownership. |
|
| 392 | + | unsafe fn checkAcknowledgements() { |
|
| 393 | + | for target in 0..limits::HARTS { |
|
| 394 | + | if (boot::PLATFORM.harts & (1 << target)) == 0 { |
|
| 395 | + | continue; |
|
| 396 | + | } |
|
| 397 | + | let posting = sync::enter(); |
|
| 398 | + | let id = try! dispatch::request(&mut state::SHARED.remote, target, |
|
| 399 | + | remote::Action::Reschedule); |
|
| 400 | + | sync::leave(posting); |
|
| 401 | + | let mut acknowledged = false; |
|
| 402 | + | for attempt in 0..POLL_ATTEMPTS { |
|
| 403 | + | let guard = sync::enter(); |
|
| 404 | + | if try! remote::completed(&state::SHARED.remote, 0, id) { |
|
| 405 | + | try! remote::release(&mut state::SHARED.remote, 0, id); |
|
| 406 | + | set acknowledged = true; |
|
| 407 | + | } |
|
| 408 | + | sync::leave(guard); |
|
| 409 | + | if acknowledged { |
|
| 410 | + | break; |
|
| 411 | + | } |
|
| 412 | + | } |
|
| 413 | + | assert acknowledged; |
|
| 414 | + | } |
|
| 415 | + | for byte in "smp request acknowledgements passed\n" { |
|
| 416 | + | put(byte); |
|
| 417 | + | } |
|
| 418 | + | } |
|
| 419 | + | ||
| 420 | + | /// Dispatch on the window's hart retires elapsed authority, so a later call |
|
| 421 | + | /// finds a stale handle. |
|
| 422 | + | unsafe fn expiredCall() { |
|
| 423 | + | let guard = sync::enter(); |
|
| 424 | + | let checker = try! domains::get(&state::SHARED.domains, CHECKER); |
|
| 425 | + | let mut retired = false; |
|
| 426 | + | try budgets::query(&state::SHARED.budgets, &checker.memory.table, |
|
| 427 | + | EXPIRED) catch error { |
|
| 428 | + | assert error == abi::Error::BadHandle; |
|
| 429 | + | set retired = true; |
|
| 430 | + | }; |
|
| 431 | + | assert retired; |
|
| 432 | + | sync::leave(guard); |
|
| 433 | + | let mut rejected = false; |
|
| 434 | + | try calls::synchronized(CHECKER, abi::Operation::BudgetSplit as u32, |
|
| 435 | + | &[*EXPIRED, 1, 0, 0], 0) catch error { |
|
| 436 | + | assert error == abi::Error::BadHandle; |
|
| 437 | + | set rejected = true; |
|
| 438 | + | }; |
|
| 439 | + | assert rejected; |
|
| 440 | + | for byte in "smp resumed call rejected expired authority\n" { |
|
| 441 | + | put(byte); |
|
| 442 | + | } |
|
| 443 | + | } |
|
| 444 | + | ||
| 445 | + | /// Transfer a queued IRQ, rearm after consumption, and mask it before |
|
| 446 | + | /// capability drop. |
|
| 447 | + | unsafe fn interruptOwnership() { |
|
| 448 | + | let guard = sync::enter(); |
|
| 449 | + | let mut user = try! domains::get(&state::SHARED.domains, USER); |
|
| 450 | + | let mut checker = try! domains::get(&state::SHARED.domains, CHECKER); |
|
| 451 | + | let device = try! plic::mapping(&boot::PLATFORM) |
|
| 452 | + | else panic "interruptOwnership: missing PLIC"; |
|
| 453 | + | let enabled = PLIC_ENABLES + device.context as u64 * PLIC_ENABLE_STRIDE; |
|
| 454 | + | let bit = (1 as u32) << cases::IRQ_SOURCE; |
|
| 455 | + | assert (registers::read32(device.registers, enabled) & bit) == 0; |
|
| 456 | + | assert state::SHARED.interrupts.records[cases::IRQ_SOURCE - 1].state == |
|
| 457 | + | interrupts::State::Queued; |
|
| 458 | + | let original = |
|
| 459 | + | state::SHARED.interrupts.records[cases::IRQ_SOURCE - 1].handle; |
|
| 460 | + | let target = try! capability::install(&mut user.memory.table, |
|
| 461 | + | capability::Entry { |
|
| 462 | + | kind: abi::Kind::Domain, object: CHECKER, |
|
| 463 | + | rights: abi::Rights(0), |
|
| 464 | + | }); |
|
| 465 | + | let back = try! capability::install(&mut checker.memory.table, |
|
| 466 | + | capability::Entry { |
|
| 467 | + | kind: abi::Kind::Domain, object: USER, rights: abi::Rights(0), |
|
| 468 | + | }); |
|
| 469 | + | let moved = try! calls::invoke(USER, |
|
| 470 | + | abi::Operation::CapabilityTransfer as u32, &[*original, *target, |
|
| 471 | + | (abi::READ | abi::TRANSFER) as u64, 0], 0); |
|
| 472 | + | assert state::SHARED.interrupts.records[cases::IRQ_SOURCE - 1].owner == |
|
| 473 | + | CHECKER; |
|
| 474 | + | let first = plic::service(&mut state::SHARED.interrupts, |
|
| 475 | + | &mut state::SHARED.domains, &state::SHARED.budgets, |
|
| 476 | + | dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0))); |
|
| 477 | + | assert state::SHARED.interrupts.records[cases::IRQ_SOURCE - 1].state == |
|
| 478 | + | interrupts::State::Queued; |
|
| 479 | + | assert (registers::read32(device.registers, enabled) & bit) == 0; |
|
| 480 | + | let event = try! events::pop(&mut checker.memory.ring) |
|
| 481 | + | else panic "interruptOwnership: missing transferred IRQ"; |
|
| 482 | + | assert event.kind == events::INTERRUPT and event.code == cases::IRQ_SOURCE; |
|
| 483 | + | let before = state::SHARED.domains.events.queues[USER.index].tail; |
|
| 484 | + | let returned = try! calls::invoke(CHECKER, |
|
| 485 | + | abi::Operation::CapabilityTransfer as u32, &[moved, *back, |
|
| 486 | + | (abi::READ | abi::TRANSFER) as u64, 0], 0); |
|
| 487 | + | let second = plic::service(&mut state::SHARED.interrupts, |
|
| 488 | + | &mut state::SHARED.domains, &state::SHARED.budgets, |
|
| 489 | + | dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0))); |
|
| 490 | + | assert state::SHARED.domains.events.queues[USER.index].tail == before; |
|
| 491 | + | assert (registers::read32(device.registers, enabled) & bit) == 0; |
|
| 492 | + | let consumed = try! events::pop(&mut user.memory.ring) |
|
| 493 | + | else panic "interruptOwnership: missing original IRQ"; |
|
| 494 | + | assert consumed.kind == events::INTERRUPT and |
|
| 495 | + | consumed.code == cases::IRQ_SOURCE; |
|
| 496 | + | let rearmed = plic::service(&mut state::SHARED.interrupts, |
|
| 497 | + | &mut state::SHARED.domains, &state::SHARED.budgets, |
|
| 498 | + | dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0))); |
|
| 499 | + | assert state::SHARED.interrupts.records[cases::IRQ_SOURCE - 1].state == |
|
| 500 | + | interrupts::State::Idle; |
|
| 501 | + | assert (registers::read32(device.registers, enabled) & bit) <> 0; |
|
| 502 | + | let dropped = try! calls::invoke(USER, |
|
| 503 | + | abi::Operation::CapabilityDrop as u32, &[returned, 0, 0, 0], 0); |
|
| 504 | + | assert dropped == 0 and |
|
| 505 | + | state::SHARED.interrupts.slots[cases::IRQ_SOURCE - 1].state == |
|
| 506 | + | slots::State::Free; |
|
| 507 | + | assert (registers::read32(device.registers, enabled) & bit) == 0; |
|
| 508 | + | sync::leave(guard); |
|
| 509 | + | for byte in "smp IRQ transfer, rearm, and drop passed\n" { |
|
| 510 | + | put(byte); |
|
| 511 | + | } |
|
| 512 | + | } |
|
| 513 | + | ||
| 514 | + | /// Keep a level source asserted across full queues and repeated driver |
|
| 515 | + | /// progress. |
|
| 516 | + | unsafe fn interruptStorm() { |
|
| 517 | + | let guard = sync::enter(); |
|
| 518 | + | let mut checker = try! domains::get(&state::SHARED.domains, CHECKER); |
|
| 519 | + | let handle = try! interrupts::seed(&mut state::SHARED.interrupts, |
|
| 520 | + | &mut checker.memory.table, UART_IRQ); |
|
| 521 | + | let device = try! plic::mapping(&boot::PLATFORM) |
|
| 522 | + | else panic "interruptStorm: missing PLIC"; |
|
| 523 | + | let enabled = PLIC_ENABLES + device.context as u64 * PLIC_ENABLE_STRIDE; |
|
| 524 | + | sync::leave(guard); |
|
| 525 | + | for i in 0..events::ORDINARY { |
|
| 526 | + | let filling = sync::enter(); |
|
| 527 | + | assert try! domains::serve(&mut state::SHARED.domains, CHECKER, |
|
| 528 | + | domains::RingRequest::Send(events::Event { kind: events::WAKEUP, |
|
| 529 | + | reserved: 0, code: i, |
|
| 530 | + | value: 0 })); |
|
| 531 | + | sync::leave(filling); |
|
| 532 | + | } |
|
| 533 | + | let arming = sync::enter(); |
|
| 534 | + | let ready = plic::service(&mut state::SHARED.interrupts, |
|
| 535 | + | &mut state::SHARED.domains, &state::SHARED.budgets, |
|
| 536 | + | dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0))); |
|
| 537 | + | uartInterrupt(2); |
|
| 538 | + | sync::leave(arming); |
|
| 539 | + | let mut pending = false; |
|
| 540 | + | for attempt in 0..IRQ_PENDING_ATTEMPTS { |
|
| 541 | + | let checking = sync::enter(); |
|
| 542 | + | set pending = |
|
| 543 | + | state::SHARED.interrupts.records[UART_IRQ - 1].state == |
|
| 544 | + | interrupts::State::Pending; |
|
| 545 | + | if pending { |
|
| 546 | + | assert (registers::read32(device.registers, |
|
| 547 | + | enabled) & (1 << UART_IRQ)) == 0; |
|
| 548 | + | let queue = state::SHARED.domains.events.queues[CHECKER.index]; |
|
| 549 | + | assert queue.tail - queue.head == events::ORDINARY; |
|
| 550 | + | } |
|
| 551 | + | sync::leave(checking); |
|
| 552 | + | if pending { |
|
| 553 | + | break; |
|
| 554 | + | } |
|
| 555 | + | } |
|
| 556 | + | assert pending; |
|
| 557 | + | for i in 0..events::ORDINARY { |
|
| 558 | + | let draining = sync::enter(); |
|
| 559 | + | let mut receiver = try! domains::get(&state::SHARED.domains, CHECKER); |
|
| 560 | + | let event = try! events::pop(&mut receiver.memory.ring) |
|
| 561 | + | else panic "interruptStorm: missing saturation event"; |
|
| 562 | + | assert event.kind == events::WAKEUP and event.code == i; |
|
| 563 | + | sync::leave(draining); |
|
| 564 | + | } |
|
| 565 | + | let posting = sync::enter(); |
|
| 566 | + | let clock = dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0)); |
|
| 567 | + | try! timers::request(&mut state::SHARED.timers, &state::SHARED.domains, |
|
| 568 | + | CHECKER, 0, clock, IRQ_TIMEOUT, cases::STORM_TOKEN); |
|
| 569 | + | let request = try! dispatch::request(&mut state::SHARED.remote, 0, |
|
| 570 | + | remote::Action::Reschedule); |
|
| 571 | + | sync::leave(posting); |
|
| 572 | + | let acknowledged = try! dispatch::awaitRequest(0, request); |
|
| 573 | + | assert acknowledged == 0; |
|
| 574 | + | let mut received: u32 = 0; |
|
| 575 | + | let mut timed = false; |
|
| 576 | + | for attempt in 0..IRQ_EVENTS { |
|
| 577 | + | let consuming = sync::enter(); |
|
| 578 | + | let mut receiver = try! domains::get(&state::SHARED.domains, CHECKER); |
|
| 579 | + | if let event = try! events::pop(&mut receiver.memory.ring) { |
|
| 580 | + | if event.kind == events::INTERRUPT { |
|
| 581 | + | assert event.code == UART_IRQ; |
|
| 582 | + | set received += 1; |
|
| 583 | + | } else { |
|
| 584 | + | assert event.kind == events::TIMEOUT and |
|
| 585 | + | event.code == cases::STORM_TOKEN; |
|
| 586 | + | assert dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0)) |
|
| 587 | + | >= clock + IRQ_TIMEOUT; |
|
| 588 | + | set timed = true; |
|
| 589 | + | } |
|
| 590 | + | } |
|
| 591 | + | let notified = plic::service(&mut state::SHARED.interrupts, |
|
| 592 | + | &mut state::SHARED.domains, &state::SHARED.budgets, |
|
| 593 | + | dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0))); |
|
| 594 | + | sync::leave(consuming); |
|
| 595 | + | if timed and received >= STORM_EVENTS { |
|
| 596 | + | break; |
|
| 597 | + | } |
|
| 598 | + | } |
|
| 599 | + | let stopping = sync::enter(); |
|
| 600 | + | uartInterrupt(0); |
|
| 601 | + | let dropped = try! calls::invoke(CHECKER, |
|
| 602 | + | abi::Operation::CapabilityDrop as u32, &[*handle, 0, 0, 0], 0); |
|
| 603 | + | assert dropped == 0 and (registers::read32(device.registers, |
|
| 604 | + | enabled) & (1 << UART_IRQ)) == 0; |
|
| 605 | + | sync::leave(stopping); |
|
| 606 | + | assert timed and received >= STORM_EVENTS; |
|
| 607 | + | for byte in "smp IRQ saturation and storm timeout passed\n" { |
|
| 608 | + | put(byte); |
|
| 609 | + | } |
|
| 610 | + | } |
|
| 611 | + | ||
| 612 | + | /// Resume an actual U-mode Wait call through an IPI to an idle remote hart. |
|
| 613 | + | unsafe fn idleWakeup() { |
|
| 614 | + | let window = IDLE_WINDOW |
|
| 615 | + | else panic "idleWakeup: hart one window required"; |
|
| 616 | + | let guard = sync::enter(); |
|
| 617 | + | let clock = dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0)); |
|
| 618 | + | let handle = try! calls::rebind(&mut state::SHARED, &mut TABLE, |
|
| 619 | + | budgets::Binding { budget: window, domain: IDLE_HANDLE, |
|
| 620 | + | context: IDLE_CONTEXT }, clock); |
|
| 621 | + | let request = try! dispatch::request(&mut state::SHARED.remote, 1, |
|
| 622 | + | remote::Action::Reschedule); |
|
| 623 | + | sync::leave(guard); |
|
| 624 | + | let acknowledged = try! dispatch::awaitRequest(0, request); |
|
| 625 | + | assert acknowledged == 0; |
|
| 626 | + | let activated = try! calls::synchronized(CHECKER, |
|
| 627 | + | abi::Operation::DomainActivate as u32, &[*RECEIVER, |
|
| 628 | + | IDLE_STACK, 0, 0], clock); |
|
| 629 | + | assert activated == 0 and waiting(); |
|
| 630 | + | let now = dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0)); |
|
| 631 | + | let result = try! calls::synchronized( |
|
| 632 | + | CHECKER, |
|
| 633 | + | abi::Operation::EventWakeup as u32, |
|
| 634 | + | &[ |
|
| 635 | + | *RECEIVER, |
|
| 636 | + | cases::WAKE_TOKEN as u64, 0, 0, |
|
| 637 | + | ], |
|
| 638 | + | now |
|
| 639 | + | ); |
|
| 640 | + | assert result == 0; |
|
| 641 | + | let mut resumed = false; |
|
| 642 | + | for attempt in 0..POLL_ATTEMPTS { |
|
| 643 | + | if sync::loadAcquire(memory(WAKE_STATE) as *u64) == |
|
| 644 | + | cases::WakeStage::WakeResumed as u64 { |
|
| 645 | + | set resumed = true; |
|
| 646 | + | break; |
|
| 647 | + | } |
|
| 648 | + | } |
|
| 649 | + | assert resumed; |
|
| 650 | + | for byte in "smp idle-hart activation and wakeup passed\n" { |
|
| 651 | + | put(byte); |
|
| 652 | + | } |
|
| 653 | + | remoteTimeout(); |
|
| 654 | + | } |
|
| 655 | + | ||
| 656 | + | /// Deliver a timeout from hart zero to a waiting user context on idle hart one. |
|
| 657 | + | unsafe fn remoteTimeout() { |
|
| 658 | + | let guard = sync::enter(); |
|
| 659 | + | let mut domain = try! domains::get(&state::SHARED.domains, IDLE_DOMAIN); |
|
| 660 | + | let event = try! events::pop(&mut domain.memory.ring) |
|
| 661 | + | else panic "remoteTimeout: missing wakeup"; |
|
| 662 | + | assert event.kind == events::WAKEUP and event.code == cases::WAKE_TOKEN; |
|
| 663 | + | sync::storeRelease(memory(WAKE_STATE) as *mut u64, |
|
| 664 | + | cases::WakeStage::AllowTimeout as u64); |
|
| 665 | + | sync::leave(guard); |
|
| 666 | + | assert waiting(); |
|
| 667 | + | let posting = sync::enter(); |
|
| 668 | + | let clock = dispatch::now(try! dispatch::timer(&boot::PLATFORM, 0)); |
|
| 669 | + | try! timers::request(&mut state::SHARED.timers, &state::SHARED.domains, |
|
| 670 | + | IDLE_DOMAIN, 0, clock, REMOTE_TIMEOUT, cases::TIMEOUT_TOKEN); |
|
| 671 | + | let request = try! dispatch::request(&mut state::SHARED.remote, 0, |
|
| 672 | + | remote::Action::Reschedule); |
|
| 673 | + | sync::leave(posting); |
|
| 674 | + | let acknowledged = try! dispatch::awaitRequest(0, request); |
|
| 675 | + | assert acknowledged == 0; |
|
| 676 | + | let mut resumed = false; |
|
| 677 | + | for attempt in 0..POLL_ATTEMPTS { |
|
| 678 | + | if sync::loadAcquire(memory(WAKE_STATE) as *u64) == |
|
| 679 | + | cases::WakeStage::TimeoutResumed as u64 { |
|
| 680 | + | set resumed = true; |
|
| 681 | + | break; |
|
| 682 | + | } |
|
| 683 | + | } |
|
| 684 | + | assert resumed; |
|
| 685 | + | for byte in "smp remote timeout passed\n" { |
|
| 686 | + | put(byte); |
|
| 687 | + | } |
|
| 688 | + | } |
|
| 689 | + | ||
| 690 | + | /// Wake a parent waiting on hart one through terminal delivery on hart zero. |
|
| 691 | + | unsafe fn terminalWakeup() { |
|
| 692 | + | let guard = sync::enter(); |
|
| 693 | + | let mut receiver = try! domains::get(&state::SHARED.domains, IDLE_DOMAIN); |
|
| 694 | + | let timeout = try! events::pop(&mut receiver.memory.ring) |
|
| 695 | + | else panic "terminalWakeup: missing idle timeout"; |
|
| 696 | + | assert timeout.kind == events::TIMEOUT and |
|
| 697 | + | timeout.code == cases::TIMEOUT_TOKEN; |
|
| 698 | + | sync::storeRelease(memory(WAKE_STATE) as *mut u64, |
|
| 699 | + | cases::WakeStage::AllowTerminalWait as u64); |
|
| 700 | + | sync::leave(guard); |
|
| 701 | + | assert waiting(); |
|
| 702 | + | let stopping = sync::enter(); |
|
| 703 | + | let mut checker = try! domains::get(&state::SHARED.domains, CHECKER); |
|
| 704 | + | let child = try! capability::install(&mut checker.memory.table, |
|
| 705 | + | capability::Entry { |
|
| 706 | + | kind: abi::Kind::Domain, object: USER, rights: |
|
| 707 | + | abi::Rights(abi::DESTROY), |
|
| 708 | + | }); |
|
| 709 | + | let parent = try! calls::invoke(CHECKER, |
|
| 710 | + | abi::Operation::DomainReparent as u32, &[*child, *RECEIVER, 0, 0], 0); |
|
| 711 | + | assert parent == 0; |
|
| 712 | + | let cancelled = try! dispatch::request(&mut state::SHARED.remote, 1, |
|
| 713 | + | remote::Action::Budget(remote::Call { |
|
| 714 | + | owner: USER, context: CONTEXTS[0], |
|
| 715 | + | operation: abi::Operation::BudgetSplit as u32, arguments: [0; 4], |
|
| 716 | + | })); |
|
| 717 | + | set state::SHARED.domains.contexts[CONTEXTS[0].index].pending = cancelled; |
|
| 718 | + | let readiness = try! remote::reserve(&mut state::SHARED.remote, 0, 2); |
|
| 719 | + | let batch = remote::publish(&mut state::SHARED.remote, readiness, |
|
| 720 | + | remote::Action::Ready(CONTEXTS[1])); |
|
| 721 | + | dispatch::startReady(&mut state::SHARED.domains, CONTEXTS[1], &batch); |
|
| 722 | + | let stopped = try! domains::terminateTree(&mut state::SHARED.domains, |
|
| 723 | + | USER, events::CHILD_EXIT, 23); |
|
| 724 | + | assert stopped == 0; |
|
| 725 | + | assert state::SHARED.domains.records[DESCENDANT.index].state == |
|
| 726 | + | domains::Lifecycle::Dead; |
|
| 727 | + | // The descendant reported to USER, which dies with it, so its event |
|
| 728 | + | // moves to USER's parent. |
|
| 729 | + | assert |
|
| 730 | + | state::SHARED.domains.events.terminals[DESCENDANT.index].receiver |
|
| 731 | + | == IDLE_DOMAIN; |
|
| 732 | + | let request = try! dispatch::request(&mut state::SHARED.remote, 0, |
|
| 733 | + | remote::Action::Reschedule); |
|
| 734 | + | sync::leave(stopping); |
|
| 735 | + | let acknowledged = try! dispatch::awaitRequest(0, request); |
|
| 736 | + | assert acknowledged == 0; |
|
| 737 | + | let mut resumed = false; |
|
| 738 | + | for attempt in 0..POLL_ATTEMPTS { |
|
| 739 | + | if sync::loadAcquire(memory(WAKE_STATE) as *u64) == |
|
| 740 | + | cases::WakeStage::TerminalResumed as u64 { |
|
| 741 | + | set resumed = true; |
|
| 742 | + | break; |
|
| 743 | + | } |
|
| 744 | + | } |
|
| 745 | + | assert resumed; |
|
| 746 | + | let checking = sync::enter(); |
|
| 747 | + | assert state::SHARED.remote.slots[cancelled.index].state == |
|
| 748 | + | slots::State::Free; |
|
| 749 | + | assert state::SHARED.remote.slots[batch.ids[1].index].state == |
|
| 750 | + | slots::State::Free; |
|
| 751 | + | assert state::SHARED.domains.contexts[CONTEXTS[0].index].pending == nil; |
|
| 752 | + | assert state::SHARED.domains.contexts[CONTEXTS[1].index].pending == nil; |
|
| 753 | + | let mut domain = try! domains::get(&state::SHARED.domains, IDLE_DOMAIN); |
|
| 754 | + | let mut seen: u32 = 0; |
|
| 755 | + | for i in 0..2 { |
|
| 756 | + | let event = try! events::pop(&mut domain.memory.ring) |
|
| 757 | + | else panic "terminalWakeup: missing remote exit"; |
|
| 758 | + | if event.value == abi::id(USER) { |
|
| 759 | + | assert event.kind == events::CHILD_EXIT and event.code == 23; |
|
| 760 | + | set seen |= 1; |
|
| 761 | + | } else { |
|
| 762 | + | assert event.kind == events::FAULT and |
|
| 763 | + | event.code == abi::FAULT_DESTROYED |
|
| 764 | + | and event.value == abi::id(DESCENDANT); |
|
| 765 | + | set seen |= 2; |
|
| 766 | + | } |
|
| 767 | + | } |
|
| 768 | + | assert seen == 3; |
|
| 769 | + | sync::leave(checking); |
|
| 770 | + | for byte in "smp cascade cancelled calls and woke remote parent\n" { |
|
| 771 | + | put(byte); |
|
| 772 | + | } |
|
| 773 | + | } |
|
| 774 | + | ||
| 775 | + | /// Stop a real page-clear continuation and recover its unpublished frames. |
|
| 776 | + | unsafe fn cancelAllocation() { |
|
| 777 | + | sync::storeRelease(memory(WAKE_STATE) as *mut u64, |
|
| 778 | + | cases::WakeStage::AllowAllocation as u64); |
|
| 779 | + | let mut allocation: pages::Allocation = undefined; |
|
| 780 | + | let mut request: ?abi::Ref = nil; |
|
| 781 | + | for attempt in 0..POLL_ATTEMPTS { |
|
| 782 | + | let guard = sync::enter(); |
|
| 783 | + | let index = IDLE_CONTEXT.index; |
|
| 784 | + | let active = state::SHARED.lifecycle.active[ |
|
| 785 | + | index / fixture::CONTEXT_BITS]; |
|
| 786 | + | let mask = (1 as u32) << (index % fixture::CONTEXT_BITS); |
|
| 787 | + | if (active & mask) <> 0 { |
|
| 788 | + | match state::SHARED.lifecycle.records[index].allocation { |
|
| 789 | + | case lifecycle::Allocation::Page(value) => { |
|
| 790 | + | set allocation = value; |
|
| 791 | + | }, |
|
| 792 | + | else => panic "cancelAllocation: page allocation required", |
|
| 793 | + | } |
|
| 794 | + | assert allocation.owner == IDLE_DOMAIN and allocation.run.count |
|
| 795 | + | == cases::ALLOCATION_PAGES; |
|
| 796 | + | sync::leave(guard); |
|
| 797 | + | let result = try! calls::synchronized(CHECKER, |
|
| 798 | + | abi::Operation::DomainDestroy as u32, &[*RECEIVER, 0, 0, |
|
| 799 | + | 0], dispatch::clock()); |
|
| 800 | + | assert result == 0; |
|
| 801 | + | let checking = sync::enter(); |
|
| 802 | + | assert state::SHARED.domains.records[IDLE_DOMAIN.index].state |
|
| 803 | + | == domains::Lifecycle::Dead; |
|
| 804 | + | set request = try! dispatch::request(&mut state::SHARED.remote, 1, |
|
| 805 | + | remote::Action::Reschedule); |
|
| 806 | + | sync::leave(checking); |
|
| 807 | + | break; |
|
| 808 | + | } |
|
| 809 | + | sync::leave(guard); |
|
| 810 | + | } |
|
| 811 | + | let stopped = request |
|
| 812 | + | else panic "cancelAllocation: page call did not reserve frames"; |
|
| 813 | + | let acknowledged = try! dispatch::awaitRequest(0, stopped); |
|
| 814 | + | assert acknowledged == 0; |
|
| 815 | + | let snapshot = sync::enter(); |
|
| 816 | + | let context = try! domains::context(&state::SHARED.domains, |
|
| 817 | + | IDLE_DOMAIN, IDLE_CONTEXT); |
|
| 818 | + | for byte in "smp cancelled page status and progress: " { |
|
| 819 | + | put(byte); |
|
| 820 | + | } |
|
| 821 | + | number(context.frame.status); |
|
| 822 | + | put(32); |
|
| 823 | + | number(sync::loadAcquire(memory(WAKE_STATE) as *u64)); |
|
| 824 | + | put(10); |
|
| 825 | + | assert context.hart == nil and |
|
| 826 | + | context.state == domains::ContextState::Stopped; |
|
| 827 | + | assert (context.frame.status & fixture::MACHINE_PRIVILEGE) == |
|
| 828 | + | fixture::MACHINE_PRIVILEGE; |
|
| 829 | + | assert sync::loadAcquire(memory(WAKE_STATE) as *u64) == |
|
| 830 | + | cases::WakeStage::BeforeAllocation as u64; |
|
| 831 | + | sync::leave(snapshot); |
|
| 832 | + | let mut reclaimed = false; |
|
| 833 | + | for attempt in 0..RECLAIM_ATTEMPTS { |
|
| 834 | + | let checking = sync::enter(); |
|
| 835 | + | set reclaimed = state::SHARED.pages.backings.pool.retiredCount == 0; |
|
| 836 | + | if reclaimed { |
|
| 837 | + | sync::leave(checking); |
|
| 838 | + | break; |
|
| 839 | + | } |
|
| 840 | + | let progress = try! dispatch::request(&mut state::SHARED.remote, 0, |
|
| 841 | + | remote::Action::Reschedule); |
|
| 842 | + | sync::leave(checking); |
|
| 843 | + | let result = try! dispatch::awaitRequest(0, progress); |
|
| 844 | + | assert result == 0; |
|
| 845 | + | } |
|
| 846 | + | assert reclaimed; |
|
| 847 | + | let guard = sync::enter(); |
|
| 848 | + | let active = state::SHARED.lifecycle.active[ |
|
| 849 | + | IDLE_CONTEXT.index / fixture::CONTEXT_BITS]; |
|
| 850 | + | let mask = (1 as u32) << (IDLE_CONTEXT.index % fixture::CONTEXT_BITS); |
|
| 851 | + | assert (active & mask) == 0; |
|
| 852 | + | assert state::SHARED.pages.slots[allocation.page.index].state == |
|
| 853 | + | slots::State::Free; |
|
| 854 | + | let backing = allocation.backing.index; |
|
| 855 | + | assert state::SHARED.pages.backings.slots[backing].state == |
|
| 856 | + | slots::State::Free; |
|
| 857 | + | for i in allocation.run.first..allocation.run.first + allocation.run.count { |
|
| 858 | + | assert frames::available(&state::SHARED.pages.backings.pool, i); |
|
| 859 | + | } |
|
| 860 | + | sync::leave(guard); |
|
| 861 | + | for byte in "smp cancelled preempted page allocation\n" { |
|
| 862 | + | put(byte); |
|
| 863 | + | } |
|
| 864 | + | } |
|
| 865 | + | ||
| 866 | + | /// Wait for dispatch-driven teardown and verify all dead execution ownership is |
|
| 867 | + | /// gone. |
|
| 868 | + | unsafe fn reclaimedDomains() { |
|
| 869 | + | for attempt in 0..POLL_ATTEMPTS { |
|
| 870 | + | let guard = sync::enter(); |
|
| 871 | + | let mut done = state::SHARED.pages.backings.pool.retiredCount == 0; |
|
| 872 | + | for owner in [USER, IDLE_DOMAIN, DESCENDANT] { |
|
| 873 | + | if slots::matches(&state::SHARED.domains.slots[..], owner, |
|
| 874 | + | slots::State::Live) { |
|
| 875 | + | set done = false; |
|
| 876 | + | } |
|
| 877 | + | } |
|
| 878 | + | if done { |
|
| 879 | + | assert state::SHARED.domains.dead == 0; |
|
| 880 | + | for owner in [USER, IDLE_DOMAIN, DESCENDANT] { |
|
| 881 | + | assert state::SHARED.domains.slots[owner.index].state == |
|
| 882 | + | slots::State::Free; |
|
| 883 | + | assert state::SHARED.pages.backings.domains[owner.index] == 0; |
|
| 884 | + | for i in 0..state::SHARED.domains.contextSlots.len { |
|
| 885 | + | if state::SHARED.domains.contextSlots[i].state == |
|
| 886 | + | slots::State::Live { |
|
| 887 | + | assert state::SHARED.domains.contexts[i].owner <> owner; |
|
| 888 | + | } |
|
| 889 | + | } |
|
| 890 | + | for i in 0..state::SHARED.budgets.slots.len { |
|
| 891 | + | if state::SHARED.budgets.slots[i].state == |
|
| 892 | + | slots::State::Live { |
|
| 893 | + | assert state::SHARED.budgets.windows[i].owner <> owner; |
|
| 894 | + | } |
|
| 895 | + | } |
|
| 896 | + | for i in 0..state::SHARED.interrupts.slots.len { |
|
| 897 | + | if state::SHARED.interrupts.slots[i].state == |
|
| 898 | + | slots::State::Live { |
|
| 899 | + | assert state::SHARED.interrupts.records[i].owner <> |
|
| 900 | + | owner; |
|
| 901 | + | } |
|
| 902 | + | } |
|
| 903 | + | } |
|
| 904 | + | sync::leave(guard); |
|
| 905 | + | for byte in "smp reaped dead domains and recovered ownership\n" { |
|
| 906 | + | put(byte); |
|
| 907 | + | } |
|
| 908 | + | return; |
|
| 909 | + | } |
|
| 910 | + | let progress = try! dispatch::request(&mut state::SHARED.remote, 0, |
|
| 911 | + | remote::Action::Reschedule); |
|
| 912 | + | sync::leave(guard); |
|
| 913 | + | let result = try! dispatch::awaitRequest(0, progress); |
|
| 914 | + | assert result == 0; |
|
| 915 | + | } |
|
| 916 | + | panic "reclaimedDomains: domain reaper did not finish"; |
|
| 917 | + | } |
|
| 918 | + | ||
| 919 | + | /// Confirm the waiting context has released hart ownership before its wakeup. |
|
| 920 | + | unsafe fn waiting() -> bool { |
|
| 921 | + | for attempt in 0..POLL_ATTEMPTS { |
|
| 922 | + | let guard = sync::enter(); |
|
| 923 | + | let context = try! domains::context(&state::SHARED.domains, |
|
| 924 | + | IDLE_DOMAIN, IDLE_CONTEXT); |
|
| 925 | + | let stopped = context.state == domains::ContextState::Waiting and |
|
| 926 | + | context.hart == nil; |
|
| 927 | + | sync::leave(guard); |
|
| 928 | + | if stopped { |
|
| 929 | + | return true; |
|
| 930 | + | } |
|
| 931 | + | } |
|
| 932 | + | return false; |
|
| 933 | + | } |
|
| 934 | + | ||
| 935 | + | /// Print one full-width hexadecimal diagnostic value. |
|
| 936 | + | fn number(value: u64) { |
|
| 937 | + | let digits = "0123456789abcdef"; |
|
| 938 | + | for i in 0..16 { |
|
| 939 | + | put(digits[((value >> ((15 - i) as u64 * 4)) & 15) as u32]); |
|
| 940 | + | } |
|
| 941 | + | } |
test/smp/kernel/invariants.rad
added
+82 -0
| 1 | + | //! Final ownership and interval checks over published kernel objects. |
|
| 2 | + | use kernel::state; |
|
| 3 | + | use kernel::abi; |
|
| 4 | + | use kernel::boot; |
|
| 5 | + | use kernel::slots; |
|
| 6 | + | use kernel::capability; |
|
| 7 | + | use kernel::domains; |
|
| 8 | + | use kernel::budgets; |
|
| 9 | + | use kernel::interrupts; |
|
| 10 | + | use kernel::sync; |
|
| 11 | + | ||
| 12 | + | /// Select authoritative capability storage, including the fixture's boot owner. |
|
| 13 | + | /// The bootstrap table and domain memory must outlive the metadata transaction. |
|
| 14 | + | unsafe fn table(owner: abi::Ref, bootstrap: &capability::Table) -> *unsafe capability::Table { |
|
| 15 | + | if owner == bootstrap.owner { |
|
| 16 | + | return bootstrap as *unsafe capability::Table; |
|
| 17 | + | } |
|
| 18 | + | let domain = try! domains::get(&state::SHARED.domains, owner); |
|
| 19 | + | assert domain.state <> domains::Lifecycle::Dead; |
|
| 20 | + | assert domain.memory.table.owner == owner; |
|
| 21 | + | return &domain.memory.table; |
|
| 22 | + | } |
|
| 23 | + | ||
| 24 | + | /// Check live references, exclusive execution ownership, and disjoint CPU windows. |
|
| 25 | + | /// Each budget or IRQ check uses a separate bounded metadata transaction. |
|
| 26 | + | export unsafe fn check(bootstrap: &capability::Table) { |
|
| 27 | + | let guard = sync::enter(); |
|
| 28 | + | let mut occupied: u32 = 0; |
|
| 29 | + | for i in 0..state::SHARED.domains.contextSlots.len { |
|
| 30 | + | if state::SHARED.domains.contextSlots[i].state <> slots::State::Live { |
|
| 31 | + | continue; |
|
| 32 | + | } |
|
| 33 | + | let context = state::SHARED.domains.contexts[i]; |
|
| 34 | + | let owner = try! domains::get(&state::SHARED.domains, context.owner); |
|
| 35 | + | assert owner.state <> domains::Lifecycle::Dead; |
|
| 36 | + | if let hart = context.hart { |
|
| 37 | + | assert hart < 8 and (boot::PLATFORM.harts & (1 << hart)) <> 0; |
|
| 38 | + | assert (occupied & (1 << hart)) == 0; |
|
| 39 | + | set occupied |= 1 << hart; |
|
| 40 | + | } |
|
| 41 | + | } |
|
| 42 | + | sync::leave(guard); |
|
| 43 | + | for i in 0..state::SHARED.budgets.slots.len { |
|
| 44 | + | let guard = sync::enter(); |
|
| 45 | + | if state::SHARED.budgets.slots[i].state == slots::State::Live { |
|
| 46 | + | let object = abi::Ref { index: i, |
|
| 47 | + | generation: state::SHARED.budgets.slots[i].generation }; |
|
| 48 | + | let window = try! budgets::get(&state::SHARED.budgets, object); |
|
| 49 | + | assert window.start < window.end and window.hart < 8; |
|
| 50 | + | assert (boot::PLATFORM.harts & (1 << window.hart)) <> 0; |
|
| 51 | + | let owner = table(window.owner, bootstrap); |
|
| 52 | + | let entry = try! capability::get(&*owner, window.handle); |
|
| 53 | + | assert entry.kind == abi::Kind::Budget and entry.object == object; |
|
| 54 | + | if let context = window.context { |
|
| 55 | + | let bound = try! domains::context(&state::SHARED.domains, window.owner, context); |
|
| 56 | + | assert bound.owner == window.owner; |
|
| 57 | + | } |
|
| 58 | + | for j in i + 1..state::SHARED.budgets.slots.len { |
|
| 59 | + | if state::SHARED.budgets.slots[j].state <> slots::State::Live { |
|
| 60 | + | continue; |
|
| 61 | + | } |
|
| 62 | + | let other = state::SHARED.budgets.windows[j]; |
|
| 63 | + | if other.hart == window.hart { |
|
| 64 | + | assert window.end <= other.start or other.end <= window.start; |
|
| 65 | + | } |
|
| 66 | + | } |
|
| 67 | + | } |
|
| 68 | + | sync::leave(guard); |
|
| 69 | + | } |
|
| 70 | + | for i in 0..state::SHARED.interrupts.count { |
|
| 71 | + | let guard = sync::enter(); |
|
| 72 | + | if state::SHARED.interrupts.slots[i].state == slots::State::Live { |
|
| 73 | + | let object = abi::Ref { index: i, |
|
| 74 | + | generation: state::SHARED.interrupts.slots[i].generation }; |
|
| 75 | + | let irq = try! interrupts::get(&state::SHARED.interrupts, object); |
|
| 76 | + | let owner = table(irq.owner, bootstrap); |
|
| 77 | + | let entry = try! capability::get(&*owner, irq.handle); |
|
| 78 | + | assert entry.kind == abi::Kind::Interrupt and entry.object == object; |
|
| 79 | + | } |
|
| 80 | + | sync::leave(guard); |
|
| 81 | + | } |
|
| 82 | + | } |
test/smp/machine.ras
added
+54 -0
| 1 | + | //! Start all initialized harts after the fixture publishes its shared domains. |
|
| 2 | + | // Machine completion device address. |
|
| 3 | + | .constant SMP_FINISH_ADDRESS 0x10001000; |
|
| 4 | + | // Successful machine completion value. |
|
| 5 | + | .constant SMP_FINISH_PASS 0x5555; |
|
| 6 | + | // Fixture UART register base. |
|
| 7 | + | .constant SMP_UART_ADDRESS 0x10000000; |
|
| 8 | + | // Machine software interrupt-enable bit. |
|
| 9 | + | .constant SMP_SOFTWARE_ENABLE 8; |
|
| 10 | + | // UART interrupt-enable register offset. |
|
| 11 | + | .constant SMP_UART_IRQ_ENABLE 1; |
|
| 12 | + | ||
| 13 | + | .text; |
|
| 14 | + | call @kernel::boot::initialize; |
|
| 15 | + | call @kernel::dispatchcheck::start; |
|
| 16 | + | call @kernel::boot::run; |
|
| 17 | + | ebreak; |
|
| 18 | + | .export @kernel::dispatchcheck::memory; |
|
| 19 | + | .export @kernel::dispatchcheck::kernelGp; |
|
| 20 | + | .export @kernel::dispatchcheck::completion; |
|
| 21 | + | .export @kernel::dispatchcheck::finish; |
|
| 22 | + | .export @kernel::dispatchcheck::put; |
|
| 23 | + | .export @kernel::dispatchcheck::park; |
|
| 24 | + | .export @kernel::dispatchcheck::uartInterrupt; |
|
| 25 | + | @kernel::dispatchcheck::memory |
|
| 26 | + | ret; |
|
| 27 | + | @kernel::dispatchcheck::kernelGp |
|
| 28 | + | mv %a0 %gp; |
|
| 29 | + | ret; |
|
| 30 | + | @kernel::dispatchcheck::completion |
|
| 31 | + | la %a0 @complete; |
|
| 32 | + | ret; |
|
| 33 | + | @complete |
|
| 34 | + | call @kernel::dispatchcheck::verify; |
|
| 35 | + | ebreak; |
|
| 36 | + | @kernel::dispatchcheck::put |
|
| 37 | + | li %t0 SMP_UART_ADDRESS; |
|
| 38 | + | sb %a0 0(%t0); |
|
| 39 | + | ret; |
|
| 40 | + | @kernel::dispatchcheck::uartInterrupt |
|
| 41 | + | li %t0 SMP_UART_ADDRESS; |
|
| 42 | + | fence iorw iorw; |
|
| 43 | + | sb %a0 SMP_UART_IRQ_ENABLE(%t0); |
|
| 44 | + | fence iorw iorw; |
|
| 45 | + | ret; |
|
| 46 | + | @kernel::dispatchcheck::park |
|
| 47 | + | csrsi mie SMP_SOFTWARE_ENABLE; |
|
| 48 | + | wfi; |
|
| 49 | + | ret; |
|
| 50 | + | @kernel::dispatchcheck::finish |
|
| 51 | + | li %t0 SMP_FINISH_ADDRESS; |
|
| 52 | + | li %t1 SMP_FINISH_PASS; |
|
| 53 | + | sw %t1 0(%t0); |
|
| 54 | + | ebreak; |
test/smp/spin.rad
added
+99 -0
| 1 | + | //! Concurrent user contexts with shared private state and separate call |
|
| 2 | + | //! buffers. |
|
| 3 | + | export mod cases; |
|
| 4 | + | export mod abi; |
|
| 5 | + | export mod sys; |
|
| 6 | + | ||
| 7 | + | /// Completed allocation transactions, indexed by the executing hart. |
|
| 8 | + | export unsafe static count: [u64; 8] = [0; 8]; |
|
| 9 | + | ||
| 10 | + | /// Future budget handles assigned to callers on the preceding online hart. |
|
| 11 | + | export unsafe static windows: [u64; 8] = [0; 8]; |
|
| 12 | + | ||
| 13 | + | /// Capability used to notify the shared domain from each online hart. |
|
| 14 | + | export unsafe static notification: u64 = 0; |
|
| 15 | + | ||
| 16 | + | /// Shared-domain capability for the injected physical interrupt source. |
|
| 17 | + | export unsafe static interrupt: u64 = 0; |
|
| 18 | + | ||
| 19 | + | /// Observed destination hart plus one after each context changes budget. |
|
| 20 | + | export unsafe static migrations: [u64; 8] = [0; 8]; |
|
| 21 | + | ||
| 22 | + | /// Progress shared with the machine fixture for waits and interrupted |
|
| 23 | + | /// allocation. |
|
| 24 | + | export unsafe static wakeState: u64 = 0; |
|
| 25 | + | ||
| 26 | + | /// Nonzero selects the independently activated idle-wakeup context. |
|
| 27 | + | export unsafe static idle: u64 = 0; |
|
| 28 | + | ||
| 29 | + | /// Check hart identity and shared-table allocation through repeated direct |
|
| 30 | + | /// calls. |
|
| 31 | + | unsafe fn run(hart: u64) { |
|
| 32 | + | let source = sys::queryInterrupt(abi::Handle(interrupt)); |
|
| 33 | + | assert source.number == cases::IRQ_SOURCE; |
|
| 34 | + | let original = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)); |
|
| 35 | + | assert original.hart == hart; |
|
| 36 | + | let budget = abi::Handle(windows[hart as u32]); |
|
| 37 | + | let before = try! sys::queryBudget(budget); |
|
| 38 | + | let right = try! sys::budgetSplit(budget, before.start + 1000); |
|
| 39 | + | let leftInfo = try! sys::queryBudget(budget); |
|
| 40 | + | let rightInfo = try! sys::queryBudget(right); |
|
| 41 | + | assert leftInfo.start == before.start and leftInfo.end == rightInfo.start; |
|
| 42 | + | assert rightInfo.end == before.end and rightInfo.hart == before.hart; |
|
| 43 | + | let merged = try! sys::budgetMerge(budget, right); |
|
| 44 | + | assert merged == budget; |
|
| 45 | + | let after = try! sys::queryBudget(merged); |
|
| 46 | + | assert after.start == before.start and after.end == before.end; |
|
| 47 | + | try! sys::capabilityDrop(merged); |
|
| 48 | + | try! sys::wakeup(abi::Handle(notification), hart as u32); |
|
| 49 | + | for i in 0 .. cases::TRANSACTIONS { |
|
| 50 | + | let page = try! sys::pageAllocate(abi::Handle(0), 1); |
|
| 51 | + | let info = sys::queryPage(page); |
|
| 52 | + | assert info.count == 1 and info.base <> 0; |
|
| 53 | + | try! sys::capabilityDrop(page); |
|
| 54 | + | let current = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)); |
|
| 55 | + | assert current.context == original.context and current.hart == hart; |
|
| 56 | + | set count[hart as u32] += 1; |
|
| 57 | + | } |
|
| 58 | + | while true { |
|
| 59 | + | let current = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)); |
|
| 60 | + | assert current.context == original.context; |
|
| 61 | + | if current.budget <> original.budget { |
|
| 62 | + | set migrations[hart as u32] = current.hart + 1; |
|
| 63 | + | } |
|
| 64 | + | } |
|
| 65 | + | } |
|
| 66 | + | ||
| 67 | + | /// Start an additional context with its physical hart index as a data argument. |
|
| 68 | + | export unsafe fn worker(hart: u64, size: u64) { |
|
| 69 | + | assert size == 8; |
|
| 70 | + | run(hart); |
|
| 71 | + | } |
|
| 72 | + | ||
| 73 | + | /// The initial context owns hart zero's window. |
|
| 74 | + | @default unsafe fn main(env: *abi::Env) { |
|
| 75 | + | if idle <> 0 { |
|
| 76 | + | set wakeState = cases::WakeStage::BeforeWakeWait as u64; |
|
| 77 | + | sys::wait(); |
|
| 78 | + | set wakeState = cases::WakeStage::WakeResumed as u64; |
|
| 79 | + | while wakeState <> cases::WakeStage::AllowTimeout as u64 { |
|
| 80 | + | } |
|
| 81 | + | set wakeState = cases::WakeStage::BeforeTimeoutWait as u64; |
|
| 82 | + | sys::wait(); |
|
| 83 | + | set wakeState = cases::WakeStage::TimeoutResumed as u64; |
|
| 84 | + | while wakeState <> cases::WakeStage::AllowTerminalWait as u64 { |
|
| 85 | + | } |
|
| 86 | + | set wakeState = cases::WakeStage::BeforeTerminalWait as u64; |
|
| 87 | + | sys::wait(); |
|
| 88 | + | set wakeState = cases::WakeStage::TerminalResumed as u64; |
|
| 89 | + | while wakeState <> cases::WakeStage::AllowAllocation as u64 { |
|
| 90 | + | } |
|
| 91 | + | set wakeState = cases::WakeStage::BeforeAllocation as u64; |
|
| 92 | + | let allocation = try! sys::pageAllocate(abi::Handle(0), |
|
| 93 | + | cases::ALLOCATION_PAGES as u64); |
|
| 94 | + | set wakeState = cases::WakeStage::AllocationComplete as u64; |
|
| 95 | + | while true { |
|
| 96 | + | } |
|
| 97 | + | } |
|
| 98 | + | run(0); |
|
| 99 | + | } |
test/sync/build.rad
added
+65 -0
| 1 | + | //! Build a native image from the synchronization machine fixture. |
|
| 2 | + | ||
| 3 | + | use std::sys; |
|
| 4 | + | use std::io; |
|
| 5 | + | use std::sys::unix; |
|
| 6 | + | use std::lang::alloc; |
|
| 7 | + | use std::lang::strings; |
|
| 8 | + | use std::arch::rv64::asm; |
|
| 9 | + | use std::arch::rv64::image; |
|
| 10 | + | use std::arch::rv64; |
|
| 11 | + | ||
| 12 | + | /// Synchronization fixture code base and entry address. |
|
| 13 | + | constant CODE_BASE: u64 = 0x80010000; |
|
| 14 | + | /// Synchronization fixture writable data base. |
|
| 15 | + | constant RW_BASE: u64 = 0x80020000; |
|
| 16 | + | /// Bytes reserved for synchronization fixture data. |
|
| 17 | + | constant RW_SIZE: u32 = 4096; |
|
| 18 | + | ||
| 19 | + | /// Number of elements reserved for source storage. |
|
| 20 | + | constant SOURCE_CAPACITY: u32 = 65536; |
|
| 21 | + | /// Number of elements reserved for text storage. |
|
| 22 | + | constant TEXT_CAPACITY: u32 = 4096; |
|
| 23 | + | /// Number of elements reserved for memory storage. |
|
| 24 | + | constant MEMORY_CAPACITY: u32 = 16777216; |
|
| 25 | + | ||
| 26 | + | /// Assembly input storage. |
|
| 27 | + | static SOURCE: [u8; SOURCE_CAPACITY] = [0; SOURCE_CAPACITY]; |
|
| 28 | + | /// Encoded text words. |
|
| 29 | + | static TEXT: [u32; TEXT_CAPACITY] = [0; TEXT_CAPACITY]; |
|
| 30 | + | /// Assembly name and fixup storage. |
|
| 31 | + | static MEMORY: [u8; MEMORY_CAPACITY] = [0; MEMORY_CAPACITY]; |
|
| 32 | + | /// Slots of the interned assembly identifiers. |
|
| 33 | + | unsafe static TABLE: [*[u8]; strings::TABLE_SIZE] = undefined; |
|
| 34 | + | ||
| 35 | + | /// Assemble startup and synchronization routines into one native code segment. |
|
| 36 | + | @default unsafe fn main(env: *sys::Env) -> i32 { |
|
| 37 | + | assert env.args.len == 3; |
|
| 38 | + | let length = unix::readFile(env.args[1], &mut SOURCE[..]) else panic "main: missing assembly"; |
|
| 39 | + | let mut arena = alloc::new(&mut MEMORY[..]); |
|
| 40 | + | let mut pool = strings::pool(&mut TABLE[..]); |
|
| 41 | + | let data: *mut [u8] = &mut []; |
|
| 42 | + | let program = try asm::assemble( |
|
| 43 | + | asm::scanner::SourceKind::File { path: env.args[1] }, &SOURCE[..length], |
|
| 44 | + | &mut TEXT[..], data, &mut arena, &mut pool, 0) catch err { |
|
| 45 | + | asm::printError(err, &env.args[1][..], &SOURCE[..length]); |
|
| 46 | + | return 1; |
|
| 47 | + | }; |
|
| 48 | + | assert program.externalFixups.len == 0; |
|
| 49 | + | let size = program.text.len * rv64::INSTR_SIZE as u32; |
|
| 50 | + | let header = try! image::header(image::Layout { |
|
| 51 | + | code: image::Segment { address: CODE_BASE, size }, |
|
| 52 | + | roData: image::Segment { address: 0, size: 0 }, |
|
| 53 | + | rwData: image::Segment { address: RW_BASE, size: 0 }, |
|
| 54 | + | rwTail: RW_SIZE, |
|
| 55 | + | }); |
|
| 56 | + | let fd = unix::openOpts(env.args[2], |
|
| 57 | + | unix::OpenFlags(*unix::O_WRONLY | *unix::O_CREAT | *unix::O_TRUNC), |
|
| 58 | + | unix::FILE_MODE); |
|
| 59 | + | assert fd >= 0; |
|
| 60 | + | let written = unix::writeAll(fd, &header[..]) and unix::writeAll(fd, |
|
| 61 | + | @sliceOf(program.text.ptr as *u8, size)); |
|
| 62 | + | let closed = unix::close(fd) == 0; |
|
| 63 | + | assert written and closed; |
|
| 64 | + | return 0; |
|
| 65 | + | } |
test/sync/machine.ras
added
+136 -0
| 1 | + | //! Exercise code publication and contended metadata updates on each hart. |
|
| 2 | + | .constant EXPECTED HARTS * 100; |
|
| 3 | + | .text; |
|
| 4 | + | @entry |
|
| 5 | + | li %t0 0x1888; |
|
| 6 | + | csrw mstatus %t0; |
|
| 7 | + | csrr %s7 mstatus; |
|
| 8 | + | call @kernel::sync::maskInterrupts; |
|
| 9 | + | li %t0 8; |
|
| 10 | + | bne %a0 %t0 @fail; |
|
| 11 | + | csrr %t0 mstatus; |
|
| 12 | + | andi %t1 %s7 -9; |
|
| 13 | + | bne %t0 %t1 @fail; |
|
| 14 | + | call @kernel::sync::restoreInterrupts; |
|
| 15 | + | csrr %t0 mstatus; |
|
| 16 | + | bne %t0 %s7 @fail; |
|
| 17 | + | li %a0 0; |
|
| 18 | + | call @kernel::sync::restoreInterrupts; |
|
| 19 | + | csrr %t0 mstatus; |
|
| 20 | + | andi %t1 %s7 -9; |
|
| 21 | + | bne %t0 %t1 @fail; |
|
| 22 | + | csrr %s6 mhartid; |
|
| 23 | + | li %s0 0x80020000; |
|
| 24 | + | bnez %s6 @waitCode; |
|
| 25 | + | li %t0 0x100000000-4; |
|
| 26 | + | sw %t0 0(%s0); |
|
| 27 | + | sw %t0 4(%s0); |
|
| 28 | + | li %t0 0x02a00513; |
|
| 29 | + | sw %t0 64(%s0); |
|
| 30 | + | li %t0 0x00008067; |
|
| 31 | + | sw %t0 68(%s0); |
|
| 32 | + | li %t0 37; |
|
| 33 | + | sw %t0 44(%s0); |
|
| 34 | + | addi %a0 %s0 40; |
|
| 35 | + | li %a1 -1; |
|
| 36 | + | call @kernel::sync::storeRelease32; |
|
| 37 | + | addi %a0 %s0 24; |
|
| 38 | + | li %a1 1; |
|
| 39 | + | call @kernel::sync::storeRelease; |
|
| 40 | + | @waitCode |
|
| 41 | + | addi %a0 %s0 24; |
|
| 42 | + | call @kernel::sync::loadAcquire; |
|
| 43 | + | beqz %a0 @waitCode; |
|
| 44 | + | addi %a0 %s0 40; |
|
| 45 | + | call @kernel::sync::loadAcquire32; |
|
| 46 | + | li %t0 0xffffffff; |
|
| 47 | + | bne %a0 %t0 @fail; |
|
| 48 | + | lwu %t0 44(%s0); |
|
| 49 | + | li %t1 37; |
|
| 50 | + | bne %t0 %t1 @fail; |
|
| 51 | + | call @kernel::sync::syncInstructions; |
|
| 52 | + | addi %t0 %s0 64; |
|
| 53 | + | jalr %ra %t0 0; |
|
| 54 | + | li %t0 42; |
|
| 55 | + | bne %a0 %t0 @fail; |
|
| 56 | + | li %s1 100; |
|
| 57 | + | @ticket |
|
| 58 | + | mv %a0 %s0; |
|
| 59 | + | call @kernel::sync::nextTicket; |
|
| 60 | + | mv %s2 %a0; |
|
| 61 | + | li %t0 100; |
|
| 62 | + | bne %s1 %t0 @acquire; |
|
| 63 | + | // Every hart holds its first ticket before any hart can enter the lock. |
|
| 64 | + | addi %a0 %s0 72; |
|
| 65 | + | li %a1 1; |
|
| 66 | + | call @kernel::sync::fetchAdd; |
|
| 67 | + | @queued |
|
| 68 | + | addi %a0 %s0 72; |
|
| 69 | + | call @kernel::sync::loadAcquire; |
|
| 70 | + | li %t0 HARTS; |
|
| 71 | + | bne %a0 %t0 @queued; |
|
| 72 | + | @acquire |
|
| 73 | + | addi %a0 %s0 4; |
|
| 74 | + | call @kernel::sync::loadAcquire32; |
|
| 75 | + | bne %a0 %s2 @acquire; |
|
| 76 | + | // The protected count must follow ticket order across the u32 wrap. |
|
| 77 | + | addi %t1 %s2 4; |
|
| 78 | + | slli %t1 %t1 32; |
|
| 79 | + | srli %t1 %t1 32; |
|
| 80 | + | ld %t0 8(%s0); |
|
| 81 | + | bne %t0 %t1 @fail; |
|
| 82 | + | addi %t0 %t0 1; |
|
| 83 | + | sd %t0 8(%s0); |
|
| 84 | + | addi %a0 %s0 4; |
|
| 85 | + | addi %a1 %s2 1; |
|
| 86 | + | call @kernel::sync::storeRelease32; |
|
| 87 | + | addi %s1 %s1 -1; |
|
| 88 | + | bnez %s1 @ticket; |
|
| 89 | + | addi %a0 %s0 16; |
|
| 90 | + | li %a1 1; |
|
| 91 | + | call @kernel::sync::fetchAdd; |
|
| 92 | + | addi %a0 %a0 1; |
|
| 93 | + | li %t0 HARTS; |
|
| 94 | + | beq %a0 %t0 @check; |
|
| 95 | + | @park |
|
| 96 | + | wfi; |
|
| 97 | + | j @park; |
|
| 98 | + | @check |
|
| 99 | + | ld %t0 8(%s0); |
|
| 100 | + | li %t1 EXPECTED; |
|
| 101 | + | bne %t0 %t1 @fail; |
|
| 102 | + | lwu %t0 0(%s0); |
|
| 103 | + | lwu %t2 4(%s0); |
|
| 104 | + | bne %t0 %t2 @fail; |
|
| 105 | + | addi %t1 %t1 -4; |
|
| 106 | + | bne %t0 %t1 @fail; |
|
| 107 | + | addi %t2 %s0 32; |
|
| 108 | + | lr.w.aq %t0 (%t2); |
|
| 109 | + | bnez %t0 @fail; |
|
| 110 | + | li %t1 13; |
|
| 111 | + | sc.w.rl %t0 %t1 (%t2); |
|
| 112 | + | bnez %t0 @fail; |
|
| 113 | + | lr.d.aq %t0 (%t2); |
|
| 114 | + | bne %t0 %t1 @fail; |
|
| 115 | + | sc.d.rl %t0 %zero (%t2); |
|
| 116 | + | bnez %t0 @fail; |
|
| 117 | + | li %a0 0x02000000; |
|
| 118 | + | fence iorw iorw; |
|
| 119 | + | lwu %a0 0(%a0); |
|
| 120 | + | fence iorw iorw; |
|
| 121 | + | bnez %a0 @fail; |
|
| 122 | + | j @success; |
|
| 123 | + | @fail |
|
| 124 | + | li %a0 0x10001000; |
|
| 125 | + | li %a1 0x13333; |
|
| 126 | + | fence iorw iorw; |
|
| 127 | + | sw %a1 0(%a0); |
|
| 128 | + | fence iorw iorw; |
|
| 129 | + | ebreak; |
|
| 130 | + | @success |
|
| 131 | + | li %a0 0x10001000; |
|
| 132 | + | li %a1 0x5555; |
|
| 133 | + | fence iorw iorw; |
|
| 134 | + | sw %a1 0(%a0); |
|
| 135 | + | fence iorw iorw; |
|
| 136 | + | ebreak; |
test/sync/run
added
+13 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Execute the synchronization boundary under deterministic hart interleaving. |
|
| 3 | + | set -eu |
|
| 4 | + | emulator=${RAD_EMULATOR:-emulator} |
|
| 5 | + | work=$(mktemp -d) |
|
| 6 | + | trap 'rm -rf "$work"' EXIT HUP INT TERM |
|
| 7 | + | for harts in 1 2 8; do |
|
| 8 | + | printf '.constant HARTS %s;\n' "$harts" > "$work/sync.ras" |
|
| 9 | + | cat test/sync/machine.ras kernel/kernel/sync.ras >> "$work/sync.ras" |
|
| 10 | + | "$emulator" -run test/sync/build.rv64 -- "$work/sync.ras" "$work/sync.rv64" |
|
| 11 | + | "$emulator" -machine -harts="$harts" -run "$work/sync.rv64" |
|
| 12 | + | done |
|
| 13 | + | printf 'synchronization: code publication, locks, atomics, and MMIO passed on 1/2/8 harts\n' |
test/termination/cases.rad
added
+26 -0
| 1 | + | //! User termination scenarios shared with the kernel checker. |
|
| 2 | + | ||
| 3 | + | /// Operation selected by the parent through the argument page. |
|
| 4 | + | export union Scenario: Copy { |
|
| 5 | + | /// Exit without an argument page. |
|
| 6 | + | Exit = 0, |
|
| 7 | + | /// Abort through the public syscall wrapper. |
|
| 8 | + | Abort = 1, |
|
| 9 | + | /// Raise a user breakpoint through a failed assertion. |
|
| 10 | + | Breakpoint = 2, |
|
| 11 | + | /// Destroy the current domain. |
|
| 12 | + | DestroySelf = 3, |
|
| 13 | + | /// Call the raw query boundary with an invalid self handle. |
|
| 14 | + | InvalidSelfQuery = 4, |
|
| 15 | + | /// Query the installed finish device. |
|
| 16 | + | DeviceQuery = 8, |
|
| 17 | + | } |
|
| 18 | + | ||
| 19 | + | /// Status returned by the child with no argument page. |
|
| 20 | + | export constant EXIT_STATUS: u32 = 37; |
|
| 21 | + | /// Status returned after a successful device query. |
|
| 22 | + | export constant DEVICE_STATUS: u32 = 51; |
|
| 23 | + | /// RISC-V breakpoint exception code reported to the parent. |
|
| 24 | + | export constant BREAKPOINT_CAUSE: u32 = 3; |
|
| 25 | + | /// Emulator finish-device physical address. |
|
| 26 | + | export constant FINISH_ADDRESS: u64 = 0x10001000; |
test/termination/kernel/dispatchcheck.rad
added
+142 -0
| 1 | + | //! Native exit and fault reporting to a surviving parent. |
|
| 2 | + | export mod inputs; |
|
| 3 | + | export mod cases; |
|
| 4 | + | use kernel::fixture; |
|
| 5 | + | use kernel::state; |
|
| 6 | + | use kernel::abi; |
|
| 7 | + | use kernel::pages; |
|
| 8 | + | use kernel::capability; |
|
| 9 | + | use kernel::domains; |
|
| 10 | + | use kernel::dispatch; |
|
| 11 | + | use kernel::boot; |
|
| 12 | + | use kernel::events; |
|
| 13 | + | use kernel::sync; |
|
| 14 | + | use kernel::devices; |
|
| 15 | + | use kernel::platform; |
|
| 16 | + | ||
| 17 | + | /// Number of child scenarios, in CPU-window order. |
|
| 18 | + | constant COUNT: u32 = 6; |
|
| 19 | + | /// Delay before the first child window, in machine timer ticks. |
|
| 20 | + | constant START_DELAY: u64 = 1000000; |
|
| 21 | + | /// Duration of each child and checker window, in machine timer ticks. |
|
| 22 | + | constant WINDOW_TICKS: u64 = 2000000; |
|
| 23 | + | ||
| 24 | + | /// One stimulus and its expected terminal event. |
|
| 25 | + | record Scenario: Copy { |
|
| 26 | + | /// User action and capability configuration. |
|
| 27 | + | scenario: cases::Scenario, |
|
| 28 | + | /// Expected lifecycle event kind. |
|
| 29 | + | kind: u16, |
|
| 30 | + | /// Expected exit status or fault code. |
|
| 31 | + | code: u32, |
|
| 32 | + | } |
|
| 33 | + | ||
| 34 | + | /// Scenarios in the order in which their CPU windows execute. |
|
| 35 | + | static SCENARIOS: [Scenario; COUNT] = [ |
|
| 36 | + | Scenario { scenario: cases::Scenario::Exit, |
|
| 37 | + | kind: events::CHILD_EXIT, code: cases::EXIT_STATUS }, |
|
| 38 | + | Scenario { scenario: cases::Scenario::Abort, |
|
| 39 | + | kind: events::FAULT, code: abi::FAULT_ABORT }, |
|
| 40 | + | Scenario { scenario: cases::Scenario::Breakpoint, |
|
| 41 | + | kind: events::FAULT, code: cases::BREAKPOINT_CAUSE }, |
|
| 42 | + | Scenario { scenario: cases::Scenario::DestroySelf, |
|
| 43 | + | kind: events::CHILD_EXIT, code: 0 }, |
|
| 44 | + | Scenario { scenario: cases::Scenario::InvalidSelfQuery, |
|
| 45 | + | kind: events::FAULT, code: abi::FAULT_ABORT }, |
|
| 46 | + | Scenario { scenario: cases::Scenario::DeviceQuery, |
|
| 47 | + | kind: events::CHILD_EXIT, code: cases::DEVICE_STATUS }, |
|
| 48 | + | ]; |
|
| 49 | + | ||
| 50 | + | // State retained across setup and verification is initialized before |
|
| 51 | + | // dispatch starts. The scheduled checker reads it after the user workload. |
|
| 52 | + | /// Bootstrap creation, allocation, and budget authority. |
|
| 53 | + | unsafe static TABLE: capability::Table = undefined; |
|
| 54 | + | /// Child domain identities retained by terminal events. |
|
| 55 | + | unsafe static CHILDREN: [fixture::Actor; COUNT] = undefined; |
|
| 56 | + | /// Surviving event receiver and native completion checker. |
|
| 57 | + | unsafe static PARENT: fixture::Actor = undefined; |
|
| 58 | + | /// Map a validated physical page. |
|
| 59 | + | fn memory(address: u64) -> *mut u8; |
|
| 60 | + | /// Current kernel package-state base. |
|
| 61 | + | fn kernelGp() -> u64; |
|
| 62 | + | /// Native entry for the completion checker. |
|
| 63 | + | fn completion() -> u64; |
|
| 64 | + | /// Signal successful completion to the machine fixture. |
|
| 65 | + | fn finish(); |
|
| 66 | + | ||
| 67 | + | /// Give each child a disjoint window, followed by the surviving parent. |
|
| 68 | + | export unsafe fn setup() { |
|
| 69 | + | let authority = fixture::bootstrap(&mut TABLE); |
|
| 70 | + | let image = fixture::load(&mut TABLE, authority, &inputs::INPUT[..]); |
|
| 71 | + | set PARENT = fixture::create(&mut TABLE, authority, image); |
|
| 72 | + | let parentHandle = PARENT.handle; |
|
| 73 | + | fixture::machine(PARENT.domain, completion(), kernelGp()); |
|
| 74 | + | for i in 0..COUNT { |
|
| 75 | + | let actor = fixture::create(&mut TABLE, authority, image); |
|
| 76 | + | let handle = actor.handle; |
|
| 77 | + | let object = actor.domain; |
|
| 78 | + | let mut child = try! domains::get(&state::SHARED.domains, object); |
|
| 79 | + | let scenario = SCENARIOS[i].scenario; |
|
| 80 | + | set CHILDREN[i] = actor; |
|
| 81 | + | try! domains::reparent(&mut state::SHARED.domains, &TABLE, handle, |
|
| 82 | + | parentHandle); |
|
| 83 | + | let moved = fixture::stack(&mut TABLE, authority, |
|
| 84 | + | &mut child.memory.table); |
|
| 85 | + | // The Exit scenario has no arguments. The others read a case and a |
|
| 86 | + | // device handle. |
|
| 87 | + | let mut args = abi::Handle(0); |
|
| 88 | + | let mut size: u64 = 0; |
|
| 89 | + | if scenario <> cases::Scenario::Exit { |
|
| 90 | + | let page = try! pages::allocate(&mut state::SHARED.pages, |
|
| 91 | + | &mut TABLE, authority, 1); |
|
| 92 | + | let storage = try! pages::get(&state::SHARED.pages, |
|
| 93 | + | (try! capability::get(&TABLE, page)).object); |
|
| 94 | + | let words = @sliceOf(memory(storage.base) as *mut u64, 2); |
|
| 95 | + | set words[0] = scenario as u64; |
|
| 96 | + | if scenario == cases::Scenario::DeviceQuery { |
|
| 97 | + | let mut installed = false; |
|
| 98 | + | for index in 0..state::SHARED.devices.count { |
|
| 99 | + | if state::SHARED.devices.regions[index].kind <> |
|
| 100 | + | platform::Kind::Finish { |
|
| 101 | + | continue; |
|
| 102 | + | } |
|
| 103 | + | let device = try! devices::install(&state::SHARED.devices, |
|
| 104 | + | &mut child.memory.table, index); |
|
| 105 | + | set words[1] = *device; |
|
| 106 | + | set installed = true; |
|
| 107 | + | break; |
|
| 108 | + | } |
|
| 109 | + | assert installed; |
|
| 110 | + | } |
|
| 111 | + | set args = try! pages::grant(&mut state::SHARED.pages, &TABLE, |
|
| 112 | + | &mut child.memory.table, page, abi::READ as u64); |
|
| 113 | + | set size = 16; |
|
| 114 | + | } |
|
| 115 | + | try! fixture::activate(&TABLE, handle, moved, args, size); |
|
| 116 | + | } |
|
| 117 | + | let timer = try! dispatch::timer(&boot::PLATFORM, 0); |
|
| 118 | + | let clock = dispatch::now(timer); |
|
| 119 | + | fixture::schedule(&mut TABLE, &CHILDREN[..], PARENT, |
|
| 120 | + | clock + START_DELAY, WINDOW_TICKS, clock); |
|
| 121 | + | } |
|
| 122 | + | ||
| 123 | + | /// Check each terminal kind, status, and generation while the parent continues |
|
| 124 | + | /// to run. |
|
| 125 | + | export unsafe fn verify() { |
|
| 126 | + | let guard = sync::enter(); |
|
| 127 | + | let mut parent = try! domains::get(&state::SHARED.domains, PARENT.domain); |
|
| 128 | + | for scenario, i in &SCENARIOS[..] { |
|
| 129 | + | let actor = CHILDREN[i]; |
|
| 130 | + | let event = try! events::pop(&mut parent.memory.ring) |
|
| 131 | + | else panic "verify: missing termination event"; |
|
| 132 | + | assert event.value == abi::id(actor.domain); |
|
| 133 | + | assert event.code == scenario.code and event.kind == scenario.kind; |
|
| 134 | + | assert state::SHARED.domains.records[actor.domain.index].state == |
|
| 135 | + | domains::Lifecycle::Dead; |
|
| 136 | + | } |
|
| 137 | + | try! events::refresh(&mut state::SHARED.domains.events, PARENT.domain, |
|
| 138 | + | &parent.memory.ring); |
|
| 139 | + | assert try! events::pop(&mut parent.memory.ring) == nil; |
|
| 140 | + | sync::leave(guard); |
|
| 141 | + | finish(); |
|
| 142 | + | } |
test/termination/machine.ras
added
+30 -0
| 1 | + | //! Boot one hart and check user termination through production dispatch. |
|
| 2 | + | // Machine completion device address. |
|
| 3 | + | .constant TERMINATION_FINISH_ADDRESS 0x10001000; |
|
| 4 | + | // Successful machine completion value. |
|
| 5 | + | .constant TERMINATION_FINISH_PASS 0x5555; |
|
| 6 | + | ||
| 7 | + | .text; |
|
| 8 | + | call @kernel::boot::initialize; |
|
| 9 | + | call @kernel::dispatchcheck::setup; |
|
| 10 | + | call @kernel::boot::run; |
|
| 11 | + | .export @kernel::dispatchcheck::memory; |
|
| 12 | + | .export @kernel::dispatchcheck::kernelGp; |
|
| 13 | + | .export @kernel::dispatchcheck::completion; |
|
| 14 | + | .export @kernel::dispatchcheck::finish; |
|
| 15 | + | @kernel::dispatchcheck::memory |
|
| 16 | + | ret; |
|
| 17 | + | @kernel::dispatchcheck::kernelGp |
|
| 18 | + | mv %a0 %gp; |
|
| 19 | + | ret; |
|
| 20 | + | @kernel::dispatchcheck::completion |
|
| 21 | + | la %a0 @complete; |
|
| 22 | + | ret; |
|
| 23 | + | @complete |
|
| 24 | + | call @kernel::dispatchcheck::verify; |
|
| 25 | + | ebreak; |
|
| 26 | + | @kernel::dispatchcheck::finish |
|
| 27 | + | li %t0 TERMINATION_FINISH_ADDRESS; |
|
| 28 | + | li %t1 TERMINATION_FINISH_PASS; |
|
| 29 | + | sw %t1 0(%t0); |
|
| 30 | + | ebreak; |
test/termination/spin.rad
added
+34 -0
| 1 | + | //! User termination and CPU-fault entry paths. |
|
| 2 | + | export mod cases; |
|
| 3 | + | export mod abi; |
|
| 4 | + | export mod sys; |
|
| 5 | + | ||
| 6 | + | /// Issue a raw call to test the kernel error boundary. |
|
| 7 | + | @intrinsic fn ecall(operation: u32, a0: i64, a1: i64, a2: i64, a3: i64) -> i64; |
|
| 8 | + | ||
| 9 | + | /// Exercise an explicit exit, explicit abort, or a user breakpoint fault. The |
|
| 10 | + | /// parent selects the case with the first word of the arguments Page, and no |
|
| 11 | + | /// Page selects exit. The second word holds the device handle of the last case. |
|
| 12 | + | @default fn main(env: *abi::Env) { |
|
| 13 | + | if env.argsSize == 0 { |
|
| 14 | + | sys::exit(cases::EXIT_STATUS); |
|
| 15 | + | } |
|
| 16 | + | let words = try! sys::pageWords(abi::Handle(env.args)); |
|
| 17 | + | let mode = words[0]; |
|
| 18 | + | if mode == cases::Scenario::Abort as u64 { |
|
| 19 | + | sys::abort(); |
|
| 20 | + | } |
|
| 21 | + | if mode == cases::Scenario::DestroySelf as u64 { |
|
| 22 | + | try! sys::domainDestroy(abi::Handle(0), 0); |
|
| 23 | + | } |
|
| 24 | + | if mode == cases::Scenario::InvalidSelfQuery as u64 { |
|
| 25 | + | let result = ecall(abi::Operation::QueryDomain as u32, 0, 0, 0, 0); |
|
| 26 | + | sys::exit(99); |
|
| 27 | + | } |
|
| 28 | + | if mode == cases::Scenario::DeviceQuery as u64 { |
|
| 29 | + | let device = sys::queryDevice(abi::Handle(words[1])); |
|
| 30 | + | assert device.base == cases::FINISH_ADDRESS and device.size >= 4; |
|
| 31 | + | sys::exit(cases::DEVICE_STATUS); |
|
| 32 | + | } |
|
| 33 | + | assert false; |
|
| 34 | + | } |
test/trap/machine.ras
added
+345 -0
| 1 | + | //! Machine and user trap entry with a nested machine timer interrupt. |
|
| 2 | + | // Machine completion device address. |
|
| 3 | + | .constant TRAP_TEST_FINISH_ADDRESS 0x10001000; |
|
| 4 | + | // Successful machine completion value. |
|
| 5 | + | .constant TRAP_TEST_FINISH_PASS 0x5555; |
|
| 6 | + | // Failed machine completion value. |
|
| 7 | + | .constant TRAP_TEST_FINISH_FAIL 0x13333; |
|
| 8 | + | // Previous machine privilege field in mstatus. |
|
| 9 | + | .constant TRAP_TEST_MPP_MACHINE 0x1800; |
|
| 10 | + | // Hart zero timer compare register address. |
|
| 11 | + | .constant TRAP_TEST_TIMER_COMPARE 0x02004000; |
|
| 12 | + | // Machine interrupt-enable bit in mstatus. |
|
| 13 | + | .constant TRAP_TEST_MIE 8; |
|
| 14 | + | // Machine timer interrupt-enable bit. |
|
| 15 | + | .constant TRAP_TEST_TIMER_ENABLE 128; |
|
| 16 | + | // Machine timer interrupt cause code. |
|
| 17 | + | .constant TRAP_TEST_TIMER_IRQ 7; |
|
| 18 | + | // User environment call exception code. |
|
| 19 | + | .constant TRAP_TEST_USER_CALL 8; |
|
| 20 | + | // Machine environment call exception code. |
|
| 21 | + | .constant TRAP_TEST_MACHINE_CALL 11; |
|
| 22 | + | // Illegal instruction exception code. |
|
| 23 | + | .constant TRAP_TEST_ILLEGAL_INSTRUCTION 2; |
|
| 24 | + | ||
| 25 | + | .text; |
|
| 26 | + | li %s0 0x80020000; |
|
| 27 | + | sd %sp 0(%s0); |
|
| 28 | + | li %t0 4096; |
|
| 29 | + | sub %t0 %sp %t0; |
|
| 30 | + | sd %t0 8(%s0); |
|
| 31 | + | li %t0 0x12345678; |
|
| 32 | + | sd %t0 16(%s0); |
|
| 33 | + | la %t0 @handler; |
|
| 34 | + | sd %t0 24(%s0); |
|
| 35 | + | mv %a0 %s0; |
|
| 36 | + | call @kernel::trap::install; |
|
| 37 | + | li %ra 17; |
|
| 38 | + | li %gp 51; |
|
| 39 | + | li %tp 68; |
|
| 40 | + | li %t0 85; |
|
| 41 | + | li %t1 102; |
|
| 42 | + | li %t2 119; |
|
| 43 | + | li %s1 153; |
|
| 44 | + | li %a0 170; |
|
| 45 | + | li %a1 187; |
|
| 46 | + | li %a2 204; |
|
| 47 | + | li %a3 221; |
|
| 48 | + | li %a4 238; |
|
| 49 | + | li %a5 255; |
|
| 50 | + | li %a6 272; |
|
| 51 | + | li %a7 289; |
|
| 52 | + | li %s2 306; |
|
| 53 | + | li %s3 323; |
|
| 54 | + | li %s4 340; |
|
| 55 | + | li %s5 357; |
|
| 56 | + | li %s6 374; |
|
| 57 | + | li %s7 391; |
|
| 58 | + | li %s8 408; |
|
| 59 | + | li %s9 425; |
|
| 60 | + | li %s10 442; |
|
| 61 | + | li %s11 459; |
|
| 62 | + | li %t3 476; |
|
| 63 | + | li %t4 493; |
|
| 64 | + | li %t5 510; |
|
| 65 | + | li %t6 527; |
|
| 66 | + | ecall; |
|
| 67 | + | addi %t0 %t0 -85; |
|
| 68 | + | bnez %t0 @fail; |
|
| 69 | + | li %t0 17; |
|
| 70 | + | bne %ra %t0 @fail; |
|
| 71 | + | li %t0 51; |
|
| 72 | + | bne %gp %t0 @fail; |
|
| 73 | + | li %t0 68; |
|
| 74 | + | bne %tp %t0 @fail; |
|
| 75 | + | li %t0 102; |
|
| 76 | + | bne %t1 %t0 @fail; |
|
| 77 | + | li %t0 119; |
|
| 78 | + | bne %t2 %t0 @fail; |
|
| 79 | + | li %t0 153; |
|
| 80 | + | bne %s1 %t0 @fail; |
|
| 81 | + | li %t0 170; |
|
| 82 | + | bne %a0 %t0 @fail; |
|
| 83 | + | li %t0 187; |
|
| 84 | + | bne %a1 %t0 @fail; |
|
| 85 | + | li %t0 204; |
|
| 86 | + | bne %a2 %t0 @fail; |
|
| 87 | + | li %t0 221; |
|
| 88 | + | bne %a3 %t0 @fail; |
|
| 89 | + | li %t0 238; |
|
| 90 | + | bne %a4 %t0 @fail; |
|
| 91 | + | li %t0 255; |
|
| 92 | + | bne %a5 %t0 @fail; |
|
| 93 | + | li %t0 272; |
|
| 94 | + | bne %a6 %t0 @fail; |
|
| 95 | + | li %t0 289; |
|
| 96 | + | bne %a7 %t0 @fail; |
|
| 97 | + | li %t0 306; |
|
| 98 | + | bne %s2 %t0 @fail; |
|
| 99 | + | li %t0 323; |
|
| 100 | + | bne %s3 %t0 @fail; |
|
| 101 | + | li %t0 340; |
|
| 102 | + | bne %s4 %t0 @fail; |
|
| 103 | + | li %t0 357; |
|
| 104 | + | bne %s5 %t0 @fail; |
|
| 105 | + | li %t0 374; |
|
| 106 | + | bne %s6 %t0 @fail; |
|
| 107 | + | li %t0 391; |
|
| 108 | + | bne %s7 %t0 @fail; |
|
| 109 | + | li %t0 408; |
|
| 110 | + | bne %s8 %t0 @fail; |
|
| 111 | + | li %t0 425; |
|
| 112 | + | bne %s9 %t0 @fail; |
|
| 113 | + | li %t0 442; |
|
| 114 | + | bne %s10 %t0 @fail; |
|
| 115 | + | li %t0 459; |
|
| 116 | + | bne %s11 %t0 @fail; |
|
| 117 | + | li %t0 476; |
|
| 118 | + | bne %t3 %t0 @fail; |
|
| 119 | + | li %t0 493; |
|
| 120 | + | bne %t4 %t0 @fail; |
|
| 121 | + | li %t0 510; |
|
| 122 | + | bne %t5 %t0 @fail; |
|
| 123 | + | li %t0 527; |
|
| 124 | + | bne %t6 %t0 @fail; |
|
| 125 | + | li %t0 0x80020000; |
|
| 126 | + | bne %s0 %t0 @fail; |
|
| 127 | + | ld %t0 0(%s0); |
|
| 128 | + | bne %sp %t0 @fail; |
|
| 129 | + | ld %t0 64(%s0); |
|
| 130 | + | li %t1 2; |
|
| 131 | + | bne %t0 %t1 @fail; |
|
| 132 | + | la %t0 @user; |
|
| 133 | + | csrw mepc %t0; |
|
| 134 | + | csrw mstatus %zero; |
|
| 135 | + | li %sp 0x80020ff0; |
|
| 136 | + | mret; |
|
| 137 | + | @user |
|
| 138 | + | li %ra 17; |
|
| 139 | + | li %gp 51; |
|
| 140 | + | li %tp 68; |
|
| 141 | + | li %t0 85; |
|
| 142 | + | li %t1 102; |
|
| 143 | + | li %t2 119; |
|
| 144 | + | li %s1 153; |
|
| 145 | + | li %a0 170; |
|
| 146 | + | li %a1 187; |
|
| 147 | + | li %a2 204; |
|
| 148 | + | li %a3 221; |
|
| 149 | + | li %a4 238; |
|
| 150 | + | li %a5 255; |
|
| 151 | + | li %a6 272; |
|
| 152 | + | li %a7 289; |
|
| 153 | + | li %s2 306; |
|
| 154 | + | li %s3 323; |
|
| 155 | + | li %s4 340; |
|
| 156 | + | li %s5 357; |
|
| 157 | + | li %s6 374; |
|
| 158 | + | li %s7 391; |
|
| 159 | + | li %s8 408; |
|
| 160 | + | li %s9 425; |
|
| 161 | + | li %s10 442; |
|
| 162 | + | li %s11 459; |
|
| 163 | + | li %t3 476; |
|
| 164 | + | li %t4 493; |
|
| 165 | + | li %t5 510; |
|
| 166 | + | li %t6 527; |
|
| 167 | + | ecall; |
|
| 168 | + | addi %t0 %t0 -85; |
|
| 169 | + | bnez %t0 @fail; |
|
| 170 | + | li %t0 17; |
|
| 171 | + | bne %ra %t0 @fail; |
|
| 172 | + | li %t0 51; |
|
| 173 | + | bne %gp %t0 @fail; |
|
| 174 | + | li %t0 68; |
|
| 175 | + | bne %tp %t0 @fail; |
|
| 176 | + | li %t0 102; |
|
| 177 | + | bne %t1 %t0 @fail; |
|
| 178 | + | li %t0 119; |
|
| 179 | + | bne %t2 %t0 @fail; |
|
| 180 | + | li %t0 153; |
|
| 181 | + | bne %s1 %t0 @fail; |
|
| 182 | + | li %t0 170; |
|
| 183 | + | bne %a0 %t0 @fail; |
|
| 184 | + | li %t0 187; |
|
| 185 | + | bne %a1 %t0 @fail; |
|
| 186 | + | li %t0 204; |
|
| 187 | + | bne %a2 %t0 @fail; |
|
| 188 | + | li %t0 221; |
|
| 189 | + | bne %a3 %t0 @fail; |
|
| 190 | + | li %t0 238; |
|
| 191 | + | bne %a4 %t0 @fail; |
|
| 192 | + | li %t0 255; |
|
| 193 | + | bne %a5 %t0 @fail; |
|
| 194 | + | li %t0 272; |
|
| 195 | + | bne %a6 %t0 @fail; |
|
| 196 | + | li %t0 289; |
|
| 197 | + | bne %a7 %t0 @fail; |
|
| 198 | + | li %t0 306; |
|
| 199 | + | bne %s2 %t0 @fail; |
|
| 200 | + | li %t0 323; |
|
| 201 | + | bne %s3 %t0 @fail; |
|
| 202 | + | li %t0 340; |
|
| 203 | + | bne %s4 %t0 @fail; |
|
| 204 | + | li %t0 357; |
|
| 205 | + | bne %s5 %t0 @fail; |
|
| 206 | + | li %t0 374; |
|
| 207 | + | bne %s6 %t0 @fail; |
|
| 208 | + | li %t0 391; |
|
| 209 | + | bne %s7 %t0 @fail; |
|
| 210 | + | li %t0 408; |
|
| 211 | + | bne %s8 %t0 @fail; |
|
| 212 | + | li %t0 425; |
|
| 213 | + | bne %s9 %t0 @fail; |
|
| 214 | + | li %t0 442; |
|
| 215 | + | bne %s10 %t0 @fail; |
|
| 216 | + | li %t0 459; |
|
| 217 | + | bne %s11 %t0 @fail; |
|
| 218 | + | li %t0 476; |
|
| 219 | + | bne %t3 %t0 @fail; |
|
| 220 | + | li %t0 493; |
|
| 221 | + | bne %t4 %t0 @fail; |
|
| 222 | + | li %t0 510; |
|
| 223 | + | bne %t5 %t0 @fail; |
|
| 224 | + | li %t0 527; |
|
| 225 | + | bne %t6 %t0 @fail; |
|
| 226 | + | li %t0 0x80020000; |
|
| 227 | + | bne %s0 %t0 @fail; |
|
| 228 | + | li %t0 0x80020ff0; |
|
| 229 | + | bne %sp %t0 @fail; |
|
| 230 | + | li %t0 85; |
|
| 231 | + | @illegalInstruction |
|
| 232 | + | csrr %t0 mhartid; |
|
| 233 | + | addi %t0 %t0 -85; |
|
| 234 | + | bnez %t0 @fail; |
|
| 235 | + | li %a7 1234; |
|
| 236 | + | ecall; |
|
| 237 | + | j @fail; |
|
| 238 | + | @success |
|
| 239 | + | li %t0 TRAP_TEST_FINISH_ADDRESS; |
|
| 240 | + | li %t1 TRAP_TEST_FINISH_PASS; |
|
| 241 | + | sw %t1 0(%t0); |
|
| 242 | + | @fail |
|
| 243 | + | li %t0 TRAP_TEST_FINISH_ADDRESS; |
|
| 244 | + | li %t1 TRAP_TEST_FINISH_FAIL; |
|
| 245 | + | sw %t1 0(%t0); |
|
| 246 | + | @park |
|
| 247 | + | wfi; |
|
| 248 | + | j @park; |
|
| 249 | + | ||
| 250 | + | // Validate stack/base switching before enabling a nested timer interrupt. |
|
| 251 | + | @handler |
|
| 252 | + | li %t0 0x12345678; |
|
| 253 | + | bne %gp %t0 @fail; |
|
| 254 | + | li %t0 0x80020000; |
|
| 255 | + | bne %tp %t0 @fail; |
|
| 256 | + | bne %a1 %tp @fail; |
|
| 257 | + | bne %a0 %sp @fail; |
|
| 258 | + | ld %t0 272(%a0); |
|
| 259 | + | li %t1 1; |
|
| 260 | + | slli %t1 %t1 63; |
|
| 261 | + | ori %t1 %t1 TRAP_TEST_TIMER_IRQ; |
|
| 262 | + | beq %t0 %t1 @timer; |
|
| 263 | + | li %t1 TRAP_TEST_USER_CALL; |
|
| 264 | + | beq %t0 %t1 @userCall; |
|
| 265 | + | li %t1 TRAP_TEST_ILLEGAL_INSTRUCTION; |
|
| 266 | + | beq %t0 %t1 @illegal; |
|
| 267 | + | li %t1 TRAP_TEST_MACHINE_CALL; |
|
| 268 | + | bne %t0 %t1 @fail; |
|
| 269 | + | ld %t0 64(%tp); |
|
| 270 | + | bnez %t0 @fail; |
|
| 271 | + | li %t0 1; |
|
| 272 | + | sd %t0 64(%tp); |
|
| 273 | + | addi %sp %sp -16; |
|
| 274 | + | sd %ra 0(%sp); |
|
| 275 | + | sd %a0 8(%sp); |
|
| 276 | + | li %t0 TRAP_TEST_TIMER_COMPARE; |
|
| 277 | + | sd %zero 0(%t0); |
|
| 278 | + | li %t0 TRAP_TEST_TIMER_ENABLE; |
|
| 279 | + | csrw mie %t0; |
|
| 280 | + | li %s10 0; |
|
| 281 | + | csrsi mstatus TRAP_TEST_MIE; |
|
| 282 | + | @awaitTimer |
|
| 283 | + | addi %s10 %s10 1; |
|
| 284 | + | ld %t0 64(%tp); |
|
| 285 | + | li %t1 2; |
|
| 286 | + | bne %t0 %t1 @awaitTimer; |
|
| 287 | + | li %t0 1; |
|
| 288 | + | bne %s10 %t0 @fail; |
|
| 289 | + | csrr %t0 mstatus; |
|
| 290 | + | andi %t0 %t0 TRAP_TEST_MIE; |
|
| 291 | + | beqz %t0 @fail; |
|
| 292 | + | csrci mstatus TRAP_TEST_MIE; |
|
| 293 | + | csrw mie %zero; |
|
| 294 | + | ld %ra 0(%sp); |
|
| 295 | + | ld %a0 8(%sp); |
|
| 296 | + | addi %sp %sp 16; |
|
| 297 | + | j @advance; |
|
| 298 | + | @timer |
|
| 299 | + | ld %t0 256(%a0); |
|
| 300 | + | la %t1 @awaitTimer; |
|
| 301 | + | bne %t0 %t1 @fail; |
|
| 302 | + | ld %t0 264(%a0); |
|
| 303 | + | li %t1 TRAP_TEST_MPP_MACHINE; |
|
| 304 | + | and %t0 %t0 %t1; |
|
| 305 | + | bne %t0 %t1 @fail; |
|
| 306 | + | ld %t0 16(%a0); |
|
| 307 | + | addi %t1 %a0 288; |
|
| 308 | + | bne %t0 %t1 @fail; |
|
| 309 | + | li %t0 TRAP_TEST_TIMER_COMPARE; |
|
| 310 | + | li %t1 -1; |
|
| 311 | + | sd %t1 0(%t0); |
|
| 312 | + | li %t0 2; |
|
| 313 | + | sd %t0 64(%tp); |
|
| 314 | + | ret; |
|
| 315 | + | @illegal |
|
| 316 | + | ld %t0 256(%a0); |
|
| 317 | + | la %t1 @illegalInstruction; |
|
| 318 | + | bne %t0 %t1 @fail; |
|
| 319 | + | ld %t0 280(%a0); |
|
| 320 | + | li %t1 0xf14022f3; |
|
| 321 | + | bne %t0 %t1 @fail; |
|
| 322 | + | li %t0 1; |
|
| 323 | + | sd %t0 72(%tp); |
|
| 324 | + | j @advance; |
|
| 325 | + | @userCall |
|
| 326 | + | ld %t0 136(%a0); |
|
| 327 | + | li %t1 1234; |
|
| 328 | + | bne %t0 %t1 @checkUserFrame; |
|
| 329 | + | ld %t0 72(%tp); |
|
| 330 | + | li %t1 1; |
|
| 331 | + | bne %t0 %t1 @fail; |
|
| 332 | + | j @success; |
|
| 333 | + | @checkUserFrame |
|
| 334 | + | ld %t0 264(%a0); |
|
| 335 | + | li %t1 TRAP_TEST_MPP_MACHINE; |
|
| 336 | + | and %t0 %t0 %t1; |
|
| 337 | + | bnez %t0 @fail; |
|
| 338 | + | ld %t0 0(%tp); |
|
| 339 | + | addi %t0 %t0 -288; |
|
| 340 | + | bne %a0 %t0 @fail; |
|
| 341 | + | @advance |
|
| 342 | + | ld %t0 256(%a0); |
|
| 343 | + | addi %t0 %t0 4; |
|
| 344 | + | sd %t0 256(%a0); |
|
| 345 | + | ret; |
test/trap/reservations.ras
added
+112 -0
| 1 | + | //! Reservation isolation at trap entry, frame resume, and user context switches. |
|
| 2 | + | // Machine completion device address. |
|
| 3 | + | .constant RESERVATION_TEST_FINISH_ADDRESS 0x10001000; |
|
| 4 | + | // Successful machine completion value. |
|
| 5 | + | .constant RESERVATION_TEST_FINISH_PASS 0x5555; |
|
| 6 | + | // Failed machine completion value. |
|
| 7 | + | .constant RESERVATION_TEST_FINISH_FAIL 0x13333; |
|
| 8 | + | // Previous machine privilege field in mstatus. |
|
| 9 | + | .constant RESERVATION_TEST_MPP_MACHINE 0x1800; |
|
| 10 | + | // Machine environment call exception code. |
|
| 11 | + | .constant RESERVATION_TEST_MACHINE_CALL 11; |
|
| 12 | + | // User environment call exception code. |
|
| 13 | + | .constant RESERVATION_TEST_USER_CALL 8; |
|
| 14 | + | ||
| 15 | + | .text; |
|
| 16 | + | li %s0 0x80020000; |
|
| 17 | + | sd %sp 0(%s0); |
|
| 18 | + | li %t0 4096; |
|
| 19 | + | sub %t0 %sp %t0; |
|
| 20 | + | sd %t0 8(%s0); |
|
| 21 | + | sd %zero 16(%s0); |
|
| 22 | + | la %t0 @handler; |
|
| 23 | + | sd %t0 24(%s0); |
|
| 24 | + | mv %a0 %s0; |
|
| 25 | + | call @kernel::trap::install; |
|
| 26 | + | addi %s1 %s0 80; |
|
| 27 | + | ||
| 28 | + | // Enter the vector with a live reservation and the machine trap CSR state. |
|
| 29 | + | la %t0 @resumed; |
|
| 30 | + | csrw mepc %t0; |
|
| 31 | + | li %t0 RESERVATION_TEST_MACHINE_CALL; |
|
| 32 | + | csrw mcause %t0; |
|
| 33 | + | li %t0 RESERVATION_TEST_MPP_MACHINE; |
|
| 34 | + | csrw mstatus %t0; |
|
| 35 | + | lr.d %t0 (%s1); |
|
| 36 | + | j @kernel::trap::vector; |
|
| 37 | + | @resumed |
|
| 38 | + | sc.d %t0 %zero (%s1); |
|
| 39 | + | beqz %t0 @fail; |
|
| 40 | + | ld %t0 0(%s1); |
|
| 41 | + | bnez %t0 @fail; |
|
| 42 | + | li %t0 2; |
|
| 43 | + | sd %t0 88(%s0); |
|
| 44 | + | la %t0 @first; |
|
| 45 | + | csrw mepc %t0; |
|
| 46 | + | csrw mstatus %zero; |
|
| 47 | + | mret; |
|
| 48 | + | ||
| 49 | + | // The first user context retains its loaded value across a peer's execution. |
|
| 50 | + | @first |
|
| 51 | + | lr.d %a2 (%s1); |
|
| 52 | + | ecall; |
|
| 53 | + | @firstResume |
|
| 54 | + | sc.d %a3 %a2 (%s1); |
|
| 55 | + | beqz %a3 @fail; |
|
| 56 | + | ld %t0 0(%s1); |
|
| 57 | + | li %t1 1; |
|
| 58 | + | bne %t0 %t1 @fail; |
|
| 59 | + | ecall; |
|
| 60 | + | j @fail; |
|
| 61 | + | ||
| 62 | + | // The peer changes shared memory and reserves the same location before exit. |
|
| 63 | + | @peer |
|
| 64 | + | li %t0 1; |
|
| 65 | + | sd %t0 0(%s1); |
|
| 66 | + | lr.d %t0 (%s1); |
|
| 67 | + | ecall; |
|
| 68 | + | j @fail; |
|
| 69 | + | ||
| 70 | + | // Inject a live return reservation after trap entry has completed. |
|
| 71 | + | @handler |
|
| 72 | + | ld %t0 88(%s0); |
|
| 73 | + | beqz %t0 @entryCheck; |
|
| 74 | + | ld %t1 272(%a0); |
|
| 75 | + | li %t2 RESERVATION_TEST_USER_CALL; |
|
| 76 | + | bne %t1 %t2 @fail; |
|
| 77 | + | li %t1 2; |
|
| 78 | + | beq %t0 %t1 @switchPeer; |
|
| 79 | + | li %t1 3; |
|
| 80 | + | beq %t0 %t1 @switchFirst; |
|
| 81 | + | li %t1 4; |
|
| 82 | + | bne %t0 %t1 @fail; |
|
| 83 | + | j @success; |
|
| 84 | + | @entryCheck |
|
| 85 | + | sc.d %t0 %zero (%s1); |
|
| 86 | + | beqz %t0 @fail; |
|
| 87 | + | li %t0 1; |
|
| 88 | + | sd %t0 88(%s0); |
|
| 89 | + | lr.d %t0 (%s1); |
|
| 90 | + | ret; |
|
| 91 | + | @switchPeer |
|
| 92 | + | li %t0 3; |
|
| 93 | + | sd %t0 88(%s0); |
|
| 94 | + | la %t0 @peer; |
|
| 95 | + | sd %t0 256(%a0); |
|
| 96 | + | ret; |
|
| 97 | + | @switchFirst |
|
| 98 | + | li %t0 4; |
|
| 99 | + | sd %t0 88(%s0); |
|
| 100 | + | la %t0 @firstResume; |
|
| 101 | + | sd %t0 256(%a0); |
|
| 102 | + | lr.d %t0 (%s1); |
|
| 103 | + | ret; |
|
| 104 | + | @success |
|
| 105 | + | li %t0 RESERVATION_TEST_FINISH_ADDRESS; |
|
| 106 | + | li %t1 RESERVATION_TEST_FINISH_PASS; |
|
| 107 | + | sw %t1 0(%t0); |
|
| 108 | + | @fail |
|
| 109 | + | li %t0 RESERVATION_TEST_FINISH_ADDRESS; |
|
| 110 | + | li %t1 RESERVATION_TEST_FINISH_FAIL; |
|
| 111 | + | sw %t1 0(%t0); |
|
| 112 | + | ebreak; |
test/trap/run
added
+13 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Execute the production trap boundary with a deterministic nested timer. |
|
| 3 | + | set -eu |
|
| 4 | + | emulator=${RAD_EMULATOR:-emulator} |
|
| 5 | + | work=$(mktemp -d) |
|
| 6 | + | trap 'rm -rf "$work"' EXIT HUP INT TERM |
|
| 7 | + | for fixture in machine reservations; do |
|
| 8 | + | cat "test/trap/$fixture.ras" kernel/kernel/trap.ras > "$work/trap.ras" |
|
| 9 | + | "$emulator" -run test/sync/build.rv64 -- "$work/trap.ras" "$work/trap.rv64" |
|
| 10 | + | "$emulator" -machine -max-steps=1000000 -run "$work/trap.rv64" |
|
| 11 | + | done |
|
| 12 | + | printf 'trap contexts: machine/user registers, nested timer, illegal instruction, and stacks passed\n' |
|
| 13 | + | printf 'trap reservations: entry, resume, and user context isolation passed\n' |